]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - lib/ext2fs/unix_io.c
libext2fs: unix_io: fix potential error path deadlock in reuse_cache()
[thirdparty/e2fsprogs.git] / lib / ext2fs / unix_io.c
CommitLineData
3839e657 1/*
fff45483 2 * unix_io.c --- This is the Unix (well, really POSIX) implementation
a4613d13 3 * of the I/O manager.
3839e657
TT
4 *
5 * Implements a one-block write-through cache.
6 *
efc6f628 7 * Includes support for Windows NT support under Cygwin.
fff45483 8 *
64e1b274 9 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
a4613d13 10 * 2002 by Theodore Ts'o.
19c78dc0
TT
11 *
12 * %Begin-Header%
543547a5
TT
13 * This file may be redistributed under the terms of the GNU Library
14 * General Public License, version 2.
19c78dc0 15 * %End-Header%
3839e657
TT
16 */
17
ca82d22b 18#if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
ca209dc6
AD
19#define _XOPEN_SOURCE 600
20#define _DARWIN_C_SOURCE
21#define _FILE_OFFSET_BITS 64
f1644c32 22#ifndef _LARGEFILE_SOURCE
dc5f68ca 23#define _LARGEFILE_SOURCE
f1644c32
TT
24#endif
25#ifndef _LARGEFILE64_SOURCE
dc5f68ca 26#define _LARGEFILE64_SOURCE
f1644c32 27#endif
cf5301d7 28#ifndef _GNU_SOURCE
7f1a1fbf 29#define _GNU_SOURCE
cf5301d7 30#endif
ca82d22b 31#endif
dc5f68ca 32
d1154eb4 33#include "config.h"
3839e657
TT
34#include <stdio.h>
35#include <string.h>
4cbe8af4 36#if HAVE_UNISTD_H
3839e657 37#include <unistd.h>
4cbe8af4 38#endif
c4e749ab
TT
39#if HAVE_ERRNO_H
40#include <errno.h>
41#endif
3839e657
TT
42#include <fcntl.h>
43#include <time.h>
f154d2f6
TT
44#ifdef __linux__
45#include <sys/utsname.h>
46#endif
ca209dc6
AD
47#if HAVE_SYS_TYPES_H
48#include <sys/types.h>
49#endif
7ed7a4b6
ES
50#ifdef HAVE_SYS_IOCTL_H
51#include <sys/ioctl.h>
52#endif
53#ifdef HAVE_SYS_MOUNT_H
54#include <sys/mount.h>
55#endif
224b1567
TT
56#ifdef HAVE_SYS_PRCTL_H
57#include <sys/prctl.h>
58#else
59#define PR_GET_DUMPABLE 3
60#endif
1d2ff46a 61#if HAVE_SYS_STAT_H
3839e657 62#include <sys/stat.h>
1d2ff46a 63#endif
fff45483 64#if HAVE_SYS_RESOURCE_H
8880e759 65#include <sys/resource.h>
fff45483 66#endif
d2bfdc7f
LC
67#if HAVE_LINUX_FALLOC_H
68#include <linux/falloc.h>
69#endif
f20627cc
TT
70#ifdef HAVE_PTHREAD
71#include <pthread.h>
72#endif
3839e657 73
7f1a1fbf 74#if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
7ed7a4b6
ES
75#define BLKROGET _IO(0x12, 94) /* Get read-only status (0 = read_write). */
76#endif
77
7f1a1fbf
TT
78#undef ALIGN_DEBUG
79
b5abe6fa 80#include "ext2_fs.h"
7b4e4534 81#include "ext2fs.h"
f61aa76e 82#include "ext2fsP.h"
3839e657 83
f3db3566
TT
84/*
85 * For checking structure magic numbers...
86 */
87
88#define EXT2_CHECK_MAGIC(struct, code) \
89 if ((struct)->magic != (code)) return (code)
adfc8c6c
TT
90
91struct unix_cache {
40024fdb
TT
92 char *buf;
93 unsigned long long block;
94 int access_time;
95 unsigned dirty:1;
96 unsigned in_use:1;
f6cb7f01 97 unsigned write_err:1;
adfc8c6c
TT
98};
99
100#define CACHE_SIZE 8
82c4660c
TT
101#define WRITE_DIRECT_SIZE 4 /* Must be smaller than CACHE_SIZE */
102#define READ_DIRECT_SIZE 4 /* Should be smaller than CACHE_SIZE */
adfc8c6c 103
3839e657 104struct unix_private_data {
f3db3566 105 int magic;
3839e657
TT
106 int dev;
107 int flags;
7f1a1fbf 108 int align;
adfc8c6c 109 int access_time;
2e8ca9a2 110 ext2_loff_t offset;
adfc8c6c 111 struct unix_cache cache[CACHE_SIZE];
7f1a1fbf 112 void *bounce;
6d96b00d 113 struct struct_io_stats io_stats;
f20627cc
TT
114#ifdef HAVE_PTHREAD
115 pthread_mutex_t cache_mutex;
116 pthread_mutex_t bounce_mutex;
117 pthread_mutex_t stats_mutex;
118#endif
3839e657
TT
119};
120
d4e5abfb
AS
121#define IS_ALIGNED(n, align) ((((uintptr_t) n) & \
122 ((uintptr_t) ((align)-1))) == 0)
7f1a1fbf 123
f20627cc
TT
124typedef enum lock_kind {
125 CACHE_MTX, BOUNCE_MTX, STATS_MTX
126} kind_t;
127
128#ifdef HAVE_PTHREAD
129static inline pthread_mutex_t *get_mutex(struct unix_private_data *data,
130 kind_t kind)
131{
132 if (data->flags & IO_FLAG_THREADS) {
133 switch (kind) {
134 case CACHE_MTX:
135 return &data->cache_mutex;
136 case BOUNCE_MTX:
137 return &data->bounce_mutex;
138 case STATS_MTX:
139 return &data->stats_mutex;
140 }
141 }
142 return NULL;
143}
144#endif
145
146static inline void mutex_lock(struct unix_private_data *data, kind_t kind)
147{
148#ifdef HAVE_PTHREAD
149 pthread_mutex_t *mtx = get_mutex(data,kind);
150
151 if (mtx)
152 pthread_mutex_lock(mtx);
153#endif
154}
155
156static inline void mutex_unlock(struct unix_private_data *data, kind_t kind)
157{
158#ifdef HAVE_PTHREAD
159 pthread_mutex_t *mtx = get_mutex(data,kind);
160
161 if (mtx)
162 pthread_mutex_unlock(mtx);
163#endif
164}
165
6d96b00d
TT
166static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
167{
a4613d13 168 errcode_t retval = 0;
6d96b00d
TT
169
170 struct unix_private_data *data;
171
172 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
173 data = (struct unix_private_data *) channel->private_data;
174 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
175
f20627cc
TT
176 if (stats) {
177 mutex_lock(data, STATS_MTX);
6d96b00d 178 *stats = &data->io_stats;
f20627cc
TT
179 mutex_unlock(data, STATS_MTX);
180 }
6d96b00d
TT
181
182 return retval;
183}
184
224b1567
TT
185static char *safe_getenv(const char *arg)
186{
187 if ((getuid() != geteuid()) || (getgid() != getegid()))
188 return NULL;
189#ifdef HAVE_PRCTL
190 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
191 return NULL;
192#else
193#if (defined(linux) && defined(SYS_prctl))
194 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
195 return NULL;
196#endif
197#endif
198
199#if defined(HAVE_SECURE_GETENV)
200 return secure_getenv(arg);
201#elif defined(HAVE___SECURE_GETENV)
202 return __secure_getenv(arg);
203#else
204 return getenv(arg);
205#endif
206}
207
adfc8c6c
TT
208/*
209 * Here are the raw I/O functions
210 */
211static errcode_t raw_read_blk(io_channel channel,
212 struct unix_private_data *data,
59ecd32d 213 unsigned long long block,
d32c915a 214 int count, void *bufv)
adfc8c6c
TT
215{
216 errcode_t retval;
54434927 217 ssize_t size;
adfc8c6c
TT
218 ext2_loff_t location;
219 int actual = 0;
d32c915a 220 unsigned char *buf = bufv;
065c01cd 221 ssize_t really_read = 0;
c0015961
TT
222 unsigned long long aligned_blk;
223 int align_size, offset;
adfc8c6c 224
9bfbf1d5 225 size = (count < 0) ? -count : (ext2_loff_t) count * channel->block_size;
f20627cc 226 mutex_lock(data, STATS_MTX);
6d96b00d 227 data->io_stats.bytes_read += size;
f20627cc 228 mutex_unlock(data, STATS_MTX);
2e8ca9a2 229 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
baa35446 230
e1af249a 231 if (data->flags & IO_FLAG_FORCE_BOUNCE)
224b1567 232 goto bounce_read;
224b1567 233
f00948ad 234#ifdef HAVE_PREAD64
baa35446
DW
235 /* Try an aligned pread */
236 if ((channel->align == 0) ||
237 (IS_ALIGNED(buf, channel->align) &&
c0015961 238 IS_ALIGNED(location, channel->align) &&
baa35446 239 IS_ALIGNED(size, channel->align))) {
f00948ad
TT
240 actual = pread64(data->dev, buf, size, location);
241 if (actual == size)
242 return 0;
15cb3b97 243 actual = 0;
f00948ad
TT
244 }
245#elif HAVE_PREAD
246 /* Try an aligned pread */
247 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
248 ((channel->align == 0) ||
249 (IS_ALIGNED(buf, channel->align) &&
c0015961 250 IS_ALIGNED(location, channel->align) &&
f00948ad 251 IS_ALIGNED(size, channel->align)))) {
baa35446
DW
252 actual = pread(data->dev, buf, size, location);
253 if (actual == size)
254 return 0;
15cb3b97 255 actual = 0;
baa35446
DW
256 }
257#endif /* HAVE_PREAD */
258
0a05b903
TT
259 if ((channel->align == 0) ||
260 (IS_ALIGNED(buf, channel->align) &&
c0015961 261 IS_ALIGNED(location, channel->align) &&
0a05b903 262 IS_ALIGNED(size, channel->align))) {
d557b965
TT
263 mutex_lock(data, BOUNCE_MTX);
264 if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
265 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
266 goto error_unlock;
267 }
7f1a1fbf
TT
268 actual = read(data->dev, buf, size);
269 if (actual != size) {
270 short_read:
025b828c
TT
271 if (actual < 0) {
272 retval = errno;
7f1a1fbf 273 actual = 0;
025b828c
TT
274 } else
275 retval = EXT2_ET_SHORT_READ;
d557b965 276 goto error_unlock;
7f1a1fbf 277 }
d557b965 278 goto success_unlock;
adfc8c6c 279 }
fff45483 280
7f1a1fbf
TT
281#ifdef ALIGN_DEBUG
282 printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
283 (unsigned long) size);
fff45483 284#endif
7f1a1fbf
TT
285
286 /*
287 * The buffer or size which we're trying to read isn't aligned
288 * to the O_DIRECT rules, so we need to do this the hard way...
289 */
224b1567 290bounce_read:
e1af249a
TT
291 if (channel->align == 0)
292 channel->align = 1;
c0015961
TT
293 if ((channel->block_size > channel->align) &&
294 (channel->block_size % channel->align) == 0)
295 align_size = channel->block_size;
296 else
297 align_size = channel->align;
298 aligned_blk = location / align_size;
299 offset = location % align_size;
300
d557b965 301 mutex_lock(data, BOUNCE_MTX);
c0015961
TT
302 if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
303 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
d557b965 304 goto error_unlock;
c0015961 305 }
7f1a1fbf 306 while (size > 0) {
c0015961
TT
307 actual = read(data->dev, data->bounce, align_size);
308 if (actual != align_size) {
f20627cc 309 mutex_unlock(data, BOUNCE_MTX);
065c01cd
MA
310 actual = really_read;
311 buf -= really_read;
312 size += really_read;
fff45483 313 goto short_read;
065c01cd 314 }
f53fa5a2
AK
315 if ((actual + offset) > align_size)
316 actual = align_size - offset;
317 if (actual > size)
318 actual = size;
f158f896 319 memcpy(buf, (char *)data->bounce + offset, actual);
c0015961 320
065c01cd 321 really_read += actual;
7f1a1fbf
TT
322 size -= actual;
323 buf += actual;
c0015961
TT
324 offset = 0;
325 aligned_blk++;
fff45483 326 }
d557b965
TT
327success_unlock:
328 mutex_unlock(data, BOUNCE_MTX);
fff45483
TT
329 return 0;
330
d557b965
TT
331error_unlock:
332 mutex_unlock(data, BOUNCE_MTX);
15cb3b97
TT
333 if (actual >= 0 && actual < size)
334 memset((char *) buf+actual, 0, size-actual);
fff45483
TT
335 if (channel->read_error)
336 retval = (channel->read_error)(channel, block, count, buf,
337 size, actual, retval);
338 return retval;
339}
adfc8c6c 340
4073feef
TT
341#define RAW_WRITE_NO_HANDLER 1
342
adfc8c6c
TT
343static errcode_t raw_write_blk(io_channel channel,
344 struct unix_private_data *data,
59ecd32d 345 unsigned long long block,
4073feef
TT
346 int count, const void *bufv,
347 int flags)
adfc8c6c 348{
54434927 349 ssize_t size;
adfc8c6c
TT
350 ext2_loff_t location;
351 int actual = 0;
352 errcode_t retval;
d32c915a 353 const unsigned char *buf = bufv;
c0015961
TT
354 unsigned long long aligned_blk;
355 int align_size, offset;
adfc8c6c
TT
356
357 if (count == 1)
358 size = channel->block_size;
359 else {
360 if (count < 0)
361 size = -count;
362 else
9bfbf1d5 363 size = (ext2_loff_t) count * channel->block_size;
adfc8c6c 364 }
f20627cc 365 mutex_lock(data, STATS_MTX);
6d96b00d 366 data->io_stats.bytes_written += size;
f20627cc 367 mutex_unlock(data, STATS_MTX);
adfc8c6c 368
2e8ca9a2 369 location = ((ext2_loff_t) block * channel->block_size) + data->offset;
baa35446 370
e1af249a 371 if (data->flags & IO_FLAG_FORCE_BOUNCE)
224b1567 372 goto bounce_write;
224b1567 373
f00948ad 374#ifdef HAVE_PWRITE64
baa35446
DW
375 /* Try an aligned pwrite */
376 if ((channel->align == 0) ||
377 (IS_ALIGNED(buf, channel->align) &&
c0015961 378 IS_ALIGNED(location, channel->align) &&
baa35446 379 IS_ALIGNED(size, channel->align))) {
f00948ad
TT
380 actual = pwrite64(data->dev, buf, size, location);
381 if (actual == size)
382 return 0;
383 }
384#elif HAVE_PWRITE
385 /* Try an aligned pwrite */
386 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) &&
387 ((channel->align == 0) ||
388 (IS_ALIGNED(buf, channel->align) &&
c0015961 389 IS_ALIGNED(location, channel->align) &&
f00948ad 390 IS_ALIGNED(size, channel->align)))) {
baa35446
DW
391 actual = pwrite(data->dev, buf, size, location);
392 if (actual == size)
393 return 0;
394 }
395#endif /* HAVE_PWRITE */
396
0a05b903
TT
397 if ((channel->align == 0) ||
398 (IS_ALIGNED(buf, channel->align) &&
c0015961 399 IS_ALIGNED(location, channel->align) &&
0a05b903 400 IS_ALIGNED(size, channel->align))) {
d557b965
TT
401 mutex_lock(data, BOUNCE_MTX);
402 if (ext2fs_llseek(data->dev, location, SEEK_SET) < 0) {
403 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
4c928984 404 goto error_unlock;
d557b965 405 }
7f1a1fbf 406 actual = write(data->dev, buf, size);
d557b965 407 mutex_unlock(data, BOUNCE_MTX);
025b828c
TT
408 if (actual < 0) {
409 retval = errno;
410 goto error_out;
411 }
7f1a1fbf
TT
412 if (actual != size) {
413 short_write:
414 retval = EXT2_ET_SHORT_WRITE;
415 goto error_out;
416 }
417 return 0;
418 }
419
420#ifdef ALIGN_DEBUG
421 printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
422 (unsigned long) size);
423#endif
424 /*
425 * The buffer or size which we're trying to write isn't aligned
426 * to the O_DIRECT rules, so we need to do this the hard way...
427 */
224b1567 428bounce_write:
e1af249a
TT
429 if (channel->align == 0)
430 channel->align = 1;
c0015961
TT
431 if ((channel->block_size > channel->align) &&
432 (channel->block_size % channel->align) == 0)
433 align_size = channel->block_size;
434 else
435 align_size = channel->align;
436 aligned_blk = location / align_size;
437 offset = location % align_size;
438
7f1a1fbf 439 while (size > 0) {
c0015961
TT
440 int actual_w;
441
f20627cc 442 mutex_lock(data, BOUNCE_MTX);
c0015961
TT
443 if (size < align_size || offset) {
444 if (ext2fs_llseek(data->dev, aligned_blk * align_size,
445 SEEK_SET) < 0) {
446 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
d557b965 447 goto error_unlock;
c0015961 448 }
7f1a1fbf 449 actual = read(data->dev, data->bounce,
c0015961
TT
450 align_size);
451 if (actual != align_size) {
065c01cd 452 if (actual < 0) {
025b828c 453 retval = errno;
d557b965 454 goto error_unlock;
065c01cd 455 }
ac3256fd 456 memset((char *) data->bounce + actual, 0,
c0015961 457 align_size - actual);
7f1a1fbf
TT
458 }
459 }
460 actual = size;
f53fa5a2
AK
461 if ((actual + offset) > align_size)
462 actual = align_size - offset;
463 if (actual > size)
464 actual = size;
c0015961
TT
465 memcpy(((char *)data->bounce) + offset, buf, actual);
466 if (ext2fs_llseek(data->dev, aligned_blk * align_size, SEEK_SET) < 0) {
127e2291 467 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
d557b965 468 goto error_unlock;
127e2291 469 }
c0015961 470 actual_w = write(data->dev, data->bounce, align_size);
f20627cc 471 mutex_unlock(data, BOUNCE_MTX);
c0015961 472 if (actual_w < 0) {
025b828c
TT
473 retval = errno;
474 goto error_out;
475 }
c0015961 476 if (actual_w != align_size)
7f1a1fbf
TT
477 goto short_write;
478 size -= actual;
479 buf += actual;
d6cad379 480 location += actual;
c0015961
TT
481 aligned_blk++;
482 offset = 0;
adfc8c6c
TT
483 }
484 return 0;
efc6f628 485
d557b965
TT
486error_unlock:
487 mutex_unlock(data, BOUNCE_MTX);
adfc8c6c 488error_out:
4073feef 489 if (((flags & RAW_WRITE_NO_HANDLER) == 0) && channel->write_error)
adfc8c6c
TT
490 retval = (channel->write_error)(channel, block, count, buf,
491 size, actual, retval);
492 return retval;
493}
494
495
496/*
497 * Here we implement the cache functions
498 */
499
500/* Allocate the cache buffers */
501static errcode_t alloc_cache(io_channel channel,
502 struct unix_private_data *data)
503{
504 errcode_t retval;
505 struct unix_cache *cache;
506 int i;
efc6f628 507
adfc8c6c
TT
508 data->access_time = 0;
509 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
510 cache->block = 0;
511 cache->access_time = 0;
512 cache->dirty = 0;
513 cache->in_use = 0;
faafdb76
TT
514 if (cache->buf)
515 ext2fs_free_mem(&cache->buf);
fd1c5a06 516 retval = io_channel_alloc_buf(channel, 0, &cache->buf);
7f1a1fbf 517 if (retval)
adfc8c6c
TT
518 return retval;
519 }
224b1567 520 if (channel->align || data->flags & IO_FLAG_FORCE_BOUNCE) {
7f1a1fbf
TT
521 if (data->bounce)
522 ext2fs_free_mem(&data->bounce);
fd1c5a06 523 retval = io_channel_alloc_buf(channel, 0, &data->bounce);
7f1a1fbf
TT
524 }
525 return retval;
adfc8c6c
TT
526}
527
528/* Free the cache buffers */
54434927 529static void free_cache(struct unix_private_data *data)
adfc8c6c
TT
530{
531 struct unix_cache *cache;
532 int i;
efc6f628 533
adfc8c6c
TT
534 data->access_time = 0;
535 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
536 cache->block = 0;
537 cache->access_time = 0;
538 cache->dirty = 0;
539 cache->in_use = 0;
540 if (cache->buf)
c4e3d3f3 541 ext2fs_free_mem(&cache->buf);
adfc8c6c 542 }
7f1a1fbf
TT
543 if (data->bounce)
544 ext2fs_free_mem(&data->bounce);
adfc8c6c
TT
545}
546
b8a95315 547#ifndef NO_IO_CACHE
adfc8c6c 548/*
82c4660c
TT
549 * Try to find a block in the cache. If the block is not found, and
550 * eldest is a non-zero pointer, then fill in eldest with the cache
551 * entry to that should be reused.
adfc8c6c 552 */
54434927 553static struct unix_cache *find_cached_block(struct unix_private_data *data,
59ecd32d 554 unsigned long long block,
82c4660c 555 struct unix_cache **eldest)
adfc8c6c 556{
31dbecd4 557 struct unix_cache *cache, *unused_cache, *oldest_cache;
adfc8c6c 558 int i;
efc6f628 559
31dbecd4 560 unused_cache = oldest_cache = 0;
adfc8c6c
TT
561 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
562 if (!cache->in_use) {
82c4660c
TT
563 if (!unused_cache)
564 unused_cache = cache;
adfc8c6c
TT
565 continue;
566 }
567 if (cache->block == block) {
568 cache->access_time = ++data->access_time;
569 return cache;
570 }
571 if (!oldest_cache ||
572 (cache->access_time < oldest_cache->access_time))
573 oldest_cache = cache;
574 }
82c4660c
TT
575 if (eldest)
576 *eldest = (unused_cache) ? unused_cache : oldest_cache;
577 return 0;
578}
579
580/*
581 * Reuse a particular cache entry for another block.
582 */
f6cb7f01
TT
583static errcode_t reuse_cache(io_channel channel,
584 struct unix_private_data *data, struct unix_cache *cache,
585 unsigned long long block)
82c4660c 586{
f6cb7f01
TT
587 if (cache->dirty && cache->in_use) {
588 errcode_t retval;
589
590 retval = raw_write_blk(channel, data, cache->block, 1,
591 cache->buf, RAW_WRITE_NO_HANDLER);
592 if (retval) {
593 cache->write_err = 1;
594 return retval;
595 }
596 }
82c4660c 597
adfc8c6c 598 cache->in_use = 1;
1d47dfb9 599 cache->dirty = 0;
f6cb7f01 600 cache->write_err = 0;
adfc8c6c
TT
601 cache->block = block;
602 cache->access_time = ++data->access_time;
f6cb7f01 603 return 0;
adfc8c6c
TT
604}
605
f20627cc
TT
606#define FLUSH_INVALIDATE 0x01
607#define FLUSH_NOLOCK 0x02
608
adfc8c6c
TT
609/*
610 * Flush all of the blocks in the cache
611 */
612static errcode_t flush_cached_blocks(io_channel channel,
613 struct unix_private_data *data,
f20627cc 614 int flags)
adfc8c6c
TT
615{
616 struct unix_cache *cache;
617 errcode_t retval, retval2;
618 int i;
efc6f628 619
adfc8c6c 620 retval2 = 0;
f20627cc
TT
621 if ((flags & FLUSH_NOLOCK) == 0)
622 mutex_lock(data, CACHE_MTX);
adfc8c6c
TT
623 for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
624 if (!cache->in_use)
625 continue;
efc6f628 626
f20627cc 627 if (flags & FLUSH_INVALIDATE)
adfc8c6c 628 cache->in_use = 0;
efc6f628 629
adfc8c6c
TT
630 if (!cache->dirty)
631 continue;
efc6f628 632
adfc8c6c 633 retval = raw_write_blk(channel, data,
4073feef 634 cache->block, 1, cache->buf, 0);
adfc8c6c
TT
635 if (retval)
636 retval2 = retval;
637 else
638 cache->dirty = 0;
639 }
f20627cc
TT
640 if ((flags & FLUSH_NOLOCK) == 0)
641 mutex_unlock(data, CACHE_MTX);
adfc8c6c
TT
642 return retval2;
643}
b8a95315 644#endif /* NO_IO_CACHE */
adfc8c6c 645
d866599a
LC
646#ifdef __linux__
647#ifndef BLKDISCARDZEROES
648#define BLKDISCARDZEROES _IO(0x12,124)
649#endif
650#endif
651
182acd17
AD
652int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
653{
654 if (mode)
655#if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
656 return open64(pathname, flags, mode);
657 else
658 return open64(pathname, flags);
659#else
660 return open(pathname, flags, mode);
661 else
662 return open(pathname, flags);
663#endif
664}
665
666int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
667{
668#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
669 return stat64(path, buf);
670#else
671 return stat(path, buf);
672#endif
673}
674
675int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
676{
677#if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
678 return fstat64(fd, buf);
679#else
680 return fstat(fd, buf);
681#endif
682}
683
4ccf9e4f
AS
684
685static errcode_t unix_open_channel(const char *name, int fd,
686 int flags, io_channel *channel,
687 io_manager io_mgr)
3839e657
TT
688{
689 io_channel io = NULL;
690 struct unix_private_data *data = NULL;
691 errcode_t retval;
c859cb1d 692 ext2fs_struct_stat st;
f154d2f6 693#ifdef __linux__
a4613d13 694 struct utsname ut;
f154d2f6 695#endif
3839e657 696
224b1567
TT
697 if (safe_getenv("UNIX_IO_FORCE_BOUNCE"))
698 flags |= IO_FLAG_FORCE_BOUNCE;
699
e8236b49
TT
700#ifdef __linux__
701 /*
702 * We need to make sure any previous errors in the block
703 * device are thrown away, sigh.
704 */
705 (void) fsync(fd);
706#endif
707
c4e3d3f3 708 retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
7b4e4534 709 if (retval)
624e8ebe 710 goto cleanup;
f3db3566
TT
711 memset(io, 0, sizeof(struct struct_io_channel));
712 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
c4e3d3f3 713 retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
7b4e4534 714 if (retval)
3839e657 715 goto cleanup;
7b4e4534 716
4ccf9e4f 717 io->manager = io_mgr;
c4e3d3f3 718 retval = ext2fs_get_mem(strlen(name)+1, &io->name);
7b4e4534 719 if (retval)
3839e657 720 goto cleanup;
7b4e4534 721
3839e657
TT
722 strcpy(io->name, name);
723 io->private_data = data;
f3db3566
TT
724 io->block_size = 1024;
725 io->read_error = 0;
726 io->write_error = 0;
a29f4d30 727 io->refcount = 1;
f20627cc 728 io->flags = 0;
3839e657
TT
729
730 memset(data, 0, sizeof(struct unix_private_data));
f3db3566 731 data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
6d96b00d 732 data->io_stats.num_fields = 2;
4ccf9e4f
AS
733 data->flags = flags;
734 data->dev = fd;
7b4e4534 735
d9a5d375 736#if defined(O_DIRECT)
4ccf9e4f 737 if (flags & IO_FLAG_DIRECT_IO)
dd0a2679 738 io->align = ext2fs_get_dio_alignment(data->dev);
d9a5d375 739#elif defined(F_NOCACHE)
4ccf9e4f 740 if (flags & IO_FLAG_DIRECT_IO)
dd0a2679 741 io->align = 4096;
534a4c3d 742#endif
64e1b274 743
d2bfdc7f
LC
744 /*
745 * If the device is really a block device, then set the
746 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
747 * because we are going to use punch hole instead of discard
748 * and if it succeed, subsequent read from sparse area returns
749 * zero.
750 */
4ccf9e4f 751 if (ext2fs_fstat(data->dev, &st) == 0) {
f61aa76e 752 if (ext2fsP_is_disk_device(st.st_mode))
d2bfdc7f
LC
753 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
754 else
755 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
756 }
757
d866599a 758#ifdef BLKDISCARDZEROES
1d6fd6d0
AD
759 {
760 int zeroes = 0;
761 if (ioctl(data->dev, BLKDISCARDZEROES, &zeroes) == 0 &&
762 zeroes)
763 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
764 }
d866599a
LC
765#endif
766
3a68c6f1 767#if defined(__CYGWIN__)
7f1a1fbf
TT
768 /*
769 * Some operating systems require that the buffers be aligned,
770 * regardless of O_DIRECT
771 */
dd0a2679
TT
772 if (!io->align)
773 io->align = 512;
7f1a1fbf
TT
774#endif
775
3a68c6f1
TT
776#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
777 if (io->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
778 int dio_align = ext2fs_get_dio_alignment(fd);
779
780 if (io->align < dio_align)
781 io->align = dio_align;
782 }
783#endif
7f1a1fbf
TT
784
785 if ((retval = alloc_cache(io, data)))
786 goto cleanup;
787
7ed7a4b6
ES
788#ifdef BLKROGET
789 if (flags & IO_FLAG_RW) {
790 int error;
791 int readonly = 0;
792
793 /* Is the block device actually writable? */
794 error = ioctl(data->dev, BLKROGET, &readonly);
795 if (!error && readonly) {
7ed7a4b6
ES
796 retval = EPERM;
797 goto cleanup;
798 }
799 }
800#endif
801
64e1b274
TT
802#ifdef __linux__
803#undef RLIM_INFINITY
804#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
805#define RLIM_INFINITY ((unsigned long)(~0UL>>1))
806#else
807#define RLIM_INFINITY (~0UL)
808#endif
8880e759 809 /*
f154d2f6
TT
810 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
811 * block devices are wrongly getting hit by the filesize
812 * limit. This workaround isn't perfect, since it won't work
813 * if glibc wasn't built against 2.2 header files. (Sigh.)
efc6f628 814 *
8880e759 815 */
f154d2f6
TT
816 if ((flags & IO_FLAG_RW) &&
817 (uname(&ut) == 0) &&
818 ((ut.release[0] == '2') && (ut.release[1] == '.') &&
819 (ut.release[2] == '4') && (ut.release[3] == '.') &&
820 (ut.release[4] == '1') && (ut.release[5] >= '0') &&
821 (ut.release[5] < '8')) &&
4ccf9e4f 822 (ext2fs_fstat(data->dev, &st) == 0) &&
f61aa76e 823 (ext2fsP_is_disk_device(st.st_mode))) {
8880e759 824 struct rlimit rlim;
efc6f628 825
64e1b274 826 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
8880e759
TT
827 setrlimit(RLIMIT_FSIZE, &rlim);
828 getrlimit(RLIMIT_FSIZE, &rlim);
bd27880b
TT
829 if (((unsigned long) rlim.rlim_cur) <
830 ((unsigned long) rlim.rlim_max)) {
8880e759
TT
831 rlim.rlim_cur = rlim.rlim_max;
832 setrlimit(RLIMIT_FSIZE, &rlim);
833 }
834 }
f20627cc
TT
835#endif
836#ifdef HAVE_PTHREAD
837 if (flags & IO_FLAG_THREADS) {
838 io->flags |= CHANNEL_FLAGS_THREADS;
839 retval = pthread_mutex_init(&data->cache_mutex, NULL);
840 if (retval)
841 goto cleanup;
842 retval = pthread_mutex_init(&data->bounce_mutex, NULL);
843 if (retval) {
844 pthread_mutex_destroy(&data->cache_mutex);
845 goto cleanup;
846 }
847 retval = pthread_mutex_init(&data->stats_mutex, NULL);
848 if (retval) {
849 pthread_mutex_destroy(&data->cache_mutex);
850 pthread_mutex_destroy(&data->bounce_mutex);
851 goto cleanup;
852 }
853 }
64e1b274 854#endif
3839e657
TT
855 *channel = io;
856 return 0;
857
858cleanup:
3839e657 859 if (data) {
4e0bb5eb
TT
860 if (data->dev >= 0)
861 close(data->dev);
54434927 862 free_cache(data);
c4e3d3f3 863 ext2fs_free_mem(&data);
3839e657 864 }
4e0bb5eb
TT
865 if (io) {
866 if (io->name) {
867 ext2fs_free_mem(&io->name);
868 }
c4e3d3f3 869 ext2fs_free_mem(&io);
4e0bb5eb 870 }
3839e657
TT
871 return retval;
872}
873
4ccf9e4f
AS
874static errcode_t unixfd_open(const char *str_fd, int flags,
875 io_channel *channel)
876{
877 int fd;
878 int fd_flags;
879
880 fd = atoi(str_fd);
f47f3195 881#if defined(HAVE_FCNTL)
4ccf9e4f
AS
882 fd_flags = fcntl(fd, F_GETFD);
883 if (fd_flags == -1)
dd2ed58a 884 return EBADF;
4ccf9e4f
AS
885
886 flags = 0;
887 if (fd_flags & O_RDWR)
888 flags |= IO_FLAG_RW;
889 if (fd_flags & O_EXCL)
890 flags |= IO_FLAG_EXCLUSIVE;
891#if defined(O_DIRECT)
892 if (fd_flags & O_DIRECT)
893 flags |= IO_FLAG_DIRECT_IO;
894#endif
f47f3195 895#endif /* HAVE_FCNTL */
4ccf9e4f
AS
896
897 return unix_open_channel(str_fd, fd, flags, channel, unixfd_io_manager);
898}
899
900static errcode_t unix_open(const char *name, int flags,
901 io_channel *channel)
902{
903 int fd = -1;
904 int open_flags;
905
906 if (name == 0)
907 return EXT2_ET_BAD_DEVICE_NAME;
908
909 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
910 if (flags & IO_FLAG_EXCLUSIVE)
911 open_flags |= O_EXCL;
912#if defined(O_DIRECT)
913 if (flags & IO_FLAG_DIRECT_IO)
914 open_flags |= O_DIRECT;
915#endif
916 fd = ext2fs_open_file(name, open_flags, 0);
917 if (fd < 0)
918 return errno;
919#if defined(F_NOCACHE) && !defined(IO_DIRECT)
920 if (flags & IO_FLAG_DIRECT_IO) {
921 if (fcntl(fd, F_NOCACHE, 1) < 0)
922 return errno;
923 }
924#endif
925 return unix_open_channel(name, fd, flags, channel, unix_io_manager);
926}
927
3839e657
TT
928static errcode_t unix_close(io_channel channel)
929{
930 struct unix_private_data *data;
931 errcode_t retval = 0;
932
f3db3566 933 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
3839e657 934 data = (struct unix_private_data *) channel->private_data;
f3db3566 935 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
a29f4d30
TT
936
937 if (--channel->refcount > 0)
938 return 0;
adfc8c6c 939
b8a95315 940#ifndef NO_IO_CACHE
adfc8c6c 941 retval = flush_cached_blocks(channel, data, 0);
b8a95315 942#endif
adfc8c6c 943
3839e657
TT
944 if (close(data->dev) < 0)
945 retval = errno;
54434927 946 free_cache(data);
f20627cc
TT
947#ifdef HAVE_PTHREAD
948 if (data->flags & IO_FLAG_THREADS) {
949 pthread_mutex_destroy(&data->cache_mutex);
950 pthread_mutex_destroy(&data->bounce_mutex);
951 pthread_mutex_destroy(&data->stats_mutex);
952 }
953#endif
f12e285f 954
c4e3d3f3 955 ext2fs_free_mem(&channel->private_data);
3839e657 956 if (channel->name)
c4e3d3f3
TT
957 ext2fs_free_mem(&channel->name);
958 ext2fs_free_mem(&channel);
3839e657
TT
959 return retval;
960}
961
962static errcode_t unix_set_blksize(io_channel channel, int blksize)
963{
964 struct unix_private_data *data;
f20627cc 965 errcode_t retval = 0;
3839e657 966
f3db3566 967 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
3839e657 968 data = (struct unix_private_data *) channel->private_data;
f3db3566
TT
969 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
970
3839e657 971 if (channel->block_size != blksize) {
f20627cc
TT
972 mutex_lock(data, CACHE_MTX);
973 mutex_lock(data, BOUNCE_MTX);
b8a95315 974#ifndef NO_IO_CACHE
ece58037 975 if ((retval = flush_cached_blocks(channel, data, FLUSH_NOLOCK))){
976 mutex_unlock(data, BOUNCE_MTX);
977 mutex_unlock(data, CACHE_MTX);
adfc8c6c 978 return retval;
ece58037 979 }
b8a95315 980#endif
efc6f628 981
3839e657 982 channel->block_size = blksize;
54434927 983 free_cache(data);
f20627cc
TT
984 retval = alloc_cache(channel, data);
985 mutex_unlock(data, BOUNCE_MTX);
986 mutex_unlock(data, CACHE_MTX);
3839e657 987 }
f20627cc 988 return retval;
3839e657
TT
989}
990
59ecd32d 991static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
3839e657
TT
992 int count, void *buf)
993{
994 struct unix_private_data *data;
699d448e
TT
995 struct unix_cache *cache;
996 errcode_t retval;
31dbecd4 997 char *cp;
adfc8c6c 998 int i, j;
3839e657 999
f3db3566 1000 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
3839e657 1001 data = (struct unix_private_data *) channel->private_data;
f3db3566 1002 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
3839e657 1003
b8a95315
TT
1004#ifdef NO_IO_CACHE
1005 return raw_read_blk(channel, data, block, count, buf);
1006#else
2caad646
TT
1007 if (data->flags & IO_FLAG_NOCACHE)
1008 return raw_read_blk(channel, data, block, count, buf);
3839e657 1009 /*
82c4660c
TT
1010 * If we're doing an odd-sized read or a very large read,
1011 * flush out the cache and then do a direct read.
3839e657 1012 */
82c4660c 1013 if (count < 0 || count > WRITE_DIRECT_SIZE) {
adfc8c6c
TT
1014 if ((retval = flush_cached_blocks(channel, data, 0)))
1015 return retval;
1016 return raw_read_blk(channel, data, block, count, buf);
3839e657 1017 }
adfc8c6c 1018
31dbecd4 1019 cp = buf;
f20627cc 1020 mutex_lock(data, CACHE_MTX);
adfc8c6c
TT
1021 while (count > 0) {
1022 /* If it's in the cache, use it! */
699d448e 1023 if ((cache = find_cached_block(data, block, NULL))) {
adfc8c6c 1024#ifdef DEBUG
d0ff90d5 1025 printf("Using cached block %lu\n", block);
f3db3566 1026#endif
31dbecd4 1027 memcpy(cp, cache->buf, channel->block_size);
adfc8c6c
TT
1028 count--;
1029 block++;
31dbecd4 1030 cp += channel->block_size;
adfc8c6c
TT
1031 continue;
1032 }
7f1a1fbf 1033
adfc8c6c
TT
1034 /*
1035 * Find the number of uncached blocks so we can do a
1036 * single read request
1037 */
1038 for (i=1; i < count; i++)
699d448e 1039 if (find_cached_block(data, block+i, NULL))
adfc8c6c
TT
1040 break;
1041#ifdef DEBUG
d0ff90d5 1042 printf("Reading %d blocks starting at %lu\n", i, block);
adfc8c6c 1043#endif
699d448e 1044 mutex_unlock(data, CACHE_MTX);
31dbecd4 1045 if ((retval = raw_read_blk(channel, data, block, i, cp)))
699d448e
TT
1046 return retval;
1047 mutex_lock(data, CACHE_MTX);
efc6f628 1048
adfc8c6c
TT
1049 /* Save the results in the cache */
1050 for (j=0; j < i; j++) {
699d448e 1051 if (!find_cached_block(data, block, &cache)) {
f6cb7f01
TT
1052 retval = reuse_cache(channel, data,
1053 cache, block);
1054 if (retval)
1055 goto call_write_handler;
699d448e
TT
1056 memcpy(cache->buf, cp, channel->block_size);
1057 }
adfc8c6c 1058 count--;
699d448e 1059 block++;
31dbecd4 1060 cp += channel->block_size;
adfc8c6c 1061 }
3839e657 1062 }
f20627cc 1063 mutex_unlock(data, CACHE_MTX);
699d448e 1064 return 0;
f6cb7f01
TT
1065
1066call_write_handler:
1067 if (cache->write_err && channel->write_error) {
1068 char *err_buf = NULL;
1069 unsigned long long err_block = cache->block;
1070
1071 cache->dirty = 0;
1072 cache->in_use = 0;
1073 cache->write_err = 0;
1074 if (io_channel_alloc_buf(channel, 0, &err_buf))
1075 err_buf = NULL;
1076 else
1077 memcpy(err_buf, cache->buf, channel->block_size);
1078 mutex_unlock(data, CACHE_MTX);
1079 (channel->write_error)(channel, err_block, 1, err_buf,
1080 channel->block_size, -1,
1081 retval);
1082 if (err_buf)
1083 ext2fs_free_mem(&err_buf);
1084 } else
1085 mutex_unlock(data, CACHE_MTX);
1086 return retval;
b8a95315 1087#endif /* NO_IO_CACHE */
3839e657
TT
1088}
1089
59ecd32d
JS
1090static errcode_t unix_read_blk(io_channel channel, unsigned long block,
1091 int count, void *buf)
1092{
1093 return unix_read_blk64(channel, block, count, buf);
1094}
1095
1096static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
3839e657
TT
1097 int count, const void *buf)
1098{
1099 struct unix_private_data *data;
82c4660c 1100 struct unix_cache *cache, *reuse;
23b7c8b8 1101 errcode_t retval = 0;
31dbecd4
TT
1102 const char *cp;
1103 int writethrough;
3839e657 1104
f3db3566 1105 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
3839e657 1106 data = (struct unix_private_data *) channel->private_data;
f3db3566 1107 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
3839e657 1108
b8a95315 1109#ifdef NO_IO_CACHE
4073feef 1110 return raw_write_blk(channel, data, block, count, buf, 0);
efc6f628 1111#else
2caad646 1112 if (data->flags & IO_FLAG_NOCACHE)
4073feef 1113 return raw_write_blk(channel, data, block, count, buf, 0);
adfc8c6c
TT
1114 /*
1115 * If we're doing an odd-sized write or a very large write,
1116 * flush out the cache completely and then do a direct write.
1117 */
82c4660c 1118 if (count < 0 || count > WRITE_DIRECT_SIZE) {
f20627cc
TT
1119 if ((retval = flush_cached_blocks(channel, data,
1120 FLUSH_INVALIDATE)))
adfc8c6c 1121 return retval;
4073feef 1122 return raw_write_blk(channel, data, block, count, buf, 0);
3839e657
TT
1123 }
1124
adfc8c6c
TT
1125 /*
1126 * For a moderate-sized multi-block write, first force a write
1127 * if we're in write-through cache mode, and then fill the
1128 * cache with the blocks.
1129 */
1130 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
1131 if (writethrough)
4073feef 1132 retval = raw_write_blk(channel, data, block, count, buf, 0);
efc6f628 1133
31dbecd4 1134 cp = buf;
f20627cc 1135 mutex_lock(data, CACHE_MTX);
adfc8c6c 1136 while (count > 0) {
54434927 1137 cache = find_cached_block(data, block, &reuse);
adfc8c6c 1138 if (!cache) {
f6cb7f01
TT
1139 errcode_t err;
1140
82c4660c 1141 cache = reuse;
f6cb7f01
TT
1142 err = reuse_cache(channel, data, cache, block);
1143 if (err)
1144 goto call_write_handler;
adfc8c6c 1145 }
8d5324c4
DW
1146 if (cache->buf != cp)
1147 memcpy(cache->buf, cp, channel->block_size);
82c4660c 1148 cache->dirty = !writethrough;
adfc8c6c
TT
1149 count--;
1150 block++;
31dbecd4 1151 cp += channel->block_size;
adfc8c6c 1152 }
f20627cc 1153 mutex_unlock(data, CACHE_MTX);
3839e657 1154 return retval;
f6cb7f01
TT
1155
1156call_write_handler:
1157 if (cache->write_err && channel->write_error) {
1158 char *err_buf = NULL;
1159 unsigned long long err_block = cache->block;
1160
1161 cache->dirty = 0;
1162 cache->in_use = 0;
1163 cache->write_err = 0;
1164 if (io_channel_alloc_buf(channel, 0, &err_buf))
1165 err_buf = NULL;
1166 else
1167 memcpy(err_buf, cache->buf, channel->block_size);
1168 mutex_unlock(data, CACHE_MTX);
1169 (channel->write_error)(channel, err_block, 1, err_buf,
1170 channel->block_size, -1,
1171 retval);
1172 if (err_buf)
1173 ext2fs_free_mem(&err_buf);
1174 } else
1175 mutex_unlock(data, CACHE_MTX);
1176 return retval;
b8a95315 1177#endif /* NO_IO_CACHE */
3839e657
TT
1178}
1179
ca209dc6
AD
1180static errcode_t unix_cache_readahead(io_channel channel,
1181 unsigned long long block,
1182 unsigned long long count)
1183{
1184#ifdef POSIX_FADV_WILLNEED
1185 struct unix_private_data *data;
1186
1187 data = (struct unix_private_data *)channel->private_data;
a7ac9c22 1188 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
ca209dc6 1189 return posix_fadvise(data->dev,
a7ac9c22 1190 (ext2_loff_t)block * channel->block_size + data->offset,
ca209dc6
AD
1191 (ext2_loff_t)count * channel->block_size,
1192 POSIX_FADV_WILLNEED);
1193#else
1194 return EXT2_ET_OP_NOT_SUPPORTED;
1195#endif
1196}
1197
59ecd32d
JS
1198static errcode_t unix_write_blk(io_channel channel, unsigned long block,
1199 int count, const void *buf)
1200{
1201 return unix_write_blk64(channel, block, count, buf);
1202}
1203
c180ac86
TT
1204static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
1205 int size, const void *buf)
1206{
1207 struct unix_private_data *data;
31dbecd4 1208 errcode_t retval = 0;
54434927 1209 ssize_t actual;
c180ac86
TT
1210
1211 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1212 data = (struct unix_private_data *) channel->private_data;
1213 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1214
0a05b903 1215 if (channel->align != 0) {
7f1a1fbf
TT
1216#ifdef ALIGN_DEBUG
1217 printf("unix_write_byte: O_DIRECT fallback\n");
1218#endif
1219 return EXT2_ET_UNIMPLEMENTED;
1220 }
1221
b8a95315 1222#ifndef NO_IO_CACHE
c180ac86
TT
1223 /*
1224 * Flush out the cache completely
1225 */
f20627cc 1226 if ((retval = flush_cached_blocks(channel, data, FLUSH_INVALIDATE)))
c180ac86 1227 return retval;
b8a95315 1228#endif
c180ac86 1229
2e8ca9a2 1230 if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
c180ac86 1231 return errno;
efc6f628 1232
c180ac86 1233 actual = write(data->dev, buf, size);
025b828c
TT
1234 if (actual < 0)
1235 return errno;
c180ac86
TT
1236 if (actual != size)
1237 return EXT2_ET_SHORT_WRITE;
1238
1239 return 0;
1240}
1241
3839e657 1242/*
efc6f628 1243 * Flush data buffers to disk.
3839e657
TT
1244 */
1245static errcode_t unix_flush(io_channel channel)
1246{
f3db3566 1247 struct unix_private_data *data;
adfc8c6c 1248 errcode_t retval = 0;
efc6f628 1249
f3db3566
TT
1250 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1251 data = (struct unix_private_data *) channel->private_data;
1252 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
adfc8c6c 1253
b8a95315 1254#ifndef NO_IO_CACHE
adfc8c6c 1255 retval = flush_cached_blocks(channel, data, 0);
b8a95315 1256#endif
f47f3195 1257#ifdef HAVE_FSYNC
025d31b1
ES
1258 if (!retval && fsync(data->dev) != 0)
1259 return errno;
f47f3195 1260#endif
adfc8c6c 1261 return retval;
3839e657
TT
1262}
1263
efc6f628 1264static errcode_t unix_set_option(io_channel channel, const char *option,
2e8ca9a2
TT
1265 const char *arg)
1266{
1267 struct unix_private_data *data;
2aee23f3 1268 unsigned long long tmp;
2caad646 1269 errcode_t retval;
2e8ca9a2
TT
1270 char *end;
1271
1272 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1273 data = (struct unix_private_data *) channel->private_data;
1274 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1275
1276 if (!strcmp(option, "offset")) {
1277 if (!arg)
1278 return EXT2_ET_INVALID_ARGUMENT;
1279
2aee23f3 1280 tmp = strtoull(arg, &end, 0);
2e8ca9a2
TT
1281 if (*end)
1282 return EXT2_ET_INVALID_ARGUMENT;
1283 data->offset = tmp;
2aee23f3
TT
1284 if (data->offset < 0)
1285 return EXT2_ET_INVALID_ARGUMENT;
2e8ca9a2
TT
1286 return 0;
1287 }
2caad646
TT
1288 if (!strcmp(option, "cache")) {
1289 if (!arg)
1290 return EXT2_ET_INVALID_ARGUMENT;
1291 if (!strcmp(arg, "on")) {
1292 data->flags &= ~IO_FLAG_NOCACHE;
1293 return 0;
1294 }
1295 if (!strcmp(arg, "off")) {
1296 retval = flush_cached_blocks(channel, data, 0);
1297 data->flags |= IO_FLAG_NOCACHE;
1298 return retval;
1299 }
1300 return EXT2_ET_INVALID_ARGUMENT;
1301 }
2e8ca9a2
TT
1302 return EXT2_ET_INVALID_ARGUMENT;
1303}
e90a59ed
LC
1304
1305#if defined(__linux__) && !defined(BLKDISCARD)
d2bfdc7f 1306#define BLKDISCARD _IO(0x12,119)
e90a59ed
LC
1307#endif
1308
1309static errcode_t unix_discard(io_channel channel, unsigned long long block,
1310 unsigned long long count)
1311{
e90a59ed 1312 struct unix_private_data *data;
e90a59ed
LC
1313 int ret;
1314
1315 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1316 data = (struct unix_private_data *) channel->private_data;
1317 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1318
ab589110
DW
1319 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
1320#ifdef BLKDISCARD
1321 __u64 range[2];
1322
1323 range[0] = (__u64)(block) * channel->block_size + data->offset;
1324 range[1] = (__u64)(count) * channel->block_size;
1325
1326 ret = ioctl(data->dev, BLKDISCARD, &range);
1327#else
1328 goto unimplemented;
1329#endif
1330 } else {
1331#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
1332 /*
1333 * If we are not on block device, try to use punch hole
1334 * to reclaim free space.
1335 */
1336 ret = fallocate(data->dev,
1337 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1338 (off_t)(block) * channel->block_size + data->offset,
1339 (off_t)(count) * channel->block_size);
1340#else
1341 goto unimplemented;
1342#endif
1343 }
d2bfdc7f
LC
1344 if (ret < 0) {
1345 if (errno == EOPNOTSUPP)
1346 goto unimplemented;
e90a59ed 1347 return errno;
d2bfdc7f 1348 }
e90a59ed 1349 return 0;
d2bfdc7f 1350unimplemented:
e90a59ed 1351 return EXT2_ET_UNIMPLEMENTED;
e90a59ed 1352}
a4613d13 1353
3f6a9786
DW
1354/*
1355 * If we know about ZERO_RANGE, try that before we try PUNCH_HOLE because
1356 * ZERO_RANGE doesn't unmap preallocated blocks. We prefer fallocate because
1357 * it always invalidates page cache, and libext2fs requires that reads after
1358 * ZERO_RANGE return zeroes.
1359 */
1360static int __unix_zeroout(int fd, off_t offset, off_t len)
1361{
1362 int ret = -1;
1363
1364#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_ZERO_RANGE)
1365 ret = fallocate(fd, FALLOC_FL_ZERO_RANGE, offset, len);
1366 if (ret == 0)
1367 return 0;
1368#endif
1369#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
1370 ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1371 offset, len);
1372 if (ret == 0)
1373 return 0;
1374#endif
1375 errno = EOPNOTSUPP;
1376 return ret;
1377}
1378
25f291c9 1379/* parameters might not be used if OS doesn't support zeroout */
fddc423d 1380#if __GNUC_PREREQ (4, 6)
25f291c9
TT
1381#pragma GCC diagnostic push
1382#pragma GCC diagnostic ignored "-Wunused-parameter"
fddc423d 1383#endif
3d28f545
DW
1384static errcode_t unix_zeroout(io_channel channel, unsigned long long block,
1385 unsigned long long count)
1386{
1387 struct unix_private_data *data;
1388 int ret;
1389
1390 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
1391 data = (struct unix_private_data *) channel->private_data;
1392 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
1393
224b1567 1394 if (safe_getenv("UNIX_IO_NOZEROOUT"))
3d28f545
DW
1395 goto unimplemented;
1396
3f6a9786 1397 if (!(channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE)) {
3d28f545 1398 /* Regular file, try to use truncate/punch/zero. */
3d28f545
DW
1399 struct stat statbuf;
1400
1401 if (count == 0)
1402 return 0;
1403 /*
1404 * If we're trying to zero a range past the end of the file,
cf491d3a 1405 * extend the file size, then truncate everything.
3d28f545
DW
1406 */
1407 ret = fstat(data->dev, &statbuf);
1408 if (ret)
1409 goto err;
a7ac9c22
MH
1410 if ((unsigned long long) statbuf.st_size <
1411 (block + count) * channel->block_size + data->offset) {
3d28f545 1412 ret = ftruncate(data->dev,
a7ac9c22 1413 (block + count) * channel->block_size + data->offset);
3d28f545
DW
1414 if (ret)
1415 goto err;
1416 }
3d28f545 1417 }
3f6a9786
DW
1418
1419 ret = __unix_zeroout(data->dev,
1420 (off_t)(block) * channel->block_size + data->offset,
1421 (off_t)(count) * channel->block_size);
3d28f545
DW
1422err:
1423 if (ret < 0) {
1424 if (errno == EOPNOTSUPP)
1425 goto unimplemented;
1426 return errno;
1427 }
1428 return 0;
1429unimplemented:
1430 return EXT2_ET_UNIMPLEMENTED;
1431}
fddc423d 1432#if __GNUC_PREREQ (4, 6)
25f291c9 1433#pragma GCC diagnostic pop
fddc423d 1434#endif
3d28f545 1435
a4613d13 1436static struct struct_io_manager struct_unix_manager = {
d4ecec45
TT
1437 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1438 .name = "Unix I/O Manager",
1439 .open = unix_open,
1440 .close = unix_close,
1441 .set_blksize = unix_set_blksize,
1442 .read_blk = unix_read_blk,
1443 .write_blk = unix_write_blk,
1444 .flush = unix_flush,
1445 .write_byte = unix_write_byte,
1446 .set_option = unix_set_option,
1447 .get_stats = unix_get_stats,
1448 .read_blk64 = unix_read_blk64,
1449 .write_blk64 = unix_write_blk64,
1450 .discard = unix_discard,
ca209dc6 1451 .cache_readahead = unix_cache_readahead,
3d28f545 1452 .zeroout = unix_zeroout,
a4613d13
AD
1453};
1454
1455io_manager unix_io_manager = &struct_unix_manager;
4ccf9e4f
AS
1456
1457static struct struct_io_manager struct_unixfd_manager = {
1458 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1459 .name = "Unix fd I/O Manager",
1460 .open = unixfd_open,
1461 .close = unix_close,
1462 .set_blksize = unix_set_blksize,
1463 .read_blk = unix_read_blk,
1464 .write_blk = unix_write_blk,
1465 .flush = unix_flush,
1466 .write_byte = unix_write_byte,
1467 .set_option = unix_set_option,
1468 .get_stats = unix_get_stats,
1469 .read_blk64 = unix_read_blk64,
1470 .write_blk64 = unix_write_blk64,
1471 .discard = unix_discard,
1472 .cache_readahead = unix_cache_readahead,
1473 .zeroout = unix_zeroout,
1474};
1475
1476io_manager unixfd_io_manager = &struct_unixfd_manager;