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