blob: 8497a415a1ffb8f33929e9b8d62468eca7acb8b5 [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;
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'o2e8ca9a2004-11-30 14:07:11 -050086static errcode_t unix_set_option(io_channel channel, const char *option,
87 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,
91 struct unix_cache *cache, unsigned long block);
92
Matthias Andree289e0552004-03-30 03:57:41 +020093/* __FreeBSD_kernel__ is defined by GNU/kFreeBSD - the FreeBSD kernel
94 * does not know buffered block devices - everything is raw. */
95#if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
Matthias Andreeb34cbdd2003-12-28 18:21:26 +010096#define NEED_BOUNCE_BUFFER
97#else
98#undef NEED_BOUNCE_BUFFER
99#endif
100
Theodore Ts'of3db3561997-04-26 13:34:30 +0000101static struct struct_io_manager struct_unix_manager = {
102 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 "Unix I/O Manager",
104 unix_open,
105 unix_close,
106 unix_set_blksize,
107 unix_read_blk,
108 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000109 unix_flush,
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100110#ifdef NEED_BOUNCE_BUFFER
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500111 0,
Theodore Ts'offf45482003-04-13 00:44:19 -0400112#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500113 unix_write_byte,
Theodore Ts'offf45482003-04-13 00:44:19 -0400114#endif
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400115 unix_set_option,
116 unix_get_stats,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000117};
118
119io_manager unix_io_manager = &struct_unix_manager;
120
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400121static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
122{
123 errcode_t retval = 0;
124
125 struct unix_private_data *data;
126
127 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
128 data = (struct unix_private_data *) channel->private_data;
129 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
130
131 if (stats)
132 *stats = &data->io_stats;
133
134 return retval;
135}
136
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000137/*
138 * Here are the raw I/O functions
139 */
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100140#ifndef NEED_BOUNCE_BUFFER
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000141static errcode_t raw_read_blk(io_channel channel,
142 struct unix_private_data *data,
143 unsigned long block,
144 int count, void *buf)
145{
146 errcode_t retval;
Theodore Ts'o54434922003-12-07 01:28:50 -0500147 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000148 ext2_loff_t location;
149 int actual = 0;
150
151 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400152 data->io_stats.bytes_read += size;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500153 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000154 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
155 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
156 goto error_out;
157 }
158 actual = read(data->dev, buf, size);
159 if (actual != size) {
160 if (actual < 0)
161 actual = 0;
162 retval = EXT2_ET_SHORT_READ;
163 goto error_out;
164 }
165 return 0;
166
167error_out:
168 memset((char *) buf+actual, 0, size-actual);
169 if (channel->read_error)
170 retval = (channel->read_error)(channel, block, count, buf,
171 size, actual, retval);
172 return retval;
173}
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100174#else /* NEED_BOUNCE_BUFFER */
Theodore Ts'offf45482003-04-13 00:44:19 -0400175/*
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100176 * Windows and FreeBSD block devices only allow sector alignment IO in offset and size
Theodore Ts'offf45482003-04-13 00:44:19 -0400177 */
178static errcode_t raw_read_blk(io_channel channel,
179 struct unix_private_data *data,
180 unsigned long block,
181 int count, void *buf)
182{
183 errcode_t retval;
184 size_t size, alignsize, fragment;
185 ext2_loff_t location;
186 int total = 0, actual;
187#define BLOCKALIGN 512
188 char sector[BLOCKALIGN];
189
190 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400191 data->io_stats.bytes_read += size;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500192 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'offf45482003-04-13 00:44:19 -0400193#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400194 printf("count=%d, size=%d, block=%lu, blk_size=%d, location=%llx\n",
195 count, size, block, channel->block_size, (long long)location);
Theodore Ts'offf45482003-04-13 00:44:19 -0400196#endif
197 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
198 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
199 goto error_out;
200 }
201 fragment = size % BLOCKALIGN;
202 alignsize = size - fragment;
203 if (alignsize) {
204 actual = read(data->dev, buf, alignsize);
205 if (actual != alignsize)
206 goto short_read;
207 }
208 if (fragment) {
209 actual = read(data->dev, sector, BLOCKALIGN);
210 if (actual != BLOCKALIGN)
211 goto short_read;
212 memcpy(buf+alignsize, sector, fragment);
213 }
214 return 0;
215
216short_read:
217 if (actual>0)
218 total += actual;
219 retval = EXT2_ET_SHORT_READ;
220
221error_out:
222 memset((char *) buf+total, 0, size-actual);
223 if (channel->read_error)
224 retval = (channel->read_error)(channel, block, count, buf,
225 size, actual, retval);
226 return retval;
227}
228#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000229
230static errcode_t raw_write_blk(io_channel channel,
231 struct unix_private_data *data,
232 unsigned long block,
233 int count, const void *buf)
234{
Theodore Ts'o54434922003-12-07 01:28:50 -0500235 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000236 ext2_loff_t location;
237 int actual = 0;
238 errcode_t retval;
239
240 if (count == 1)
241 size = channel->block_size;
242 else {
243 if (count < 0)
244 size = -count;
245 else
246 size = count * channel->block_size;
247 }
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400248 data->io_stats.bytes_written += size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000249
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500250 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000251 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
252 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
253 goto error_out;
254 }
255
256 actual = write(data->dev, buf, size);
257 if (actual != size) {
258 retval = EXT2_ET_SHORT_WRITE;
259 goto error_out;
260 }
261 return 0;
262
263error_out:
264 if (channel->write_error)
265 retval = (channel->write_error)(channel, block, count, buf,
266 size, actual, retval);
267 return retval;
268}
269
270
271/*
272 * Here we implement the cache functions
273 */
274
275/* Allocate the cache buffers */
276static errcode_t alloc_cache(io_channel channel,
277 struct unix_private_data *data)
278{
279 errcode_t retval;
280 struct unix_cache *cache;
281 int i;
282
283 data->access_time = 0;
284 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
285 cache->block = 0;
286 cache->access_time = 0;
287 cache->dirty = 0;
288 cache->in_use = 0;
289 if ((retval = ext2fs_get_mem(channel->block_size,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400290 &cache->buf)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000291 return retval;
292 }
293 return 0;
294}
295
296/* Free the cache buffers */
Theodore Ts'o54434922003-12-07 01:28:50 -0500297static void free_cache(struct unix_private_data *data)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000298{
299 struct unix_cache *cache;
300 int i;
301
302 data->access_time = 0;
303 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
304 cache->block = 0;
305 cache->access_time = 0;
306 cache->dirty = 0;
307 cache->in_use = 0;
308 if (cache->buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400309 ext2fs_free_mem(&cache->buf);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000310 cache->buf = 0;
311 }
312}
313
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400314#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000315/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500316 * Try to find a block in the cache. If the block is not found, and
317 * eldest is a non-zero pointer, then fill in eldest with the cache
318 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000319 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500320static struct unix_cache *find_cached_block(struct unix_private_data *data,
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000321 unsigned long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500322 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000323{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000324 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000325 int i;
326
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000327 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000328 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
329 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500330 if (!unused_cache)
331 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000332 continue;
333 }
334 if (cache->block == block) {
335 cache->access_time = ++data->access_time;
336 return cache;
337 }
338 if (!oldest_cache ||
339 (cache->access_time < oldest_cache->access_time))
340 oldest_cache = cache;
341 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500342 if (eldest)
343 *eldest = (unused_cache) ? unused_cache : oldest_cache;
344 return 0;
345}
346
347/*
348 * Reuse a particular cache entry for another block.
349 */
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500350static void reuse_cache(io_channel channel, struct unix_private_data *data,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500351 struct unix_cache *cache, unsigned long block)
352{
353 if (cache->dirty && cache->in_use)
354 raw_write_blk(channel, data, cache->block, 1, cache->buf);
355
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000356 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500357 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000358 cache->block = block;
359 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000360}
361
362/*
363 * Flush all of the blocks in the cache
364 */
365static errcode_t flush_cached_blocks(io_channel channel,
366 struct unix_private_data *data,
367 int invalidate)
368
369{
370 struct unix_cache *cache;
371 errcode_t retval, retval2;
372 int i;
373
374 retval2 = 0;
375 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
376 if (!cache->in_use)
377 continue;
378
379 if (invalidate)
380 cache->in_use = 0;
381
382 if (!cache->dirty)
383 continue;
384
385 retval = raw_write_blk(channel, data,
386 cache->block, 1, cache->buf);
387 if (retval)
388 retval2 = retval;
389 else
390 cache->dirty = 0;
391 }
392 return retval2;
393}
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400394#endif /* NO_IO_CACHE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000395
Theodore Ts'o3839e651997-04-26 13:21:57 +0000396static errcode_t unix_open(const char *name, int flags, io_channel *channel)
397{
398 io_channel io = NULL;
399 struct unix_private_data *data = NULL;
400 errcode_t retval;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000401 int open_flags;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500402 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400403#ifdef __linux__
404 struct utsname ut;
405#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000406
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000407 if (name == 0)
408 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400409 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000410 if (retval)
411 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000412 memset(io, 0, sizeof(struct struct_io_channel));
413 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400414 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000415 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000416 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000417
Theodore Ts'o3839e651997-04-26 13:21:57 +0000418 io->manager = unix_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400419 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000420 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000421 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000422
Theodore Ts'o3839e651997-04-26 13:21:57 +0000423 strcpy(io->name, name);
424 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000425 io->block_size = 1024;
426 io->read_error = 0;
427 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000428 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000429
430 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000431 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400432 data->io_stats.num_fields = 2;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000433
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000434 if ((retval = alloc_cache(io, data)))
435 goto cleanup;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500436
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000437 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
Theodore Ts'ofa6c6532006-03-18 18:57:44 -0500438 if (flags & IO_FLAG_EXCLUSIVE)
439 open_flags |= O_EXCL;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000440#ifdef HAVE_OPEN64
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500441 data->dev = open64(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000442#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500443 data->dev = open(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000444#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445 if (data->dev < 0) {
446 retval = errno;
447 goto cleanup;
448 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500449
450#ifdef __linux__
451#undef RLIM_INFINITY
452#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
453#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
454#else
455#define RLIM_INFINITY (~0UL)
456#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500457 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400458 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
459 * block devices are wrongly getting hit by the filesize
460 * limit. This workaround isn't perfect, since it won't work
461 * if glibc wasn't built against 2.2 header files. (Sigh.)
462 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500463 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400464 if ((flags & IO_FLAG_RW) &&
465 (uname(&ut) == 0) &&
466 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
467 (ut.release[2] == '4') && (ut.release[3] == '.') &&
468 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
469 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500470 (fstat(data->dev, &st) == 0) &&
471 (S_ISBLK(st.st_mode))) {
472 struct rlimit rlim;
473
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500474 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500475 setrlimit(RLIMIT_FSIZE, &rlim);
476 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100477 if (((unsigned long) rlim.rlim_cur) <
478 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500479 rlim.rlim_cur = rlim.rlim_max;
480 setrlimit(RLIMIT_FSIZE, &rlim);
481 }
482 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500483#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000484 *channel = io;
485 return 0;
486
487cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488 if (data) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500489 free_cache(data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400490 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000491 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000492 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400493 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000494 return retval;
495}
496
497static errcode_t unix_close(io_channel channel)
498{
499 struct unix_private_data *data;
500 errcode_t retval = 0;
501
Theodore Ts'of3db3561997-04-26 13:34:30 +0000502 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000504 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000505
506 if (--channel->refcount > 0)
507 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000508
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400509#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000510 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400511#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000512
Theodore Ts'o3839e651997-04-26 13:21:57 +0000513 if (close(data->dev) < 0)
514 retval = errno;
Theodore Ts'o54434922003-12-07 01:28:50 -0500515 free_cache(data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500516
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400517 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400519 ext2fs_free_mem(&channel->name);
520 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521 return retval;
522}
523
524static errcode_t unix_set_blksize(io_channel channel, int blksize)
525{
526 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000527 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000528
Theodore Ts'of3db3561997-04-26 13:34:30 +0000529 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000531 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
532
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400534#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000535 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000536 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400537#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000538
539 channel->block_size = blksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500540 free_cache(data);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000541 if ((retval = alloc_cache(channel, data)))
542 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000543 }
544 return 0;
545}
546
547
548static errcode_t unix_read_blk(io_channel channel, unsigned long block,
549 int count, void *buf)
550{
551 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500552 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000554 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000555 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000556
Theodore Ts'of3db3561997-04-26 13:34:30 +0000557 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000558 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000559 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000560
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400561#ifdef NO_IO_CACHE
562 return raw_read_blk(channel, data, block, count, buf);
563#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000564 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500565 * If we're doing an odd-sized read or a very large read,
566 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500568 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000569 if ((retval = flush_cached_blocks(channel, data, 0)))
570 return retval;
571 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000572 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000573
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000574 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000575 while (count > 0) {
576 /* If it's in the cache, use it! */
Theodore Ts'o54434922003-12-07 01:28:50 -0500577 if ((cache = find_cached_block(data, block, &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000578#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400579 printf("Using cached block %lu\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000580#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000581 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000582 count--;
583 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000584 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000585 continue;
586 }
587 /*
588 * Find the number of uncached blocks so we can do a
589 * single read request
590 */
591 for (i=1; i < count; i++)
Theodore Ts'o54434922003-12-07 01:28:50 -0500592 if (find_cached_block(data, block+i, &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000593 break;
594#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400595 printf("Reading %d blocks starting at %lu\n", i, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000596#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000597 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000598 return retval;
599
600 /* Save the results in the cache */
601 for (j=0; j < i; j++) {
602 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500603 cache = reuse[j];
604 reuse_cache(channel, data, cache, block++);
605 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000606 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000607 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000608 }
609 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400610#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000611}
612
613static errcode_t unix_write_blk(io_channel channel, unsigned long block,
614 int count, const void *buf)
615{
616 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500617 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500618 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000619 const char *cp;
620 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000621
Theodore Ts'of3db3561997-04-26 13:34:30 +0000622 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000624 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000625
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400626#ifdef NO_IO_CACHE
627 return raw_write_blk(channel, data, block, count, buf);
628#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000629 /*
630 * If we're doing an odd-sized write or a very large write,
631 * flush out the cache completely and then do a direct write.
632 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500633 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000634 if ((retval = flush_cached_blocks(channel, data, 1)))
635 return retval;
636 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000637 }
638
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000639 /*
640 * For a moderate-sized multi-block write, first force a write
641 * if we're in write-through cache mode, and then fill the
642 * cache with the blocks.
643 */
644 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
645 if (writethrough)
646 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000648 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000649 while (count > 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500650 cache = find_cached_block(data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000651 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500652 cache = reuse;
653 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000654 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500655 memcpy(cache->buf, cp, channel->block_size);
656 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000657 count--;
658 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000659 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000660 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000661 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400662#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663}
664
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000665static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
666 int size, const void *buf)
667{
668 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000669 errcode_t retval = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500670 ssize_t actual;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000671
672 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
673 data = (struct unix_private_data *) channel->private_data;
674 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
675
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400676#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000677 /*
678 * Flush out the cache completely
679 */
680 if ((retval = flush_cached_blocks(channel, data, 1)))
681 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400682#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000683
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500684 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000685 return errno;
686
687 actual = write(data->dev, buf, size);
688 if (actual != size)
689 return EXT2_ET_SHORT_WRITE;
690
691 return 0;
692}
693
Theodore Ts'o3839e651997-04-26 13:21:57 +0000694/*
Theodore Ts'o36f21431997-06-14 07:25:40 +0000695 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000696 */
697static errcode_t unix_flush(io_channel channel)
698{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000699 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000700 errcode_t retval = 0;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000701
702 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
703 data = (struct unix_private_data *) channel->private_data;
704 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000705
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400706#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000707 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400708#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000709 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000710 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000711}
712
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500713static errcode_t unix_set_option(io_channel channel, const char *option,
714 const char *arg)
715{
716 struct unix_private_data *data;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500717 unsigned long long tmp;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500718 char *end;
719
720 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);
723
724 if (!strcmp(option, "offset")) {
725 if (!arg)
726 return EXT2_ET_INVALID_ARGUMENT;
727
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500728 tmp = strtoull(arg, &end, 0);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500729 if (*end)
730 return EXT2_ET_INVALID_ARGUMENT;
731 data->offset = tmp;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500732 if (data->offset < 0)
733 return EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500734 return 0;
735 }
736 return EXT2_ET_INVALID_ARGUMENT;
737}