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
oamlAudioFile(std::string _filename,oamlBase * _base,oamlFileCallbacks * cbs,bool _verbose)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"
OpenFile()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
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
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 }
Open()139 }
140
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 }
Load()156
157 /*
158 * Here are the raw I/O functions
159 */
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
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;
Read32(unsigned int pos)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 */
ReadFloat(unsigned int pos,bool isTail)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 */
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 */
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 */
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 */
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 */
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
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 #elif defined(F_NOCACHE)
503 if (flags & IO_FLAG_DIRECT_IO)
504 io->align = 4096;
505 #endif
506
507 /*
508 * If the device is really a block device, then set the
509 * appropriate flag, otherwise we can set DISCARD_ZEROES flag
510 * because we are going to use punch hole instead of discard
511 * and if it succeed, subsequent read from sparse area returns
512 * zero.
513 */
514 if (ext2fs_fstat(data->dev, &st) == 0) {
515 if (ext2fsP_is_disk_device(st.st_mode))
516 io->flags |= CHANNEL_FLAGS_BLOCK_DEVICE;
517 else
518 io->flags |= CHANNEL_FLAGS_DISCARD_ZEROES;
519 }
520
521 if ((retval = alloc_cache(io, data)))
522 goto cleanup;
523
524 #ifdef BLKROGET
525 if (flags & IO_FLAG_RW) {
526 int error;
527 int readonly = 0;
528
529 /* Is the block device actually writable? */
530 error = ioctl(data->dev, BLKROGET, &readonly);
531 if (!error && readonly) {
532 retval = EPERM;
533 goto cleanup;
534 }
535 }
536 #endif
537
538 *channel = io;
539 return 0;
540
541 cleanup:
542 if (data) {
543 if (data->dev >= 0)
544 close(data->dev);
545 free_cache(data);
546 ext2fs_free_mem(&data);
547 }
548 if (io) {
549 if (io->name) {
550 ext2fs_free_mem(&io->name);
551 }
552 ext2fs_free_mem(&io);
553 }
554 return retval;
555 }
556
557 static DWORD windows_open_device(struct windows_private_data *data, int open_flags)
558 {
559 DWORD ret = 0;
560
561 if (*data->name != '\\')
562 strcpy(data->cf_device, data->name);
563 else if (fake_dos_name_for_device(data))
564 return -1;
565
566 DWORD desired_access = GENERIC_READ | ((open_flags & O_RDWR) ? GENERIC_WRITE : 0);
567 DWORD share_mode = (open_flags & O_EXCL) ? 0 : FILE_SHARE_READ | ((open_flags & O_RDWR) ? FILE_SHARE_WRITE : 0);
568 DWORD flags_and_attributes =
569 #if defined(O_DIRECT)
570 (open_flags & O_DIRECT) ? (FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH) : FILE_ATTRIBUTE_NORMAL;
571 #else
572 FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
573 #endif
574
575 data->handle = CreateFile(data->cf_device, desired_access, share_mode, NULL, OPEN_EXISTING,
576 flags_and_attributes, NULL);
577
578 if (data->handle == INVALID_HANDLE_VALUE) {
579 ret = GetLastError();
580 goto invalid_handle;
581 }
582
583 TRACE("e2fsprogs::windows_open_device::CreateFile(\"%s\") = %p", data->cf_device, data->handle);
584
585 data->dev = _open_osfhandle((intptr_t)data->handle, 0);
586 if (data->dev < 0) {
587 ret = GetLastError() ? GetLastError() : 9999;
588 goto osfhandle_error;
589 }
590
591 TRACE("e2fsprogs::windows_open_device::_open_osfhandle(%p) = %d", data->handle, data->dev);
592
593 return 0;
594
595 osfhandle_error:
596 TRACE("e2fsprogs::windows_open_device::CloseHandle(%p)", data->handle);
597 CloseHandle(data->handle);
598 invalid_handle:
599 remove_fake_dos_name(data);
600 TRACE("e2fsprogs::windows_open_device() = %lu, errno = %d", ret, errno);
601 return ret;
602 }
603
604 static struct windows_private_data *init_private_data(const char *name, int flags)
605 {
606 struct windows_private_data *data = NULL;
607
608 if (ext2fs_get_mem(sizeof(struct windows_private_data), &data))
609 return NULL;
610
611 memset(data, 0, sizeof(struct windows_private_data));
612 strncpy(data->name, name, sizeof(data->name));
613 data->magic = EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL;
614 data->io_stats.num_fields = 2;
615 data->flags = flags;
616 data->handle = INVALID_HANDLE_VALUE;
617
618 return data;
619 }
620
621 static errcode_t windows_open(const char *name, int flags, io_channel *channel)
622 {
623 int fd = -1;
624 int open_flags;
625 struct windows_private_data *data;
626
627 if (name == 0)
628 return EXT2_ET_BAD_DEVICE_NAME;
629
630 data = init_private_data(name, flags);
631 if (!data)
632 return EXT2_ET_NO_MEMORY;
633
634 open_flags = (flags & IO_FLAG_RW) ? O_RDWR : O_RDONLY;
635 if (flags & IO_FLAG_EXCLUSIVE)
636 open_flags |= O_EXCL;
637 #if defined(O_DIRECT)
638 if (flags & IO_FLAG_DIRECT_IO)
639 open_flags |= O_DIRECT;
640 #endif
641
642 if (windows_open_device(data, open_flags)) {
643 ext2fs_free_mem(&data);
644 return EXT2_ET_BAD_DEVICE_NAME;
645 }
646
647 #if defined(F_NOCACHE) && !defined(IO_DIRECT)
648 if (flags & IO_FLAG_DIRECT_IO) {
649 if (fcntl(fd, F_NOCACHE, 1) < 0)
650 return errno;
651 }
652 #endif
653 return windows_open_channel(data, flags, channel, windows_io_manager);
654 }
655
656 static errcode_t windows_close(io_channel channel)
657 {
658 struct windows_private_data *data;
659 errcode_t retval = 0;
660
661 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
662 data = (struct windows_private_data *) channel->private_data;
663 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
664
665 if (--channel->refcount > 0)
666 return 0;
667
668 #ifndef NO_IO_CACHE
669 retval = flush_cached_blocks(channel, data, 0);
670 #endif
671
672 remove_fake_dos_name(data);
673
674 if (_close(data->dev) != 0)
675 retval = errno;
676
677 TRACE("e2fsprogs::windows_close::_close(%d)", data->dev);
678
679 free_cache(data);
680
681 ext2fs_free_mem(&channel->private_data);
682 if (channel->name)
683 ext2fs_free_mem(&channel->name);
684 ext2fs_free_mem(&channel);
685 return retval;
686 }
687
688 static errcode_t windows_set_blksize(io_channel channel, int blksize)
689 {
690 struct windows_private_data *data;
691 errcode_t retval;
692
693 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
694 data = (struct windows_private_data *) channel->private_data;
695 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
696
697 if (channel->block_size != blksize) {
698 #ifndef NO_IO_CACHE
699 if ((retval = flush_cached_blocks(channel, data, 0)))
700 return retval;
701 #endif
702
703 channel->block_size = blksize;
704 free_cache(data);
705 if ((retval = alloc_cache(channel, data)))
706 return retval;
707 }
708 return 0;
709 }
710
711 static errcode_t windows_read_blk64(io_channel channel, unsigned long long block, int count, void *buf)
712 {
713 struct windows_private_data *data;
714 struct windows_cache *cache, *reuse[READ_DIRECT_SIZE];
715 errcode_t retval;
716 char *cp;
717 int i, j;
718
719 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
720 data = (struct windows_private_data *) channel->private_data;
721 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
722
723 #ifdef NO_IO_CACHE
724 return raw_read_blk(channel, data, block, count, buf);
725 #else
726 /*
727 * If we're doing an odd-sized read or a very large read,
728 * flush out the cache and then do a direct read.
729 */
730 if (count < 0 || count > WRITE_DIRECT_SIZE) {
731 if ((retval = flush_cached_blocks(channel, data, 0)))
732 return retval;
733 return raw_read_blk(channel, data, block, count, buf);
734 }
735
736 cp = buf;
737 while (count > 0) {
738 /* If it's in the cache, use it! */
739 if ((cache = find_cached_block(data, block, &reuse[0]))) {
740 #ifdef DEBUG
741 printf("Using cached block %lu\n", block);
742 #endif
743 memcpy(cp, cache->buf, channel->block_size);
744 count--;
745 block++;
746 cp += channel->block_size;
747 continue;
748 }
749 if (count == 1) {
750 /*
751 * Special case where we read directly into the
752 * cache buffer; important in the O_DIRECT case
753 */
754 cache = reuse[0];
755 reuse_cache(channel, data, cache, block);
756 if ((retval = raw_read_blk(channel, data, block, 1,
757 cache->buf))) {
758 cache->in_use = 0;
759 return retval;
760 }
761 memcpy(cp, cache->buf, channel->block_size);
762 return 0;
763 }
764
765 /*
766 * Find the number of uncached blocks so we can do a
767 * single read request
768 */
769 for (i=1; i < count; i++)
770 if (find_cached_block(data, block+i, &reuse[i]))
771 break;
772 #ifdef DEBUG
773 printf("Reading %d blocks starting at %lu\n", i, block);
774 #endif
775 if ((retval = raw_read_blk(channel, data, block, i, cp)))
776 return retval;
777
778 /* Save the results in the cache */
779 for (j=0; j < i; j++) {
780 count--;
781 cache = reuse[j];
782 reuse_cache(channel, data, cache, block++);
783 memcpy(cache->buf, cp, channel->block_size);
784 cp += channel->block_size;
785 }
786 }
787 return 0;
788 #endif /* NO_IO_CACHE */
789 }
790
791 static errcode_t windows_read_blk(io_channel channel, unsigned long block,
792 int count, void *buf)
793 {
794 return windows_read_blk64(channel, block, count, buf);
795 }
796
797 static errcode_t windows_write_blk64(io_channel channel,
798 unsigned long long block,
799 int count, const void *buf)
800 {
801 struct windows_private_data *data;
802 struct windows_cache *cache, *reuse;
803 errcode_t retval = 0;
804 const char *cp;
805 int writethrough;
806
807 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
808 data = (struct windows_private_data *) channel->private_data;
809 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
810
811 #ifdef NO_IO_CACHE
812 return raw_write_blk(channel, data, block, count, buf);
813 #else
814 /*
815 * If we're doing an odd-sized write or a very large write,
816 * flush out the cache completely and then do a direct write.
817 */
818 if (count < 0 || count > WRITE_DIRECT_SIZE) {
819 if ((retval = flush_cached_blocks(channel, data, 1)))
820 return retval;
821 return raw_write_blk(channel, data, block, count, buf);
822 }
823
824 /*
825 * For a moderate-sized multi-block write, first force a write
826 * if we're in write-through cache mode, and then fill the
827 * cache with the blocks.
828 */
829 writethrough = channel->flags & CHANNEL_FLAGS_WRITETHROUGH;
830 if (writethrough)
831 retval = raw_write_blk(channel, data, block, count, buf);
832
833 cp = buf;
834 while (count > 0) {
835 cache = find_cached_block(data, block, &reuse);
836 if (!cache) {
837 cache = reuse;
838 reuse_cache(channel, data, cache, block);
839 }
840 if (cache->buf != cp)
841 memcpy(cache->buf, cp, channel->block_size);
842 cache->dirty = !writethrough;
843 count--;
844 block++;
845 cp += channel->block_size;
846 }
847 return retval;
848 #endif /* NO_IO_CACHE */
849 }
850
851 static errcode_t windows_cache_readahead(io_channel channel,
852 unsigned long long block,
853 unsigned long long count)
854 {
855 return EXT2_ET_OP_NOT_SUPPORTED;
856 }
857
858 static errcode_t windows_write_blk(io_channel channel, unsigned long block,
859 int count, const void *buf)
860 {
861 return windows_write_blk64(channel, block, count, buf);
862 }
863
864 static errcode_t windows_write_byte(io_channel channel, unsigned long offset,
865 int size, const void *buf)
866 {
867 return EXT2_ET_UNIMPLEMENTED;
868 }
869
870 HANDLE windows_get_handle(io_channel channel)
871 {
872 struct windows_private_data *data;
873
874 EXT2_CHECK_MAGIC_RETURN(channel, EXT2_ET_MAGIC_IO_CHANNEL, INVALID_HANDLE_VALUE);
875 data = (struct windows_private_data *) channel->private_data;
876 EXT2_CHECK_MAGIC_RETURN(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL, INVALID_HANDLE_VALUE);
877
878 return data->handle;
879 }
880
881 /*
882 * Flush data buffers to disk.
883 */
884 static errcode_t windows_flush(io_channel channel)
885 {
886 struct windows_private_data *data;
887 errcode_t retval = 0;
888
889 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
890 data = (struct windows_private_data *) channel->private_data;
891 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
892
893 #ifndef NO_IO_CACHE
894 retval = flush_cached_blocks(channel, data, 0);
895 #endif
896
897 return retval;
898 }
899
900 static errcode_t windows_set_option(io_channel channel, const char *option,
901 const char *arg)
902 {
903 struct windows_private_data *data;
904 unsigned long long tmp;
905 char *end;
906
907 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
908 data = (struct windows_private_data *) channel->private_data;
909 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
910
911 if (!strcmp(option, "offset")) {
912 if (!arg)
913 return EXT2_ET_INVALID_ARGUMENT;
914
915 tmp = strtoull(arg, &end, 0);
916 if (*end)
917 return EXT2_ET_INVALID_ARGUMENT;
918 data->offset = tmp;
919 if (data->offset < 0)
920 return EXT2_ET_INVALID_ARGUMENT;
921 return 0;
922 }
923 return EXT2_ET_INVALID_ARGUMENT;
924 }
925
926 static errcode_t windows_discard(io_channel channel, unsigned long long block,
927 unsigned long long count)
928 {
929 TRACE("e2fsprogs::windows_discard::EXT2_ET_UNIMPLEMENTED");
930 return EXT2_ET_UNIMPLEMENTED;
931 }
932
933 /* parameters might not be used if OS doesn't support zeroout */
934 #if __GNUC_PREREQ (4, 6)
935 #pragma GCC diagnostic push
936 #pragma GCC diagnostic ignored "-Wunused-parameter"
937 #endif
938 static errcode_t windows_zeroout(io_channel channel, unsigned long long block,
939 unsigned long long count)
940 {
941 struct windows_private_data *data;
942 int ret;
943
944 EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
945 data = (struct windows_private_data *) channel->private_data;
946 EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_WINDOWS_IO_CHANNEL);
947
948 if (channel->flags & CHANNEL_FLAGS_BLOCK_DEVICE) {
949 /* Not implemented until the BLKZEROOUT mess is fixed */
950 goto unimplemented;
951 } else {
952 /* Regular file, try to use truncate/punch/zero. */
953 struct stat statbuf;
954
955 if (count == 0)
956 return 0;
957 /*
958 * If we're trying to zero a range past the end of the file,
959 * extend the file size, then truncate everything.
960 */
961 ret = fstat(data->dev, &statbuf);
962 if (ret)
963 goto err;
964 if ((unsigned long long) statbuf.st_size <
965 (block + count) * channel->block_size + data->offset) {
966 ret = ftruncate(data->dev,
967 (block + count) * channel->block_size + data->offset);
968 if (ret)
969 goto err;
970 }
971 goto unimplemented;
972 }
973 err:
974 if (ret < 0) {
975 if (errno == EOPNOTSUPP)
976 goto unimplemented;
977 return errno;
978 }
979 return 0;
980 unimplemented:
981 return EXT2_ET_UNIMPLEMENTED;
982 }
983
984 int ext2fs_open_file(const char *pathname, int flags, mode_t mode)
985 {
986 flags |= O_BINARY;
987
988 if (mode)
989 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
990 return open64(pathname, flags, mode);
991 else
992 return open64(pathname, flags);
993 #else
994 return open(pathname, flags, mode);
995 else
996 return open(pathname, flags);
997 #endif
998 }
999
1000 int ext2fs_stat(const char *path, ext2fs_struct_stat *buf)
1001 {
1002 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
1003 return stat64(path, buf);
1004 #else
1005 return stat(path, buf);
1006 #endif
1007 }
1008
1009 int ext2fs_fstat(int fd, ext2fs_struct_stat *buf)
1010 {
1011 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
1012 return fstat64(fd, buf);
1013 #else
1014 return fstat(fd, buf);
1015 #endif
1016 }
1017
1018 #if __GNUC_PREREQ (4, 6)
1019 #pragma GCC diagnostic pop
1020 #endif
1021
1022 static struct struct_io_manager struct_windows_manager = {
1023 .magic = EXT2_ET_MAGIC_IO_MANAGER,
1024 .name = "Windows I/O Manager",
1025 .open = windows_open,
1026 .close = windows_close,
1027 .set_blksize = windows_set_blksize,
1028 .read_blk = windows_read_blk,
1029 .write_blk = windows_write_blk,
1030 .flush = windows_flush,
1031 .write_byte = windows_write_byte,
1032 .set_option = windows_set_option,
1033 .get_stats = windows_get_stats,
1034 .read_blk64 = windows_read_blk64,
1035 .write_blk64 = windows_write_blk64,
1036 .discard = windows_discard,
1037 .cache_readahead = windows_cache_readahead,
1038 .zeroout = windows_zeroout,
1039 };
1040
1041 io_manager windows_io_manager = &struct_windows_manager;
1042