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