posixio.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  1. /*
  2. * Copyright 1996, University Corporation for Atmospheric Research
  3. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  4. */
  5. /* $Id: posixio.c,v 1.89 2010/05/22 21:59:08 dmh Exp $ */
  6. /* For MinGW Build */
  7. #if defined(_WIN32) || defined(_WIN64)
  8. #include <windows.h>
  9. #include <winbase.h>
  10. #include <io.h>
  11. #define fstat64 fstat
  12. #define lseek64 lseek
  13. #endif
  14. #include <config.h>
  15. #include <assert.h>
  16. #include <stdlib.h>
  17. #include <errno.h>
  18. #ifndef ENOERR
  19. #define ENOERR 0
  20. #endif
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #include <string.h>
  25. #ifdef _MSC_VER /* Microsoft Compilers */
  26. #include <io.h>
  27. #else
  28. #include <unistd.h>
  29. #endif
  30. #ifndef HAVE_SSIZE_T
  31. #define ssize_t int
  32. #endif
  33. #ifndef SEEK_SET
  34. #define SEEK_SET 0
  35. #define SEEK_CUR 1
  36. #define SEEK_END 2
  37. #endif
  38. #include "ncio.h"
  39. #include "fbits.h"
  40. #include "rnd.h"
  41. /* #define INSTRUMENT 1 */
  42. #if INSTRUMENT /* debugging */
  43. #undef NDEBUG
  44. #include <stdio.h>
  45. /*#include "instr.h"*/
  46. #endif
  47. #undef MIN /* system may define MIN somewhere and complain */
  48. #define MIN(mm,nn) (((mm) < (nn)) ? (mm) : (nn))
  49. #if !defined(NDEBUG) && !defined(X_INT_MAX)
  50. #define X_INT_MAX 2147483647
  51. #endif
  52. #if 0 /* !defined(NDEBUG) && !defined(X_ALIGN) */
  53. #define X_ALIGN 4
  54. #else
  55. #undef X_ALIGN
  56. #endif
  57. /* These are needed on mingw to get a dll to compile. They really
  58. * should be provided in sys/stats.h, but what the heck. Let's not be
  59. * too picky! */
  60. #ifndef S_IRGRP
  61. #define S_IRGRP 0000040
  62. #endif
  63. #ifndef S_IROTH
  64. #define S_IROTH 0000004
  65. #endif
  66. #ifndef S_IWGRP
  67. #define S_IWGRP 0000020
  68. #endif
  69. #ifndef S_IWOTH
  70. #define S_IWOTH 0000002
  71. #endif
  72. /*Forward*/
  73. static int ncio_px_filesize(ncio *nciop, off_t *filesizep);
  74. static int ncio_px_pad_length(ncio *nciop, off_t length);
  75. static int ncio_px_close(ncio *nciop, int doUnlink);
  76. static int ncio_spx_close(ncio *nciop, int doUnlink);
  77. /*
  78. * Define the following for debugging.
  79. */
  80. /* #define ALWAYS_NC_SHARE 1 */
  81. /* Begin OS */
  82. #ifndef POSIXIO_DEFAULT_PAGESIZE
  83. #define POSIXIO_DEFAULT_PAGESIZE 4096
  84. #endif
  85. /*
  86. * What is the system pagesize?
  87. */
  88. static size_t
  89. pagesize(void)
  90. {
  91. size_t pgsz;
  92. #if defined(_WIN32) || defined(_WIN64)
  93. SYSTEM_INFO info;
  94. #endif
  95. /* Hmm, aren't standards great? */
  96. #if defined(_SC_PAGE_SIZE) && !defined(_SC_PAGESIZE)
  97. #define _SC_PAGESIZE _SC_PAGE_SIZE
  98. #endif
  99. /* For MinGW Builds */
  100. #if defined(_WIN32) || defined(_WIN64)
  101. GetSystemInfo(&info);
  102. pgsz = (size_t)info.dwPageSize;
  103. #elif defined(_SC_PAGESIZE)
  104. pgsz = (size_t)sysconf(_SC_PAGESIZE);
  105. #elif defined(HAVE_GETPAGESIZE)
  106. pgsz = (size_t) getpagesize();
  107. #endif
  108. if(pgsz > 0)
  109. return (size_t) pgsz;
  110. return (size_t)POSIXIO_DEFAULT_PAGESIZE;
  111. }
  112. /*
  113. * What is the preferred I/O block size?
  114. */
  115. static size_t
  116. blksize(int fd)
  117. {
  118. #if defined(HAVE_ST_BLKSIZE)
  119. struct stat sb;
  120. if (fstat(fd, &sb) > -1)
  121. {
  122. if(sb.st_blksize >= 8192)
  123. return (size_t) sb.st_blksize;
  124. return 8192;
  125. }
  126. /* else, silent in the face of error */
  127. #endif
  128. return (size_t) 2 * pagesize();
  129. }
  130. /*
  131. * Sortof like ftruncate, except won't make the
  132. * file shorter.
  133. */
  134. static int
  135. fgrow(const int fd, const off_t len)
  136. {
  137. struct stat sb;
  138. if (fstat(fd, &sb) < 0)
  139. return errno;
  140. if (len < sb.st_size)
  141. return ENOERR;
  142. {
  143. const long dumb = 0;
  144. /* we don't use ftruncate() due to problem with FAT32 file systems */
  145. /* cache current position */
  146. const off_t pos = lseek(fd, 0, SEEK_CUR);
  147. if(pos < 0)
  148. return errno;
  149. if (lseek(fd, len-sizeof(dumb), SEEK_SET) < 0)
  150. return errno;
  151. if(write(fd, &dumb, sizeof(dumb)) < 0)
  152. return errno;
  153. if (lseek(fd, pos, SEEK_SET) < 0)
  154. return errno;
  155. }
  156. return ENOERR;
  157. }
  158. /*
  159. * Sortof like ftruncate, except won't make the file shorter. Differs
  160. * from fgrow by only writing one byte at designated seek position, if
  161. * needed.
  162. */
  163. static int
  164. fgrow2(const int fd, const off_t len)
  165. {
  166. struct stat sb;
  167. if (fstat(fd, &sb) < 0)
  168. return errno;
  169. if (len <= sb.st_size)
  170. return ENOERR;
  171. {
  172. const char dumb = 0;
  173. /* we don't use ftruncate() due to problem with FAT32 file systems */
  174. /* cache current position */
  175. const off_t pos = lseek(fd, 0, SEEK_CUR);
  176. if(pos < 0)
  177. return errno;
  178. if (lseek(fd, len-1, SEEK_SET) < 0)
  179. return errno;
  180. if(write(fd, &dumb, sizeof(dumb)) < 0)
  181. return errno;
  182. if (lseek(fd, pos, SEEK_SET) < 0)
  183. return errno;
  184. }
  185. return ENOERR;
  186. }
  187. /* End OS */
  188. /* Begin px */
  189. /* The px_ functions are for posix systems, when NC_SHARE is not in
  190. effect. */
  191. /* Write out a "page" of data to the file. The size of the page
  192. (i.e. the extent) varies.
  193. nciop - pointer to the file metadata.
  194. offset - where in the file should this page be written.
  195. extent - how many bytes should be written.
  196. vp - pointer to the data to write.
  197. posp - pointer to current position in file, updated after write.
  198. */
  199. static int
  200. px_pgout(ncio *const nciop,
  201. off_t const offset, const size_t extent,
  202. void *const vp, off_t *posp)
  203. {
  204. ssize_t partial;
  205. size_t nextent;
  206. char *nvp;
  207. #ifdef X_ALIGN
  208. assert(offset % X_ALIGN == 0);
  209. #endif
  210. assert(*posp == OFF_NONE || *posp == lseek(nciop->fd, 0, SEEK_CUR));
  211. if(*posp != offset)
  212. {
  213. if(lseek(nciop->fd, offset, SEEK_SET) != offset)
  214. {
  215. return errno;
  216. }
  217. *posp = offset;
  218. }
  219. /* Old write, didn't handle partial writes correctly */
  220. /* if(write(nciop->fd, vp, extent) != (ssize_t) extent) */
  221. /* { */
  222. /* return errno; */
  223. /* } */
  224. nextent = extent;
  225. nvp = vp;
  226. while((partial = write(nciop->fd, nvp, nextent)) != -1) {
  227. if(partial == nextent)
  228. break;
  229. nvp += partial;
  230. nextent -= partial;
  231. }
  232. if(partial == -1)
  233. return errno;
  234. *posp += extent;
  235. return ENOERR;
  236. }
  237. /* Read in a page of data.
  238. nciop - a pointer to the ncio struct for this file.
  239. offset - byte offset in file where read starts.
  240. extent - the size of the page that will be read.
  241. vp - a pointer to where the data will end up.
  242. nreadp - returned number of bytes actually read (may be less than extent).
  243. posp - pointer to current position in file, updated after read.
  244. */
  245. static int
  246. px_pgin(ncio *const nciop,
  247. off_t const offset, const size_t extent,
  248. void *const vp, size_t *nreadp, off_t *posp)
  249. {
  250. int status;
  251. ssize_t nread;
  252. #ifdef X_ALIGN
  253. assert(offset % X_ALIGN == 0);
  254. assert(extent % X_ALIGN == 0);
  255. #endif
  256. assert(*posp == OFF_NONE || *posp == lseek(nciop->fd, 0, SEEK_CUR));
  257. if(*posp != offset)
  258. {
  259. if(lseek(nciop->fd, offset, SEEK_SET) != offset)
  260. {
  261. status = errno;
  262. return status;
  263. }
  264. *posp = offset;
  265. }
  266. errno = 0;
  267. nread = read(nciop->fd, vp, extent);
  268. if(nread != (ssize_t) extent)
  269. {
  270. status = errno;
  271. if(nread == -1 || status != ENOERR)
  272. return status;
  273. /* else it's okay we read less than asked for */
  274. (void) memset((char *)vp + nread, 0, (ssize_t)extent - nread);
  275. }
  276. *nreadp = nread;
  277. *posp += nread;
  278. return ENOERR;
  279. }
  280. /* This struct is for POSIX systems, with NC_SHARE not in effect. If
  281. NC_SHARE is used, see ncio_spx.
  282. blksz - block size for reads and writes to file.
  283. pos - current read/write position in file.
  284. bf_offset - file offset corresponding to start of memory buffer
  285. bf_extent - number of bytes in I/O request
  286. bf_cnt - number of bytes available in buffer
  287. bf_base - pointer to beginning of buffer.
  288. bf_rflags - buffer region flags (defined in ncio.h) tell the lock
  289. status, read/write permissions, and modification status of regions
  290. of data in the buffer.
  291. bf_refcount - buffer reference count.
  292. slave - used in moves.
  293. */
  294. typedef struct ncio_px {
  295. size_t blksz;
  296. off_t pos;
  297. /* buffer */
  298. off_t bf_offset;
  299. size_t bf_extent;
  300. size_t bf_cnt;
  301. void *bf_base;
  302. int bf_rflags;
  303. int bf_refcount;
  304. /* chain for double buffering in px_move */
  305. struct ncio_px *slave;
  306. } ncio_px;
  307. /*ARGSUSED*/
  308. /* This function indicates the file region starting at offset may be
  309. released.
  310. This is for POSIX, without NC_SHARE. If called with RGN_MODIFIED
  311. flag, sets the modified flag in pxp->bf_rflags and decrements the
  312. reference count.
  313. pxp - pointer to posix non-share ncio_px struct.
  314. offset - file offset for beginning of to region to be
  315. released.
  316. rflags - only RGN_MODIFIED is relevent to this function, others ignored
  317. */
  318. static int
  319. px_rel(ncio_px *const pxp, off_t offset, int rflags)
  320. {
  321. assert(pxp->bf_offset <= offset
  322. && offset < pxp->bf_offset + (off_t) pxp->bf_extent);
  323. assert(pIf(fIsSet(rflags, RGN_MODIFIED),
  324. fIsSet(pxp->bf_rflags, RGN_WRITE)));
  325. if(fIsSet(rflags, RGN_MODIFIED))
  326. {
  327. fSet(pxp->bf_rflags, RGN_MODIFIED);
  328. }
  329. pxp->bf_refcount--;
  330. return ENOERR;
  331. }
  332. /* This function indicates the file region starting at offset may be
  333. released. Each read or write to the file is bracketed by a call to
  334. the "get" region function and a call to the "rel" region function.
  335. If you only read from the memory region, release it with a flag of
  336. 0, if you modify the region, release it with a flag of
  337. RGN_MODIFIED.
  338. For POSIX system, without NC_SHARE, this becomes the rel function
  339. pointed to by the ncio rel function pointer. It mearly checks for
  340. file write permission, then calls px_rel to do everything.
  341. nciop - pointer to ncio struct.
  342. offset - num bytes from beginning of buffer to region to be
  343. released.
  344. rflags - only RGN_MODIFIED is relevent to this function, others ignored
  345. */
  346. static int
  347. ncio_px_rel(ncio *const nciop, off_t offset, int rflags)
  348. {
  349. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  350. if(fIsSet(rflags, RGN_MODIFIED) && !fIsSet(nciop->ioflags, NC_WRITE))
  351. return EPERM; /* attempt to write readonly file */
  352. return px_rel(pxp, offset, rflags);
  353. }
  354. /* POSIX get. This will "make a region available." Since we're using
  355. buffered IO, this means that if needed, we'll fetch a new page from
  356. the file, otherwise, just return a pointer to what's in memory
  357. already.
  358. nciop - pointer to ncio struct, containing file info.
  359. pxp - pointer to ncio_px struct, which contains special metadate
  360. for posix files without NC_SHARE.
  361. offset - start byte of region to get.
  362. extent - how many bytes to read.
  363. rflags - One of the RGN_* flags defined in ncio.h.
  364. vpp - pointer to pointer that will recieve data.
  365. NOTES:
  366. * For blkoffset round offset down to the nearest pxp->blksz. This
  367. provides the offset (in bytes) to the beginning of the block that
  368. holds the current offset.
  369. * diff tells how far into the current block we are.
  370. * For blkextent round up to the number of bytes at the beginning of
  371. the next block, after the one that holds our current position, plus
  372. whatever extra (i.e. the extent) that we are about to grab.
  373. * The blkextent can't be more than twice the pxp->blksz. That's
  374. because the pxp->blksize is the sizehint, and in ncio_px_init2 the
  375. buffer (pointed to by pxp->bf-base) is allocated with 2 *
  376. *sizehintp. This is checked (unneccesarily) more than once in
  377. asserts.
  378. * If this is called on a newly opened file, pxp->bf_offset will be
  379. OFF_NONE and we'll jump to label pgin to immediately read in a
  380. page.
  381. */
  382. static int
  383. px_get(ncio *const nciop, ncio_px *const pxp,
  384. off_t offset, size_t extent,
  385. int rflags,
  386. void **const vpp)
  387. {
  388. int status = ENOERR;
  389. const off_t blkoffset = _RNDDOWN(offset, (off_t)pxp->blksz);
  390. off_t diff = (size_t)(offset - blkoffset);
  391. off_t blkextent = _RNDUP(diff + extent, pxp->blksz);
  392. assert(extent != 0);
  393. assert(extent < X_INT_MAX); /* sanity check */
  394. assert(offset >= 0); /* sanity check */
  395. if(2 * pxp->blksz < blkextent)
  396. return E2BIG; /* TODO: temporary kludge */
  397. if(pxp->bf_offset == OFF_NONE)
  398. {
  399. /* Uninitialized */
  400. if(pxp->bf_base == NULL)
  401. {
  402. assert(pxp->bf_extent == 0);
  403. assert(blkextent <= 2 * pxp->blksz);
  404. pxp->bf_base = malloc(2 * pxp->blksz);
  405. if(pxp->bf_base == NULL)
  406. return ENOMEM;
  407. }
  408. goto pgin;
  409. }
  410. /* else */
  411. assert(blkextent <= 2 * pxp->blksz);
  412. if(blkoffset == pxp->bf_offset)
  413. {
  414. /* hit */
  415. if(blkextent > pxp->bf_extent)
  416. {
  417. /* page in upper */
  418. void *const middle =
  419. (void *)((char *)pxp->bf_base + pxp->blksz);
  420. assert(pxp->bf_extent == pxp->blksz);
  421. status = px_pgin(nciop,
  422. pxp->bf_offset + (off_t)pxp->blksz,
  423. pxp->blksz,
  424. middle,
  425. &pxp->bf_cnt,
  426. &pxp->pos);
  427. if(status != ENOERR)
  428. return status;
  429. pxp->bf_extent = 2 * pxp->blksz;
  430. pxp->bf_cnt += pxp->blksz;
  431. }
  432. goto done;
  433. }
  434. /* else */
  435. if(pxp->bf_extent > pxp->blksz
  436. && blkoffset == pxp->bf_offset + (off_t)pxp->blksz)
  437. {
  438. /* hit in upper half */
  439. if(blkextent == pxp->blksz)
  440. {
  441. /* all in upper half, no fault needed */
  442. diff += pxp->blksz;
  443. goto done;
  444. }
  445. /* else */
  446. if(pxp->bf_cnt > pxp->blksz)
  447. {
  448. /* data in upper half */
  449. void *const middle =
  450. (void *)((char *)pxp->bf_base + pxp->blksz);
  451. assert(pxp->bf_extent == 2 * pxp->blksz);
  452. if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
  453. {
  454. /* page out lower half */
  455. assert(pxp->bf_refcount <= 0);
  456. status = px_pgout(nciop,
  457. pxp->bf_offset,
  458. pxp->blksz,
  459. pxp->bf_base,
  460. &pxp->pos);
  461. if(status != ENOERR)
  462. return status;
  463. }
  464. pxp->bf_cnt -= pxp->blksz;
  465. /* copy upper half into lower half */
  466. (void) memcpy(pxp->bf_base, middle, pxp->bf_cnt);
  467. }
  468. else /* added to fix nofill bug */
  469. {
  470. assert(pxp->bf_extent == 2 * pxp->blksz);
  471. /* still have to page out lower half, if modified */
  472. if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
  473. {
  474. assert(pxp->bf_refcount <= 0);
  475. status = px_pgout(nciop,
  476. pxp->bf_offset,
  477. pxp->blksz,
  478. pxp->bf_base,
  479. &pxp->pos);
  480. if(status != ENOERR)
  481. return status;
  482. }
  483. }
  484. pxp->bf_offset = blkoffset;
  485. /* pxp->bf_extent = pxp->blksz; */
  486. assert(blkextent == 2 * pxp->blksz);
  487. {
  488. /* page in upper */
  489. void *const middle =
  490. (void *)((char *)pxp->bf_base + pxp->blksz);
  491. status = px_pgin(nciop,
  492. pxp->bf_offset + (off_t)pxp->blksz,
  493. pxp->blksz,
  494. middle,
  495. &pxp->bf_cnt,
  496. &pxp->pos);
  497. if(status != ENOERR)
  498. return status;
  499. pxp->bf_extent = 2 * pxp->blksz;
  500. pxp->bf_cnt += pxp->blksz;
  501. }
  502. goto done;
  503. }
  504. /* else */
  505. if(blkoffset == pxp->bf_offset - (off_t)pxp->blksz)
  506. {
  507. /* wants the page below */
  508. void *const middle =
  509. (void *)((char *)pxp->bf_base + pxp->blksz);
  510. size_t upper_cnt = 0;
  511. if(pxp->bf_cnt > pxp->blksz)
  512. {
  513. /* data in upper half */
  514. assert(pxp->bf_extent == 2 * pxp->blksz);
  515. if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
  516. {
  517. /* page out upper half */
  518. assert(pxp->bf_refcount <= 0);
  519. status = px_pgout(nciop,
  520. pxp->bf_offset + (off_t)pxp->blksz,
  521. pxp->bf_cnt - pxp->blksz,
  522. middle,
  523. &pxp->pos);
  524. if(status != ENOERR)
  525. return status;
  526. }
  527. pxp->bf_cnt = pxp->blksz;
  528. pxp->bf_extent = pxp->blksz;
  529. }
  530. if(pxp->bf_cnt > 0)
  531. {
  532. /* copy lower half into upper half */
  533. (void) memcpy(middle, pxp->bf_base, pxp->blksz);
  534. upper_cnt = pxp->bf_cnt;
  535. }
  536. /* read page below into lower half */
  537. status = px_pgin(nciop,
  538. blkoffset,
  539. pxp->blksz,
  540. pxp->bf_base,
  541. &pxp->bf_cnt,
  542. &pxp->pos);
  543. if(status != ENOERR)
  544. return status;
  545. pxp->bf_offset = blkoffset;
  546. if(upper_cnt != 0)
  547. {
  548. pxp->bf_extent = 2 * pxp->blksz;
  549. pxp->bf_cnt = pxp->blksz + upper_cnt;
  550. }
  551. else
  552. {
  553. pxp->bf_extent = pxp->blksz;
  554. }
  555. goto done;
  556. }
  557. /* else */
  558. /* no overlap */
  559. if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
  560. {
  561. assert(pxp->bf_refcount <= 0);
  562. status = px_pgout(nciop,
  563. pxp->bf_offset,
  564. pxp->bf_cnt,
  565. pxp->bf_base,
  566. &pxp->pos);
  567. if(status != ENOERR)
  568. return status;
  569. pxp->bf_rflags = 0;
  570. }
  571. pgin:
  572. status = px_pgin(nciop,
  573. blkoffset,
  574. blkextent,
  575. pxp->bf_base,
  576. &pxp->bf_cnt,
  577. &pxp->pos);
  578. if(status != ENOERR)
  579. return status;
  580. pxp->bf_offset = blkoffset;
  581. pxp->bf_extent = blkextent;
  582. done:
  583. extent += diff;
  584. if(pxp->bf_cnt < extent)
  585. pxp->bf_cnt = extent;
  586. assert(pxp->bf_cnt <= pxp->bf_extent);
  587. pxp->bf_rflags |= rflags;
  588. pxp->bf_refcount++;
  589. *vpp = (char *)pxp->bf_base + diff;
  590. return ENOERR;
  591. }
  592. /* Request that the region (offset, extent) be made available through
  593. *vpp.
  594. This function converts a file region specified by an offset and
  595. extent to a memory pointer. The region may be locked until the
  596. corresponding call to rel().
  597. For POSIX systems, without NC_SHARE. This function gets a page of
  598. size extent?
  599. This is a wrapper for the function px_get, which does all the heavy
  600. lifting.
  601. nciop - pointer to ncio struct for this file.
  602. offset - offset (from beginning of file?) to the data we want to
  603. read.
  604. extent - the number of bytes to read from the file.
  605. rflags - One of the RGN_* flags defined in ncio.h.
  606. vpp - handle to point at data when it's been read.
  607. */
  608. static int
  609. ncio_px_get(ncio *const nciop,
  610. off_t offset, size_t extent,
  611. int rflags,
  612. void **const vpp)
  613. {
  614. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  615. if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE))
  616. return EPERM; /* attempt to write readonly file */
  617. /* reclaim space used in move */
  618. if(pxp->slave != NULL)
  619. {
  620. if(pxp->slave->bf_base != NULL)
  621. {
  622. free(pxp->slave->bf_base);
  623. pxp->slave->bf_base = NULL;
  624. pxp->slave->bf_extent = 0;
  625. pxp->slave->bf_offset = OFF_NONE;
  626. }
  627. free(pxp->slave);
  628. pxp->slave = NULL;
  629. }
  630. return px_get(nciop, pxp, offset, extent, rflags, vpp);
  631. }
  632. /* ARGSUSED */
  633. static int
  634. px_double_buffer(ncio *const nciop, off_t to, off_t from,
  635. size_t nbytes, int rflags)
  636. {
  637. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  638. int status = ENOERR;
  639. void *src;
  640. void *dest;
  641. #if INSTRUMENT
  642. fprintf(stderr, "\tdouble_buffr %ld %ld %ld\n",
  643. (long)to, (long)from, (long)nbytes);
  644. #endif
  645. status = px_get(nciop, pxp, to, nbytes, RGN_WRITE,
  646. &dest);
  647. if(status != ENOERR)
  648. return status;
  649. if(pxp->slave == NULL)
  650. {
  651. pxp->slave = (ncio_px *) malloc(sizeof(ncio_px));
  652. if(pxp->slave == NULL)
  653. return ENOMEM;
  654. pxp->slave->blksz = pxp->blksz;
  655. /* pos done below */
  656. pxp->slave->bf_offset = pxp->bf_offset;
  657. pxp->slave->bf_extent = pxp->bf_extent;
  658. pxp->slave->bf_cnt = pxp->bf_cnt;
  659. pxp->slave->bf_base = malloc(2 * pxp->blksz);
  660. if(pxp->slave->bf_base == NULL)
  661. return ENOMEM;
  662. (void) memcpy(pxp->slave->bf_base, pxp->bf_base,
  663. pxp->bf_extent);
  664. pxp->slave->bf_rflags = 0;
  665. pxp->slave->bf_refcount = 0;
  666. pxp->slave->slave = NULL;
  667. }
  668. pxp->slave->pos = pxp->pos;
  669. status = px_get(nciop, pxp->slave, from, nbytes, 0,
  670. &src);
  671. if(status != ENOERR)
  672. return status;
  673. if(pxp->pos != pxp->slave->pos)
  674. {
  675. /* position changed, sync */
  676. pxp->pos = pxp->slave->pos;
  677. }
  678. (void) memcpy(dest, src, nbytes);
  679. (void)px_rel(pxp->slave, from, 0);
  680. (void)px_rel(pxp, to, RGN_MODIFIED);
  681. return status;
  682. }
  683. /* Like memmove(), safely move possibly overlapping data.
  684. Copy one region to another without making anything available to
  685. higher layers. May be just implemented in terms of get() and rel(),
  686. or may be tricky to be efficient. Only used in by nc_enddef()
  687. after redefinition.
  688. nciop - pointer to ncio struct with file info.
  689. to - src for move?
  690. from - dest for move?
  691. nbytes - number of bytes to move.
  692. rflags - One of the RGN_* flags defined in ncio.h. The only
  693. reasonable flag value is RGN_NOLOCK.
  694. */
  695. static int
  696. ncio_px_move(ncio *const nciop, off_t to, off_t from,
  697. size_t nbytes, int rflags)
  698. {
  699. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  700. int status = ENOERR;
  701. off_t lower;
  702. off_t upper;
  703. char *base;
  704. size_t diff;
  705. size_t extent;
  706. if(to == from)
  707. return ENOERR; /* NOOP */
  708. if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE))
  709. return EPERM; /* attempt to write readonly file */
  710. rflags &= RGN_NOLOCK; /* filter unwanted flags */
  711. if(to > from)
  712. {
  713. /* growing */
  714. lower = from;
  715. upper = to;
  716. }
  717. else
  718. {
  719. /* shrinking */
  720. lower = to;
  721. upper = from;
  722. }
  723. diff = (size_t)(upper - lower);
  724. extent = diff + nbytes;
  725. #if INSTRUMENT
  726. fprintf(stderr, "ncio_px_move %ld %ld %ld %ld %ld\n",
  727. (long)to, (long)from, (long)nbytes, (long)lower, (long)extent);
  728. #endif
  729. if(extent > pxp->blksz)
  730. {
  731. size_t remaining = nbytes;
  732. if(to > from)
  733. {
  734. off_t frm = from + nbytes;
  735. off_t toh = to + nbytes;
  736. for(;;)
  737. {
  738. size_t loopextent = MIN(remaining, pxp->blksz);
  739. frm -= loopextent;
  740. toh -= loopextent;
  741. status = px_double_buffer(nciop, toh, frm,
  742. loopextent, rflags) ;
  743. if(status != ENOERR)
  744. return status;
  745. remaining -= loopextent;
  746. if(remaining == 0)
  747. break; /* normal loop exit */
  748. }
  749. }
  750. else
  751. {
  752. for(;;)
  753. {
  754. size_t loopextent = MIN(remaining, pxp->blksz);
  755. status = px_double_buffer(nciop, to, from,
  756. loopextent, rflags) ;
  757. if(status != ENOERR)
  758. return status;
  759. remaining -= loopextent;
  760. if(remaining == 0)
  761. break; /* normal loop exit */
  762. to += loopextent;
  763. from += loopextent;
  764. }
  765. }
  766. return ENOERR;
  767. }
  768. #if INSTRUMENT
  769. fprintf(stderr, "\tncio_px_move small\n");
  770. #endif
  771. status = px_get(nciop, pxp, lower, extent, RGN_WRITE|rflags,
  772. (void **)&base);
  773. if(status != ENOERR)
  774. return status;
  775. if(to > from)
  776. (void) memmove(base + diff, base, nbytes);
  777. else
  778. (void) memmove(base, base + diff, nbytes);
  779. (void) px_rel(pxp, lower, RGN_MODIFIED);
  780. return status;
  781. }
  782. /* Flush any buffers to disk. May be a no-op on if I/O is unbuffered.
  783. This function is used when NC_SHARE is NOT used.
  784. */
  785. static int
  786. ncio_px_sync(ncio *const nciop)
  787. {
  788. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  789. int status = ENOERR;
  790. if(fIsSet(pxp->bf_rflags, RGN_MODIFIED))
  791. {
  792. assert(pxp->bf_refcount <= 0);
  793. status = px_pgout(nciop, pxp->bf_offset,
  794. pxp->bf_cnt,
  795. pxp->bf_base, &pxp->pos);
  796. if(status != ENOERR)
  797. return status;
  798. pxp->bf_rflags = 0;
  799. }
  800. else if (!fIsSet(pxp->bf_rflags, RGN_WRITE))
  801. {
  802. /*
  803. * The dataset is readonly. Invalidate the buffers so
  804. * that the next ncio_px_get() will actually read data.
  805. */
  806. pxp->bf_offset = OFF_NONE;
  807. pxp->bf_cnt = 0;
  808. }
  809. return status;
  810. }
  811. /* Internal function called at close to
  812. free up anything hanging off pvt.
  813. */
  814. static void
  815. ncio_px_freepvt(void *const pvt)
  816. {
  817. ncio_px *const pxp = (ncio_px *)pvt;
  818. if(pxp == NULL)
  819. return;
  820. if(pxp->slave != NULL)
  821. {
  822. if(pxp->slave->bf_base != NULL)
  823. {
  824. free(pxp->slave->bf_base);
  825. pxp->slave->bf_base = NULL;
  826. pxp->slave->bf_extent = 0;
  827. pxp->slave->bf_offset = OFF_NONE;
  828. }
  829. free(pxp->slave);
  830. pxp->slave = NULL;
  831. }
  832. if(pxp->bf_base != NULL)
  833. {
  834. free(pxp->bf_base);
  835. pxp->bf_base = NULL;
  836. pxp->bf_extent = 0;
  837. pxp->bf_offset = OFF_NONE;
  838. }
  839. }
  840. /* This is the second half of the ncio initialization. This is called
  841. after the file has actually been opened.
  842. The most important thing that happens is the allocation of a block
  843. of memory at pxp->bf_base. This is going to be twice the size of
  844. the chunksizehint (rounded up to the nearest sizeof(double)) passed
  845. in from nc__create or nc__open. The rounded chunksizehint (passed
  846. in here in sizehintp) is going to be stored as pxp->blksize.
  847. According to our "contract" we are not allowed to ask for an extent
  848. larger than this chunksize/sizehint/blksize from the ncio get
  849. function.
  850. nciop - pointer to the ncio struct
  851. sizehintp - pointer to a size hint that will be rounded up and
  852. passed back to the caller.
  853. isNew - true if this is being called from ncio_create for a new
  854. file.
  855. */
  856. static int
  857. ncio_px_init2(ncio *const nciop, size_t *sizehintp, int isNew)
  858. {
  859. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  860. const size_t bufsz = 2 * *sizehintp;
  861. assert(nciop->fd >= 0);
  862. pxp->blksz = *sizehintp;
  863. assert(pxp->bf_base == NULL);
  864. /* this is separate allocation because it may grow */
  865. pxp->bf_base = malloc(bufsz);
  866. if(pxp->bf_base == NULL)
  867. return ENOMEM;
  868. /* else */
  869. pxp->bf_cnt = 0;
  870. if(isNew)
  871. {
  872. /* save a read */
  873. pxp->pos = 0;
  874. pxp->bf_offset = 0;
  875. pxp->bf_extent = bufsz;
  876. (void) memset(pxp->bf_base, 0, pxp->bf_extent);
  877. }
  878. return ENOERR;
  879. }
  880. /* This is the first of a two-part initialization of the ncio struct.
  881. Here the rel, get, move, sync, and free function pointers are set
  882. to their POSIX non-NC_SHARE functions (ncio_px_*).
  883. The ncio_px struct is also partially initialized.
  884. */
  885. static void
  886. ncio_px_init(ncio *const nciop)
  887. {
  888. ncio_px *const pxp = (ncio_px *)nciop->pvt;
  889. *((ncio_relfunc **)&nciop->rel) = ncio_px_rel; /* cast away const */
  890. *((ncio_getfunc **)&nciop->get) = ncio_px_get; /* cast away const */
  891. *((ncio_movefunc **)&nciop->move) = ncio_px_move; /* cast away const */
  892. *((ncio_syncfunc **)&nciop->sync) = ncio_px_sync; /* cast away const */
  893. *((ncio_filesizefunc **)&nciop->filesize) = ncio_px_filesize; /* cast away const */
  894. *((ncio_pad_lengthfunc **)&nciop->pad_length) = ncio_px_pad_length; /* cast away const */
  895. *((ncio_closefunc **)&nciop->close) = ncio_px_close; /* cast away const */
  896. pxp->blksz = 0;
  897. pxp->pos = -1;
  898. pxp->bf_offset = OFF_NONE;
  899. pxp->bf_extent = 0;
  900. pxp->bf_rflags = 0;
  901. pxp->bf_refcount = 0;
  902. pxp->bf_base = NULL;
  903. pxp->slave = NULL;
  904. }
  905. /* Begin spx */
  906. /* This is the struct that gets hung of ncio->pvt(?) when the NC_SHARE
  907. flag is used.
  908. */
  909. typedef struct ncio_spx {
  910. off_t pos;
  911. /* buffer */
  912. off_t bf_offset;
  913. size_t bf_extent;
  914. size_t bf_cnt;
  915. void *bf_base;
  916. } ncio_spx;
  917. /*ARGSUSED*/
  918. /* This function releases the region specified by offset.
  919. For POSIX system, with NC_SHARE, this becomes the rel function
  920. pointed to by the ncio rel function pointer. It mearly checks for
  921. file write permission, then calls px_rel to do everything.
  922. nciop - pointer to ncio struct.
  923. offset - beginning of region.
  924. rflags - One of the RGN_* flags defined in ncio.h. If set to
  925. RGN_MODIFIED it means that the data in this region were modified,
  926. and it needs to be written out to the disk immediately (since we
  927. are not buffering with NC_SHARE on).
  928. */
  929. static int
  930. ncio_spx_rel(ncio *const nciop, off_t offset, int rflags)
  931. {
  932. ncio_spx *const pxp = (ncio_spx *)nciop->pvt;
  933. int status = ENOERR;
  934. assert(pxp->bf_offset <= offset);
  935. assert(pxp->bf_cnt != 0);
  936. assert(pxp->bf_cnt <= pxp->bf_extent);
  937. #ifdef X_ALIGN
  938. assert(offset < pxp->bf_offset + X_ALIGN);
  939. assert(pxp->bf_cnt % X_ALIGN == 0 );
  940. #endif
  941. if(fIsSet(rflags, RGN_MODIFIED))
  942. {
  943. if(!fIsSet(nciop->ioflags, NC_WRITE))
  944. return EPERM; /* attempt to write readonly file */
  945. status = px_pgout(nciop, pxp->bf_offset,
  946. pxp->bf_cnt,
  947. pxp->bf_base, &pxp->pos);
  948. /* if error, invalidate buffer anyway */
  949. }
  950. pxp->bf_offset = OFF_NONE;
  951. pxp->bf_cnt = 0;
  952. return status;
  953. }
  954. /* Request that the region (offset, extent) be made available through
  955. *vpp.
  956. This function converts a file region specified by an offset and
  957. extent to a memory pointer. The region may be locked until the
  958. corresponding call to rel().
  959. For POSIX systems, with NC_SHARE.
  960. nciop - pointer to ncio struct for this file.
  961. offset - offset (from beginning of file?) to the data we want to
  962. read.
  963. extent - the number of bytes we want.
  964. rflags - One of the RGN_* flags defined in ncio.h. May be RGN_NOLOCK.
  965. vpp - handle to point at data when it's been read.
  966. */
  967. static int
  968. ncio_spx_get(ncio *const nciop,
  969. off_t offset, size_t extent,
  970. int rflags,
  971. void **const vpp)
  972. {
  973. ncio_spx *const pxp = (ncio_spx *)nciop->pvt;
  974. int status = ENOERR;
  975. #ifdef X_ALIGN
  976. size_t rem;
  977. #endif
  978. if(fIsSet(rflags, RGN_WRITE) && !fIsSet(nciop->ioflags, NC_WRITE))
  979. return EPERM; /* attempt to write readonly file */
  980. assert(extent != 0);
  981. assert(extent < X_INT_MAX); /* sanity check */
  982. assert(pxp->bf_cnt == 0);
  983. #ifdef X_ALIGN
  984. rem = (size_t)(offset % X_ALIGN);
  985. if(rem != 0)
  986. {
  987. offset -= rem;
  988. extent += rem;
  989. }
  990. {
  991. const size_t rndup = extent % X_ALIGN;
  992. if(rndup != 0)
  993. extent += X_ALIGN - rndup;
  994. }
  995. assert(offset % X_ALIGN == 0);
  996. assert(extent % X_ALIGN == 0);
  997. #endif
  998. if(pxp->bf_extent < extent)
  999. {
  1000. if(pxp->bf_base != NULL)
  1001. {
  1002. free(pxp->bf_base);
  1003. pxp->bf_base = NULL;
  1004. pxp->bf_extent = 0;
  1005. }
  1006. assert(pxp->bf_extent == 0);
  1007. pxp->bf_base = malloc(extent);
  1008. if(pxp->bf_base == NULL)
  1009. return ENOMEM;
  1010. pxp->bf_extent = extent;
  1011. }
  1012. status = px_pgin(nciop, offset,
  1013. extent,
  1014. pxp->bf_base,
  1015. &pxp->bf_cnt, &pxp->pos);
  1016. if(status != ENOERR)
  1017. return status;
  1018. pxp->bf_offset = offset;
  1019. if(pxp->bf_cnt < extent)
  1020. pxp->bf_cnt = extent;
  1021. #ifdef X_ALIGN
  1022. *vpp = (char *)pxp->bf_base + rem;
  1023. #else
  1024. *vpp = pxp->bf_base;
  1025. #endif
  1026. return ENOERR;
  1027. }
  1028. #if 0
  1029. /*ARGSUSED*/
  1030. static int
  1031. strategy(ncio *const nciop, off_t to, off_t offset,
  1032. size_t extent, int rflags)
  1033. {
  1034. static ncio_spx pxp[1];
  1035. int status = ENOERR;
  1036. #ifdef X_ALIGN
  1037. size_t rem;
  1038. #endif
  1039. assert(extent != 0);
  1040. assert(extent < X_INT_MAX); /* sanity check */
  1041. #if INSTRUMENT
  1042. fprintf(stderr, "strategy %ld at %ld to %ld\n",
  1043. (long)extent, (long)offset, (long)to);
  1044. #endif
  1045. #ifdef X_ALIGN
  1046. rem = (size_t)(offset % X_ALIGN);
  1047. if(rem != 0)
  1048. {
  1049. offset -= rem;
  1050. extent += rem;
  1051. }
  1052. {
  1053. const size_t rndup = extent % X_ALIGN;
  1054. if(rndup != 0)
  1055. extent += X_ALIGN - rndup;
  1056. }
  1057. assert(offset % X_ALIGN == 0);
  1058. assert(extent % X_ALIGN == 0);
  1059. #endif
  1060. if(pxp->bf_extent < extent)
  1061. {
  1062. if(pxp->bf_base != NULL)
  1063. {
  1064. free(pxp->bf_base);
  1065. pxp->bf_base = NULL;
  1066. pxp->bf_extent = 0;
  1067. }
  1068. assert(pxp->bf_extent == 0);
  1069. pxp->bf_base = malloc(extent);
  1070. if(pxp->bf_base == NULL)
  1071. return ENOMEM;
  1072. pxp->bf_extent = extent;
  1073. }
  1074. status = px_pgin(nciop, offset,
  1075. extent,
  1076. pxp->bf_base,
  1077. &pxp->bf_cnt, &pxp->pos);
  1078. if(status != ENOERR)
  1079. return status;
  1080. pxp->bf_offset = to; /* TODO: XALIGN */
  1081. if(pxp->bf_cnt < extent)
  1082. pxp->bf_cnt = extent;
  1083. status = px_pgout(nciop, pxp->bf_offset,
  1084. pxp->bf_cnt,
  1085. pxp->bf_base, &pxp->pos);
  1086. /* if error, invalidate buffer anyway */
  1087. pxp->bf_offset = OFF_NONE;
  1088. pxp->bf_cnt = 0;
  1089. return status;
  1090. }
  1091. #endif
  1092. /* Copy one region to another without making anything available to
  1093. higher layers. May be just implemented in terms of get() and rel(),
  1094. or may be tricky to be efficient. Only used in by nc_enddef()
  1095. after redefinition.
  1096. nciop - pointer to ncio struct for this file.
  1097. to - dest for move?
  1098. from - src for move?
  1099. nbytes - number of bytes to move.
  1100. rflags - One of the RGN_* flags defined in ncio.h.
  1101. */
  1102. static int
  1103. ncio_spx_move(ncio *const nciop, off_t to, off_t from,
  1104. size_t nbytes, int rflags)
  1105. {
  1106. int status = ENOERR;
  1107. off_t lower = from;
  1108. off_t upper = to;
  1109. char *base;
  1110. size_t diff;
  1111. size_t extent;
  1112. rflags &= RGN_NOLOCK; /* filter unwanted flags */
  1113. if(to == from)
  1114. return ENOERR; /* NOOP */
  1115. if(to > from)
  1116. {
  1117. /* growing */
  1118. lower = from;
  1119. upper = to;
  1120. }
  1121. else
  1122. {
  1123. /* shrinking */
  1124. lower = to;
  1125. upper = from;
  1126. }
  1127. diff = (size_t)(upper - lower);
  1128. extent = diff + nbytes;
  1129. status = ncio_spx_get(nciop, lower, extent, RGN_WRITE|rflags,
  1130. (void **)&base);
  1131. if(status != ENOERR)
  1132. return status;
  1133. if(to > from)
  1134. (void) memmove(base + diff, base, nbytes);
  1135. else
  1136. (void) memmove(base, base + diff, nbytes);
  1137. (void) ncio_spx_rel(nciop, lower, RGN_MODIFIED);
  1138. return status;
  1139. }
  1140. /*ARGSUSED*/
  1141. /* Flush any buffers to disk. May be a no-op on if I/O is unbuffered.
  1142. */
  1143. static int
  1144. ncio_spx_sync(ncio *const nciop)
  1145. {
  1146. /* NOOP */
  1147. return ENOERR;
  1148. }
  1149. static void
  1150. ncio_spx_freepvt(void *const pvt)
  1151. {
  1152. ncio_spx *const pxp = (ncio_spx *)pvt;
  1153. if(pxp == NULL)
  1154. return;
  1155. if(pxp->bf_base != NULL)
  1156. {
  1157. free(pxp->bf_base);
  1158. pxp->bf_base = NULL;
  1159. pxp->bf_offset = OFF_NONE;
  1160. pxp->bf_extent = 0;
  1161. pxp->bf_cnt = 0;
  1162. }
  1163. }
  1164. /* This does the second half of the ncio_spx struct initialization for
  1165. POSIX systems, with NC_SHARE on.
  1166. nciop - pointer to ncio struct for this file. File has been opened.
  1167. sizehintp - pointer to a size which will be rounded up to the
  1168. nearest 8-byt boundary and then used as the max size "chunk" (or
  1169. page) to read from the file.
  1170. */
  1171. static int
  1172. ncio_spx_init2(ncio *const nciop, const size_t *const sizehintp)
  1173. {
  1174. ncio_spx *const pxp = (ncio_spx *)nciop->pvt;
  1175. assert(nciop->fd >= 0);
  1176. pxp->bf_extent = *sizehintp;
  1177. assert(pxp->bf_base == NULL);
  1178. /* this is separate allocation because it may grow */
  1179. pxp->bf_base = malloc(pxp->bf_extent);
  1180. if(pxp->bf_base == NULL)
  1181. {
  1182. pxp->bf_extent = 0;
  1183. return ENOMEM;
  1184. }
  1185. /* else */
  1186. return ENOERR;
  1187. }
  1188. /* First half of init for ncio_spx struct, setting the rel, get, move,
  1189. snyc, and free function pointers to the NC_SHARE versions of these
  1190. functions (i.e. the ncio_spx_* functions).
  1191. */
  1192. static void
  1193. ncio_spx_init(ncio *const nciop)
  1194. {
  1195. ncio_spx *const pxp = (ncio_spx *)nciop->pvt;
  1196. *((ncio_relfunc **)&nciop->rel) = ncio_spx_rel; /* cast away const */
  1197. *((ncio_getfunc **)&nciop->get) = ncio_spx_get; /* cast away const */
  1198. *((ncio_movefunc **)&nciop->move) = ncio_spx_move; /* cast away const */
  1199. *((ncio_syncfunc **)&nciop->sync) = ncio_spx_sync; /* cast away const */
  1200. /* shared with _px_ */
  1201. *((ncio_filesizefunc **)&nciop->filesize) = ncio_px_filesize; /* cast away const */
  1202. *((ncio_pad_lengthfunc **)&nciop->pad_length) = ncio_px_pad_length; /* cast away const */
  1203. *((ncio_closefunc **)&nciop->close) = ncio_spx_close; /* cast away const */
  1204. pxp->pos = -1;
  1205. pxp->bf_offset = OFF_NONE;
  1206. pxp->bf_extent = 0;
  1207. pxp->bf_cnt = 0;
  1208. pxp->bf_base = NULL;
  1209. }
  1210. /* */
  1211. /* This will call whatever free function is attached to the free
  1212. function pointer in ncio. It's called from ncio_close, and from
  1213. ncio_open and ncio_create when an error occurs that the file
  1214. metadata must be freed.
  1215. */
  1216. static void
  1217. ncio_px_free(ncio *nciop)
  1218. {
  1219. if(nciop == NULL)
  1220. return;
  1221. if(nciop->pvt != NULL)
  1222. ncio_px_freepvt(nciop->pvt);
  1223. free(nciop);
  1224. }
  1225. static void
  1226. ncio_spx_free(ncio *nciop)
  1227. {
  1228. if(nciop == NULL)
  1229. return;
  1230. if(nciop->pvt != NULL)
  1231. ncio_spx_freepvt(nciop->pvt);
  1232. free(nciop);
  1233. }
  1234. /* Create a new ncio struct to hold info about the file. This will
  1235. create and init the ncio_px or ncio_spx struct (the latter if
  1236. NC_SHARE is used.)
  1237. */
  1238. static ncio *
  1239. ncio_px_new(const char *path, int ioflags)
  1240. {
  1241. size_t sz_ncio = M_RNDUP(sizeof(ncio));
  1242. size_t sz_path = M_RNDUP(strlen(path) +1);
  1243. size_t sz_ncio_pvt;
  1244. ncio *nciop;
  1245. #if ALWAYS_NC_SHARE /* DEBUG */
  1246. fSet(ioflags, NC_SHARE);
  1247. #endif
  1248. if(fIsSet(ioflags, NC_SHARE))
  1249. sz_ncio_pvt = sizeof(ncio_spx);
  1250. else
  1251. sz_ncio_pvt = sizeof(ncio_px);
  1252. nciop = (ncio *) malloc(sz_ncio + sz_path + sz_ncio_pvt);
  1253. if(nciop == NULL)
  1254. return NULL;
  1255. nciop->ioflags = ioflags;
  1256. *((int *)&nciop->fd) = -1; /* cast away const */
  1257. nciop->path = (char *) ((char *)nciop + sz_ncio);
  1258. (void) strcpy((char *)nciop->path, path); /* cast away const */
  1259. /* cast away const */
  1260. *((void **)&nciop->pvt) = (void *)(nciop->path + sz_path);
  1261. if(fIsSet(ioflags, NC_SHARE))
  1262. ncio_spx_init(nciop);
  1263. else
  1264. ncio_px_init(nciop);
  1265. return nciop;
  1266. }
  1267. /* Public below this point */
  1268. #ifndef NCIO_MINBLOCKSIZE
  1269. #define NCIO_MINBLOCKSIZE 256
  1270. #endif
  1271. #ifndef NCIO_MAXBLOCKSIZE
  1272. #define NCIO_MAXBLOCKSIZE 268435456 /* sanity check, about X_SIZE_T_MAX/8 */
  1273. #endif
  1274. #ifdef S_IRUSR
  1275. #define NC_DEFAULT_CREAT_MODE \
  1276. (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) /* 0666 */
  1277. #else
  1278. #define NC_DEFAULT_CREAT_MODE 0666
  1279. #endif
  1280. /* Create a file, and the ncio struct to go with it. This funtion is
  1281. only called from nc__create_mp.
  1282. path - path of file to create.
  1283. ioflags - flags from nc_create
  1284. initialsz - From the netcdf man page: "The argument
  1285. Iinitialsize sets the initial size of the file at creation time."
  1286. igeto -
  1287. igetsz -
  1288. sizehintp - this eventually goes into pxp->blksz and is the size of
  1289. a page of data for buffered reads and writes.
  1290. nciopp - pointer to a pointer that will get location of newly
  1291. created and inited ncio struct.
  1292. igetvpp - pointer to pointer which will get the location of ?
  1293. */
  1294. int
  1295. posixio_create(const char *path, int ioflags,
  1296. size_t initialsz,
  1297. off_t igeto, size_t igetsz, size_t *sizehintp,
  1298. ncio **nciopp, void **const igetvpp)
  1299. {
  1300. ncio *nciop;
  1301. int oflags = (O_RDWR|O_CREAT);
  1302. int fd;
  1303. int status;
  1304. if(initialsz < (size_t)igeto + igetsz)
  1305. initialsz = (size_t)igeto + igetsz;
  1306. fSet(ioflags, NC_WRITE);
  1307. if(path == NULL || *path == 0)
  1308. return EINVAL;
  1309. nciop = ncio_px_new(path, ioflags);
  1310. if(nciop == NULL)
  1311. return ENOMEM;
  1312. if(fIsSet(ioflags, NC_NOCLOBBER))
  1313. fSet(oflags, O_EXCL);
  1314. else
  1315. fSet(oflags, O_TRUNC);
  1316. #ifdef O_BINARY
  1317. fSet(oflags, O_BINARY);
  1318. #endif
  1319. #ifdef vms
  1320. fd = open(path, oflags, NC_DEFAULT_CREAT_MODE, "ctx=stm");
  1321. #else
  1322. /* Should we mess with the mode based on NC_SHARE ?? */
  1323. fd = open(path, oflags, NC_DEFAULT_CREAT_MODE);
  1324. #endif
  1325. #if 0
  1326. (void) fprintf(stderr, "ncio_create(): path=\"%s\"\n", path);
  1327. (void) fprintf(stderr, "ncio_create(): oflags=0x%x\n", oflags);
  1328. #endif
  1329. if(fd < 0)
  1330. {
  1331. status = errno;
  1332. goto unwind_new;
  1333. }
  1334. *((int *)&nciop->fd) = fd; /* cast away const */
  1335. if(*sizehintp < NCIO_MINBLOCKSIZE)
  1336. {
  1337. /* Use default */
  1338. *sizehintp = blksize(fd);
  1339. }
  1340. else if(*sizehintp >= NCIO_MAXBLOCKSIZE)
  1341. {
  1342. /* Use maximum allowed value */
  1343. *sizehintp = NCIO_MAXBLOCKSIZE;
  1344. }
  1345. else
  1346. {
  1347. *sizehintp = M_RNDUP(*sizehintp);
  1348. }
  1349. if(fIsSet(nciop->ioflags, NC_SHARE))
  1350. status = ncio_spx_init2(nciop, sizehintp);
  1351. else
  1352. status = ncio_px_init2(nciop, sizehintp, 1);
  1353. if(status != ENOERR)
  1354. goto unwind_open;
  1355. if(initialsz != 0)
  1356. {
  1357. status = fgrow(fd, (off_t)initialsz);
  1358. if(status != ENOERR)
  1359. goto unwind_open;
  1360. }
  1361. if(igetsz != 0)
  1362. {
  1363. status = nciop->get(nciop,
  1364. igeto, igetsz,
  1365. RGN_WRITE,
  1366. igetvpp);
  1367. if(status != ENOERR)
  1368. goto unwind_open;
  1369. }
  1370. *nciopp = nciop;
  1371. return ENOERR;
  1372. unwind_open:
  1373. (void) close(fd);
  1374. /* ?? unlink */
  1375. /*FALLTHRU*/
  1376. unwind_new:
  1377. ncio_close(nciop,!fIsSet(ioflags, NC_NOCLOBBER));
  1378. return status;
  1379. }
  1380. /* This function opens the data file. It is only called from nc.c,
  1381. from nc__open_mp and nc_delete_mp.
  1382. path - path of data file.
  1383. ioflags - flags passed into nc_open.
  1384. igeto - looks like this function can do an initial page get, and
  1385. igeto is going to be the offset for that. But it appears to be
  1386. unused
  1387. igetsz - the size in bytes of initial page get (a.k.a. extent). Not
  1388. ever used in the library.
  1389. sizehintp - pointer to sizehint parameter from nc__open or
  1390. nc__create. This is used to set pxp->blksz.
  1391. Here's what the man page has to say:
  1392. "The argument referenced by chunksize controls a space versus time
  1393. tradeoff, memory allocated in the netcdf library versus number of
  1394. system calls.
  1395. Because of internal requirements, the value may not be set to
  1396. exactly the value requested. The actual value chosen is returned by reference.
  1397. Using the value NC_SIZEHINT_DEFAULT causes the library to choose a
  1398. default. How the system choses the default depends on the
  1399. system. On many systems, the "preferred I/O block size" is
  1400. available from the stat() system call, struct stat member
  1401. st_blksize. If this is available it is used. Lacking that, twice
  1402. the system pagesize is used. Lacking a call to discover the system
  1403. pagesize, we just set default chunksize to 8192.
  1404. The chunksize is a property of a given open netcdf descriptor ncid,
  1405. it is not a persistent property of the netcdf dataset."
  1406. nciopp - pointer to pointer that will get address of newly created
  1407. and inited ncio struct.
  1408. igetvpp - handle to pass back pointer to data from inital page
  1409. read, if this were ever used, which it isn't.
  1410. */
  1411. int
  1412. posixio_open(const char *path,
  1413. int ioflags,
  1414. off_t igeto, size_t igetsz, size_t *sizehintp,
  1415. ncio **nciopp, void **const igetvpp)
  1416. {
  1417. ncio *nciop;
  1418. int oflags = fIsSet(ioflags, NC_WRITE) ? O_RDWR : O_RDONLY;
  1419. int fd;
  1420. int status;
  1421. if(path == NULL || *path == 0)
  1422. return EINVAL;
  1423. nciop = ncio_px_new(path, ioflags);
  1424. if(nciop == NULL)
  1425. return ENOMEM;
  1426. #ifdef O_BINARY
  1427. fSet(oflags, O_BINARY);
  1428. #endif
  1429. #ifdef vms
  1430. fd = open(path, oflags, 0, "ctx=stm");
  1431. #else
  1432. fd = open(path, oflags, 0);
  1433. #endif
  1434. if(fd < 0)
  1435. {
  1436. status = errno;
  1437. goto unwind_new;
  1438. }
  1439. *((int *)&nciop->fd) = fd; /* cast away const */
  1440. if(*sizehintp < NCIO_MINBLOCKSIZE)
  1441. {
  1442. /* Use default */
  1443. *sizehintp = blksize(fd);
  1444. }
  1445. else if(*sizehintp >= NCIO_MAXBLOCKSIZE)
  1446. {
  1447. /* Use maximum allowed value */
  1448. *sizehintp = NCIO_MAXBLOCKSIZE;
  1449. }
  1450. else
  1451. {
  1452. *sizehintp = M_RNDUP(*sizehintp);
  1453. }
  1454. if(fIsSet(nciop->ioflags, NC_SHARE))
  1455. status = ncio_spx_init2(nciop, sizehintp);
  1456. else
  1457. status = ncio_px_init2(nciop, sizehintp, 0);
  1458. if(status != ENOERR)
  1459. goto unwind_open;
  1460. if(igetsz != 0)
  1461. {
  1462. status = nciop->get(nciop,
  1463. igeto, igetsz,
  1464. 0,
  1465. igetvpp);
  1466. if(status != ENOERR)
  1467. goto unwind_open;
  1468. }
  1469. *nciopp = nciop;
  1470. return ENOERR;
  1471. unwind_open:
  1472. (void) close(fd);
  1473. /*FALLTHRU*/
  1474. unwind_new:
  1475. ncio_close(nciop,0);
  1476. return status;
  1477. }
  1478. /*
  1479. * Get file size in bytes.
  1480. */
  1481. static int
  1482. ncio_px_filesize(ncio *nciop, off_t *filesizep)
  1483. {
  1484. struct stat sb;
  1485. assert(nciop != NULL);
  1486. if (fstat(nciop->fd, &sb) < 0)
  1487. return errno;
  1488. *filesizep = sb.st_size;
  1489. return ENOERR;
  1490. }
  1491. /*
  1492. * Sync any changes to disk, then truncate or extend file so its size
  1493. * is length. This is only intended to be called before close, if the
  1494. * file is open for writing and the actual size does not match the
  1495. * calculated size, perhaps as the result of having been previously
  1496. * written in NOFILL mode.
  1497. */
  1498. static int
  1499. ncio_px_pad_length(ncio *nciop, off_t length)
  1500. {
  1501. int status = ENOERR;
  1502. if(nciop == NULL)
  1503. return EINVAL;
  1504. if(!fIsSet(nciop->ioflags, NC_WRITE))
  1505. return EPERM; /* attempt to write readonly file */
  1506. status = nciop->sync(nciop);
  1507. if(status != ENOERR)
  1508. return status;
  1509. status = fgrow2(nciop->fd, length);
  1510. if(status != ENOERR)
  1511. return status;
  1512. return ENOERR;
  1513. }
  1514. /* Write out any dirty buffers to disk and
  1515. ensure that next read will get data from disk.
  1516. Sync any changes, then close the open file associated with the ncio
  1517. struct, and free its memory.
  1518. nciop - pointer to ncio to close.
  1519. doUnlink - if true, unlink file
  1520. */
  1521. static int
  1522. ncio_px_close(ncio *nciop, int doUnlink)
  1523. {
  1524. int status = ENOERR;
  1525. if(nciop == NULL)
  1526. return EINVAL;
  1527. status = nciop->sync(nciop);
  1528. (void) close(nciop->fd);
  1529. if(doUnlink)
  1530. (void) unlink(nciop->path);
  1531. ncio_px_free(nciop);
  1532. return status;
  1533. }
  1534. static int
  1535. ncio_spx_close(ncio *nciop, int doUnlink)
  1536. {
  1537. int status = ENOERR;
  1538. if(nciop == NULL)
  1539. return EINVAL;
  1540. status = nciop->sync(nciop);
  1541. (void) close(nciop->fd);
  1542. if(doUnlink)
  1543. (void) unlink(nciop->path);
  1544. ncio_spx_free(nciop);
  1545. return status;
  1546. }