Whamcloud - gitweb
2302374c9d0ea3e70d4e080e9ee0496b255d00f2
[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 #define _LARGEFILE_SOURCE
19 #define _LARGEFILE64_SOURCE
20 #define _GNU_SOURCE
21
22 #include <stdio.h>
23 #include <string.h>
24 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #if HAVE_ERRNO_H
28 #include <errno.h>
29 #endif
30 #include <fcntl.h>
31 #include <time.h>
32 #ifdef __linux__
33 #include <sys/utsname.h>
34 #endif
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
37 #endif
38 #ifdef HAVE_SYS_MOUNT_H
39 #include <sys/mount.h>
40 #endif
41 #if HAVE_SYS_STAT_H
42 #include <sys/stat.h>
43 #endif
44 #if HAVE_SYS_TYPES_H
45 #include <sys/types.h>
46 #endif
47 #if HAVE_SYS_RESOURCE_H
48 #include <sys/resource.h>
49 #endif
50
51 #if defined(__linux__) && defined(_IO) && !defined(BLKROGET)
52 #define BLKROGET   _IO(0x12, 94) /* Get read-only status (0 = read_write).  */
53 #endif
54
55 #if defined(__linux__) && defined(_IO) && !defined(BLKSSZGET)
56 #define BLKSSZGET  _IO(0x12,104)/* get block device sector size */
57 #endif
58
59 #undef ALIGN_DEBUG
60
61 #include "ext2_fs.h"
62 #include "ext2fs.h"
63
64 /*
65  * For checking structure magic numbers...
66  */
67
68 #define EXT2_CHECK_MAGIC(struct, code) \
69           if ((struct)->magic != (code)) return (code)
70
71 struct unix_cache {
72         char            *buf;
73         unsigned long   block;
74         int             access_time;
75         unsigned        dirty:1;
76         unsigned        in_use:1;
77 };
78
79 #define CACHE_SIZE 8
80 #define WRITE_DIRECT_SIZE 4     /* Must be smaller than CACHE_SIZE */
81 #define READ_DIRECT_SIZE 4      /* Should be smaller than CACHE_SIZE */
82
83 struct unix_private_data {
84         int     magic;
85         int     dev;
86         int     flags;
87         int     align;
88         int     access_time;
89         ext2_loff_t offset;
90         struct unix_cache cache[CACHE_SIZE];
91         void    *bounce;
92         struct struct_io_stats io_stats;
93 };
94
95 #define IS_ALIGNED(n, align) ((((unsigned long) n) & \
96                                ((unsigned long) ((align)-1))) == 0)
97
98 static errcode_t unix_open(const char *name, int flags, io_channel *channel);
99 static errcode_t unix_close(io_channel channel);
100 static errcode_t unix_set_blksize(io_channel channel, int blksize);
101 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
102                                int count, void *data);
103 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
104                                 int count, const void *data);
105 static errcode_t unix_flush(io_channel channel);
106 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
107                                 int size, const void *data);
108 static errcode_t unix_set_option(io_channel channel, const char *option,
109                                  const char *arg);
110 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
111 ;
112 static void reuse_cache(io_channel channel, struct unix_private_data *data,
113                  struct unix_cache *cache, unsigned long long block);
114 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
115                                int count, void *data);
116 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
117                                 int count, const void *data);
118 static errcode_t unix_discard(io_channel channel, unsigned long long block,
119                               unsigned long long count);
120
121 static struct struct_io_manager struct_unix_manager = {
122         EXT2_ET_MAGIC_IO_MANAGER,
123         "Unix I/O Manager",
124         unix_open,
125         unix_close,
126         unix_set_blksize,
127         unix_read_blk,
128         unix_write_blk,
129         unix_flush,
130         unix_write_byte,
131         unix_set_option,
132         unix_get_stats,
133         unix_read_blk64,
134         unix_write_blk64,
135         unix_discard,
136 };
137
138 io_manager unix_io_manager = &struct_unix_manager;
139
140 static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
141 {
142         errcode_t       retval = 0;
143
144         struct unix_private_data *data;
145
146         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
147         data = (struct unix_private_data *) channel->private_data;
148         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
149
150         if (stats)
151                 *stats = &data->io_stats;
152
153         return retval;
154 }
155
156 /*
157  * Here are the raw I/O functions
158  */
159 static errcode_t raw_read_blk(io_channel channel,
160                               struct unix_private_data *data,
161                               unsigned long long block,
162                               int count, void *buf)
163 {
164         errcode_t       retval;
165         ssize_t         size;
166         ext2_loff_t     location;
167         int             actual = 0;
168
169         size = (count < 0) ? -count : count * channel->block_size;
170         data->io_stats.bytes_read += size;
171         location = ((ext2_loff_t) block * channel->block_size) + data->offset;
172         if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
173                 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
174                 goto error_out;
175         }
176         if ((data->align == 0) ||
177             ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
178                 actual = read(data->dev, buf, size);
179                 if (actual != size) {
180                 short_read:
181                         if (actual < 0)
182                                 actual = 0;
183                         retval = EXT2_ET_SHORT_READ;
184                         goto error_out;
185                 }
186                 return 0;
187         }
188
189 #ifdef ALIGN_DEBUG
190         printf("raw_read_blk: O_DIRECT fallback: %p %lu\n", buf,
191                (unsigned long) size);
192 #endif
193
194         /*
195          * The buffer or size which we're trying to read isn't aligned
196          * to the O_DIRECT rules, so we need to do this the hard way...
197          */
198         while (size > 0) {
199                 actual = read(data->dev, data->bounce, channel->block_size);
200                 if (actual != channel->block_size)
201                         goto short_read;
202                 actual = size;
203                 if (size > channel->block_size)
204                         actual = channel->block_size;
205                 memcpy(buf, data->bounce, actual);
206                 size -= actual;
207                 buf += actual;
208         }
209         return 0;
210
211 error_out:
212         memset((char *) buf+actual, 0, size-actual);
213         if (channel->read_error)
214                 retval = (channel->read_error)(channel, block, count, buf,
215                                                size, actual, retval);
216         return retval;
217 }
218
219 static errcode_t raw_write_blk(io_channel channel,
220                                struct unix_private_data *data,
221                                unsigned long long block,
222                                int count, const void *buf)
223 {
224         ssize_t         size;
225         ext2_loff_t     location;
226         int             actual = 0;
227         errcode_t       retval;
228
229         if (count == 1)
230                 size = channel->block_size;
231         else {
232                 if (count < 0)
233                         size = -count;
234                 else
235                         size = count * channel->block_size;
236         }
237         data->io_stats.bytes_written += size;
238
239         location = ((ext2_loff_t) block * channel->block_size) + data->offset;
240         if (ext2fs_llseek(data->dev, location, SEEK_SET) != location) {
241                 retval = errno ? errno : EXT2_ET_LLSEEK_FAILED;
242                 goto error_out;
243         }
244
245         if ((data->align == 0) ||
246             ((IS_ALIGNED(buf, data->align)) && IS_ALIGNED(size, data->align))) {
247                 actual = write(data->dev, buf, size);
248                 if (actual != size) {
249                 short_write:
250                         retval = EXT2_ET_SHORT_WRITE;
251                         goto error_out;
252                 }
253                 return 0;
254         }
255
256 #ifdef ALIGN_DEBUG
257         printf("raw_write_blk: O_DIRECT fallback: %p %lu\n", buf,
258                (unsigned long) size);
259 #endif
260         /*
261          * The buffer or size which we're trying to write isn't aligned
262          * to the O_DIRECT rules, so we need to do this the hard way...
263          */
264         while (size > 0) {
265                 if (size < channel->block_size) {
266                         actual = read(data->dev, data->bounce,
267                                       channel->block_size);
268                         if (actual != channel->block_size) {
269                                 retval = EXT2_ET_SHORT_READ;
270                                 goto error_out;
271                         }
272                 }
273                 actual = size;
274                 if (size > channel->block_size)
275                         actual = channel->block_size;
276                 memcpy(data->bounce, buf, actual);
277                 actual = write(data->dev, data->bounce, channel->block_size);
278                 if (actual != channel->block_size)
279                         goto short_write;
280                 size -= actual;
281                 buf += actual;
282         }
283         return 0;
284
285 error_out:
286         if (channel->write_error)
287                 retval = (channel->write_error)(channel, block, count, buf,
288                                                 size, actual, retval);
289         return retval;
290 }
291
292
293 /*
294  * Here we implement the cache functions
295  */
296
297 /* Allocate the cache buffers */
298 static errcode_t alloc_cache(io_channel channel,
299                              struct unix_private_data *data)
300 {
301         errcode_t               retval;
302         struct unix_cache       *cache;
303         int                     i;
304
305         data->access_time = 0;
306         for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
307                 cache->block = 0;
308                 cache->access_time = 0;
309                 cache->dirty = 0;
310                 cache->in_use = 0;
311                 if (cache->buf)
312                         ext2fs_free_mem(&cache->buf);
313                 retval = ext2fs_get_memalign(channel->block_size,
314                                              data->align, &cache->buf);
315                 if (retval)
316                         return retval;
317         }
318         if (data->align) {
319                 if (data->bounce)
320                         ext2fs_free_mem(&data->bounce);
321                 retval = ext2fs_get_memalign(channel->block_size, data->align,
322                                              &data->bounce);
323         }
324         return retval;
325 }
326
327 /* Free the cache buffers */
328 static void free_cache(struct unix_private_data *data)
329 {
330         struct unix_cache       *cache;
331         int                     i;
332
333         data->access_time = 0;
334         for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
335                 cache->block = 0;
336                 cache->access_time = 0;
337                 cache->dirty = 0;
338                 cache->in_use = 0;
339                 if (cache->buf)
340                         ext2fs_free_mem(&cache->buf);
341         }
342         if (data->bounce)
343                 ext2fs_free_mem(&data->bounce);
344 }
345
346 #ifndef NO_IO_CACHE
347 /*
348  * Try to find a block in the cache.  If the block is not found, and
349  * eldest is a non-zero pointer, then fill in eldest with the cache
350  * entry to that should be reused.
351  */
352 static struct unix_cache *find_cached_block(struct unix_private_data *data,
353                                             unsigned long long block,
354                                             struct unix_cache **eldest)
355 {
356         struct unix_cache       *cache, *unused_cache, *oldest_cache;
357         int                     i;
358
359         unused_cache = oldest_cache = 0;
360         for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
361                 if (!cache->in_use) {
362                         if (!unused_cache)
363                                 unused_cache = cache;
364                         continue;
365                 }
366                 if (cache->block == block) {
367                         cache->access_time = ++data->access_time;
368                         return cache;
369                 }
370                 if (!oldest_cache ||
371                     (cache->access_time < oldest_cache->access_time))
372                         oldest_cache = cache;
373         }
374         if (eldest)
375                 *eldest = (unused_cache) ? unused_cache : oldest_cache;
376         return 0;
377 }
378
379 /*
380  * Reuse a particular cache entry for another block.
381  */
382 static void reuse_cache(io_channel channel, struct unix_private_data *data,
383                  struct unix_cache *cache, unsigned long long block)
384 {
385         if (cache->dirty && cache->in_use)
386                 raw_write_blk(channel, data, cache->block, 1, cache->buf);
387
388         cache->in_use = 1;
389         cache->dirty = 0;
390         cache->block = block;
391         cache->access_time = ++data->access_time;
392 }
393
394 /*
395  * Flush all of the blocks in the cache
396  */
397 static errcode_t flush_cached_blocks(io_channel channel,
398                                      struct unix_private_data *data,
399                                      int invalidate)
400
401 {
402         struct unix_cache       *cache;
403         errcode_t               retval, retval2;
404         int                     i;
405
406         retval2 = 0;
407         for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
408                 if (!cache->in_use)
409                         continue;
410
411                 if (invalidate)
412                         cache->in_use = 0;
413
414                 if (!cache->dirty)
415                         continue;
416
417                 retval = raw_write_blk(channel, data,
418                                        cache->block, 1, cache->buf);
419                 if (retval)
420                         retval2 = retval;
421                 else
422                         cache->dirty = 0;
423         }
424         return retval2;
425 }
426 #endif /* NO_IO_CACHE */
427
428 static errcode_t unix_open(const char *name, int flags, io_channel *channel)
429 {
430         io_channel      io = NULL;
431         struct unix_private_data *data = NULL;
432         errcode_t       retval;
433         int             open_flags;
434         struct stat     st;
435 #ifdef __linux__
436         struct          utsname ut;
437 #endif
438
439         if (name == 0)
440                 return EXT2_ET_BAD_DEVICE_NAME;
441         retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
442         if (retval)
443                 return retval;
444         memset(io, 0, sizeof(struct struct_io_channel));
445         io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
446         retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
447         if (retval)
448                 goto cleanup;
449
450         io->manager = unix_io_manager;
451         retval = ext2fs_get_mem(strlen(name)+1, &io->name);
452         if (retval)
453                 goto cleanup;
454
455         strcpy(io->name, name);
456         io->private_data = data;
457         io->block_size = 1024;
458         io->read_error = 0;
459         io->write_error = 0;
460         io->refcount = 1;
461
462         memset(data, 0, sizeof(struct unix_private_data));
463         data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
464         data->io_stats.num_fields = 2;
465
466         open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
467         if (flags & IO_FLAG_EXCLUSIVE)
468                 open_flags |= O_EXCL;
469         if (flags & IO_FLAG_DIRECT_IO)
470                 open_flags |= O_DIRECT;
471         data->flags = flags;
472
473 #ifdef HAVE_OPEN64
474         data->dev = open64(io->name, open_flags);
475 #else
476         data->dev = open(io->name, open_flags);
477 #endif
478         if (data->dev < 0) {
479                 retval = errno;
480                 goto cleanup;
481         }
482
483 #ifdef BLKSSZGET
484         if (flags & IO_FLAG_DIRECT_IO) {
485                 if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
486                         data->align = io->block_size;
487         }
488 #endif
489
490 #if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
491         /*
492          * Some operating systems require that the buffers be aligned,
493          * regardless of O_DIRECT
494          */
495         data->align = 512;
496 #endif
497
498
499         if ((retval = alloc_cache(io, data)))
500                 goto cleanup;
501
502 #ifdef BLKROGET
503         if (flags & IO_FLAG_RW) {
504                 int error;
505                 int readonly = 0;
506
507                 /* Is the block device actually writable? */
508                 error = ioctl(data->dev, BLKROGET, &readonly);
509                 if (!error && readonly) {
510                         close(data->dev);
511                         retval = EPERM;
512                         goto cleanup;
513                 }
514         }
515 #endif
516
517 #ifdef __linux__
518 #undef RLIM_INFINITY
519 #if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
520 #define RLIM_INFINITY   ((unsigned long)(~0UL>>1))
521 #else
522 #define RLIM_INFINITY  (~0UL)
523 #endif
524         /*
525          * Work around a bug in 2.4.10-2.4.18 kernels where writes to
526          * block devices are wrongly getting hit by the filesize
527          * limit.  This workaround isn't perfect, since it won't work
528          * if glibc wasn't built against 2.2 header files.  (Sigh.)
529          *
530          */
531         if ((flags & IO_FLAG_RW) &&
532             (uname(&ut) == 0) &&
533             ((ut.release[0] == '2') && (ut.release[1] == '.') &&
534              (ut.release[2] == '4') && (ut.release[3] == '.') &&
535              (ut.release[4] == '1') && (ut.release[5] >= '0') &&
536              (ut.release[5] < '8')) &&
537             (fstat(data->dev, &st) == 0) &&
538             (S_ISBLK(st.st_mode))) {
539                 struct rlimit   rlim;
540
541                 rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
542                 setrlimit(RLIMIT_FSIZE, &rlim);
543                 getrlimit(RLIMIT_FSIZE, &rlim);
544                 if (((unsigned long) rlim.rlim_cur) <
545                     ((unsigned long) rlim.rlim_max)) {
546                         rlim.rlim_cur = rlim.rlim_max;
547                         setrlimit(RLIMIT_FSIZE, &rlim);
548                 }
549         }
550 #endif
551         *channel = io;
552         return 0;
553
554 cleanup:
555         if (data) {
556                 free_cache(data);
557                 ext2fs_free_mem(&data);
558         }
559         if (io)
560                 ext2fs_free_mem(&io);
561         return retval;
562 }
563
564 static errcode_t unix_close(io_channel channel)
565 {
566         struct unix_private_data *data;
567         errcode_t       retval = 0;
568
569         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
570         data = (struct unix_private_data *) channel->private_data;
571         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
572
573         if (--channel->refcount > 0)
574                 return 0;
575
576 #ifndef NO_IO_CACHE
577         retval = flush_cached_blocks(channel, data, 0);
578 #endif
579
580         if (close(data->dev) < 0)
581                 retval = errno;
582         free_cache(data);
583
584         ext2fs_free_mem(&channel->private_data);
585         if (channel->name)
586                 ext2fs_free_mem(&channel->name);
587         ext2fs_free_mem(&channel);
588         return retval;
589 }
590
591 static errcode_t unix_set_blksize(io_channel channel, int blksize)
592 {
593         struct unix_private_data *data;
594         errcode_t               retval;
595
596         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
597         data = (struct unix_private_data *) channel->private_data;
598         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
599
600         if (channel->block_size != blksize) {
601 #ifndef NO_IO_CACHE
602                 if ((retval = flush_cached_blocks(channel, data, 0)))
603                         return retval;
604 #endif
605
606                 channel->block_size = blksize;
607                 free_cache(data);
608                 if ((retval = alloc_cache(channel, data)))
609                         return retval;
610         }
611         return 0;
612 }
613
614
615 static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
616                                int count, void *buf)
617 {
618         struct unix_private_data *data;
619         struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
620         errcode_t       retval;
621         char            *cp;
622         int             i, j;
623
624         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
625         data = (struct unix_private_data *) channel->private_data;
626         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
627
628 #ifdef NO_IO_CACHE
629         return raw_read_blk(channel, data, block, count, buf);
630 #else
631         /*
632          * If we're doing an odd-sized read or a very large read,
633          * flush out the cache and then do a direct read.
634          */
635         if (count < 0 || count > WRITE_DIRECT_SIZE) {
636                 if ((retval = flush_cached_blocks(channel, data, 0)))
637                         return retval;
638                 return raw_read_blk(channel, data, block, count, buf);
639         }
640
641         cp = buf;
642         while (count > 0) {
643                 /* If it's in the cache, use it! */
644                 if ((cache = find_cached_block(data, block, &reuse[0]))) {
645 #ifdef DEBUG
646                         printf("Using cached block %lu\n", block);
647 #endif
648                         memcpy(cp, cache->buf, channel->block_size);
649                         count--;
650                         block++;
651                         cp += channel->block_size;
652                         continue;
653                 }
654                 if (count == 1) {
655                         /*
656                          * Special case where we read directly into the
657                          * cache buffer; important in the O_DIRECT case
658                          */
659                         cache = reuse[0];
660                         reuse_cache(channel, data, cache, block);
661                         if ((retval = raw_read_blk(channel, data, block, 1,
662                                                    cache->buf))) {
663                                 cache->in_use = 0;
664                                 return retval;
665                         }
666                         memcpy(cp, cache->buf, channel->block_size);
667                         return 0;
668                 }
669
670                 /*
671                  * Find the number of uncached blocks so we can do a
672                  * single read request
673                  */
674                 for (i=1; i < count; i++)
675                         if (find_cached_block(data, block+i, &reuse[i]))
676                                 break;
677 #ifdef DEBUG
678                 printf("Reading %d blocks starting at %lu\n", i, block);
679 #endif
680                 if ((retval = raw_read_blk(channel, data, block, i, cp)))
681                         return retval;
682
683                 /* Save the results in the cache */
684                 for (j=0; j < i; j++) {
685                         count--;
686                         cache = reuse[j];
687                         reuse_cache(channel, data, cache, block++);
688                         memcpy(cache->buf, cp, channel->block_size);
689                         cp += channel->block_size;
690                 }
691         }
692         return 0;
693 #endif /* NO_IO_CACHE */
694 }
695
696 static errcode_t unix_read_blk(io_channel channel, unsigned long block,
697                                int count, void *buf)
698 {
699         return unix_read_blk64(channel, block, count, buf);
700 }
701
702 static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
703                                 int count, const void *buf)
704 {
705         struct unix_private_data *data;
706         struct unix_cache *cache, *reuse;
707         errcode_t       retval = 0;
708         const char      *cp;
709         int             writethrough;
710
711         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
712         data = (struct unix_private_data *) channel->private_data;
713         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
714
715 #ifdef NO_IO_CACHE
716         return raw_write_blk(channel, data, block, count, buf);
717 #else
718         /*
719          * If we're doing an odd-sized write or a very large write,
720          * flush out the cache completely and then do a direct write.
721          */
722         if (count < 0 || count > WRITE_DIRECT_SIZE) {
723                 if ((retval = flush_cached_blocks(channel, data, 1)))
724                         return retval;
725                 return raw_write_blk(channel, data, block, count, buf);
726         }
727
728         /*
729          * For a moderate-sized multi-block write, first force a write
730          * if we're in write-through cache mode, and then fill the
731          * cache with the blocks.
732          */
733         writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
734         if (writethrough)
735                 retval = raw_write_blk(channel, data, block, count, buf);
736
737         cp = buf;
738         while (count > 0) {
739                 cache = find_cached_block(data, block, &reuse);
740                 if (!cache) {
741                         cache = reuse;
742                         reuse_cache(channel, data, cache, block);
743                 }
744                 memcpy(cache->buf, cp, channel->block_size);
745                 cache->dirty = !writethrough;
746                 count--;
747                 block++;
748                 cp += channel->block_size;
749         }
750         return retval;
751 #endif /* NO_IO_CACHE */
752 }
753
754 static errcode_t unix_write_blk(io_channel channel, unsigned long block,
755                                 int count, const void *buf)
756 {
757         return unix_write_blk64(channel, block, count, buf);
758 }
759
760 static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
761                                  int size, const void *buf)
762 {
763         struct unix_private_data *data;
764         errcode_t       retval = 0;
765         ssize_t         actual;
766
767         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
768         data = (struct unix_private_data *) channel->private_data;
769         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
770
771         if (data->align != 0) {
772 #ifdef ALIGN_DEBUG
773                 printf("unix_write_byte: O_DIRECT fallback\n");
774 #endif
775                 return EXT2_ET_UNIMPLEMENTED;
776         }
777
778 #ifndef NO_IO_CACHE
779         /*
780          * Flush out the cache completely
781          */
782         if ((retval = flush_cached_blocks(channel, data, 1)))
783                 return retval;
784 #endif
785
786         if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
787                 return errno;
788
789         actual = write(data->dev, buf, size);
790         if (actual != size)
791                 return EXT2_ET_SHORT_WRITE;
792
793         return 0;
794 }
795
796 /*
797  * Flush data buffers to disk.
798  */
799 static errcode_t unix_flush(io_channel channel)
800 {
801         struct unix_private_data *data;
802         errcode_t retval = 0;
803
804         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
805         data = (struct unix_private_data *) channel->private_data;
806         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
807
808 #ifndef NO_IO_CACHE
809         retval = flush_cached_blocks(channel, data, 0);
810 #endif
811         fsync(data->dev);
812         return retval;
813 }
814
815 static errcode_t unix_set_option(io_channel channel, const char *option,
816                                  const char *arg)
817 {
818         struct unix_private_data *data;
819         unsigned long long tmp;
820         char *end;
821
822         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
823         data = (struct unix_private_data *) channel->private_data;
824         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
825
826         if (!strcmp(option, "offset")) {
827                 if (!arg)
828                         return EXT2_ET_INVALID_ARGUMENT;
829
830                 tmp = strtoull(arg, &end, 0);
831                 if (*end)
832                         return EXT2_ET_INVALID_ARGUMENT;
833                 data->offset = tmp;
834                 if (data->offset < 0)
835                         return EXT2_ET_INVALID_ARGUMENT;
836                 return 0;
837         }
838         return EXT2_ET_INVALID_ARGUMENT;
839 }
840
841 #if defined(__linux__) && !defined(BLKDISCARD)
842 #define BLKDISCARD      _IO(0x12,119)
843 #endif
844
845 static errcode_t unix_discard(io_channel channel, unsigned long long block,
846                               unsigned long long count)
847 {
848 #ifdef BLKDISCARD
849         struct unix_private_data *data;
850         __uint64_t      range[2];
851         int             ret;
852
853         EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
854         data = (struct unix_private_data *) channel->private_data;
855         EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
856
857         range[0] = (__uint64_t)(block) * channel->block_size;
858         range[1] = (__uint64_t)(count) * channel->block_size;
859
860         ret = ioctl(data->dev, BLKDISCARD, &range);
861         if (ret < 0)
862                 return errno;
863         return 0;
864 #else
865         return EXT2_ET_UNIMPLEMENTED;
866 #endif
867 }