blob: 8c599aa6515852d0d019b4820784caa586f2cc2d [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%
Theodore Ts'o543547a2010-05-17 21:31:56 -040013 * This file may be redistributed under the terms of the GNU Library
14 * General Public License, version 2.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000015 * %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
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -050034#ifdef HAVE_SYS_IOCTL_H
35#include <sys/ioctl.h>
36#endif
37#ifdef HAVE_SYS_MOUNT_H
38#include <sys/mount.h>
39#endif
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000040#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000041#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000042#endif
43#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000044#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000045#endif
Theodore Ts'offf45482003-04-13 00:44:19 -040046#if HAVE_SYS_RESOURCE_H
Theodore Ts'o8880e752001-11-26 21:05:36 -050047#include <sys/resource.h>
Theodore Ts'offf45482003-04-13 00:44:19 -040048#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000049
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -050050#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
51#define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
52#endif
53
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000054#include "ext2_fs.h"
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000055#include "ext2fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000056
Theodore Ts'of3db3561997-04-26 13:34:30 +000057/*
58 * For checking structure magic numbers...
59 */
60
61#define EXT2_CHECK_MAGIC(struct, code) \
62 if ((struct)->magic != (code)) return (code)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000063
64struct unix_cache {
65 char *buf;
66 unsigned long block;
67 int access_time;
Matthias Andree83e692e2004-03-30 04:17:14 +020068 unsigned dirty:1;
69 unsigned in_use:1;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000070};
71
72#define CACHE_SIZE 8
Theodore Ts'o82c46602002-11-09 14:56:17 -050073#define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
74#define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000075
Theodore Ts'o3839e651997-04-26 13:21:57 +000076struct unix_private_data {
Theodore Ts'of3db3561997-04-26 13:34:30 +000077 int magic;
Theodore Ts'o3839e651997-04-26 13:21:57 +000078 int dev;
79 int flags;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000080 int access_time;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050081 ext2_loff_t offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000082 struct unix_cache cache[CACHE_SIZE];
Theodore Ts'o6d96b002007-08-03 20:07:09 -040083 struct struct_io_stats io_stats;
Theodore Ts'o3839e651997-04-26 13:21:57 +000084};
85
86static errcode_t unix_open(const char *name, int flags, io_channel *channel);
87static errcode_t unix_close(io_channel channel);
88static errcode_t unix_set_blksize(io_channel channel, int blksize);
89static errcode_t unix_read_blk(io_channel channel, unsigned long block,
90 int count, void *data);
91static errcode_t unix_write_blk(io_channel channel, unsigned long block,
92 int count, const void *data);
93static errcode_t unix_flush(io_channel channel);
Theodore Ts'oc180ac82000-10-26 20:24:43 +000094static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
95 int size, const void *data);
Theodore Ts'oefc6f622008-08-27 23:07:54 -040096static errcode_t unix_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050097 const char *arg);
Theodore Ts'o6d96b002007-08-03 20:07:09 -040098static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
99;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500100static void reuse_cache(io_channel channel, struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600101 struct unix_cache *cache, unsigned long long block);
102static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
103 int count, void *data);
104static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
105 int count, const void *data);
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500106
Matthias Andree289e0552004-03-30 03:57:41 +0200107/* __FreeBSD_kernel__ is defined by GNU/kFreeBSD - the FreeBSD kernel
108 * does not know buffered block devices - everything is raw. */
109#if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100110#define NEED_BOUNCE_BUFFER
111#else
112#undef NEED_BOUNCE_BUFFER
113#endif
114
Theodore Ts'of3db3561997-04-26 13:34:30 +0000115static struct struct_io_manager struct_unix_manager = {
116 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000117 "Unix I/O Manager",
118 unix_open,
119 unix_close,
120 unix_set_blksize,
121 unix_read_blk,
122 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000123 unix_flush,
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100124#ifdef NEED_BOUNCE_BUFFER
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500125 0,
Theodore Ts'offf45482003-04-13 00:44:19 -0400126#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500127 unix_write_byte,
Theodore Ts'offf45482003-04-13 00:44:19 -0400128#endif
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400129 unix_set_option,
130 unix_get_stats,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600131 unix_read_blk64,
132 unix_write_blk64,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000133};
134
135io_manager unix_io_manager = &struct_unix_manager;
136
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400137static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
138{
139 errcode_t retval = 0;
140
141 struct unix_private_data *data;
142
143 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
144 data = (struct unix_private_data *) channel->private_data;
145 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
146
147 if (stats)
148 *stats = &data->io_stats;
149
150 return retval;
151}
152
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000153/*
154 * Here are the raw I/O functions
155 */
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100156#ifndef NEED_BOUNCE_BUFFER
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000157static errcode_t raw_read_blk(io_channel channel,
158 struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600159 unsigned long long block,
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000160 int count, void *buf)
161{
162 errcode_t retval;
Theodore Ts'o54434922003-12-07 01:28:50 -0500163 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000164 ext2_loff_t location;
165 int actual = 0;
166
167 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400168 data->io_stats.bytes_read += size;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500169 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000170 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
171 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
172 goto error_out;
173 }
174 actual = read(data->dev, buf, size);
175 if (actual != size) {
176 if (actual < 0)
177 actual = 0;
178 retval = EXT2_ET_SHORT_READ;
179 goto error_out;
180 }
181 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400182
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000183error_out:
184 memset((char *) buf+actual, 0, size-actual);
185 if (channel->read_error)
186 retval = (channel->read_error)(channel, block, count, buf,
187 size, actual, retval);
188 return retval;
189}
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100190#else /* NEED_BOUNCE_BUFFER */
Theodore Ts'offf45482003-04-13 00:44:19 -0400191/*
Matthias Andreeb34cbdd2003-12-28 18:21:26 +0100192 * Windows and FreeBSD block devices only allow sector alignment IO in offset and size
Theodore Ts'offf45482003-04-13 00:44:19 -0400193 */
194static errcode_t raw_read_blk(io_channel channel,
195 struct unix_private_data *data,
196 unsigned long block,
197 int count, void *buf)
198{
199 errcode_t retval;
200 size_t size, alignsize, fragment;
201 ext2_loff_t location;
202 int total = 0, actual;
203#define BLOCKALIGN 512
204 char sector[BLOCKALIGN];
205
206 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400207 data->io_stats.bytes_read += size;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500208 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'offf45482003-04-13 00:44:19 -0400209#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400210 printf("count=%d, size=%d, block=%lu, blk_size=%d, location=%llx\n",
211 count, size, block, channel->block_size, (long long)location);
Theodore Ts'offf45482003-04-13 00:44:19 -0400212#endif
213 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
214 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
215 goto error_out;
216 }
217 fragment = size % BLOCKALIGN;
218 alignsize = size - fragment;
219 if (alignsize) {
220 actual = read(data->dev, buf, alignsize);
221 if (actual != alignsize)
222 goto short_read;
223 }
224 if (fragment) {
225 actual = read(data->dev, sector, BLOCKALIGN);
226 if (actual != BLOCKALIGN)
227 goto short_read;
228 memcpy(buf+alignsize, sector, fragment);
229 }
230 return 0;
231
232short_read:
233 if (actual>0)
234 total += actual;
235 retval = EXT2_ET_SHORT_READ;
236
237error_out:
238 memset((char *) buf+total, 0, size-actual);
239 if (channel->read_error)
240 retval = (channel->read_error)(channel, block, count, buf,
241 size, actual, retval);
242 return retval;
243}
244#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000245
246static errcode_t raw_write_blk(io_channel channel,
247 struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600248 unsigned long long block,
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000249 int count, const void *buf)
250{
Theodore Ts'o54434922003-12-07 01:28:50 -0500251 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000252 ext2_loff_t location;
253 int actual = 0;
254 errcode_t retval;
255
256 if (count == 1)
257 size = channel->block_size;
258 else {
259 if (count < 0)
260 size = -count;
261 else
262 size = count * channel->block_size;
263 }
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400264 data->io_stats.bytes_written += size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000265
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500266 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000267 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
268 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
269 goto error_out;
270 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400271
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000272 actual = write(data->dev, buf, size);
273 if (actual != size) {
274 retval = EXT2_ET_SHORT_WRITE;
275 goto error_out;
276 }
277 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400278
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000279error_out:
280 if (channel->write_error)
281 retval = (channel->write_error)(channel, block, count, buf,
282 size, actual, retval);
283 return retval;
284}
285
286
287/*
288 * Here we implement the cache functions
289 */
290
291/* Allocate the cache buffers */
292static errcode_t alloc_cache(io_channel channel,
293 struct unix_private_data *data)
294{
295 errcode_t retval;
296 struct unix_cache *cache;
297 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400298
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000299 data->access_time = 0;
300 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
301 cache->block = 0;
302 cache->access_time = 0;
303 cache->dirty = 0;
304 cache->in_use = 0;
305 if ((retval = ext2fs_get_mem(channel->block_size,
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400306 &cache->buf)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000307 return retval;
308 }
309 return 0;
310}
311
312/* Free the cache buffers */
Theodore Ts'o54434922003-12-07 01:28:50 -0500313static void free_cache(struct unix_private_data *data)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000314{
315 struct unix_cache *cache;
316 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400317
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000318 data->access_time = 0;
319 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
320 cache->block = 0;
321 cache->access_time = 0;
322 cache->dirty = 0;
323 cache->in_use = 0;
324 if (cache->buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400325 ext2fs_free_mem(&cache->buf);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000326 cache->buf = 0;
327 }
328}
329
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400330#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000331/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500332 * Try to find a block in the cache. If the block is not found, and
333 * eldest is a non-zero pointer, then fill in eldest with the cache
334 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000335 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500336static struct unix_cache *find_cached_block(struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600337 unsigned long long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500338 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000339{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000340 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000341 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400342
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000343 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000344 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
345 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500346 if (!unused_cache)
347 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000348 continue;
349 }
350 if (cache->block == block) {
351 cache->access_time = ++data->access_time;
352 return cache;
353 }
354 if (!oldest_cache ||
355 (cache->access_time < oldest_cache->access_time))
356 oldest_cache = cache;
357 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500358 if (eldest)
359 *eldest = (unused_cache) ? unused_cache : oldest_cache;
360 return 0;
361}
362
363/*
364 * Reuse a particular cache entry for another block.
365 */
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500366static void reuse_cache(io_channel channel, struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600367 struct unix_cache *cache, unsigned long long block)
Theodore Ts'o82c46602002-11-09 14:56:17 -0500368{
369 if (cache->dirty && cache->in_use)
370 raw_write_blk(channel, data, cache->block, 1, cache->buf);
371
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000372 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500373 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000374 cache->block = block;
375 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000376}
377
378/*
379 * Flush all of the blocks in the cache
380 */
381static errcode_t flush_cached_blocks(io_channel channel,
382 struct unix_private_data *data,
383 int invalidate)
384
385{
386 struct unix_cache *cache;
387 errcode_t retval, retval2;
388 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400389
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000390 retval2 = 0;
391 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
392 if (!cache->in_use)
393 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400394
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000395 if (invalidate)
396 cache->in_use = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400397
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000398 if (!cache->dirty)
399 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400400
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000401 retval = raw_write_blk(channel, data,
402 cache->block, 1, cache->buf);
403 if (retval)
404 retval2 = retval;
405 else
406 cache->dirty = 0;
407 }
408 return retval2;
409}
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400410#endif /* NO_IO_CACHE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000411
Theodore Ts'o3839e651997-04-26 13:21:57 +0000412static errcode_t unix_open(const char *name, int flags, io_channel *channel)
413{
414 io_channel io = NULL;
415 struct unix_private_data *data = NULL;
416 errcode_t retval;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000417 int open_flags;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500418 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400419#ifdef __linux__
420 struct utsname ut;
421#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000422
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000423 if (name == 0)
424 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400425 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000426 if (retval)
427 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000428 memset(io, 0, sizeof(struct struct_io_channel));
429 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400430 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000431 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000432 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000433
Theodore Ts'o3839e651997-04-26 13:21:57 +0000434 io->manager = unix_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400435 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000436 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000437 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000438
Theodore Ts'o3839e651997-04-26 13:21:57 +0000439 strcpy(io->name, name);
440 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000441 io->block_size = 1024;
442 io->read_error = 0;
443 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000444 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000445
446 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000447 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400448 data->io_stats.num_fields = 2;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000449
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000450 if ((retval = alloc_cache(io, data)))
451 goto cleanup;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500452
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000453 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
Theodore Ts'ofa6c6532006-03-18 18:57:44 -0500454 if (flags & IO_FLAG_EXCLUSIVE)
455 open_flags |= O_EXCL;
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000456#ifdef HAVE_OPEN64
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500457 data->dev = open64(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000458#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500459 data->dev = open(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000460#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461 if (data->dev < 0) {
462 retval = errno;
463 goto cleanup;
464 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500465
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -0500466#ifdef BLKROGET
467 if (flags & IO_FLAG_RW) {
468 int error;
469 int readonly = 0;
470
471 /* Is the block device actually writable? */
472 error = ioctl(data->dev, BLKROGET, &readonly);
473 if (!error && readonly) {
474 close(data->dev);
475 retval = EPERM;
476 goto cleanup;
477 }
478 }
479#endif
480
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500481#ifdef __linux__
482#undef RLIM_INFINITY
483#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
484#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
485#else
486#define RLIM_INFINITY (~0UL)
487#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500488 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400489 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
490 * block devices are wrongly getting hit by the filesize
491 * limit. This workaround isn't perfect, since it won't work
492 * if glibc wasn't built against 2.2 header files. (Sigh.)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400493 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500494 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400495 if ((flags & IO_FLAG_RW) &&
496 (uname(&ut) == 0) &&
497 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
498 (ut.release[2] == '4') && (ut.release[3] == '.') &&
499 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
500 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500501 (fstat(data->dev, &st) == 0) &&
502 (S_ISBLK(st.st_mode))) {
503 struct rlimit rlim;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400504
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500505 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500506 setrlimit(RLIMIT_FSIZE, &rlim);
507 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100508 if (((unsigned long) rlim.rlim_cur) <
509 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500510 rlim.rlim_cur = rlim.rlim_max;
511 setrlimit(RLIMIT_FSIZE, &rlim);
512 }
513 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500514#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 *channel = io;
516 return 0;
517
518cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519 if (data) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500520 free_cache(data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400521 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000523 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400524 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525 return retval;
526}
527
528static errcode_t unix_close(io_channel channel)
529{
530 struct unix_private_data *data;
531 errcode_t retval = 0;
532
Theodore Ts'of3db3561997-04-26 13:34:30 +0000533 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000535 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000536
537 if (--channel->refcount > 0)
538 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000539
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400540#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000541 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400542#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000543
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 if (close(data->dev) < 0)
545 retval = errno;
Theodore Ts'o54434922003-12-07 01:28:50 -0500546 free_cache(data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500547
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400548 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400550 ext2fs_free_mem(&channel->name);
551 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000552 return retval;
553}
554
555static errcode_t unix_set_blksize(io_channel channel, int blksize)
556{
557 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000558 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000559
Theodore Ts'of3db3561997-04-26 13:34:30 +0000560 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000561 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000562 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
563
Theodore Ts'o3839e651997-04-26 13:21:57 +0000564 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400565#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000566 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000567 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400568#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400569
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000570 channel->block_size = blksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500571 free_cache(data);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000572 if ((retval = alloc_cache(channel, data)))
573 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000574 }
575 return 0;
576}
577
578
Jose R. Santos59ecd322008-03-03 10:41:24 -0600579static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000580 int count, void *buf)
581{
582 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500583 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000584 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000585 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000586 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000587
Theodore Ts'of3db3561997-04-26 13:34:30 +0000588 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000589 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000590 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000591
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400592#ifdef NO_IO_CACHE
593 return raw_read_blk(channel, data, block, count, buf);
594#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000595 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500596 * If we're doing an odd-sized read or a very large read,
597 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500599 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000600 if ((retval = flush_cached_blocks(channel, data, 0)))
601 return retval;
602 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000604
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000605 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000606 while (count > 0) {
607 /* If it's in the cache, use it! */
Theodore Ts'o54434922003-12-07 01:28:50 -0500608 if ((cache = find_cached_block(data, block, &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000609#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400610 printf("Using cached block %lu\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000611#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000612 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000613 count--;
614 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000615 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000616 continue;
617 }
618 /*
619 * Find the number of uncached blocks so we can do a
620 * single read request
621 */
622 for (i=1; i < count; i++)
Theodore Ts'o54434922003-12-07 01:28:50 -0500623 if (find_cached_block(data, block+i, &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000624 break;
625#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400626 printf("Reading %d blocks starting at %lu\n", i, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000627#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000628 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000629 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400630
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000631 /* Save the results in the cache */
632 for (j=0; j < i; j++) {
633 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500634 cache = reuse[j];
635 reuse_cache(channel, data, cache, block++);
636 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000637 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000638 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639 }
640 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400641#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000642}
643
Jose R. Santos59ecd322008-03-03 10:41:24 -0600644static errcode_t unix_read_blk(io_channel channel, unsigned long block,
645 int count, void *buf)
646{
647 return unix_read_blk64(channel, block, count, buf);
648}
649
650static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651 int count, const void *buf)
652{
653 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500654 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500655 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000656 const char *cp;
657 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000658
Theodore Ts'of3db3561997-04-26 13:34:30 +0000659 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000660 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000661 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000662
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400663#ifdef NO_IO_CACHE
664 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400665#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000666 /*
667 * If we're doing an odd-sized write or a very large write,
668 * flush out the cache completely and then do a direct write.
669 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500670 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000671 if ((retval = flush_cached_blocks(channel, data, 1)))
672 return retval;
673 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000674 }
675
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000676 /*
677 * For a moderate-sized multi-block write, first force a write
678 * if we're in write-through cache mode, and then fill the
679 * cache with the blocks.
680 */
681 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
682 if (writethrough)
683 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400684
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000685 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000686 while (count > 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500687 cache = find_cached_block(data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000688 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500689 cache = reuse;
690 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000691 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500692 memcpy(cache->buf, cp, channel->block_size);
693 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000694 count--;
695 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000696 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000697 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000698 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400699#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000700}
701
Jose R. Santos59ecd322008-03-03 10:41:24 -0600702static errcode_t unix_write_blk(io_channel channel, unsigned long block,
703 int count, const void *buf)
704{
705 return unix_write_blk64(channel, block, count, buf);
706}
707
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000708static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
709 int size, const void *buf)
710{
711 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000712 errcode_t retval = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500713 ssize_t actual;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000714
715 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
716 data = (struct unix_private_data *) channel->private_data;
717 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
718
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400719#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000720 /*
721 * Flush out the cache completely
722 */
723 if ((retval = flush_cached_blocks(channel, data, 1)))
724 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400725#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000726
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500727 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000728 return errno;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400729
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000730 actual = write(data->dev, buf, size);
731 if (actual != size)
732 return EXT2_ET_SHORT_WRITE;
733
734 return 0;
735}
736
Theodore Ts'o3839e651997-04-26 13:21:57 +0000737/*
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400738 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000739 */
740static errcode_t unix_flush(io_channel channel)
741{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000742 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000743 errcode_t retval = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400744
Theodore Ts'of3db3561997-04-26 13:34:30 +0000745 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
746 data = (struct unix_private_data *) channel->private_data;
747 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000748
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400749#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000750 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400751#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000752 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000753 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000754}
755
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400756static errcode_t unix_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500757 const char *arg)
758{
759 struct unix_private_data *data;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500760 unsigned long long tmp;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500761 char *end;
762
763 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
764 data = (struct unix_private_data *) channel->private_data;
765 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
766
767 if (!strcmp(option, "offset")) {
768 if (!arg)
769 return EXT2_ET_INVALID_ARGUMENT;
770
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500771 tmp = strtoull(arg, &end, 0);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500772 if (*end)
773 return EXT2_ET_INVALID_ARGUMENT;
774 data->offset = tmp;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500775 if (data->offset < 0)
776 return EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500777 return 0;
778 }
779 return EXT2_ET_INVALID_ARGUMENT;
780}