aboutsummaryrefslogtreecommitdiff
path: root/src/mmap.c
blob: 273f64ca549a0fae1d933163c07daf06e26e87c3 (plain)
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
/* This file is part of GDBM.
   Copyright (C) 2007, 2011, 2013, 2016-2019 Free Software Foundation,
   Inc.

   GDBM is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GDBM is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GDBM. If not, see <http://www.gnu.org/licenses/>.   */

#include "autoconf.h"

#if HAVE_MMAP

# include "gdbmdefs.h"

# include <sys/types.h>
# include <sys/time.h>
# include <sys/file.h>
# include <sys/stat.h>
# include <sys/mman.h>
# include <stdio.h>

/* Some systems fail to define this */
# ifndef MAP_FAILED
#  define MAP_FAILED ((void*)-1)
# endif

# if defined(MAP_POPULATE)
#  define GDBM_MMAP_FLAGS MAP_POPULATE
# elif defined(MAP_PREFAULT_READ)
#  define GDBM_MMAP_FLAGS MAP_PREFAULT_READ
# else
#  define GDBM_MMAP_FLAGS 0
# endif

/* Translate current offset in the mapped region into the absolute position */
# define _GDBM_MMAPPED_POS(dbf) ((dbf)->mapped_off + (dbf)->mapped_pos)
/* Return true if the absolute offset OFF lies within the currentlty mmapped
   region */
# define _GDBM_IN_MAPPED_REGION_P(dbf, off) \
  ((off) >= (dbf)->mapped_off \
   && ((off) - (dbf)->mapped_off) < (dbf)->mapped_size)
/* Return true if the current region needs to be remapped */
# define _GDBM_NEED_REMAP(dbf) \
  (!(dbf)->mapped_region || (dbf)->mapped_pos == (dbf)->mapped_size)
/* Return the sum of the currently mapped size and DELTA */
static inline off_t
SUM_FILE_SIZE (GDBM_FILE dbf, off_t delta)
{
  if (delta >= 0
      && off_t_sum_ok (dbf->mapped_off, dbf->mapped_size)
      && off_t_sum_ok (dbf->mapped_off + dbf->mapped_size, delta))
    return dbf->mapped_off + dbf->mapped_size + delta;
  return -1;
}

/* Store the size of the GDBM file DBF in *PSIZE.
   Return 0 on success and -1 on failure. */
int
_gdbm_file_size (GDBM_FILE dbf, off_t *psize)
{
  struct stat sb;
  if (fstat (dbf->desc, &sb))
    {
      GDBM_SET_ERRNO (dbf, GDBM_FILE_STAT_ERROR, FALSE);
      return -1;
    }
  *psize = sb.st_size;
  return 0;
}

/* Unmap the region. Reset all mapped fields to initial values. */
void
_gdbm_mapped_unmap (GDBM_FILE dbf)
{
  if (dbf->mapped_region)
    {
      munmap (dbf->mapped_region, dbf->mapped_size);
      dbf->mapped_region = NULL;
      dbf->mapped_size = 0;
      dbf->mapped_pos = 0;
      dbf->mapped_off = 0;
    }
}

/* Remap the DBF file according to dbf->{mapped_off,mapped_pos,mapped_size}.
   Take care to recompute {mapped_off,mapped_pos} so that the former lies
   on a page size boundary. */
int
_gdbm_internal_remap (GDBM_FILE dbf, size_t size)
{
  void *p;
  int prot = PROT_READ;
  size_t page_size = sysconf (_SC_PAGESIZE);

  if (dbf->mapped_region)
    {
      munmap (dbf->mapped_region, dbf->mapped_size);
      dbf->mapped_region = NULL;
    }
  dbf->mapped_size = size;

  if (size == 0)
    return 0;
  
  dbf->mapped_pos += dbf->mapped_off % page_size;
  dbf->mapped_off = (dbf->mapped_off / page_size) * page_size;

  if (dbf->read_write)
    prot |= PROT_WRITE;
  
  p = mmap (NULL, dbf->mapped_size, prot, MAP_SHARED | GDBM_MMAP_FLAGS,
	    dbf->desc, dbf->mapped_off);
  if (p == MAP_FAILED)
    {
      dbf->mapped_region = NULL;
      GDBM_SET_ERRNO (dbf, GDBM_MALLOC_ERROR, FALSE);
      return -1;
    }
  
  dbf->mapped_region = p;
  return 0;
}

