unix_io.c revision d866599ab4955858b1541f0891b1b165ba66493a
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
71struct 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
83struct 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
98static errcode_t unix_open(const char *name, int flags, io_channel *channel);
99static errcode_t unix_close(io_channel channel);
100static errcode_t unix_set_blksize(io_channel channel, int blksize);
101static errcode_t unix_read_blk(io_channel channel, unsigned long block,
102			       int count, void *data);
103static errcode_t unix_write_blk(io_channel channel, unsigned long block,
104				int count, const void *data);
105static errcode_t unix_flush(io_channel channel);
106static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
107				int size, const void *data);
108static errcode_t unix_set_option(io_channel channel, const char *option,
109				 const char *arg);
110static errcode_t unix_get_stats(io_channel channel, io_stats *stats)
111;
112static void reuse_cache(io_channel channel, struct unix_private_data *data,
113		 struct unix_cache *cache, unsigned long long block);
114static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
115			       int count, void *data);
116static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
117				int count, const void *data);
118static errcode_t unix_discard(io_channel channel, unsigned long long block,
119			      unsigned long long count);
120
121static 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
138io_manager unix_io_manager = &struct_unix_manager;
139
140static 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 */
159static 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
211error_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
219static 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
285error_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 */
298static 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 */
328static 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 */
352static 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 */
382static 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 */
397static 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#ifdef __linux__
429#ifndef BLKDISCARDZEROES
430#define BLKDISCARDZEROES _IO(0x12,124)
431#endif
432#endif
433
434static errcode_t unix_open(const char *name, int flags, io_channel *channel)
435{
436	io_channel	io = NULL;
437	struct unix_private_data *data = NULL;
438	errcode_t	retval;
439	int		open_flags, zeroes = 0;
440	struct stat	st;
441#ifdef __linux__
442	struct 		utsname ut;
443#endif
444
445	if (name == 0)
446		return EXT2_ET_BAD_DEVICE_NAME;
447	retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
448	if (retval)
449		return retval;
450	memset(io, 0, sizeof(struct struct_io_channel));
451	io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
452	retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
453	if (retval)
454		goto cleanup;
455
456	io->manager = unix_io_manager;
457	retval = ext2fs_get_mem(strlen(name)+1, &io->name);
458	if (retval)
459		goto cleanup;
460
461	strcpy(io->name, name);
462	io->private_data = data;
463	io->block_size = 1024;
464	io->read_error = 0;
465	io->write_error = 0;
466	io->refcount = 1;
467
468	memset(data, 0, sizeof(struct unix_private_data));
469	data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
470	data->io_stats.num_fields = 2;
471
472	open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
473	if (flags & IO_FLAG_EXCLUSIVE)
474		open_flags |= O_EXCL;
475	if (flags & IO_FLAG_DIRECT_IO)
476		open_flags |= O_DIRECT;
477	data->flags = flags;
478
479#ifdef HAVE_OPEN64
480	data->dev = open64(io->name, open_flags);
481#else
482	data->dev = open(io->name, open_flags);
483#endif
484	if (data->dev < 0) {
485		retval = errno;
486		goto cleanup;
487	}
488
489#ifdef BLKSSZGET
490	if (flags & IO_FLAG_DIRECT_IO) {
491		if (ioctl(data->dev, BLKSSZGET, &data->align) != 0)
492			data->align = io->block_size;
493	}
494#endif
495
496#ifdef BLKDISCARDZEROES
497	ioctl(data->dev, BLKDISCARDZEROES, &zeroes);
498	if (zeroes)
499		io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
500#endif
501
502#if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
503	/*
504	 * Some operating systems require that the buffers be aligned,
505	 * regardless of O_DIRECT
506	 */
507	data->align = 512;
508#endif
509
510
511	if ((retval = alloc_cache(io, data)))
512		goto cleanup;
513
514#ifdef BLKROGET
515	if (flags & IO_FLAG_RW) {
516		int error;
517		int readonly = 0;
518
519		/* Is the block device actually writable? */
520		error = ioctl(data->dev, BLKROGET, &readonly);
521		if (!error && readonly) {
522			close(data->dev);
523			retval = EPERM;
524			goto cleanup;
525		}
526	}
527#endif
528
529#ifdef __linux__
530#undef RLIM_INFINITY
531#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
532#define RLIM_INFINITY	((unsigned long)(~0UL>>1))
533#else
534#define RLIM_INFINITY  (~0UL)
535#endif
536	/*
537	 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
538	 * block devices are wrongly getting hit by the filesize
539	 * limit.  This workaround isn't perfect, since it won't work
540	 * if glibc wasn't built against 2.2 header files.  (Sigh.)
541	 *
542	 */
543	if ((flags & IO_FLAG_RW) &&
544	    (uname(&ut) == 0) &&
545	    ((ut.release[0] == '2') && (ut.release[1] == '.') &&
546	     (ut.release[2] == '4') && (ut.release[3] == '.') &&
547	     (ut.release[4] == '1') && (ut.release[5] >= '0') &&
548	     (ut.release[5] < '8')) &&
549	    (fstat(data->dev, &st) == 0) &&
550	    (S_ISBLK(st.st_mode))) {
551		struct rlimit	rlim;
552
553		rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
554		setrlimit(RLIMIT_FSIZE, &rlim);
555		getrlimit(RLIMIT_FSIZE, &rlim);
556		if (((unsigned long) rlim.rlim_cur) <
557		    ((unsigned long) rlim.rlim_max)) {
558			rlim.rlim_cur = rlim.rlim_max;
559			setrlimit(RLIMIT_FSIZE, &rlim);
560		}
561	}
562#endif
563	*channel = io;
564	return 0;
565
566cleanup:
567	if (data) {
568		free_cache(data);
569		ext2fs_free_mem(&data);
570	}
571	if (io)
572		ext2fs_free_mem(&io);
573	return retval;
574}
575
576static errcode_t unix_close(io_channel channel)
577{
578	struct unix_private_data *data;
579	errcode_t	retval = 0;
580
581	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
582	data = (struct unix_private_data *) channel->private_data;
583	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
584
585	if (--channel->refcount > 0)
586		return 0;
587
588#ifndef NO_IO_CACHE
589	retval = flush_cached_blocks(channel, data, 0);
590#endif
591
592	if (close(data->dev) < 0)
593		retval = errno;
594	free_cache(data);
595
596	ext2fs_free_mem(&channel->private_data);
597	if (channel->name)
598		ext2fs_free_mem(&channel->name);
599	ext2fs_free_mem(&channel);
600	return retval;
601}
602
603static errcode_t unix_set_blksize(io_channel channel, int blksize)
604{
605	struct unix_private_data *data;
606	errcode_t		retval;
607
608	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
609	data = (struct unix_private_data *) channel->private_data;
610	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
611
612	if (channel->block_size != blksize) {
613#ifndef NO_IO_CACHE
614		if ((retval = flush_cached_blocks(channel, data, 0)))
615			return retval;
616#endif
617
618		channel->block_size = blksize;
619		free_cache(data);
620		if ((retval = alloc_cache(channel, data)))
621			return retval;
622	}
623	return 0;
624}
625
626
627static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
628			       int count, void *buf)
629{
630	struct unix_private_data *data;
631	struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
632	errcode_t	retval;
633	char		*cp;
634	int		i, j;
635
636	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
637	data = (struct unix_private_data *) channel->private_data;
638	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
639
640#ifdef NO_IO_CACHE
641	return raw_read_blk(channel, data, block, count, buf);
642#else
643	/*
644	 * If we're doing an odd-sized read or a very large read,
645	 * flush out the cache and then do a direct read.
646	 */
647	if (count < 0 || count > WRITE_DIRECT_SIZE) {
648		if ((retval = flush_cached_blocks(channel, data, 0)))
649			return retval;
650		return raw_read_blk(channel, data, block, count, buf);
651	}
652
653	cp = buf;
654	while (count > 0) {
655		/* If it's in the cache, use it! */
656		if ((cache = find_cached_block(data, block, &reuse[0]))) {
657#ifdef DEBUG
658			printf("Using cached block %lu\n", block);
659#endif
660			memcpy(cp, cache->buf, channel->block_size);
661			count--;
662			block++;
663			cp += channel->block_size;
664			continue;
665		}
666		if (count == 1) {
667			/*
668			 * Special case where we read directly into the
669			 * cache buffer; important in the O_DIRECT case
670			 */
671			cache = reuse[0];
672			reuse_cache(channel, data, cache, block);
673			if ((retval = raw_read_blk(channel, data, block, 1,
674						   cache->buf))) {
675				cache->in_use = 0;
676				return retval;
677			}
678			memcpy(cp, cache->buf, channel->block_size);
679			return 0;
680		}
681
682		/*
683		 * Find the number of uncached blocks so we can do a
684		 * single read request
685		 */
686		for (i=1; i < count; i++)
687			if (find_cached_block(data, block+i, &reuse[i]))
688				break;
689#ifdef DEBUG
690		printf("Reading %d blocks starting at %lu\n", i, block);
691#endif
692		if ((retval = raw_read_blk(channel, data, block, i, cp)))
693			return retval;
694
695		/* Save the results in the cache */
696		for (j=0; j < i; j++) {
697			count--;
698			cache = reuse[j];
699			reuse_cache(channel, data, cache, block++);
700			memcpy(cache->buf, cp, channel->block_size);
701			cp += channel->block_size;
702		}
703	}
704	return 0;
705#endif /* NO_IO_CACHE */
706}
707
708static errcode_t unix_read_blk(io_channel channel, unsigned long block,
709			       int count, void *buf)
710{
711	return unix_read_blk64(channel, block, count, buf);
712}
713
714static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
715				int count, const void *buf)
716{
717	struct unix_private_data *data;
718	struct unix_cache *cache, *reuse;
719	errcode_t	retval = 0;
720	const char	*cp;
721	int		writethrough;
722
723	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
724	data = (struct unix_private_data *) channel->private_data;
725	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
726
727#ifdef NO_IO_CACHE
728	return raw_write_blk(channel, data, block, count, buf);
729#else
730	/*
731	 * If we're doing an odd-sized write or a very large write,
732	 * flush out the cache completely and then do a direct write.
733	 */
734	if (count < 0 || count > WRITE_DIRECT_SIZE) {
735		if ((retval = flush_cached_blocks(channel, data, 1)))
736			return retval;
737		return raw_write_blk(channel, data, block, count, buf);
738	}
739
740	/*
741	 * For a moderate-sized multi-block write, first force a write
742	 * if we're in write-through cache mode, and then fill the
743	 * cache with the blocks.
744	 */
745	writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
746	if (writethrough)
747		retval = raw_write_blk(channel, data, block, count, buf);
748
749	cp = buf;
750	while (count > 0) {
751		cache = find_cached_block(data, block, &reuse);
752		if (!cache) {
753			cache = reuse;
754			reuse_cache(channel, data, cache, block);
755		}
756		memcpy(cache->buf, cp, channel->block_size);
757		cache->dirty = !writethrough;
758		count--;
759		block++;
760		cp += channel->block_size;
761	}
762	return retval;
763#endif /* NO_IO_CACHE */
764}
765
766static errcode_t unix_write_blk(io_channel channel, unsigned long block,
767				int count, const void *buf)
768{
769	return unix_write_blk64(channel, block, count, buf);
770}
771
772static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
773				 int size, const void *buf)
774{
775	struct unix_private_data *data;
776	errcode_t	retval = 0;
777	ssize_t		actual;
778
779	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
780	data = (struct unix_private_data *) channel->private_data;
781	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
782
783	if (data->align != 0) {
784#ifdef ALIGN_DEBUG
785		printf("unix_write_byte: O_DIRECT fallback\n");
786#endif
787		return EXT2_ET_UNIMPLEMENTED;
788	}
789
790#ifndef NO_IO_CACHE
791	/*
792	 * Flush out the cache completely
793	 */
794	if ((retval = flush_cached_blocks(channel, data, 1)))
795		return retval;
796#endif
797
798	if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
799		return errno;
800
801	actual = write(data->dev, buf, size);
802	if (actual != size)
803		return EXT2_ET_SHORT_WRITE;
804
805	return 0;
806}
807
808/*
809 * Flush data buffers to disk.
810 */
811static errcode_t unix_flush(io_channel channel)
812{
813	struct unix_private_data *data;
814	errcode_t retval = 0;
815
816	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
817	data = (struct unix_private_data *) channel->private_data;
818	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
819
820#ifndef NO_IO_CACHE
821	retval = flush_cached_blocks(channel, data, 0);
822#endif
823	fsync(data->dev);
824	return retval;
825}
826
827static errcode_t unix_set_option(io_channel channel, const char *option,
828				 const char *arg)
829{
830	struct unix_private_data *data;
831	unsigned long long tmp;
832	char *end;
833
834	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
835	data = (struct unix_private_data *) channel->private_data;
836	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
837
838	if (!strcmp(option, "offset")) {
839		if (!arg)
840			return EXT2_ET_INVALID_ARGUMENT;
841
842		tmp = strtoull(arg, &end, 0);
843		if (*end)
844			return EXT2_ET_INVALID_ARGUMENT;
845		data->offset = tmp;
846		if (data->offset < 0)
847			return EXT2_ET_INVALID_ARGUMENT;
848		return 0;
849	}
850	return EXT2_ET_INVALID_ARGUMENT;
851}
852
853#if defined(__linux__) && !defined(BLKDISCARD)
854#define BLKDISCARD	_IO(0x12,119)
855#endif
856
857static errcode_t unix_discard(io_channel channel, unsigned long long block,
858			      unsigned long long count)
859{
860#ifdef BLKDISCARD
861	struct unix_private_data *data;
862	__uint64_t	range[2];
863	int		ret;
864
865	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
866	data = (struct unix_private_data *) channel->private_data;
867	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
868
869	range[0] = (__uint64_t)(block) * channel->block_size;
870	range[1] = (__uint64_t)(count) * channel->block_size;
871
872	ret = ioctl(data->dev, BLKDISCARD, &range);
873	if (ret < 0)
874		return errno;
875	return 0;
876#else
877	return EXT2_ET_UNIMPLEMENTED;
878#endif
879}
880