blob: d77e59dbd076a20458f5506e071dacef45627180 [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'oefc6f622008-08-27 23:07:54 -04007 * Includes support for Windows NT support under Cygwin.
Theodore Ts'offf45482003-04-13 00:44:19 -04008 *
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;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050071 ext2_loff_t offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000072 struct unix_cache cache[CACHE_SIZE];
Theodore Ts'o6d96b002007-08-03 20:07:09 -040073 struct struct_io_stats io_stats;
Theodore Ts'o3839e651997-04-26 13:21:57 +000074};
75
76static errcode_t unix_open(const char *name, int flags, io_channel *channel);
77static errcode_t unix_close(io_channel channel);
78static errcode_t unix_set_blksize(io_channel channel, int blksize);
79static errcode_t unix_read_blk(io_channel channel, unsigned long block,
80 int count, void *data);
81static errcode_t unix_write_blk(io_channel channel, unsigned long block,
82 int count, const void *data);
83static errcode_t unix_flush(io_channel channel);
Theodore Ts'oc180ac82000-10-26 20:24:43 +000084static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
85 int size, const void *data);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040086static errcode_t unix_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050087 const char *arg);
Theodore Ts'o6d96b002007-08-03 20:07:09 -040088static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
89;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -050090static void reuse_cache(io_channel channel, struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -060091 struct unix_cache *cache, unsigned long long block);
92static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
93 int count, void *data);
94static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
95 int count, const void *data);
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -050096
Matthias Andree289e0552004-03-30 03:57:41 +020097/* __FreeBSD_kernel__ is defined by GNU/kFreeBSD - the FreeBSD kernel
98 * does not know buffered block devices - everything is raw. */
99#if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100100#define NEED_BOUNCE_BUFFER
101#else
102#undef NEED_BOUNCE_BUFFER
103#endif
104
Theodore Ts'of3db3561997-04-26 13:34:30 +0000105static struct struct_io_manager struct_unix_manager = {
106 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107 "Unix I/O Manager",
108 unix_open,
109 unix_close,
110 unix_set_blksize,
111 unix_read_blk,
112 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000113 unix_flush,
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100114#ifdef NEED_BOUNCE_BUFFER
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500115 0,
Theodore Ts'offf45482003-04-13 00:44:19 -0400116#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500117 unix_write_byte,
Theodore Ts'offf45482003-04-13 00:44:19 -0400118#endif
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400119 unix_set_option,
120 unix_get_stats,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600121 unix_read_blk64,
122 unix_write_blk64,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000123};
124
125io_manager unix_io_manager = &struct_unix_manager;
126
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400127static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
128{
129 errcode_t retval = 0;
130
131 struct unix_private_data *data;
132
133 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
134 data = (struct unix_private_data *) channel->private_data;
135 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
136
137 if (stats)
138 *stats = &data->io_stats;
139
140 return retval;
141}
142
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000143/*
144 * Here are the raw I/O functions
145 */
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100146#ifndef NEED_BOUNCE_BUFFER
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000147static errcode_t raw_read_blk(io_channel channel,
148 struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600149 unsigned long long block,
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000150 int count, void *buf)
151{
152 errcode_t retval;
Theodore Ts'o54434922003-12-07 01:28:50 -0500153 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000154 ext2_loff_t location;
155 int actual = 0;
156
157 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400158 data->io_stats.bytes_read += size;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500159 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000160 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
161 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
162 goto error_out;
163 }
164 actual = read(data->dev, buf, size);
165 if (actual != size) {
166 if (actual < 0)
167 actual = 0;
168 retval = EXT2_ET_SHORT_READ;
169 goto error_out;
170 }
171 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400172
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000173error_out:
174 memset((char *) buf+actual, 0, size-actual);
175 if (channel->read_error)
176 retval = (channel->read_error)(channel, block, count, buf,
177 size, actual, retval);
178 return retval;
179}
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100180#else /* NEED_BOUNCE_BUFFER */
Theodore Ts'offf45482003-04-13 00:44:19 -0400181/*
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100182 * Windows and FreeBSD block devices only allow sector alignment IO in offset and size
Theodore Ts'offf45482003-04-13 00:44:19 -0400183 */
184static errcode_t raw_read_blk(io_channel channel,
185 struct unix_private_data *data,
186 unsigned long block,
187 int count, void *buf)
188{
189 errcode_t retval;
190 size_t size, alignsize, fragment;
191 ext2_loff_t location;
192 int total = 0, actual;
193#define BLOCKALIGN 512
194 char sector[BLOCKALIGN];
195
196 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400197 data->io_stats.bytes_read += size;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500198 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'offf45482003-04-13 00:44:19 -0400199#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400200 printf("count=%d, size=%d, block=%lu, blk_size=%d, location=%llx\n",
201 count, size, block, channel->block_size, (long long)location);
Theodore Ts'offf45482003-04-13 00:44:19 -0400202#endif
203 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
204 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
205 goto error_out;
206 }
207 fragment = size % BLOCKALIGN;
208 alignsize = size - fragment;
209 if (alignsize) {
210 actual = read(data->dev, buf, alignsize);
211 if (actual != alignsize)
212 goto short_read;
213 }
214 if (fragment) {
215 actual = read(data->dev, sector, BLOCKALIGN);
216 if (actual != BLOCKALIGN)
217 goto short_read;
218 memcpy(buf+alignsize, sector, fragment);
219 }
220 return 0;
221
222short_read:
223 if (actual>0)
224 total += actual;
225 retval = EXT2_ET_SHORT_READ;
226
227error_out:
228 memset((char *) buf+total, 0, size-actual);
229 if (channel->read_error)
230 retval = (channel->read_error)(channel, block, count, buf,
231 size, actual, retval);
232 return retval;
233}
234#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000235
236static errcode_t raw_write_blk(io_channel channel,
237 struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600238 unsigned long long block,
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000239 int count, const void *buf)
240{
Theodore Ts'o54434922003-12-07 01:28:50 -0500241 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000242 ext2_loff_t location;
243 int actual = 0;
244 errcode_t retval;
245
246 if (count == 1)
247 size = channel->block_size;
248 else {
249 if (count < 0)
250 size = -count;
251 else
252 size = count * channel->block_size;
253 }
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400254 data->io_stats.bytes_written += size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000255
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500256 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000257 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
258 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
259 goto error_out;
260 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400261
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000262 actual = write(data->dev, buf, size);
263 if (actual != size) {
264 retval = EXT2_ET_SHORT_WRITE;
265 goto error_out;
266 }
267 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400268
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000269error_out:
270 if (channel->write_error)
271 retval = (channel->write_error)(channel, block, count, buf,
272 size, actual, retval);
273 return retval;
274}
275
276
277/*
278 * Here we implement the cache functions
279 */
280
281/* Allocate the cache buffers */
282static errcode_t alloc_cache(io_channel channel,
283 struct unix_private_data *data)
284{
285 errcode_t retval;
286 struct unix_cache *cache;
287 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400288
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000289 data->access_time = 0;
290 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
291 cache->block = 0;
292 cache->access_time = 0;
293 cache->dirty = 0;
294 cache->in_use = 0;
295 if ((retval = ext2fs_get_mem(channel->block_size,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400296 &cache->buf)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000297 return retval;
298 }
299 return 0;
300}
301
302/* Free the cache buffers */
Theodore Ts'o54434922003-12-07 01:28:50 -0500303static void free_cache(struct unix_private_data *data)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000304{
305 struct unix_cache *cache;
306 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400307
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000308 data->access_time = 0;
309 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
310 cache->block = 0;
311 cache->access_time = 0;
312 cache->dirty = 0;
313 cache->in_use = 0;
314 if (cache->buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400315 ext2fs_free_mem(&cache->buf);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000316 cache->buf = 0;
317 }
318}
319
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400320#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000321/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500322 * Try to find a block in the cache. If the block is not found, and
323 * eldest is a non-zero pointer, then fill in eldest with the cache
324 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000325 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500326static struct unix_cache *find_cached_block(struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600327 unsigned long long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500328 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000329{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000330 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000331 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400332
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000333 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000334 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
335 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500336 if (!unused_cache)
337 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000338 continue;
339 }
340 if (cache->block == block) {
341 cache->access_time = ++data->access_time;
342 return cache;
343 }
344 if (!oldest_cache ||
345 (cache->access_time < oldest_cache->access_time))
346 oldest_cache = cache;
347 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500348 if (eldest)
349 *eldest = (unused_cache) ? unused_cache : oldest_cache;
350 return 0;
351}
352
353/*
354 * Reuse a particular cache entry for another block.
355 */
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500356static void reuse_cache(io_channel channel, struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600357 struct unix_cache *cache, unsigned long long block)
Theodore Ts'o82c46602002-11-09 14:56:17 -0500358{
359 if (cache->dirty && cache->in_use)
360 raw_write_blk(channel, data, cache->block, 1, cache->buf);
361
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000362 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500363 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000364 cache->block = block;
365 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000366}
367
368/*
369 * Flush all of the blocks in the cache
370 */
371static errcode_t flush_cached_blocks(io_channel channel,
372 struct unix_private_data *data,
373 int invalidate)
374
375{
376 struct unix_cache *cache;
377 errcode_t retval, retval2;
378 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400379
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000380 retval2 = 0;
381 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
382 if (!cache->in_use)
383 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400384
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000385 if (invalidate)
386 cache->in_use = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400387
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000388 if (!cache->dirty)
389 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400390
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000391 retval = raw_write_blk(channel, data,
392 cache->block, 1, cache->buf);
393 if (retval)
394 retval2 = retval;
395 else
396 cache->dirty = 0;
397 }
398 return retval2;
399}
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400400#endif /* NO_IO_CACHE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000401
Theodore Ts'o3839e651997-04-26 13:21:57 +0000402static errcode_t unix_open(const char *name, int flags, io_channel *channel)
403{
404 io_channel io = NULL;
405 struct unix_private_data *data = NULL;
406 errcode_t retval;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000407 int open_flags;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500408 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400409#ifdef __linux__
410 struct utsname ut;
411#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000412
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000413 if (name == 0)
414 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400415 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000416 if (retval)
417 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000418 memset(io, 0, sizeof(struct struct_io_channel));
419 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400420 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000421 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000422 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000423
Theodore Ts'o3839e651997-04-26 13:21:57 +0000424 io->manager = unix_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400425 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000426 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000427 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000428
Theodore Ts'o3839e651997-04-26 13:21:57 +0000429 strcpy(io->name, name);
430 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000431 io->block_size = 1024;
432 io->read_error = 0;
433 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000434 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000435
436 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000437 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400438 data->io_stats.num_fields = 2;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000439
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000440 if ((retval = alloc_cache(io, data)))
441 goto cleanup;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500442
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000443 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
Theodore Ts'ofa6c6532006-03-18 18:57:44 -0500444 if (flags & IO_FLAG_EXCLUSIVE)
445 open_flags |= O_EXCL;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000446#ifdef HAVE_OPEN64
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500447 data->dev = open64(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000448#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500449 data->dev = open(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000450#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000451 if (data->dev < 0) {
452 retval = errno;
453 goto cleanup;
454 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500455
456#ifdef __linux__
457#undef RLIM_INFINITY
458#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
459#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
460#else
461#define RLIM_INFINITY (~0UL)
462#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500463 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400464 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
465 * block devices are wrongly getting hit by the filesize
466 * limit. This workaround isn't perfect, since it won't work
467 * if glibc wasn't built against 2.2 header files. (Sigh.)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400468 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500469 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400470 if ((flags & IO_FLAG_RW) &&
471 (uname(&ut) == 0) &&
472 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
473 (ut.release[2] == '4') && (ut.release[3] == '.') &&
474 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
475 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500476 (fstat(data->dev, &st) == 0) &&
477 (S_ISBLK(st.st_mode))) {
478 struct rlimit rlim;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400479
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500480 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500481 setrlimit(RLIMIT_FSIZE, &rlim);
482 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100483 if (((unsigned long) rlim.rlim_cur) <
484 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500485 rlim.rlim_cur = rlim.rlim_max;
486 setrlimit(RLIMIT_FSIZE, &rlim);
487 }
488 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500489#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490 *channel = io;
491 return 0;
492
493cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000494 if (data) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500495 free_cache(data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400496 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000497 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000498 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400499 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000500 return retval;
501}
502
503static errcode_t unix_close(io_channel channel)
504{
505 struct unix_private_data *data;
506 errcode_t retval = 0;
507
Theodore Ts'of3db3561997-04-26 13:34:30 +0000508 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000510 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000511
512 if (--channel->refcount > 0)
513 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000514
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400515#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000516 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400517#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000518
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519 if (close(data->dev) < 0)
520 retval = errno;
Theodore Ts'o54434922003-12-07 01:28:50 -0500521 free_cache(data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500522
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400523 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400525 ext2fs_free_mem(&channel->name);
526 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527 return retval;
528}
529
530static errcode_t unix_set_blksize(io_channel channel, int blksize)
531{
532 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000533 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534
Theodore Ts'of3db3561997-04-26 13:34:30 +0000535 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000537 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
538
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400540#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000541 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000542 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400543#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400544
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000545 channel->block_size = blksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500546 free_cache(data);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000547 if ((retval = alloc_cache(channel, data)))
548 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549 }
550 return 0;
551}
552
553
Jose R. Santos59ecd322008-03-03 10:41:24 -0600554static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 int count, void *buf)
556{
557 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500558 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000559 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000560 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000561 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000562
Theodore Ts'of3db3561997-04-26 13:34:30 +0000563 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000564 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000565 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000566
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400567#ifdef NO_IO_CACHE
568 return raw_read_blk(channel, data, block, count, buf);
569#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000570 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500571 * If we're doing an odd-sized read or a very large read,
572 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000573 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500574 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000575 if ((retval = flush_cached_blocks(channel, data, 0)))
576 return retval;
577 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000578 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000579
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000580 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000581 while (count > 0) {
582 /* If it's in the cache, use it! */
Theodore Ts'o54434922003-12-07 01:28:50 -0500583 if ((cache = find_cached_block(data, block, &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000584#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400585 printf("Using cached block %lu\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000586#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000587 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000588 count--;
589 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000590 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000591 continue;
592 }
593 /*
594 * Find the number of uncached blocks so we can do a
595 * single read request
596 */
597 for (i=1; i < count; i++)
Theodore Ts'o54434922003-12-07 01:28:50 -0500598 if (find_cached_block(data, block+i, &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000599 break;
600#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400601 printf("Reading %d blocks starting at %lu\n", i, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000602#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000603 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000604 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400605
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000606 /* Save the results in the cache */
607 for (j=0; j < i; j++) {
608 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500609 cache = reuse[j];
610 reuse_cache(channel, data, cache, block++);
611 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000612 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000613 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000614 }
615 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400616#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000617}
618
Jose R. Santos59ecd322008-03-03 10:41:24 -0600619static errcode_t unix_read_blk(io_channel channel, unsigned long block,
620 int count, void *buf)
621{
622 return unix_read_blk64(channel, block, count, buf);
623}
624
625static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626 int count, const void *buf)
627{
628 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500629 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500630 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000631 const char *cp;
632 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000633
Theodore Ts'of3db3561997-04-26 13:34:30 +0000634 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000635 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000636 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000637
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400638#ifdef NO_IO_CACHE
639 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400640#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000641 /*
642 * If we're doing an odd-sized write or a very large write,
643 * flush out the cache completely and then do a direct write.
644 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500645 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000646 if ((retval = flush_cached_blocks(channel, data, 1)))
647 return retval;
648 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000649 }
650
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000651 /*
652 * For a moderate-sized multi-block write, first force a write
653 * if we're in write-through cache mode, and then fill the
654 * cache with the blocks.
655 */
656 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
657 if (writethrough)
658 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400659
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000660 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000661 while (count > 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500662 cache = find_cached_block(data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000663 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500664 cache = reuse;
665 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000666 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500667 memcpy(cache->buf, cp, channel->block_size);
668 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000669 count--;
670 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000671 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000672 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000673 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400674#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000675}
676
Jose R. Santos59ecd322008-03-03 10:41:24 -0600677static errcode_t unix_write_blk(io_channel channel, unsigned long block,
678 int count, const void *buf)
679{
680 return unix_write_blk64(channel, block, count, buf);
681}
682
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000683static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
684 int size, const void *buf)
685{
686 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000687 errcode_t retval = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500688 ssize_t actual;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000689
690 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
691 data = (struct unix_private_data *) channel->private_data;
692 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
693
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400694#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000695 /*
696 * Flush out the cache completely
697 */
698 if ((retval = flush_cached_blocks(channel, data, 1)))
699 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400700#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000701
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500702 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000703 return errno;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400704
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000705 actual = write(data->dev, buf, size);
706 if (actual != size)
707 return EXT2_ET_SHORT_WRITE;
708
709 return 0;
710}
711
Theodore Ts'o3839e651997-04-26 13:21:57 +0000712/*
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400713 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000714 */
715static errcode_t unix_flush(io_channel channel)
716{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000717 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000718 errcode_t retval = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400719
Theodore Ts'of3db3561997-04-26 13:34:30 +0000720 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
721 data = (struct unix_private_data *) channel->private_data;
722 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000723
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400724#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000725 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400726#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000727 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000728 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000729}
730
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400731static errcode_t unix_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500732 const char *arg)
733{
734 struct unix_private_data *data;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500735 unsigned long long tmp;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500736 char *end;
737
738 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
739 data = (struct unix_private_data *) channel->private_data;
740 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
741
742 if (!strcmp(option, "offset")) {
743 if (!arg)
744 return EXT2_ET_INVALID_ARGUMENT;
745
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500746 tmp = strtoull(arg, &end, 0);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500747 if (*end)
748 return EXT2_ET_INVALID_ARGUMENT;
749 data->offset = tmp;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500750 if (data->offset < 0)
751 return EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500752 return 0;
753 }
754 return EXT2_ET_INVALID_ARGUMENT;
755}