# define _REMAP_DEFAULT 0
# define _REMAP_EXTEND  1
# define _REMAP_END     2

/* Remap the GDBM file so that its mapped region ends on SIZEth byte.
   If the file is opened with write permissions, FLAG controls how
   it is expanded.  The value _REMAP_DEFAULT truncates SIZE to the
   actual file size.  The value _REMAP_EXTEND extends the file, if
   necessary, to accomodate max(SIZE,dbf->header->next_block) bytes.
   Finally, the value _REMAP_END instructs the function to use 
   max(SIZE, file_size) as the upper bound of the mapped region.

   If the file is opened read-only, FLAG is ignored and SIZE is
   truncated to the actual file size.

   The upper bound obtained that way is used as a *hint* to select
   the actual size of the mapped region. which can never exceed
   dbf->mapped_size_max.
   
   The function returns 0 on success, -1 on failure. */
int
_gdbm_mapped_remap (GDBM_FILE dbf, off_t size, int flag)
{
  off_t file_size, pos;

  if (size < 0)
    {
      errno = EINVAL;
      GDBM_SET_ERRNO (dbf, GDBM_FILE_SEEK_ERROR, TRUE);
      return -1;
    }

  if (size < dbf->mapped_size)
    /* Nothing to do */
    return 0;
  
  if (_gdbm_file_size (dbf, &file_size))
    {
      SAVE_ERRNO (_gdbm_mapped_unmap (dbf));
      return -1; 
    }

  if (flag == _REMAP_END && size < file_size)
    size = file_size;
  
  if (dbf->read_write)
    {
      if (size > file_size)
	{
	  if (flag != _REMAP_DEFAULT)
	    {
	      if (size < dbf->header->next_block)
		size = dbf->header->next_block;
	      if (_gdbm_file_extend (dbf, size))
		return -1;
	      file_size = size;
	    }
	  else
	    {
	      return 0;
	    }
	}
    }
  else
    {
      if (size > file_size)
	size = file_size;
      
      if (size == SUM_FILE_SIZE (dbf, 0))
	return 0;
    }

  pos = _GDBM_MMAPPED_POS (dbf);
  if (size > dbf->mapped_size_max)
    {
      dbf->mapped_off = pos;
      dbf->mapped_pos = 0;
      size = dbf->mapped_size_max;
      if (dbf->mapped_off + size > file_size)
	size = file_size - dbf->mapped_off;
    }
  else
    {
      dbf->mapped_pos += dbf->mapped_off;
      dbf->mapped_off = 0;
    }
  if (pos > file_size)
    {
      errno = EINVAL;
      GDBM_SET_ERRNO (dbf, GDBM_FILE_SEEK_ERROR, TRUE);
      return -1;
    }
  return _gdbm_internal_remap (dbf, size);
}

/* Initialize mapping system. If the file size is less than MAPPED_SIZE_MAX,
   map the entire file into the memory. Otherwise, map first MAPPED_SIZE_MAX
   bytes. */
int
_gdbm_mapped_init (GDBM_FILE dbf)
{
  if (dbf->mapped_size_max == 0)
    dbf->mapped_size_max = SIZE_T_MAX;
  return _gdbm_mapped_remap (dbf, 0, _REMAP_END);
}

/* Read LEN bytes from the GDBM file DBF into BUFFER. If mmapping is
   not initialized or if it fails, fall back to the classical read(1).
   Return number of bytes read or -1 on failure. */
ssize_t
_gdbm_mapped_read (GDBM_FILE dbf, void *buffer, size_t len)
{
  if (dbf->memory_mapping)
    {
      ssize_t total = 0;
      char *cbuf = buffer;
      
      while (len)
	{
	  size_t nbytes;

	  if (_GDBM_NEED_REMAP (dbf))
	    {
	      off_t pos = _GDBM_MMAPPED_POS (dbf);
	      if (_gdbm_mapped_remap (dbf, SUM_FILE_SIZE (dbf, len),
				      _REMAP_DEFAULT))
		{
		  int rc;

		  if (dbf->need_recovery)
		    return -1;

		  /* Disable memory mapping and retry */
		  dbf->memory_mapping = FALSE;
		  if (lseek (dbf->desc, pos, SEEK_SET) != pos)
		    return total > 0 ? total : -1;
		  rc = read (dbf->desc, cbuf, len);
		  if (rc == -1)
		    return total > 0 ? total : -1;
		  return total + rc;
		}
	    }

	  nbytes = dbf->mapped_size - dbf->mapped_pos;
	  if (nbytes == 0)
	    break;
	  if (nbytes > len)
	    nbytes = len;

	  memcpy (cbuf, (char*) dbf->mapped_region + dbf->mapped_pos, nbytes);
	  cbuf += nbytes;
	  dbf->mapped_pos += nbytes;
	  total += nbytes;
	  len -= nbytes;
	}
      return total;
    }
  return read (dbf->desc, buffer, len);
}

