xref: /aosp_15_r20/external/e2fsprogs/lib/ext2fs/windows_io.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1 /*
2  * windows_io.c --- This is the Windows implementation of the I/O manager.
3  *
4  * Implements a one-block write-through cache.
5  *
6  * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
7  *	2002 by Theodore Ts'o.
8  *
9  * %Begin-Header%
10  * This file may be redistributed under the terms of the GNU Library
11  * General Public License, version 2.
12  * %End-Header%
13  */
14 
15 #include <windows.h>
16 #include <winioctl.h>
17 #include <io.h>
18 
19 #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
20 #define _XOPEN_SOURCE 600
21 #define _DARWIN_C_SOURCE
22 #define _FILE_OFFSET_BITS 64
23 #ifndef _LARGEFILE_SOURCE
24 #define _LARGEFILE_SOURCE
25 #endif
26 #ifndef _LARGEFILE64_SOURCE
27 #define _LARGEFILE64_SOURCE
28 #endif
29 #ifndef _GNU_SOURCE
30 #define _GNU_SOURCE
31 #endif
32 #endif
33 
34 #include "config.h"
35 #include <stdio.h>
36 #include <string.h>
37 #if HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40 #if HAVE_ERRNO_H
41 #include <errno.h>
42 #endif
43 #include <fcntl.h>
44 #include <time.h>
45 #if HAVE_SYS_TYPES_H
46 #include <sys/types.h>
47 #endif
48 #define PR_GET_DUMPABLE 3
49 #if HAVE_SYS_STAT_H
50 #include <sys/stat.h>
51 #endif
52 #include <fcntl.h>
53 
54 #undef ALIGN_DEBUG
55 
56 //#define DEBUG
57 #ifdef DEBUG
58 #define TRACE(...) {\
59 		    char __log[256];\
60 		    snprintf(__log, sizeof(__log), __VA_ARGS__);\
61 		    __log[sizeof(__log)-1] = 0;\
62 		    OutputDebugString(__log);\
63 		}
64 #else
65 #define TRACE(...) do { } while (0);
66 #endif
67 
68 #include "ext2_fs.h"
69 #include "ext2fs.h"
70 #include "ext2fsP.h"
71 
72 /*
73  * For checking structure magic numbers...
74  */
75 
76 #define EXT2_CHECK_MAGIC(struct, code) \
77 	  if ((struct)->magic != (code)) return (code)
78 #define EXT2_CHECK_MAGIC_RETURN(struct, code, ret) \
79 	  if ((struct)->magic != (code)) return (ret)
80 
81 #define EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL  0x10ed
82 
83 struct windows_cache {
84 	char			*buf;
85 	unsigned long long	block;
86 	int			access_time;
87 	unsigned		dirty:1;
88 	unsigned		in_use:1;
89 };
90 
91 #define CACHE_SIZE 8
92 #define WRITE_DIRECT_SIZE 4	/* Must be smaller than CACHE_SIZE */
93 #define READ_DIRECT_SIZE 4	/* Should be smaller than CACHE_SIZE */
94 
95 struct windows_private_data {
96 	int	magic;
97 	char	name[MAX_PATH];
98 	HANDLE	handle;
99 	char	dos_device[MAX_PATH];
100 	char	cf_device[MAX_PATH];
101 	int	dev;
102 	int	flags;
103 	int	align;
104 	int	access_time;
105 	ext2_loff_t offset;
106 	struct windows_cache cache[CACHE_SIZE];
107 	void	*bounce;
108 	struct struct_io_stats io_stats;
109 };
110 
111 #define IS_ALIGNED(n, align) ((((uintptr_t) n) & \
112 			       ((uintptr_t) ((align)-1))) == 0)
113 
fake_dos_name_for_device(struct windows_private_data * data)114 static int fake_dos_name_for_device(struct windows_private_data *data)
115 {
116 	if (strncmp(data->name, "\\\\", 2) == 0) {
117 		data->dos_device[0] = 0;
118 		strcpy(data->cf_device, data->name);
119 		return 0;
120 	}
121 
122 	_snprintf(data->dos_device, MAX_PATH, "fakedevice%lu", GetCurrentProcessId());
123 
124 	if (!DefineDosDevice(DDD_RAW_TARGET_PATH, data->dos_device, data->name))
125 		return 1;
126 
127 	_snprintf(data->cf_device, MAX_PATH, "\\\\.\\%s", data->dos_device);
128 	TRACE("e2fsprogs::fake_dos_name_for_device::DefineDosDevice(\"%s\")", data->dos_device);
129 
130 	return 0;
131 }
132 
remove_fake_dos_name(struct windows_private_data * data)133 static void remove_fake_dos_name(struct windows_private_data *data)
134 {
135 	if (*data->dos_device) {
136 		TRACE("e2fsprogs::remove_fake_dos_name::DefineDosDevice(\"%s\")", data->dos_device);
137 		DefineDosDevice(DDD_RAW_TARGET_PATH | DDD_EXACT_MATCH_ON_REMOVE | DDD_REMOVE_DEFINITION, data->dos_device, data->name);
138 	}
139 }
140 
windows_get_stats(io_channel channel,io_stats * stats)141 static errcode_t windows_get_stats(io_channel channel, io_stats *stats)
142 {
143 	errcode_t	retval = 0;
144 
145 	struct windows_private_data *data;
146 
147 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
148 	data = (struct windows_private_data *) channel->private_data;
149 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
150 
151 	if (stats)
152 		*stats = &data->io_stats;
153 
154 	return retval;
155 }
156 
157 /*
158  * Here are the raw I/O functions
159  */
raw_read_blk(io_channel channel,struct windows_private_data * data,unsigned long long block,int count,void * bufv)160 static errcode_t raw_read_blk(io_channel channel,
161 			      struct windows_private_data *data,
162 			      unsigned long long block,
163 			      int count, void *bufv)
164 {
165 	errcode_t	retval;
166 	ssize_t	size;
167 	ext2_loff_t	location;
168 	DWORD		actual = 0;
169 	unsigned char	*buf = bufv;
170 	ssize_t		really_read = 0;
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 
176 	if (data->flags & IO_FLAG_FORCE_BOUNCE) {
177 		if (SetFilePointer(data->handle, location, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
178 			retval = GetLastError();
179 			goto error_out;
180 		}
181 		goto bounce_read;
182 	}
183 
184 	if (SetFilePointer(data->handle, location, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
185 		retval = GetLastError();
186 		goto error_out;
187 	}
188 	if ((channel->align == 0) || (IS_ALIGNED(buf, channel->align) && IS_ALIGNED(size, channel->align))) {
189 		if (!ReadFile(data->handle, buf, size, &actual, NULL)) {
190 			retval = GetLastError();
191 			goto error_out;
192 		}
193 		if (actual != size) {
194 	short_read:
195 		if (actual < 0) {
196 			retval = GetLastError();
197 			actual = 0;
198 		} else
199 			retval = EXT2_ET_SHORT_READ;
200 		goto error_out;
201 		}
202 	return 0;
203 	}
204 
205 	/*
206 	 * The buffer or size which we're trying to read isn't aligned
207 	 * to the O_DIRECT rules, so we need to do this the hard way...
208 	 */
209 bounce_read:
210 	while (size > 0) {
211 		if (!ReadFile(data->handle, data->bounce, channel->block_size, &actual, NULL)) {
212 			retval = GetLastError();
213 			goto error_out;
214 		}
215 		if (actual != channel->block_size) {
216 			actual = really_read;
217 			buf -= really_read;
218 			size += really_read;
219 			goto short_read;
220 		}
221 		actual = size;
222 		if (size > channel->block_size)
223 			actual = channel->block_size;
224 		memcpy(buf, data->bounce, actual);
225 		really_read += actual;
226 		size -= actual;
227 		buf += actual;
228 	}
229 	return 0;
230 
231 error_out:
232 	if (actual >= 0 && actual < size)
233 		memset((char *) buf+actual, 0, size-actual);
234 	if (channel->read_error)
235 		retval = (channel->read_error)(channel, block, count, buf,
236 					       size, actual, retval);
237 	return retval;
238 }
239 
raw_write_blk(io_channel channel,struct windows_private_data * data,unsigned long long block,int count,const void * bufv)240 static errcode_t raw_write_blk(io_channel channel,
241 			       struct windows_private_data *data,
242 			       unsigned long long block,
243 			       int count, const void *bufv)
244 {
245 	ssize_t		size;
246 	ext2_loff_t	location;
247 	DWORD		actual = 0;
248 	errcode_t	retval;
249 	const unsigned char *buf = bufv;
250 
251 	if (count == 1)
252 		size = channel->block_size;
253 	else {
254 		if (count < 0)
255 			size = -count;
256 		else
257 			size = count * channel->block_size;
258 	}
259 	data->io_stats.bytes_written += size;
260 
261 	location = ((ext2_loff_t) block * channel->block_size) + data->offset;
262 
263 	if (data->flags & IO_FLAG_FORCE_BOUNCE) {
264 		if (SetFilePointer(data->handle, location, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
265 			retval = GetLastError();
266 			goto error_out;
267 		}
268 	goto bounce_write;
269 	}
270 
271 		if (SetFilePointer(data->handle, location, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
272 			retval = GetLastError();
273 		goto error_out;
274 	}
275 
276 	SetLastError(0);
277 
278 	if ((channel->align == 0) || (IS_ALIGNED(buf, channel->align) && IS_ALIGNED(size, channel->align))) {
279 		if (!WriteFile(data->handle, buf, size, &actual, NULL)) {
280 			retval = GetLastError();
281 			goto error_out;
282 		}
283 
284 		if (actual != size) {
285 		short_write:
286 			retval = EXT2_ET_SHORT_WRITE;
287 			goto error_out;
288 		}
289 		return 0;
290 	}
291 
292 	/*
293 	 * The buffer or size which we're trying to write isn't aligned
294 	 * to the O_DIRECT rules, so we need to do this the hard way...
295 	 */
296 bounce_write:
297 	while (size > 0) {
298 		if (size < channel->block_size) {
299 			if (!ReadFile(data->handle, data->bounce, channel->block_size, &actual, NULL)) {
300 				retval = GetLastError();
301 				goto error_out;
302 			}
303 			if (actual != channel->block_size) {
304 				if (actual < 0) {
305 					retval = GetLastError();
306 					goto error_out;
307 				}
308 				memset((char *) data->bounce + actual, 0,
309 				       channel->block_size - actual);
310 			}
311 		}
312 		actual = size;
313 		if (size > channel->block_size)
314 			actual = channel->block_size;
315 		memcpy(data->bounce, buf, actual);
316 		if (SetFilePointer(data->handle, location, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
317 			retval = GetLastError();
318 			goto error_out;
319 		}
320 		if (!WriteFile(data->handle, data->bounce, channel->block_size, &actual, NULL)) {
321 			retval = GetLastError();
322 			goto error_out;
323 		}
324 
325 		if (actual != channel->block_size)
326 			goto short_write;
327 		size -= actual;
328 		buf += actual;
329 		location += actual;
330 	}
331 	return 0;
332 
333 error_out:
334 	if (channel->write_error)
335 		retval = (channel->write_error)(channel, block, count, buf,
336 						size, actual, retval);
337 	return retval;
338 }
339 
340 
341 /*
342  * Here we implement the cache functions
343  */
344 
345 /* Allocate the cache buffers */
alloc_cache(io_channel channel,struct windows_private_data * data)346 static errcode_t alloc_cache(io_channel channel,
347 			     struct windows_private_data *data)
348 {
349 	errcode_t		retval;
350 	struct windows_cache	*cache;
351 	int			i;
352 
353 	data->access_time = 0;
354 	for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
355 		cache->block = 0;
356 		cache->access_time = 0;
357 		cache->dirty = 0;
358 		cache->in_use = 0;
359 		if (cache->buf)
360 			ext2fs_free_mem(&cache->buf);
361 		retval = io_channel_alloc_buf(channel, 0, &cache->buf);
362 		if (retval)
363 			return retval;
364 	}
365 	if (channel->align || data->flags & IO_FLAG_FORCE_BOUNCE) {
366 		if (data->bounce)
367 			ext2fs_free_mem(&data->bounce);
368 		retval = io_channel_alloc_buf(channel, 0, &data->bounce);
369 	}
370 	return retval;
371 }
372 
373 /* Free the cache buffers */
free_cache(struct windows_private_data * data)374 static void free_cache(struct windows_private_data *data)
375 {
376 	struct windows_cache    *cache;
377 	int			i;
378 
379 	data->access_time = 0;
380 	for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
381 		cache->block = 0;
382 		cache->access_time = 0;
383 		cache->dirty = 0;
384 		cache->in_use = 0;
385 		if (cache->buf)
386 			ext2fs_free_mem(&cache->buf);
387 	}
388 	if (data->bounce)
389 		ext2fs_free_mem(&data->bounce);
390 }
391 
392 #ifndef NO_IO_CACHE
393 /*
394  * Try to find a block in the cache.  If the block is not found, and
395  * eldest is a non-zero pointer, then fill in eldest with the cache
396  * entry to that should be reused.
397  */
find_cached_block(struct windows_private_data * data,unsigned long long block,struct windows_cache ** eldest)398 static struct windows_cache *find_cached_block(struct windows_private_data *data,
399 					    unsigned long long block,
400 					    struct windows_cache **eldest)
401 {
402 	struct windows_cache	*cache, *unused_cache, *oldest_cache;
403 	int			i;
404 
405 	unused_cache = oldest_cache = 0;
406 	for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
407 		if (!cache->in_use) {
408 			if (!unused_cache)
409 				unused_cache = cache;
410 			continue;
411 		}
412 		if (cache->block == block) {
413 			cache->access_time = ++data->access_time;
414 			return cache;
415 		}
416 		if (!oldest_cache ||
417 		   (cache->access_time < oldest_cache->access_time))
418 			oldest_cache = cache;
419 	}
420 	if (eldest)
421 		*eldest = (unused_cache) ? unused_cache : oldest_cache;
422 	return 0;
423 }
424 
425 /*
426  * Reuse a particular cache entry for another block.
427  */
reuse_cache(io_channel channel,struct windows_private_data * data,struct windows_cache * cache,unsigned long long block)428 static void reuse_cache(io_channel channel, struct windows_private_data *data,
429 		 struct windows_cache *cache, unsigned long long block)
430 {
431 	if (cache->dirty && cache->in_use)
432 		raw_write_blk(channel, data, cache->block, 1, cache->buf);
433 
434 	cache->in_use = 1;
435 	cache->dirty = 0;
436 	cache->block = block;
437 	cache->access_time = ++data->access_time;
438 }
439 
440 /*
441  * Flush all of the blocks in the cache
442  */
flush_cached_blocks(io_channel channel,struct windows_private_data * data,int invalidate)443 static errcode_t flush_cached_blocks(io_channel channel,
444 				     struct windows_private_data *data,
445 				     int invalidate)
446 {
447 	struct windows_cache	*cache;
448 	errcode_t	retval, retval2;
449 	int			i;
450 
451 	retval2 = 0;
452 	for (i=0, cache = data->cache; i < CACHE_SIZE; i++, cache++) {
453 		if (!cache->in_use)
454 			continue;
455 
456 		if (invalidate)
457 			cache->in_use = 0;
458 
459 		if (!cache->dirty)
460 			continue;
461 
462 		retval = raw_write_blk(channel, data,
463 				       cache->block, 1, cache->buf);
464 		if (retval)
465 			retval2 = retval;
466 		else
467 			cache->dirty = 0;
468 	}
469 	return retval2;
470 }
471 #endif /* NO_IO_CACHE */
472 
windows_open_channel(struct windows_private_data * data,int flags,io_channel * channel,io_manager io_mgr)473 static errcode_t windows_open_channel(struct windows_private_data *data,
474 				   int flags, io_channel *channel,
475 				   io_manager io_mgr)
476 {
477 	io_channel	io = NULL;
478 	errcode_t	retval;
479 	ext2fs_struct_stat st;
480 
481 	retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io);
482 	if (retval)
483 		goto cleanup;
484 	memset(io, 0, sizeof(struct struct_io_channel));
485 	io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
486 
487 	io->manager = io_mgr;
488 	retval = ext2fs_get_mem(strlen(data->name)+1, &io->name);
489 	if (retval)
490 		goto cleanup;
491 
492 	strcpy(io->name, data->name);
493 	io->private_data = data;
494 	io->block_size = 1024;
495 	io->read_error = 0;
496 	io->write_error = 0;
497 	io->refcount = 1;
498 
499 #if defined(O_DIRECT)
500 	if (flags & IO_FLAG_DIRECT_IO)
501 		io->align = ext2fs_get_dio_alignment(data->dev);
502 #endif
503 
504 	/*
505 	 * If the device is really a block device, then set the
506 	 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
507 	 * because we are going to use punch hole instead of discard
508 	 * and if it succeed, subsequent read from sparse area returns
509 	 * zero.
510 	 */
511 	if (ext2fs_fstat(data->dev, &st) == 0) {
512 		if (ext2fsP_is_disk_device(st.st_mode))
513 			io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
514 		else
515 			io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
516 	}
517 
518 	if ((retval = alloc_cache(io, data)))
519 		goto cleanup;
520 
521 #ifdef BLKROGET
522 	if (flags & IO_FLAG_RW) {
523 		int error;
524 		int readonly = 0;
525 
526 		/* Is the block device actually writable? */
527 		error = ioctl(data->dev, BLKROGET, &readonly);
528 		if (!error && readonly) {
529 			retval = EPERM;
530 			goto cleanup;
531 		}
532 	}
533 #endif
534 
535 	*channel = io;
536 	return 0;
537 
538 cleanup:
539 	if (data) {
540 		if (data->dev >= 0)
541 			close(data->dev);
542 		free_cache(data);
543 		ext2fs_free_mem(&data);
544 	}
545 	if (io) {
546 		if (io->name) {
547 			ext2fs_free_mem(&io->name);
548 		}
549 		ext2fs_free_mem(&io);
550 	}
551 	return retval;
552 }
553 
windows_open_device(struct windows_private_data * data,int open_flags)554 static DWORD windows_open_device(struct windows_private_data *data, int open_flags)
555 {
556 	DWORD ret = 0;
557 
558 	if (*data->name != '\\')
559 		strcpy(data->cf_device, data->name);
560 	else if (fake_dos_name_for_device(data))
561 		return -1;
562 
563 	DWORD desired_access = GENERIC_READ | ((open_flags & O_RDWR) ? GENERIC_WRITE : 0);
564 	DWORD share_mode = (open_flags & O_EXCL) ? 0 : FILE_SHARE_READ | ((open_flags & O_RDWR) ? FILE_SHARE_WRITE : 0);
565 	DWORD flags_and_attributes =
566 #if defined(O_DIRECT)
567 	(open_flags & O_DIRECT) ? (FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH) : FILE_ATTRIBUTE_NORMAL;
568 #else
569 	FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
570 #endif
571 
572 	data->handle = CreateFile(data->cf_device, desired_access, share_mode, NULL, OPEN_EXISTING,
573 				  flags_and_attributes, NULL);
574 
575 	if (data->handle == INVALID_HANDLE_VALUE) {
576 		ret = GetLastError();
577 		goto invalid_handle;
578 	}
579 
580 	TRACE("e2fsprogs::windows_open_device::CreateFile(\"%s\") = %p", data->cf_device, data->handle);
581 
582 	data->dev = _open_osfhandle((intptr_t)data->handle, 0);
583 	if (data->dev < 0) {
584 		ret = GetLastError() ? GetLastError() : 9999;
585 		goto osfhandle_error;
586 	}
587 
588 	TRACE("e2fsprogs::windows_open_device::_open_osfhandle(%p) = %d", data->handle, data->dev);
589 
590 	return 0;
591 
592 osfhandle_error:
593 	TRACE("e2fsprogs::windows_open_device::CloseHandle(%p)", data->handle);
594 	CloseHandle(data->handle);
595 invalid_handle:
596 	remove_fake_dos_name(data);
597 	TRACE("e2fsprogs::windows_open_device() = %lu, errno = %d", ret, errno);
598 	return ret;
599 }
600 
init_private_data(const char * name,int flags)601 static struct windows_private_data *init_private_data(const char *name, int flags)
602 {
603 	struct windows_private_data *data = NULL;
604 
605 	if (ext2fs_get_mem(sizeof(struct windows_private_data), &data))
606 		return NULL;
607 
608 	memset(data, 0, sizeof(struct windows_private_data));
609 	strncpy(data->name, name, sizeof(data->name) - 1);
610 	data->magic = EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL;
611 	data->io_stats.num_fields = 2;
612 	data->flags = flags;
613 	data->handle = INVALID_HANDLE_VALUE;
614 
615 	return data;
616 }
617 
windows_open(const char * name,int flags,io_channel * channel)618 static errcode_t windows_open(const char *name, int flags, io_channel *channel)
619 {
620 	int open_flags;
621 	struct windows_private_data *data;
622 
623 	if (name == 0)
624 		return EXT2_ET_BAD_DEVICE_NAME;
625 
626 	data = init_private_data(name, flags);
627 	if (!data)
628 		return EXT2_ET_NO_MEMORY;
629 
630 	open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
631 	if (flags & IO_FLAG_EXCLUSIVE)
632 		open_flags |= O_EXCL;
633 #if defined(O_DIRECT)
634 	if (flags & IO_FLAG_DIRECT_IO)
635 		open_flags |= O_DIRECT;
636 #endif
637 
638 	if (windows_open_device(data, open_flags)) {
639 		ext2fs_free_mem(&data);
640 		return EXT2_ET_BAD_DEVICE_NAME;
641 	}
642 
643 	return windows_open_channel(data, flags, channel, windows_io_manager);
644 }
645 
windows_close(io_channel channel)646 static errcode_t windows_close(io_channel channel)
647 {
648 	struct windows_private_data *data;
649 	errcode_t	retval = 0;
650 
651 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
652 	data = (struct windows_private_data *) channel->private_data;
653 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
654 
655 	if (--channel->refcount > 0)
656 		return 0;
657 
658 #ifndef NO_IO_CACHE
659 	retval = flush_cached_blocks(channel, data, 0);
660 #endif
661 
662 	remove_fake_dos_name(data);
663 
664 	if (_close(data->dev) != 0)
665 		retval = errno;
666 
667 	TRACE("e2fsprogs::windows_close::_close(%d)", data->dev);
668 
669 	free_cache(data);
670 
671 	ext2fs_free_mem(&channel->private_data);
672 	if (channel->name)
673 		ext2fs_free_mem(&channel->name);
674 	ext2fs_free_mem(&channel);
675 	return retval;
676 }
677 
windows_set_blksize(io_channel channel,int blksize)678 static errcode_t windows_set_blksize(io_channel channel, int blksize)
679 {
680 	struct windows_private_data *data;
681 	errcode_t		retval;
682 
683 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
684 	data = (struct windows_private_data *) channel->private_data;
685 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
686 
687 	if (channel->block_size != blksize) {
688 #ifndef NO_IO_CACHE
689 		if ((retval = flush_cached_blocks(channel, data, 0)))
690 			return retval;
691 #endif
692 
693 		channel->block_size = blksize;
694 		free_cache(data);
695 		if ((retval = alloc_cache(channel, data)))
696 			return retval;
697 	}
698 	return 0;
699 }
700 
windows_read_blk64(io_channel channel,unsigned long long block,int count,void * buf)701 static errcode_t windows_read_blk64(io_channel channel, unsigned long long block, int count, void *buf)
702 {
703 	struct windows_private_data *data;
704 	struct windows_cache *cache, *reuse[READ_DIRECT_SIZE];
705 	errcode_t	retval;
706 	char		*cp;
707 	int		i, j;
708 
709 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
710 	data = (struct windows_private_data *) channel->private_data;
711 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
712 
713 #ifdef NO_IO_CACHE
714 	return raw_read_blk(channel, data, block, count, buf);
715 #else
716 	/*
717 	 * If we're doing an odd-sized read or a very large read,
718 	 * flush out the cache and then do a direct read.
719 	 */
720 	if (count < 0 || count > WRITE_DIRECT_SIZE) {
721 		if ((retval = flush_cached_blocks(channel, data, 0)))
722 			return retval;
723 		return raw_read_blk(channel, data, block, count, buf);
724 	}
725 
726 	cp = buf;
727 	while (count > 0) {
728 		/* If it's in the cache, use it! */
729 		if ((cache = find_cached_block(data, block, &reuse[0]))) {
730 #ifdef DEBUG
731 			printf("Using cached block %lu\n", block);
732 #endif
733 			memcpy(cp, cache->buf, channel->block_size);
734 			count--;
735 			block++;
736 			cp += channel->block_size;
737 			continue;
738 		}
739 		if (count == 1) {
740 			/*
741 			 * Special case where we read directly into the
742 			 * cache buffer; important in the O_DIRECT case
743 			 */
744 			cache = reuse[0];
745 			reuse_cache(channel, data, cache, block);
746 			if ((retval = raw_read_blk(channel, data, block, 1,
747 						   cache->buf))) {
748 				cache->in_use = 0;
749 				return retval;
750 			}
751 			memcpy(cp, cache->buf, channel->block_size);
752 			return 0;
753 		}
754 
755 		/*
756 		 * Find the number of uncached blocks so we can do a
757 		 * single read request
758 		 */
759 		for (i=1; i < count; i++)
760 			if (find_cached_block(data, block+i, &reuse[i]))
761 				break;
762 #ifdef DEBUG
763 		printf("Reading %d blocks starting at %lu\n", i, block);
764 #endif
765 		if ((retval = raw_read_blk(channel, data, block, i, cp)))
766 			return retval;
767 
768 		/* Save the results in the cache */
769 		for (j=0; j < i; j++) {
770 			count--;
771 			cache = reuse[j];
772 			reuse_cache(channel, data, cache, block++);
773 			memcpy(cache->buf, cp, channel->block_size);
774 			cp += channel->block_size;
775 		}
776 	}
777 	return 0;
778 #endif /* NO_IO_CACHE */
779 }
780 
windows_read_blk(io_channel channel,unsigned long block,int count,void * buf)781 static errcode_t windows_read_blk(io_channel channel, unsigned long block,
782 			       int count, void *buf)
783 {
784 	return windows_read_blk64(channel, block, count, buf);
785 }
786 
windows_write_blk64(io_channel channel,unsigned long long block,int count,const void * buf)787 static errcode_t windows_write_blk64(io_channel channel,
788 				unsigned long long block,
789 				int count, const void *buf)
790 {
791 	struct windows_private_data *data;
792 	struct windows_cache *cache, *reuse;
793 	errcode_t	retval = 0;
794 	const char	*cp;
795 	int		writethrough;
796 
797 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
798 	data = (struct windows_private_data *) channel->private_data;
799 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
800 
801 #ifdef NO_IO_CACHE
802 	return raw_write_blk(channel, data, block, count, buf);
803 #else
804 	/*
805 	 * If we're doing an odd-sized write or a very large write,
806 	 * flush out the cache completely and then do a direct write.
807 	 */
808 	if (count < 0 || count > WRITE_DIRECT_SIZE) {
809 		if ((retval = flush_cached_blocks(channel, data, 1)))
810 			return retval;
811 		return raw_write_blk(channel, data, block, count, buf);
812 	}
813 
814 	/*
815 	 * For a moderate-sized multi-block write, first force a write
816 	 * if we're in write-through cache mode, and then fill the
817 	 * cache with the blocks.
818 	 */
819 	writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
820 	if (writethrough)
821 		retval = raw_write_blk(channel, data, block, count, buf);
822 
823 	cp = buf;
824 	while (count > 0) {
825 		cache = find_cached_block(data, block, &reuse);
826 		if (!cache) {
827 			cache = reuse;
828 			reuse_cache(channel, data, cache, block);
829 		}
830 		if (cache->buf != cp)
831 			memcpy(cache->buf, cp, channel->block_size);
832 		cache->dirty = !writethrough;
833 		count--;
834 		block++;
835 		cp += channel->block_size;
836 	}
837 	return retval;
838 #endif /* NO_IO_CACHE */
839 }
840 
windows_cache_readahead(io_channel channel,unsigned long long block,unsigned long long count)841 static errcode_t windows_cache_readahead(io_channel channel,
842 				      unsigned long long block,
843 				      unsigned long long count)
844 {
845 	return EXT2_ET_OP_NOT_SUPPORTED;
846 }
847 
windows_write_blk(io_channel channel,unsigned long block,int count,const void * buf)848 static errcode_t windows_write_blk(io_channel channel, unsigned long block,
849 				int count, const void *buf)
850 {
851 	return windows_write_blk64(channel, block, count, buf);
852 }
853 
windows_write_byte(io_channel channel,unsigned long offset,int size,const void * buf)854 static errcode_t windows_write_byte(io_channel channel, unsigned long offset,
855 				 int size, const void *buf)
856 {
857 	return EXT2_ET_UNIMPLEMENTED;
858 }
859 
860 /*
861  * Flush data buffers to disk.
862  */
windows_flush(io_channel channel)863 static errcode_t windows_flush(io_channel channel)
864 {
865 	struct windows_private_data *data;
866 	errcode_t retval = 0;
867 
868 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
869 	data = (struct windows_private_data *) channel->private_data;
870 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
871 
872 #ifndef NO_IO_CACHE
873 	retval = flush_cached_blocks(channel, data, 0);
874 #endif
875 
876 	return retval;
877 }
878 
windows_set_option(io_channel channel,const char * option,const char * arg)879 static errcode_t windows_set_option(io_channel channel, const char *option,
880 				 const char *arg)
881 {
882 	struct windows_private_data *data;
883 	unsigned long long tmp;
884 	char *end;
885 
886 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
887 	data = (struct windows_private_data *) channel->private_data;
888 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
889 
890 	if (!strcmp(option, "offset")) {
891 		if (!arg)
892 			return EXT2_ET_INVALID_ARGUMENT;
893 
894 		tmp = strtoull(arg, &end, 0);
895 		if (*end)
896 			return EXT2_ET_INVALID_ARGUMENT;
897 		data->offset = tmp;
898 		if (data->offset < 0)
899 			return EXT2_ET_INVALID_ARGUMENT;
900 		return 0;
901 	}
902 	return EXT2_ET_INVALID_ARGUMENT;
903 }
904 
windows_discard(io_channel channel,unsigned long long block,unsigned long long count)905 static errcode_t windows_discard(io_channel channel, unsigned long long block,
906 				 unsigned long long count)
907 {
908 	TRACE("e2fsprogs::windows_discard::EXT2_ET_UNIMPLEMENTED");
909 	return EXT2_ET_UNIMPLEMENTED;
910 }
911 
912 /* parameters might not be used if OS doesn't support zeroout */
913 #if __GNUC_PREREQ (4, 6)
914 #pragma GCC diagnostic push
915 #pragma GCC diagnostic ignored "-Wunused-parameter"
916 #endif
windows_zeroout(io_channel channel,unsigned long long block,unsigned long long count)917 static errcode_t windows_zeroout(io_channel channel, unsigned long long block,
918 			      unsigned long long count)
919 {
920 	struct windows_private_data *data;
921 	int		ret;
922 
923 	EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
924 	data = (struct windows_private_data *) channel->private_data;
925 	EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
926 
927 	if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
928 		/* Not implemented until the BLKZEROOUT mess is fixed */
929 		goto unimplemented;
930 	} else {
931 		/* Regular file, try to use truncate/punch/zero. */
932 		struct stat statbuf;
933 
934 		if (count == 0)
935 			return 0;
936 		/*
937 		 * If we're trying to zero a range past the end of the file,
938 		 * extend the file size, then truncate everything.
939 		 */
940 		ret = fstat(data->dev, &statbuf);
941 		if (ret)
942 			goto err;
943 		if ((unsigned long long) statbuf.st_size <
944 			(block + count) * channel->block_size + data->offset) {
945 			ret = ftruncate(data->dev,
946 					(block + count) * channel->block_size + data->offset);
947 			if (ret)
948 				goto err;
949 		}
950 		goto unimplemented;
951 	}
952 err:
953 	if (ret < 0) {
954 		if (errno == EOPNOTSUPP)
955 			goto unimplemented;
956 		return errno;
957 	}
958 	return 0;
959 unimplemented:
960 	return EXT2_ET_UNIMPLEMENTED;
961 }
962 
ext2fs_open_file(const char * pathname,int flags,mode_t mode)963 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
964 {
965 	flags |= O_BINARY;
966 
967 	if (mode)
968 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
969 		return open64(pathname, flags, mode);
970 	else
971 		return open64(pathname, flags);
972 #else
973 		return open(pathname, flags, mode);
974 	else
975 		return open(pathname, flags);
976 #endif
977 }
978 
ext2fs_stat(const char * path,ext2fs_struct_stat * buf)979 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
980 {
981 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
982 	return stat64(path, buf);
983 #else
984 	return stat(path, buf);
985 #endif
986 }
987 
ext2fs_fstat(int fd,ext2fs_struct_stat * buf)988 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
989 {
990 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
991 	return fstat64(fd, buf);
992 #else
993 	return fstat(fd, buf);
994 #endif
995 }
996 
997 #if __GNUC_PREREQ (4, 6)
998 #pragma GCC diagnostic pop
999 #endif
1000 
1001 static struct struct_io_manager struct_windows_manager = {
1002 	.magic		= EXT2_ET_MAGIC_IO_MANAGER,
1003 	.name		= "Windows I/O Manager",
1004 	.open		= windows_open,
1005 	.close		= windows_close,
1006 	.set_blksize	= windows_set_blksize,
1007 	.read_blk	= windows_read_blk,
1008 	.write_blk	= windows_write_blk,
1009 	.flush		= windows_flush,
1010 	.write_byte	= windows_write_byte,
1011 	.set_option	= windows_set_option,
1012 	.get_stats	= windows_get_stats,
1013 	.read_blk64	= windows_read_blk64,
1014 	.write_blk64	= windows_write_blk64,
1015 	.discard	= windows_discard,
1016 	.cache_readahead	= windows_cache_readahead,
1017 	.zeroout	= windows_zeroout,
1018 };
1019 
1020 io_manager windows_io_manager = &struct_windows_manager;
1021