blob: c1d05615b0c94a2aab978b27b07be7e1bc94b89c [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
Andreas Dilgercf5301d2011-06-11 10:58:25 -040020#ifndef _GNU_SOURCE
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040021#define _GNU_SOURCE
Andreas Dilgercf5301d2011-06-11 10:58:25 -040022#endif
Theodore Ts'odc5f68c2000-05-25 23:31:54 +000023
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <stdio.h>
25#include <string.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000026#if HAVE_UNISTD_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000028#endif
Theodore Ts'oc4e749a1998-02-20 05:33:14 +000029#if HAVE_ERRNO_H
30#include <errno.h>
31#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000032#include <fcntl.h>
33#include <time.h>
Theodore Ts'of154d2f2002-07-14 08:33:32 -040034#ifdef __linux__
35#include <sys/utsname.h>
36#endif
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -050037#ifdef HAVE_SYS_IOCTL_H
38#include <sys/ioctl.h>
39#endif
40#ifdef HAVE_SYS_MOUNT_H
41#include <sys/mount.h>
42#endif
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000043#if HAVE_SYS_STAT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000044#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000045#endif
46#if HAVE_SYS_TYPES_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000047#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000048#endif
Theodore Ts'offf45482003-04-13 00:44:19 -040049#if HAVE_SYS_RESOURCE_H
Theodore Ts'o8880e752001-11-26 21:05:36 -050050#include <sys/resource.h>
Theodore Ts'offf45482003-04-13 00:44:19 -040051#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000052
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040053#if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -050054#define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
55#endif
56
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040057#if defined(__linux__) && defined(_IO) && !defined(BLKSSZGET)
58#define BLKSSZGET _IO(0x12,104)/* get block device sector size */
59#endif
60
61#undef ALIGN_DEBUG
62
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000063#include "ext2_fs.h"
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000064#include "ext2fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000065
Theodore Ts'of3db3561997-04-26 13:34:30 +000066/*
67 * For checking structure magic numbers...
68 */
69
70#define EXT2_CHECK_MAGIC(struct, code) \
71 if ((struct)->magic != (code)) return (code)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000072
73struct unix_cache {
74 char *buf;
75 unsigned long block;
76 int access_time;
Matthias Andree83e692e2004-03-30 04:17:14 +020077 unsigned dirty:1;
78 unsigned in_use:1;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000079};
80
81#define CACHE_SIZE 8
Theodore Ts'o82c46602002-11-09 14:56:17 -050082#define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
83#define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000084
Theodore Ts'o3839e651997-04-26 13:21:57 +000085struct unix_private_data {
Theodore Ts'of3db3561997-04-26 13:34:30 +000086 int magic;
Theodore Ts'o3839e651997-04-26 13:21:57 +000087 int dev;
88 int flags;
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040089 int align;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000090 int access_time;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050091 ext2_loff_t offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000092 struct unix_cache cache[CACHE_SIZE];
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040093 void *bounce;
Theodore Ts'o6d96b002007-08-03 20:07:09 -040094 struct struct_io_stats io_stats;
Theodore Ts'o3839e651997-04-26 13:21:57 +000095};
96
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040097#define IS_ALIGNED(n, align) ((((unsigned long) n) & \
98 ((unsigned long) ((align)-1))) == 0)
99
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100static errcode_t unix_open(const char *name, int flags, io_channel *channel);
101static errcode_t unix_close(io_channel channel);
102static errcode_t unix_set_blksize(io_channel channel, int blksize);
103static errcode_t unix_read_blk(io_channel channel, unsigned long block,
104 int count, void *data);
105static errcode_t unix_write_blk(io_channel channel, unsigned long block,
106 int count, const void *data);
107static errcode_t unix_flush(io_channel channel);
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000108static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
109 int size, const void *data);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400110static errcode_t unix_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500111 const char *arg);
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400112static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
113;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500114static void reuse_cache(io_channel channel, struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600115 struct unix_cache *cache, unsigned long long block);
116static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
117 int count, void *data);
118static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
119 int count, const void *data);
Lukas Czernere90a59e2010-11-18 03:38:36 +0000120static errcode_t unix_discard(io_channel channel, unsigned long long block,
121 unsigned long long count);
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500122
Theodore Ts'of3db3561997-04-26 13:34:30 +0000123static struct struct_io_manager struct_unix_manager = {
124 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000125 "Unix I/O Manager",
126 unix_open,
127 unix_close,
128 unix_set_blksize,
129 unix_read_blk,
130 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000131 unix_flush,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500132 unix_write_byte,
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400133 unix_set_option,
134 unix_get_stats,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600135 unix_read_blk64,
136 unix_write_blk64,
Lukas Czernere90a59e2010-11-18 03:38:36 +0000137 unix_discard,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138};
139
140io_manager unix_io_manager = &struct_unix_manager;
141
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400142static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
143{
144 errcode_t retval = 0;
145
146 struct unix_private_data *data;
147
148 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
149 data = (struct unix_private_data *) channel->private_data;
150 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
151
152 if (stats)
153 *stats = &data->io_stats;
154
155 return retval;
156}
157
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000158/*
159 * Here are the raw I/O functions
160 */
161static errcode_t raw_read_blk(io_channel channel,
162 struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600163 unsigned long long block,
Theodore Ts'od32c9152011-07-07 13:50:22 -0400164 int count, void *bufv)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000165{
166 errcode_t retval;
Theodore Ts'o54434922003-12-07 01:28:50 -0500167 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000168 ext2_loff_t location;
169 int actual = 0;
Theodore Ts'od32c9152011-07-07 13:50:22 -0400170 unsigned char *buf = bufv;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000171
172 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400173 data->io_stats.bytes_read += size;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500174 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000175 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
176 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
177 goto error_out;
178 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400179 if ((data->align == 0) ||
180 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
181 actual = read(data->dev, buf, size);
182 if (actual != size) {
183 short_read:
184 if (actual < 0)
185 actual = 0;
186 retval = EXT2_ET_SHORT_READ;
187 goto error_out;
188 }
189 return 0;
190 }
191
192#ifdef ALIGN_DEBUG
193 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
194 (unsigned long) size);
195#endif
196
197 /*
198 * The buffer or size which we're trying to read isn't aligned
199 * to the O_DIRECT rules, so we need to do this the hard way...
200 */
201 while (size > 0) {
202 actual = read(data->dev, data->bounce, channel->block_size);
203 if (actual != channel->block_size)
204 goto short_read;
205 actual = size;
206 if (size > channel->block_size)
207 actual = channel->block_size;
208 memcpy(buf, data->bounce, actual);
209 size -= actual;
210 buf += actual;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000211 }
212 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400213
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000214error_out:
215 memset((char *) buf+actual, 0, size-actual);
216 if (channel->read_error)
217 retval = (channel->read_error)(channel, block, count, buf,
218 size, actual, retval);
219 return retval;
220}
221
222static errcode_t raw_write_blk(io_channel channel,
223 struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600224 unsigned long long block,
Theodore Ts'od32c9152011-07-07 13:50:22 -0400225 int count, const void *bufv)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000226{
Theodore Ts'o54434922003-12-07 01:28:50 -0500227 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000228 ext2_loff_t location;
229 int actual = 0;
230 errcode_t retval;
Theodore Ts'od32c9152011-07-07 13:50:22 -0400231 const unsigned char *buf = bufv;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000232
233 if (count == 1)
234 size = channel->block_size;
235 else {
236 if (count < 0)
237 size = -count;
238 else
239 size = count * channel->block_size;
240 }
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400241 data->io_stats.bytes_written += size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000242
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500243 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000244 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
245 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
246 goto error_out;
247 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400248
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400249 if ((data->align == 0) ||
250 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
251 actual = write(data->dev, buf, size);
252 if (actual != size) {
253 short_write:
254 retval = EXT2_ET_SHORT_WRITE;
255 goto error_out;
256 }
257 return 0;
258 }
259
260#ifdef ALIGN_DEBUG
261 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
262 (unsigned long) size);
263#endif
264 /*
265 * The buffer or size which we're trying to write isn't aligned
266 * to the O_DIRECT rules, so we need to do this the hard way...
267 */
268 while (size > 0) {
269 if (size < channel->block_size) {
270 actual = read(data->dev, data->bounce,
271 channel->block_size);
272 if (actual != channel->block_size) {
273 retval = EXT2_ET_SHORT_READ;
274 goto error_out;
275 }
276 }
277 actual = size;
278 if (size > channel->block_size)
279 actual = channel->block_size;
280 memcpy(data->bounce, buf, actual);
281 actual = write(data->dev, data->bounce, channel->block_size);
282 if (actual != channel->block_size)
283 goto short_write;
284 size -= actual;
285 buf += actual;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000286 }
287 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400288
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000289error_out:
290 if (channel->write_error)
291 retval = (channel->write_error)(channel, block, count, buf,
292 size, actual, retval);
293 return retval;
294}
295
296
297/*
298 * Here we implement the cache functions
299 */
300
301/* Allocate the cache buffers */
302static errcode_t alloc_cache(io_channel channel,
303 struct unix_private_data *data)
304{
305 errcode_t retval;
306 struct unix_cache *cache;
307 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400308
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000309 data->access_time = 0;
310 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
311 cache->block = 0;
312 cache->access_time = 0;
313 cache->dirty = 0;
314 cache->in_use = 0;
Theodore Ts'ofaafdb72010-09-23 13:06:31 -0400315 if (cache->buf)
316 ext2fs_free_mem(&cache->buf);
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400317 retval = ext2fs_get_memalign(channel->block_size,
318 data->align, &cache->buf);
319 if (retval)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000320 return retval;
321 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400322 if (data->align) {
323 if (data->bounce)
324 ext2fs_free_mem(&data->bounce);
325 retval = ext2fs_get_memalign(channel->block_size, data->align,
326 &data->bounce);
327 }
328 return retval;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000329}
330
331/* Free the cache buffers */
Theodore Ts'o54434922003-12-07 01:28:50 -0500332static void free_cache(struct unix_private_data *data)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000333{
334 struct unix_cache *cache;
335 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400336
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000337 data->access_time = 0;
338 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
339 cache->block = 0;
340 cache->access_time = 0;
341 cache->dirty = 0;
342 cache->in_use = 0;
343 if (cache->buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400344 ext2fs_free_mem(&cache->buf);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000345 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400346 if (data->bounce)
347 ext2fs_free_mem(&data->bounce);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000348}
349
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400350#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000351/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500352 * Try to find a block in the cache. If the block is not found, and
353 * eldest is a non-zero pointer, then fill in eldest with the cache
354 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000355 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500356static struct unix_cache *find_cached_block(struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600357 unsigned long long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500358 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000359{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000360 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000361 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400362
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000363 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000364 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
365 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500366 if (!unused_cache)
367 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000368 continue;
369 }
370 if (cache->block == block) {
371 cache->access_time = ++data->access_time;
372 return cache;
373 }
374 if (!oldest_cache ||
375 (cache->access_time < oldest_cache->access_time))
376 oldest_cache = cache;
377 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500378 if (eldest)
379 *eldest = (unused_cache) ? unused_cache : oldest_cache;
380 return 0;
381}
382
383/*
384 * Reuse a particular cache entry for another block.
385 */
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500386static void reuse_cache(io_channel channel, struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600387 struct unix_cache *cache, unsigned long long block)
Theodore Ts'o82c46602002-11-09 14:56:17 -0500388{
389 if (cache->dirty && cache->in_use)
390 raw_write_blk(channel, data, cache->block, 1, cache->buf);
391
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000392 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500393 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000394 cache->block = block;
395 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000396}
397
398/*
399 * Flush all of the blocks in the cache
400 */
401static errcode_t flush_cached_blocks(io_channel channel,
402 struct unix_private_data *data,
403 int invalidate)
404
405{
406 struct unix_cache *cache;
407 errcode_t retval, retval2;
408 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400409
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000410 retval2 = 0;
411 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
412 if (!cache->in_use)
413 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400414
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000415 if (invalidate)
416 cache->in_use = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400417
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000418 if (!cache->dirty)
419 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400420
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000421 retval = raw_write_blk(channel, data,
422 cache->block, 1, cache->buf);
423 if (retval)
424 retval2 = retval;
425 else
426 cache->dirty = 0;
427 }
428 return retval2;
429}
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400430#endif /* NO_IO_CACHE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000431
Lukas Czernerd8665992010-11-18 03:38:37 +0000432#ifdef __linux__
433#ifndef BLKDISCARDZEROES
434#define BLKDISCARDZEROES _IO(0x12,124)
435#endif
436#endif
437
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438static errcode_t unix_open(const char *name, int flags, io_channel *channel)
439{
440 io_channel io = NULL;
441 struct unix_private_data *data = NULL;
442 errcode_t retval;
Lukas Czernerd8665992010-11-18 03:38:37 +0000443 int open_flags, zeroes = 0;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500444 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400445#ifdef __linux__
446 struct utsname ut;
447#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000448
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000449 if (name == 0)
450 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400451 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000452 if (retval)
453 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000454 memset(io, 0, sizeof(struct struct_io_channel));
455 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400456 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000457 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000458 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000459
Theodore Ts'o3839e651997-04-26 13:21:57 +0000460 io->manager = unix_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400461 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000462 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000464
Theodore Ts'o3839e651997-04-26 13:21:57 +0000465 strcpy(io->name, name);
466 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000467 io->block_size = 1024;
468 io->read_error = 0;
469 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000470 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000471
472 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000473 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400474 data->io_stats.num_fields = 2;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000475
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000476 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
Theodore Ts'ofa6c6532006-03-18 18:57:44 -0500477 if (flags & IO_FLAG_EXCLUSIVE)
478 open_flags |= O_EXCL;
Andreas Dilger534a4c32011-06-11 11:50:01 -0400479#ifdef O_DIRECT
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400480 if (flags & IO_FLAG_DIRECT_IO)
481 open_flags |= O_DIRECT;
Andreas Dilger534a4c32011-06-11 11:50:01 -0400482#endif
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400483 data->flags = flags;
484
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000485#ifdef HAVE_OPEN64
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500486 data->dev = open64(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000487#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500488 data->dev = open(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000489#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490 if (data->dev < 0) {
491 retval = errno;
492 goto cleanup;
493 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500494
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400495#ifdef BLKSSZGET
496 if (flags & IO_FLAG_DIRECT_IO) {
497 if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
498 data->align = io->block_size;
499 }
500#endif
501
Lukas Czernerd8665992010-11-18 03:38:37 +0000502#ifdef BLKDISCARDZEROES
503 ioctl(data->dev, BLKDISCARDZEROES, &zeroes);
504 if (zeroes)
505 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
506#endif
507
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400508#if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
509 /*
510 * Some operating systems require that the buffers be aligned,
511 * regardless of O_DIRECT
512 */
513 data->align = 512;
514#endif
515
516
517 if ((retval = alloc_cache(io, data)))
518 goto cleanup;
519
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -0500520#ifdef BLKROGET
521 if (flags & IO_FLAG_RW) {
522 int error;
523 int readonly = 0;
524
525 /* Is the block device actually writable? */
526 error = ioctl(data->dev, BLKROGET, &readonly);
527 if (!error && readonly) {
528 close(data->dev);
529 retval = EPERM;
530 goto cleanup;
531 }
532 }
533#endif
534
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500535#ifdef __linux__
536#undef RLIM_INFINITY
537#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
538#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
539#else
540#define RLIM_INFINITY (~0UL)
541#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500542 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400543 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
544 * block devices are wrongly getting hit by the filesize
545 * limit. This workaround isn't perfect, since it won't work
546 * if glibc wasn't built against 2.2 header files. (Sigh.)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400547 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500548 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400549 if ((flags & IO_FLAG_RW) &&
550 (uname(&ut) == 0) &&
551 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
552 (ut.release[2] == '4') && (ut.release[3] == '.') &&
553 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
554 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500555 (fstat(data->dev, &st) == 0) &&
556 (S_ISBLK(st.st_mode))) {
557 struct rlimit rlim;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400558
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500559 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500560 setrlimit(RLIMIT_FSIZE, &rlim);
561 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100562 if (((unsigned long) rlim.rlim_cur) <
563 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500564 rlim.rlim_cur = rlim.rlim_max;
565 setrlimit(RLIMIT_FSIZE, &rlim);
566 }
567 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500568#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000569 *channel = io;
570 return 0;
571
572cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000573 if (data) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500574 free_cache(data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400575 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000577 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400578 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000579 return retval;
580}
581
582static errcode_t unix_close(io_channel channel)
583{
584 struct unix_private_data *data;
585 errcode_t retval = 0;
586
Theodore Ts'of3db3561997-04-26 13:34:30 +0000587 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000589 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000590
591 if (--channel->refcount > 0)
592 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000593
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400594#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000595 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400596#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000597
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598 if (close(data->dev) < 0)
599 retval = errno;
Theodore Ts'o54434922003-12-07 01:28:50 -0500600 free_cache(data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500601
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400602 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400604 ext2fs_free_mem(&channel->name);
605 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000606 return retval;
607}
608
609static errcode_t unix_set_blksize(io_channel channel, int blksize)
610{
611 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000612 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000613
Theodore Ts'of3db3561997-04-26 13:34:30 +0000614 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000615 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000616 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
617
Theodore Ts'o3839e651997-04-26 13:21:57 +0000618 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400619#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000620 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000621 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400622#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400623
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000624 channel->block_size = blksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500625 free_cache(data);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000626 if ((retval = alloc_cache(channel, data)))
627 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 }
629 return 0;
630}
631
632
Jose R. Santos59ecd322008-03-03 10:41:24 -0600633static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000634 int count, void *buf)
635{
636 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500637 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000638 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000639 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000640 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000641
Theodore Ts'of3db3561997-04-26 13:34:30 +0000642 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000643 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000644 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000645
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400646#ifdef NO_IO_CACHE
647 return raw_read_blk(channel, data, block, count, buf);
648#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000649 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500650 * If we're doing an odd-sized read or a very large read,
651 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000652 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500653 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000654 if ((retval = flush_cached_blocks(channel, data, 0)))
655 return retval;
656 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000657 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000658
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000659 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000660 while (count > 0) {
661 /* If it's in the cache, use it! */
Theodore Ts'o54434922003-12-07 01:28:50 -0500662 if ((cache = find_cached_block(data, block, &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000663#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400664 printf("Using cached block %lu\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000665#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000666 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000667 count--;
668 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000669 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000670 continue;
671 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400672 if (count == 1) {
673 /*
674 * Special case where we read directly into the
675 * cache buffer; important in the O_DIRECT case
676 */
677 cache = reuse[0];
678 reuse_cache(channel, data, cache, block);
679 if ((retval = raw_read_blk(channel, data, block, 1,
680 cache->buf))) {
681 cache->in_use = 0;
682 return retval;
683 }
684 memcpy(cp, cache->buf, channel->block_size);
685 return 0;
686 }
687
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000688 /*
689 * Find the number of uncached blocks so we can do a
690 * single read request
691 */
692 for (i=1; i < count; i++)
Theodore Ts'o54434922003-12-07 01:28:50 -0500693 if (find_cached_block(data, block+i, &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000694 break;
695#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400696 printf("Reading %d blocks starting at %lu\n", i, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000697#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000698 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000699 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400700
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000701 /* Save the results in the cache */
702 for (j=0; j < i; j++) {
703 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500704 cache = reuse[j];
705 reuse_cache(channel, data, cache, block++);
706 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000707 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000708 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000709 }
710 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400711#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000712}
713
Jose R. Santos59ecd322008-03-03 10:41:24 -0600714static errcode_t unix_read_blk(io_channel channel, unsigned long block,
715 int count, void *buf)
716{
717 return unix_read_blk64(channel, block, count, buf);
718}
719
720static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000721 int count, const void *buf)
722{
723 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500724 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500725 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000726 const char *cp;
727 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000728
Theodore Ts'of3db3561997-04-26 13:34:30 +0000729 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000730 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000731 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000732
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400733#ifdef NO_IO_CACHE
734 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400735#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000736 /*
737 * If we're doing an odd-sized write or a very large write,
738 * flush out the cache completely and then do a direct write.
739 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500740 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000741 if ((retval = flush_cached_blocks(channel, data, 1)))
742 return retval;
743 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000744 }
745
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000746 /*
747 * For a moderate-sized multi-block write, first force a write
748 * if we're in write-through cache mode, and then fill the
749 * cache with the blocks.
750 */
751 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
752 if (writethrough)
753 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400754
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000755 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000756 while (count > 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500757 cache = find_cached_block(data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000758 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500759 cache = reuse;
760 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000761 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500762 memcpy(cache->buf, cp, channel->block_size);
763 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000764 count--;
765 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000766 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000767 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000768 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400769#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000770}
771
Jose R. Santos59ecd322008-03-03 10:41:24 -0600772static errcode_t unix_write_blk(io_channel channel, unsigned long block,
773 int count, const void *buf)
774{
775 return unix_write_blk64(channel, block, count, buf);
776}
777
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000778static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
779 int size, const void *buf)
780{
781 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000782 errcode_t retval = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500783 ssize_t actual;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000784
785 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
786 data = (struct unix_private_data *) channel->private_data;
787 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
788
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400789 if (data->align != 0) {
790#ifdef ALIGN_DEBUG
791 printf("unix_write_byte: O_DIRECT fallback\n");
792#endif
793 return EXT2_ET_UNIMPLEMENTED;
794 }
795
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400796#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000797 /*
798 * Flush out the cache completely
799 */
800 if ((retval = flush_cached_blocks(channel, data, 1)))
801 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400802#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000803
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500804 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000805 return errno;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400806
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000807 actual = write(data->dev, buf, size);
808 if (actual != size)
809 return EXT2_ET_SHORT_WRITE;
810
811 return 0;
812}
813
Theodore Ts'o3839e651997-04-26 13:21:57 +0000814/*
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400815 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000816 */
817static errcode_t unix_flush(io_channel channel)
818{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000819 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000820 errcode_t retval = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400821
Theodore Ts'of3db3561997-04-26 13:34:30 +0000822 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
823 data = (struct unix_private_data *) channel->private_data;
824 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000825
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400826#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000827 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400828#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000829 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000830 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000831}
832
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400833static errcode_t unix_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500834 const char *arg)
835{
836 struct unix_private_data *data;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500837 unsigned long long tmp;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500838 char *end;
839
840 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
841 data = (struct unix_private_data *) channel->private_data;
842 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
843
844 if (!strcmp(option, "offset")) {
845 if (!arg)
846 return EXT2_ET_INVALID_ARGUMENT;
847
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500848 tmp = strtoull(arg, &end, 0);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500849 if (*end)
850 return EXT2_ET_INVALID_ARGUMENT;
851 data->offset = tmp;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500852 if (data->offset < 0)
853 return EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500854 return 0;
855 }
856 return EXT2_ET_INVALID_ARGUMENT;
857}
Lukas Czernere90a59e2010-11-18 03:38:36 +0000858
859#if defined(__linux__) && !defined(BLKDISCARD)
860#define BLKDISCARD _IO(0x12,119)
861#endif
862
863static errcode_t unix_discard(io_channel channel, unsigned long long block,
864 unsigned long long count)
865{
866#ifdef BLKDISCARD
867 struct unix_private_data *data;
868 __uint64_t range[2];
869 int ret;
870
871 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
872 data = (struct unix_private_data *) channel->private_data;
873 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
874
875 range[0] = (__uint64_t)(block) * channel->block_size;
876 range[1] = (__uint64_t)(count) * channel->block_size;
877
878 ret = ioctl(data->dev, BLKDISCARD, &range);
879 if (ret < 0)
880 return errno;
881 return 0;
882#else
883 return EXT2_ET_UNIMPLEMENTED;
884#endif
885}