/* Write LEN bytes from BUFFER to the GDBM file DBF. If mmapping is
   not initialized or if it fails, fall back to the classical write(1).
   Return number of bytes written or -1 on failure. */
ssize_t
_gdbm_mapped_write (GDBM_FILE dbf, void *buffer, size_t len)
{
  if (dbf->memory_mapping)
    {
      ssize_t total = 0;
      char *cbuf = buffer;

      while (len)
	{
	  size_t nbytes;

	  if (_GDBM_NEED_REMAP (dbf))
	    {
	      off_t pos = _GDBM_MMAPPED_POS (dbf);
	      if (_gdbm_mapped_remap (dbf, SUM_FILE_SIZE (dbf, len),
				      _REMAP_EXTEND))
		{
		  int rc;

		  if (dbf->need_recovery)
		    return -1;

		  dbf->memory_mapping = FALSE;
		  if (lseek (dbf->desc, pos, SEEK_SET) != pos)
		    return total > 0 ? total : -1;
		  rc = write (dbf->desc, cbuf, len);
		  if (rc == -1)
		    return total > 0 ? total : -1;
		  return total + rc;
		}
	    }

	  nbytes = dbf->mapped_size - dbf->mapped_pos;
	  if (nbytes == 0)
	    break;
	  if (nbytes > len)
	    nbytes = len;

	  memcpy ((char*) dbf->mapped_region + dbf->mapped_pos, cbuf, nbytes);
	  cbuf += nbytes;
	  dbf->mapped_pos += nbytes;
	  total += nbytes;
	  len -= nbytes;
	}
      return total;
    }
  return write (dbf->desc, buffer, len);
}

/* Seek to the offset OFFSET in the GDBM file DBF, according to the
   lseek(1)-style directive WHENCE. Return the resulting absolute
   offset or -1 in case of failure. If mmapping is not initialized or
   if it fails, fall back to the classical lseek(1).

   Return number of bytes written or -1 on failure. */
   
off_t
_gdbm_mapped_lseek (GDBM_FILE dbf, off_t offset, int whence)
{
  if (dbf->memory_mapping)
    {
      off_t needle;
      
      switch (whence)
	{
	case SEEK_SET:
	  needle = offset;
	  break;
	  
	case SEEK_CUR:
	  needle = offset + _GDBM_MMAPPED_POS (dbf);
	  break;
	  
	case SEEK_END:
	  {
 	    off_t file_size;
	    if (_gdbm_file_size (dbf, &file_size))
	      return -1;
	    needle = file_size - offset; 
	    break;
	  }

	default:
	  errno = EINVAL;
	  return -1;
	}

      if (needle < 0)
	{
	  errno = EINVAL;
	  return -1;
	}
      
      if (!_GDBM_IN_MAPPED_REGION_P (dbf, needle))
	{
	  _gdbm_mapped_unmap (dbf);
	  dbf->mapped_off = needle;
	  dbf->mapped_pos = 0;
	}
      else
	dbf->mapped_pos = needle - dbf->mapped_off;
      return needle;
    }
  return lseek (dbf->desc, offset, whence);
}

/* Sync the mapped region to disk. */
int
_gdbm_mapped_sync (GDBM_FILE dbf)
{
  int rc;
  
  if (dbf->mapped_region)
    rc = msync (dbf->mapped_region, dbf->mapped_size,
		MS_SYNC | MS_INVALIDATE);
  else
    rc = fsync (dbf->desc);
  if (rc)
    GDBM_SET_ERRNO (dbf, GDBM_FILE_SYNC_ERROR, TRUE);
  return rc;
}

#endif

Return to:

Send suggestions and report system problems to the System administrator.