unix_io.c revision dd0a2679ddd0a9bf53e32efc0f67a7e7a5ea5f00
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	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 = ext2fs_get_memalign(channel->block_size,
320					     channel->align, &cache->buf);
321		if (retval)
322			return retval;
323	}
324	if (channel->align) {
325		if (data->bounce)
326			ext2fs_free_mem(&data->bounce);
327		retval = ext2fs_get_memalign(channel->block_size,
328					     channel->align,
329					     &data->bounce);
330	}
331	return retval;
332}
333
334/* Free the cache buffers */
335static void free_cache(struct unix_private_data *data)
336{
337	struct unix_cache	*cache;
338	int			i;
339
340	data->access_time = 0;
341	for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
342		cache->block = 0;
343		cache->access_time = 0;
344		cache->dirty = 0;
345		cache->in_use = 0;
346		if (cache->buf)
347			ext2fs_free_mem(&cache->buf);
348	}
349	if (data->bounce)
350		ext2fs_free_mem(&data->bounce);
351}
352
353#ifndef NO_IO_CACHE
354/*
355 * Try to find a block in the cache.  If the block is not found, and
356 * eldest is a non-zero pointer, then fill in eldest with the cache
357 * entry to that should be reused.
358 */
359static struct unix_cache *find_cached_block(struct unix_private_data *data,
360					    unsigned long long block,
361					    struct unix_cache **eldest)
362{
363	struct unix_cache	*cache, *unused_cache, *oldest_cache;
364	int			i;
365
366	unused_cache = oldest_cache = 0;
367	for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
368		if (!cache->in_use) {
369			if (!unused_cache)
370				unused_cache = cache;
371			continue;
372		}
373		if (cache->block == block) {
374			cache->access_time = ++data->access_time;
375			return cache;
376		}
377		if (!oldest_cache ||
378		    (cache->access_time < oldest_cache->access_time))
379			oldest_cache = cache;
380	}
381	if (eldest)
382		*eldest = (unused_cache) ? unused_cache : oldest_cache;
383	return 0;
384}
385
386/*
387 * Reuse a particular cache entry for another block.
388 */
389static void reuse_cache(io_channel channel, struct unix_private_data *data,
390		 struct unix_cache *cache, unsigned long long block)
391{
392	if (cache->dirty && cache->in_use)
393		raw_write_blk(channel, data, cache->block, 1, cache->buf);
394
395	cache->in_use = 1;
396	cache->dirty = 0;
397	cache->block = block;
398	cache->access_time = ++data->access_time;
399}
400
401/*
402 * Flush all of the blocks in the cache
403 */
404static errcode_t flush_cached_blocks(io_channel channel,
405				     struct unix_private_data *data,
406				     int invalidate)
407
408{
409	struct unix_cache	*cache;
410	errcode_t		retval, retval2;
411	int			i;
412
413	retval2 = 0;
414	for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
415		if (!cache->in_use)
416			continue;
417
418		if (invalidate)
419			cache->in_use = 0;
420
421		if (!cache->dirty)
422			continue;
423
424		retval = raw_write_blk(channel, data,
425				       cache->block, 1, cache->buf);
426		if (retval)
427			retval2 = retval;
428		else
429			cache->dirty = 0;
430	}
431	return retval2;
432}
433#endif /* NO_IO_CACHE */
434
435#ifdef __linux__
436#ifndef BLKDISCARDZEROES
437#define BLKDISCARDZEROES _IO(0x12,124)
438#endif
439#endif
440
441static errcode_t unix_open(const char *name, int flags, io_channel *channel)
442{
443	io_channel	io = NULL;
444	struct unix_private_data *data = NULL;
445	errcode_t	retval;
446	int		open_flags, zeroes = 0;
447	int		f_nocache = 0;
448	ext2fs_struct_stat st;
449#ifdef __linux__
450	struct 		utsname ut;
451#endif
452
453	if (name == 0)
454		return EXT2_ET_BAD_DEVICE_NAME;
455	retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
456	if (retval)
457		goto cleanup;
458	memset(io, 0, sizeof(struct struct_io_channel));
459	io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
460	retval = ext2fs_get_mem(sizeof(struct unix_private_data), &data);
461	if (retval)
462		goto cleanup;
463
464	io->manager = unix_io_manager;
465	retval = ext2fs_get_mem(strlen(name)+1, &io->name);
466	if (retval)
467		goto cleanup;
468
469	strcpy(io->name, name);
470	io->private_data = data;
471	io->block_size = 1024;
472	io->read_error = 0;
473	io->write_error = 0;
474	io->refcount = 1;
475
476	memset(data, 0, sizeof(struct unix_private_data));
477	data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL;
478	data->io_stats.num_fields = 2;
479
480	open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
481	if (flags & IO_FLAG_EXCLUSIVE)
482		open_flags |= O_EXCL;
483#if defined(O_DIRECT)
484	if (flags & IO_FLAG_DIRECT_IO) {
485		open_flags |= O_DIRECT;
486		io->align = ext2fs_get_dio_alignment(data->dev);
487	}
488#elif defined(F_NOCACHE)
489	if (flags & IO_FLAG_DIRECT_IO) {
490		f_nocache = F_NOCACHE;
491		io->align = 4096;
492	}
493#endif
494	data->flags = flags;
495
496	data->dev = ext2fs_open_file(io->name, open_flags, 0);
497	if (data->dev < 0) {
498		retval = errno;
499		goto cleanup;
500	}
501	if (f_nocache) {
502		if (fcntl(data->dev, f_nocache, 1) < 0) {
503			retval = errno;
504			goto cleanup;
505		}
506	}
507
508	/*
509	 * If the device is really a block device, then set the
510	 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
511	 * because we are going to use punch hole instead of discard
512	 * and if it succeed, subsequent read from sparse area returns
513	 * zero.
514	 */
515	if (ext2fs_stat(io->name, &st) == 0) {
516		if (S_ISBLK(st.st_mode))
517			io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
518		else
519			io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
520	}
521
522#ifdef BLKDISCARDZEROES
523	ioctl(data->dev, BLKDISCARDZEROES, &zeroes);
524	if (zeroes)
525		io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
526#endif
527
528#if defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
529	/*
530	 * Some operating systems require that the buffers be aligned,
531	 * regardless of O_DIRECT
532	 */
533	if (!io->align)
534		io->align = 512;
535#endif
536
537
538	if ((retval = alloc_cache(io, data)))
539		goto cleanup;
540
541#ifdef BLKROGET
542	if (flags & IO_FLAG_RW) {
543		int error;
544		int readonly = 0;
545
546		/* Is the block device actually writable? */
547		error = ioctl(data->dev, BLKROGET, &readonly);
548		if (!error && readonly) {
549			close(data->dev);
550			retval = EPERM;
551			goto cleanup;
552		}
553	}
554#endif
555
556#ifdef __linux__
557#undef RLIM_INFINITY
558#if (defined(__alpha__) || ((defined(__sparc__) || defined(__mips__)) && (SIZEOF_LONG == 4)))
559#define RLIM_INFINITY	((unsigned long)(~0UL>>1))
560#else
561#define RLIM_INFINITY  (~0UL)
562#endif
563	/*
564	 * Work around a bug in 2.4.10-2.4.18 kernels where writes to
565	 * block devices are wrongly getting hit by the filesize
566	 * limit.  This workaround isn't perfect, since it won't work
567	 * if glibc wasn't built against 2.2 header files.  (Sigh.)
568	 *
569	 */
570	if ((flags & IO_FLAG_RW) &&
571	    (uname(&ut) == 0) &&
572	    ((ut.release[0] == '2') && (ut.release[1] == '.') &&
573	     (ut.release[2] == '4') && (ut.release[3] == '.') &&
574	     (ut.release[4] == '1') && (ut.release[5] >= '0') &&
575	     (ut.release[5] < '8')) &&
576	    (ext2fs_stat(io->name, &st) == 0) &&
577	    (S_ISBLK(st.st_mode))) {
578		struct rlimit	rlim;
579
580		rlim.rlim_cur = rlim.rlim_max = (unsigned long) RLIM_INFINITY;
581		setrlimit(RLIMIT_FSIZE, &rlim);
582		getrlimit(RLIMIT_FSIZE, &rlim);
583		if (((unsigned long) rlim.rlim_cur) <
584		    ((unsigned long) rlim.rlim_max)) {
585			rlim.rlim_cur = rlim.rlim_max;
586			setrlimit(RLIMIT_FSIZE, &rlim);
587		}
588	}
589#endif
590	*channel = io;
591	return 0;
592
593cleanup:
594	if (data) {
595		free_cache(data);
596		ext2fs_free_mem(&data);
597	}
598	if (io)
599		ext2fs_free_mem(&io);
600	return retval;
601}
602
603static errcode_t unix_close(io_channel channel)
604{
605	struct unix_private_data *data;
606	errcode_t	retval = 0;
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->refcount > 0)
613		return 0;
614
615#ifndef NO_IO_CACHE
616	retval = flush_cached_blocks(channel, data, 0);
617#endif
618
619	if (close(data->dev) < 0)
620		retval = errno;
621	free_cache(data);
622
623	ext2fs_free_mem(&channel->private_data);
624	if (channel->name)
625		ext2fs_free_mem(&channel->name);
626	ext2fs_free_mem(&channel);
627	return retval;
628}
629
630static errcode_t unix_set_blksize(io_channel channel, int blksize)
631{
632	struct unix_private_data *data;
633	errcode_t		retval;
634
635	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
636	data = (struct unix_private_data *) channel->private_data;
637	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
638
639	if (channel->block_size != blksize) {
640#ifndef NO_IO_CACHE
641		if ((retval = flush_cached_blocks(channel, data, 0)))
642			return retval;
643#endif
644
645		channel->block_size = blksize;
646		free_cache(data);
647		if ((retval = alloc_cache(channel, data)))
648			return retval;
649	}
650	return 0;
651}
652
653
654static errcode_t unix_read_blk64(io_channel channel, unsigned long long block,
655			       int count, void *buf)
656{
657	struct unix_private_data *data;
658	struct unix_cache *cache, *reuse[READ_DIRECT_SIZE];
659	errcode_t	retval;
660	char		*cp;
661	int		i, j;
662
663	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
664	data = (struct unix_private_data *) channel->private_data;
665	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
666
667#ifdef NO_IO_CACHE
668	return raw_read_blk(channel, data, block, count, buf);
669#else
670	/*
671	 * If we're doing an odd-sized read or a very large read,
672	 * flush out the cache and then do a direct read.
673	 */
674	if (count < 0 || count > WRITE_DIRECT_SIZE) {
675		if ((retval = flush_cached_blocks(channel, data, 0)))
676			return retval;
677		return raw_read_blk(channel, data, block, count, buf);
678	}
679
680	cp = buf;
681	while (count > 0) {
682		/* If it's in the cache, use it! */
683		if ((cache = find_cached_block(data, block, &reuse[0]))) {
684#ifdef DEBUG
685			printf("Using cached block %lu\n", block);
686#endif
687			memcpy(cp, cache->buf, channel->block_size);
688			count--;
689			block++;
690			cp += channel->block_size;
691			continue;
692		}
693		if (count == 1) {
694			/*
695			 * Special case where we read directly into the
696			 * cache buffer; important in the O_DIRECT case
697			 */
698			cache = reuse[0];
699			reuse_cache(channel, data, cache, block);
700			if ((retval = raw_read_blk(channel, data, block, 1,
701						   cache->buf))) {
702				cache->in_use = 0;
703				return retval;
704			}
705			memcpy(cp, cache->buf, channel->block_size);
706			return 0;
707		}
708
709		/*
710		 * Find the number of uncached blocks so we can do a
711		 * single read request
712		 */
713		for (i=1; i < count; i++)
714			if (find_cached_block(data, block+i, &reuse[i]))
715				break;
716#ifdef DEBUG
717		printf("Reading %d blocks starting at %lu\n", i, block);
718#endif
719		if ((retval = raw_read_blk(channel, data, block, i, cp)))
720			return retval;
721
722		/* Save the results in the cache */
723		for (j=0; j < i; j++) {
724			count--;
725			cache = reuse[j];
726			reuse_cache(channel, data, cache, block++);
727			memcpy(cache->buf, cp, channel->block_size);
728			cp += channel->block_size;
729		}
730	}
731	return 0;
732#endif /* NO_IO_CACHE */
733}
734
735static errcode_t unix_read_blk(io_channel channel, unsigned long block,
736			       int count, void *buf)
737{
738	return unix_read_blk64(channel, block, count, buf);
739}
740
741static errcode_t unix_write_blk64(io_channel channel, unsigned long long block,
742				int count, const void *buf)
743{
744	struct unix_private_data *data;
745	struct unix_cache *cache, *reuse;
746	errcode_t	retval = 0;
747	const char	*cp;
748	int		writethrough;
749
750	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
751	data = (struct unix_private_data *) channel->private_data;
752	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
753
754#ifdef NO_IO_CACHE
755	return raw_write_blk(channel, data, block, count, buf);
756#else
757	/*
758	 * If we're doing an odd-sized write or a very large write,
759	 * flush out the cache completely and then do a direct write.
760	 */
761	if (count < 0 || count > WRITE_DIRECT_SIZE) {
762		if ((retval = flush_cached_blocks(channel, data, 1)))
763			return retval;
764		return raw_write_blk(channel, data, block, count, buf);
765	}
766
767	/*
768	 * For a moderate-sized multi-block write, first force a write
769	 * if we're in write-through cache mode, and then fill the
770	 * cache with the blocks.
771	 */
772	writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
773	if (writethrough)
774		retval = raw_write_blk(channel, data, block, count, buf);
775
776	cp = buf;
777	while (count > 0) {
778		cache = find_cached_block(data, block, &reuse);
779		if (!cache) {
780			cache = reuse;
781			reuse_cache(channel, data, cache, block);
782		}
783		memcpy(cache->buf, cp, channel->block_size);
784		cache->dirty = !writethrough;
785		count--;
786		block++;
787		cp += channel->block_size;
788	}
789	return retval;
790#endif /* NO_IO_CACHE */
791}
792
793static errcode_t unix_write_blk(io_channel channel, unsigned long block,
794				int count, const void *buf)
795{
796	return unix_write_blk64(channel, block, count, buf);
797}
798
799static errcode_t unix_write_byte(io_channel channel, unsigned long offset,
800				 int size, const void *buf)
801{
802	struct unix_private_data *data;
803	errcode_t	retval = 0;
804	ssize_t		actual;
805
806	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
807	data = (struct unix_private_data *) channel->private_data;
808	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
809
810	if (channel->align != 0) {
811#ifdef ALIGN_DEBUG
812		printf("unix_write_byte: O_DIRECT fallback\n");
813#endif
814		return EXT2_ET_UNIMPLEMENTED;
815	}
816
817#ifndef NO_IO_CACHE
818	/*
819	 * Flush out the cache completely
820	 */
821	if ((retval = flush_cached_blocks(channel, data, 1)))
822		return retval;
823#endif
824
825	if (lseek(data->dev, offset + data->offset, SEEK_SET) < 0)
826		return errno;
827
828	actual = write(data->dev, buf, size);
829	if (actual != size)
830		return EXT2_ET_SHORT_WRITE;
831
832	return 0;
833}
834
835/*
836 * Flush data buffers to disk.
837 */
838static errcode_t unix_flush(io_channel channel)
839{
840	struct unix_private_data *data;
841	errcode_t retval = 0;
842
843	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
844	data = (struct unix_private_data *) channel->private_data;
845	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
846
847#ifndef NO_IO_CACHE
848	retval = flush_cached_blocks(channel, data, 0);
849#endif
850	fsync(data->dev);
851	return retval;
852}
853
854static errcode_t unix_set_option(io_channel channel, const char *option,
855				 const char *arg)
856{
857	struct unix_private_data *data;
858	unsigned long long tmp;
859	char *end;
860
861	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
862	data = (struct unix_private_data *) channel->private_data;
863	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
864
865	if (!strcmp(option, "offset")) {
866		if (!arg)
867			return EXT2_ET_INVALID_ARGUMENT;
868
869		tmp = strtoull(arg, &end, 0);
870		if (*end)
871			return EXT2_ET_INVALID_ARGUMENT;
872		data->offset = tmp;
873		if (data->offset < 0)
874			return EXT2_ET_INVALID_ARGUMENT;
875		return 0;
876	}
877	return EXT2_ET_INVALID_ARGUMENT;
878}
879
880#if defined(__linux__) && !defined(BLKDISCARD)
881#define BLKDISCARD		_IO(0x12,119)
882#endif
883
884static errcode_t unix_discard(io_channel channel, unsigned long long block,
885			      unsigned long long count)
886{
887	struct unix_private_data *data;
888	__uint64_t	range[2];
889	int		ret;
890
891	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
892	data = (struct unix_private_data *) channel->private_data;
893	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL);
894
895	if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
896#ifdef BLKDISCARD
897		range[0] = (__uint64_t)(block) * channel->block_size;
898		range[1] = (__uint64_t)(count) * channel->block_size;
899
900		ret = ioctl(data->dev, BLKDISCARD, &range);
901#else
902		goto unimplemented;
903#endif
904	} else {
905#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
906		/*
907		 * If we are not on block device, try to use punch hole
908		 * to reclaim free space.
909		 */
910		ret = fallocate(data->dev,
911				FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
912				(off_t)(block) * channel->block_size,
913				(off_t)(count) * channel->block_size);
914#else
915		goto unimplemented;
916#endif
917	}
918	if (ret < 0) {
919		if (errno == EOPNOTSUPP)
920			goto unimplemented;
921		return errno;
922	}
923	return 0;
924unimplemented:
925	return EXT2_ET_UNIMPLEMENTED;
926}
927