blob: dddaf6f2b99619f713a6b6deafa76d069daba648 [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'oadfc8c62000-10-18 19:22:24 +0000164 int count, void *buf)
165{
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;
170
171 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400172 data->io_stats.bytes_read += size;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500173 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000174 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
175 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
176 goto error_out;
177 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400178 if ((data->align == 0) ||
179 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
180 actual = read(data->dev, buf, size);
181 if (actual != size) {
182 short_read:
183 if (actual < 0)
184 actual = 0;
185 retval = EXT2_ET_SHORT_READ;
186 goto error_out;
187 }
188 return 0;
189 }
190
191#ifdef ALIGN_DEBUG
192 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
193 (unsigned long) size);
194#endif
195
196 /*
197 * The buffer or size which we're trying to read isn't aligned
198 * to the O_DIRECT rules, so we need to do this the hard way...
199 */
200 while (size > 0) {
201 actual = read(data->dev, data->bounce, channel->block_size);
202 if (actual != channel->block_size)
203 goto short_read;
204 actual = size;
205 if (size > channel->block_size)
206 actual = channel->block_size;
207 memcpy(buf, data->bounce, actual);
208 size -= actual;
209 buf += actual;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000210 }
211 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400212
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000213error_out:
214 memset((char *) buf+actual, 0, size-actual);
215 if (channel->read_error)
216 retval = (channel->read_error)(channel, block, count, buf,
217 size, actual, retval);
218 return retval;
219}
220
221static errcode_t raw_write_blk(io_channel channel,
222 struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600223 unsigned long long block,
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000224 int count, const void *buf)
225{
Theodore Ts'o54434922003-12-07 01:28:50 -0500226 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000227 ext2_loff_t location;
228 int actual = 0;
229 errcode_t retval;
230
231 if (count == 1)
232 size = channel->block_size;
233 else {
234 if (count < 0)
235 size = -count;
236 else
237 size = count * channel->block_size;
238 }
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400239 data->io_stats.bytes_written += size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000240
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500241 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000242 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
243 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
244 goto error_out;
245 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400246
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400247 if ((data->align == 0) ||
248 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
249 actual = write(data->dev, buf, size);
250 if (actual != size) {
251 short_write:
252 retval = EXT2_ET_SHORT_WRITE;
253 goto error_out;
254 }
255 return 0;
256 }
257
258#ifdef ALIGN_DEBUG
259 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
260 (unsigned long) size);
261#endif
262 /*
263 * The buffer or size which we're trying to write isn't aligned
264 * to the O_DIRECT rules, so we need to do this the hard way...
265 */
266 while (size > 0) {
267 if (size < channel->block_size) {
268 actual = read(data->dev, data->bounce,
269 channel->block_size);
270 if (actual != channel->block_size) {
271 retval = EXT2_ET_SHORT_READ;
272 goto error_out;
273 }
274 }
275 actual = size;
276 if (size > channel->block_size)
277 actual = channel->block_size;
278 memcpy(data->bounce, buf, actual);
279 actual = write(data->dev, data->bounce, channel->block_size);
280 if (actual != channel->block_size)
281 goto short_write;
282 size -= actual;
283 buf += actual;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000284 }
285 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400286
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000287error_out:
288 if (channel->write_error)
289 retval = (channel->write_error)(channel, block, count, buf,
290 size, actual, retval);
291 return retval;
292}
293
294
295/*
296 * Here we implement the cache functions
297 */
298
299/* Allocate the cache buffers */
300static errcode_t alloc_cache(io_channel channel,
301 struct unix_private_data *data)
302{
303 errcode_t retval;
304 struct unix_cache *cache;
305 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400306
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000307 data->access_time = 0;
308 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
309 cache->block = 0;
310 cache->access_time = 0;
311 cache->dirty = 0;
312 cache->in_use = 0;
Theodore Ts'ofaafdb72010-09-23 13:06:31 -0400313 if (cache->buf)
314 ext2fs_free_mem(&cache->buf);
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400315 retval = ext2fs_get_memalign(channel->block_size,
316 data->align, &cache->buf);
317 if (retval)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000318 return retval;
319 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400320 if (data->align) {
321 if (data->bounce)
322 ext2fs_free_mem(&data->bounce);
323 retval = ext2fs_get_memalign(channel->block_size, data->align,
324 &data->bounce);
325 }
326 return retval;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000327}
328
329/* Free the cache buffers */
Theodore Ts'o54434922003-12-07 01:28:50 -0500330static void free_cache(struct unix_private_data *data)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000331{
332 struct unix_cache *cache;
333 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400334
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000335 data->access_time = 0;
336 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
337 cache->block = 0;
338 cache->access_time = 0;
339 cache->dirty = 0;
340 cache->in_use = 0;
341 if (cache->buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400342 ext2fs_free_mem(&cache->buf);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000343 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400344 if (data->bounce)
345 ext2fs_free_mem(&data->bounce);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000346}
347
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400348#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000349/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500350 * Try to find a block in the cache. If the block is not found, and
351 * eldest is a non-zero pointer, then fill in eldest with the cache
352 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000353 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500354static struct unix_cache *find_cached_block(struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600355 unsigned long long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500356 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000357{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000358 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000359 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400360
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000361 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000362 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
363 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500364 if (!unused_cache)
365 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000366 continue;
367 }
368 if (cache->block == block) {
369 cache->access_time = ++data->access_time;
370 return cache;
371 }
372 if (!oldest_cache ||
373 (cache->access_time < oldest_cache->access_time))
374 oldest_cache = cache;
375 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500376 if (eldest)
377 *eldest = (unused_cache) ? unused_cache : oldest_cache;
378 return 0;
379}
380
381/*
382 * Reuse a particular cache entry for another block.
383 */
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500384static void reuse_cache(io_channel channel, struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600385 struct unix_cache *cache, unsigned long long block)
Theodore Ts'o82c46602002-11-09 14:56:17 -0500386{
387 if (cache->dirty && cache->in_use)
388 raw_write_blk(channel, data, cache->block, 1, cache->buf);
389
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000390 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500391 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000392 cache->block = block;
393 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000394}
395
396/*
397 * Flush all of the blocks in the cache
398 */
399static errcode_t flush_cached_blocks(io_channel channel,
400 struct unix_private_data *data,
401 int invalidate)
402
403{
404 struct unix_cache *cache;
405 errcode_t retval, retval2;
406 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400407
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000408 retval2 = 0;
409 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
410 if (!cache->in_use)
411 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400412
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000413 if (invalidate)
414 cache->in_use = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400415
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000416 if (!cache->dirty)
417 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400418
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000419 retval = raw_write_blk(channel, data,
420 cache->block, 1, cache->buf);
421 if (retval)
422 retval2 = retval;
423 else
424 cache->dirty = 0;
425 }
426 return retval2;
427}
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400428#endif /* NO_IO_CACHE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000429
Lukas Czernerd8665992010-11-18 03:38:37 +0000430#ifdef __linux__
431#ifndef BLKDISCARDZEROES
432#define BLKDISCARDZEROES _IO(0x12,124)
433#endif
434#endif
435
Theodore Ts'o3839e651997-04-26 13:21:57 +0000436static errcode_t unix_open(const char *name, int flags, io_channel *channel)
437{
438 io_channel io = NULL;
439 struct unix_private_data *data = NULL;
440 errcode_t retval;
Lukas Czernerd8665992010-11-18 03:38:37 +0000441 int open_flags, zeroes = 0;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500442 struct stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400443#ifdef __linux__
444 struct utsname ut;
445#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000446
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000447 if (name == 0)
448 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400449 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000450 if (retval)
451 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000452 memset(io, 0, sizeof(struct struct_io_channel));
453 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400454 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000455 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000456 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000457
Theodore Ts'o3839e651997-04-26 13:21:57 +0000458 io->manager = unix_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400459 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000460 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000462
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463 strcpy(io->name, name);
464 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000465 io->block_size = 1024;
466 io->read_error = 0;
467 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000468 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469
470 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000471 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400472 data->io_stats.num_fields = 2;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000473
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000474 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
Theodore Ts'ofa6c6532006-03-18 18:57:44 -0500475 if (flags & IO_FLAG_EXCLUSIVE)
476 open_flags |= O_EXCL;
Andreas Dilger534a4c32011-06-11 11:50:01 -0400477#ifdef O_DIRECT
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400478 if (flags & IO_FLAG_DIRECT_IO)
479 open_flags |= O_DIRECT;
Andreas Dilger534a4c32011-06-11 11:50:01 -0400480#endif
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400481 data->flags = flags;
482
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000483#ifdef HAVE_OPEN64
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500484 data->dev = open64(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000485#else
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500486 data->dev = open(io->name, open_flags);
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000487#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488 if (data->dev < 0) {
489 retval = errno;
490 goto cleanup;
491 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500492
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400493#ifdef BLKSSZGET
494 if (flags & IO_FLAG_DIRECT_IO) {
495 if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
496 data->align = io->block_size;
497 }
498#endif
499
Lukas Czernerd8665992010-11-18 03:38:37 +0000500#ifdef BLKDISCARDZEROES
501 ioctl(data->dev, BLKDISCARDZEROES, &zeroes);
502 if (zeroes)
503 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
504#endif
505
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400506#if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
507 /*
508 * Some operating systems require that the buffers be aligned,
509 * regardless of O_DIRECT
510 */
511 data->align = 512;
512#endif
513
514
515 if ((retval = alloc_cache(io, data)))
516 goto cleanup;
517
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -0500518#ifdef BLKROGET
519 if (flags & IO_FLAG_RW) {
520 int error;
521 int readonly = 0;
522
523 /* Is the block device actually writable? */
524 error = ioctl(data->dev, BLKROGET, &readonly);
525 if (!error && readonly) {
526 close(data->dev);
527 retval = EPERM;
528 goto cleanup;
529 }
530 }
531#endif
532
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500533#ifdef __linux__
534#undef RLIM_INFINITY
535#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
536#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
537#else
538#define RLIM_INFINITY (~0UL)
539#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500540 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400541 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
542 * block devices are wrongly getting hit by the filesize
543 * limit. This workaround isn't perfect, since it won't work
544 * if glibc wasn't built against 2.2 header files. (Sigh.)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400545 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500546 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400547 if ((flags & IO_FLAG_RW) &&
548 (uname(&ut) == 0) &&
549 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
550 (ut.release[2] == '4') && (ut.release[3] == '.') &&
551 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
552 (ut.release[5] < '8')) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500553 (fstat(data->dev, &st) == 0) &&
554 (S_ISBLK(st.st_mode))) {
555 struct rlimit rlim;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400556
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500557 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500558 setrlimit(RLIMIT_FSIZE, &rlim);
559 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100560 if (((unsigned long) rlim.rlim_cur) <
561 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500562 rlim.rlim_cur = rlim.rlim_max;
563 setrlimit(RLIMIT_FSIZE, &rlim);
564 }
565 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500566#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567 *channel = io;
568 return 0;
569
570cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000571 if (data) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500572 free_cache(data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400573 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000574 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000575 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400576 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000577 return retval;
578}
579
580static errcode_t unix_close(io_channel channel)
581{
582 struct unix_private_data *data;
583 errcode_t retval = 0;
584
Theodore Ts'of3db3561997-04-26 13:34:30 +0000585 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000586 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000587 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000588
589 if (--channel->refcount > 0)
590 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000591
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400592#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000593 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400594#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000595
Theodore Ts'o3839e651997-04-26 13:21:57 +0000596 if (close(data->dev) < 0)
597 retval = errno;
Theodore Ts'o54434922003-12-07 01:28:50 -0500598 free_cache(data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500599
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400600 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000601 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400602 ext2fs_free_mem(&channel->name);
603 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000604 return retval;
605}
606
607static errcode_t unix_set_blksize(io_channel channel, int blksize)
608{
609 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000610 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000611
Theodore Ts'of3db3561997-04-26 13:34:30 +0000612 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000613 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000614 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
615
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400617#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000618 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000619 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400620#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400621
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000622 channel->block_size = blksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500623 free_cache(data);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000624 if ((retval = alloc_cache(channel, data)))
625 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626 }
627 return 0;
628}
629
630
Jose R. Santos59ecd322008-03-03 10:41:24 -0600631static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000632 int count, void *buf)
633{
634 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500635 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000637 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000638 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639
Theodore Ts'of3db3561997-04-26 13:34:30 +0000640 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000641 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000642 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000643
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400644#ifdef NO_IO_CACHE
645 return raw_read_blk(channel, data, block, count, buf);
646#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500648 * If we're doing an odd-sized read or a very large read,
649 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000650 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500651 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000652 if ((retval = flush_cached_blocks(channel, data, 0)))
653 return retval;
654 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000655 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000656
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000657 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000658 while (count > 0) {
659 /* If it's in the cache, use it! */
Theodore Ts'o54434922003-12-07 01:28:50 -0500660 if ((cache = find_cached_block(data, block, &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000661#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400662 printf("Using cached block %lu\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000663#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000664 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000665 count--;
666 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000667 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000668 continue;
669 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400670 if (count == 1) {
671 /*
672 * Special case where we read directly into the
673 * cache buffer; important in the O_DIRECT case
674 */
675 cache = reuse[0];
676 reuse_cache(channel, data, cache, block);
677 if ((retval = raw_read_blk(channel, data, block, 1,
678 cache->buf))) {
679 cache->in_use = 0;
680 return retval;
681 }
682 memcpy(cp, cache->buf, channel->block_size);
683 return 0;
684 }
685
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000686 /*
687 * Find the number of uncached blocks so we can do a
688 * single read request
689 */
690 for (i=1; i < count; i++)
Theodore Ts'o54434922003-12-07 01:28:50 -0500691 if (find_cached_block(data, block+i, &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000692 break;
693#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400694 printf("Reading %d blocks starting at %lu\n", i, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000695#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000696 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000697 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400698
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000699 /* Save the results in the cache */
700 for (j=0; j < i; j++) {
701 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500702 cache = reuse[j];
703 reuse_cache(channel, data, cache, block++);
704 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000705 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000706 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000707 }
708 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400709#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000710}
711
Jose R. Santos59ecd322008-03-03 10:41:24 -0600712static errcode_t unix_read_blk(io_channel channel, unsigned long block,
713 int count, void *buf)
714{
715 return unix_read_blk64(channel, block, count, buf);
716}
717
718static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000719 int count, const void *buf)
720{
721 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500722 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500723 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000724 const char *cp;
725 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000726
Theodore Ts'of3db3561997-04-26 13:34:30 +0000727 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000728 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000729 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000730
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400731#ifdef NO_IO_CACHE
732 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400733#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000734 /*
735 * If we're doing an odd-sized write or a very large write,
736 * flush out the cache completely and then do a direct write.
737 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500738 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000739 if ((retval = flush_cached_blocks(channel, data, 1)))
740 return retval;
741 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000742 }
743
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000744 /*
745 * For a moderate-sized multi-block write, first force a write
746 * if we're in write-through cache mode, and then fill the
747 * cache with the blocks.
748 */
749 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
750 if (writethrough)
751 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400752
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000753 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000754 while (count > 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500755 cache = find_cached_block(data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000756 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500757 cache = reuse;
758 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000759 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500760 memcpy(cache->buf, cp, channel->block_size);
761 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000762 count--;
763 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000764 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000765 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000766 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400767#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000768}
769
Jose R. Santos59ecd322008-03-03 10:41:24 -0600770static errcode_t unix_write_blk(io_channel channel, unsigned long block,
771 int count, const void *buf)
772{
773 return unix_write_blk64(channel, block, count, buf);
774}
775
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000776static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
777 int size, const void *buf)
778{
779 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000780 errcode_t retval = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500781 ssize_t actual;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000782
783 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
784 data = (struct unix_private_data *) channel->private_data;
785 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
786
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400787 if (data->align != 0) {
788#ifdef ALIGN_DEBUG
789 printf("unix_write_byte: O_DIRECT fallback\n");
790#endif
791 return EXT2_ET_UNIMPLEMENTED;
792 }
793
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400794#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000795 /*
796 * Flush out the cache completely
797 */
798 if ((retval = flush_cached_blocks(channel, data, 1)))
799 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400800#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000801
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500802 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000803 return errno;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400804
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000805 actual = write(data->dev, buf, size);
806 if (actual != size)
807 return EXT2_ET_SHORT_WRITE;
808
809 return 0;
810}
811
Theodore Ts'o3839e651997-04-26 13:21:57 +0000812/*
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400813 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000814 */
815static errcode_t unix_flush(io_channel channel)
816{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000817 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000818 errcode_t retval = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400819
Theodore Ts'of3db3561997-04-26 13:34:30 +0000820 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
821 data = (struct unix_private_data *) channel->private_data;
822 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000823
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400824#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000825 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400826#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000827 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000828 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000829}
830
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400831static errcode_t unix_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500832 const char *arg)
833{
834 struct unix_private_data *data;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500835 unsigned long long tmp;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500836 char *end;
837
838 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
839 data = (struct unix_private_data *) channel->private_data;
840 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
841
842 if (!strcmp(option, "offset")) {
843 if (!arg)
844 return EXT2_ET_INVALID_ARGUMENT;
845
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500846 tmp = strtoull(arg, &end, 0);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500847 if (*end)
848 return EXT2_ET_INVALID_ARGUMENT;
849 data->offset = tmp;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500850 if (data->offset < 0)
851 return EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500852 return 0;
853 }
854 return EXT2_ET_INVALID_ARGUMENT;
855}
Lukas Czernere90a59e2010-11-18 03:38:36 +0000856
857#if defined(__linux__) && !defined(BLKDISCARD)
858#define BLKDISCARD _IO(0x12,119)
859#endif
860
861static errcode_t unix_discard(io_channel channel, unsigned long long block,
862 unsigned long long count)
863{
864#ifdef BLKDISCARD
865 struct unix_private_data *data;
866 __uint64_t range[2];
867 int ret;
868
869 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
870 data = (struct unix_private_data *) channel->private_data;
871 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
872
873 range[0] = (__uint64_t)(block) * channel->block_size;
874 range[1] = (__uint64_t)(count) * channel->block_size;
875
876 ret = ioctl(data->dev, BLKDISCARD, &range);
877 if (ret < 0)
878 return errno;
879 return 0;
880#else
881 return EXT2_ET_UNIMPLEMENTED;
882#endif
883}