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