blob: ecddfa6bc7a5c5c93edd0f06cceeb16cc86409dc [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
Lukas Czernerd2bfdc72011-09-15 23:44:59 -040052#if HAVE_LINUX_FALLOC_H
53#include <linux/falloc.h>
54#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000055
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040056#if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -050057#define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
58#endif
59
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040060#if defined(__linux__) && defined(_IO) && !defined(BLKSSZGET)
61#define BLKSSZGET _IO(0x12,104)/* get block device sector size */
62#endif
63
64#undef ALIGN_DEBUG
65
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000066#include "ext2_fs.h"
Theodore Ts'o7b4e4531997-10-26 03:41:24 +000067#include "ext2fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000068
Theodore Ts'of3db3561997-04-26 13:34:30 +000069/*
70 * For checking structure magic numbers...
71 */
72
73#define EXT2_CHECK_MAGIC(struct, code) \
74 if ((struct)->magic != (code)) return (code)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000075
76struct unix_cache {
77 char *buf;
78 unsigned long block;
79 int access_time;
Matthias Andree83e692e2004-03-30 04:17:14 +020080 unsigned dirty:1;
81 unsigned in_use:1;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000082};
83
84#define CACHE_SIZE 8
Theodore Ts'o82c46602002-11-09 14:56:17 -050085#define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
86#define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000087
Theodore Ts'o3839e651997-04-26 13:21:57 +000088struct unix_private_data {
Theodore Ts'of3db3561997-04-26 13:34:30 +000089 int magic;
Theodore Ts'o3839e651997-04-26 13:21:57 +000090 int dev;
91 int flags;
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040092 int align;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000093 int access_time;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -050094 ext2_loff_t offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +000095 struct unix_cache cache[CACHE_SIZE];
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -040096 void *bounce;
Theodore Ts'o6d96b002007-08-03 20:07:09 -040097 struct struct_io_stats io_stats;
Theodore Ts'o3839e651997-04-26 13:21:57 +000098};
99
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400100#define IS_ALIGNED(n, align) ((((unsigned long) n) & \
101 ((unsigned long) ((align)-1))) == 0)
102
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103static errcode_t unix_open(const char *name, int flags, io_channel *channel);
104static errcode_t unix_close(io_channel channel);
105static errcode_t unix_set_blksize(io_channel channel, int blksize);
106static errcode_t unix_read_blk(io_channel channel, unsigned long block,
107 int count, void *data);
108static errcode_t unix_write_blk(io_channel channel, unsigned long block,
109 int count, const void *data);
110static errcode_t unix_flush(io_channel channel);
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000111static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
112 int size, const void *data);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400113static errcode_t unix_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500114 const char *arg);
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400115static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
116;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500117static void reuse_cache(io_channel channel, struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600118 struct unix_cache *cache, unsigned long long block);
119static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
120 int count, void *data);
121static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
122 int count, const void *data);
Lukas Czernere90a59e2010-11-18 03:38:36 +0000123static errcode_t unix_discard(io_channel channel, unsigned long long block,
124 unsigned long long count);
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500125
Theodore Ts'of3db3561997-04-26 13:34:30 +0000126static struct struct_io_manager struct_unix_manager = {
127 EXT2_ET_MAGIC_IO_MANAGER,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128 "Unix I/O Manager",
129 unix_open,
130 unix_close,
131 unix_set_blksize,
132 unix_read_blk,
133 unix_write_blk,
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000134 unix_flush,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500135 unix_write_byte,
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400136 unix_set_option,
137 unix_get_stats,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600138 unix_read_blk64,
139 unix_write_blk64,
Lukas Czernere90a59e2010-11-18 03:38:36 +0000140 unix_discard,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000141};
142
143io_manager unix_io_manager = &struct_unix_manager;
144
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400145static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
146{
147 errcode_t retval = 0;
148
149 struct unix_private_data *data;
150
151 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
152 data = (struct unix_private_data *) channel->private_data;
153 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
154
155 if (stats)
156 *stats = &data->io_stats;
157
158 return retval;
159}
160
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000161/*
162 * Here are the raw I/O functions
163 */
164static errcode_t raw_read_blk(io_channel channel,
165 struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600166 unsigned long long block,
Theodore Ts'od32c9152011-07-07 13:50:22 -0400167 int count, void *bufv)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000168{
169 errcode_t retval;
Theodore Ts'o54434922003-12-07 01:28:50 -0500170 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000171 ext2_loff_t location;
172 int actual = 0;
Theodore Ts'od32c9152011-07-07 13:50:22 -0400173 unsigned char *buf = bufv;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000174
175 size = (count < 0) ? -count : count * channel->block_size;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400176 data->io_stats.bytes_read += size;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500177 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000178 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
179 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
180 goto error_out;
181 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400182 if ((data->align == 0) ||
183 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
184 actual = read(data->dev, buf, size);
185 if (actual != size) {
186 short_read:
187 if (actual < 0)
188 actual = 0;
189 retval = EXT2_ET_SHORT_READ;
190 goto error_out;
191 }
192 return 0;
193 }
194
195#ifdef ALIGN_DEBUG
196 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
197 (unsigned long) size);
198#endif
199
200 /*
201 * The buffer or size which we're trying to read isn't aligned
202 * to the O_DIRECT rules, so we need to do this the hard way...
203 */
204 while (size > 0) {
205 actual = read(data->dev, data->bounce, channel->block_size);
206 if (actual != channel->block_size)
207 goto short_read;
208 actual = size;
209 if (size > channel->block_size)
210 actual = channel->block_size;
211 memcpy(buf, data->bounce, actual);
212 size -= actual;
213 buf += actual;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000214 }
215 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400216
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000217error_out:
218 memset((char *) buf+actual, 0, size-actual);
219 if (channel->read_error)
220 retval = (channel->read_error)(channel, block, count, buf,
221 size, actual, retval);
222 return retval;
223}
224
225static errcode_t raw_write_blk(io_channel channel,
226 struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600227 unsigned long long block,
Theodore Ts'od32c9152011-07-07 13:50:22 -0400228 int count, const void *bufv)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000229{
Theodore Ts'o54434922003-12-07 01:28:50 -0500230 ssize_t size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000231 ext2_loff_t location;
232 int actual = 0;
233 errcode_t retval;
Theodore Ts'od32c9152011-07-07 13:50:22 -0400234 const unsigned char *buf = bufv;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000235
236 if (count == 1)
237 size = channel->block_size;
238 else {
239 if (count < 0)
240 size = -count;
241 else
242 size = count * channel->block_size;
243 }
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400244 data->io_stats.bytes_written += size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000245
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500246 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000247 if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
248 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
249 goto error_out;
250 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400251
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400252 if ((data->align == 0) ||
253 ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
254 actual = write(data->dev, buf, size);
255 if (actual != size) {
256 short_write:
257 retval = EXT2_ET_SHORT_WRITE;
258 goto error_out;
259 }
260 return 0;
261 }
262
263#ifdef ALIGN_DEBUG
264 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
265 (unsigned long) size);
266#endif
267 /*
268 * The buffer or size which we're trying to write isn't aligned
269 * to the O_DIRECT rules, so we need to do this the hard way...
270 */
271 while (size > 0) {
272 if (size < channel->block_size) {
273 actual = read(data->dev, data->bounce,
274 channel->block_size);
275 if (actual != channel->block_size) {
276 retval = EXT2_ET_SHORT_READ;
277 goto error_out;
278 }
279 }
280 actual = size;
281 if (size > channel->block_size)
282 actual = channel->block_size;
283 memcpy(data->bounce, buf, actual);
284 actual = write(data->dev, data->bounce, channel->block_size);
285 if (actual != channel->block_size)
286 goto short_write;
287 size -= actual;
288 buf += actual;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000289 }
290 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400291
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000292error_out:
293 if (channel->write_error)
294 retval = (channel->write_error)(channel, block, count, buf,
295 size, actual, retval);
296 return retval;
297}
298
299
300/*
301 * Here we implement the cache functions
302 */
303
304/* Allocate the cache buffers */
305static errcode_t alloc_cache(io_channel channel,
306 struct unix_private_data *data)
307{
308 errcode_t retval;
309 struct unix_cache *cache;
310 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400311
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000312 data->access_time = 0;
313 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
314 cache->block = 0;
315 cache->access_time = 0;
316 cache->dirty = 0;
317 cache->in_use = 0;
Theodore Ts'ofaafdb72010-09-23 13:06:31 -0400318 if (cache->buf)
319 ext2fs_free_mem(&cache->buf);
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400320 retval = ext2fs_get_memalign(channel->block_size,
321 data->align, &cache->buf);
322 if (retval)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000323 return retval;
324 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400325 if (data->align) {
326 if (data->bounce)
327 ext2fs_free_mem(&data->bounce);
328 retval = ext2fs_get_memalign(channel->block_size, data->align,
329 &data->bounce);
330 }
331 return retval;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000332}
333
334/* Free the cache buffers */
Theodore Ts'o54434922003-12-07 01:28:50 -0500335static void free_cache(struct unix_private_data *data)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000336{
337 struct unix_cache *cache;
338 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400339
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000340 data->access_time = 0;
341 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
342 cache->block = 0;
343 cache->access_time = 0;
344 cache->dirty = 0;
345 cache->in_use = 0;
346 if (cache->buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400347 ext2fs_free_mem(&cache->buf);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000348 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400349 if (data->bounce)
350 ext2fs_free_mem(&data->bounce);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000351}
352
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400353#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000354/*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500355 * Try to find a block in the cache. If the block is not found, and
356 * eldest is a non-zero pointer, then fill in eldest with the cache
357 * entry to that should be reused.
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000358 */
Theodore Ts'o54434922003-12-07 01:28:50 -0500359static struct unix_cache *find_cached_block(struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600360 unsigned long long block,
Theodore Ts'o82c46602002-11-09 14:56:17 -0500361 struct unix_cache **eldest)
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000362{
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000363 struct unix_cache *cache, *unused_cache, *oldest_cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000364 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400365
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000366 unused_cache = oldest_cache = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000367 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
368 if (!cache->in_use) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500369 if (!unused_cache)
370 unused_cache = cache;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000371 continue;
372 }
373 if (cache->block == block) {
374 cache->access_time = ++data->access_time;
375 return cache;
376 }
377 if (!oldest_cache ||
378 (cache->access_time < oldest_cache->access_time))
379 oldest_cache = cache;
380 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500381 if (eldest)
382 *eldest = (unused_cache) ? unused_cache : oldest_cache;
383 return 0;
384}
385
386/*
387 * Reuse a particular cache entry for another block.
388 */
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500389static void reuse_cache(io_channel channel, struct unix_private_data *data,
Jose R. Santos59ecd322008-03-03 10:41:24 -0600390 struct unix_cache *cache, unsigned long long block)
Theodore Ts'o82c46602002-11-09 14:56:17 -0500391{
392 if (cache->dirty && cache->in_use)
393 raw_write_blk(channel, data, cache->block, 1, cache->buf);
394
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000395 cache->in_use = 1;
Theodore Ts'o1d47dfb2002-11-09 10:33:49 -0500396 cache->dirty = 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000397 cache->block = block;
398 cache->access_time = ++data->access_time;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000399}
400
401/*
402 * Flush all of the blocks in the cache
403 */
404static errcode_t flush_cached_blocks(io_channel channel,
405 struct unix_private_data *data,
406 int invalidate)
407
408{
409 struct unix_cache *cache;
410 errcode_t retval, retval2;
411 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400412
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000413 retval2 = 0;
414 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
415 if (!cache->in_use)
416 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400417
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000418 if (invalidate)
419 cache->in_use = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400420
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000421 if (!cache->dirty)
422 continue;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400423
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000424 retval = raw_write_blk(channel, data,
425 cache->block, 1, cache->buf);
426 if (retval)
427 retval2 = retval;
428 else
429 cache->dirty = 0;
430 }
431 return retval2;
432}
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400433#endif /* NO_IO_CACHE */
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000434
Lukas Czernerd8665992010-11-18 03:38:37 +0000435#ifdef __linux__
436#ifndef BLKDISCARDZEROES
437#define BLKDISCARDZEROES _IO(0x12,124)
438#endif
439#endif
440
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441static errcode_t unix_open(const char *name, int flags, io_channel *channel)
442{
443 io_channel io = NULL;
444 struct unix_private_data *data = NULL;
445 errcode_t retval;
Lukas Czernerd8665992010-11-18 03:38:37 +0000446 int open_flags, zeroes = 0;
Lukas Czernerc859cb12011-09-15 23:44:48 -0400447 ext2fs_struct_stat st;
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400448#ifdef __linux__
449 struct utsname ut;
450#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000451
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000452 if (name == 0)
453 return EXT2_ET_BAD_DEVICE_NAME;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400454 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000455 if (retval)
456 return retval;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000457 memset(io, 0, sizeof(struct struct_io_channel));
458 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400459 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
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 io->manager = unix_io_manager;
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400464 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000465 if (retval)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000466 goto cleanup;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000467
Theodore Ts'o3839e651997-04-26 13:21:57 +0000468 strcpy(io->name, name);
469 io->private_data = data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000470 io->block_size = 1024;
471 io->read_error = 0;
472 io->write_error = 0;
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000473 io->refcount = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000474
475 memset(data, 0, sizeof(struct unix_private_data));
Theodore Ts'of3db3561997-04-26 13:34:30 +0000476 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400477 data->io_stats.num_fields = 2;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000478
Theodore Ts'odc5f68c2000-05-25 23:31:54 +0000479 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
Theodore Ts'ofa6c6532006-03-18 18:57:44 -0500480 if (flags & IO_FLAG_EXCLUSIVE)
481 open_flags |= O_EXCL;
Andreas Dilger534a4c32011-06-11 11:50:01 -0400482#ifdef O_DIRECT
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400483 if (flags & IO_FLAG_DIRECT_IO)
484 open_flags |= O_DIRECT;
Andreas Dilger534a4c32011-06-11 11:50:01 -0400485#endif
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400486 data->flags = flags;
487
Lukas Czernerc859cb12011-09-15 23:44:48 -0400488 data->dev = ext2fs_open_file(io->name, open_flags);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000489 if (data->dev < 0) {
490 retval = errno;
491 goto cleanup;
492 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500493
Lukas Czernerd2bfdc72011-09-15 23:44:59 -0400494 /*
495 * If the device is really a block device, then set the
496 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
497 * because we are going to use punch hole instead of discard
498 * and if it succeed, subsequent read from sparse area returns
499 * zero.
500 */
501 if (ext2fs_stat(io->name, &st) == 0) {
502 if (S_ISBLK(st.st_mode))
503 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
504 else
505 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
506 }
507
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400508#ifdef BLKSSZGET
509 if (flags & IO_FLAG_DIRECT_IO) {
510 if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
511 data->align = io->block_size;
512 }
513#endif
514
Lukas Czernerd8665992010-11-18 03:38:37 +0000515#ifdef BLKDISCARDZEROES
516 ioctl(data->dev, BLKDISCARDZEROES, &zeroes);
517 if (zeroes)
518 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
519#endif
520
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400521#if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
522 /*
523 * Some operating systems require that the buffers be aligned,
524 * regardless of O_DIRECT
525 */
526 data->align = 512;
527#endif
528
529
530 if ((retval = alloc_cache(io, data)))
531 goto cleanup;
532
Eric Sandeen7ed7a4b2008-10-10 17:17:43 -0500533#ifdef BLKROGET
534 if (flags & IO_FLAG_RW) {
535 int error;
536 int readonly = 0;
537
538 /* Is the block device actually writable? */
539 error = ioctl(data->dev, BLKROGET, &readonly);
540 if (!error && readonly) {
541 close(data->dev);
542 retval = EPERM;
543 goto cleanup;
544 }
545 }
546#endif
547
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500548#ifdef __linux__
549#undef RLIM_INFINITY
550#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
551#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
552#else
553#define RLIM_INFINITY (~0UL)
554#endif
Theodore Ts'o8880e752001-11-26 21:05:36 -0500555 /*
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400556 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
557 * block devices are wrongly getting hit by the filesize
558 * limit. This workaround isn't perfect, since it won't work
559 * if glibc wasn't built against 2.2 header files. (Sigh.)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400560 *
Theodore Ts'o8880e752001-11-26 21:05:36 -0500561 */
Theodore Ts'of154d2f2002-07-14 08:33:32 -0400562 if ((flags & IO_FLAG_RW) &&
563 (uname(&ut) == 0) &&
564 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
565 (ut.release[2] == '4') && (ut.release[3] == '.') &&
566 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
567 (ut.release[5] < '8')) &&
Lukas Czernerc859cb12011-09-15 23:44:48 -0400568 (ext2fs_stat(io->name, &st) == 0) &&
Theodore Ts'o8880e752001-11-26 21:05:36 -0500569 (S_ISBLK(st.st_mode))) {
570 struct rlimit rlim;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400571
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500572 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
Theodore Ts'o8880e752001-11-26 21:05:36 -0500573 setrlimit(RLIMIT_FSIZE, &rlim);
574 getrlimit(RLIMIT_FSIZE, &rlim);
Theodore Ts'obd278802001-12-03 05:47:32 +0100575 if (((unsigned long) rlim.rlim_cur) <
576 ((unsigned long) rlim.rlim_max)) {
Theodore Ts'o8880e752001-11-26 21:05:36 -0500577 rlim.rlim_cur = rlim.rlim_max;
578 setrlimit(RLIMIT_FSIZE, &rlim);
579 }
580 }
Theodore Ts'o64e1b272002-02-23 18:50:32 -0500581#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000582 *channel = io;
583 return 0;
584
585cleanup:
Theodore Ts'o3839e651997-04-26 13:21:57 +0000586 if (data) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500587 free_cache(data);
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400588 ext2fs_free_mem(&data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000589 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000590 if (io)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400591 ext2fs_free_mem(&io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592 return retval;
593}
594
595static errcode_t unix_close(io_channel channel)
596{
597 struct unix_private_data *data;
598 errcode_t retval = 0;
599
Theodore Ts'of3db3561997-04-26 13:34:30 +0000600 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000601 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000602 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oa29f4d31997-04-29 21:26:48 +0000603
604 if (--channel->refcount > 0)
605 return 0;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000606
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400607#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000608 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400609#endif
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000610
Theodore Ts'o3839e651997-04-26 13:21:57 +0000611 if (close(data->dev) < 0)
612 retval = errno;
Theodore Ts'o54434922003-12-07 01:28:50 -0500613 free_cache(data);
Theodore Ts'of12e2852002-02-20 01:06:25 -0500614
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400615 ext2fs_free_mem(&channel->private_data);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616 if (channel->name)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400617 ext2fs_free_mem(&channel->name);
618 ext2fs_free_mem(&channel);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000619 return retval;
620}
621
622static errcode_t unix_set_blksize(io_channel channel, int blksize)
623{
624 struct unix_private_data *data;
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000625 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626
Theodore Ts'of3db3561997-04-26 13:34:30 +0000627 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000629 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
630
Theodore Ts'o3839e651997-04-26 13:21:57 +0000631 if (channel->block_size != blksize) {
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400632#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000633 if ((retval = flush_cached_blocks(channel, data, 0)))
Theodore Ts'o7b4e4531997-10-26 03:41:24 +0000634 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400635#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400636
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000637 channel->block_size = blksize;
Theodore Ts'o54434922003-12-07 01:28:50 -0500638 free_cache(data);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000639 if ((retval = alloc_cache(channel, data)))
640 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000641 }
642 return 0;
643}
644
645
Jose R. Santos59ecd322008-03-03 10:41:24 -0600646static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647 int count, void *buf)
648{
649 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500650 struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000652 char *cp;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000653 int i, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000654
Theodore Ts'of3db3561997-04-26 13:34:30 +0000655 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000657 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000658
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400659#ifdef NO_IO_CACHE
660 return raw_read_blk(channel, data, block, count, buf);
661#else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000662 /*
Theodore Ts'o82c46602002-11-09 14:56:17 -0500663 * If we're doing an odd-sized read or a very large read,
664 * flush out the cache and then do a direct read.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000665 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500666 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000667 if ((retval = flush_cached_blocks(channel, data, 0)))
668 return retval;
669 return raw_read_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000670 }
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000671
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000672 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000673 while (count > 0) {
674 /* If it's in the cache, use it! */
Theodore Ts'o54434922003-12-07 01:28:50 -0500675 if ((cache = find_cached_block(data, block, &reuse[0]))) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000676#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400677 printf("Using cached block %lu\n", block);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000678#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000679 memcpy(cp, cache->buf, channel->block_size);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000680 count--;
681 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000682 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000683 continue;
684 }
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400685 if (count == 1) {
686 /*
687 * Special case where we read directly into the
688 * cache buffer; important in the O_DIRECT case
689 */
690 cache = reuse[0];
691 reuse_cache(channel, data, cache, block);
692 if ((retval = raw_read_blk(channel, data, block, 1,
693 cache->buf))) {
694 cache->in_use = 0;
695 return retval;
696 }
697 memcpy(cp, cache->buf, channel->block_size);
698 return 0;
699 }
700
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000701 /*
702 * Find the number of uncached blocks so we can do a
703 * single read request
704 */
705 for (i=1; i < count; i++)
Theodore Ts'o54434922003-12-07 01:28:50 -0500706 if (find_cached_block(data, block+i, &reuse[i]))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000707 break;
708#ifdef DEBUG
Eric Sandeend0ff90d2006-09-12 14:56:15 -0400709 printf("Reading %d blocks starting at %lu\n", i, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000710#endif
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000711 if ((retval = raw_read_blk(channel, data, block, i, cp)))
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000712 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400713
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000714 /* Save the results in the cache */
715 for (j=0; j < i; j++) {
716 count--;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500717 cache = reuse[j];
718 reuse_cache(channel, data, cache, block++);
719 memcpy(cache->buf, cp, channel->block_size);
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000720 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000721 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000722 }
723 return 0;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400724#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000725}
726
Jose R. Santos59ecd322008-03-03 10:41:24 -0600727static errcode_t unix_read_blk(io_channel channel, unsigned long block,
728 int count, void *buf)
729{
730 return unix_read_blk64(channel, block, count, buf);
731}
732
733static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000734 int count, const void *buf)
735{
736 struct unix_private_data *data;
Theodore Ts'o82c46602002-11-09 14:56:17 -0500737 struct unix_cache *cache, *reuse;
Theodore Ts'o23b7c8b2003-01-22 18:30:01 -0500738 errcode_t retval = 0;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000739 const char *cp;
740 int writethrough;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000741
Theodore Ts'of3db3561997-04-26 13:34:30 +0000742 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000743 data = (struct unix_private_data *) channel->private_data;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000744 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000745
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400746#ifdef NO_IO_CACHE
747 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400748#else
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000749 /*
750 * If we're doing an odd-sized write or a very large write,
751 * flush out the cache completely and then do a direct write.
752 */
Theodore Ts'o82c46602002-11-09 14:56:17 -0500753 if (count < 0 || count > WRITE_DIRECT_SIZE) {
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000754 if ((retval = flush_cached_blocks(channel, data, 1)))
755 return retval;
756 return raw_write_blk(channel, data, block, count, buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000757 }
758
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000759 /*
760 * For a moderate-sized multi-block write, first force a write
761 * if we're in write-through cache mode, and then fill the
762 * cache with the blocks.
763 */
764 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
765 if (writethrough)
766 retval = raw_write_blk(channel, data, block, count, buf);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400767
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000768 cp = buf;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000769 while (count > 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500770 cache = find_cached_block(data, block, &reuse);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000771 if (!cache) {
Theodore Ts'o82c46602002-11-09 14:56:17 -0500772 cache = reuse;
773 reuse_cache(channel, data, cache, block);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000774 }
Theodore Ts'o82c46602002-11-09 14:56:17 -0500775 memcpy(cache->buf, cp, channel->block_size);
776 cache->dirty = !writethrough;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000777 count--;
778 block++;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000779 cp += channel->block_size;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000780 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000781 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400782#endif /* NO_IO_CACHE */
Theodore Ts'o3839e651997-04-26 13:21:57 +0000783}
784
Jose R. Santos59ecd322008-03-03 10:41:24 -0600785static errcode_t unix_write_blk(io_channel channel, unsigned long block,
786 int count, const void *buf)
787{
788 return unix_write_blk64(channel, block, count, buf);
789}
790
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000791static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
792 int size, const void *buf)
793{
794 struct unix_private_data *data;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000795 errcode_t retval = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -0500796 ssize_t actual;
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000797
798 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
799 data = (struct unix_private_data *) channel->private_data;
800 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
801
Theodore Ts'o7f1a1fb2010-09-24 10:02:25 -0400802 if (data->align != 0) {
803#ifdef ALIGN_DEBUG
804 printf("unix_write_byte: O_DIRECT fallback\n");
805#endif
806 return EXT2_ET_UNIMPLEMENTED;
807 }
808
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400809#ifndef NO_IO_CACHE
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000810 /*
811 * Flush out the cache completely
812 */
813 if ((retval = flush_cached_blocks(channel, data, 1)))
814 return retval;
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400815#endif
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000816
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500817 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000818 return errno;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400819
Theodore Ts'oc180ac82000-10-26 20:24:43 +0000820 actual = write(data->dev, buf, size);
821 if (actual != size)
822 return EXT2_ET_SHORT_WRITE;
823
824 return 0;
825}
826
Theodore Ts'o3839e651997-04-26 13:21:57 +0000827/*
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400828 * Flush data buffers to disk.
Theodore Ts'o3839e651997-04-26 13:21:57 +0000829 */
830static errcode_t unix_flush(io_channel channel)
831{
Theodore Ts'of3db3561997-04-26 13:34:30 +0000832 struct unix_private_data *data;
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000833 errcode_t retval = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400834
Theodore Ts'of3db3561997-04-26 13:34:30 +0000835 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
836 data = (struct unix_private_data *) channel->private_data;
837 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000838
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400839#ifndef NO_IO_CACHE
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000840 retval = flush_cached_blocks(channel, data, 0);
Theodore Ts'ob8a95312003-05-13 23:41:29 -0400841#endif
Theodore Ts'o36f21431997-06-14 07:25:40 +0000842 fsync(data->dev);
Theodore Ts'oadfc8c62000-10-18 19:22:24 +0000843 return retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000844}
845
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400846static errcode_t unix_set_option(io_channel channel, const char *option,
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500847 const char *arg)
848{
849 struct unix_private_data *data;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500850 unsigned long long tmp;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500851 char *end;
852
853 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
854 data = (struct unix_private_data *) channel->private_data;
855 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
856
857 if (!strcmp(option, "offset")) {
858 if (!arg)
859 return EXT2_ET_INVALID_ARGUMENT;
860
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500861 tmp = strtoull(arg, &end, 0);
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500862 if (*end)
863 return EXT2_ET_INVALID_ARGUMENT;
864 data->offset = tmp;
Theodore Ts'o2aee23f2006-11-12 10:40:40 -0500865 if (data->offset < 0)
866 return EXT2_ET_INVALID_ARGUMENT;
Theodore Ts'o2e8ca9a2004-11-30 14:07:11 -0500867 return 0;
868 }
869 return EXT2_ET_INVALID_ARGUMENT;
870}
Lukas Czernere90a59e2010-11-18 03:38:36 +0000871
872#if defined(__linux__) && !defined(BLKDISCARD)
Lukas Czernerd2bfdc72011-09-15 23:44:59 -0400873#define BLKDISCARD _IO(0x12,119)
Lukas Czernere90a59e2010-11-18 03:38:36 +0000874#endif
875
876static errcode_t unix_discard(io_channel channel, unsigned long long block,
877 unsigned long long count)
878{
Lukas Czernere90a59e2010-11-18 03:38:36 +0000879 struct unix_private_data *data;
880 __uint64_t range[2];
881 int ret;
882
883 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
884 data = (struct unix_private_data *) channel->private_data;
885 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
886
Lukas Czernerd2bfdc72011-09-15 23:44:59 -0400887 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
888#ifdef BLKDISCARD
889 range[0] = (__uint64_t)(block) * channel->block_size;
890 range[1] = (__uint64_t)(count) * channel->block_size;
Lukas Czernere90a59e2010-11-18 03:38:36 +0000891
Lukas Czernerd2bfdc72011-09-15 23:44:59 -0400892 ret = ioctl(data->dev, BLKDISCARD, &range);
Lukas Czernere90a59e2010-11-18 03:38:36 +0000893#else
Lukas Czernerd2bfdc72011-09-15 23:44:59 -0400894 goto unimplemented;
Lukas Czernere90a59e2010-11-18 03:38:36 +0000895#endif
Lukas Czernerd2bfdc72011-09-15 23:44:59 -0400896 } else {
897#ifdef FALLOC_FL_PUNCH_HOLE
898 /*
899 * If we are not on block device, try to use punch hole
900 * to reclaim free space.
901 */
902 ret = fallocate(data->dev,
903 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
904 (off_t)(block) * channel->block_size,
905 (off_t)(count) * channel->block_size);
906#else
907 goto unimplemented;
908#endif
909 }
910 if (ret < 0) {
911 if (errno == EOPNOTSUPP)
912 goto unimplemented;
913 return errno;
914 }
915 return 0;
916unimplemented:
917 return EXT2_ET_UNIMPLEMENTED;
Lukas Czernere90a59e2010-11-18 03:38:36 +0000918}