mmapio.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * Copyright 1996, University Corporation for Atmospheric Research
  3. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  4. */
  5. #include "config.h"
  6. #include <assert.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #ifdef _MSC_VER /* Microsoft Compilers */
  11. #include <io.h>
  12. #endif
  13. #ifdef HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #ifdef HAVE_FCNTL_H
  17. #include <fcntl.h>
  18. #endif
  19. #include "nc.h"
  20. #undef DEBUG
  21. #ifdef DEBUG
  22. #include <stdio.h>
  23. #endif
  24. #include <sys/mman.h>
  25. #ifndef MAP_ANONYMOUS
  26. # ifdef MAP_ANON
  27. # define MAP_ANONYMOUS MAP_ANON
  28. # endif
  29. #endif
  30. /* !MAP_ANONYMOUS => !HAVE_MMAP */
  31. #ifndef MAP_ANONYMOUS
  32. #error mmap not fully implemented: missing MAP_ANONYMOUS
  33. #endif
  34. #ifdef HAVE_MMAP
  35. /* This is conditionalized by __USE_GNU ; why? */
  36. extern void *mremap(void*,size_t,size_t,int);
  37. # ifndef MREMAP_MAYMOVE
  38. # define MREMAP_MAYMOVE 1
  39. # endif
  40. #endif /*HAVE_MMAP*/
  41. #ifndef HAVE_SSIZE_T
  42. #define ssize_t int
  43. #endif
  44. #ifndef SEEK_SET
  45. #define SEEK_SET 0
  46. #define SEEK_CUR 1
  47. #define SEEK_END 2
  48. #endif
  49. /* Define the mode flags for create: let umask decide */
  50. #define OPENMODE 0666
  51. #include "ncio.h"
  52. #include "fbits.h"
  53. #include "rnd.h"
  54. /* #define INSTRUMENT 1 */
  55. #if INSTRUMENT /* debugging */
  56. #undef NDEBUG
  57. #include <stdio.h>
  58. /*#include "instr.h"*/
  59. #endif
  60. #ifndef MMAP_MAXBLOCKSIZE
  61. #define MMAP_MAXBLOCKSIZE 268435456 /* sanity check, about X_SIZE_T_MAX/8 */
  62. #endif
  63. #undef MIN /* system may define MIN somewhere and complain */
  64. #define MIN(mm,nn) (((mm) < (nn)) ? (mm) : (nn))
  65. #if !defined(NDEBUG) && !defined(X_INT_MAX)
  66. #define X_INT_MAX 2147483647
  67. #endif
  68. #if 0 /* !defined(NDEBUG) && !defined(X_ALIGN) */
  69. #define X_ALIGN 4
  70. #else
  71. #undef X_ALIGN
  72. #endif
  73. /* Private data for mmap */
  74. typedef struct NCMMAPIO {
  75. int locked; /* => we cannot realloc */
  76. int persist; /* => save to a file; triggered by NC_WRITE */
  77. char* memory;
  78. off_t alloc;
  79. off_t size;
  80. off_t pos;
  81. int mapfd;
  82. } NCMMAPIO;
  83. /* Forward */
  84. static int mmapio_rel(ncio *const nciop, off_t offset, int rflags);
  85. static int mmapio_get(ncio *const nciop, off_t offset, size_t extent, int rflags, void **const vpp);
  86. static int mmapio_move(ncio *const nciop, off_t to, off_t from, size_t nbytes, int rflags);
  87. static int mmapio_sync(ncio *const nciop);
  88. static int mmapio_filesize(ncio* nciop, off_t* filesizep);
  89. static int mmapio_pad_length(ncio* nciop, off_t length);
  90. static int mmapio_close(ncio* nciop, int);
  91. /* Mnemonic */
  92. #define DOOPEN 1
  93. static long pagesize = 0;
  94. /* Create a new ncio struct to hold info about the file. */
  95. static int
  96. mmapio_new(const char* path, int ioflags, off_t initialsize, ncio** nciopp, NCMMAPIO** mmapp)
  97. {
  98. int status = NC_NOERR;
  99. ncio* nciop = NULL;
  100. NCMMAPIO* mmapio = NULL;
  101. int openfd = -1;
  102. if(pagesize == 0) {
  103. #if defined HAVE_SYSCONF
  104. pagesize = sysconf(_SC_PAGE_SIZE);
  105. #elif defined HAVE_GETPAGESIZE
  106. pagesize = getpagesize();
  107. #else
  108. pagesize = 4096; /* good guess */
  109. #endif
  110. }
  111. errno = 0;
  112. /* Always force the allocated size to be a multiple of pagesize */
  113. if(initialsize == 0) initialsize = pagesize;
  114. if((initialsize % pagesize) != 0)
  115. initialsize += (pagesize - (initialsize % pagesize));
  116. nciop = (ncio* )calloc(1,sizeof(ncio));
  117. if(nciop == NULL) {status = NC_ENOMEM; goto fail;}
  118. nciop->ioflags = ioflags;
  119. *((int*)&nciop->fd) = -1; /* caller will fix */
  120. *((char**)&nciop->path) = strdup(path);
  121. if(nciop->path == NULL) {status = NC_ENOMEM; goto fail;}
  122. *((ncio_relfunc**)&nciop->rel) = mmapio_rel;
  123. *((ncio_getfunc**)&nciop->get) = mmapio_get;
  124. *((ncio_movefunc**)&nciop->move) = mmapio_move;
  125. *((ncio_syncfunc**)&nciop->sync) = mmapio_sync;
  126. *((ncio_filesizefunc**)&nciop->filesize) = mmapio_filesize;
  127. *((ncio_pad_lengthfunc**)&nciop->pad_length) = mmapio_pad_length;
  128. *((ncio_closefunc**)&nciop->close) = mmapio_close;
  129. mmapio = (NCMMAPIO*)calloc(1,sizeof(NCMMAPIO));
  130. if(mmapio == NULL) {status = NC_ENOMEM; goto fail;}
  131. *((void* *)&nciop->pvt) = mmapio;
  132. mmapio->alloc = initialsize;
  133. mmapio->memory = NULL;
  134. mmapio->size = 0;
  135. mmapio->pos = 0;
  136. mmapio->persist = fIsSet(ioflags,NC_WRITE);
  137. /* See if ok to use mmap */
  138. if(sizeof(void*) < 8 && fIsSet(ioflags,NC_64BIT_OFFSET))
  139. return NC_DISKLESS; /* cannot support */
  140. mmapio->mapfd = -1;
  141. if(nciopp) *nciopp = nciop;
  142. if(mmapp) *mmapp = mmapio;
  143. done:
  144. if(openfd >= 0) close(openfd);
  145. return status;
  146. fail:
  147. if(nciop != NULL) {
  148. if(nciop->path != NULL) free((char*)nciop->path);
  149. }
  150. goto done;
  151. }
  152. /* Create a file, and the ncio struct to go with it. This function is
  153. only called from nc__create_mp.
  154. path - path of file to create.
  155. ioflags - flags from nc_create
  156. initialsz - From the netcdf man page: "The argument
  157. Iinitialsize sets the initial size of the file at creation time."
  158. igeto -
  159. igetsz -
  160. sizehintp - the size of a page of data for buffered reads and writes.
  161. nciopp - pointer to a pointer that will get location of newly
  162. created and inited ncio struct.
  163. mempp - pointer to pointer to the initial memory read.
  164. */
  165. int
  166. mmapio_create(const char* path, int ioflags,
  167. size_t initialsz,
  168. off_t igeto, size_t igetsz, size_t* sizehintp,
  169. ncio* *nciopp, void** const mempp)
  170. {
  171. ncio* nciop;
  172. int fd;
  173. int status;
  174. NCMMAPIO* mmapio = NULL;
  175. int persist = (ioflags & NC_WRITE?1:0);
  176. int oflags;
  177. if(path == NULL ||* path == 0)
  178. return NC_EINVAL;
  179. /* For diskless open has, the file must be classic version 1 or 2.*/
  180. if(fIsSet(ioflags,NC_NETCDF4))
  181. return NC_EDISKLESS; /* violates constraints */
  182. status = mmapio_new(path, ioflags, initialsz, &nciop, &mmapio);
  183. if(status != NC_NOERR)
  184. return status;
  185. mmapio->size = 0;
  186. if(!persist) {
  187. mmapio->mapfd = -1;
  188. mmapio->memory = (char*)mmap(NULL,mmapio->alloc,
  189. PROT_READ|PROT_WRITE,
  190. MAP_PRIVATE|MAP_ANONYMOUS,
  191. mmapio->mapfd,0);
  192. {mmapio->memory[0] = 0;} /* test writing of the mmap'd memory */
  193. } else { /*persist */
  194. /* Open the file, but make sure we can write it if needed */
  195. oflags = (persist ? O_RDWR : O_RDONLY);
  196. #ifdef O_BINARY
  197. fSet(oflags, O_BINARY);
  198. #endif
  199. oflags |= (O_CREAT|O_TRUNC);
  200. if(fIsSet(ioflags,NC_NOCLOBBER))
  201. oflags |= O_EXCL;
  202. #ifdef vms
  203. fd = open(path, oflags, 0, "ctx=stm");
  204. #else
  205. fd = open(path, oflags, OPENMODE);
  206. #endif
  207. if(fd < 0) {status = errno; goto unwind_open;}
  208. mmapio->mapfd = fd;
  209. { /* Cause the output file to have enough allocated space */
  210. lseek(fd,mmapio->alloc-1,SEEK_SET); /* cause file to appear */
  211. write(fd,"",mmapio->alloc);
  212. lseek(fd,0,SEEK_SET); /* rewind */
  213. }
  214. mmapio->memory = (char*)mmap(NULL,mmapio->alloc,
  215. PROT_READ|PROT_WRITE,
  216. MAP_SHARED,
  217. mmapio->mapfd,0);
  218. if(mmapio->memory == NULL) {
  219. return NC_EDISKLESS;
  220. }
  221. } /*!persist*/
  222. #ifdef DEBUG
  223. fprintf(stderr,"mmap_create: initial memory: %lu/%lu\n",(unsigned long)mmapio->memory,(unsigned long)mmapio->alloc);
  224. #endif
  225. fd = nc__pseudofd();
  226. *((int* )&nciop->fd) = fd;
  227. fSet(nciop->ioflags, NC_WRITE);
  228. if(igetsz != 0)
  229. {
  230. status = nciop->get(nciop,
  231. igeto, igetsz,
  232. RGN_WRITE,
  233. mempp);
  234. if(status != NC_NOERR)
  235. goto unwind_open;
  236. }
  237. /* Pick a default sizehint */
  238. if(sizehintp) *sizehintp = pagesize;
  239. *nciopp = nciop;
  240. return NC_NOERR;
  241. unwind_open:
  242. mmapio_close(nciop,1);
  243. return status;
  244. }
  245. /* This function opens the data file. It is only called from nc.c,
  246. from nc__open_mp and nc_delete_mp.
  247. path - path of data file.
  248. ioflags - flags passed into nc_open.
  249. igeto - looks like this function can do an initial page get, and
  250. igeto is going to be the offset for that. But it appears to be
  251. unused
  252. igetsz - the size in bytes of initial page get (a.k.a. extent). Not
  253. ever used in the library.
  254. sizehintp - the size of a page of data for buffered reads and writes.
  255. nciopp - pointer to pointer that will get address of newly created
  256. and inited ncio struct.
  257. mempp - pointer to pointer to the initial memory read.
  258. */
  259. int
  260. mmapio_open(const char* path,
  261. int ioflags,
  262. off_t igeto, size_t igetsz, size_t* sizehintp,
  263. ncio* *nciopp, void** const mempp)
  264. {
  265. ncio* nciop;
  266. int fd;
  267. int status;
  268. int persist = (fIsSet(ioflags,NC_WRITE)?1:0);
  269. int oflags;
  270. NCMMAPIO* mmapio = NULL;
  271. size_t sizehint;
  272. off_t filesize;
  273. if(path == NULL ||* path == 0)
  274. return EINVAL;
  275. assert(sizehintp != NULL);
  276. sizehint = *sizehintp;
  277. /* Open the file, but make sure we can write it if needed */
  278. oflags = (persist ? O_RDWR : O_RDONLY);
  279. #ifdef O_BINARY
  280. fSet(oflags, O_BINARY);
  281. #endif
  282. oflags |= O_EXCL;
  283. #ifdef vms
  284. fd = open(path, oflags, 0, "ctx=stm");
  285. #else
  286. fd = open(path, oflags, OPENMODE);
  287. #endif
  288. if(fd < 0) {status = errno; goto unwind_open;}
  289. /* get current filesize = max(|file|,initialize)*/
  290. filesize = lseek(fd,0,SEEK_END);
  291. if(filesize < 0) {status = errno; goto unwind_open;}
  292. /* move pointer back to beginning of file */
  293. (void)lseek(fd,0,SEEK_SET);
  294. if(filesize < (off_t)sizehint)
  295. filesize = (off_t)sizehint;
  296. status = mmapio_new(path, ioflags, filesize, &nciop, &mmapio);
  297. if(status != NC_NOERR)
  298. return status;
  299. mmapio->size = filesize;
  300. mmapio->mapfd = fd;
  301. mmapio->memory = (char*)mmap(NULL,mmapio->alloc,
  302. persist?(PROT_READ|PROT_WRITE):(PROT_READ),
  303. MAP_SHARED,
  304. mmapio->mapfd,0);
  305. #ifdef DEBUG
  306. fprintf(stderr,"mmapio_open: initial memory: %lu/%lu\n",(unsigned long)mmapio->memory,(unsigned long)mmapio->alloc);
  307. #endif
  308. /* Use half the filesize as the blocksize */
  309. sizehint = filesize/2;
  310. fd = nc__pseudofd();
  311. *((int* )&nciop->fd) = fd;
  312. if(igetsz != 0)
  313. {
  314. status = nciop->get(nciop,
  315. igeto, igetsz,
  316. 0,
  317. mempp);
  318. if(status != NC_NOERR)
  319. goto unwind_open;
  320. }
  321. *sizehintp = sizehint;
  322. *nciopp = nciop;
  323. return NC_NOERR;
  324. unwind_open:
  325. mmapio_close(nciop,0);
  326. return status;
  327. }
  328. /*
  329. * Get file size in bytes.
  330. */
  331. static int
  332. mmapio_filesize(ncio* nciop, off_t* filesizep)
  333. {
  334. NCMMAPIO* mmapio;
  335. if(nciop == NULL || nciop->pvt == NULL) return NC_EINVAL;
  336. mmapio = (NCMMAPIO*)nciop->pvt;
  337. if(filesizep != NULL) *filesizep = mmapio->size;
  338. return NC_NOERR;
  339. }
  340. /*
  341. * Sync any changes to disk, then truncate or extend file so its size
  342. * is length. This is only intended to be called before close, if the
  343. * file is open for writing and the actual size does not match the
  344. * calculated size, perhaps as the result of having been previously
  345. * written in NOFILL mode.
  346. */
  347. static int
  348. mmapio_pad_length(ncio* nciop, off_t length)
  349. {
  350. NCMMAPIO* mmapio;
  351. if(nciop == NULL || nciop->pvt == NULL) return NC_EINVAL;
  352. mmapio = (NCMMAPIO*)nciop->pvt;
  353. if(!fIsSet(nciop->ioflags, NC_WRITE))
  354. return EPERM; /* attempt to write readonly file*/
  355. if(mmapio->locked > 0)
  356. return NC_EDISKLESS;
  357. if(length > mmapio->alloc) {
  358. /* Realloc the allocated memory to a multiple of the pagesize*/
  359. off_t newsize = length;
  360. void* newmem = NULL;
  361. /* Round to a multiple of pagesize */
  362. if((newsize % pagesize) != 0)
  363. newsize += (pagesize - (newsize % pagesize));
  364. /* Force file size to be properly extended */
  365. { /* Cause the output file to have enough allocated space */
  366. off_t pos = lseek(mmapio->mapfd,0,SEEK_CUR); /* save current position*/
  367. /* cause file to be extended in size */
  368. lseek(mmapio->mapfd,newsize-1,SEEK_SET);
  369. write(mmapio->mapfd,"",mmapio->alloc);
  370. lseek(mmapio->mapfd,pos,SEEK_SET); /* reset position */
  371. }
  372. newmem = (char*)mremap(mmapio->memory,mmapio->alloc,newsize,MREMAP_MAYMOVE);
  373. if(newmem == NULL) return NC_ENOMEM;
  374. #ifdef DEBUG
  375. fprintf(stderr,"realloc: %lu/%lu -> %lu/%lu\n",
  376. (unsigned long)mmapio->memory,(unsigned long)mmapio->alloc,
  377. (unsigned long)newmem,(unsigned long)newsize);
  378. #endif
  379. mmapio->memory = newmem;
  380. mmapio->alloc = newsize;
  381. }
  382. mmapio->size = length;
  383. return NC_NOERR;
  384. }
  385. /* Write out any dirty buffers to disk and
  386. ensure that next read will get data from disk.
  387. Sync any changes, then close the open file associated with the ncio
  388. struct, and free its memory.
  389. nciop - pointer to ncio to close.
  390. doUnlink - if true, unlink file
  391. */
  392. static int
  393. mmapio_close(ncio* nciop, int doUnlink)
  394. {
  395. int status = NC_NOERR;
  396. NCMMAPIO* mmapio;
  397. if(nciop == NULL || nciop->pvt == NULL) return NC_NOERR;
  398. mmapio = (NCMMAPIO*)nciop->pvt;
  399. assert(mmapio != NULL);
  400. /* Since we are using mmap, persisting to a file should be automatic */
  401. status = munmap(mmapio->memory,mmapio->alloc);
  402. mmapio->memory = NULL; /* so we do not try to free it */
  403. /* Close file if it was open */
  404. if(mmapio->mapfd >= 0)
  405. close(mmapio->mapfd);
  406. /* do cleanup */
  407. if(mmapio != NULL) free(mmapio);
  408. if(nciop->path != NULL) free((char*)nciop->path);
  409. free(nciop);
  410. return status;
  411. }
  412. static int
  413. guarantee(ncio* nciop, off_t endpoint)
  414. {
  415. NCMMAPIO* mmapio = (NCMMAPIO*)nciop->pvt;
  416. if(endpoint > mmapio->alloc) {
  417. /* extend the allocated memory and size */
  418. int status = mmapio_pad_length(nciop,endpoint);
  419. if(status != NC_NOERR) return status;
  420. }
  421. if(mmapio->size < endpoint)
  422. mmapio->size = endpoint;
  423. return NC_NOERR;
  424. }
  425. /*
  426. * Request that the region (offset, extent)
  427. * be made available through *vpp.
  428. */
  429. static int
  430. mmapio_get(ncio* const nciop, off_t offset, size_t extent, int rflags, void** const vpp)
  431. {
  432. int status = NC_NOERR;
  433. NCMMAPIO* mmapio;
  434. if(nciop == NULL || nciop->pvt == NULL) return NC_EINVAL;
  435. mmapio = (NCMMAPIO*)nciop->pvt;
  436. status = guarantee(nciop, offset+extent);
  437. mmapio->locked++;
  438. if(status != NC_NOERR) return status;
  439. if(vpp) *vpp = mmapio->memory+offset;
  440. return NC_NOERR;
  441. }
  442. /*
  443. * Like memmove(), safely move possibly overlapping data.
  444. */
  445. static int
  446. mmapio_move(ncio* const nciop, off_t to, off_t from, size_t nbytes, int ignored)
  447. {
  448. int status = NC_NOERR;
  449. NCMMAPIO* mmapio;
  450. if(nciop == NULL || nciop->pvt == NULL) return NC_EINVAL;
  451. mmapio = (NCMMAPIO*)nciop->pvt;
  452. if(from < to) {
  453. /* extend if "to" is not currently allocated */
  454. status = guarantee(nciop,to+nbytes);
  455. if(status != NC_NOERR) return status;
  456. }
  457. /* check for overlap */
  458. if((to + nbytes) > from || (from + nbytes) > to) {
  459. /* Ranges overlap */
  460. #ifdef HAVE_MEMMOVE
  461. memmove((void*)(mmapio->memory+to),(void*)(mmapio->memory+from),nbytes);
  462. #else
  463. off_t overlap;
  464. off_t nbytes1;
  465. if((from + nbytes) > to) {
  466. overlap = ((from + nbytes) - to); /* # bytes of overlap */
  467. nbytes1 = (nbytes - overlap); /* # bytes of non-overlap */
  468. /* move the non-overlapping part */
  469. memcpy((void*)(mmapio->memory+(to+overlap)),
  470. (void*)(mmapio->memory+(from+overlap)),
  471. nbytes1);
  472. /* move the overlapping part */
  473. memcpy((void*)(mmapio->memory+to),
  474. (void*)(mmapio->memory+from),
  475. overlap);
  476. } else { /*((to + nbytes) > from) */
  477. overlap = ((to + nbytes) - from); /* # bytes of overlap */
  478. nbytes1 = (nbytes - overlap); /* # bytes of non-overlap */
  479. /* move the non-overlapping part */
  480. memcpy((void*)(mmapio->memory+to),
  481. (void*)(mmapio->memory+from),
  482. nbytes1);
  483. /* move the overlapping part */
  484. memcpy((void*)(mmapio->memory+(to+nbytes1)),
  485. (void*)(mmapio->memory+(from+nbytes1)),
  486. overlap);
  487. }
  488. #endif
  489. } else {/* no overlap */
  490. memcpy((void*)(mmapio->memory+to),(void*)(mmapio->memory+from),nbytes);
  491. }
  492. return status;
  493. }
  494. static int
  495. mmapio_rel(ncio* const nciop, off_t offset, int rflags)
  496. {
  497. NCMMAPIO* mmapio;
  498. if(nciop == NULL || nciop->pvt == NULL) return NC_EINVAL;
  499. mmapio = (NCMMAPIO*)nciop->pvt;
  500. mmapio->locked--;
  501. return NC_NOERR; /* do nothing */
  502. }
  503. /*
  504. * Write out any dirty buffers to disk and
  505. * ensure that next read will get data from disk.
  506. */
  507. static int
  508. mmapio_sync(ncio* const nciop)
  509. {
  510. return NC_NOERR; /* do nothing */
  511. }