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