forked from Unidata/netcdf-c
/
posixio.c
1909 lines (1621 loc) · 43.9 KB
/
posixio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 1996, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
/* $Id: posixio.c,v 1.89 2010/05/22 21:59:08 dmh Exp $ */
/* For MinGW Build */
#include <config.h>
#include <stdio.h>
/* Windows platforms, including MinGW, Cygwin, Visual Studio */
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <winbase.h>
#include <io.h>
#else
#include <unistd.h>
#endif
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#ifndef NC_NOERR
#define NC_NOERR 0
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#ifndef HAVE_SSIZE_T
typedef int ssize_t;
#endif
#ifndef SEEK_SET
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#endif
#include "ncio.h"
#include "fbits.h"
#include "rnd.h"
/* #define INSTRUMENT 1 */
#if INSTRUMENT /* debugging */
#undef NDEBUG
#include <stdio.h>
#include "instr.h"
#endif
#undef MIN /* system may define MIN somewhere and complain */
#define MIN(mm,nn) (((mm) < (nn)) ? (mm) : (nn))
#if !defined(NDEBUG) && !defined(X_INT_MAX)
#define X_INT_MAX 2147483647
#endif
#if 0 /* !defined(NDEBUG) && !defined(X_ALIGN) */
#define X_ALIGN 4
#else
#undef X_ALIGN
#endif
/* These are needed on mingw to get a dll to compile. They really
* should be provided in sys/stats.h, but what the heck. Let's not be
* too picky! */
#ifndef S_IRGRP
#define S_IRGRP 0000040
#endif
#ifndef S_IROTH
#define S_IROTH 0000004
#endif
#ifndef S_IWGRP
#define S_IWGRP 0000020
#endif
#ifndef S_IWOTH
#define S_IWOTH 0000002
#endif
/*Forward*/
static int ncio_px_filesize(ncio *nciop, off_t *filesizep);
static int ncio_px_pad_length(ncio *nciop, off_t length);
static int ncio_px_close(ncio *nciop, int doUnlink);
static int ncio_spx_close(ncio *nciop, int doUnlink);
/*
* Define the following for debugging.
*/
/* #define ALWAYS_NC_SHARE 1 */
/* Begin OS */
#ifndef POSIXIO_DEFAULT_PAGESIZE
#define POSIXIO_DEFAULT_PAGESIZE 4096
#endif
/*! Cross-platform file length.
*
* Some versions of Visual Studio are throwing errno 132
* when fstat is used on large files. This function is
* an attempt to get around that.
*
* @par fd File Descriptor.
* @return -1 on error, length of file (in bytes) otherwise.
*/
static size_t nc_get_filelen(const int fd) {
size_t flen;
#ifdef HAVE_FILE_LENGTH_I64
__int64 file_len = 0;
if ((file_len = _filelengthi64(fd)) < 0) {
return file_len;
}
flen = (size_t)file_len;
#else
int res = 0;
struct stat sb;
if((res = fstat(fd,&sb)) <0)
return res;
flen = sb.st_size;
#endif
return flen;
}
/*
* What is the system pagesize?
*/
static size_t
pagesize(void)
{
size_t pgsz;
#if defined(_WIN32) || defined(_WIN64)
SYSTEM_INFO info;
#endif
/* Hmm, aren't standards great? */
#if defined(_SC_PAGE_SIZE) && !defined(_SC_PAGESIZE)
#define _SC_PAGESIZE _SC_PAGE_SIZE
#endif
/* For MinGW Builds */
#if defined(_WIN32) || defined(_WIN64)
GetSystemInfo(&info);
pgsz = (size_t)info.dwPageSize;
#elif defined(_SC_PAGESIZE)
pgsz = (size_t)sysconf(_SC_PAGESIZE);
#elif defined(HAVE_GETPAGESIZE)
pgsz = (size_t) getpagesize();
#endif
if(pgsz > 0)
return (size_t) pgsz;
return (size_t)POSIXIO_DEFAULT_PAGESIZE;
}
/*
* What is the preferred I/O block size?
*/
static size_t
blksize(int fd)
{
#if defined(HAVE_ST_BLKSIZE)
struct stat sb;
if (fstat(fd, &sb) > -1)
{
if(sb.st_blksize >= 8192)
return (size_t) sb.st_blksize;
return 8192;
}
/* else, silent in the face of error */
#endif
return (size_t) 2 * pagesize();
}
/*
* Sortof like ftruncate, except won't make the
* file shorter.
*/
static int
fgrow(const int fd, const off_t len)
{
struct stat sb;
if (fstat(fd, &sb) < 0)
return errno;
if (len < sb.st_size)
return NC_NOERR;
{
const long dumb = 0;
/* we don't use ftruncate() due to problem with FAT32 file systems */
/* cache current position */
const off_t pos = lseek(fd, 0, SEEK_CUR);
if(pos < 0)
return errno;
if (lseek(fd, len-sizeof(dumb), SEEK_SET) < 0)
return errno;
if(write(fd, &dumb, sizeof(dumb)) < 0)
return errno;
if (lseek(fd, pos, SEEK_SET) < 0)
return errno;
}
return NC_NOERR;
}
/*
* Sortof like ftruncate, except won't make the file shorter. Differs
* from fgrow by only writing one byte at designated seek position, if
* needed.
*/
static int
fgrow2(const int fd, const off_t len)
{
/* There is a problem with fstat on Windows based systems
which manifests (so far) when Config RELEASE is built.
Use _filelengthi64 isntead.
See https://github.com/Unidata/netcdf-c/issues/188
*/
size_t file_len = nc_get_filelen(fd);
if(file_len < 0) return errno;
if(len <= file_len)
return NC_NOERR;
{
const char dumb = 0;
/* we don't use ftruncate() due to problem with FAT32 file systems */
/* cache current position */
const off_t pos = lseek(fd, 0, SEEK_CUR);
if(pos < 0)
return errno;
if (lseek(fd, len-1, SEEK_SET) < 0)
return errno;
if(write(fd, &dumb, sizeof(dumb)) < 0)
return errno;
if (lseek(fd, pos, SEEK_SET) < 0)
return errno;
}
return NC_NOERR;
}
/* End OS */
/* Begin px */
/* The px_ functions are for posix systems, when NC_SHARE is not in
effect. */
/* Write out a "page" of data to the file. The size of the page
(i.e. the extent) varies.
nciop - pointer to the file metadata.
offset - where in the file should this page be written.
extent - how many bytes should be written.
vp - pointer to the data to write.
posp - pointer to current position in file, updated after write.
*/
static int
px_pgout(ncio *const nciop,
off_t const offset, const size_t extent,
void *const vp, off_t *posp)
{
ssize_t partial;
size_t nextent;
char *nvp;
#ifdef X_ALIGN
assert(offset % X_ALIGN == 0);
#endif
assert(*posp == OFF_NONE || *posp == lseek(nciop->fd, 0, SEEK_CUR));
if(*posp != offset)
{
if(lseek(nciop->fd, offset, SEEK_SET) != offset)
{
return errno;
}
*posp = offset;
}
/* Old write, didn't handle partial writes correctly */
/* if(write(nciop->fd, vp, extent) != (ssize_t) extent) */
/* { */
/* return errno; */
/* } */
nextent = extent;
nvp = vp;
while((partial = write(nciop->fd, nvp, nextent)) != -1) {
if(partial == nextent)
break;
nvp += partial;
nextent -= partial;
}
if(partial == -1)
return errno;
*posp += extent;
return NC_NOERR;
}
/*! Read in a page of data.
@param[in] nciop A pointer to the ncio struct for this file.
@param[in] offset The byte offset in file where read starts.
@param[in] extent The size of the page that will be read.
@param[in] vp A pointer to where the data will end up.
@param[in,out] nreadp Returned number of bytes actually read (may be less than extent).
@param[in,out] posp The pointer to current position in file, updated after read.
@return Return 0 on success, otherwise an error code.
*/
static int
px_pgin(ncio *const nciop,
off_t const offset, const size_t extent,
void *const vp, size_t *nreadp, off_t *posp)
{
int status;
ssize_t nread;
size_t read_count = 0;
ssize_t bytes_xfered = 0;
void *p = vp;
#ifdef X_ALIGN
assert(offset % X_ALIGN == 0);
assert(extent % X_ALIGN == 0);
#endif
/* *posp == OFF_NONE (-1) on first call. This
is problematic because lseek also returns -1
on error. Use errno instead. */
if(*posp != OFF_NONE && *posp != lseek(nciop->fd, 0, SEEK_CUR)) {
if(errno) {
status = errno;
printf("Error %d: %s\n",errno,strerror(errno));
return status;
}
}
if(*posp != offset)
{
if(lseek(nciop->fd, offset, SEEK_SET) != offset)
{
status = errno;
return status;
}
*posp = offset;
}
errno = 0;
/* Handle the case where the read is interrupted
by a signal (see NCF-337,
http://pubs.opengroup.org/onlinepubs/009695399/functions/read.html)
When this happens, nread will (should) be the bytes read, and
errno will be set to EINTR. On older systems nread might be -1.
If this is the case, there's not a whole lot we can do about it
as we can't compute any offsets, so we will attempt to read again.
This *feels* like it could lead to an infinite loop, but it shouldn't
unless the read is being constantly interrupted by a signal, and is
on an older system which returns -1 instead of bytexs read.
The case where it's a short read is already handled by the function
(according to the comment below, at least). */
do {
nread = read(nciop->fd,vp,extent);
} while (nread == -1 && errno == EINTR);
if(nread != (ssize_t)extent) {
status = errno;
if( nread == -1 || (status != EINTR && status != NC_NOERR))
return status;
/* else it's okay we read less than asked for */
(void) memset((char *)vp + nread, 0, (ssize_t)extent - nread);
}
*nreadp = nread;
*posp += nread;
return NC_NOERR;
}
/* This struct is for POSIX systems, with NC_SHARE not in effect. If
NC_SHARE is used, see ncio_spx.
blksz - block size for reads and writes to file.
pos - current read/write position in file.
bf_offset - file offset corresponding to start of memory buffer
bf_extent - number of bytes in I/O request
bf_cnt - number of bytes available in buffer
bf_base - pointer to beginning of buffer.
bf_rflags - buffer region flags (defined in ncio.h) tell the lock
status, read/write permissions, and modification status of regions
of data in the buffer.
bf_refcount - buffer reference count.
slave - used in moves.
*/
typedef struct ncio_px {
size_t blksz;
off_t pos;
/* buffer */
off_t bf_offset;
size_t bf_extent;
size_t bf_cnt;
void *bf_base;
int bf_rflags;
int bf_refcount;
/* chain for double buffering in px_move */
struct ncio_px *slave;
} ncio_px;
/*ARGSUSED*/
/* This function indicates the file region starting at offset may be
released.
This is for POSIX, without NC_SHARE. If called with RGN_MODIFIED
flag, sets the modified flag in pxp->bf_rflags and decrements the
reference count.
pxp - pointer to posix non-share ncio_px struct.
offset - file offset for beginning of to region to be
released.
rflags - only RGN_MODIFIED is relevant to this function, others ignored
*/
static int
px_rel(ncio_px *const pxp, off_t offset, int rflags)
{
assert(pxp->bf_offset <= offset
&& offset < pxp->bf_offset + (off_t) pxp->bf_extent);
assert(pIf(fIsSet(rflags, RGN_MODIFIED),
fIsSet(pxp->bf_rflags, RGN_WRITE)));
if(fIsSet(rflags, RGN_MODIFIED))
{
fSet(pxp->bf_rflags, RGN_MODIFIED);
}
pxp->bf_refcount--;
return NC_NOERR;
}
/* This function indicates the file region starting at offset may be
released. Each read or write to the file is bracketed by a call to
the "get" region function and a call to the "rel" region function.
If you only read from the memory region, release it with a flag of
0, if you modify the region, release it with a flag of
RGN_MODIFIED.
For POSIX system, without NC_SHARE, this becomes the rel function
pointed to by the ncio rel function pointer. It mearly checks for
file write permission, then calls px_rel to do everything.
nciop - pointer to ncio struct.
offset - num bytes from beginning of buffer to region to be
released.
rflags - only RGN_MODIFIED is relevant to this function, others ignored
*/
static int
ncio_px_rel(ncio *const nciop, off_t offset, int rflags)
{
ncio_px *const pxp = (ncio_px *)nciop->pvt;
if(fIsSet(rflags, RGN_MODIFIED) && !fIsSet(nciop->ioflags, NC_WRITE))
return EPERM; /* attempt to write readonly file */
return px_rel(pxp, offset, rflags);
}
/* POSIX get. This will "make a region available." Since we're using
buffered IO, this means that if needed, we'll fetch a new page from
the file, otherwise, just return a pointer to what's in memory
already.
nciop - pointer to ncio struct, containing file info.
pxp - pointer to ncio_px struct, which contains special metadate
for posix files without NC_SHARE.
offset - start byte of region to get.
extent - how many bytes to read.
rflags - One of the RGN_* flags defined in ncio.h.
vpp - pointer to pointer that will receive data.
NOTES:
* For blkoffset round offset down to the nearest pxp->blksz. This
provides the offset (in bytes) to the beginning of the block that
holds the current offset.
* diff tells how far into the current block we are.
* For blkextent round up to the number of bytes at the beginning of
the next block, after the one that holds our current position, plus
whatever extra (i.e. the extent) that we are about to grab.
* The blkextent can't be more than twice the pxp->blksz. That's
because the pxp->blksize is the sizehint, and in ncio_px_init2 the
buffer (pointed to by pxp->bf-base) is allocated with 2 *
*sizehintp. This is checked (unneccesarily) more than once in
asserts.
* If this is called on a newly opened file, pxp->bf_offset will be
OFF_NONE and we'll jump to label pgin to immediately read in a
page.
*/
static int
px_get(ncio *const nciop, ncio_px *const pxp,
off_t offset, size_t extent,
int rflags,
void **const vpp)
{
int status = NC_NOERR;
const off_t blkoffset = _RNDDOWN(offset, (off_t)pxp->blksz);
off_t diff = (size_t)(offset - blkoffset);
off_t blkextent = _RNDUP(diff + extent, pxp->blksz);
assert(extent != 0);
assert(extent < X_INT_MAX); /* sanity check */
assert(offset >= 0); /* sanity check */
if(2 * pxp->blksz < blkextent)
return E2BIG; /* TODO: temporary kludge */
if(pxp->bf_offset == OFF_NONE)
{
/* Uninitialized */
if(pxp->bf_base == NULL)
{
assert(pxp->bf_extent == 0);
assert(blkextent <= 2 * pxp->blksz);
pxp->bf_base = malloc(2 * pxp->blksz);
if(pxp->bf_base == NULL)
return ENOMEM;
}
goto pgin;
}
/* else */
assert(blkextent <= 2 * pxp->blksz);
if(blkoffset == pxp->bf_offset)
{
/* hit */
if(blkextent > pxp->bf_extent)
{
/* page in upper */
void *const middle =
(void *)((char *)pxp->bf_base + pxp->blksz);
assert(pxp->bf_extent == pxp->blksz);
status = px_pgin(nciop,
pxp->bf_offset + (off_t)pxp->blksz,
pxp->blksz,
middle,
&pxp->bf_cnt,
&pxp->pos);
if(status != NC_NOERR)
return status;
pxp->bf_extent = 2 * pxp->blksz;
pxp->bf_cnt += pxp->blksz;
}
goto done;
}
/* else */
if(pxp->bf_extent > pxp->blksz
&& blkoffset == pxp->bf_offset + (off_t)pxp->blksz)
{
/* hit in upper half */
if(blkextent == pxp->blksz)
{
/* all in upper half, no fault needed */
diff += pxp->blksz;
goto done;
}
/* else */
if(pxp->bf_cnt > pxp->blksz)
{
/* data in upper half */
void *const middle =
(void *)((char *)pxp->bf_base + pxp->blksz);
assert(pxp->bf_extent == 2 * pxp->blksz);
if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
{
/* page out lower half */
assert(pxp->bf_refcount <= 0);
status = px_pgout(nciop,
pxp->bf_offset,
pxp->blksz,
pxp->bf_base,
&pxp->pos);
if(status != NC_NOERR)
return status;
}
pxp->bf_cnt -= pxp->blksz;
/* copy upper half into lower half */
(void) memcpy(pxp->bf_base, middle, pxp->bf_cnt);
}
else /* added to fix nofill bug */
{
assert(pxp->bf_extent == 2 * pxp->blksz);
/* still have to page out lower half, if modified */
if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
{
assert(pxp->bf_refcount <= 0);
status = px_pgout(nciop,
pxp->bf_offset,
pxp->blksz,
pxp->bf_base,
&pxp->pos);
if(status != NC_NOERR)
return status;
}
}
pxp->bf_offset = blkoffset;
/* pxp->bf_extent = pxp->blksz; */
assert(blkextent == 2 * pxp->blksz);
{
/* page in upper */
void *const middle =
(void *)((char *)pxp->bf_base + pxp->blksz);
status = px_pgin(nciop,
pxp->bf_offset + (off_t)pxp->blksz,
pxp->blksz,
middle,
&pxp->bf_cnt,
&pxp->pos);
if(status != NC_NOERR)
return status;
pxp->bf_extent = 2 * pxp->blksz;
pxp->bf_cnt += pxp->blksz;
}
goto done;
}
/* else */
if(blkoffset == pxp->bf_offset - (off_t)pxp->blksz)
{
/* wants the page below */
void *const middle =
(void *)((char *)pxp->bf_base + pxp->blksz);
size_t upper_cnt = 0;
if(pxp->bf_cnt > pxp->blksz)
{
/* data in upper half */
assert(pxp->bf_extent == 2 * pxp->blksz);
if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
{
/* page out upper half */
assert(pxp->bf_refcount <= 0);
status = px_pgout(nciop,
pxp->bf_offset + (off_t)pxp->blksz,
pxp->bf_cnt - pxp->blksz,
middle,
&pxp->pos);
if(status != NC_NOERR)
return status;
}
pxp->bf_cnt = pxp->blksz;
pxp->bf_extent = pxp->blksz;
}
if(pxp->bf_cnt > 0)
{
/* copy lower half into upper half */
(void) memcpy(middle, pxp->bf_base, pxp->blksz);
upper_cnt = pxp->bf_cnt;
}
/* read page below into lower half */
status = px_pgin(nciop,
blkoffset,
pxp->blksz,
pxp->bf_base,
&pxp->bf_cnt,
&pxp->pos);
if(status != NC_NOERR)
return status;
pxp->bf_offset = blkoffset;
if(upper_cnt != 0)
{
pxp->bf_extent = 2 * pxp->blksz;
pxp->bf_cnt = pxp->blksz + upper_cnt;
}
else
{
pxp->bf_extent = pxp->blksz;
}
goto done;
}
/* else */
/* no overlap */
if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
{
assert(pxp->bf_refcount <= 0);
status = px_pgout(nciop,
pxp->bf_offset,
pxp->bf_cnt,
pxp->bf_base,
&pxp->pos);
if(status != NC_NOERR)
return status;
pxp->bf_rflags = 0;
}
pgin:
status = px_pgin(nciop,
blkoffset,
blkextent,
pxp->bf_base,
&pxp->bf_cnt,
&pxp->pos);
if(status != NC_NOERR)
return status;
pxp->bf_offset = blkoffset;
pxp->bf_extent = blkextent;
done:
extent += diff;
if(pxp->bf_cnt < extent)
pxp->bf_cnt = extent;
assert(pxp->bf_cnt <= pxp->bf_extent);
pxp->bf_rflags |= rflags;
pxp->bf_refcount++;
#ifndef __CHAR_UNSIGNED__
*vpp = (void *)((char *)pxp->bf_base + diff);
#else
*vpp = (void *)((signed char*)pxp->bf_base + diff);
#endif
return NC_NOERR;
}
/* Request that the region (offset, extent) be made available through
*vpp.
This function converts a file region specified by an offset and
extent to a memory pointer. The region may be locked until the
corresponding call to rel().
For POSIX systems, without NC_SHARE. This function gets a page of
size extent?
This is a wrapper for the function px_get, which does all the heavy
lifting.
nciop - pointer to ncio struct for this file.
offset - offset (from beginning of file?) to the data we want to
read.
extent - the number of bytes to read from the file.
rflags - One of the RGN_* flags defined in ncio.h.
vpp - handle to point at data when it's been read.
*/
static int
ncio_px_get(ncio *const nciop,
off_t offset, size_t extent,
int rflags,
void **const vpp)
{
ncio_px *const pxp = (ncio_px *)nciop->pvt;
if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE))
return EPERM; /* attempt to write readonly file */
/* reclaim space used in move */
if(pxp->slave != NULL)
{
if(pxp->slave->bf_base != NULL)
{
free(pxp->slave->bf_base);
pxp->slave->bf_base = NULL;
pxp->slave->bf_extent = 0;
pxp->slave->bf_offset = OFF_NONE;
}
free(pxp->slave);
pxp->slave = NULL;
}
return px_get(nciop, pxp, offset, extent, rflags, vpp);
}
/* ARGSUSED */
static int
px_double_buffer(ncio *const nciop, off_t to, off_t from,
size_t nbytes, int rflags)
{
ncio_px *const pxp = (ncio_px *)nciop->pvt;
int status = NC_NOERR;
void *src;
void *dest;
#if INSTRUMENT
fprintf(stderr, "\tdouble_buffr %ld %ld %ld\n",
(long)to, (long)from, (long)nbytes);
#endif
status = px_get(nciop, pxp, to, nbytes, RGN_WRITE,
&dest);
if(status != NC_NOERR)
return status;
if(pxp->slave == NULL)
{
pxp->slave = (ncio_px *) malloc(sizeof(ncio_px));
if(pxp->slave == NULL)
return ENOMEM;
pxp->slave->blksz = pxp->blksz;
/* pos done below */
pxp->slave->bf_offset = pxp->bf_offset;
pxp->slave->bf_extent = pxp->bf_extent;
pxp->slave->bf_cnt = pxp->bf_cnt;
pxp->slave->bf_base = malloc(2 * pxp->blksz);
if(pxp->slave->bf_base == NULL)
return ENOMEM;
(void) memcpy(pxp->slave->bf_base, pxp->bf_base,
pxp->bf_extent);
pxp->slave->bf_rflags = 0;
pxp->slave->bf_refcount = 0;
pxp->slave->slave = NULL;
}
pxp->slave->pos = pxp->pos;
status = px_get(nciop, pxp->slave, from, nbytes, 0,
&src);
if(status != NC_NOERR)
return status;
if(pxp->pos != pxp->slave->pos)
{
/* position changed, sync */
pxp->pos = pxp->slave->pos;
}
(void) memcpy(dest, src, nbytes);
(void)px_rel(pxp->slave, from, 0);
(void)px_rel(pxp, to, RGN_MODIFIED);
return status;
}
/* Like memmove(), safely move possibly overlapping data.
Copy one region to another without making anything available to
higher layers. May be just implemented in terms of get() and rel(),
or may be tricky to be efficient. Only used in by nc_enddef()
after redefinition.
nciop - pointer to ncio struct with file info.
to - src for move?
from - dest for move?
nbytes - number of bytes to move.
rflags - One of the RGN_* flags defined in ncio.h. The only
reasonable flag value is RGN_NOLOCK.
*/
static int
ncio_px_move(ncio *const nciop, off_t to, off_t from,
size_t nbytes, int rflags)
{
ncio_px *const pxp = (ncio_px *)nciop->pvt;
int status = NC_NOERR;
off_t lower;
off_t upper;
char *base;
size_t diff;
size_t extent;
if(to == from)
return NC_NOERR; /* NOOP */
if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE))
return EPERM; /* attempt to write readonly file */
rflags &= RGN_NOLOCK; /* filter unwanted flags */
if(to > from)
{
/* growing */
lower = from;
upper = to;
}
else
{
/* shrinking */
lower = to;
upper = from;
}
diff = (size_t)(upper - lower);
extent = diff + nbytes;
#if INSTRUMENT
fprintf(stderr, "ncio_px_move %ld %ld %ld %ld %ld\n",
(long)to, (long)from, (long)nbytes, (long)lower, (long)extent);
#endif
if(extent > pxp->blksz)
{
size_t remaining = nbytes;
if(to > from)
{
off_t frm = from + nbytes;
off_t toh = to + nbytes;
for(;;)
{
size_t loopextent = MIN(remaining, pxp->blksz);
frm -= loopextent;
toh -= loopextent;
status = px_double_buffer(nciop, toh, frm,
loopextent, rflags) ;
if(status != NC_NOERR)
return status;
remaining -= loopextent;
if(remaining == 0)
break; /* normal loop exit */
}
}
else
{
for(;;)
{
size_t loopextent = MIN(remaining, pxp->blksz);
status = px_double_buffer(nciop, to, from,
loopextent, rflags) ;
if(status != NC_NOERR)
return status;
remaining -= loopextent;
if(remaining == 0)
break; /* normal loop exit */
to += loopextent;
from += loopextent;
}
}
return NC_NOERR;
}
#if INSTRUMENT
fprintf(stderr, "\tncio_px_move small\n");
#endif
status = px_get(nciop, pxp, lower, extent, RGN_WRITE|rflags,
(void **)&base);
if(status != NC_NOERR)
return status;
if(to > from)
(void) memmove(base + diff, base, nbytes);
else
(void) memmove(base, base + diff, nbytes);
(void) px_rel(pxp, lower, RGN_MODIFIED);
return status;
}
/* Flush any buffers to disk. May be a no-op on if I/O is unbuffered.
This function is used when NC_SHARE is NOT used.
*/
static int
ncio_px_sync(ncio *const nciop)
{
ncio_px *const pxp = (ncio_px *)nciop->pvt;
int status = NC_NOERR;
if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
{
assert(pxp->bf_refcount <= 0);
status = px_pgout(nciop, pxp->bf_offset,
pxp->bf_cnt,
pxp->bf_base, &pxp->pos);
if(status != NC_NOERR)
return status;
pxp->bf_rflags = 0;
}
else if (!fIsSet(pxp->bf_rflags, RGN_WRITE))
{
/*
* The dataset is readonly. Invalidate the buffers so
* that the next ncio_px_get() will actually read data.
*/
pxp->bf_offset = OFF_NONE;
pxp->bf_cnt = 0;
}
return status;
}
/* Internal function called at close to
free up anything hanging off pvt.