1 /** @file mdb.c
2 * @brief Lightning memory-mapped database library
3 *
4 * A Btree-based database management library modeled loosely on the
5 * BerkeleyDB API, but much simplified.
6 */
7 /*
8 * Copyright 2011-2019 Howard Chu, Symas Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
13 * Public License.
14 *
15 * A copy of this license is available in the file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
18 *
19 * This code is derived from btree.c written by Martin Hedenfalk.
20 *
21 * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
22 *
23 * Permission to use, copy, modify, and distribute this software for any
24 * purpose with or without fee is hereby granted, provided that the above
25 * copyright notice and this permission notice appear in all copies.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
28 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
29 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
30 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
32 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
33 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 */
35 #ifndef _GNU_SOURCE
36 #define _GNU_SOURCE 1
37 #endif
38 #if defined(__WIN64__)
39 #define _FILE_OFFSET_BITS 64
40 #endif
41 #ifdef _WIN32
42 #include <malloc.h>
43 #include <windows.h>
44 #include <wchar.h> /* get wcscpy() */
45
46 /** getpid() returns int; MinGW defines pid_t but MinGW64 typedefs it
47 * as int64 which is wrong. MSVC doesn't define it at all, so just
48 * don't use it.
49 */
50 #define MDB_PID_T int
51 #define MDB_THR_T DWORD
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #ifdef __GNUC__
55 # include <sys/param.h>
56 #else
57 # define LITTLE_ENDIAN 1234
58 # define BIG_ENDIAN 4321
59 # define BYTE_ORDER LITTLE_ENDIAN
60 # ifndef SSIZE_MAX
61 # define SSIZE_MAX INT_MAX
62 # endif
63 #endif
64 #else
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #define MDB_PID_T pid_t
68 #define MDB_THR_T pthread_t
69 #include <sys/param.h>
70 #include <sys/uio.h>
71 #include <sys/mman.h>
72 #ifdef HAVE_SYS_FILE_H
73 #include <sys/file.h>
74 #endif
75 #include <fcntl.h>
76 #endif
77
78 #if defined(__mips) && defined(__linux)
79 /* MIPS has cache coherency issues, requires explicit cache control */
80 #include <sys/cachectl.h>
81 #define CACHEFLUSH(addr, bytes, cache) cacheflush(addr, bytes, cache)
82 #else
83 #define CACHEFLUSH(addr, bytes, cache)
84 #endif
85
86 #if defined(__linux) && !defined(MDB_FDATASYNC_WORKS)
87 /** fdatasync is broken on ext3/ext4fs on older kernels, see
88 * description in #mdb_env_open2 comments. You can safely
89 * define MDB_FDATASYNC_WORKS if this code will only be run
90 * on kernels 3.6 and newer.
91 */
92 #define BROKEN_FDATASYNC
93 #endif
94
95 #include <errno.h>
96 #include <limits.h>
97 #include <stddef.h>
98 #include <inttypes.h>
99 #include <stdio.h>
100 #include <stdlib.h>
101 #include <string.h>
102 #include <time.h>
103
104 #ifdef _MSC_VER
105 #include <io.h>
106 typedef SSIZE_T ssize_t;
107 #else
108 #include <unistd.h>
109 #endif
110
111 #if defined(__sun) || defined(__ANDROID__)
112 /* Most platforms have posix_memalign, older may only have memalign */
113 #define HAVE_MEMALIGN 1
114 #include <malloc.h>
115 /* On Solaris, we need the POSIX sigwait function */
116 #if defined (__sun)
117 # define _POSIX_PTHREAD_SEMANTICS 1
118 #endif
119 #endif
120
121 #if !(defined(BYTE_ORDER) || defined(__BYTE_ORDER))
122 #include <netinet/in.h>
123 #include <resolv.h> /* defines BYTE_ORDER on HPUX and Solaris */
124 #endif
125
126 #if defined(__APPLE__) || defined (BSD) || defined(__FreeBSD_kernel__)
127 # define MDB_USE_POSIX_SEM 1
128 # define MDB_FDATASYNC fsync
129 #elif defined(__ANDROID__)
130 # define MDB_FDATASYNC fsync
131 #endif
132
133 #ifndef _WIN32
134 #include <pthread.h>
135 #include <signal.h>
136 #ifdef MDB_USE_POSIX_SEM
137 # define MDB_USE_HASH 1
138 #include <semaphore.h>
139 #else
140 #define MDB_USE_POSIX_MUTEX 1
141 #endif
142 #endif
143
144 #if defined(_WIN32) + defined(MDB_USE_POSIX_SEM) \
145 + defined(MDB_USE_POSIX_MUTEX) != 1
146 # error "Ambiguous shared-lock implementation"
147 #endif
148
149 #ifdef USE_VALGRIND
150 #include <valgrind/memcheck.h>
151 #define VGMEMP_CREATE(h,r,z) VALGRIND_CREATE_MEMPOOL(h,r,z)
152 #define VGMEMP_ALLOC(h,a,s) VALGRIND_MEMPOOL_ALLOC(h,a,s)
153 #define VGMEMP_FREE(h,a) VALGRIND_MEMPOOL_FREE(h,a)
154 #define VGMEMP_DESTROY(h) VALGRIND_DESTROY_MEMPOOL(h)
155 #define VGMEMP_DEFINED(a,s) VALGRIND_MAKE_MEM_DEFINED(a,s)
156 #else
157 #define VGMEMP_CREATE(h,r,z)
158 #define VGMEMP_ALLOC(h,a,s)
159 #define VGMEMP_FREE(h,a)
160 #define VGMEMP_DESTROY(h)
161 #define VGMEMP_DEFINED(a,s)
162 #endif
163
164 #ifndef BYTE_ORDER
165 # if (defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN)) && !(defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN))
166 /* Solaris just defines one or the other */
167 # define LITTLE_ENDIAN 1234
168 # define BIG_ENDIAN 4321
169 # ifdef _LITTLE_ENDIAN
170 # define BYTE_ORDER LITTLE_ENDIAN
171 # else
172 # define BYTE_ORDER BIG_ENDIAN
173 # endif
174 # else
175 # define BYTE_ORDER __BYTE_ORDER
176 # endif
177 #endif
178
179 #ifndef LITTLE_ENDIAN
180 #define LITTLE_ENDIAN __LITTLE_ENDIAN
181 #endif
182 #ifndef BIG_ENDIAN
183 #define BIG_ENDIAN __BIG_ENDIAN
184 #endif
185
186 #if defined(__i386) || defined(__x86_64) || defined(_M_IX86)
187 #define MISALIGNED_OK 1
188 #endif
189
190 #include "lmdb.h"
191 #include "midl.h"
192
193 #if (BYTE_ORDER == LITTLE_ENDIAN) == (BYTE_ORDER == BIG_ENDIAN)
194 # error "Unknown or unsupported endianness (BYTE_ORDER)"
195 #elif (-6 & 5) || CHAR_BIT != 8 || UINT_MAX < 0xffffffff || ULONG_MAX % 0xFFFF
196 # error "Two's complement, reasonably sized integer types, please"
197 #endif
198
199 #if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 2) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
200 /** Mark infrequently used env functions as cold. This puts them in a separate
201 * section, and optimizes them for size */
202 #define ESECT __attribute__ ((cold))
203 #else
204 #define ESECT
205 #endif
206
207 #ifdef _WIN32
208 #define CALL_CONV WINAPI
209 #else
210 #define CALL_CONV
211 #endif
212
213 /** @defgroup internal LMDB Internals
214 * @{
215 */
216 /** @defgroup compat Compatibility Macros
217 * A bunch of macros to minimize the amount of platform-specific ifdefs
218 * needed throughout the rest of the code. When the features this library
219 * needs are similar enough to POSIX to be hidden in a one-or-two line
220 * replacement, this macro approach is used.
221 * @{
222 */
223
224 /** Features under development */
225 #ifndef MDB_DEVEL
226 #define MDB_DEVEL 0
227 #endif
228
229 /** Wrapper around __func__, which is a C99 feature */
230 #if __STDC_VERSION__ >= 199901L
231 # define mdb_func_ __func__
232 #elif __GNUC__ >= 2 || _MSC_VER >= 1300
233 # define mdb_func_ __FUNCTION__
234 #else
235 /* If a debug message says <mdb_unknown>(), update the #if statements above */
236 # define mdb_func_ "<mdb_unknown>"
237 #endif
238
239 /* Internal error codes, not exposed outside liblmdb */
240 #define MDB_NO_ROOT (MDB_LAST_ERRCODE + 10)
241 #ifdef _WIN32
242 #define MDB_OWNERDEAD ((int) WAIT_ABANDONED)
243 #elif defined(MDB_USE_POSIX_MUTEX) && defined(EOWNERDEAD)
244 #define MDB_OWNERDEAD EOWNERDEAD /**< #LOCK_MUTEX0() result if dead owner */
245 #endif
246
247 #ifdef __GLIBC__
248 #define GLIBC_VER ((__GLIBC__ << 16 )| __GLIBC_MINOR__)
249 #endif
250 /** Some platforms define the EOWNERDEAD error code
251 * even though they don't support Robust Mutexes.
252 * Compile with -DMDB_USE_ROBUST=0, or use some other
253 * mechanism like -DMDB_USE_POSIX_SEM instead of
254 * -DMDB_USE_POSIX_MUTEX.
255 * (Posix semaphores are not robust.)
256 */
257 #ifndef MDB_USE_ROBUST
258 /* Android currently lacks Robust Mutex support. So does glibc < 2.4. */
259 # if defined(MDB_USE_POSIX_MUTEX) && (defined(__ANDROID__) || \
260 (defined(__GLIBC__) && GLIBC_VER < 0x020004))
261 # define MDB_USE_ROBUST 0
262 # else
263 # define MDB_USE_ROBUST 1
264 # endif
265 #endif /* !MDB_USE_ROBUST */
266
267 #if defined(MDB_USE_POSIX_MUTEX) && (MDB_USE_ROBUST)
268 /* glibc < 2.12 only provided _np API */
269 # if (defined(__GLIBC__) && GLIBC_VER < 0x02000c) || \
270 (defined(PTHREAD_MUTEX_ROBUST_NP) && !defined(PTHREAD_MUTEX_ROBUST))
271 # define PTHREAD_MUTEX_ROBUST PTHREAD_MUTEX_ROBUST_NP
272 # define pthread_mutexattr_setrobust(attr, flag) pthread_mutexattr_setrobust_np(attr, flag)
273 # define pthread_mutex_consistent(mutex) pthread_mutex_consistent_np(mutex)
274 # endif
275 #endif /* MDB_USE_POSIX_MUTEX && MDB_USE_ROBUST */
276
277 #if defined(MDB_OWNERDEAD) && (MDB_USE_ROBUST)
278 #define MDB_ROBUST_SUPPORTED 1
279 #endif
280
281 #ifdef _WIN32
282 #define MDB_USE_HASH 1
283 #define MDB_PIDLOCK 0
284 #define THREAD_RET DWORD
285 #define pthread_t HANDLE
286 #define pthread_mutex_t HANDLE
287 #define pthread_cond_t HANDLE
288 typedef HANDLE mdb_mutex_t, mdb_mutexref_t;
289 #define pthread_key_t DWORD
290 #define pthread_self() GetCurrentThreadId()
291 #define pthread_key_create(x,y) \
292 ((*(x) = TlsAlloc()) == TLS_OUT_OF_INDEXES ? ErrCode() : 0)
293 #define pthread_key_delete(x) TlsFree(x)
294 #define pthread_getspecific(x) TlsGetValue(x)
295 #define pthread_setspecific(x,y) (TlsSetValue(x,y) ? 0 : ErrCode())
296 #define pthread_mutex_unlock(x) ReleaseMutex(*x)
297 #define pthread_mutex_lock(x) WaitForSingleObject(*x, INFINITE)
298 #define pthread_cond_signal(x) SetEvent(*x)
299 #define pthread_cond_wait(cond,mutex) do{SignalObjectAndWait(*mutex, *cond, INFINITE, FALSE); WaitForSingleObject(*mutex, INFINITE);}while(0)
300 #define THREAD_CREATE(thr,start,arg) \
301 (((thr) = CreateThread(NULL, 0, start, arg, 0, NULL)) ? 0 : ErrCode())
302 #define THREAD_FINISH(thr) \
303 (WaitForSingleObject(thr, INFINITE) ? ErrCode() : 0)
304 #define LOCK_MUTEX0(mutex) WaitForSingleObject(mutex, INFINITE)
305 #define UNLOCK_MUTEX(mutex) ReleaseMutex(mutex)
306 #define mdb_mutex_consistent(mutex) 0
307 #define getpid() GetCurrentProcessId()
308 #define MDB_FDATASYNC(fd) (!FlushFileBuffers(fd))
309 #define MDB_MSYNC(addr,len,flags) (!FlushViewOfFile(addr,len))
310 #define ErrCode() GetLastError()
311 #define GET_PAGESIZE(x) {SYSTEM_INFO si; GetSystemInfo(&si); (x) = si.dwPageSize;}
312 #define close(fd) (CloseHandle(fd) ? 0 : -1)
313 #define munmap(ptr,len) UnmapViewOfFile(ptr)
314 #ifdef PROCESS_QUERY_LIMITED_INFORMATION
315 #define MDB_PROCESS_QUERY_LIMITED_INFORMATION PROCESS_QUERY_LIMITED_INFORMATION
316 #else
317 #define MDB_PROCESS_QUERY_LIMITED_INFORMATION 0x1000
318 #endif
319 #define Z "I"
320 #else
321 #define THREAD_RET void *
322 #define THREAD_CREATE(thr,start,arg) pthread_create(&thr,NULL,start,arg)
323 #define THREAD_FINISH(thr) pthread_join(thr,NULL)
324 #define Z "z" /**< printf format modifier for size_t */
325
326 /** For MDB_LOCK_FORMAT: True if readers take a pid lock in the lockfile */
327 #define MDB_PIDLOCK 1
328
329 #ifdef MDB_USE_POSIX_SEM
330
331 typedef sem_t *mdb_mutex_t, *mdb_mutexref_t;
332 #define LOCK_MUTEX0(mutex) mdb_sem_wait(mutex)
333 #define UNLOCK_MUTEX(mutex) sem_post(mutex)
334
335 static int
mdb_sem_wait(sem_t * sem)336 mdb_sem_wait(sem_t *sem)
337 {
338 int rc;
339 while ((rc = sem_wait(sem)) && (rc = errno) == EINTR) ;
340 return rc;
341 }
342
343 #else /* MDB_USE_POSIX_MUTEX: */
344 /** Shared mutex/semaphore as the original is stored.
345 *
346 * Not for copies. Instead it can be assigned to an #mdb_mutexref_t.
347 * When mdb_mutexref_t is a pointer and mdb_mutex_t is not, then it
348 * is array[size 1] so it can be assigned to the pointer.
349 */
350 typedef pthread_mutex_t mdb_mutex_t[1];
351 /** Reference to an #mdb_mutex_t */
352 typedef pthread_mutex_t *mdb_mutexref_t;
353 /** Lock the reader or writer mutex.
354 * Returns 0 or a code to give #mdb_mutex_failed(), as in #LOCK_MUTEX().
355 */
356 #define LOCK_MUTEX0(mutex) pthread_mutex_lock(mutex)
357 /** Unlock the reader or writer mutex.
358 */
359 #define UNLOCK_MUTEX(mutex) pthread_mutex_unlock(mutex)
360 /** Mark mutex-protected data as repaired, after death of previous owner.
361 */
362 #define mdb_mutex_consistent(mutex) pthread_mutex_consistent(mutex)
363 #endif /* MDB_USE_POSIX_SEM */
364
365 /** Get the error code for the last failed system function.
366 */
367 #define ErrCode() errno
368
369 /** An abstraction for a file handle.
370 * On POSIX systems file handles are small integers. On Windows
371 * they're opaque pointers.
372 */
373 #define HANDLE int
374
375 /** A value for an invalid file handle.
376 * Mainly used to initialize file variables and signify that they are
377 * unused.
378 */
379 #define INVALID_HANDLE_VALUE (-1)
380
381 /** Get the size of a memory page for the system.
382 * This is the basic size that the platform's memory manager uses, and is
383 * fundamental to the use of memory-mapped files.
384 */
385 #define GET_PAGESIZE(x) ((x) = sysconf(_SC_PAGE_SIZE))
386 #endif
387
388 #if defined(_WIN32) || defined(MDB_USE_POSIX_SEM)
389 #define MNAME_LEN 32
390 #else
391 #define MNAME_LEN (sizeof(pthread_mutex_t))
392 #endif
393
394 /** @} */
395
396 #ifdef MDB_ROBUST_SUPPORTED
397 /** Lock mutex, handle any error, set rc = result.
398 * Return 0 on success, nonzero (not rc) on error.
399 */
400 #define LOCK_MUTEX(rc, env, mutex) \
401 (((rc) = LOCK_MUTEX0(mutex)) && \
402 ((rc) = mdb_mutex_failed(env, mutex, rc)))
403 static int mdb_mutex_failed(MDB_env *env, mdb_mutexref_t mutex, int rc);
404 #else
405 #define LOCK_MUTEX(rc, env, mutex) ((rc) = LOCK_MUTEX0(mutex))
406 #define mdb_mutex_failed(env, mutex, rc) (rc)
407 #endif
408
409 #ifndef _WIN32
410 /** A flag for opening a file and requesting synchronous data writes.
411 * This is only used when writing a meta page. It's not strictly needed;
412 * we could just do a normal write and then immediately perform a flush.
413 * But if this flag is available it saves us an extra system call.
414 *
415 * @note If O_DSYNC is undefined but exists in /usr/include,
416 * preferably set some compiler flag to get the definition.
417 */
418 #ifndef MDB_DSYNC
419 # ifdef O_DSYNC
420 # define MDB_DSYNC O_DSYNC
421 # else
422 # define MDB_DSYNC O_SYNC
423 # endif
424 #endif
425 #endif
426
427 /** Function for flushing the data of a file. Define this to fsync
428 * if fdatasync() is not supported.
429 */
430 #ifndef MDB_FDATASYNC
431 # define MDB_FDATASYNC fdatasync
432 #endif
433
434 #ifndef MDB_MSYNC
435 # define MDB_MSYNC(addr,len,flags) msync(addr,len,flags)
436 #endif
437
438 #ifndef MS_SYNC
439 #define MS_SYNC 1
440 #endif
441
442 #ifndef MS_ASYNC
443 #define MS_ASYNC 0
444 #endif
445
446 /** A page number in the database.
447 * Note that 64 bit page numbers are overkill, since pages themselves
448 * already represent 12-13 bits of addressable memory, and the OS will
449 * always limit applications to a maximum of 63 bits of address space.
450 *
451 * @note In the #MDB_node structure, we only store 48 bits of this value,
452 * which thus limits us to only 60 bits of addressable data.
453 */
454 typedef MDB_ID pgno_t;
455
456 /** A transaction ID.
457 * See struct MDB_txn.mt_txnid for details.
458 */
459 typedef MDB_ID txnid_t;
460
461 /** @defgroup debug Debug Macros
462 * @{
463 */
464 #ifndef MDB_DEBUG
465 /** Enable debug output. Needs variable argument macros (a C99 feature).
466 * Set this to 1 for copious tracing. Set to 2 to add dumps of all IDLs
467 * read from and written to the database (used for free space management).
468 */
469 #define MDB_DEBUG 0
470 #endif
471
472 #if MDB_DEBUG
473 static int mdb_debug;
474 static txnid_t mdb_debug_start;
475
476 /** Print a debug message with printf formatting.
477 * Requires double parenthesis around 2 or more args.
478 */
479 # define DPRINTF(args) ((void) ((mdb_debug) && DPRINTF0 args))
480 # define DPRINTF0(fmt, ...) \
481 fprintf(stderr, "%s:%d " fmt "\n", mdb_func_, __LINE__, __VA_ARGS__)
482 #else
483 # define DPRINTF(args) ((void) 0)
484 #endif
485 /** Print a debug string.
486 * The string is printed literally, with no format processing.
487 */
488 #define DPUTS(arg) DPRINTF(("%s", arg))
489 /** Debuging output value of a cursor DBI: Negative in a sub-cursor. */
490 #define DDBI(mc) \
491 (((mc)->mc_flags & C_SUB) ? -(int)(mc)->mc_dbi : (int)(mc)->mc_dbi)
492 /** @} */
493
494 /** @brief The maximum size of a database page.
495 *
496 * It is 32k or 64k, since value-PAGEBASE must fit in
497 * #MDB_page.%mp_upper.
498 *
499 * LMDB will use database pages < OS pages if needed.
500 * That causes more I/O in write transactions: The OS must
501 * know (read) the whole page before writing a partial page.
502 *
503 * Note that we don't currently support Huge pages. On Linux,
504 * regular data files cannot use Huge pages, and in general
505 * Huge pages aren't actually pageable. We rely on the OS
506 * demand-pager to read our data and page it out when memory
507 * pressure from other processes is high. So until OSs have
508 * actual paging support for Huge pages, they're not viable.
509 */
510 #define MAX_PAGESIZE (PAGEBASE ? 0x10000 : 0x8000)
511
512 /** The minimum number of keys required in a database page.
513 * Setting this to a larger value will place a smaller bound on the
514 * maximum size of a data item. Data items larger than this size will
515 * be pushed into overflow pages instead of being stored directly in
516 * the B-tree node. This value used to default to 4. With a page size
517 * of 4096 bytes that meant that any item larger than 1024 bytes would
518 * go into an overflow page. That also meant that on average 2-3KB of
519 * each overflow page was wasted space. The value cannot be lower than
520 * 2 because then there would no longer be a tree structure. With this
521 * value, items larger than 2KB will go into overflow pages, and on
522 * average only 1KB will be wasted.
523 */
524 #define MDB_MINKEYS 2
525
526 /** A stamp that identifies a file as an LMDB file.
527 * There's nothing special about this value other than that it is easily
528 * recognizable, and it will reflect any byte order mismatches.
529 */
530 #define MDB_MAGIC 0xBEEFC0DE
531
532 /** The version number for a database's datafile format. */
533 #define MDB_DATA_VERSION ((MDB_DEVEL) ? 999 : 1)
534 /** The version number for a database's lockfile format. */
535 #define MDB_LOCK_VERSION 1
536
537 /** @brief The max size of a key we can write, or 0 for computed max.
538 *
539 * This macro should normally be left alone or set to 0.
540 * Note that a database with big keys or dupsort data cannot be
541 * reliably modified by a liblmdb which uses a smaller max.
542 * The default is 511 for backwards compat, or 0 when #MDB_DEVEL.
543 *
544 * Other values are allowed, for backwards compat. However:
545 * A value bigger than the computed max can break if you do not
546 * know what you are doing, and liblmdb <= 0.9.10 can break when
547 * modifying a DB with keys/dupsort data bigger than its max.
548 *
549 * Data items in an #MDB_DUPSORT database are also limited to
550 * this size, since they're actually keys of a sub-DB. Keys and
551 * #MDB_DUPSORT data items must fit on a node in a regular page.
552 */
553 #ifndef MDB_MAXKEYSIZE
554 #define MDB_MAXKEYSIZE ((MDB_DEVEL) ? 0 : 511)
555 #endif
556
557 /** The maximum size of a key we can write to the environment. */
558 #if MDB_MAXKEYSIZE
559 #define ENV_MAXKEY(env) (MDB_MAXKEYSIZE)
560 #else
561 #define ENV_MAXKEY(env) ((env)->me_maxkey)
562 #endif
563
564 /** @brief The maximum size of a data item.
565 *
566 * We only store a 32 bit value for node sizes.
567 */
568 #define MAXDATASIZE 0xffffffffUL
569
570 #if MDB_DEBUG
571 /** Key size which fits in a #DKBUF.
572 * @ingroup debug
573 */
574 #define DKBUF_MAXKEYSIZE ((MDB_MAXKEYSIZE) > 0 ? (MDB_MAXKEYSIZE) : 511)
575 /** A key buffer.
576 * @ingroup debug
577 * This is used for printing a hex dump of a key's contents.
578 */
579 #define DKBUF char kbuf[DKBUF_MAXKEYSIZE*2+1]
580 /** Display a key in hex.
581 * @ingroup debug
582 * Invoke a function to display a key in hex.
583 */
584 #define DKEY(x) mdb_dkey(x, kbuf)
585 #else
586 #define DKBUF
587 #define DKEY(x) 0
588 #endif
589
590 /** An invalid page number.
591 * Mainly used to denote an empty tree.
592 */
593 #define P_INVALID (~(pgno_t)0)
594
595 /** Test if the flags \b f are set in a flag word \b w. */
596 #define F_ISSET(w, f) (((w) & (f)) == (f))
597
598 /** Round \b n up to an even number. */
599 #define EVEN(n) (((n) + 1U) & -2) /* sign-extending -2 to match n+1U */
600
601 /** Used for offsets within a single page.
602 * Since memory pages are typically 4 or 8KB in size, 12-13 bits,
603 * this is plenty.
604 */
605 typedef uint16_t indx_t;
606
607 /** Default size of memory map.
608 * This is certainly too small for any actual applications. Apps should always set
609 * the size explicitly using #mdb_env_set_mapsize().
610 */
611 #define DEFAULT_MAPSIZE 1048576
612
613 /** @defgroup readers Reader Lock Table
614 * Readers don't acquire any locks for their data access. Instead, they
615 * simply record their transaction ID in the reader table. The reader
616 * mutex is needed just to find an empty slot in the reader table. The
617 * slot's address is saved in thread-specific data so that subsequent read
618 * transactions started by the same thread need no further locking to proceed.
619 *
620 * If #MDB_NOTLS is set, the slot address is not saved in thread-specific data.
621 *
622 * No reader table is used if the database is on a read-only filesystem, or
623 * if #MDB_NOLOCK is set.
624 *
625 * Since the database uses multi-version concurrency control, readers don't
626 * actually need any locking. This table is used to keep track of which
627 * readers are using data from which old transactions, so that we'll know
628 * when a particular old transaction is no longer in use. Old transactions
629 * that have discarded any data pages can then have those pages reclaimed
630 * for use by a later write transaction.
631 *
632 * The lock table is constructed such that reader slots are aligned with the
633 * processor's cache line size. Any slot is only ever used by one thread.
634 * This alignment guarantees that there will be no contention or cache
635 * thrashing as threads update their own slot info, and also eliminates
636 * any need for locking when accessing a slot.
637 *
638 * A writer thread will scan every slot in the table to determine the oldest
639 * outstanding reader transaction. Any freed pages older than this will be
640 * reclaimed by the writer. The writer doesn't use any locks when scanning
641 * this table. This means that there's no guarantee that the writer will
642 * see the most up-to-date reader info, but that's not required for correct
643 * operation - all we need is to know the upper bound on the oldest reader,
644 * we don't care at all about the newest reader. So the only consequence of
645 * reading stale information here is that old pages might hang around a
646 * while longer before being reclaimed. That's actually good anyway, because
647 * the longer we delay reclaiming old pages, the more likely it is that a
648 * string of contiguous pages can be found after coalescing old pages from
649 * many old transactions together.
650 * @{
651 */
652 /** Number of slots in the reader table.
653 * This value was chosen somewhat arbitrarily. 126 readers plus a
654 * couple mutexes fit exactly into 8KB on my development machine.
655 * Applications should set the table size using #mdb_env_set_maxreaders().
656 */
657 #define DEFAULT_READERS 126
658
659 /** The size of a CPU cache line in bytes. We want our lock structures
660 * aligned to this size to avoid false cache line sharing in the
661 * lock table.
662 * This value works for most CPUs. For Itanium this should be 128.
663 */
664 #ifndef CACHELINE
665 #define CACHELINE 64
666 #endif
667
668 /** The information we store in a single slot of the reader table.
669 * In addition to a transaction ID, we also record the process and
670 * thread ID that owns a slot, so that we can detect stale information,
671 * e.g. threads or processes that went away without cleaning up.
672 * @note We currently don't check for stale records. We simply re-init
673 * the table when we know that we're the only process opening the
674 * lock file.
675 */
676 typedef struct MDB_rxbody {
677 /** Current Transaction ID when this transaction began, or (txnid_t)-1.
678 * Multiple readers that start at the same time will probably have the
679 * same ID here. Again, it's not important to exclude them from
680 * anything; all we need to know is which version of the DB they
681 * started from so we can avoid overwriting any data used in that
682 * particular version.
683 */
684 volatile txnid_t mrb_txnid;
685 /** The process ID of the process owning this reader txn. */
686 volatile MDB_PID_T mrb_pid;
687 /** The thread ID of the thread owning this txn. */
688 volatile MDB_THR_T mrb_tid;
689 } MDB_rxbody;
690
691 /** The actual reader record, with cacheline padding. */
692 typedef struct MDB_reader {
693 union {
694 MDB_rxbody mrx;
695 /** shorthand for mrb_txnid */
696 #define mr_txnid mru.mrx.mrb_txnid
697 #define mr_pid mru.mrx.mrb_pid
698 #define mr_tid mru.mrx.mrb_tid
699 /** cache line alignment */
700 char pad[(sizeof(MDB_rxbody)+CACHELINE-1) & ~(CACHELINE-1)];
701 } mru;
702 } MDB_reader;
703
704 /** The header for the reader table.
705 * The table resides in a memory-mapped file. (This is a different file
706 * than is used for the main database.)
707 *
708 * For POSIX the actual mutexes reside in the shared memory of this
709 * mapped file. On Windows, mutexes are named objects allocated by the
710 * kernel; we store the mutex names in this mapped file so that other
711 * processes can grab them. This same approach is also used on
712 * MacOSX/Darwin (using named semaphores) since MacOSX doesn't support
713 * process-shared POSIX mutexes. For these cases where a named object
714 * is used, the object name is derived from a 64 bit FNV hash of the
715 * environment pathname. As such, naming collisions are extremely
716 * unlikely. If a collision occurs, the results are unpredictable.
717 */
718 typedef struct MDB_txbody {
719 /** Stamp identifying this as an LMDB file. It must be set
720 * to #MDB_MAGIC. */
721 uint32_t mtb_magic;
722 /** Format of this lock file. Must be set to #MDB_LOCK_FORMAT. */
723 uint32_t mtb_format;
724 #if defined(_WIN32) || defined(MDB_USE_POSIX_SEM)
725 char mtb_rmname[MNAME_LEN];
726 #else
727 /** Mutex protecting access to this table.
728 * This is the reader table lock used with LOCK_MUTEX().
729 */
730 mdb_mutex_t mtb_rmutex;
731 #endif
732 /** The ID of the last transaction committed to the database.
733 * This is recorded here only for convenience; the value can always
734 * be determined by reading the main database meta pages.
735 */
736 volatile txnid_t mtb_txnid;
737 /** The number of slots that have been used in the reader table.
738 * This always records the maximum count, it is not decremented
739 * when readers release their slots.
740 */
741 volatile unsigned mtb_numreaders;
742 } MDB_txbody;
743
744 /** The actual reader table definition. */
745 typedef struct MDB_txninfo {
746 union {
747 MDB_txbody mtb;
748 #define mti_magic mt1.mtb.mtb_magic
749 #define mti_format mt1.mtb.mtb_format
750 #define mti_rmutex mt1.mtb.mtb_rmutex
751 #define mti_rmname mt1.mtb.mtb_rmname
752 #define mti_txnid mt1.mtb.mtb_txnid
753 #define mti_numreaders mt1.mtb.mtb_numreaders
754 char pad[(sizeof(MDB_txbody)+CACHELINE-1) & ~(CACHELINE-1)];
755 } mt1;
756 union {
757 #if defined(_WIN32) || defined(MDB_USE_POSIX_SEM)
758 char mt2_wmname[MNAME_LEN];
759 #define mti_wmname mt2.mt2_wmname
760 #else
761 mdb_mutex_t mt2_wmutex;
762 #define mti_wmutex mt2.mt2_wmutex
763 #endif
764 char pad[(MNAME_LEN+CACHELINE-1) & ~(CACHELINE-1)];
765 } mt2;
766 MDB_reader mti_readers[1];
767 } MDB_txninfo;
768
769 /** Lockfile format signature: version, features and field layout */
770 #define MDB_LOCK_FORMAT \
771 ((uint32_t) \
772 ((MDB_LOCK_VERSION) \
773 /* Flags which describe functionality */ \
774 + (((MDB_PIDLOCK) != 0) << 16)))
775 /** @} */
776
777 /** Common header for all page types. The page type depends on #mp_flags.
778 *
779 * #P_BRANCH and #P_LEAF pages have unsorted '#MDB_node's at the end, with
780 * sorted #mp_ptrs[] entries referring to them. Exception: #P_LEAF2 pages
781 * omit mp_ptrs and pack sorted #MDB_DUPFIXED values after the page header.
782 *
783 * #P_OVERFLOW records occupy one or more contiguous pages where only the
784 * first has a page header. They hold the real data of #F_BIGDATA nodes.
785 *
786 * #P_SUBP sub-pages are small leaf "pages" with duplicate data.
787 * A node with flag #F_DUPDATA but not #F_SUBDATA contains a sub-page.
788 * (Duplicate data can also go in sub-databases, which use normal pages.)
789 *
790 * #P_META pages contain #MDB_meta, the start point of an LMDB snapshot.
791 *
792 * Each non-metapage up to #MDB_meta.%mm_last_pg is reachable exactly once
793 * in the snapshot: Either used by a database or listed in a freeDB record.
794 */
795 typedef struct MDB_page {
796 #define mp_pgno mp_p.p_pgno
797 #define mp_next mp_p.p_next
798 union {
799 pgno_t p_pgno; /**< page number */
800 struct MDB_page *p_next; /**< for in-memory list of freed pages */
801 } mp_p;
802 uint16_t mp_pad; /**< key size if this is a LEAF2 page */
803 /** @defgroup mdb_page Page Flags
804 * @ingroup internal
805 * Flags for the page headers.
806 * @{
807 */
808 #define P_BRANCH 0x01 /**< branch page */
809 #define P_LEAF 0x02 /**< leaf page */
810 #define P_OVERFLOW 0x04 /**< overflow page */
811 #define P_META 0x08 /**< meta page */
812 #define P_DIRTY 0x10 /**< dirty page, also set for #P_SUBP pages */
813 #define P_LEAF2 0x20 /**< for #MDB_DUPFIXED records */
814 #define P_SUBP 0x40 /**< for #MDB_DUPSORT sub-pages */
815 #define P_LOOSE 0x4000 /**< page was dirtied then freed, can be reused */
816 #define P_KEEP 0x8000 /**< leave this page alone during spill */
817 /** @} */
818 uint16_t mp_flags; /**< @ref mdb_page */
819 #define mp_lower mp_pb.pb.pb_lower
820 #define mp_upper mp_pb.pb.pb_upper
821 #define mp_pages mp_pb.pb_pages
822 union {
823 struct {
824 indx_t pb_lower; /**< lower bound of free space */
825 indx_t pb_upper; /**< upper bound of free space */
826 } pb;
827 uint32_t pb_pages; /**< number of overflow pages */
828 } mp_pb;
829 indx_t mp_ptrs[1]; /**< dynamic size */
830 } MDB_page;
831
832 /** Size of the page header, excluding dynamic data at the end */
833 #define PAGEHDRSZ ((unsigned) offsetof(MDB_page, mp_ptrs))
834
835 /** Address of first usable data byte in a page, after the header */
836 #define METADATA(p) ((void *)((char *)(p) + PAGEHDRSZ))
837
838 /** ITS#7713, change PAGEBASE to handle 65536 byte pages */
839 #define PAGEBASE ((MDB_DEVEL) ? PAGEHDRSZ : 0)
840
841 /** Number of nodes on a page */
842 #define NUMKEYS(p) (((p)->mp_lower - (PAGEHDRSZ-PAGEBASE)) >> 1)
843
844 /** The amount of space remaining in the page */
845 #define SIZELEFT(p) (indx_t)((p)->mp_upper - (p)->mp_lower)
846
847 /** The percentage of space used in the page, in tenths of a percent. */
848 #define PAGEFILL(env, p) (1000L * ((env)->me_psize - PAGEHDRSZ - SIZELEFT(p)) / \
849 ((env)->me_psize - PAGEHDRSZ))
850 /** The minimum page fill factor, in tenths of a percent.
851 * Pages emptier than this are candidates for merging.
852 */
853 #define FILL_THRESHOLD 250
854
855 /** Test if a page is a leaf page */
856 #define IS_LEAF(p) F_ISSET((p)->mp_flags, P_LEAF)
857 /** Test if a page is a LEAF2 page */
858 #define IS_LEAF2(p) F_ISSET((p)->mp_flags, P_LEAF2)
859 /** Test if a page is a branch page */
860 #define IS_BRANCH(p) F_ISSET((p)->mp_flags, P_BRANCH)
861 /** Test if a page is an overflow page */
862 #define IS_OVERFLOW(p) F_ISSET((p)->mp_flags, P_OVERFLOW)
863 /** Test if a page is a sub page */
864 #define IS_SUBP(p) F_ISSET((p)->mp_flags, P_SUBP)
865
866 /** The number of overflow pages needed to store the given size. */
867 #define OVPAGES(size, psize) ((PAGEHDRSZ-1 + (size)) / (psize) + 1)
868
869 /** Link in #MDB_txn.%mt_loose_pgs list.
870 * Kept outside the page header, which is needed when reusing the page.
871 */
872 #define NEXT_LOOSE_PAGE(p) (*(MDB_page **)((p) + 2))
873
874 /** Header for a single key/data pair within a page.
875 * Used in pages of type #P_BRANCH and #P_LEAF without #P_LEAF2.
876 * We guarantee 2-byte alignment for 'MDB_node's.
877 *
878 * #mn_lo and #mn_hi are used for data size on leaf nodes, and for child
879 * pgno on branch nodes. On 64 bit platforms, #mn_flags is also used
880 * for pgno. (Branch nodes have no flags). Lo and hi are in host byte
881 * order in case some accesses can be optimized to 32-bit word access.
882 *
883 * Leaf node flags describe node contents. #F_BIGDATA says the node's
884 * data part is the page number of an overflow page with actual data.
885 * #F_DUPDATA and #F_SUBDATA can be combined giving duplicate data in
886 * a sub-page/sub-database, and named databases (just #F_SUBDATA).
887 */
888 typedef struct MDB_node {
889 /** part of data size or pgno
890 * @{ */
891 #if BYTE_ORDER == LITTLE_ENDIAN
892 unsigned short mn_lo, mn_hi;
893 #else
894 unsigned short mn_hi, mn_lo;
895 #endif
896 /** @} */
897 /** @defgroup mdb_node Node Flags
898 * @ingroup internal
899 * Flags for node headers.
900 * @{
901 */
902 #define F_BIGDATA 0x01 /**< data put on overflow page */
903 #define F_SUBDATA 0x02 /**< data is a sub-database */
904 #define F_DUPDATA 0x04 /**< data has duplicates */
905
906 /** valid flags for #mdb_node_add() */
907 #define NODE_ADD_FLAGS (F_DUPDATA|F_SUBDATA|MDB_RESERVE|MDB_APPEND)
908
909 /** @} */
910 unsigned short mn_flags; /**< @ref mdb_node */
911 unsigned short mn_ksize; /**< key size */
912 char mn_data[1]; /**< key and data are appended here */
913 } MDB_node;
914
915 /** Size of the node header, excluding dynamic data at the end */
916 #define NODESIZE offsetof(MDB_node, mn_data)
917
918 /** Bit position of top word in page number, for shifting mn_flags */
919 #define PGNO_TOPWORD ((pgno_t)-1 > 0xffffffffu ? 32 : 0)
920
921 /** Size of a node in a branch page with a given key.
922 * This is just the node header plus the key, there is no data.
923 */
924 #define INDXSIZE(k) (NODESIZE + ((k) == NULL ? 0 : (k)->mv_size))
925
926 /** Size of a node in a leaf page with a given key and data.
927 * This is node header plus key plus data size.
928 */
929 #define LEAFSIZE(k, d) (NODESIZE + (k)->mv_size + (d)->mv_size)
930
931 /** Address of node \b i in page \b p */
932 #define NODEPTR(p, i) ((MDB_node *)((char *)(p) + (p)->mp_ptrs[i] + PAGEBASE))
933
934 /** Address of the key for the node */
935 #define NODEKEY(node) (void *)((node)->mn_data)
936
937 /** Address of the data for a node */
938 #define NODEDATA(node) (void *)((char *)(node)->mn_data + (node)->mn_ksize)
939
940 /** Get the page number pointed to by a branch node */
941 #define NODEPGNO(node) \
942 ((node)->mn_lo | ((pgno_t) (node)->mn_hi << 16) | \
943 (PGNO_TOPWORD ? ((pgno_t) (node)->mn_flags << PGNO_TOPWORD) : 0))
944 /** Set the page number in a branch node */
945 #define SETPGNO(node,pgno) do { \
946 (node)->mn_lo = (pgno) & 0xffff; (node)->mn_hi = (pgno) >> 16; \
947 if (PGNO_TOPWORD) (node)->mn_flags = (pgno) >> PGNO_TOPWORD; } while(0)
948
949 /** Get the size of the data in a leaf node */
950 #define NODEDSZ(node) ((node)->mn_lo | ((unsigned)(node)->mn_hi << 16))
951 /** Set the size of the data for a leaf node */
952 #define SETDSZ(node,size) do { \
953 (node)->mn_lo = (size) & 0xffff; (node)->mn_hi = (size) >> 16;} while(0)
954 /** The size of a key in a node */
955 #define NODEKSZ(node) ((node)->mn_ksize)
956
957 /** Copy a page number from src to dst */
958 #ifdef MISALIGNED_OK
959 #define COPY_PGNO(dst,src) dst = src
960 #else
961 #if SIZE_MAX > 4294967295UL
962 #define COPY_PGNO(dst,src) do { \
963 unsigned short *s, *d; \
964 s = (unsigned short *)&(src); \
965 d = (unsigned short *)&(dst); \
966 *d++ = *s++; \
967 *d++ = *s++; \
968 *d++ = *s++; \
969 *d = *s; \
970 } while (0)
971 #else
972 #define COPY_PGNO(dst,src) do { \
973 unsigned short *s, *d; \
974 s = (unsigned short *)&(src); \
975 d = (unsigned short *)&(dst); \
976 *d++ = *s++; \
977 *d = *s; \
978 } while (0)
979 #endif
980 #endif
981 /** The address of a key in a LEAF2 page.
982 * LEAF2 pages are used for #MDB_DUPFIXED sorted-duplicate sub-DBs.
983 * There are no node headers, keys are stored contiguously.
984 */
985 #define LEAF2KEY(p, i, ks) ((char *)(p) + PAGEHDRSZ + ((i)*(ks)))
986
987 /** Set the \b node's key into \b keyptr, if requested. */
988 #define MDB_GET_KEY(node, keyptr) { if ((keyptr) != NULL) { \
989 (keyptr)->mv_size = NODEKSZ(node); (keyptr)->mv_data = NODEKEY(node); } }
990
991 /** Set the \b node's key into \b key. */
992 #define MDB_GET_KEY2(node, key) { key.mv_size = NODEKSZ(node); key.mv_data = NODEKEY(node); }
993
994 /** Information about a single database in the environment. */
995 typedef struct MDB_db {
996 uint32_t md_pad; /**< also ksize for LEAF2 pages */
997 uint16_t md_flags; /**< @ref mdb_dbi_open */
998 uint16_t md_depth; /**< depth of this tree */
999 pgno_t md_branch_pages; /**< number of internal pages */
1000 pgno_t md_leaf_pages; /**< number of leaf pages */
1001 pgno_t md_overflow_pages; /**< number of overflow pages */
1002 size_t md_entries; /**< number of data items */
1003 pgno_t md_root; /**< the root page of this tree */
1004 } MDB_db;
1005
1006 #define MDB_VALID 0x8000 /**< DB handle is valid, for me_dbflags */
1007 #define PERSISTENT_FLAGS (0xffff & ~(MDB_VALID))
1008 /** #mdb_dbi_open() flags */
1009 #define VALID_FLAGS (MDB_REVERSEKEY|MDB_DUPSORT|MDB_INTEGERKEY|MDB_DUPFIXED|\
1010 MDB_INTEGERDUP|MDB_REVERSEDUP|MDB_CREATE)
1011
1012 /** Handle for the DB used to track free pages. */
1013 #define FREE_DBI 0
1014 /** Handle for the default DB. */
1015 #define MAIN_DBI 1
1016 /** Number of DBs in metapage (free and main) - also hardcoded elsewhere */
1017 #define CORE_DBS 2
1018
1019 /** Number of meta pages - also hardcoded elsewhere */
1020 #define NUM_METAS 2
1021
1022 /** Meta page content.
1023 * A meta page is the start point for accessing a database snapshot.
1024 * Pages 0-1 are meta pages. Transaction N writes meta page #(N % 2).
1025 */
1026 typedef struct MDB_meta {
1027 /** Stamp identifying this as an LMDB file. It must be set
1028 * to #MDB_MAGIC. */
1029 uint32_t mm_magic;
1030 /** Version number of this file. Must be set to #MDB_DATA_VERSION. */
1031 uint32_t mm_version;
1032 void *mm_address; /**< address for fixed mapping */
1033 size_t mm_mapsize; /**< size of mmap region */
1034 MDB_db mm_dbs[CORE_DBS]; /**< first is free space, 2nd is main db */
1035 /** The size of pages used in this DB */
1036 #define mm_psize mm_dbs[FREE_DBI].md_pad
1037 /** Any persistent environment flags. @ref mdb_env */
1038 #define mm_flags mm_dbs[FREE_DBI].md_flags
1039 /** Last used page in the datafile.
1040 * Actually the file may be shorter if the freeDB lists the final pages.
1041 */
1042 pgno_t mm_last_pg;
1043 volatile txnid_t mm_txnid; /**< txnid that committed this page */
1044 } MDB_meta;
1045
1046 /** Buffer for a stack-allocated meta page.
1047 * The members define size and alignment, and silence type
1048 * aliasing warnings. They are not used directly; that could
1049 * mean incorrectly using several union members in parallel.
1050 */
1051 typedef union MDB_metabuf {
1052 MDB_page mb_page;
1053 struct {
1054 char mm_pad[PAGEHDRSZ];
1055 MDB_meta mm_meta;
1056 } mb_metabuf;
1057 } MDB_metabuf;
1058
1059 /** Auxiliary DB info.
1060 * The information here is mostly static/read-only. There is
1061 * only a single copy of this record in the environment.
1062 */
1063 typedef struct MDB_dbx {
1064 MDB_val md_name; /**< name of the database */
1065 MDB_cmp_func *md_cmp; /**< function for comparing keys */
1066 MDB_cmp_func *md_dcmp; /**< function for comparing data items */
1067 MDB_rel_func *md_rel; /**< user relocate function */
1068 void *md_relctx; /**< user-provided context for md_rel */
1069 } MDB_dbx;
1070
1071 /** A database transaction.
1072 * Every operation requires a transaction handle.
1073 */
1074 struct MDB_txn {
1075 MDB_txn *mt_parent; /**< parent of a nested txn */
1076 /** Nested txn under this txn, set together with flag #MDB_TXN_HAS_CHILD */
1077 MDB_txn *mt_child;
1078 pgno_t mt_next_pgno; /**< next unallocated page */
1079 /** The ID of this transaction. IDs are integers incrementing from 1.
1080 * Only committed write transactions increment the ID. If a transaction
1081 * aborts, the ID may be re-used by the next writer.
1082 */
1083 txnid_t mt_txnid;
1084 MDB_env *mt_env; /**< the DB environment */
1085 /** The list of pages that became unused during this transaction.
1086 */
1087 MDB_IDL mt_free_pgs;
1088 /** The list of loose pages that became unused and may be reused
1089 * in this transaction, linked through #NEXT_LOOSE_PAGE(page).
1090 */
1091 MDB_page *mt_loose_pgs;
1092 /** Number of loose pages (#mt_loose_pgs) */
1093 int mt_loose_count;
1094 /** The sorted list of dirty pages we temporarily wrote to disk
1095 * because the dirty list was full. page numbers in here are
1096 * shifted left by 1, deleted slots have the LSB set.
1097 */
1098 MDB_IDL mt_spill_pgs;
1099 union {
1100 /** For write txns: Modified pages. Sorted when not MDB_WRITEMAP. */
1101 MDB_ID2L dirty_list;
1102 /** For read txns: This thread/txn's reader table slot, or NULL. */
1103 MDB_reader *reader;
1104 } mt_u;
1105 /** Array of records for each DB known in the environment. */
1106 MDB_dbx *mt_dbxs;
1107 /** Array of MDB_db records for each known DB */
1108 MDB_db *mt_dbs;
1109 /** Array of sequence numbers for each DB handle */
1110 unsigned int *mt_dbiseqs;
1111 /** @defgroup mt_dbflag Transaction DB Flags
1112 * @ingroup internal
1113 * @{
1114 */
1115 #define DB_DIRTY 0x01 /**< DB was written in this txn */
1116 #define DB_STALE 0x02 /**< Named-DB record is older than txnID */
1117 #define DB_NEW 0x04 /**< Named-DB handle opened in this txn */
1118 #define DB_VALID 0x08 /**< DB handle is valid, see also #MDB_VALID */
1119 #define DB_USRVALID 0x10 /**< As #DB_VALID, but not set for #FREE_DBI */
1120 #define DB_DUPDATA 0x20 /**< DB is #MDB_DUPSORT data */
1121 /** @} */
1122 /** In write txns, array of cursors for each DB */
1123 MDB_cursor **mt_cursors;
1124 /** Array of flags for each DB */
1125 unsigned char *mt_dbflags;
1126 /** Number of DB records in use, or 0 when the txn is finished.
1127 * This number only ever increments until the txn finishes; we
1128 * don't decrement it when individual DB handles are closed.
1129 */
1130 MDB_dbi mt_numdbs;
1131
1132 /** @defgroup mdb_txn Transaction Flags
1133 * @ingroup internal
1134 * @{
1135 */
1136 /** #mdb_txn_begin() flags */
1137 #define MDB_TXN_BEGIN_FLAGS MDB_RDONLY
1138 #define MDB_TXN_RDONLY MDB_RDONLY /**< read-only transaction */
1139 /* internal txn flags */
1140 #define MDB_TXN_WRITEMAP MDB_WRITEMAP /**< copy of #MDB_env flag in writers */
1141 #define MDB_TXN_FINISHED 0x01 /**< txn is finished or never began */
1142 #define MDB_TXN_ERROR 0x02 /**< txn is unusable after an error */
1143 #define MDB_TXN_DIRTY 0x04 /**< must write, even if dirty list is empty */
1144 #define MDB_TXN_SPILLS 0x08 /**< txn or a parent has spilled pages */
1145 #define MDB_TXN_HAS_CHILD 0x10 /**< txn has an #MDB_txn.%mt_child */
1146 /** most operations on the txn are currently illegal */
1147 #define MDB_TXN_BLOCKED (MDB_TXN_FINISHED|MDB_TXN_ERROR|MDB_TXN_HAS_CHILD)
1148 /** @} */
1149 unsigned int mt_flags; /**< @ref mdb_txn */
1150 /** #dirty_list room: Array size - \#dirty pages visible to this txn.
1151 * Includes ancestor txns' dirty pages not hidden by other txns'
1152 * dirty/spilled pages. Thus commit(nested txn) has room to merge
1153 * dirty_list into mt_parent after freeing hidden mt_parent pages.
1154 */
1155 unsigned int mt_dirty_room;
1156 };
1157
1158 /** Enough space for 2^32 nodes with minimum of 2 keys per node. I.e., plenty.
1159 * At 4 keys per node, enough for 2^64 nodes, so there's probably no need to
1160 * raise this on a 64 bit machine.
1161 */
1162 #define CURSOR_STACK 32
1163
1164 struct MDB_xcursor;
1165
1166 /** Cursors are used for all DB operations.
1167 * A cursor holds a path of (page pointer, key index) from the DB
1168 * root to a position in the DB, plus other state. #MDB_DUPSORT
1169 * cursors include an xcursor to the current data item. Write txns
1170 * track their cursors and keep them up to date when data moves.
1171 * Exception: An xcursor's pointer to a #P_SUBP page can be stale.
1172 * (A node with #F_DUPDATA but no #F_SUBDATA contains a subpage).
1173 */
1174 struct MDB_cursor {
1175 /** Next cursor on this DB in this txn */
1176 MDB_cursor *mc_next;
1177 /** Backup of the original cursor if this cursor is a shadow */
1178 MDB_cursor *mc_backup;
1179 /** Context used for databases with #MDB_DUPSORT, otherwise NULL */
1180 struct MDB_xcursor *mc_xcursor;
1181 /** The transaction that owns this cursor */
1182 MDB_txn *mc_txn;
1183 /** The database handle this cursor operates on */
1184 MDB_dbi mc_dbi;
1185 /** The database record for this cursor */
1186 MDB_db *mc_db;
1187 /** The database auxiliary record for this cursor */
1188 MDB_dbx *mc_dbx;
1189 /** The @ref mt_dbflag for this database */
1190 unsigned char *mc_dbflag;
1191 unsigned short mc_snum; /**< number of pushed pages */
1192 unsigned short mc_top; /**< index of top page, normally mc_snum-1 */
1193 /** @defgroup mdb_cursor Cursor Flags
1194 * @ingroup internal
1195 * Cursor state flags.
1196 * @{
1197 */
1198 #define C_INITIALIZED 0x01 /**< cursor has been initialized and is valid */
1199 #define C_EOF 0x02 /**< No more data */
1200 #define C_SUB 0x04 /**< Cursor is a sub-cursor */
1201 #define C_DEL 0x08 /**< last op was a cursor_del */
1202 #define C_UNTRACK 0x40 /**< Un-track cursor when closing */
1203 /** @} */
1204 unsigned int mc_flags; /**< @ref mdb_cursor */
1205 MDB_page *mc_pg[CURSOR_STACK]; /**< stack of pushed pages */
1206 indx_t mc_ki[CURSOR_STACK]; /**< stack of page indices */
1207 };
1208
1209 /** Context for sorted-dup records.
1210 * We could have gone to a fully recursive design, with arbitrarily
1211 * deep nesting of sub-databases. But for now we only handle these
1212 * levels - main DB, optional sub-DB, sorted-duplicate DB.
1213 */
1214 typedef struct MDB_xcursor {
1215 /** A sub-cursor for traversing the Dup DB */
1216 MDB_cursor mx_cursor;
1217 /** The database record for this Dup DB */
1218 MDB_db mx_db;
1219 /** The auxiliary DB record for this Dup DB */
1220 MDB_dbx mx_dbx;
1221 /** The @ref mt_dbflag for this Dup DB */
1222 unsigned char mx_dbflag;
1223 } MDB_xcursor;
1224
1225 /** Check if there is an inited xcursor */
1226 #define XCURSOR_INITED(mc) \
1227 ((mc)->mc_xcursor && ((mc)->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED))
1228
1229 /** Update the xcursor's sub-page pointer, if any, in \b mc. Needed
1230 * when the node which contains the sub-page may have moved. Called
1231 * with leaf page \b mp = mc->mc_pg[\b top].
1232 */
1233 #define XCURSOR_REFRESH(mc, top, mp) do { \
1234 MDB_page *xr_pg = (mp); \
1235 MDB_node *xr_node; \
1236 if (!XCURSOR_INITED(mc) || (mc)->mc_ki[top] >= NUMKEYS(xr_pg)) break; \
1237 xr_node = NODEPTR(xr_pg, (mc)->mc_ki[top]); \
1238 if ((xr_node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA) \
1239 (mc)->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(xr_node); \
1240 } while (0)
1241
1242 /** State of FreeDB old pages, stored in the MDB_env */
1243 typedef struct MDB_pgstate {
1244 pgno_t *mf_pghead; /**< Reclaimed freeDB pages, or NULL before use */
1245 txnid_t mf_pglast; /**< ID of last used record, or 0 if !mf_pghead */
1246 } MDB_pgstate;
1247
1248 /** The database environment. */
1249 struct MDB_env {
1250 HANDLE me_fd; /**< The main data file */
1251 HANDLE me_lfd; /**< The lock file */
1252 HANDLE me_mfd; /**< For writing and syncing the meta pages */
1253 /** Failed to update the meta page. Probably an I/O error. */
1254 #define MDB_FATAL_ERROR 0x80000000U
1255 /** Some fields are initialized. */
1256 #define MDB_ENV_ACTIVE 0x20000000U
1257 /** me_txkey is set */
1258 #define MDB_ENV_TXKEY 0x10000000U
1259 /** fdatasync is unreliable */
1260 #define MDB_FSYNCONLY 0x08000000U
1261 uint32_t me_flags; /**< @ref mdb_env */
1262 unsigned int me_psize; /**< DB page size, inited from me_os_psize */
1263 unsigned int me_os_psize; /**< OS page size, from #GET_PAGESIZE */
1264 unsigned int me_maxreaders; /**< size of the reader table */
1265 /** Max #MDB_txninfo.%mti_numreaders of interest to #mdb_env_close() */
1266 volatile int me_close_readers;
1267 MDB_dbi me_numdbs; /**< number of DBs opened */
1268 MDB_dbi me_maxdbs; /**< size of the DB table */
1269 MDB_PID_T me_pid; /**< process ID of this env */
1270 char *me_path; /**< path to the DB files */
1271 char *me_map; /**< the memory map of the data file */
1272 MDB_txninfo *me_txns; /**< the memory map of the lock file or NULL */
1273 MDB_meta *me_metas[NUM_METAS]; /**< pointers to the two meta pages */
1274 void *me_pbuf; /**< scratch area for DUPSORT put() */
1275 MDB_txn *me_txn; /**< current write transaction */
1276 MDB_txn *me_txn0; /**< prealloc'd write transaction */
1277 size_t me_mapsize; /**< size of the data memory map */
1278 off_t me_size; /**< current file size */
1279 pgno_t me_maxpg; /**< me_mapsize / me_psize */
1280 MDB_dbx *me_dbxs; /**< array of static DB info */
1281 uint16_t *me_dbflags; /**< array of flags from MDB_db.md_flags */
1282 unsigned int *me_dbiseqs; /**< array of dbi sequence numbers */
1283 pthread_key_t me_txkey; /**< thread-key for readers */
1284 txnid_t me_pgoldest; /**< ID of oldest reader last time we looked */
1285 MDB_pgstate me_pgstate; /**< state of old pages from freeDB */
1286 # define me_pglast me_pgstate.mf_pglast
1287 # define me_pghead me_pgstate.mf_pghead
1288 MDB_page *me_dpages; /**< list of malloc'd blocks for re-use */
1289 /** IDL of pages that became unused in a write txn */
1290 MDB_IDL me_free_pgs;
1291 /** ID2L of pages written during a write txn. Length MDB_IDL_UM_SIZE. */
1292 MDB_ID2L me_dirty_list;
1293 /** Max number of freelist items that can fit in a single overflow page */
1294 int me_maxfree_1pg;
1295 /** Max size of a node on a page */
1296 unsigned int me_nodemax;
1297 #if !(MDB_MAXKEYSIZE)
1298 unsigned int me_maxkey; /**< max size of a key */
1299 #endif
1300 int me_live_reader; /**< have liveness lock in reader table */
1301 #ifdef _WIN32
1302 int me_pidquery; /**< Used in OpenProcess */
1303 #endif
1304 #ifdef MDB_USE_POSIX_MUTEX /* Posix mutexes reside in shared mem */
1305 # define me_rmutex me_txns->mti_rmutex /**< Shared reader lock */
1306 # define me_wmutex me_txns->mti_wmutex /**< Shared writer lock */
1307 #else
1308 mdb_mutex_t me_rmutex;
1309 mdb_mutex_t me_wmutex;
1310 #endif
1311 void *me_userctx; /**< User-settable context */
1312 MDB_assert_func *me_assert_func; /**< Callback for assertion failures */
1313 };
1314
1315 /** Nested transaction */
1316 typedef struct MDB_ntxn {
1317 MDB_txn mnt_txn; /**< the transaction */
1318 MDB_pgstate mnt_pgstate; /**< parent transaction's saved freestate */
1319 } MDB_ntxn;
1320
1321 /** max number of pages to commit in one writev() call */
1322 #define MDB_COMMIT_PAGES 64
1323 #if defined(IOV_MAX) && IOV_MAX < MDB_COMMIT_PAGES
1324 #undef MDB_COMMIT_PAGES
1325 #define MDB_COMMIT_PAGES IOV_MAX
1326 #endif
1327
1328 /** max bytes to write in one call */
1329 #define MAX_WRITE (0x40000000U >> (sizeof(ssize_t) == 4))
1330
1331 /** Check \b txn and \b dbi arguments to a function */
1332 #define TXN_DBI_EXIST(txn, dbi, validity) \
1333 ((txn) && (dbi)<(txn)->mt_numdbs && ((txn)->mt_dbflags[dbi] & (validity)))
1334
1335 /** Check for misused \b dbi handles */
1336 #define TXN_DBI_CHANGED(txn, dbi) \
1337 ((txn)->mt_dbiseqs[dbi] != (txn)->mt_env->me_dbiseqs[dbi])
1338
1339 static int mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp);
1340 static int mdb_page_new(MDB_cursor *mc, uint32_t flags, int num, MDB_page **mp);
1341 static int mdb_page_touch(MDB_cursor *mc);
1342
1343 #define MDB_END_NAMES {"committed", "empty-commit", "abort", "reset", \
1344 "reset-tmp", "fail-begin", "fail-beginchild"}
1345 enum {
1346 /* mdb_txn_end operation number, for logging */
1347 MDB_END_COMMITTED, MDB_END_EMPTY_COMMIT, MDB_END_ABORT, MDB_END_RESET,
1348 MDB_END_RESET_TMP, MDB_END_FAIL_BEGIN, MDB_END_FAIL_BEGINCHILD
1349 };
1350 #define MDB_END_OPMASK 0x0F /**< mask for #mdb_txn_end() operation number */
1351 #define MDB_END_UPDATE 0x10 /**< update env state (DBIs) */
1352 #define MDB_END_FREE 0x20 /**< free txn unless it is #MDB_env.%me_txn0 */
1353 #define MDB_END_SLOT MDB_NOTLS /**< release any reader slot if #MDB_NOTLS */
1354 static void mdb_txn_end(MDB_txn *txn, unsigned mode);
1355
1356 static int mdb_page_get(MDB_cursor *mc, pgno_t pgno, MDB_page **mp, int *lvl);
1357 static int mdb_page_search_root(MDB_cursor *mc,
1358 MDB_val *key, int modify);
1359 #define MDB_PS_MODIFY 1
1360 #define MDB_PS_ROOTONLY 2
1361 #define MDB_PS_FIRST 4
1362 #define MDB_PS_LAST 8
1363 static int mdb_page_search(MDB_cursor *mc,
1364 MDB_val *key, int flags);
1365 static int mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst);
1366
1367 #define MDB_SPLIT_REPLACE MDB_APPENDDUP /**< newkey is not new */
1368 static int mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata,
1369 pgno_t newpgno, unsigned int nflags);
1370
1371 static int mdb_env_read_header(MDB_env *env, MDB_meta *meta);
1372 static MDB_meta *mdb_env_pick_meta(const MDB_env *env);
1373 static int mdb_env_write_meta(MDB_txn *txn);
1374 #ifdef MDB_USE_POSIX_MUTEX /* Drop unused excl arg */
1375 # define mdb_env_close0(env, excl) mdb_env_close1(env)
1376 #endif
1377 static void mdb_env_close0(MDB_env *env, int excl);
1378
1379 static MDB_node *mdb_node_search(MDB_cursor *mc, MDB_val *key, int *exactp);
1380 static int mdb_node_add(MDB_cursor *mc, indx_t indx,
1381 MDB_val *key, MDB_val *data, pgno_t pgno, unsigned int flags);
1382 static void mdb_node_del(MDB_cursor *mc, int ksize);
1383 static void mdb_node_shrink(MDB_page *mp, indx_t indx);
1384 static int mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst, int fromleft);
1385 static int mdb_node_read(MDB_cursor *mc, MDB_node *leaf, MDB_val *data);
1386 static size_t mdb_leaf_size(MDB_env *env, MDB_val *key, MDB_val *data);
1387 static size_t mdb_branch_size(MDB_env *env, MDB_val *key);
1388
1389 static int mdb_rebalance(MDB_cursor *mc);
1390 static int mdb_update_key(MDB_cursor *mc, MDB_val *key);
1391
1392 static void mdb_cursor_pop(MDB_cursor *mc);
1393 static int mdb_cursor_push(MDB_cursor *mc, MDB_page *mp);
1394
1395 static int mdb_cursor_del0(MDB_cursor *mc);
1396 static int mdb_del0(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data, unsigned flags);
1397 static int mdb_cursor_sibling(MDB_cursor *mc, int move_right);
1398 static int mdb_cursor_next(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op);
1399 static int mdb_cursor_prev(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op);
1400 static int mdb_cursor_set(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op,
1401 int *exactp);
1402 static int mdb_cursor_first(MDB_cursor *mc, MDB_val *key, MDB_val *data);
1403 static int mdb_cursor_last(MDB_cursor *mc, MDB_val *key, MDB_val *data);
1404
1405 static void mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx);
1406 static void mdb_xcursor_init0(MDB_cursor *mc);
1407 static void mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node);
1408 static void mdb_xcursor_init2(MDB_cursor *mc, MDB_xcursor *src_mx, int force);
1409
1410 static int mdb_drop0(MDB_cursor *mc, int subs);
1411 static void mdb_default_cmp(MDB_txn *txn, MDB_dbi dbi);
1412 static int mdb_reader_check0(MDB_env *env, int rlocked, int *dead);
1413
1414 /** @cond */
1415 static MDB_cmp_func mdb_cmp_memn, mdb_cmp_memnr, mdb_cmp_int, mdb_cmp_cint, mdb_cmp_long;
1416 /** @endcond */
1417
1418 /** Compare two items pointing at size_t's of unknown alignment. */
1419 #ifdef MISALIGNED_OK
1420 # define mdb_cmp_clong mdb_cmp_long
1421 #else
1422 # define mdb_cmp_clong mdb_cmp_cint
1423 #endif
1424
1425 #ifdef _WIN32
1426 static SECURITY_DESCRIPTOR mdb_null_sd;
1427 static SECURITY_ATTRIBUTES mdb_all_sa;
1428 static int mdb_sec_inited;
1429
1430 struct MDB_name;
1431 static int utf8_to_utf16(const char *src, struct MDB_name *dst, int xtra);
1432 #endif
1433
1434 /** Return the library version info. */
1435 char * ESECT
mdb_version(int * major,int * minor,int * patch)1436 mdb_version(int *major, int *minor, int *patch)
1437 {
1438 if (major) *major = MDB_VERSION_MAJOR;
1439 if (minor) *minor = MDB_VERSION_MINOR;
1440 if (patch) *patch = MDB_VERSION_PATCH;
1441 return MDB_VERSION_STRING;
1442 }
1443
1444 /** Table of descriptions for LMDB @ref errors */
1445 static char *const mdb_errstr[] = {
1446 "MDB_KEYEXIST: Key/data pair already exists",
1447 "MDB_NOTFOUND: No matching key/data pair found",
1448 "MDB_PAGE_NOTFOUND: Requested page not found",
1449 "MDB_CORRUPTED: Located page was wrong type",
1450 "MDB_PANIC: Update of meta page failed or environment had fatal error",
1451 "MDB_VERSION_MISMATCH: Database environment version mismatch",
1452 "MDB_INVALID: File is not an LMDB file",
1453 "MDB_MAP_FULL: Environment mapsize limit reached",
1454 "MDB_DBS_FULL: Environment maxdbs limit reached",
1455 "MDB_READERS_FULL: Environment maxreaders limit reached",
1456 "MDB_TLS_FULL: Thread-local storage keys full - too many environments open",
1457 "MDB_TXN_FULL: Transaction has too many dirty pages - transaction too big",
1458 "MDB_CURSOR_FULL: Internal error - cursor stack limit reached",
1459 "MDB_PAGE_FULL: Internal error - page has no more space",
1460 "MDB_MAP_RESIZED: Database contents grew beyond environment mapsize",
1461 "MDB_INCOMPATIBLE: Operation and DB incompatible, or DB flags changed",
1462 "MDB_BAD_RSLOT: Invalid reuse of reader locktable slot",
1463 "MDB_BAD_TXN: Transaction must abort, has a child, or is invalid",
1464 "MDB_BAD_VALSIZE: Unsupported size of key/DB name/data, or wrong DUPFIXED size",
1465 "MDB_BAD_DBI: The specified DBI handle was closed/changed unexpectedly",
1466 };
1467
1468 char *
mdb_strerror(int err)1469 mdb_strerror(int err)
1470 {
1471 #ifdef _WIN32
1472 /** HACK: pad 4KB on stack over the buf. Return system msgs in buf.
1473 * This works as long as no function between the call to mdb_strerror
1474 * and the actual use of the message uses more than 4K of stack.
1475 */
1476 #define MSGSIZE 1024
1477 #define PADSIZE 4096
1478 char buf[MSGSIZE+PADSIZE], *ptr = buf;
1479 #endif
1480 int i;
1481 if (!err)
1482 return ("Successful return: 0");
1483
1484 if (err >= MDB_KEYEXIST && err <= MDB_LAST_ERRCODE) {
1485 i = err - MDB_KEYEXIST;
1486 return mdb_errstr[i];
1487 }
1488
1489 #ifdef _WIN32
1490 /* These are the C-runtime error codes we use. The comment indicates
1491 * their numeric value, and the Win32 error they would correspond to
1492 * if the error actually came from a Win32 API. A major mess, we should
1493 * have used LMDB-specific error codes for everything.
1494 */
1495 switch(err) {
1496 case ENOENT: /* 2, FILE_NOT_FOUND */
1497 case EIO: /* 5, ACCESS_DENIED */
1498 case ENOMEM: /* 12, INVALID_ACCESS */
1499 case EACCES: /* 13, INVALID_DATA */
1500 case EBUSY: /* 16, CURRENT_DIRECTORY */
1501 case EINVAL: /* 22, BAD_COMMAND */
1502 case ENOSPC: /* 28, OUT_OF_PAPER */
1503 return strerror(err);
1504 default:
1505 ;
1506 }
1507 buf[0] = 0;
1508 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
1509 FORMAT_MESSAGE_IGNORE_INSERTS,
1510 NULL, err, 0, ptr, MSGSIZE, (va_list *)buf+MSGSIZE);
1511 return ptr;
1512 #else
1513 return strerror(err);
1514 #endif
1515 }
1516
1517 /** assert(3) variant in cursor context */
1518 #define mdb_cassert(mc, expr) mdb_assert0((mc)->mc_txn->mt_env, expr, #expr)
1519 /** assert(3) variant in transaction context */
1520 #define mdb_tassert(txn, expr) mdb_assert0((txn)->mt_env, expr, #expr)
1521 /** assert(3) variant in environment context */
1522 #define mdb_eassert(env, expr) mdb_assert0(env, expr, #expr)
1523
1524 #ifndef NDEBUG
1525 # define mdb_assert0(env, expr, expr_txt) ((expr) ? (void)0 : \
1526 mdb_assert_fail(env, expr_txt, mdb_func_, __FILE__, __LINE__))
1527
1528 static void ESECT
mdb_assert_fail(MDB_env * env,const char * expr_txt,const char * func,const char * file,int line)1529 mdb_assert_fail(MDB_env *env, const char *expr_txt,
1530 const char *func, const char *file, int line)
1531 {
1532 char buf[400];
1533 sprintf(buf, "%.100s:%d: Assertion '%.200s' failed in %.40s()",
1534 file, line, expr_txt, func);
1535 if (env->me_assert_func)
1536 env->me_assert_func(env, buf);
1537 fprintf(stderr, "%s\n", buf);
1538 abort();
1539 }
1540 #else
1541 # define mdb_assert0(env, expr, expr_txt) ((void) 0)
1542 #endif /* NDEBUG */
1543
1544 #if MDB_DEBUG
1545 /** Return the page number of \b mp which may be sub-page, for debug output */
1546 static pgno_t
mdb_dbg_pgno(MDB_page * mp)1547 mdb_dbg_pgno(MDB_page *mp)
1548 {
1549 pgno_t ret;
1550 COPY_PGNO(ret, mp->mp_pgno);
1551 return ret;
1552 }
1553
1554 /** Display a key in hexadecimal and return the address of the result.
1555 * @param[in] key the key to display
1556 * @param[in] buf the buffer to write into. Should always be #DKBUF.
1557 * @return The key in hexadecimal form.
1558 */
1559 char *
mdb_dkey(MDB_val * key,char * buf)1560 mdb_dkey(MDB_val *key, char *buf)
1561 {
1562 char *ptr = buf;
1563 unsigned char *c = key->mv_data;
1564 unsigned int i;
1565
1566 if (!key)
1567 return "";
1568
1569 if (key->mv_size > DKBUF_MAXKEYSIZE)
1570 return "MDB_MAXKEYSIZE";
1571 /* may want to make this a dynamic check: if the key is mostly
1572 * printable characters, print it as-is instead of converting to hex.
1573 */
1574 #if 1
1575 buf[0] = '\0';
1576 for (i=0; i<key->mv_size; i++)
1577 ptr += sprintf(ptr, "%02x", *c++);
1578 #else
1579 sprintf(buf, "%.*s", key->mv_size, key->mv_data);
1580 #endif
1581 return buf;
1582 }
1583
1584 static const char *
mdb_leafnode_type(MDB_node * n)1585 mdb_leafnode_type(MDB_node *n)
1586 {
1587 static char *const tp[2][2] = {{"", ": DB"}, {": sub-page", ": sub-DB"}};
1588 return F_ISSET(n->mn_flags, F_BIGDATA) ? ": overflow page" :
1589 tp[F_ISSET(n->mn_flags, F_DUPDATA)][F_ISSET(n->mn_flags, F_SUBDATA)];
1590 }
1591
1592 /** Display all the keys in the page. */
1593 void
mdb_page_list(MDB_page * mp)1594 mdb_page_list(MDB_page *mp)
1595 {
1596 pgno_t pgno = mdb_dbg_pgno(mp);
1597 const char *type, *state = (mp->mp_flags & P_DIRTY) ? ", dirty" : "";
1598 MDB_node *node;
1599 unsigned int i, nkeys, nsize, total = 0;
1600 MDB_val key;
1601 DKBUF;
1602
1603 switch (mp->mp_flags & (P_BRANCH|P_LEAF|P_LEAF2|P_META|P_OVERFLOW|P_SUBP)) {
1604 case P_BRANCH: type = "Branch page"; break;
1605 case P_LEAF: type = "Leaf page"; break;
1606 case P_LEAF|P_SUBP: type = "Sub-page"; break;
1607 case P_LEAF|P_LEAF2: type = "LEAF2 page"; break;
1608 case P_LEAF|P_LEAF2|P_SUBP: type = "LEAF2 sub-page"; break;
1609 case P_OVERFLOW:
1610 fprintf(stderr, "Overflow page %"Z"u pages %u%s\n",
1611 pgno, mp->mp_pages, state);
1612 return;
1613 case P_META:
1614 fprintf(stderr, "Meta-page %"Z"u txnid %"Z"u\n",
1615 pgno, ((MDB_meta *)METADATA(mp))->mm_txnid);
1616 return;
1617 default:
1618 fprintf(stderr, "Bad page %"Z"u flags 0x%X\n", pgno, mp->mp_flags);
1619 return;
1620 }
1621
1622 nkeys = NUMKEYS(mp);
1623 fprintf(stderr, "%s %"Z"u numkeys %d%s\n", type, pgno, nkeys, state);
1624
1625 for (i=0; i<nkeys; i++) {
1626 if (IS_LEAF2(mp)) { /* LEAF2 pages have no mp_ptrs[] or node headers */
1627 key.mv_size = nsize = mp->mp_pad;
1628 key.mv_data = LEAF2KEY(mp, i, nsize);
1629 total += nsize;
1630 fprintf(stderr, "key %d: nsize %d, %s\n", i, nsize, DKEY(&key));
1631 continue;
1632 }
1633 node = NODEPTR(mp, i);
1634 key.mv_size = node->mn_ksize;
1635 key.mv_data = node->mn_data;
1636 nsize = NODESIZE + key.mv_size;
1637 if (IS_BRANCH(mp)) {
1638 fprintf(stderr, "key %d: page %"Z"u, %s\n", i, NODEPGNO(node),
1639 DKEY(&key));
1640 total += nsize;
1641 } else {
1642 if (F_ISSET(node->mn_flags, F_BIGDATA))
1643 nsize += sizeof(pgno_t);
1644 else
1645 nsize += NODEDSZ(node);
1646 total += nsize;
1647 nsize += sizeof(indx_t);
1648 fprintf(stderr, "key %d: nsize %d, %s%s\n",
1649 i, nsize, DKEY(&key), mdb_leafnode_type(node));
1650 }
1651 total = EVEN(total);
1652 }
1653 fprintf(stderr, "Total: header %d + contents %d + unused %d\n",
1654 IS_LEAF2(mp) ? PAGEHDRSZ : PAGEBASE + mp->mp_lower, total, SIZELEFT(mp));
1655 }
1656
1657 void
mdb_cursor_chk(MDB_cursor * mc)1658 mdb_cursor_chk(MDB_cursor *mc)
1659 {
1660 unsigned int i;
1661 MDB_node *node;
1662 MDB_page *mp;
1663
1664 if (!mc->mc_snum || !(mc->mc_flags & C_INITIALIZED)) return;
1665 for (i=0; i<mc->mc_top; i++) {
1666 mp = mc->mc_pg[i];
1667 node = NODEPTR(mp, mc->mc_ki[i]);
1668 if (NODEPGNO(node) != mc->mc_pg[i+1]->mp_pgno)
1669 printf("oops!\n");
1670 }
1671 if (mc->mc_ki[i] >= NUMKEYS(mc->mc_pg[i]))
1672 printf("ack!\n");
1673 if (XCURSOR_INITED(mc)) {
1674 node = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
1675 if (((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA) &&
1676 mc->mc_xcursor->mx_cursor.mc_pg[0] != NODEDATA(node)) {
1677 printf("blah!\n");
1678 }
1679 }
1680 }
1681 #endif
1682
1683 #if (MDB_DEBUG) > 2
1684 /** Count all the pages in each DB and in the freelist
1685 * and make sure it matches the actual number of pages
1686 * being used.
1687 * All named DBs must be open for a correct count.
1688 */
mdb_audit(MDB_txn * txn)1689 static void mdb_audit(MDB_txn *txn)
1690 {
1691 MDB_cursor mc;
1692 MDB_val key, data;
1693 MDB_ID freecount, count;
1694 MDB_dbi i;
1695 int rc;
1696
1697 freecount = 0;
1698 mdb_cursor_init(&mc, txn, FREE_DBI, NULL);
1699 while ((rc = mdb_cursor_get(&mc, &key, &data, MDB_NEXT)) == 0)
1700 freecount += *(MDB_ID *)data.mv_data;
1701 mdb_tassert(txn, rc == MDB_NOTFOUND);
1702
1703 count = 0;
1704 for (i = 0; i<txn->mt_numdbs; i++) {
1705 MDB_xcursor mx;
1706 if (!(txn->mt_dbflags[i] & DB_VALID))
1707 continue;
1708 mdb_cursor_init(&mc, txn, i, &mx);
1709 if (txn->mt_dbs[i].md_root == P_INVALID)
1710 continue;
1711 count += txn->mt_dbs[i].md_branch_pages +
1712 txn->mt_dbs[i].md_leaf_pages +
1713 txn->mt_dbs[i].md_overflow_pages;
1714 if (txn->mt_dbs[i].md_flags & MDB_DUPSORT) {
1715 rc = mdb_page_search(&mc, NULL, MDB_PS_FIRST);
1716 for (; rc == MDB_SUCCESS; rc = mdb_cursor_sibling(&mc, 1)) {
1717 unsigned j;
1718 MDB_page *mp;
1719 mp = mc.mc_pg[mc.mc_top];
1720 for (j=0; j<NUMKEYS(mp); j++) {
1721 MDB_node *leaf = NODEPTR(mp, j);
1722 if (leaf->mn_flags & F_SUBDATA) {
1723 MDB_db db;
1724 memcpy(&db, NODEDATA(leaf), sizeof(db));
1725 count += db.md_branch_pages + db.md_leaf_pages +
1726 db.md_overflow_pages;
1727 }
1728 }
1729 }
1730 mdb_tassert(txn, rc == MDB_NOTFOUND);
1731 }
1732 }
1733 if (freecount + count + NUM_METAS != txn->mt_next_pgno) {
1734 fprintf(stderr, "audit: %"Z"u freecount: %"Z"u count: %"Z"u total: %"Z"u next_pgno: %"Z"u\n",
1735 txn->mt_txnid, freecount, count+NUM_METAS,
1736 freecount+count+NUM_METAS, txn->mt_next_pgno);
1737 }
1738 }
1739 #endif
1740
1741 int
mdb_cmp(MDB_txn * txn,MDB_dbi dbi,const MDB_val * a,const MDB_val * b)1742 mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b)
1743 {
1744 return txn->mt_dbxs[dbi].md_cmp(a, b);
1745 }
1746
1747 int
mdb_dcmp(MDB_txn * txn,MDB_dbi dbi,const MDB_val * a,const MDB_val * b)1748 mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b)
1749 {
1750 MDB_cmp_func *dcmp = txn->mt_dbxs[dbi].md_dcmp;
1751 #if UINT_MAX < SIZE_MAX
1752 if (dcmp == mdb_cmp_int && a->mv_size == sizeof(size_t))
1753 dcmp = mdb_cmp_clong;
1754 #endif
1755 return dcmp(a, b);
1756 }
1757
1758 /** Allocate memory for a page.
1759 * Re-use old malloc'd pages first for singletons, otherwise just malloc.
1760 * Set #MDB_TXN_ERROR on failure.
1761 */
1762 static MDB_page *
mdb_page_malloc(MDB_txn * txn,unsigned num)1763 mdb_page_malloc(MDB_txn *txn, unsigned num)
1764 {
1765 MDB_env *env = txn->mt_env;
1766 MDB_page *ret = env->me_dpages;
1767 size_t psize = env->me_psize, sz = psize, off;
1768 /* For ! #MDB_NOMEMINIT, psize counts how much to init.
1769 * For a single page alloc, we init everything after the page header.
1770 * For multi-page, we init the final page; if the caller needed that
1771 * many pages they will be filling in at least up to the last page.
1772 */
1773 if (num == 1) {
1774 if (ret) {
1775 VGMEMP_ALLOC(env, ret, sz);
1776 VGMEMP_DEFINED(ret, sizeof(ret->mp_next));
1777 env->me_dpages = ret->mp_next;
1778 return ret;
1779 }
1780 psize -= off = PAGEHDRSZ;
1781 } else {
1782 sz *= num;
1783 off = sz - psize;
1784 }
1785 if ((ret = malloc(sz)) != NULL) {
1786 VGMEMP_ALLOC(env, ret, sz);
1787 if (!(env->me_flags & MDB_NOMEMINIT)) {
1788 memset((char *)ret + off, 0, psize);
1789 ret->mp_pad = 0;
1790 }
1791 } else {
1792 txn->mt_flags |= MDB_TXN_ERROR;
1793 }
1794 return ret;
1795 }
1796 /** Free a single page.
1797 * Saves single pages to a list, for future reuse.
1798 * (This is not used for multi-page overflow pages.)
1799 */
1800 static void
mdb_page_free(MDB_env * env,MDB_page * mp)1801 mdb_page_free(MDB_env *env, MDB_page *mp)
1802 {
1803 mp->mp_next = env->me_dpages;
1804 VGMEMP_FREE(env, mp);
1805 env->me_dpages = mp;
1806 }
1807
1808 /** Free a dirty page */
1809 static void
mdb_dpage_free(MDB_env * env,MDB_page * dp)1810 mdb_dpage_free(MDB_env *env, MDB_page *dp)
1811 {
1812 if (!IS_OVERFLOW(dp) || dp->mp_pages == 1) {
1813 mdb_page_free(env, dp);
1814 } else {
1815 /* large pages just get freed directly */
1816 VGMEMP_FREE(env, dp);
1817 free(dp);
1818 }
1819 }
1820
1821 /** Return all dirty pages to dpage list */
1822 static void
mdb_dlist_free(MDB_txn * txn)1823 mdb_dlist_free(MDB_txn *txn)
1824 {
1825 MDB_env *env = txn->mt_env;
1826 MDB_ID2L dl = txn->mt_u.dirty_list;
1827 unsigned i, n = dl[0].mid;
1828
1829 for (i = 1; i <= n; i++) {
1830 mdb_dpage_free(env, dl[i].mptr);
1831 }
1832 dl[0].mid = 0;
1833 }
1834
1835 /** Loosen or free a single page.
1836 * Saves single pages to a list for future reuse
1837 * in this same txn. It has been pulled from the freeDB
1838 * and already resides on the dirty list, but has been
1839 * deleted. Use these pages first before pulling again
1840 * from the freeDB.
1841 *
1842 * If the page wasn't dirtied in this txn, just add it
1843 * to this txn's free list.
1844 */
1845 static int
mdb_page_loose(MDB_cursor * mc,MDB_page * mp)1846 mdb_page_loose(MDB_cursor *mc, MDB_page *mp)
1847 {
1848 int loose = 0;
1849 pgno_t pgno = mp->mp_pgno;
1850 MDB_txn *txn = mc->mc_txn;
1851
1852 if ((mp->mp_flags & P_DIRTY) && mc->mc_dbi != FREE_DBI) {
1853 if (txn->mt_parent) {
1854 MDB_ID2 *dl = txn->mt_u.dirty_list;
1855 /* If txn has a parent, make sure the page is in our
1856 * dirty list.
1857 */
1858 if (dl[0].mid) {
1859 unsigned x = mdb_mid2l_search(dl, pgno);
1860 if (x <= dl[0].mid && dl[x].mid == pgno) {
1861 if (mp != dl[x].mptr) { /* bad cursor? */
1862 mc->mc_flags &= ~(C_INITIALIZED|C_EOF);
1863 txn->mt_flags |= MDB_TXN_ERROR;
1864 return MDB_CORRUPTED;
1865 }
1866 /* ok, it's ours */
1867 loose = 1;
1868 }
1869 }
1870 } else {
1871 /* no parent txn, so it's just ours */
1872 loose = 1;
1873 }
1874 }
1875 if (loose) {
1876 DPRINTF(("loosen db %d page %"Z"u", DDBI(mc),
1877 mp->mp_pgno));
1878 NEXT_LOOSE_PAGE(mp) = txn->mt_loose_pgs;
1879 txn->mt_loose_pgs = mp;
1880 txn->mt_loose_count++;
1881 mp->mp_flags |= P_LOOSE;
1882 } else {
1883 int rc = mdb_midl_append(&txn->mt_free_pgs, pgno);
1884 if (rc)
1885 return rc;
1886 }
1887
1888 return MDB_SUCCESS;
1889 }
1890
1891 /** Set or clear P_KEEP in dirty, non-overflow, non-sub pages watched by txn.
1892 * @param[in] mc A cursor handle for the current operation.
1893 * @param[in] pflags Flags of the pages to update:
1894 * P_DIRTY to set P_KEEP, P_DIRTY|P_KEEP to clear it.
1895 * @param[in] all No shortcuts. Needed except after a full #mdb_page_flush().
1896 * @return 0 on success, non-zero on failure.
1897 */
1898 static int
mdb_pages_xkeep(MDB_cursor * mc,unsigned pflags,int all)1899 mdb_pages_xkeep(MDB_cursor *mc, unsigned pflags, int all)
1900 {
1901 enum { Mask = P_SUBP|P_DIRTY|P_LOOSE|P_KEEP };
1902 MDB_txn *txn = mc->mc_txn;
1903 MDB_cursor *m3, *m0 = mc;
1904 MDB_xcursor *mx;
1905 MDB_page *dp, *mp;
1906 MDB_node *leaf;
1907 unsigned i, j;
1908 int rc = MDB_SUCCESS, level;
1909
1910 /* Mark pages seen by cursors */
1911 if (mc->mc_flags & C_UNTRACK)
1912 mc = NULL; /* will find mc in mt_cursors */
1913 for (i = txn->mt_numdbs;; mc = txn->mt_cursors[--i]) {
1914 for (; mc; mc=mc->mc_next) {
1915 if (!(mc->mc_flags & C_INITIALIZED))
1916 continue;
1917 for (m3 = mc;; m3 = &mx->mx_cursor) {
1918 mp = NULL;
1919 for (j=0; j<m3->mc_snum; j++) {
1920 mp = m3->mc_pg[j];
1921 if ((mp->mp_flags & Mask) == pflags)
1922 mp->mp_flags ^= P_KEEP;
1923 }
1924 mx = m3->mc_xcursor;
1925 /* Proceed to mx if it is at a sub-database */
1926 if (! (mx && (mx->mx_cursor.mc_flags & C_INITIALIZED)))
1927 break;
1928 if (! (mp && (mp->mp_flags & P_LEAF)))
1929 break;
1930 leaf = NODEPTR(mp, m3->mc_ki[j-1]);
1931 if (!(leaf->mn_flags & F_SUBDATA))
1932 break;
1933 }
1934 }
1935 if (i == 0)
1936 break;
1937 }
1938
1939 if (all) {
1940 /* Mark dirty root pages */
1941 for (i=0; i<txn->mt_numdbs; i++) {
1942 if (txn->mt_dbflags[i] & DB_DIRTY) {
1943 pgno_t pgno = txn->mt_dbs[i].md_root;
1944 if (pgno == P_INVALID)
1945 continue;
1946 if ((rc = mdb_page_get(m0, pgno, &dp, &level)) != MDB_SUCCESS)
1947 break;
1948 if ((dp->mp_flags & Mask) == pflags && level <= 1)
1949 dp->mp_flags ^= P_KEEP;
1950 }
1951 }
1952 }
1953
1954 return rc;
1955 }
1956
1957 static int mdb_page_flush(MDB_txn *txn, int keep);
1958
1959 /** Spill pages from the dirty list back to disk.
1960 * This is intended to prevent running into #MDB_TXN_FULL situations,
1961 * but note that they may still occur in a few cases:
1962 * 1) our estimate of the txn size could be too small. Currently this
1963 * seems unlikely, except with a large number of #MDB_MULTIPLE items.
1964 * 2) child txns may run out of space if their parents dirtied a
1965 * lot of pages and never spilled them. TODO: we probably should do
1966 * a preemptive spill during #mdb_txn_begin() of a child txn, if
1967 * the parent's dirty_room is below a given threshold.
1968 *
1969 * Otherwise, if not using nested txns, it is expected that apps will
1970 * not run into #MDB_TXN_FULL any more. The pages are flushed to disk
1971 * the same way as for a txn commit, e.g. their P_DIRTY flag is cleared.
1972 * If the txn never references them again, they can be left alone.
1973 * If the txn only reads them, they can be used without any fuss.
1974 * If the txn writes them again, they can be dirtied immediately without
1975 * going thru all of the work of #mdb_page_touch(). Such references are
1976 * handled by #mdb_page_unspill().
1977 *
1978 * Also note, we never spill DB root pages, nor pages of active cursors,
1979 * because we'll need these back again soon anyway. And in nested txns,
1980 * we can't spill a page in a child txn if it was already spilled in a
1981 * parent txn. That would alter the parent txns' data even though
1982 * the child hasn't committed yet, and we'd have no way to undo it if
1983 * the child aborted.
1984 *
1985 * @param[in] m0 cursor A cursor handle identifying the transaction and
1986 * database for which we are checking space.
1987 * @param[in] key For a put operation, the key being stored.
1988 * @param[in] data For a put operation, the data being stored.
1989 * @return 0 on success, non-zero on failure.
1990 */
1991 static int
mdb_page_spill(MDB_cursor * m0,MDB_val * key,MDB_val * data)1992 mdb_page_spill(MDB_cursor *m0, MDB_val *key, MDB_val *data)
1993 {
1994 MDB_txn *txn = m0->mc_txn;
1995 MDB_page *dp;
1996 MDB_ID2L dl = txn->mt_u.dirty_list;
1997 unsigned int i, j, need;
1998 int rc;
1999
2000 if (m0->mc_flags & C_SUB)
2001 return MDB_SUCCESS;
2002
2003 /* Estimate how much space this op will take */
2004 i = m0->mc_db->md_depth;
2005 /* Named DBs also dirty the main DB */
2006 if (m0->mc_dbi >= CORE_DBS)
2007 i += txn->mt_dbs[MAIN_DBI].md_depth;
2008 /* For puts, roughly factor in the key+data size */
2009 if (key)
2010 i += (LEAFSIZE(key, data) + txn->mt_env->me_psize) / txn->mt_env->me_psize;
2011 i += i; /* double it for good measure */
2012 need = i;
2013
2014 if (txn->mt_dirty_room > i)
2015 return MDB_SUCCESS;
2016
2017 if (!txn->mt_spill_pgs) {
2018 txn->mt_spill_pgs = mdb_midl_alloc(MDB_IDL_UM_MAX);
2019 if (!txn->mt_spill_pgs)
2020 return ENOMEM;
2021 } else {
2022 /* purge deleted slots */
2023 MDB_IDL sl = txn->mt_spill_pgs;
2024 unsigned int num = sl[0];
2025 j=0;
2026 for (i=1; i<=num; i++) {
2027 if (!(sl[i] & 1))
2028 sl[++j] = sl[i];
2029 }
2030 sl[0] = j;
2031 }
2032
2033 /* Preserve pages which may soon be dirtied again */
2034 if ((rc = mdb_pages_xkeep(m0, P_DIRTY, 1)) != MDB_SUCCESS)
2035 goto done;
2036
2037 /* Less aggressive spill - we originally spilled the entire dirty list,
2038 * with a few exceptions for cursor pages and DB root pages. But this
2039 * turns out to be a lot of wasted effort because in a large txn many
2040 * of those pages will need to be used again. So now we spill only 1/8th
2041 * of the dirty pages. Testing revealed this to be a good tradeoff,
2042 * better than 1/2, 1/4, or 1/10.
2043 */
2044 if (need < MDB_IDL_UM_MAX / 8)
2045 need = MDB_IDL_UM_MAX / 8;
2046
2047 /* Save the page IDs of all the pages we're flushing */
2048 /* flush from the tail forward, this saves a lot of shifting later on. */
2049 for (i=dl[0].mid; i && need; i--) {
2050 MDB_ID pn = dl[i].mid << 1;
2051 dp = dl[i].mptr;
2052 if (dp->mp_flags & (P_LOOSE|P_KEEP))
2053 continue;
2054 /* Can't spill twice, make sure it's not already in a parent's
2055 * spill list.
2056 */
2057 if (txn->mt_parent) {
2058 MDB_txn *tx2;
2059 for (tx2 = txn->mt_parent; tx2; tx2 = tx2->mt_parent) {
2060 if (tx2->mt_spill_pgs) {
2061 j = mdb_midl_search(tx2->mt_spill_pgs, pn);
2062 if (j <= tx2->mt_spill_pgs[0] && tx2->mt_spill_pgs[j] == pn) {
2063 dp->mp_flags |= P_KEEP;
2064 break;
2065 }
2066 }
2067 }
2068 if (tx2)
2069 continue;
2070 }
2071 if ((rc = mdb_midl_append(&txn->mt_spill_pgs, pn)))
2072 goto done;
2073 need--;
2074 }
2075 mdb_midl_sort(txn->mt_spill_pgs);
2076
2077 /* Flush the spilled part of dirty list */
2078 if ((rc = mdb_page_flush(txn, i)) != MDB_SUCCESS)
2079 goto done;
2080
2081 /* Reset any dirty pages we kept that page_flush didn't see */
2082 rc = mdb_pages_xkeep(m0, P_DIRTY|P_KEEP, i);
2083
2084 done:
2085 txn->mt_flags |= rc ? MDB_TXN_ERROR : MDB_TXN_SPILLS;
2086 return rc;
2087 }
2088
2089 /** Find oldest txnid still referenced. Expects txn->mt_txnid > 0. */
2090 static txnid_t
mdb_find_oldest(MDB_txn * txn)2091 mdb_find_oldest(MDB_txn *txn)
2092 {
2093 int i;
2094 txnid_t mr, oldest = txn->mt_txnid - 1;
2095 if (txn->mt_env->me_txns) {
2096 MDB_reader *r = txn->mt_env->me_txns->mti_readers;
2097 for (i = txn->mt_env->me_txns->mti_numreaders; --i >= 0; ) {
2098 if (r[i].mr_pid) {
2099 mr = r[i].mr_txnid;
2100 if (oldest > mr)
2101 oldest = mr;
2102 }
2103 }
2104 }
2105 return oldest;
2106 }
2107
2108 /** Add a page to the txn's dirty list */
2109 static void
mdb_page_dirty(MDB_txn * txn,MDB_page * mp)2110 mdb_page_dirty(MDB_txn *txn, MDB_page *mp)
2111 {
2112 MDB_ID2 mid;
2113 int rc, (*insert)(MDB_ID2L, MDB_ID2 *);
2114
2115 if (txn->mt_flags & MDB_TXN_WRITEMAP) {
2116 insert = mdb_mid2l_append;
2117 } else {
2118 insert = mdb_mid2l_insert;
2119 }
2120 mid.mid = mp->mp_pgno;
2121 mid.mptr = mp;
2122 rc = insert(txn->mt_u.dirty_list, &mid);
2123 mdb_tassert(txn, rc == 0);
2124 txn->mt_dirty_room--;
2125 }
2126
2127 /** Allocate page numbers and memory for writing. Maintain me_pglast,
2128 * me_pghead and mt_next_pgno. Set #MDB_TXN_ERROR on failure.
2129 *
2130 * If there are free pages available from older transactions, they
2131 * are re-used first. Otherwise allocate a new page at mt_next_pgno.
2132 * Do not modify the freedB, just merge freeDB records into me_pghead[]
2133 * and move me_pglast to say which records were consumed. Only this
2134 * function can create me_pghead and move me_pglast/mt_next_pgno.
2135 * @param[in] mc cursor A cursor handle identifying the transaction and
2136 * database for which we are allocating.
2137 * @param[in] num the number of pages to allocate.
2138 * @param[out] mp Address of the allocated page(s). Requests for multiple pages
2139 * will always be satisfied by a single contiguous chunk of memory.
2140 * @return 0 on success, non-zero on failure.
2141 */
2142 static int
mdb_page_alloc(MDB_cursor * mc,int num,MDB_page ** mp)2143 mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
2144 {
2145 #ifdef MDB_PARANOID /* Seems like we can ignore this now */
2146 /* Get at most <Max_retries> more freeDB records once me_pghead
2147 * has enough pages. If not enough, use new pages from the map.
2148 * If <Paranoid> and mc is updating the freeDB, only get new
2149 * records if me_pghead is empty. Then the freelist cannot play
2150 * catch-up with itself by growing while trying to save it.
2151 */
2152 enum { Paranoid = 1, Max_retries = 500 };
2153 #else
2154 enum { Paranoid = 0, Max_retries = INT_MAX /*infinite*/ };
2155 #endif
2156 int rc, retry = num * 60;
2157 MDB_txn *txn = mc->mc_txn;
2158 MDB_env *env = txn->mt_env;
2159 pgno_t pgno, *mop = env->me_pghead;
2160 unsigned i, j, mop_len = mop ? mop[0] : 0, n2 = num-1;
2161 MDB_page *np;
2162 txnid_t oldest = 0, last;
2163 MDB_cursor_op op;
2164 MDB_cursor m2;
2165 int found_old = 0;
2166
2167 /* If there are any loose pages, just use them */
2168 if (num == 1 && txn->mt_loose_pgs) {
2169 np = txn->mt_loose_pgs;
2170 txn->mt_loose_pgs = NEXT_LOOSE_PAGE(np);
2171 txn->mt_loose_count--;
2172 DPRINTF(("db %d use loose page %"Z"u", DDBI(mc),
2173 np->mp_pgno));
2174 *mp = np;
2175 return MDB_SUCCESS;
2176 }
2177
2178 *mp = NULL;
2179
2180 /* If our dirty list is already full, we can't do anything */
2181 if (txn->mt_dirty_room == 0) {
2182 rc = MDB_TXN_FULL;
2183 goto fail;
2184 }
2185
2186 for (op = MDB_FIRST;; op = MDB_NEXT) {
2187 MDB_val key, data;
2188 MDB_node *leaf;
2189 pgno_t *idl;
2190
2191 /* Seek a big enough contiguous page range. Prefer
2192 * pages at the tail, just truncating the list.
2193 */
2194 if (mop_len > n2) {
2195 i = mop_len;
2196 do {
2197 pgno = mop[i];
2198 if (mop[i-n2] == pgno+n2)
2199 goto search_done;
2200 } while (--i > n2);
2201 if (--retry < 0)
2202 break;
2203 }
2204
2205 if (op == MDB_FIRST) { /* 1st iteration */
2206 /* Prepare to fetch more and coalesce */
2207 last = env->me_pglast;
2208 oldest = env->me_pgoldest;
2209 mdb_cursor_init(&m2, txn, FREE_DBI, NULL);
2210 if (last) {
2211 op = MDB_SET_RANGE;
2212 key.mv_data = &last; /* will look up last+1 */
2213 key.mv_size = sizeof(last);
2214 }
2215 if (Paranoid && mc->mc_dbi == FREE_DBI)
2216 retry = -1;
2217 }
2218 if (Paranoid && retry < 0 && mop_len)
2219 break;
2220
2221 last++;
2222 /* Do not fetch more if the record will be too recent */
2223 if (oldest <= last) {
2224 if (!found_old) {
2225 oldest = mdb_find_oldest(txn);
2226 env->me_pgoldest = oldest;
2227 found_old = 1;
2228 }
2229 if (oldest <= last)
2230 break;
2231 }
2232 rc = mdb_cursor_get(&m2, &key, NULL, op);
2233 if (rc) {
2234 if (rc == MDB_NOTFOUND)
2235 break;
2236 goto fail;
2237 }
2238 last = *(txnid_t*)key.mv_data;
2239 if (oldest <= last) {
2240 if (!found_old) {
2241 oldest = mdb_find_oldest(txn);
2242 env->me_pgoldest = oldest;
2243 found_old = 1;
2244 }
2245 if (oldest <= last)
2246 break;
2247 }
2248 np = m2.mc_pg[m2.mc_top];
2249 leaf = NODEPTR(np, m2.mc_ki[m2.mc_top]);
2250 if ((rc = mdb_node_read(&m2, leaf, &data)) != MDB_SUCCESS)
2251 goto fail;
2252
2253 idl = (MDB_ID *) data.mv_data;
2254 i = idl[0];
2255 if (!mop) {
2256 if (!(env->me_pghead = mop = mdb_midl_alloc(i))) {
2257 rc = ENOMEM;
2258 goto fail;
2259 }
2260 } else {
2261 if ((rc = mdb_midl_need(&env->me_pghead, i)) != 0)
2262 goto fail;
2263 mop = env->me_pghead;
2264 }
2265 env->me_pglast = last;
2266 #if (MDB_DEBUG) > 1
2267 DPRINTF(("IDL read txn %"Z"u root %"Z"u num %u",
2268 last, txn->mt_dbs[FREE_DBI].md_root, i));
2269 for (j = i; j; j--)
2270 DPRINTF(("IDL %"Z"u", idl[j]));
2271 #endif
2272 /* Merge in descending sorted order */
2273 mdb_midl_xmerge(mop, idl);
2274 mop_len = mop[0];
2275 }
2276
2277 /* Use new pages from the map when nothing suitable in the freeDB */
2278 i = 0;
2279 pgno = txn->mt_next_pgno;
2280 if (pgno + num >= env->me_maxpg) {
2281 DPUTS("DB size maxed out");
2282 rc = MDB_MAP_FULL;
2283 goto fail;
2284 }
2285
2286 search_done:
2287 if (env->me_flags & MDB_WRITEMAP) {
2288 np = (MDB_page *)(env->me_map + env->me_psize * pgno);
2289 } else {
2290 if (!(np = mdb_page_malloc(txn, num))) {
2291 rc = ENOMEM;
2292 goto fail;
2293 }
2294 }
2295 if (i) {
2296 mop[0] = mop_len -= num;
2297 /* Move any stragglers down */
2298 for (j = i-num; j < mop_len; )
2299 mop[++j] = mop[++i];
2300 } else {
2301 txn->mt_next_pgno = pgno + num;
2302 }
2303 np->mp_pgno = pgno;
2304 mdb_page_dirty(txn, np);
2305 *mp = np;
2306
2307 return MDB_SUCCESS;
2308
2309 fail:
2310 txn->mt_flags |= MDB_TXN_ERROR;
2311 return rc;
2312 }
2313
2314 /** Copy the used portions of a non-overflow page.
2315 * @param[in] dst page to copy into
2316 * @param[in] src page to copy from
2317 * @param[in] psize size of a page
2318 */
2319 static void
mdb_page_copy(MDB_page * dst,MDB_page * src,unsigned int psize)2320 mdb_page_copy(MDB_page *dst, MDB_page *src, unsigned int psize)
2321 {
2322 enum { Align = sizeof(pgno_t) };
2323 indx_t upper = src->mp_upper, lower = src->mp_lower, unused = upper-lower;
2324
2325 /* If page isn't full, just copy the used portion. Adjust
2326 * alignment so memcpy may copy words instead of bytes.
2327 */
2328 if ((unused &= -Align) && !IS_LEAF2(src)) {
2329 upper = (upper + PAGEBASE) & -Align;
2330 memcpy(dst, src, (lower + PAGEBASE + (Align-1)) & -Align);
2331 memcpy((pgno_t *)((char *)dst+upper), (pgno_t *)((char *)src+upper),
2332 psize - upper);
2333 } else {
2334 memcpy(dst, src, psize - unused);
2335 }
2336 }
2337
2338 /** Pull a page off the txn's spill list, if present.
2339 * If a page being referenced was spilled to disk in this txn, bring
2340 * it back and make it dirty/writable again.
2341 * @param[in] txn the transaction handle.
2342 * @param[in] mp the page being referenced. It must not be dirty.
2343 * @param[out] ret the writable page, if any. ret is unchanged if
2344 * mp wasn't spilled.
2345 */
2346 static int
mdb_page_unspill(MDB_txn * txn,MDB_page * mp,MDB_page ** ret)2347 mdb_page_unspill(MDB_txn *txn, MDB_page *mp, MDB_page **ret)
2348 {
2349 MDB_env *env = txn->mt_env;
2350 const MDB_txn *tx2;
2351 unsigned x;
2352 pgno_t pgno = mp->mp_pgno, pn = pgno << 1;
2353
2354 for (tx2 = txn; tx2; tx2=tx2->mt_parent) {
2355 if (!tx2->mt_spill_pgs)
2356 continue;
2357 x = mdb_midl_search(tx2->mt_spill_pgs, pn);
2358 if (x <= tx2->mt_spill_pgs[0] && tx2->mt_spill_pgs[x] == pn) {
2359 MDB_page *np;
2360 int num;
2361 if (txn->mt_dirty_room == 0)
2362 return MDB_TXN_FULL;
2363 if (IS_OVERFLOW(mp))
2364 num = mp->mp_pages;
2365 else
2366 num = 1;
2367 if (env->me_flags & MDB_WRITEMAP) {
2368 np = mp;
2369 } else {
2370 np = mdb_page_malloc(txn, num);
2371 if (!np)
2372 return ENOMEM;
2373 if (num > 1)
2374 memcpy(np, mp, num * env->me_psize);
2375 else
2376 mdb_page_copy(np, mp, env->me_psize);
2377 }
2378 if (tx2 == txn) {
2379 /* If in current txn, this page is no longer spilled.
2380 * If it happens to be the last page, truncate the spill list.
2381 * Otherwise mark it as deleted by setting the LSB.
2382 */
2383 if (x == txn->mt_spill_pgs[0])
2384 txn->mt_spill_pgs[0]--;
2385 else
2386 txn->mt_spill_pgs[x] |= 1;
2387 } /* otherwise, if belonging to a parent txn, the
2388 * page remains spilled until child commits
2389 */
2390
2391 mdb_page_dirty(txn, np);
2392 np->mp_flags |= P_DIRTY;
2393 *ret = np;
2394 break;
2395 }
2396 }
2397 return MDB_SUCCESS;
2398 }
2399
2400 /** Touch a page: make it dirty and re-insert into tree with updated pgno.
2401 * Set #MDB_TXN_ERROR on failure.
2402 * @param[in] mc cursor pointing to the page to be touched
2403 * @return 0 on success, non-zero on failure.
2404 */
2405 static int
mdb_page_touch(MDB_cursor * mc)2406 mdb_page_touch(MDB_cursor *mc)
2407 {
2408 MDB_page *mp = mc->mc_pg[mc->mc_top], *np;
2409 MDB_txn *txn = mc->mc_txn;
2410 MDB_cursor *m2, *m3;
2411 pgno_t pgno;
2412 int rc;
2413
2414 if (!F_ISSET(mp->mp_flags, P_DIRTY)) {
2415 if (txn->mt_flags & MDB_TXN_SPILLS) {
2416 np = NULL;
2417 rc = mdb_page_unspill(txn, mp, &np);
2418 if (rc)
2419 goto fail;
2420 if (np)
2421 goto done;
2422 }
2423 if ((rc = mdb_midl_need(&txn->mt_free_pgs, 1)) ||
2424 (rc = mdb_page_alloc(mc, 1, &np)))
2425 goto fail;
2426 pgno = np->mp_pgno;
2427 DPRINTF(("touched db %d page %"Z"u -> %"Z"u", DDBI(mc),
2428 mp->mp_pgno, pgno));
2429 mdb_cassert(mc, mp->mp_pgno != pgno);
2430 mdb_midl_xappend(txn->mt_free_pgs, mp->mp_pgno);
2431 /* Update the parent page, if any, to point to the new page */
2432 if (mc->mc_top) {
2433 MDB_page *parent = mc->mc_pg[mc->mc_top-1];
2434 MDB_node *node = NODEPTR(parent, mc->mc_ki[mc->mc_top-1]);
2435 SETPGNO(node, pgno);
2436 } else {
2437 mc->mc_db->md_root = pgno;
2438 }
2439 } else if (txn->mt_parent && !IS_SUBP(mp)) {
2440 MDB_ID2 mid, *dl = txn->mt_u.dirty_list;
2441 pgno = mp->mp_pgno;
2442 /* If txn has a parent, make sure the page is in our
2443 * dirty list.
2444 */
2445 if (dl[0].mid) {
2446 unsigned x = mdb_mid2l_search(dl, pgno);
2447 if (x <= dl[0].mid && dl[x].mid == pgno) {
2448 if (mp != dl[x].mptr) { /* bad cursor? */
2449 mc->mc_flags &= ~(C_INITIALIZED|C_EOF);
2450 txn->mt_flags |= MDB_TXN_ERROR;
2451 return MDB_CORRUPTED;
2452 }
2453 return 0;
2454 }
2455 }
2456 mdb_cassert(mc, dl[0].mid < MDB_IDL_UM_MAX);
2457 /* No - copy it */
2458 np = mdb_page_malloc(txn, 1);
2459 if (!np)
2460 return ENOMEM;
2461 mid.mid = pgno;
2462 mid.mptr = np;
2463 rc = mdb_mid2l_insert(dl, &mid);
2464 mdb_cassert(mc, rc == 0);
2465 } else {
2466 return 0;
2467 }
2468
2469 mdb_page_copy(np, mp, txn->mt_env->me_psize);
2470 np->mp_pgno = pgno;
2471 np->mp_flags |= P_DIRTY;
2472
2473 done:
2474 /* Adjust cursors pointing to mp */
2475 mc->mc_pg[mc->mc_top] = np;
2476 m2 = txn->mt_cursors[mc->mc_dbi];
2477 if (mc->mc_flags & C_SUB) {
2478 for (; m2; m2=m2->mc_next) {
2479 m3 = &m2->mc_xcursor->mx_cursor;
2480 if (m3->mc_snum < mc->mc_snum) continue;
2481 if (m3->mc_pg[mc->mc_top] == mp)
2482 m3->mc_pg[mc->mc_top] = np;
2483 }
2484 } else {
2485 for (; m2; m2=m2->mc_next) {
2486 if (m2->mc_snum < mc->mc_snum) continue;
2487 if (m2 == mc) continue;
2488 if (m2->mc_pg[mc->mc_top] == mp) {
2489 m2->mc_pg[mc->mc_top] = np;
2490 if (IS_LEAF(np))
2491 XCURSOR_REFRESH(m2, mc->mc_top, np);
2492 }
2493 }
2494 }
2495 return 0;
2496
2497 fail:
2498 txn->mt_flags |= MDB_TXN_ERROR;
2499 return rc;
2500 }
2501
2502 int
mdb_env_sync(MDB_env * env,int force)2503 mdb_env_sync(MDB_env *env, int force)
2504 {
2505 int rc = 0;
2506 if (env->me_flags & MDB_RDONLY)
2507 return EACCES;
2508 if (force || !F_ISSET(env->me_flags, MDB_NOSYNC)) {
2509 if (env->me_flags & MDB_WRITEMAP) {
2510 int flags = ((env->me_flags & MDB_MAPASYNC) && !force)
2511 ? MS_ASYNC : MS_SYNC;
2512 if (MDB_MSYNC(env->me_map, env->me_mapsize, flags))
2513 rc = ErrCode();
2514 #ifdef _WIN32
2515 else if (flags == MS_SYNC && MDB_FDATASYNC(env->me_fd))
2516 rc = ErrCode();
2517 #endif
2518 } else {
2519 #ifdef BROKEN_FDATASYNC
2520 if (env->me_flags & MDB_FSYNCONLY) {
2521 if (fsync(env->me_fd))
2522 rc = ErrCode();
2523 } else
2524 #endif
2525 if (MDB_FDATASYNC(env->me_fd))
2526 rc = ErrCode();
2527 }
2528 }
2529 return rc;
2530 }
2531
2532 /** Back up parent txn's cursors, then grab the originals for tracking */
2533 static int
mdb_cursor_shadow(MDB_txn * src,MDB_txn * dst)2534 mdb_cursor_shadow(MDB_txn *src, MDB_txn *dst)
2535 {
2536 MDB_cursor *mc, *bk;
2537 MDB_xcursor *mx;
2538 size_t size;
2539 int i;
2540
2541 for (i = src->mt_numdbs; --i >= 0; ) {
2542 if ((mc = src->mt_cursors[i]) != NULL) {
2543 size = sizeof(MDB_cursor);
2544 if (mc->mc_xcursor)
2545 size += sizeof(MDB_xcursor);
2546 for (; mc; mc = bk->mc_next) {
2547 bk = malloc(size);
2548 if (!bk)
2549 return ENOMEM;
2550 *bk = *mc;
2551 mc->mc_backup = bk;
2552 mc->mc_db = &dst->mt_dbs[i];
2553 /* Kill pointers into src to reduce abuse: The
2554 * user may not use mc until dst ends. But we need a valid
2555 * txn pointer here for cursor fixups to keep working.
2556 */
2557 mc->mc_txn = dst;
2558 mc->mc_dbflag = &dst->mt_dbflags[i];
2559 if ((mx = mc->mc_xcursor) != NULL) {
2560 *(MDB_xcursor *)(bk+1) = *mx;
2561 mx->mx_cursor.mc_txn = dst;
2562 }
2563 mc->mc_next = dst->mt_cursors[i];
2564 dst->mt_cursors[i] = mc;
2565 }
2566 }
2567 }
2568 return MDB_SUCCESS;
2569 }
2570
2571 /** Close this write txn's cursors, give parent txn's cursors back to parent.
2572 * @param[in] txn the transaction handle.
2573 * @param[in] merge true to keep changes to parent cursors, false to revert.
2574 * @return 0 on success, non-zero on failure.
2575 */
2576 static void
mdb_cursors_close(MDB_txn * txn,unsigned merge)2577 mdb_cursors_close(MDB_txn *txn, unsigned merge)
2578 {
2579 MDB_cursor **cursors = txn->mt_cursors, *mc, *next, *bk;
2580 MDB_xcursor *mx;
2581 int i;
2582
2583 for (i = txn->mt_numdbs; --i >= 0; ) {
2584 for (mc = cursors[i]; mc; mc = next) {
2585 next = mc->mc_next;
2586 if ((bk = mc->mc_backup) != NULL) {
2587 if (merge) {
2588 /* Commit changes to parent txn */
2589 mc->mc_next = bk->mc_next;
2590 mc->mc_backup = bk->mc_backup;
2591 mc->mc_txn = bk->mc_txn;
2592 mc->mc_db = bk->mc_db;
2593 mc->mc_dbflag = bk->mc_dbflag;
2594 if ((mx = mc->mc_xcursor) != NULL)
2595 mx->mx_cursor.mc_txn = bk->mc_txn;
2596 } else {
2597 /* Abort nested txn */
2598 *mc = *bk;
2599 if ((mx = mc->mc_xcursor) != NULL)
2600 *mx = *(MDB_xcursor *)(bk+1);
2601 }
2602 mc = bk;
2603 }
2604 /* Only malloced cursors are permanently tracked. */
2605 free(mc);
2606 }
2607 cursors[i] = NULL;
2608 }
2609 }
2610
2611 #if !(MDB_PIDLOCK) /* Currently the same as defined(_WIN32) */
2612 enum Pidlock_op {
2613 Pidset, Pidcheck
2614 };
2615 #else
2616 enum Pidlock_op {
2617 Pidset = F_SETLK, Pidcheck = F_GETLK
2618 };
2619 #endif
2620
2621 /** Set or check a pid lock. Set returns 0 on success.
2622 * Check returns 0 if the process is certainly dead, nonzero if it may
2623 * be alive (the lock exists or an error happened so we do not know).
2624 *
2625 * On Windows Pidset is a no-op, we merely check for the existence
2626 * of the process with the given pid. On POSIX we use a single byte
2627 * lock on the lockfile, set at an offset equal to the pid.
2628 */
2629 static int
mdb_reader_pid(MDB_env * env,enum Pidlock_op op,MDB_PID_T pid)2630 mdb_reader_pid(MDB_env *env, enum Pidlock_op op, MDB_PID_T pid)
2631 {
2632 #if !(MDB_PIDLOCK) /* Currently the same as defined(_WIN32) */
2633 int ret = 0;
2634 HANDLE h;
2635 if (op == Pidcheck) {
2636 h = OpenProcess(env->me_pidquery, FALSE, pid);
2637 /* No documented "no such process" code, but other program use this: */
2638 if (!h)
2639 return ErrCode() != ERROR_INVALID_PARAMETER;
2640 /* A process exists until all handles to it close. Has it exited? */
2641 ret = WaitForSingleObject(h, 0) != 0;
2642 CloseHandle(h);
2643 }
2644 return ret;
2645 #else
2646 for (;;) {
2647 int rc;
2648 struct flock lock_info;
2649 memset(&lock_info, 0, sizeof(lock_info));
2650 lock_info.l_type = F_WRLCK;
2651 lock_info.l_whence = SEEK_SET;
2652 lock_info.l_start = pid;
2653 lock_info.l_len = 1;
2654 if ((rc = fcntl(env->me_lfd, op, &lock_info)) == 0) {
2655 if (op == F_GETLK && lock_info.l_type != F_UNLCK)
2656 rc = -1;
2657 } else if ((rc = ErrCode()) == EINTR) {
2658 continue;
2659 }
2660 return rc;
2661 }
2662 #endif
2663 }
2664
2665 /** Common code for #mdb_txn_begin() and #mdb_txn_renew().
2666 * @param[in] txn the transaction handle to initialize
2667 * @return 0 on success, non-zero on failure.
2668 */
2669 static int
mdb_txn_renew0(MDB_txn * txn)2670 mdb_txn_renew0(MDB_txn *txn)
2671 {
2672 MDB_env *env = txn->mt_env;
2673 MDB_txninfo *ti = env->me_txns;
2674 MDB_meta *meta;
2675 unsigned int i, nr, flags = txn->mt_flags;
2676 uint16_t x;
2677 int rc, new_notls = 0;
2678
2679 if ((flags &= MDB_TXN_RDONLY) != 0) {
2680 if (!ti) {
2681 meta = mdb_env_pick_meta(env);
2682 txn->mt_txnid = meta->mm_txnid;
2683 txn->mt_u.reader = NULL;
2684 } else {
2685 MDB_reader *r = (env->me_flags & MDB_NOTLS) ? txn->mt_u.reader :
2686 pthread_getspecific(env->me_txkey);
2687 if (r) {
2688 if (r->mr_pid != env->me_pid || r->mr_txnid != (txnid_t)-1)
2689 return MDB_BAD_RSLOT;
2690 } else {
2691 MDB_PID_T pid = env->me_pid;
2692 MDB_THR_T tid = pthread_self();
2693 mdb_mutexref_t rmutex = env->me_rmutex;
2694
2695 if (!env->me_live_reader) {
2696 rc = mdb_reader_pid(env, Pidset, pid);
2697 if (rc)
2698 return rc;
2699 env->me_live_reader = 1;
2700 }
2701
2702 if (LOCK_MUTEX(rc, env, rmutex))
2703 return rc;
2704 nr = ti->mti_numreaders;
2705 for (i=0; i<nr; i++)
2706 if (ti->mti_readers[i].mr_pid == 0)
2707 break;
2708 if (i == env->me_maxreaders) {
2709 UNLOCK_MUTEX(rmutex);
2710 return MDB_READERS_FULL;
2711 }
2712 r = &ti->mti_readers[i];
2713 /* Claim the reader slot, carefully since other code
2714 * uses the reader table un-mutexed: First reset the
2715 * slot, next publish it in mti_numreaders. After
2716 * that, it is safe for mdb_env_close() to touch it.
2717 * When it will be closed, we can finally claim it.
2718 */
2719 r->mr_pid = 0;
2720 r->mr_txnid = (txnid_t)-1;
2721 r->mr_tid = tid;
2722 if (i == nr)
2723 ti->mti_numreaders = ++nr;
2724 env->me_close_readers = nr;
2725 r->mr_pid = pid;
2726 UNLOCK_MUTEX(rmutex);
2727
2728 new_notls = (env->me_flags & MDB_NOTLS);
2729 if (!new_notls && (rc=pthread_setspecific(env->me_txkey, r))) {
2730 r->mr_pid = 0;
2731 return rc;
2732 }
2733 }
2734 do /* LY: Retry on a race, ITS#7970. */
2735 r->mr_txnid = ti->mti_txnid;
2736 while(r->mr_txnid != ti->mti_txnid);
2737 txn->mt_txnid = r->mr_txnid;
2738 txn->mt_u.reader = r;
2739 meta = env->me_metas[txn->mt_txnid & 1];
2740 }
2741
2742 } else {
2743 /* Not yet touching txn == env->me_txn0, it may be active */
2744 if (ti) {
2745 if (LOCK_MUTEX(rc, env, env->me_wmutex))
2746 return rc;
2747 txn->mt_txnid = ti->mti_txnid;
2748 meta = env->me_metas[txn->mt_txnid & 1];
2749 } else {
2750 meta = mdb_env_pick_meta(env);
2751 txn->mt_txnid = meta->mm_txnid;
2752 }
2753 txn->mt_txnid++;
2754 #if MDB_DEBUG
2755 if (txn->mt_txnid == mdb_debug_start)
2756 mdb_debug = 1;
2757 #endif
2758 txn->mt_child = NULL;
2759 txn->mt_loose_pgs = NULL;
2760 txn->mt_loose_count = 0;
2761 txn->mt_dirty_room = MDB_IDL_UM_MAX;
2762 txn->mt_u.dirty_list = env->me_dirty_list;
2763 txn->mt_u.dirty_list[0].mid = 0;
2764 txn->mt_free_pgs = env->me_free_pgs;
2765 txn->mt_free_pgs[0] = 0;
2766 txn->mt_spill_pgs = NULL;
2767 env->me_txn = txn;
2768 memcpy(txn->mt_dbiseqs, env->me_dbiseqs, env->me_maxdbs * sizeof(unsigned int));
2769 }
2770
2771 /* Copy the DB info and flags */
2772 memcpy(txn->mt_dbs, meta->mm_dbs, CORE_DBS * sizeof(MDB_db));
2773
2774 /* Moved to here to avoid a data race in read TXNs */
2775 txn->mt_next_pgno = meta->mm_last_pg+1;
2776
2777 txn->mt_flags = flags;
2778
2779 /* Setup db info */
2780 txn->mt_numdbs = env->me_numdbs;
2781 for (i=CORE_DBS; i<txn->mt_numdbs; i++) {
2782 x = env->me_dbflags[i];
2783 txn->mt_dbs[i].md_flags = x & PERSISTENT_FLAGS;
2784 txn->mt_dbflags[i] = (x & MDB_VALID) ? DB_VALID|DB_USRVALID|DB_STALE : 0;
2785 }
2786 txn->mt_dbflags[MAIN_DBI] = DB_VALID|DB_USRVALID;
2787 txn->mt_dbflags[FREE_DBI] = DB_VALID;
2788
2789 if (env->me_flags & MDB_FATAL_ERROR) {
2790 DPUTS("environment had fatal error, must shutdown!");
2791 rc = MDB_PANIC;
2792 } else if (env->me_maxpg < txn->mt_next_pgno) {
2793 rc = MDB_MAP_RESIZED;
2794 } else {
2795 return MDB_SUCCESS;
2796 }
2797 mdb_txn_end(txn, new_notls /*0 or MDB_END_SLOT*/ | MDB_END_FAIL_BEGIN);
2798 return rc;
2799 }
2800
2801 int
mdb_txn_renew(MDB_txn * txn)2802 mdb_txn_renew(MDB_txn *txn)
2803 {
2804 int rc;
2805
2806 if (!txn || !F_ISSET(txn->mt_flags, MDB_TXN_RDONLY|MDB_TXN_FINISHED))
2807 return EINVAL;
2808
2809 rc = mdb_txn_renew0(txn);
2810 if (rc == MDB_SUCCESS) {
2811 DPRINTF(("renew txn %"Z"u%c %p on mdbenv %p, root page %"Z"u",
2812 txn->mt_txnid, (txn->mt_flags & MDB_TXN_RDONLY) ? 'r' : 'w',
2813 (void *)txn, (void *)txn->mt_env, txn->mt_dbs[MAIN_DBI].md_root));
2814 }
2815 return rc;
2816 }
2817
2818 int
mdb_txn_begin(MDB_env * env,MDB_txn * parent,unsigned int flags,MDB_txn ** ret)2819 mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
2820 {
2821 MDB_txn *txn;
2822 MDB_ntxn *ntxn;
2823 int rc, size, tsize;
2824
2825 flags &= MDB_TXN_BEGIN_FLAGS;
2826 flags |= env->me_flags & MDB_WRITEMAP;
2827
2828 if (env->me_flags & MDB_RDONLY & ~flags) /* write txn in RDONLY env */
2829 return EACCES;
2830
2831 if (parent) {
2832 /* Nested transactions: Max 1 child, write txns only, no writemap */
2833 flags |= parent->mt_flags;
2834 if (flags & (MDB_RDONLY|MDB_WRITEMAP|MDB_TXN_BLOCKED)) {
2835 return (parent->mt_flags & MDB_TXN_RDONLY) ? EINVAL : MDB_BAD_TXN;
2836 }
2837 /* Child txns save MDB_pgstate and use own copy of cursors */
2838 size = env->me_maxdbs * (sizeof(MDB_db)+sizeof(MDB_cursor *)+1);
2839 size += tsize = sizeof(MDB_ntxn);
2840 } else if (flags & MDB_RDONLY) {
2841 size = env->me_maxdbs * (sizeof(MDB_db)+1);
2842 size += tsize = sizeof(MDB_txn);
2843 } else {
2844 /* Reuse preallocated write txn. However, do not touch it until
2845 * mdb_txn_renew0() succeeds, since it currently may be active.
2846 */
2847 txn = env->me_txn0;
2848 goto renew;
2849 }
2850 if ((txn = calloc(1, size)) == NULL) {
2851 DPRINTF(("calloc: %s", strerror(errno)));
2852 return ENOMEM;
2853 }
2854 txn->mt_dbxs = env->me_dbxs; /* static */
2855 txn->mt_dbs = (MDB_db *) ((char *)txn + tsize);
2856 txn->mt_dbflags = (unsigned char *)txn + size - env->me_maxdbs;
2857 txn->mt_flags = flags;
2858 txn->mt_env = env;
2859
2860 if (parent) {
2861 unsigned int i;
2862 txn->mt_cursors = (MDB_cursor **)(txn->mt_dbs + env->me_maxdbs);
2863 txn->mt_dbiseqs = parent->mt_dbiseqs;
2864 txn->mt_u.dirty_list = malloc(sizeof(MDB_ID2)*MDB_IDL_UM_SIZE);
2865 if (!txn->mt_u.dirty_list ||
2866 !(txn->mt_free_pgs = mdb_midl_alloc(MDB_IDL_UM_MAX)))
2867 {
2868 free(txn->mt_u.dirty_list);
2869 free(txn);
2870 return ENOMEM;
2871 }
2872 txn->mt_txnid = parent->mt_txnid;
2873 txn->mt_dirty_room = parent->mt_dirty_room;
2874 txn->mt_u.dirty_list[0].mid = 0;
2875 txn->mt_spill_pgs = NULL;
2876 txn->mt_next_pgno = parent->mt_next_pgno;
2877 parent->mt_flags |= MDB_TXN_HAS_CHILD;
2878 parent->mt_child = txn;
2879 txn->mt_parent = parent;
2880 txn->mt_numdbs = parent->mt_numdbs;
2881 memcpy(txn->mt_dbs, parent->mt_dbs, txn->mt_numdbs * sizeof(MDB_db));
2882 /* Copy parent's mt_dbflags, but clear DB_NEW */
2883 for (i=0; i<txn->mt_numdbs; i++)
2884 txn->mt_dbflags[i] = parent->mt_dbflags[i] & ~DB_NEW;
2885 rc = 0;
2886 ntxn = (MDB_ntxn *)txn;
2887 ntxn->mnt_pgstate = env->me_pgstate; /* save parent me_pghead & co */
2888 if (env->me_pghead) {
2889 size = MDB_IDL_SIZEOF(env->me_pghead);
2890 env->me_pghead = mdb_midl_alloc(env->me_pghead[0]);
2891 if (env->me_pghead)
2892 memcpy(env->me_pghead, ntxn->mnt_pgstate.mf_pghead, size);
2893 else
2894 rc = ENOMEM;
2895 }
2896 if (!rc)
2897 rc = mdb_cursor_shadow(parent, txn);
2898 if (rc)
2899 mdb_txn_end(txn, MDB_END_FAIL_BEGINCHILD);
2900 } else { /* MDB_RDONLY */
2901 txn->mt_dbiseqs = env->me_dbiseqs;
2902 renew:
2903 rc = mdb_txn_renew0(txn);
2904 }
2905 if (rc) {
2906 if (txn != env->me_txn0)
2907 free(txn);
2908 } else {
2909 txn->mt_flags |= flags; /* could not change txn=me_txn0 earlier */
2910 *ret = txn;
2911 DPRINTF(("begin txn %"Z"u%c %p on mdbenv %p, root page %"Z"u",
2912 txn->mt_txnid, (flags & MDB_RDONLY) ? 'r' : 'w',
2913 (void *) txn, (void *) env, txn->mt_dbs[MAIN_DBI].md_root));
2914 }
2915
2916 return rc;
2917 }
2918
2919 MDB_env *
mdb_txn_env(MDB_txn * txn)2920 mdb_txn_env(MDB_txn *txn)
2921 {
2922 if(!txn) return NULL;
2923 return txn->mt_env;
2924 }
2925
2926 size_t
mdb_txn_id(MDB_txn * txn)2927 mdb_txn_id(MDB_txn *txn)
2928 {
2929 if(!txn) return 0;
2930 return txn->mt_txnid;
2931 }
2932
2933 /** Export or close DBI handles opened in this txn. */
2934 static void
mdb_dbis_update(MDB_txn * txn,int keep)2935 mdb_dbis_update(MDB_txn *txn, int keep)
2936 {
2937 int i;
2938 MDB_dbi n = txn->mt_numdbs;
2939 MDB_env *env = txn->mt_env;
2940 unsigned char *tdbflags = txn->mt_dbflags;
2941
2942 for (i = n; --i >= CORE_DBS;) {
2943 if (tdbflags[i] & DB_NEW) {
2944 if (keep) {
2945 env->me_dbflags[i] = txn->mt_dbs[i].md_flags | MDB_VALID;
2946 } else {
2947 char *ptr = env->me_dbxs[i].md_name.mv_data;
2948 if (ptr) {
2949 env->me_dbxs[i].md_name.mv_data = NULL;
2950 env->me_dbxs[i].md_name.mv_size = 0;
2951 env->me_dbflags[i] = 0;
2952 env->me_dbiseqs[i]++;
2953 free(ptr);
2954 }
2955 }
2956 }
2957 }
2958 if (keep && env->me_numdbs < n)
2959 env->me_numdbs = n;
2960 }
2961
2962 /** End a transaction, except successful commit of a nested transaction.
2963 * May be called twice for readonly txns: First reset it, then abort.
2964 * @param[in] txn the transaction handle to end
2965 * @param[in] mode why and how to end the transaction
2966 */
2967 static void
mdb_txn_end(MDB_txn * txn,unsigned mode)2968 mdb_txn_end(MDB_txn *txn, unsigned mode)
2969 {
2970 MDB_env *env = txn->mt_env;
2971 #if MDB_DEBUG
2972 static const char *const names[] = MDB_END_NAMES;
2973 #endif
2974
2975 /* Export or close DBI handles opened in this txn */
2976 mdb_dbis_update(txn, mode & MDB_END_UPDATE);
2977
2978 DPRINTF(("%s txn %"Z"u%c %p on mdbenv %p, root page %"Z"u",
2979 names[mode & MDB_END_OPMASK],
2980 txn->mt_txnid, (txn->mt_flags & MDB_TXN_RDONLY) ? 'r' : 'w',
2981 (void *) txn, (void *)env, txn->mt_dbs[MAIN_DBI].md_root));
2982
2983 if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
2984 if (txn->mt_u.reader) {
2985 txn->mt_u.reader->mr_txnid = (txnid_t)-1;
2986 if (!(env->me_flags & MDB_NOTLS)) {
2987 txn->mt_u.reader = NULL; /* txn does not own reader */
2988 } else if (mode & MDB_END_SLOT) {
2989 txn->mt_u.reader->mr_pid = 0;
2990 txn->mt_u.reader = NULL;
2991 } /* else txn owns the slot until it does MDB_END_SLOT */
2992 }
2993 txn->mt_numdbs = 0; /* prevent further DBI activity */
2994 txn->mt_flags |= MDB_TXN_FINISHED;
2995
2996 } else if (!F_ISSET(txn->mt_flags, MDB_TXN_FINISHED)) {
2997 pgno_t *pghead = env->me_pghead;
2998
2999 if (!(mode & MDB_END_UPDATE)) /* !(already closed cursors) */
3000 mdb_cursors_close(txn, 0);
3001 if (!(env->me_flags & MDB_WRITEMAP)) {
3002 mdb_dlist_free(txn);
3003 }
3004
3005 txn->mt_numdbs = 0;
3006 txn->mt_flags = MDB_TXN_FINISHED;
3007
3008 if (!txn->mt_parent) {
3009 mdb_midl_shrink(&txn->mt_free_pgs);
3010 env->me_free_pgs = txn->mt_free_pgs;
3011 /* me_pgstate: */
3012 env->me_pghead = NULL;
3013 env->me_pglast = 0;
3014
3015 env->me_txn = NULL;
3016 mode = 0; /* txn == env->me_txn0, do not free() it */
3017
3018 /* The writer mutex was locked in mdb_txn_begin. */
3019 if (env->me_txns)
3020 UNLOCK_MUTEX(env->me_wmutex);
3021 } else {
3022 txn->mt_parent->mt_child = NULL;
3023 txn->mt_parent->mt_flags &= ~MDB_TXN_HAS_CHILD;
3024 env->me_pgstate = ((MDB_ntxn *)txn)->mnt_pgstate;
3025 mdb_midl_free(txn->mt_free_pgs);
3026 mdb_midl_free(txn->mt_spill_pgs);
3027 free(txn->mt_u.dirty_list);
3028 }
3029
3030 mdb_midl_free(pghead);
3031 }
3032
3033 if (mode & MDB_END_FREE)
3034 free(txn);
3035 }
3036
3037 void
mdb_txn_reset(MDB_txn * txn)3038 mdb_txn_reset(MDB_txn *txn)
3039 {
3040 if (txn == NULL)
3041 return;
3042
3043 /* This call is only valid for read-only txns */
3044 if (!(txn->mt_flags & MDB_TXN_RDONLY))
3045 return;
3046
3047 mdb_txn_end(txn, MDB_END_RESET);
3048 }
3049
3050 void
mdb_txn_abort(MDB_txn * txn)3051 mdb_txn_abort(MDB_txn *txn)
3052 {
3053 if (txn == NULL)
3054 return;
3055
3056 if (txn->mt_child)
3057 mdb_txn_abort(txn->mt_child);
3058
3059 mdb_txn_end(txn, MDB_END_ABORT|MDB_END_SLOT|MDB_END_FREE);
3060 }
3061
3062 /** Save the freelist as of this transaction to the freeDB.
3063 * This changes the freelist. Keep trying until it stabilizes.
3064 */
3065 static int
mdb_freelist_save(MDB_txn * txn)3066 mdb_freelist_save(MDB_txn *txn)
3067 {
3068 /* env->me_pghead[] can grow and shrink during this call.
3069 * env->me_pglast and txn->mt_free_pgs[] can only grow.
3070 * Page numbers cannot disappear from txn->mt_free_pgs[].
3071 */
3072 MDB_cursor mc;
3073 MDB_env *env = txn->mt_env;
3074 int rc, maxfree_1pg = env->me_maxfree_1pg, more = 1;
3075 txnid_t pglast = 0, head_id = 0;
3076 pgno_t freecnt = 0, *free_pgs, *mop;
3077 ssize_t head_room = 0, total_room = 0, mop_len, clean_limit;
3078
3079 mdb_cursor_init(&mc, txn, FREE_DBI, NULL);
3080
3081 if (env->me_pghead) {
3082 /* Make sure first page of freeDB is touched and on freelist */
3083 rc = mdb_page_search(&mc, NULL, MDB_PS_FIRST|MDB_PS_MODIFY);
3084 if (rc && rc != MDB_NOTFOUND)
3085 return rc;
3086 }
3087
3088 if (!env->me_pghead && txn->mt_loose_pgs) {
3089 /* Put loose page numbers in mt_free_pgs, since
3090 * we may be unable to return them to me_pghead.
3091 */
3092 MDB_page *mp = txn->mt_loose_pgs;
3093 MDB_ID2 *dl = txn->mt_u.dirty_list;
3094 unsigned x;
3095 if ((rc = mdb_midl_need(&txn->mt_free_pgs, txn->mt_loose_count)) != 0)
3096 return rc;
3097 for (; mp; mp = NEXT_LOOSE_PAGE(mp)) {
3098 mdb_midl_xappend(txn->mt_free_pgs, mp->mp_pgno);
3099 /* must also remove from dirty list */
3100 if (txn->mt_flags & MDB_TXN_WRITEMAP) {
3101 for (x=1; x<=dl[0].mid; x++)
3102 if (dl[x].mid == mp->mp_pgno)
3103 break;
3104 mdb_tassert(txn, x <= dl[0].mid);
3105 } else {
3106 x = mdb_mid2l_search(dl, mp->mp_pgno);
3107 mdb_tassert(txn, dl[x].mid == mp->mp_pgno);
3108 mdb_dpage_free(env, mp);
3109 }
3110 dl[x].mptr = NULL;
3111 }
3112 {
3113 /* squash freed slots out of the dirty list */
3114 unsigned y;
3115 for (y=1; dl[y].mptr && y <= dl[0].mid; y++);
3116 if (y <= dl[0].mid) {
3117 for(x=y, y++;;) {
3118 while (!dl[y].mptr && y <= dl[0].mid) y++;
3119 if (y > dl[0].mid) break;
3120 dl[x++] = dl[y++];
3121 }
3122 dl[0].mid = x-1;
3123 } else {
3124 /* all slots freed */
3125 dl[0].mid = 0;
3126 }
3127 }
3128 txn->mt_loose_pgs = NULL;
3129 txn->mt_loose_count = 0;
3130 }
3131
3132 /* MDB_RESERVE cancels meminit in ovpage malloc (when no WRITEMAP) */
3133 clean_limit = (env->me_flags & (MDB_NOMEMINIT|MDB_WRITEMAP))
3134 ? SSIZE_MAX : maxfree_1pg;
3135
3136 for (;;) {
3137 /* Come back here after each Put() in case freelist changed */
3138 MDB_val key, data;
3139 pgno_t *pgs;
3140 ssize_t j;
3141
3142 /* If using records from freeDB which we have not yet
3143 * deleted, delete them and any we reserved for me_pghead.
3144 */
3145 while (pglast < env->me_pglast) {
3146 rc = mdb_cursor_first(&mc, &key, NULL);
3147 if (rc)
3148 return rc;
3149 pglast = head_id = *(txnid_t *)key.mv_data;
3150 total_room = head_room = 0;
3151 mdb_tassert(txn, pglast <= env->me_pglast);
3152 rc = mdb_cursor_del(&mc, 0);
3153 if (rc)
3154 return rc;
3155 }
3156
3157 /* Save the IDL of pages freed by this txn, to a single record */
3158 if (freecnt < txn->mt_free_pgs[0]) {
3159 if (!freecnt) {
3160 /* Make sure last page of freeDB is touched and on freelist */
3161 rc = mdb_page_search(&mc, NULL, MDB_PS_LAST|MDB_PS_MODIFY);
3162 if (rc && rc != MDB_NOTFOUND)
3163 return rc;
3164 }
3165 free_pgs = txn->mt_free_pgs;
3166 /* Write to last page of freeDB */
3167 key.mv_size = sizeof(txn->mt_txnid);
3168 key.mv_data = &txn->mt_txnid;
3169 do {
3170 freecnt = free_pgs[0];
3171 data.mv_size = MDB_IDL_SIZEOF(free_pgs);
3172 rc = mdb_cursor_put(&mc, &key, &data, MDB_RESERVE);
3173 if (rc)
3174 return rc;
3175 /* Retry if mt_free_pgs[] grew during the Put() */
3176 free_pgs = txn->mt_free_pgs;
3177 } while (freecnt < free_pgs[0]);
3178 mdb_midl_sort(free_pgs);
3179 memcpy(data.mv_data, free_pgs, data.mv_size);
3180 #if (MDB_DEBUG) > 1
3181 {
3182 unsigned int i = free_pgs[0];
3183 DPRINTF(("IDL write txn %"Z"u root %"Z"u num %u",
3184 txn->mt_txnid, txn->mt_dbs[FREE_DBI].md_root, i));
3185 for (; i; i--)
3186 DPRINTF(("IDL %"Z"u", free_pgs[i]));
3187 }
3188 #endif
3189 continue;
3190 }
3191
3192 mop = env->me_pghead;
3193 mop_len = (mop ? mop[0] : 0) + txn->mt_loose_count;
3194
3195 /* Reserve records for me_pghead[]. Split it if multi-page,
3196 * to avoid searching freeDB for a page range. Use keys in
3197 * range [1,me_pglast]: Smaller than txnid of oldest reader.
3198 */
3199 if (total_room >= mop_len) {
3200 if (total_room == mop_len || --more < 0)
3201 break;
3202 } else if (head_room >= maxfree_1pg && head_id > 1) {
3203 /* Keep current record (overflow page), add a new one */
3204 head_id--;
3205 head_room = 0;
3206 }
3207 /* (Re)write {key = head_id, IDL length = head_room} */
3208 total_room -= head_room;
3209 head_room = mop_len - total_room;
3210 if (head_room > maxfree_1pg && head_id > 1) {
3211 /* Overflow multi-page for part of me_pghead */
3212 head_room /= head_id; /* amortize page sizes */
3213 head_room += maxfree_1pg - head_room % (maxfree_1pg + 1);
3214 } else if (head_room < 0) {
3215 /* Rare case, not bothering to delete this record */
3216 head_room = 0;
3217 }
3218 key.mv_size = sizeof(head_id);
3219 key.mv_data = &head_id;
3220 data.mv_size = (head_room + 1) * sizeof(pgno_t);
3221 rc = mdb_cursor_put(&mc, &key, &data, MDB_RESERVE);
3222 if (rc)
3223 return rc;
3224 /* IDL is initially empty, zero out at least the length */
3225 pgs = (pgno_t *)data.mv_data;
3226 j = head_room > clean_limit ? head_room : 0;
3227 do {
3228 pgs[j] = 0;
3229 } while (--j >= 0);
3230 total_room += head_room;
3231 }
3232
3233 /* Return loose page numbers to me_pghead, though usually none are
3234 * left at this point. The pages themselves remain in dirty_list.
3235 */
3236 if (txn->mt_loose_pgs) {
3237 MDB_page *mp = txn->mt_loose_pgs;
3238 unsigned count = txn->mt_loose_count;
3239 MDB_IDL loose;
3240 /* Room for loose pages + temp IDL with same */
3241 if ((rc = mdb_midl_need(&env->me_pghead, 2*count+1)) != 0)
3242 return rc;
3243 mop = env->me_pghead;
3244 loose = mop + MDB_IDL_ALLOCLEN(mop) - count;
3245 for (count = 0; mp; mp = NEXT_LOOSE_PAGE(mp))
3246 loose[ ++count ] = mp->mp_pgno;
3247 loose[0] = count;
3248 mdb_midl_sort(loose);
3249 mdb_midl_xmerge(mop, loose);
3250 txn->mt_loose_pgs = NULL;
3251 txn->mt_loose_count = 0;
3252 mop_len = mop[0];
3253 }
3254
3255 /* Fill in the reserved me_pghead records */
3256 rc = MDB_SUCCESS;
3257 if (mop_len) {
3258 MDB_val key, data;
3259
3260 mop += mop_len;
3261 rc = mdb_cursor_first(&mc, &key, &data);
3262 for (; !rc; rc = mdb_cursor_next(&mc, &key, &data, MDB_NEXT)) {
3263 txnid_t id = *(txnid_t *)key.mv_data;
3264 ssize_t len = (ssize_t)(data.mv_size / sizeof(MDB_ID)) - 1;
3265 MDB_ID save;
3266
3267 mdb_tassert(txn, len >= 0 && id <= env->me_pglast);
3268 key.mv_data = &id;
3269 if (len > mop_len) {
3270 len = mop_len;
3271 data.mv_size = (len + 1) * sizeof(MDB_ID);
3272 }
3273 data.mv_data = mop -= len;
3274 save = mop[0];
3275 mop[0] = len;
3276 rc = mdb_cursor_put(&mc, &key, &data, MDB_CURRENT);
3277 mop[0] = save;
3278 if (rc || !(mop_len -= len))
3279 break;
3280 }
3281 }
3282 return rc;
3283 }
3284
3285 /** Flush (some) dirty pages to the map, after clearing their dirty flag.
3286 * @param[in] txn the transaction that's being committed
3287 * @param[in] keep number of initial pages in dirty_list to keep dirty.
3288 * @return 0 on success, non-zero on failure.
3289 */
3290 static int
mdb_page_flush(MDB_txn * txn,int keep)3291 mdb_page_flush(MDB_txn *txn, int keep)
3292 {
3293 MDB_env *env = txn->mt_env;
3294 MDB_ID2L dl = txn->mt_u.dirty_list;
3295 unsigned psize = env->me_psize, j;
3296 int i, pagecount = dl[0].mid, rc;
3297 size_t size = 0, pos = 0;
3298 pgno_t pgno = 0;
3299 MDB_page *dp = NULL;
3300 #ifdef _WIN32
3301 OVERLAPPED ov;
3302 #else
3303 struct iovec iov[MDB_COMMIT_PAGES];
3304 ssize_t wpos = 0, wsize = 0, wres;
3305 size_t next_pos = 1; /* impossible pos, so pos != next_pos */
3306 int n = 0;
3307 #endif
3308
3309 j = i = keep;
3310
3311 if (env->me_flags & MDB_WRITEMAP) {
3312 /* Clear dirty flags */
3313 while (++i <= pagecount) {
3314 dp = dl[i].mptr;
3315 /* Don't flush this page yet */
3316 if (dp->mp_flags & (P_LOOSE|P_KEEP)) {
3317 dp->mp_flags &= ~P_KEEP;
3318 dl[++j] = dl[i];
3319 continue;
3320 }
3321 dp->mp_flags &= ~P_DIRTY;
3322 }
3323 goto done;
3324 }
3325
3326 /* Write the pages */
3327 for (;;) {
3328 if (++i <= pagecount) {
3329 dp = dl[i].mptr;
3330 /* Don't flush this page yet */
3331 if (dp->mp_flags & (P_LOOSE|P_KEEP)) {
3332 dp->mp_flags &= ~P_KEEP;
3333 dl[i].mid = 0;
3334 continue;
3335 }
3336 pgno = dl[i].mid;
3337 /* clear dirty flag */
3338 dp->mp_flags &= ~P_DIRTY;
3339 pos = pgno * psize;
3340 size = psize;
3341 if (IS_OVERFLOW(dp)) size *= dp->mp_pages;
3342 }
3343 #ifdef _WIN32
3344 else break;
3345
3346 /* Windows actually supports scatter/gather I/O, but only on
3347 * unbuffered file handles. Since we're relying on the OS page
3348 * cache for all our data, that's self-defeating. So we just
3349 * write pages one at a time. We use the ov structure to set
3350 * the write offset, to at least save the overhead of a Seek
3351 * system call.
3352 */
3353 DPRINTF(("committing page %"Z"u", pgno));
3354 memset(&ov, 0, sizeof(ov));
3355 ov.Offset = pos & 0xffffffff;
3356 ov.OffsetHigh = pos >> 16 >> 16;
3357 if (!WriteFile(env->me_fd, dp, size, NULL, &ov)) {
3358 rc = ErrCode();
3359 DPRINTF(("WriteFile: %d", rc));
3360 return rc;
3361 }
3362 #else
3363 /* Write up to MDB_COMMIT_PAGES dirty pages at a time. */
3364 if (pos!=next_pos || n==MDB_COMMIT_PAGES || wsize+size>MAX_WRITE) {
3365 if (n) {
3366 retry_write:
3367 /* Write previous page(s) */
3368 #ifdef MDB_USE_PWRITEV
3369 wres = pwritev(env->me_fd, iov, n, wpos);
3370 #else
3371 if (n == 1) {
3372 wres = pwrite(env->me_fd, iov[0].iov_base, wsize, wpos);
3373 } else {
3374 retry_seek:
3375 if (lseek(env->me_fd, wpos, SEEK_SET) == -1) {
3376 rc = ErrCode();
3377 if (rc == EINTR)
3378 goto retry_seek;
3379 DPRINTF(("lseek: %s", strerror(rc)));
3380 return rc;
3381 }
3382 wres = writev(env->me_fd, iov, n);
3383 }
3384 #endif
3385 if (wres != wsize) {
3386 if (wres < 0) {
3387 rc = ErrCode();
3388 if (rc == EINTR)
3389 goto retry_write;
3390 DPRINTF(("Write error: %s", strerror(rc)));
3391 } else {
3392 rc = EIO; /* TODO: Use which error code? */
3393 DPUTS("short write, filesystem full?");
3394 }
3395 return rc;
3396 }
3397 n = 0;
3398 }
3399 if (i > pagecount)
3400 break;
3401 wpos = pos;
3402 wsize = 0;
3403 }
3404 DPRINTF(("committing page %"Z"u", pgno));
3405 next_pos = pos + size;
3406 iov[n].iov_len = size;
3407 iov[n].iov_base = (char *)dp;
3408 wsize += size;
3409 n++;
3410 #endif /* _WIN32 */
3411 }
3412
3413 /* MIPS has cache coherency issues, this is a no-op everywhere else
3414 * Note: for any size >= on-chip cache size, entire on-chip cache is
3415 * flushed.
3416 */
3417 CACHEFLUSH(env->me_map, txn->mt_next_pgno * env->me_psize, DCACHE);
3418
3419 for (i = keep; ++i <= pagecount; ) {
3420 dp = dl[i].mptr;
3421 /* This is a page we skipped above */
3422 if (!dl[i].mid) {
3423 dl[++j] = dl[i];
3424 dl[j].mid = dp->mp_pgno;
3425 continue;
3426 }
3427 mdb_dpage_free(env, dp);
3428 }
3429
3430 done:
3431 i--;
3432 txn->mt_dirty_room += i - j;
3433 dl[0].mid = j;
3434 return MDB_SUCCESS;
3435 }
3436
3437 int
mdb_txn_commit(MDB_txn * txn)3438 mdb_txn_commit(MDB_txn *txn)
3439 {
3440 int rc;
3441 unsigned int i, end_mode;
3442 MDB_env *env;
3443
3444 if (txn == NULL)
3445 return EINVAL;
3446
3447 /* mdb_txn_end() mode for a commit which writes nothing */
3448 end_mode = MDB_END_EMPTY_COMMIT|MDB_END_UPDATE|MDB_END_SLOT|MDB_END_FREE;
3449
3450 if (txn->mt_child) {
3451 rc = mdb_txn_commit(txn->mt_child);
3452 if (rc)
3453 goto fail;
3454 }
3455
3456 env = txn->mt_env;
3457
3458 if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
3459 goto done;
3460 }
3461
3462 if (txn->mt_flags & (MDB_TXN_FINISHED|MDB_TXN_ERROR)) {
3463 DPUTS("txn has failed/finished, can't commit");
3464 if (txn->mt_parent)
3465 txn->mt_parent->mt_flags |= MDB_TXN_ERROR;
3466 rc = MDB_BAD_TXN;
3467 goto fail;
3468 }
3469
3470 if (txn->mt_parent) {
3471 MDB_txn *parent = txn->mt_parent;
3472 MDB_page **lp;
3473 MDB_ID2L dst, src;
3474 MDB_IDL pspill;
3475 unsigned x, y, len, ps_len;
3476
3477 /* Append our free list to parent's */
3478 rc = mdb_midl_append_list(&parent->mt_free_pgs, txn->mt_free_pgs);
3479 if (rc)
3480 goto fail;
3481 mdb_midl_free(txn->mt_free_pgs);
3482 /* Failures after this must either undo the changes
3483 * to the parent or set MDB_TXN_ERROR in the parent.
3484 */
3485
3486 parent->mt_next_pgno = txn->mt_next_pgno;
3487 parent->mt_flags = txn->mt_flags;
3488
3489 /* Merge our cursors into parent's and close them */
3490 mdb_cursors_close(txn, 1);
3491
3492 /* Update parent's DB table. */
3493 memcpy(parent->mt_dbs, txn->mt_dbs, txn->mt_numdbs * sizeof(MDB_db));
3494 parent->mt_numdbs = txn->mt_numdbs;
3495 parent->mt_dbflags[FREE_DBI] = txn->mt_dbflags[FREE_DBI];
3496 parent->mt_dbflags[MAIN_DBI] = txn->mt_dbflags[MAIN_DBI];
3497 for (i=CORE_DBS; i<txn->mt_numdbs; i++) {
3498 /* preserve parent's DB_NEW status */
3499 x = parent->mt_dbflags[i] & DB_NEW;
3500 parent->mt_dbflags[i] = txn->mt_dbflags[i] | x;
3501 }
3502
3503 dst = parent->mt_u.dirty_list;
3504 src = txn->mt_u.dirty_list;
3505 /* Remove anything in our dirty list from parent's spill list */
3506 if ((pspill = parent->mt_spill_pgs) && (ps_len = pspill[0])) {
3507 x = y = ps_len;
3508 pspill[0] = (pgno_t)-1;
3509 /* Mark our dirty pages as deleted in parent spill list */
3510 for (i=0, len=src[0].mid; ++i <= len; ) {
3511 MDB_ID pn = src[i].mid << 1;
3512 while (pn > pspill[x])
3513 x--;
3514 if (pn == pspill[x]) {
3515 pspill[x] = 1;
3516 y = --x;
3517 }
3518 }
3519 /* Squash deleted pagenums if we deleted any */
3520 for (x=y; ++x <= ps_len; )
3521 if (!(pspill[x] & 1))
3522 pspill[++y] = pspill[x];
3523 pspill[0] = y;
3524 }
3525
3526 /* Remove anything in our spill list from parent's dirty list */
3527 if (txn->mt_spill_pgs && txn->mt_spill_pgs[0]) {
3528 for (i=1; i<=txn->mt_spill_pgs[0]; i++) {
3529 MDB_ID pn = txn->mt_spill_pgs[i];
3530 if (pn & 1)
3531 continue; /* deleted spillpg */
3532 pn >>= 1;
3533 y = mdb_mid2l_search(dst, pn);
3534 if (y <= dst[0].mid && dst[y].mid == pn) {
3535 free(dst[y].mptr);
3536 while (y < dst[0].mid) {
3537 dst[y] = dst[y+1];
3538 y++;
3539 }
3540 dst[0].mid--;
3541 }
3542 }
3543 }
3544
3545 /* Find len = length of merging our dirty list with parent's */
3546 x = dst[0].mid;
3547 dst[0].mid = 0; /* simplify loops */
3548 if (parent->mt_parent) {
3549 len = x + src[0].mid;
3550 y = mdb_mid2l_search(src, dst[x].mid + 1) - 1;
3551 for (i = x; y && i; y--) {
3552 pgno_t yp = src[y].mid;
3553 while (yp < dst[i].mid)
3554 i--;
3555 if (yp == dst[i].mid) {
3556 i--;
3557 len--;
3558 }
3559 }
3560 } else { /* Simplify the above for single-ancestor case */
3561 len = MDB_IDL_UM_MAX - txn->mt_dirty_room;
3562 }
3563 /* Merge our dirty list with parent's */
3564 y = src[0].mid;
3565 for (i = len; y; dst[i--] = src[y--]) {
3566 pgno_t yp = src[y].mid;
3567 while (yp < dst[x].mid)
3568 dst[i--] = dst[x--];
3569 if (yp == dst[x].mid)
3570 free(dst[x--].mptr);
3571 }
3572 mdb_tassert(txn, i == x);
3573 dst[0].mid = len;
3574 free(txn->mt_u.dirty_list);
3575 parent->mt_dirty_room = txn->mt_dirty_room;
3576 if (txn->mt_spill_pgs) {
3577 if (parent->mt_spill_pgs) {
3578 /* TODO: Prevent failure here, so parent does not fail */
3579 rc = mdb_midl_append_list(&parent->mt_spill_pgs, txn->mt_spill_pgs);
3580 if (rc)
3581 parent->mt_flags |= MDB_TXN_ERROR;
3582 mdb_midl_free(txn->mt_spill_pgs);
3583 mdb_midl_sort(parent->mt_spill_pgs);
3584 } else {
3585 parent->mt_spill_pgs = txn->mt_spill_pgs;
3586 }
3587 }
3588
3589 /* Append our loose page list to parent's */
3590 for (lp = &parent->mt_loose_pgs; *lp; lp = &NEXT_LOOSE_PAGE(*lp))
3591 ;
3592 *lp = txn->mt_loose_pgs;
3593 parent->mt_loose_count += txn->mt_loose_count;
3594
3595 parent->mt_child = NULL;
3596 mdb_midl_free(((MDB_ntxn *)txn)->mnt_pgstate.mf_pghead);
3597 free(txn);
3598 return rc;
3599 }
3600
3601 if (txn != env->me_txn) {
3602 DPUTS("attempt to commit unknown transaction");
3603 rc = EINVAL;
3604 goto fail;
3605 }
3606
3607 mdb_cursors_close(txn, 0);
3608
3609 if (!txn->mt_u.dirty_list[0].mid &&
3610 !(txn->mt_flags & (MDB_TXN_DIRTY|MDB_TXN_SPILLS)))
3611 goto done;
3612
3613 DPRINTF(("committing txn %"Z"u %p on mdbenv %p, root page %"Z"u",
3614 txn->mt_txnid, (void*)txn, (void*)env, txn->mt_dbs[MAIN_DBI].md_root));
3615
3616 /* Update DB root pointers */
3617 if (txn->mt_numdbs > CORE_DBS) {
3618 MDB_cursor mc;
3619 MDB_dbi i;
3620 MDB_val data;
3621 data.mv_size = sizeof(MDB_db);
3622
3623 mdb_cursor_init(&mc, txn, MAIN_DBI, NULL);
3624 for (i = CORE_DBS; i < txn->mt_numdbs; i++) {
3625 if (txn->mt_dbflags[i] & DB_DIRTY) {
3626 if (TXN_DBI_CHANGED(txn, i)) {
3627 rc = MDB_BAD_DBI;
3628 goto fail;
3629 }
3630 data.mv_data = &txn->mt_dbs[i];
3631 rc = mdb_cursor_put(&mc, &txn->mt_dbxs[i].md_name, &data,
3632 F_SUBDATA);
3633 if (rc)
3634 goto fail;
3635 }
3636 }
3637 }
3638
3639 rc = mdb_freelist_save(txn);
3640 if (rc)
3641 goto fail;
3642
3643 mdb_midl_free(env->me_pghead);
3644 env->me_pghead = NULL;
3645 mdb_midl_shrink(&txn->mt_free_pgs);
3646
3647 #if (MDB_DEBUG) > 2
3648 mdb_audit(txn);
3649 #endif
3650
3651 if ((rc = mdb_page_flush(txn, 0)) ||
3652 (rc = mdb_env_sync(env, 0)) ||
3653 (rc = mdb_env_write_meta(txn)))
3654 goto fail;
3655 end_mode = MDB_END_COMMITTED|MDB_END_UPDATE;
3656
3657 done:
3658 mdb_txn_end(txn, end_mode);
3659 return MDB_SUCCESS;
3660
3661 fail:
3662 mdb_txn_abort(txn);
3663 return rc;
3664 }
3665
3666 /** Read the environment parameters of a DB environment before
3667 * mapping it into memory.
3668 * @param[in] env the environment handle
3669 * @param[out] meta address of where to store the meta information
3670 * @return 0 on success, non-zero on failure.
3671 */
3672 static int ESECT
mdb_env_read_header(MDB_env * env,MDB_meta * meta)3673 mdb_env_read_header(MDB_env *env, MDB_meta *meta)
3674 {
3675 MDB_metabuf pbuf;
3676 MDB_page *p;
3677 MDB_meta *m;
3678 int i, rc, off;
3679 enum { Size = sizeof(pbuf) };
3680
3681 /* We don't know the page size yet, so use a minimum value.
3682 * Read both meta pages so we can use the latest one.
3683 */
3684
3685 for (i=off=0; i<NUM_METAS; i++, off += meta->mm_psize) {
3686 #ifdef _WIN32
3687 DWORD len;
3688 OVERLAPPED ov;
3689 memset(&ov, 0, sizeof(ov));
3690 ov.Offset = off;
3691 rc = ReadFile(env->me_fd, &pbuf, Size, &len, &ov) ? (int)len : -1;
3692 if (rc == -1 && ErrCode() == ERROR_HANDLE_EOF)
3693 rc = 0;
3694 #else
3695 rc = pread(env->me_fd, &pbuf, Size, off);
3696 #endif
3697 if (rc != Size) {
3698 if (rc == 0 && off == 0)
3699 return ENOENT;
3700 rc = rc < 0 ? (int) ErrCode() : MDB_INVALID;
3701 DPRINTF(("read: %s", mdb_strerror(rc)));
3702 return rc;
3703 }
3704
3705 p = (MDB_page *)&pbuf;
3706
3707 if (!F_ISSET(p->mp_flags, P_META)) {
3708 DPRINTF(("page %"Z"u not a meta page", p->mp_pgno));
3709 return MDB_INVALID;
3710 }
3711
3712 m = METADATA(p);
3713 if (m->mm_magic != MDB_MAGIC) {
3714 DPUTS("meta has invalid magic");
3715 return MDB_INVALID;
3716 }
3717
3718 if (m->mm_version != MDB_DATA_VERSION) {
3719 DPRINTF(("database is version %u, expected version %u",
3720 m->mm_version, MDB_DATA_VERSION));
3721 return MDB_VERSION_MISMATCH;
3722 }
3723
3724 if (off == 0 || m->mm_txnid > meta->mm_txnid)
3725 *meta = *m;
3726 }
3727 return 0;
3728 }
3729
3730 /** Fill in most of the zeroed #MDB_meta for an empty database environment */
3731 static void ESECT
mdb_env_init_meta0(MDB_env * env,MDB_meta * meta)3732 mdb_env_init_meta0(MDB_env *env, MDB_meta *meta)
3733 {
3734 meta->mm_magic = MDB_MAGIC;
3735 meta->mm_version = MDB_DATA_VERSION;
3736 meta->mm_mapsize = env->me_mapsize;
3737 meta->mm_psize = env->me_psize;
3738 meta->mm_last_pg = NUM_METAS-1;
3739 meta->mm_flags = env->me_flags & 0xffff;
3740 meta->mm_flags |= MDB_INTEGERKEY; /* this is mm_dbs[FREE_DBI].md_flags */
3741 meta->mm_dbs[FREE_DBI].md_root = P_INVALID;
3742 meta->mm_dbs[MAIN_DBI].md_root = P_INVALID;
3743 }
3744
3745 /** Write the environment parameters of a freshly created DB environment.
3746 * @param[in] env the environment handle
3747 * @param[in] meta the #MDB_meta to write
3748 * @return 0 on success, non-zero on failure.
3749 */
3750 static int ESECT
mdb_env_init_meta(MDB_env * env,MDB_meta * meta)3751 mdb_env_init_meta(MDB_env *env, MDB_meta *meta)
3752 {
3753 MDB_page *p, *q;
3754 int rc;
3755 unsigned int psize;
3756 #ifdef _WIN32
3757 DWORD len;
3758 OVERLAPPED ov;
3759 memset(&ov, 0, sizeof(ov));
3760 #define DO_PWRITE(rc, fd, ptr, size, len, pos) do { \
3761 ov.Offset = pos; \
3762 rc = WriteFile(fd, ptr, size, &len, &ov); } while(0)
3763 #else
3764 int len;
3765 #define DO_PWRITE(rc, fd, ptr, size, len, pos) do { \
3766 len = pwrite(fd, ptr, size, pos); \
3767 if (len == -1 && ErrCode() == EINTR) continue; \
3768 rc = (len >= 0); break; } while(1)
3769 #endif
3770
3771 DPUTS("writing new meta page");
3772
3773 psize = env->me_psize;
3774
3775 p = calloc(NUM_METAS, psize);
3776 if (!p)
3777 return ENOMEM;
3778
3779 p->mp_pgno = 0;
3780 p->mp_flags = P_META;
3781 *(MDB_meta *)METADATA(p) = *meta;
3782
3783 q = (MDB_page *)((char *)p + psize);
3784 q->mp_pgno = 1;
3785 q->mp_flags = P_META;
3786 *(MDB_meta *)METADATA(q) = *meta;
3787
3788 DO_PWRITE(rc, env->me_fd, p, psize * NUM_METAS, len, 0);
3789 if (!rc)
3790 rc = ErrCode();
3791 else if ((unsigned) len == psize * NUM_METAS)
3792 rc = MDB_SUCCESS;
3793 else
3794 rc = ENOSPC;
3795 free(p);
3796 return rc;
3797 }
3798
3799 /** Update the environment info to commit a transaction.
3800 * @param[in] txn the transaction that's being committed
3801 * @return 0 on success, non-zero on failure.
3802 */
3803 static int
mdb_env_write_meta(MDB_txn * txn)3804 mdb_env_write_meta(MDB_txn *txn)
3805 {
3806 MDB_env *env;
3807 MDB_meta meta, metab, *mp;
3808 unsigned flags;
3809 size_t mapsize;
3810 off_t off;
3811 int rc, len, toggle;
3812 char *ptr;
3813 HANDLE mfd;
3814 #ifdef _WIN32
3815 OVERLAPPED ov;
3816 #else
3817 int r2;
3818 #endif
3819
3820 toggle = txn->mt_txnid & 1;
3821 DPRINTF(("writing meta page %d for root page %"Z"u",
3822 toggle, txn->mt_dbs[MAIN_DBI].md_root));
3823
3824 env = txn->mt_env;
3825 flags = env->me_flags;
3826 mp = env->me_metas[toggle];
3827 mapsize = env->me_metas[toggle ^ 1]->mm_mapsize;
3828 /* Persist any increases of mapsize config */
3829 if (mapsize < env->me_mapsize)
3830 mapsize = env->me_mapsize;
3831
3832 if (flags & MDB_WRITEMAP) {
3833 mp->mm_mapsize = mapsize;
3834 mp->mm_dbs[FREE_DBI] = txn->mt_dbs[FREE_DBI];
3835 mp->mm_dbs[MAIN_DBI] = txn->mt_dbs[MAIN_DBI];
3836 mp->mm_last_pg = txn->mt_next_pgno - 1;
3837 #if (__GNUC__ * 100 + __GNUC_MINOR__ >= 404) && /* TODO: portability */ \
3838 !(defined(__i386__) || defined(__x86_64__))
3839 /* LY: issue a memory barrier, if not x86. ITS#7969 */
3840 __sync_synchronize();
3841 #endif
3842 mp->mm_txnid = txn->mt_txnid;
3843 if (!(flags & (MDB_NOMETASYNC|MDB_NOSYNC))) {
3844 unsigned meta_size = env->me_psize;
3845 rc = (env->me_flags & MDB_MAPASYNC) ? MS_ASYNC : MS_SYNC;
3846 ptr = (char *)mp - PAGEHDRSZ;
3847 #ifndef _WIN32 /* POSIX msync() requires ptr = start of OS page */
3848 r2 = (ptr - env->me_map) & (env->me_os_psize - 1);
3849 ptr -= r2;
3850 meta_size += r2;
3851 #endif
3852 if (MDB_MSYNC(ptr, meta_size, rc)) {
3853 rc = ErrCode();
3854 goto fail;
3855 }
3856 }
3857 goto done;
3858 }
3859 metab.mm_txnid = mp->mm_txnid;
3860 metab.mm_last_pg = mp->mm_last_pg;
3861
3862 meta.mm_mapsize = mapsize;
3863 meta.mm_dbs[FREE_DBI] = txn->mt_dbs[FREE_DBI];
3864 meta.mm_dbs[MAIN_DBI] = txn->mt_dbs[MAIN_DBI];
3865 meta.mm_last_pg = txn->mt_next_pgno - 1;
3866 meta.mm_txnid = txn->mt_txnid;
3867
3868 off = offsetof(MDB_meta, mm_mapsize);
3869 ptr = (char *)&meta + off;
3870 len = sizeof(MDB_meta) - off;
3871 off += (char *)mp - env->me_map;
3872
3873 /* Write to the SYNC fd unless MDB_NOSYNC/MDB_NOMETASYNC.
3874 * (me_mfd goes to the same file as me_fd, but writing to it
3875 * also syncs to disk. Avoids a separate fdatasync() call.)
3876 */
3877 mfd = (flags & (MDB_NOSYNC|MDB_NOMETASYNC)) ? env->me_fd : env->me_mfd;
3878 #ifdef _WIN32
3879 {
3880 memset(&ov, 0, sizeof(ov));
3881 ov.Offset = off;
3882 if (!WriteFile(mfd, ptr, len, (DWORD *)&rc, &ov))
3883 rc = -1;
3884 }
3885 #else
3886 retry_write:
3887 rc = pwrite(mfd, ptr, len, off);
3888 #endif
3889 if (rc != len) {
3890 rc = rc < 0 ? ErrCode() : EIO;
3891 #ifndef _WIN32
3892 if (rc == EINTR)
3893 goto retry_write;
3894 #endif
3895 DPUTS("write failed, disk error?");
3896 /* On a failure, the pagecache still contains the new data.
3897 * Write some old data back, to prevent it from being used.
3898 * Use the non-SYNC fd; we know it will fail anyway.
3899 */
3900 meta.mm_last_pg = metab.mm_last_pg;
3901 meta.mm_txnid = metab.mm_txnid;
3902 #ifdef _WIN32
3903 memset(&ov, 0, sizeof(ov));
3904 ov.Offset = off;
3905 WriteFile(env->me_fd, ptr, len, NULL, &ov);
3906 #else
3907 r2 = pwrite(env->me_fd, ptr, len, off);
3908 (void)r2; /* Silence warnings. We don't care about pwrite's return value */
3909 #endif
3910 fail:
3911 env->me_flags |= MDB_FATAL_ERROR;
3912 return rc;
3913 }
3914 /* MIPS has cache coherency issues, this is a no-op everywhere else */
3915 CACHEFLUSH(env->me_map + off, len, DCACHE);
3916 done:
3917 /* Memory ordering issues are irrelevant; since the entire writer
3918 * is wrapped by wmutex, all of these changes will become visible
3919 * after the wmutex is unlocked. Since the DB is multi-version,
3920 * readers will get consistent data regardless of how fresh or
3921 * how stale their view of these values is.
3922 */
3923 if (env->me_txns)
3924 env->me_txns->mti_txnid = txn->mt_txnid;
3925
3926 return MDB_SUCCESS;
3927 }
3928
3929 /** Check both meta pages to see which one is newer.
3930 * @param[in] env the environment handle
3931 * @return newest #MDB_meta.
3932 */
3933 static MDB_meta *
mdb_env_pick_meta(const MDB_env * env)3934 mdb_env_pick_meta(const MDB_env *env)
3935 {
3936 MDB_meta *const *metas = env->me_metas;
3937 return metas[ metas[0]->mm_txnid < metas[1]->mm_txnid ];
3938 }
3939
3940 int ESECT
mdb_env_create(MDB_env ** env)3941 mdb_env_create(MDB_env **env)
3942 {
3943 MDB_env *e;
3944
3945 e = calloc(1, sizeof(MDB_env));
3946 if (!e)
3947 return ENOMEM;
3948
3949 e->me_maxreaders = DEFAULT_READERS;
3950 e->me_maxdbs = e->me_numdbs = CORE_DBS;
3951 e->me_fd = INVALID_HANDLE_VALUE;
3952 e->me_lfd = INVALID_HANDLE_VALUE;
3953 e->me_mfd = INVALID_HANDLE_VALUE;
3954 #ifdef MDB_USE_POSIX_SEM
3955 e->me_rmutex = SEM_FAILED;
3956 e->me_wmutex = SEM_FAILED;
3957 #endif
3958 e->me_pid = getpid();
3959 GET_PAGESIZE(e->me_os_psize);
3960 VGMEMP_CREATE(e,0,0);
3961 *env = e;
3962 return MDB_SUCCESS;
3963 }
3964
3965 static int ESECT
mdb_env_map(MDB_env * env,void * addr)3966 mdb_env_map(MDB_env *env, void *addr)
3967 {
3968 MDB_page *p;
3969 unsigned int flags = env->me_flags;
3970 #ifdef _WIN32
3971 int rc;
3972 HANDLE mh;
3973 LONG sizelo, sizehi;
3974 size_t msize;
3975
3976 if (flags & MDB_RDONLY) {
3977 /* Don't set explicit map size, use whatever exists */
3978 msize = 0;
3979 sizelo = 0;
3980 sizehi = 0;
3981 } else {
3982 msize = env->me_mapsize;
3983 sizelo = msize & 0xffffffff;
3984 sizehi = msize >> 16 >> 16; /* only needed on Win64 */
3985
3986 /* Windows won't create mappings for zero length files.
3987 * and won't map more than the file size.
3988 * Just set the maxsize right now.
3989 */
3990 if (!(flags & MDB_WRITEMAP) && (SetFilePointer(env->me_fd, sizelo, &sizehi, 0) != (DWORD)sizelo
3991 || !SetEndOfFile(env->me_fd)
3992 || SetFilePointer(env->me_fd, 0, NULL, 0) != 0))
3993 return ErrCode();
3994 }
3995
3996 mh = CreateFileMapping(env->me_fd, NULL, flags & MDB_WRITEMAP ?
3997 PAGE_READWRITE : PAGE_READONLY,
3998 sizehi, sizelo, NULL);
3999 if (!mh)
4000 return ErrCode();
4001 env->me_map = MapViewOfFileEx(mh, flags & MDB_WRITEMAP ?
4002 FILE_MAP_WRITE : FILE_MAP_READ,
4003 0, 0, msize, addr);
4004 rc = env->me_map ? 0 : ErrCode();
4005 CloseHandle(mh);
4006 if (rc)
4007 return rc;
4008 #else
4009 int prot = PROT_READ;
4010 if (flags & MDB_WRITEMAP) {
4011 prot |= PROT_WRITE;
4012 if (ftruncate(env->me_fd, env->me_mapsize) < 0)
4013 return ErrCode();
4014 }
4015 env->me_map = mmap(addr, env->me_mapsize, prot, MAP_SHARED,
4016 env->me_fd, 0);
4017 if (env->me_map == MAP_FAILED) {
4018 env->me_map = NULL;
4019 return ErrCode();
4020 }
4021
4022 if (flags & MDB_NORDAHEAD) {
4023 /* Turn off readahead. It's harmful when the DB is larger than RAM. */
4024 #ifdef MADV_RANDOM
4025 madvise(env->me_map, env->me_mapsize, MADV_RANDOM);
4026 #else
4027 #ifdef POSIX_MADV_RANDOM
4028 posix_madvise(env->me_map, env->me_mapsize, POSIX_MADV_RANDOM);
4029 #endif /* POSIX_MADV_RANDOM */
4030 #endif /* MADV_RANDOM */
4031 }
4032 #endif /* _WIN32 */
4033
4034 /* Can happen because the address argument to mmap() is just a
4035 * hint. mmap() can pick another, e.g. if the range is in use.
4036 * The MAP_FIXED flag would prevent that, but then mmap could
4037 * instead unmap existing pages to make room for the new map.
4038 */
4039 if (addr && env->me_map != addr)
4040 return EBUSY; /* TODO: Make a new MDB_* error code? */
4041
4042 p = (MDB_page *)env->me_map;
4043 env->me_metas[0] = METADATA(p);
4044 env->me_metas[1] = (MDB_meta *)((char *)env->me_metas[0] + env->me_psize);
4045
4046 return MDB_SUCCESS;
4047 }
4048
4049 int ESECT
mdb_env_set_mapsize(MDB_env * env,size_t size)4050 mdb_env_set_mapsize(MDB_env *env, size_t size)
4051 {
4052 /* If env is already open, caller is responsible for making
4053 * sure there are no active txns.
4054 */
4055 if (env->me_map) {
4056 int rc;
4057 MDB_meta *meta;
4058 void *old;
4059 if (env->me_txn)
4060 return EINVAL;
4061 meta = mdb_env_pick_meta(env);
4062 if (!size)
4063 size = meta->mm_mapsize;
4064 {
4065 /* Silently round up to minimum if the size is too small */
4066 size_t minsize = (meta->mm_last_pg + 1) * env->me_psize;
4067 if (size < minsize)
4068 size = minsize;
4069 }
4070 munmap(env->me_map, env->me_mapsize);
4071 env->me_mapsize = size;
4072 old = (env->me_flags & MDB_FIXEDMAP) ? env->me_map : NULL;
4073 rc = mdb_env_map(env, old);
4074 if (rc)
4075 return rc;
4076 }
4077 env->me_mapsize = size;
4078 if (env->me_psize)
4079 env->me_maxpg = env->me_mapsize / env->me_psize;
4080 return MDB_SUCCESS;
4081 }
4082
4083 int ESECT
mdb_env_set_maxdbs(MDB_env * env,MDB_dbi dbs)4084 mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs)
4085 {
4086 if (env->me_map)
4087 return EINVAL;
4088 env->me_maxdbs = dbs + CORE_DBS;
4089 return MDB_SUCCESS;
4090 }
4091
4092 int ESECT
mdb_env_set_maxreaders(MDB_env * env,unsigned int readers)4093 mdb_env_set_maxreaders(MDB_env *env, unsigned int readers)
4094 {
4095 if (env->me_map || readers < 1)
4096 return EINVAL;
4097 env->me_maxreaders = readers;
4098 return MDB_SUCCESS;
4099 }
4100
4101 int ESECT
mdb_env_get_maxreaders(MDB_env * env,unsigned int * readers)4102 mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers)
4103 {
4104 if (!env || !readers)
4105 return EINVAL;
4106 *readers = env->me_maxreaders;
4107 return MDB_SUCCESS;
4108 }
4109
4110 static int ESECT
mdb_fsize(HANDLE fd,size_t * size)4111 mdb_fsize(HANDLE fd, size_t *size)
4112 {
4113 #ifdef _WIN32
4114 LARGE_INTEGER fsize;
4115
4116 if (!GetFileSizeEx(fd, &fsize))
4117 return ErrCode();
4118
4119 *size = fsize.QuadPart;
4120 #else
4121 struct stat st;
4122
4123 if (fstat(fd, &st))
4124 return ErrCode();
4125
4126 *size = st.st_size;
4127 #endif
4128 return MDB_SUCCESS;
4129 }
4130
4131
4132 #ifdef _WIN32
4133 typedef wchar_t mdb_nchar_t;
4134 # define MDB_NAME(str) L##str
4135 # define mdb_name_cpy wcscpy
4136 #else
4137 /** Character type for file names: char on Unix, wchar_t on Windows */
4138 typedef char mdb_nchar_t;
4139 # define MDB_NAME(str) str /**< #mdb_nchar_t[] string literal */
4140 # define mdb_name_cpy strcpy /**< Copy name (#mdb_nchar_t string) */
4141 #endif
4142
4143 /** Filename - string of #mdb_nchar_t[] */
4144 typedef struct MDB_name {
4145 int mn_len; /**< Length */
4146 int mn_alloced; /**< True if #mn_val was malloced */
4147 mdb_nchar_t *mn_val; /**< Contents */
4148 } MDB_name;
4149
4150 /** Filename suffixes [datafile,lockfile][without,with MDB_NOSUBDIR] */
4151 static const mdb_nchar_t *const mdb_suffixes[2][2] = {
4152 { MDB_NAME("/data.mdb"), MDB_NAME("") },
4153 { MDB_NAME("/lock.mdb"), MDB_NAME("-lock") }
4154 };
4155
4156 #define MDB_SUFFLEN 9 /**< Max string length in #mdb_suffixes[] */
4157
4158 /** Set up filename + scratch area for filename suffix, for opening files.
4159 * It should be freed with #mdb_fname_destroy().
4160 * On Windows, paths are converted from char *UTF-8 to wchar_t *UTF-16.
4161 *
4162 * @param[in] path Pathname for #mdb_env_open().
4163 * @param[in] envflags Whether a subdir and/or lockfile will be used.
4164 * @param[out] fname Resulting filename, with room for a suffix if necessary.
4165 */
4166 static int ESECT
mdb_fname_init(const char * path,unsigned envflags,MDB_name * fname)4167 mdb_fname_init(const char *path, unsigned envflags, MDB_name *fname)
4168 {
4169 int no_suffix = F_ISSET(envflags, MDB_NOSUBDIR|MDB_NOLOCK);
4170 fname->mn_alloced = 0;
4171 #ifdef _WIN32
4172 return utf8_to_utf16(path, fname, no_suffix ? 0 : MDB_SUFFLEN);
4173 #else
4174 fname->mn_len = strlen(path);
4175 if (no_suffix)
4176 fname->mn_val = (char *) path;
4177 else if ((fname->mn_val = malloc(fname->mn_len + MDB_SUFFLEN+1)) != NULL) {
4178 fname->mn_alloced = 1;
4179 strcpy(fname->mn_val, path);
4180 }
4181 else
4182 return ENOMEM;
4183 return MDB_SUCCESS;
4184 #endif
4185 }
4186
4187 /** Destroy \b fname from #mdb_fname_init() */
4188 #define mdb_fname_destroy(fname) \
4189 do { if ((fname).mn_alloced) free((fname).mn_val); } while (0)
4190
4191 #ifdef O_CLOEXEC /* POSIX.1-2008: Set FD_CLOEXEC atomically at open() */
4192 # define MDB_CLOEXEC O_CLOEXEC
4193 #else
4194 # define MDB_CLOEXEC 0
4195 #endif
4196
4197 /** File type, access mode etc. for #mdb_fopen() */
4198 enum mdb_fopen_type {
4199 #ifdef _WIN32
4200 MDB_O_RDONLY, MDB_O_RDWR, MDB_O_META, MDB_O_COPY, MDB_O_LOCKS
4201 #else
4202 /* A comment in mdb_fopen() explains some O_* flag choices. */
4203 MDB_O_RDONLY= O_RDONLY, /**< for RDONLY me_fd */
4204 MDB_O_RDWR = O_RDWR |O_CREAT, /**< for me_fd */
4205 MDB_O_META = O_WRONLY|MDB_DSYNC |MDB_CLOEXEC, /**< for me_mfd */
4206 MDB_O_COPY = O_WRONLY|O_CREAT|O_EXCL|MDB_CLOEXEC, /**< for #mdb_env_copy() */
4207 /** Bitmask for open() flags in enum #mdb_fopen_type. The other bits
4208 * distinguish otherwise-equal MDB_O_* constants from each other.
4209 */
4210 MDB_O_MASK = MDB_O_RDWR|MDB_CLOEXEC | MDB_O_RDONLY|MDB_O_META|MDB_O_COPY,
4211 MDB_O_LOCKS = MDB_O_RDWR|MDB_CLOEXEC | ((MDB_O_MASK+1) & ~MDB_O_MASK) /**< for me_lfd */
4212 #endif
4213 };
4214
4215 /** Open an LMDB file.
4216 * @param[in] env The LMDB environment.
4217 * @param[in,out] fname Path from from #mdb_fname_init(). A suffix is
4218 * appended if necessary to create the filename, without changing mn_len.
4219 * @param[in] which Determines file type, access mode, etc.
4220 * @param[in] mode The Unix permissions for the file, if we create it.
4221 * @param[out] res Resulting file handle.
4222 * @return 0 on success, non-zero on failure.
4223 */
4224 static int ESECT
mdb_fopen(const MDB_env * env,MDB_name * fname,enum mdb_fopen_type which,mdb_mode_t mode,HANDLE * res)4225 mdb_fopen(const MDB_env *env, MDB_name *fname,
4226 enum mdb_fopen_type which, mdb_mode_t mode,
4227 HANDLE *res)
4228 {
4229 int rc = MDB_SUCCESS;
4230 HANDLE fd;
4231 #ifdef _WIN32
4232 DWORD acc, share, disp, attrs;
4233 #else
4234 int flags;
4235 #endif
4236
4237 if (fname->mn_alloced) /* modifiable copy */
4238 mdb_name_cpy(fname->mn_val + fname->mn_len,
4239 mdb_suffixes[which==MDB_O_LOCKS][F_ISSET(env->me_flags, MDB_NOSUBDIR)]);
4240
4241 /* The directory must already exist. Usually the file need not.
4242 * MDB_O_META requires the file because we already created it using
4243 * MDB_O_RDWR. MDB_O_COPY must not overwrite an existing file.
4244 *
4245 * With MDB_O_COPY we do not want the OS to cache the writes, since
4246 * the source data is already in the OS cache.
4247 *
4248 * The lockfile needs FD_CLOEXEC (close file descriptor on exec*())
4249 * to avoid the flock() issues noted under Caveats in lmdb.h.
4250 * Also set it for other filehandles which the user cannot get at
4251 * and close himself, which he may need after fork(). I.e. all but
4252 * me_fd, which programs do use via mdb_env_get_fd().
4253 */
4254
4255 #ifdef _WIN32
4256 acc = GENERIC_READ|GENERIC_WRITE;
4257 share = FILE_SHARE_READ|FILE_SHARE_WRITE;
4258 disp = OPEN_ALWAYS;
4259 attrs = FILE_ATTRIBUTE_NORMAL;
4260 switch (which) {
4261 case MDB_O_RDONLY: /* read-only datafile */
4262 acc = GENERIC_READ;
4263 disp = OPEN_EXISTING;
4264 break;
4265 case MDB_O_META: /* for writing metapages */
4266 acc = GENERIC_WRITE;
4267 disp = OPEN_EXISTING;
4268 attrs = FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH;
4269 break;
4270 case MDB_O_COPY: /* mdb_env_copy() & co */
4271 acc = GENERIC_WRITE;
4272 share = 0;
4273 disp = CREATE_NEW;
4274 attrs = FILE_FLAG_NO_BUFFERING|FILE_FLAG_WRITE_THROUGH;
4275 break;
4276 default: break; /* silence gcc -Wswitch (not all enum values handled) */
4277 }
4278 fd = CreateFileW(fname->mn_val, acc, share, NULL, disp, attrs, NULL);
4279 #else
4280 fd = open(fname->mn_val, which & MDB_O_MASK, mode);
4281 #endif
4282
4283 if (fd == INVALID_HANDLE_VALUE)
4284 rc = ErrCode();
4285 #ifndef _WIN32
4286 else {
4287 if (which != MDB_O_RDONLY && which != MDB_O_RDWR) {
4288 /* Set CLOEXEC if we could not pass it to open() */
4289 if (!MDB_CLOEXEC && (flags = fcntl(fd, F_GETFD)) != -1)
4290 (void) fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4291 }
4292 if (which == MDB_O_COPY && env->me_psize >= env->me_os_psize) {
4293 /* This may require buffer alignment. There is no portable
4294 * way to ask how much, so we require OS pagesize alignment.
4295 */
4296 # ifdef F_NOCACHE /* __APPLE__ */
4297 (void) fcntl(fd, F_NOCACHE, 1);
4298 # elif defined O_DIRECT
4299 /* open(...O_DIRECT...) would break on filesystems without
4300 * O_DIRECT support (ITS#7682). Try to set it here instead.
4301 */
4302 if ((flags = fcntl(fd, F_GETFL)) != -1)
4303 (void) fcntl(fd, F_SETFL, flags | O_DIRECT);
4304 # endif
4305 }
4306 }
4307 #endif /* !_WIN32 */
4308
4309 *res = fd;
4310 return rc;
4311 }
4312
4313
4314 #ifdef BROKEN_FDATASYNC
4315 #include <sys/utsname.h>
4316 #include <sys/vfs.h>
4317 #endif
4318
4319 /** Further setup required for opening an LMDB environment
4320 */
4321 static int ESECT
mdb_env_open2(MDB_env * env)4322 mdb_env_open2(MDB_env *env)
4323 {
4324 unsigned int flags = env->me_flags;
4325 int i, newenv = 0, rc;
4326 MDB_meta meta;
4327
4328 #ifdef _WIN32
4329 /* See if we should use QueryLimited */
4330 rc = GetVersion();
4331 if ((rc & 0xff) > 5)
4332 env->me_pidquery = MDB_PROCESS_QUERY_LIMITED_INFORMATION;
4333 else
4334 env->me_pidquery = PROCESS_QUERY_INFORMATION;
4335 #endif /* _WIN32 */
4336
4337 #ifdef BROKEN_FDATASYNC
4338 /* ext3/ext4 fdatasync is broken on some older Linux kernels.
4339 * https://lkml.org/lkml/2012/9/3/83
4340 * Kernels after 3.6-rc6 are known good.
4341 * https://lkml.org/lkml/2012/9/10/556
4342 * See if the DB is on ext3/ext4, then check for new enough kernel
4343 * Kernels 2.6.32.60, 2.6.34.15, 3.2.30, and 3.5.4 are also known
4344 * to be patched.
4345 */
4346 {
4347 struct statfs st;
4348 fstatfs(env->me_fd, &st);
4349 while (st.f_type == 0xEF53) {
4350 struct utsname uts;
4351 int i;
4352 uname(&uts);
4353 if (uts.release[0] < '3') {
4354 if (!strncmp(uts.release, "2.6.32.", 7)) {
4355 i = atoi(uts.release+7);
4356 if (i >= 60)
4357 break; /* 2.6.32.60 and newer is OK */
4358 } else if (!strncmp(uts.release, "2.6.34.", 7)) {
4359 i = atoi(uts.release+7);
4360 if (i >= 15)
4361 break; /* 2.6.34.15 and newer is OK */
4362 }
4363 } else if (uts.release[0] == '3') {
4364 i = atoi(uts.release+2);
4365 if (i > 5)
4366 break; /* 3.6 and newer is OK */
4367 if (i == 5) {
4368 i = atoi(uts.release+4);
4369 if (i >= 4)
4370 break; /* 3.5.4 and newer is OK */
4371 } else if (i == 2) {
4372 i = atoi(uts.release+4);
4373 if (i >= 30)
4374 break; /* 3.2.30 and newer is OK */
4375 }
4376 } else { /* 4.x and newer is OK */
4377 break;
4378 }
4379 env->me_flags |= MDB_FSYNCONLY;
4380 break;
4381 }
4382 }
4383 #endif
4384
4385 if ((i = mdb_env_read_header(env, &meta)) != 0) {
4386 if (i != ENOENT)
4387 return i;
4388 DPUTS("new mdbenv");
4389 newenv = 1;
4390 env->me_psize = env->me_os_psize;
4391 if (env->me_psize > MAX_PAGESIZE)
4392 env->me_psize = MAX_PAGESIZE;
4393 memset(&meta, 0, sizeof(meta));
4394 mdb_env_init_meta0(env, &meta);
4395 meta.mm_mapsize = DEFAULT_MAPSIZE;
4396 } else {
4397 env->me_psize = meta.mm_psize;
4398 }
4399
4400 /* Was a mapsize configured? */
4401 if (!env->me_mapsize) {
4402 env->me_mapsize = meta.mm_mapsize;
4403 }
4404 {
4405 /* Make sure mapsize >= committed data size. Even when using
4406 * mm_mapsize, which could be broken in old files (ITS#7789).
4407 */
4408 size_t minsize = (meta.mm_last_pg + 1) * meta.mm_psize;
4409 if (env->me_mapsize < minsize)
4410 env->me_mapsize = minsize;
4411 }
4412 meta.mm_mapsize = env->me_mapsize;
4413
4414 if (newenv && !(flags & MDB_FIXEDMAP)) {
4415 /* mdb_env_map() may grow the datafile. Write the metapages
4416 * first, so the file will be valid if initialization fails.
4417 * Except with FIXEDMAP, since we do not yet know mm_address.
4418 * We could fill in mm_address later, but then a different
4419 * program might end up doing that - one with a memory layout
4420 * and map address which does not suit the main program.
4421 */
4422 rc = mdb_env_init_meta(env, &meta);
4423 if (rc)
4424 return rc;
4425 newenv = 0;
4426 }
4427
4428 rc = mdb_env_map(env, (flags & MDB_FIXEDMAP) ? meta.mm_address : NULL);
4429 if (rc)
4430 return rc;
4431
4432 if (newenv) {
4433 if (flags & MDB_FIXEDMAP)
4434 meta.mm_address = env->me_map;
4435 i = mdb_env_init_meta(env, &meta);
4436 if (i != MDB_SUCCESS) {
4437 return i;
4438 }
4439 }
4440
4441 env->me_maxfree_1pg = (env->me_psize - PAGEHDRSZ) / sizeof(pgno_t) - 1;
4442 env->me_nodemax = (((env->me_psize - PAGEHDRSZ) / MDB_MINKEYS) & -2)
4443 - sizeof(indx_t);
4444 #if !(MDB_MAXKEYSIZE)
4445 env->me_maxkey = env->me_nodemax - (NODESIZE + sizeof(MDB_db));
4446 #endif
4447 env->me_maxpg = env->me_mapsize / env->me_psize;
4448
4449 #if MDB_DEBUG
4450 {
4451 MDB_meta *meta = mdb_env_pick_meta(env);
4452 MDB_db *db = &meta->mm_dbs[MAIN_DBI];
4453
4454 DPRINTF(("opened database version %u, pagesize %u",
4455 meta->mm_version, env->me_psize));
4456 DPRINTF(("using meta page %d", (int) (meta->mm_txnid & 1)));
4457 DPRINTF(("depth: %u", db->md_depth));
4458 DPRINTF(("entries: %"Z"u", db->md_entries));
4459 DPRINTF(("branch pages: %"Z"u", db->md_branch_pages));
4460 DPRINTF(("leaf pages: %"Z"u", db->md_leaf_pages));
4461 DPRINTF(("overflow pages: %"Z"u", db->md_overflow_pages));
4462 DPRINTF(("root: %"Z"u", db->md_root));
4463 }
4464 #endif
4465
4466 return MDB_SUCCESS;
4467 }
4468
4469
4470 /** Release a reader thread's slot in the reader lock table.
4471 * This function is called automatically when a thread exits.
4472 * @param[in] ptr This points to the slot in the reader lock table.
4473 */
4474 static void
mdb_env_reader_dest(void * ptr)4475 mdb_env_reader_dest(void *ptr)
4476 {
4477 MDB_reader *reader = ptr;
4478
4479 #ifndef _WIN32
4480 if (reader->mr_pid == getpid()) /* catch pthread_exit() in child process */
4481 #endif
4482 /* We omit the mutex, so do this atomically (i.e. skip mr_txnid) */
4483 reader->mr_pid = 0;
4484 }
4485
4486 #ifdef _WIN32
4487 /** Junk for arranging thread-specific callbacks on Windows. This is
4488 * necessarily platform and compiler-specific. Windows supports up
4489 * to 1088 keys. Let's assume nobody opens more than 64 environments
4490 * in a single process, for now. They can override this if needed.
4491 */
4492 #ifndef MAX_TLS_KEYS
4493 #define MAX_TLS_KEYS 64
4494 #endif
4495 static pthread_key_t mdb_tls_keys[MAX_TLS_KEYS];
4496 static int mdb_tls_nkeys;
4497
mdb_tls_callback(PVOID module,DWORD reason,PVOID ptr)4498 static void NTAPI mdb_tls_callback(PVOID module, DWORD reason, PVOID ptr)
4499 {
4500 int i;
4501 switch(reason) {
4502 case DLL_PROCESS_ATTACH: break;
4503 case DLL_THREAD_ATTACH: break;
4504 case DLL_THREAD_DETACH:
4505 for (i=0; i<mdb_tls_nkeys; i++) {
4506 MDB_reader *r = pthread_getspecific(mdb_tls_keys[i]);
4507 if (r) {
4508 mdb_env_reader_dest(r);
4509 }
4510 }
4511 break;
4512 case DLL_PROCESS_DETACH: break;
4513 }
4514 }
4515 #ifdef __GNUC__
4516 #ifdef _WIN64
4517 const PIMAGE_TLS_CALLBACK mdb_tls_cbp __attribute__((section (".CRT$XLB"))) = mdb_tls_callback;
4518 #else
4519 PIMAGE_TLS_CALLBACK mdb_tls_cbp __attribute__((section (".CRT$XLB"))) = mdb_tls_callback;
4520 #endif
4521 #else
4522 #ifdef _WIN64
4523 /* Force some symbol references.
4524 * _tls_used forces the linker to create the TLS directory if not already done
4525 * mdb_tls_cbp prevents whole-program-optimizer from dropping the symbol.
4526 */
4527 #pragma comment(linker, "/INCLUDE:_tls_used")
4528 #pragma comment(linker, "/INCLUDE:mdb_tls_cbp")
4529 #pragma const_seg(".CRT$XLB")
4530 extern const PIMAGE_TLS_CALLBACK mdb_tls_cbp;
4531 const PIMAGE_TLS_CALLBACK mdb_tls_cbp = mdb_tls_callback;
4532 #pragma const_seg()
4533 #else /* _WIN32 */
4534 #pragma comment(linker, "/INCLUDE:__tls_used")
4535 #pragma comment(linker, "/INCLUDE:_mdb_tls_cbp")
4536 #pragma data_seg(".CRT$XLB")
4537 PIMAGE_TLS_CALLBACK mdb_tls_cbp = mdb_tls_callback;
4538 #pragma data_seg()
4539 #endif /* WIN 32/64 */
4540 #endif /* !__GNUC__ */
4541 #endif
4542
4543 /** Downgrade the exclusive lock on the region back to shared */
4544 static int ESECT
mdb_env_share_locks(MDB_env * env,int * excl)4545 mdb_env_share_locks(MDB_env *env, int *excl)
4546 {
4547 int rc = 0;
4548 MDB_meta *meta = mdb_env_pick_meta(env);
4549
4550 env->me_txns->mti_txnid = meta->mm_txnid;
4551
4552 #ifdef _WIN32
4553 {
4554 OVERLAPPED ov;
4555 /* First acquire a shared lock. The Unlock will
4556 * then release the existing exclusive lock.
4557 */
4558 memset(&ov, 0, sizeof(ov));
4559 if (!LockFileEx(env->me_lfd, 0, 0, 1, 0, &ov)) {
4560 rc = ErrCode();
4561 } else {
4562 UnlockFile(env->me_lfd, 0, 0, 1, 0);
4563 *excl = 0;
4564 }
4565 }
4566 #else
4567 {
4568 struct flock lock_info;
4569 /* The shared lock replaces the existing lock */
4570 memset((void *)&lock_info, 0, sizeof(lock_info));
4571 lock_info.l_type = F_RDLCK;
4572 lock_info.l_whence = SEEK_SET;
4573 lock_info.l_start = 0;
4574 lock_info.l_len = 1;
4575 while ((rc = fcntl(env->me_lfd, F_SETLK, &lock_info)) &&
4576 (rc = ErrCode()) == EINTR) ;
4577 *excl = rc ? -1 : 0; /* error may mean we lost the lock */
4578 }
4579 #endif
4580
4581 return rc;
4582 }
4583
4584 /** Try to get exclusive lock, otherwise shared.
4585 * Maintain *excl = -1: no/unknown lock, 0: shared, 1: exclusive.
4586 */
4587 static int ESECT
mdb_env_excl_lock(MDB_env * env,int * excl)4588 mdb_env_excl_lock(MDB_env *env, int *excl)
4589 {
4590 int rc = 0;
4591 #ifdef _WIN32
4592 if (LockFile(env->me_lfd, 0, 0, 1, 0)) {
4593 *excl = 1;
4594 } else {
4595 OVERLAPPED ov;
4596 memset(&ov, 0, sizeof(ov));
4597 if (LockFileEx(env->me_lfd, 0, 0, 1, 0, &ov)) {
4598 *excl = 0;
4599 } else {
4600 rc = ErrCode();
4601 }
4602 }
4603 #else
4604 struct flock lock_info;
4605 memset((void *)&lock_info, 0, sizeof(lock_info));
4606 lock_info.l_type = F_WRLCK;
4607 lock_info.l_whence = SEEK_SET;
4608 lock_info.l_start = 0;
4609 lock_info.l_len = 1;
4610 while ((rc = fcntl(env->me_lfd, F_SETLK, &lock_info)) &&
4611 (rc = ErrCode()) == EINTR) ;
4612 if (!rc) {
4613 *excl = 1;
4614 } else
4615 # ifndef MDB_USE_POSIX_MUTEX
4616 if (*excl < 0) /* always true when MDB_USE_POSIX_MUTEX */
4617 # endif
4618 {
4619 lock_info.l_type = F_RDLCK;
4620 while ((rc = fcntl(env->me_lfd, F_SETLKW, &lock_info)) &&
4621 (rc = ErrCode()) == EINTR) ;
4622 if (rc == 0)
4623 *excl = 0;
4624 }
4625 #endif
4626 return rc;
4627 }
4628
4629 #ifdef MDB_USE_HASH
4630 /*
4631 * hash_64 - 64 bit Fowler/Noll/Vo-0 FNV-1a hash code
4632 *
4633 * @(#) $Revision: 5.1 $
4634 * @(#) $Id: hash_64a.c,v 5.1 2009/06/30 09:01:38 chongo Exp $
4635 * @(#) $Source: /usr/local/src/cmd/fnv/RCS/hash_64a.c,v $
4636 *
4637 * http://www.isthe.com/chongo/tech/comp/fnv/index.html
4638 *
4639 ***
4640 *
4641 * Please do not copyright this code. This code is in the public domain.
4642 *
4643 * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
4644 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
4645 * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
4646 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
4647 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
4648 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
4649 * PERFORMANCE OF THIS SOFTWARE.
4650 *
4651 * By:
4652 * chongo <Landon Curt Noll> /\oo/\
4653 * http://www.isthe.com/chongo/
4654 *
4655 * Share and Enjoy! :-)
4656 */
4657
4658 typedef unsigned long long mdb_hash_t;
4659 #define MDB_HASH_INIT ((mdb_hash_t)0xcbf29ce484222325ULL)
4660
4661 /** perform a 64 bit Fowler/Noll/Vo FNV-1a hash on a buffer
4662 * @param[in] val value to hash
4663 * @param[in] hval initial value for hash
4664 * @return 64 bit hash
4665 *
4666 * NOTE: To use the recommended 64 bit FNV-1a hash, use MDB_HASH_INIT as the
4667 * hval arg on the first call.
4668 */
4669 static mdb_hash_t
mdb_hash_val(MDB_val * val,mdb_hash_t hval)4670 mdb_hash_val(MDB_val *val, mdb_hash_t hval)
4671 {
4672 unsigned char *s = (unsigned char *)val->mv_data; /* unsigned string */
4673 unsigned char *end = s + val->mv_size;
4674 /*
4675 * FNV-1a hash each octet of the string
4676 */
4677 while (s < end) {
4678 /* xor the bottom with the current octet */
4679 hval ^= (mdb_hash_t)*s++;
4680
4681 /* multiply by the 64 bit FNV magic prime mod 2^64 */
4682 hval += (hval << 1) + (hval << 4) + (hval << 5) +
4683 (hval << 7) + (hval << 8) + (hval << 40);
4684 }
4685 /* return our new hash value */
4686 return hval;
4687 }
4688
4689 /** Hash the string and output the encoded hash.
4690 * This uses modified RFC1924 Ascii85 encoding to accommodate systems with
4691 * very short name limits. We don't care about the encoding being reversible,
4692 * we just want to preserve as many bits of the input as possible in a
4693 * small printable string.
4694 * @param[in] str string to hash
4695 * @param[out] encbuf an array of 11 chars to hold the hash
4696 */
4697 static const char mdb_a85[]= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
4698
4699 static void ESECT
mdb_pack85(unsigned long l,char * out)4700 mdb_pack85(unsigned long l, char *out)
4701 {
4702 int i;
4703
4704 for (i=0; i<5; i++) {
4705 *out++ = mdb_a85[l % 85];
4706 l /= 85;
4707 }
4708 }
4709
4710 static void ESECT
mdb_hash_enc(MDB_val * val,char * encbuf)4711 mdb_hash_enc(MDB_val *val, char *encbuf)
4712 {
4713 mdb_hash_t h = mdb_hash_val(val, MDB_HASH_INIT);
4714
4715 mdb_pack85(h, encbuf);
4716 mdb_pack85(h>>32, encbuf+5);
4717 encbuf[10] = '\0';
4718 }
4719 #endif
4720
4721 /** Open and/or initialize the lock region for the environment.
4722 * @param[in] env The LMDB environment.
4723 * @param[in] fname Filename + scratch area, from #mdb_fname_init().
4724 * @param[in] mode The Unix permissions for the file, if we create it.
4725 * @param[in,out] excl In -1, out lock type: -1 none, 0 shared, 1 exclusive
4726 * @return 0 on success, non-zero on failure.
4727 */
4728 static int ESECT
mdb_env_setup_locks(MDB_env * env,MDB_name * fname,int mode,int * excl)4729 mdb_env_setup_locks(MDB_env *env, MDB_name *fname, int mode, int *excl)
4730 {
4731 #ifdef _WIN32
4732 # define MDB_ERRCODE_ROFS ERROR_WRITE_PROTECT
4733 #else
4734 # define MDB_ERRCODE_ROFS EROFS
4735 #endif
4736 int rc;
4737 off_t size, rsize;
4738
4739 rc = mdb_fopen(env, fname, MDB_O_LOCKS, mode, &env->me_lfd);
4740 if (rc) {
4741 /* Omit lockfile if read-only env on read-only filesystem */
4742 if (rc == MDB_ERRCODE_ROFS && (env->me_flags & MDB_RDONLY)) {
4743 return MDB_SUCCESS;
4744 }
4745 goto fail;
4746 }
4747
4748 if (!(env->me_flags & MDB_NOTLS)) {
4749 rc = pthread_key_create(&env->me_txkey, mdb_env_reader_dest);
4750 if (rc)
4751 goto fail;
4752 env->me_flags |= MDB_ENV_TXKEY;
4753 #ifdef _WIN32
4754 /* Windows TLS callbacks need help finding their TLS info. */
4755 if (mdb_tls_nkeys >= MAX_TLS_KEYS) {
4756 rc = MDB_TLS_FULL;
4757 goto fail;
4758 }
4759 mdb_tls_keys[mdb_tls_nkeys++] = env->me_txkey;
4760 #endif
4761 }
4762
4763 /* Try to get exclusive lock. If we succeed, then
4764 * nobody is using the lock region and we should initialize it.
4765 */
4766 if ((rc = mdb_env_excl_lock(env, excl))) goto fail;
4767
4768 #ifdef _WIN32
4769 size = GetFileSize(env->me_lfd, NULL);
4770 #else
4771 size = lseek(env->me_lfd, 0, SEEK_END);
4772 if (size == -1) goto fail_errno;
4773 #endif
4774 rsize = (env->me_maxreaders-1) * sizeof(MDB_reader) + sizeof(MDB_txninfo);
4775 if (size < rsize && *excl > 0) {
4776 #ifdef _WIN32
4777 if (SetFilePointer(env->me_lfd, rsize, NULL, FILE_BEGIN) != (DWORD)rsize
4778 || !SetEndOfFile(env->me_lfd))
4779 goto fail_errno;
4780 #else
4781 if (ftruncate(env->me_lfd, rsize) != 0) goto fail_errno;
4782 #endif
4783 } else {
4784 rsize = size;
4785 size = rsize - sizeof(MDB_txninfo);
4786 env->me_maxreaders = size/sizeof(MDB_reader) + 1;
4787 }
4788 {
4789 #ifdef _WIN32
4790 HANDLE mh;
4791 mh = CreateFileMapping(env->me_lfd, NULL, PAGE_READWRITE,
4792 0, 0, NULL);
4793 if (!mh) goto fail_errno;
4794 env->me_txns = MapViewOfFileEx(mh, FILE_MAP_WRITE, 0, 0, rsize, NULL);
4795 CloseHandle(mh);
4796 if (!env->me_txns) goto fail_errno;
4797 #else
4798 void *m = mmap(NULL, rsize, PROT_READ|PROT_WRITE, MAP_SHARED,
4799 env->me_lfd, 0);
4800 if (m == MAP_FAILED) goto fail_errno;
4801 env->me_txns = m;
4802 #endif
4803 }
4804 if (*excl > 0) {
4805 #ifdef _WIN32
4806 BY_HANDLE_FILE_INFORMATION stbuf;
4807 struct {
4808 DWORD volume;
4809 DWORD nhigh;
4810 DWORD nlow;
4811 } idbuf;
4812 MDB_val val;
4813 char encbuf[11];
4814
4815 if (!mdb_sec_inited) {
4816 InitializeSecurityDescriptor(&mdb_null_sd,
4817 SECURITY_DESCRIPTOR_REVISION);
4818 SetSecurityDescriptorDacl(&mdb_null_sd, TRUE, 0, FALSE);
4819 mdb_all_sa.nLength = sizeof(SECURITY_ATTRIBUTES);
4820 mdb_all_sa.bInheritHandle = FALSE;
4821 mdb_all_sa.lpSecurityDescriptor = &mdb_null_sd;
4822 mdb_sec_inited = 1;
4823 }
4824 if (!GetFileInformationByHandle(env->me_lfd, &stbuf)) goto fail_errno;
4825 idbuf.volume = stbuf.dwVolumeSerialNumber;
4826 idbuf.nhigh = stbuf.nFileIndexHigh;
4827 idbuf.nlow = stbuf.nFileIndexLow;
4828 val.mv_data = &idbuf;
4829 val.mv_size = sizeof(idbuf);
4830 mdb_hash_enc(&val, encbuf);
4831 sprintf(env->me_txns->mti_rmname, "Global\\MDBr%s", encbuf);
4832 sprintf(env->me_txns->mti_wmname, "Global\\MDBw%s", encbuf);
4833 env->me_rmutex = CreateMutexA(&mdb_all_sa, FALSE, env->me_txns->mti_rmname);
4834 if (!env->me_rmutex) goto fail_errno;
4835 env->me_wmutex = CreateMutexA(&mdb_all_sa, FALSE, env->me_txns->mti_wmname);
4836 if (!env->me_wmutex) goto fail_errno;
4837 #elif defined(MDB_USE_POSIX_SEM)
4838 struct stat stbuf;
4839 struct {
4840 dev_t dev;
4841 ino_t ino;
4842 } idbuf;
4843 MDB_val val;
4844 char encbuf[11];
4845
4846 #if defined(__NetBSD__)
4847 #define MDB_SHORT_SEMNAMES 1 /* limited to 14 chars */
4848 #endif
4849 if (fstat(env->me_lfd, &stbuf)) goto fail_errno;
4850 idbuf.dev = stbuf.st_dev;
4851 idbuf.ino = stbuf.st_ino;
4852 val.mv_data = &idbuf;
4853 val.mv_size = sizeof(idbuf);
4854 mdb_hash_enc(&val, encbuf);
4855 #ifdef MDB_SHORT_SEMNAMES
4856 encbuf[9] = '\0'; /* drop name from 15 chars to 14 chars */
4857 #endif
4858 sprintf(env->me_txns->mti_rmname, "/MDBr%s", encbuf);
4859 sprintf(env->me_txns->mti_wmname, "/MDBw%s", encbuf);
4860 /* Clean up after a previous run, if needed: Try to
4861 * remove both semaphores before doing anything else.
4862 */
4863 sem_unlink(env->me_txns->mti_rmname);
4864 sem_unlink(env->me_txns->mti_wmname);
4865 env->me_rmutex = sem_open(env->me_txns->mti_rmname,
4866 O_CREAT|O_EXCL, mode, 1);
4867 if (env->me_rmutex == SEM_FAILED) goto fail_errno;
4868 env->me_wmutex = sem_open(env->me_txns->mti_wmname,
4869 O_CREAT|O_EXCL, mode, 1);
4870 if (env->me_wmutex == SEM_FAILED) goto fail_errno;
4871 #else /* MDB_USE_POSIX_MUTEX: */
4872 pthread_mutexattr_t mattr;
4873
4874 /* Solaris needs this before initing a robust mutex. Otherwise
4875 * it may skip the init and return EBUSY "seems someone already
4876 * inited" or EINVAL "it was inited differently".
4877 */
4878 memset(env->me_txns->mti_rmutex, 0, sizeof(*env->me_txns->mti_rmutex));
4879 memset(env->me_txns->mti_wmutex, 0, sizeof(*env->me_txns->mti_wmutex));
4880
4881 if ((rc = pthread_mutexattr_init(&mattr)))
4882 goto fail;
4883
4884 rc = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
4885 #ifdef MDB_ROBUST_SUPPORTED
4886 if (!rc) rc = pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST);
4887 #endif
4888 if (!rc) rc = pthread_mutex_init(env->me_txns->mti_rmutex, &mattr);
4889 if (!rc) rc = pthread_mutex_init(env->me_txns->mti_wmutex, &mattr);
4890 pthread_mutexattr_destroy(&mattr);
4891 if (rc)
4892 goto fail;
4893 #endif /* _WIN32 || MDB_USE_POSIX_SEM */
4894
4895 env->me_txns->mti_magic = MDB_MAGIC;
4896 env->me_txns->mti_format = MDB_LOCK_FORMAT;
4897 env->me_txns->mti_txnid = 0;
4898 env->me_txns->mti_numreaders = 0;
4899
4900 } else {
4901 if (env->me_txns->mti_magic != MDB_MAGIC) {
4902 DPUTS("lock region has invalid magic");
4903 rc = MDB_INVALID;
4904 goto fail;
4905 }
4906 if (env->me_txns->mti_format != MDB_LOCK_FORMAT) {
4907 DPRINTF(("lock region has format+version 0x%x, expected 0x%x",
4908 env->me_txns->mti_format, MDB_LOCK_FORMAT));
4909 rc = MDB_VERSION_MISMATCH;
4910 goto fail;
4911 }
4912 rc = ErrCode();
4913 if (rc && rc != EACCES && rc != EAGAIN) {
4914 goto fail;
4915 }
4916 #ifdef _WIN32
4917 env->me_rmutex = OpenMutexA(SYNCHRONIZE, FALSE, env->me_txns->mti_rmname);
4918 if (!env->me_rmutex) goto fail_errno;
4919 env->me_wmutex = OpenMutexA(SYNCHRONIZE, FALSE, env->me_txns->mti_wmname);
4920 if (!env->me_wmutex) goto fail_errno;
4921 #elif defined(MDB_USE_POSIX_SEM)
4922 env->me_rmutex = sem_open(env->me_txns->mti_rmname, 0);
4923 if (env->me_rmutex == SEM_FAILED) goto fail_errno;
4924 env->me_wmutex = sem_open(env->me_txns->mti_wmname, 0);
4925 if (env->me_wmutex == SEM_FAILED) goto fail_errno;
4926 #endif
4927 }
4928 return MDB_SUCCESS;
4929
4930 fail_errno:
4931 rc = ErrCode();
4932 fail:
4933 return rc;
4934 }
4935
4936 /** Only a subset of the @ref mdb_env flags can be changed
4937 * at runtime. Changing other flags requires closing the
4938 * environment and re-opening it with the new flags.
4939 */
4940 #define CHANGEABLE (MDB_NOSYNC|MDB_NOMETASYNC|MDB_MAPASYNC|MDB_NOMEMINIT)
4941 #define CHANGELESS (MDB_FIXEDMAP|MDB_NOSUBDIR|MDB_RDONLY| \
4942 MDB_WRITEMAP|MDB_NOTLS|MDB_NOLOCK|MDB_NORDAHEAD)
4943
4944 #if VALID_FLAGS & PERSISTENT_FLAGS & (CHANGEABLE|CHANGELESS)
4945 # error "Persistent DB flags & env flags overlap, but both go in mm_flags"
4946 #endif
4947
4948 int ESECT
mdb_env_open(MDB_env * env,const char * path,unsigned int flags,mdb_mode_t mode)4949 mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode)
4950 {
4951 int rc, excl = -1;
4952 MDB_name fname;
4953
4954 if (env->me_fd!=INVALID_HANDLE_VALUE || (flags & ~(CHANGEABLE|CHANGELESS)))
4955 return EINVAL;
4956
4957 flags |= env->me_flags;
4958
4959 rc = mdb_fname_init(path, flags, &fname);
4960 if (rc)
4961 return rc;
4962
4963 if (flags & MDB_RDONLY) {
4964 /* silently ignore WRITEMAP when we're only getting read access */
4965 flags &= ~MDB_WRITEMAP;
4966 } else {
4967 if (!((env->me_free_pgs = mdb_midl_alloc(MDB_IDL_UM_MAX)) &&
4968 (env->me_dirty_list = calloc(MDB_IDL_UM_SIZE, sizeof(MDB_ID2)))))
4969 rc = ENOMEM;
4970 }
4971 env->me_flags = flags |= MDB_ENV_ACTIVE;
4972 if (rc)
4973 goto leave;
4974
4975 env->me_path = strdup(path);
4976 env->me_dbxs = calloc(env->me_maxdbs, sizeof(MDB_dbx));
4977 env->me_dbflags = calloc(env->me_maxdbs, sizeof(uint16_t));
4978 env->me_dbiseqs = calloc(env->me_maxdbs, sizeof(unsigned int));
4979 if (!(env->me_dbxs && env->me_path && env->me_dbflags && env->me_dbiseqs)) {
4980 rc = ENOMEM;
4981 goto leave;
4982 }
4983 env->me_dbxs[FREE_DBI].md_cmp = mdb_cmp_long; /* aligned MDB_INTEGERKEY */
4984
4985 /* For RDONLY, get lockfile after we know datafile exists */
4986 if (!(flags & (MDB_RDONLY|MDB_NOLOCK))) {
4987 rc = mdb_env_setup_locks(env, &fname, mode, &excl);
4988 if (rc)
4989 goto leave;
4990 }
4991
4992 rc = mdb_fopen(env, &fname,
4993 (flags & MDB_RDONLY) ? MDB_O_RDONLY : MDB_O_RDWR,
4994 mode, &env->me_fd);
4995 if (rc)
4996 goto leave;
4997
4998 if ((flags & (MDB_RDONLY|MDB_NOLOCK)) == MDB_RDONLY) {
4999 rc = mdb_env_setup_locks(env, &fname, mode, &excl);
5000 if (rc)
5001 goto leave;
5002 }
5003
5004 if ((rc = mdb_env_open2(env)) == MDB_SUCCESS) {
5005 if (!(flags & (MDB_RDONLY|MDB_WRITEMAP))) {
5006 /* Synchronous fd for meta writes. Needed even with
5007 * MDB_NOSYNC/MDB_NOMETASYNC, in case these get reset.
5008 */
5009 rc = mdb_fopen(env, &fname, MDB_O_META, mode, &env->me_mfd);
5010 if (rc)
5011 goto leave;
5012 }
5013 DPRINTF(("opened dbenv %p", (void *) env));
5014 if (excl > 0) {
5015 rc = mdb_env_share_locks(env, &excl);
5016 if (rc)
5017 goto leave;
5018 }
5019 if (!(flags & MDB_RDONLY)) {
5020 MDB_txn *txn;
5021 int tsize = sizeof(MDB_txn), size = tsize + env->me_maxdbs *
5022 (sizeof(MDB_db)+sizeof(MDB_cursor *)+sizeof(unsigned int)+1);
5023 if ((env->me_pbuf = calloc(1, env->me_psize)) &&
5024 (txn = calloc(1, size)))
5025 {
5026 txn->mt_dbs = (MDB_db *)((char *)txn + tsize);
5027 txn->mt_cursors = (MDB_cursor **)(txn->mt_dbs + env->me_maxdbs);
5028 txn->mt_dbiseqs = (unsigned int *)(txn->mt_cursors + env->me_maxdbs);
5029 txn->mt_dbflags = (unsigned char *)(txn->mt_dbiseqs + env->me_maxdbs);
5030 txn->mt_env = env;
5031 txn->mt_dbxs = env->me_dbxs;
5032 txn->mt_flags = MDB_TXN_FINISHED;
5033 env->me_txn0 = txn;
5034 } else {
5035 rc = ENOMEM;
5036 }
5037 }
5038 }
5039
5040 leave:
5041 if (rc) {
5042 mdb_env_close0(env, excl);
5043 }
5044 mdb_fname_destroy(fname);
5045 return rc;
5046 }
5047
5048 /** Destroy resources from mdb_env_open(), clear our readers & DBIs */
5049 static void ESECT
mdb_env_close0(MDB_env * env,int excl)5050 mdb_env_close0(MDB_env *env, int excl)
5051 {
5052 int i;
5053
5054 if (!(env->me_flags & MDB_ENV_ACTIVE))
5055 return;
5056
5057 /* Doing this here since me_dbxs may not exist during mdb_env_close */
5058 if (env->me_dbxs) {
5059 for (i = env->me_maxdbs; --i >= CORE_DBS; )
5060 free(env->me_dbxs[i].md_name.mv_data);
5061 free(env->me_dbxs);
5062 }
5063
5064 free(env->me_pbuf);
5065 free(env->me_dbiseqs);
5066 free(env->me_dbflags);
5067 free(env->me_path);
5068 free(env->me_dirty_list);
5069 free(env->me_txn0);
5070 mdb_midl_free(env->me_free_pgs);
5071
5072 if (env->me_flags & MDB_ENV_TXKEY) {
5073 pthread_key_delete(env->me_txkey);
5074 #ifdef _WIN32
5075 /* Delete our key from the global list */
5076 for (i=0; i<mdb_tls_nkeys; i++)
5077 if (mdb_tls_keys[i] == env->me_txkey) {
5078 mdb_tls_keys[i] = mdb_tls_keys[mdb_tls_nkeys-1];
5079 mdb_tls_nkeys--;
5080 break;
5081 }
5082 #endif
5083 }
5084
5085 if (env->me_map) {
5086 munmap(env->me_map, env->me_mapsize);
5087 }
5088 if (env->me_mfd != INVALID_HANDLE_VALUE)
5089 (void) close(env->me_mfd);
5090 if (env->me_fd != INVALID_HANDLE_VALUE)
5091 (void) close(env->me_fd);
5092 if (env->me_txns) {
5093 MDB_PID_T pid = getpid();
5094 /* Clearing readers is done in this function because
5095 * me_txkey with its destructor must be disabled first.
5096 *
5097 * We skip the the reader mutex, so we touch only
5098 * data owned by this process (me_close_readers and
5099 * our readers), and clear each reader atomically.
5100 */
5101 for (i = env->me_close_readers; --i >= 0; )
5102 if (env->me_txns->mti_readers[i].mr_pid == pid)
5103 env->me_txns->mti_readers[i].mr_pid = 0;
5104 #ifdef _WIN32
5105 if (env->me_rmutex) {
5106 CloseHandle(env->me_rmutex);
5107 if (env->me_wmutex) CloseHandle(env->me_wmutex);
5108 }
5109 /* Windows automatically destroys the mutexes when
5110 * the last handle closes.
5111 */
5112 #elif defined(MDB_USE_POSIX_SEM)
5113 if (env->me_rmutex != SEM_FAILED) {
5114 sem_close(env->me_rmutex);
5115 if (env->me_wmutex != SEM_FAILED)
5116 sem_close(env->me_wmutex);
5117 /* If we have the filelock: If we are the
5118 * only remaining user, clean up semaphores.
5119 */
5120 if (excl == 0)
5121 mdb_env_excl_lock(env, &excl);
5122 if (excl > 0) {
5123 sem_unlink(env->me_txns->mti_rmname);
5124 sem_unlink(env->me_txns->mti_wmname);
5125 }
5126 }
5127 #endif
5128 munmap((void *)env->me_txns, (env->me_maxreaders-1)*sizeof(MDB_reader)+sizeof(MDB_txninfo));
5129 }
5130 if (env->me_lfd != INVALID_HANDLE_VALUE) {
5131 #ifdef _WIN32
5132 if (excl >= 0) {
5133 /* Unlock the lockfile. Windows would have unlocked it
5134 * after closing anyway, but not necessarily at once.
5135 */
5136 UnlockFile(env->me_lfd, 0, 0, 1, 0);
5137 }
5138 #endif
5139 (void) close(env->me_lfd);
5140 }
5141
5142 env->me_flags &= ~(MDB_ENV_ACTIVE|MDB_ENV_TXKEY);
5143 }
5144
5145 void ESECT
mdb_env_close(MDB_env * env)5146 mdb_env_close(MDB_env *env)
5147 {
5148 MDB_page *dp;
5149
5150 if (env == NULL)
5151 return;
5152
5153 VGMEMP_DESTROY(env);
5154 while ((dp = env->me_dpages) != NULL) {
5155 VGMEMP_DEFINED(&dp->mp_next, sizeof(dp->mp_next));
5156 env->me_dpages = dp->mp_next;
5157 free(dp);
5158 }
5159
5160 mdb_env_close0(env, 0);
5161 free(env);
5162 }
5163
5164 /** Compare two items pointing at aligned size_t's */
5165 static int
mdb_cmp_long(const MDB_val * a,const MDB_val * b)5166 mdb_cmp_long(const MDB_val *a, const MDB_val *b)
5167 {
5168 return (*(size_t *)a->mv_data < *(size_t *)b->mv_data) ? -1 :
5169 *(size_t *)a->mv_data > *(size_t *)b->mv_data;
5170 }
5171
5172 /** Compare two items pointing at aligned unsigned int's.
5173 *
5174 * This is also set as #MDB_INTEGERDUP|#MDB_DUPFIXED's #MDB_dbx.%md_dcmp,
5175 * but #mdb_cmp_clong() is called instead if the data type is size_t.
5176 */
5177 static int
mdb_cmp_int(const MDB_val * a,const MDB_val * b)5178 mdb_cmp_int(const MDB_val *a, const MDB_val *b)
5179 {
5180 return (*(unsigned int *)a->mv_data < *(unsigned int *)b->mv_data) ? -1 :
5181 *(unsigned int *)a->mv_data > *(unsigned int *)b->mv_data;
5182 }
5183
5184 /** Compare two items pointing at unsigned ints of unknown alignment.
5185 * Nodes and keys are guaranteed to be 2-byte aligned.
5186 */
5187 static int
mdb_cmp_cint(const MDB_val * a,const MDB_val * b)5188 mdb_cmp_cint(const MDB_val *a, const MDB_val *b)
5189 {
5190 #if BYTE_ORDER == LITTLE_ENDIAN
5191 unsigned short *u, *c;
5192 int x;
5193
5194 u = (unsigned short *) ((char *) a->mv_data + a->mv_size);
5195 c = (unsigned short *) ((char *) b->mv_data + a->mv_size);
5196 do {
5197 x = *--u - *--c;
5198 } while(!x && u > (unsigned short *)a->mv_data);
5199 return x;
5200 #else
5201 unsigned short *u, *c, *end;
5202 int x;
5203
5204 end = (unsigned short *) ((char *) a->mv_data + a->mv_size);
5205 u = (unsigned short *)a->mv_data;
5206 c = (unsigned short *)b->mv_data;
5207 do {
5208 x = *u++ - *c++;
5209 } while(!x && u < end);
5210 return x;
5211 #endif
5212 }
5213
5214 /** Compare two items lexically */
5215 static int
mdb_cmp_memn(const MDB_val * a,const MDB_val * b)5216 mdb_cmp_memn(const MDB_val *a, const MDB_val *b)
5217 {
5218 int diff;
5219 ssize_t len_diff;
5220 unsigned int len;
5221
5222 len = a->mv_size;
5223 len_diff = (ssize_t) a->mv_size - (ssize_t) b->mv_size;
5224 if (len_diff > 0) {
5225 len = b->mv_size;
5226 len_diff = 1;
5227 }
5228
5229 diff = memcmp(a->mv_data, b->mv_data, len);
5230 return diff ? diff : len_diff<0 ? -1 : len_diff;
5231 }
5232
5233 /** Compare two items in reverse byte order */
5234 static int
mdb_cmp_memnr(const MDB_val * a,const MDB_val * b)5235 mdb_cmp_memnr(const MDB_val *a, const MDB_val *b)
5236 {
5237 const unsigned char *p1, *p2, *p1_lim;
5238 ssize_t len_diff;
5239 int diff;
5240
5241 p1_lim = (const unsigned char *)a->mv_data;
5242 p1 = (const unsigned char *)a->mv_data + a->mv_size;
5243 p2 = (const unsigned char *)b->mv_data + b->mv_size;
5244
5245 len_diff = (ssize_t) a->mv_size - (ssize_t) b->mv_size;
5246 if (len_diff > 0) {
5247 p1_lim += len_diff;
5248 len_diff = 1;
5249 }
5250
5251 while (p1 > p1_lim) {
5252 diff = *--p1 - *--p2;
5253 if (diff)
5254 return diff;
5255 }
5256 return len_diff<0 ? -1 : len_diff;
5257 }
5258
5259 /** Search for key within a page, using binary search.
5260 * Returns the smallest entry larger or equal to the key.
5261 * If exactp is non-null, stores whether the found entry was an exact match
5262 * in *exactp (1 or 0).
5263 * Updates the cursor index with the index of the found entry.
5264 * If no entry larger or equal to the key is found, returns NULL.
5265 */
5266 static MDB_node *
mdb_node_search(MDB_cursor * mc,MDB_val * key,int * exactp)5267 mdb_node_search(MDB_cursor *mc, MDB_val *key, int *exactp)
5268 {
5269 unsigned int i = 0, nkeys;
5270 int low, high;
5271 int rc = 0;
5272 MDB_page *mp = mc->mc_pg[mc->mc_top];
5273 MDB_node *node = NULL;
5274 MDB_val nodekey;
5275 MDB_cmp_func *cmp;
5276 DKBUF;
5277
5278 nkeys = NUMKEYS(mp);
5279
5280 DPRINTF(("searching %u keys in %s %spage %"Z"u",
5281 nkeys, IS_LEAF(mp) ? "leaf" : "branch", IS_SUBP(mp) ? "sub-" : "",
5282 mdb_dbg_pgno(mp)));
5283
5284 low = IS_LEAF(mp) ? 0 : 1;
5285 high = nkeys - 1;
5286 cmp = mc->mc_dbx->md_cmp;
5287
5288 /* Branch pages have no data, so if using integer keys,
5289 * alignment is guaranteed. Use faster mdb_cmp_int.
5290 */
5291 if (cmp == mdb_cmp_cint && IS_BRANCH(mp)) {
5292 if (NODEPTR(mp, 1)->mn_ksize == sizeof(size_t))
5293 cmp = mdb_cmp_long;
5294 else
5295 cmp = mdb_cmp_int;
5296 }
5297
5298 if (IS_LEAF2(mp)) {
5299 nodekey.mv_size = mc->mc_db->md_pad;
5300 node = NODEPTR(mp, 0); /* fake */
5301 while (low <= high) {
5302 i = (low + high) >> 1;
5303 nodekey.mv_data = LEAF2KEY(mp, i, nodekey.mv_size);
5304 rc = cmp(key, &nodekey);
5305 DPRINTF(("found leaf index %u [%s], rc = %i",
5306 i, DKEY(&nodekey), rc));
5307 if (rc == 0)
5308 break;
5309 if (rc > 0)
5310 low = i + 1;
5311 else
5312 high = i - 1;
5313 }
5314 } else {
5315 while (low <= high) {
5316 i = (low + high) >> 1;
5317
5318 node = NODEPTR(mp, i);
5319 nodekey.mv_size = NODEKSZ(node);
5320 nodekey.mv_data = NODEKEY(node);
5321
5322 rc = cmp(key, &nodekey);
5323 #if MDB_DEBUG
5324 if (IS_LEAF(mp))
5325 DPRINTF(("found leaf index %u [%s], rc = %i",
5326 i, DKEY(&nodekey), rc));
5327 else
5328 DPRINTF(("found branch index %u [%s -> %"Z"u], rc = %i",
5329 i, DKEY(&nodekey), NODEPGNO(node), rc));
5330 #endif
5331 if (rc == 0)
5332 break;
5333 if (rc > 0)
5334 low = i + 1;
5335 else
5336 high = i - 1;
5337 }
5338 }
5339
5340 if (rc > 0) { /* Found entry is less than the key. */
5341 i++; /* Skip to get the smallest entry larger than key. */
5342 if (!IS_LEAF2(mp))
5343 node = NODEPTR(mp, i);
5344 }
5345 if (exactp)
5346 *exactp = (rc == 0 && nkeys > 0);
5347 /* store the key index */
5348 mc->mc_ki[mc->mc_top] = i;
5349 if (i >= nkeys)
5350 /* There is no entry larger or equal to the key. */
5351 return NULL;
5352
5353 /* nodeptr is fake for LEAF2 */
5354 return node;
5355 }
5356
5357 #if 0
5358 static void
5359 mdb_cursor_adjust(MDB_cursor *mc, func)
5360 {
5361 MDB_cursor *m2;
5362
5363 for (m2 = mc->mc_txn->mt_cursors[mc->mc_dbi]; m2; m2=m2->mc_next) {
5364 if (m2->mc_pg[m2->mc_top] == mc->mc_pg[mc->mc_top]) {
5365 func(mc, m2);
5366 }
5367 }
5368 }
5369 #endif
5370
5371 /** Pop a page off the top of the cursor's stack. */
5372 static void
mdb_cursor_pop(MDB_cursor * mc)5373 mdb_cursor_pop(MDB_cursor *mc)
5374 {
5375 if (mc->mc_snum) {
5376 DPRINTF(("popping page %"Z"u off db %d cursor %p",
5377 mc->mc_pg[mc->mc_top]->mp_pgno, DDBI(mc), (void *) mc));
5378
5379 mc->mc_snum--;
5380 if (mc->mc_snum) {
5381 mc->mc_top--;
5382 } else {
5383 mc->mc_flags &= ~C_INITIALIZED;
5384 }
5385 }
5386 }
5387
5388 /** Push a page onto the top of the cursor's stack.
5389 * Set #MDB_TXN_ERROR on failure.
5390 */
5391 static int
mdb_cursor_push(MDB_cursor * mc,MDB_page * mp)5392 mdb_cursor_push(MDB_cursor *mc, MDB_page *mp)
5393 {
5394 DPRINTF(("pushing page %"Z"u on db %d cursor %p", mp->mp_pgno,
5395 DDBI(mc), (void *) mc));
5396
5397 if (mc->mc_snum >= CURSOR_STACK) {
5398 mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
5399 return MDB_CURSOR_FULL;
5400 }
5401
5402 mc->mc_top = mc->mc_snum++;
5403 mc->mc_pg[mc->mc_top] = mp;
5404 mc->mc_ki[mc->mc_top] = 0;
5405
5406 return MDB_SUCCESS;
5407 }
5408
5409 /** Find the address of the page corresponding to a given page number.
5410 * Set #MDB_TXN_ERROR on failure.
5411 * @param[in] mc the cursor accessing the page.
5412 * @param[in] pgno the page number for the page to retrieve.
5413 * @param[out] ret address of a pointer where the page's address will be stored.
5414 * @param[out] lvl dirty_list inheritance level of found page. 1=current txn, 0=mapped page.
5415 * @return 0 on success, non-zero on failure.
5416 */
5417 static int
mdb_page_get(MDB_cursor * mc,pgno_t pgno,MDB_page ** ret,int * lvl)5418 mdb_page_get(MDB_cursor *mc, pgno_t pgno, MDB_page **ret, int *lvl)
5419 {
5420 MDB_txn *txn = mc->mc_txn;
5421 MDB_env *env = txn->mt_env;
5422 MDB_page *p = NULL;
5423 int level;
5424
5425 if (! (txn->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_WRITEMAP))) {
5426 MDB_txn *tx2 = txn;
5427 level = 1;
5428 do {
5429 MDB_ID2L dl = tx2->mt_u.dirty_list;
5430 unsigned x;
5431 /* Spilled pages were dirtied in this txn and flushed
5432 * because the dirty list got full. Bring this page
5433 * back in from the map (but don't unspill it here,
5434 * leave that unless page_touch happens again).
5435 */
5436 if (tx2->mt_spill_pgs) {
5437 MDB_ID pn = pgno << 1;
5438 x = mdb_midl_search(tx2->mt_spill_pgs, pn);
5439 if (x <= tx2->mt_spill_pgs[0] && tx2->mt_spill_pgs[x] == pn) {
5440 p = (MDB_page *)(env->me_map + env->me_psize * pgno);
5441 goto done;
5442 }
5443 }
5444 if (dl[0].mid) {
5445 unsigned x = mdb_mid2l_search(dl, pgno);
5446 if (x <= dl[0].mid && dl[x].mid == pgno) {
5447 p = dl[x].mptr;
5448 goto done;
5449 }
5450 }
5451 level++;
5452 } while ((tx2 = tx2->mt_parent) != NULL);
5453 }
5454
5455 if (pgno < txn->mt_next_pgno) {
5456 level = 0;
5457 p = (MDB_page *)(env->me_map + env->me_psize * pgno);
5458 } else {
5459 DPRINTF(("page %"Z"u not found", pgno));
5460 txn->mt_flags |= MDB_TXN_ERROR;
5461 return MDB_PAGE_NOTFOUND;
5462 }
5463
5464 done:
5465 *ret = p;
5466 if (lvl)
5467 *lvl = level;
5468 return MDB_SUCCESS;
5469 }
5470
5471 /** Finish #mdb_page_search() / #mdb_page_search_lowest().
5472 * The cursor is at the root page, set up the rest of it.
5473 */
5474 static int
mdb_page_search_root(MDB_cursor * mc,MDB_val * key,int flags)5475 mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int flags)
5476 {
5477 MDB_page *mp = mc->mc_pg[mc->mc_top];
5478 int rc;
5479 DKBUF;
5480
5481 while (IS_BRANCH(mp)) {
5482 MDB_node *node;
5483 indx_t i;
5484
5485 DPRINTF(("branch page %"Z"u has %u keys", mp->mp_pgno, NUMKEYS(mp)));
5486 /* Don't assert on branch pages in the FreeDB. We can get here
5487 * while in the process of rebalancing a FreeDB branch page; we must
5488 * let that proceed. ITS#8336
5489 */
5490 mdb_cassert(mc, !mc->mc_dbi || NUMKEYS(mp) > 1);
5491 DPRINTF(("found index 0 to page %"Z"u", NODEPGNO(NODEPTR(mp, 0))));
5492
5493 if (flags & (MDB_PS_FIRST|MDB_PS_LAST)) {
5494 i = 0;
5495 if (flags & MDB_PS_LAST) {
5496 i = NUMKEYS(mp) - 1;
5497 /* if already init'd, see if we're already in right place */
5498 if (mc->mc_flags & C_INITIALIZED) {
5499 if (mc->mc_ki[mc->mc_top] == i) {
5500 mc->mc_top = mc->mc_snum++;
5501 mp = mc->mc_pg[mc->mc_top];
5502 goto ready;
5503 }
5504 }
5505 }
5506 } else {
5507 int exact;
5508 node = mdb_node_search(mc, key, &exact);
5509 if (node == NULL)
5510 i = NUMKEYS(mp) - 1;
5511 else {
5512 i = mc->mc_ki[mc->mc_top];
5513 if (!exact) {
5514 mdb_cassert(mc, i > 0);
5515 i--;
5516 }
5517 }
5518 DPRINTF(("following index %u for key [%s]", i, DKEY(key)));
5519 }
5520
5521 mdb_cassert(mc, i < NUMKEYS(mp));
5522 node = NODEPTR(mp, i);
5523
5524 if ((rc = mdb_page_get(mc, NODEPGNO(node), &mp, NULL)) != 0)
5525 return rc;
5526
5527 mc->mc_ki[mc->mc_top] = i;
5528 if ((rc = mdb_cursor_push(mc, mp)))
5529 return rc;
5530
5531 ready:
5532 if (flags & MDB_PS_MODIFY) {
5533 if ((rc = mdb_page_touch(mc)) != 0)
5534 return rc;
5535 mp = mc->mc_pg[mc->mc_top];
5536 }
5537 }
5538
5539 if (!IS_LEAF(mp)) {
5540 DPRINTF(("internal error, index points to a %02X page!?",
5541 mp->mp_flags));
5542 mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
5543 return MDB_CORRUPTED;
5544 }
5545
5546 DPRINTF(("found leaf page %"Z"u for key [%s]", mp->mp_pgno,
5547 key ? DKEY(key) : "null"));
5548 mc->mc_flags |= C_INITIALIZED;
5549 mc->mc_flags &= ~C_EOF;
5550
5551 return MDB_SUCCESS;
5552 }
5553
5554 /** Search for the lowest key under the current branch page.
5555 * This just bypasses a NUMKEYS check in the current page
5556 * before calling mdb_page_search_root(), because the callers
5557 * are all in situations where the current page is known to
5558 * be underfilled.
5559 */
5560 static int
mdb_page_search_lowest(MDB_cursor * mc)5561 mdb_page_search_lowest(MDB_cursor *mc)
5562 {
5563 MDB_page *mp = mc->mc_pg[mc->mc_top];
5564 MDB_node *node = NODEPTR(mp, 0);
5565 int rc;
5566
5567 if ((rc = mdb_page_get(mc, NODEPGNO(node), &mp, NULL)) != 0)
5568 return rc;
5569
5570 mc->mc_ki[mc->mc_top] = 0;
5571 if ((rc = mdb_cursor_push(mc, mp)))
5572 return rc;
5573 return mdb_page_search_root(mc, NULL, MDB_PS_FIRST);
5574 }
5575
5576 /** Search for the page a given key should be in.
5577 * Push it and its parent pages on the cursor stack.
5578 * @param[in,out] mc the cursor for this operation.
5579 * @param[in] key the key to search for, or NULL for first/last page.
5580 * @param[in] flags If MDB_PS_MODIFY is set, visited pages in the DB
5581 * are touched (updated with new page numbers).
5582 * If MDB_PS_FIRST or MDB_PS_LAST is set, find first or last leaf.
5583 * This is used by #mdb_cursor_first() and #mdb_cursor_last().
5584 * If MDB_PS_ROOTONLY set, just fetch root node, no further lookups.
5585 * @return 0 on success, non-zero on failure.
5586 */
5587 static int
mdb_page_search(MDB_cursor * mc,MDB_val * key,int flags)5588 mdb_page_search(MDB_cursor *mc, MDB_val *key, int flags)
5589 {
5590 int rc;
5591 pgno_t root;
5592
5593 /* Make sure the txn is still viable, then find the root from
5594 * the txn's db table and set it as the root of the cursor's stack.
5595 */
5596 if (mc->mc_txn->mt_flags & MDB_TXN_BLOCKED) {
5597 DPUTS("transaction may not be used now");
5598 return MDB_BAD_TXN;
5599 } else {
5600 /* Make sure we're using an up-to-date root */
5601 if (*mc->mc_dbflag & DB_STALE) {
5602 MDB_cursor mc2;
5603 if (TXN_DBI_CHANGED(mc->mc_txn, mc->mc_dbi))
5604 return MDB_BAD_DBI;
5605 mdb_cursor_init(&mc2, mc->mc_txn, MAIN_DBI, NULL);
5606 rc = mdb_page_search(&mc2, &mc->mc_dbx->md_name, 0);
5607 if (rc)
5608 return rc;
5609 {
5610 MDB_val data;
5611 int exact = 0;
5612 uint16_t flags;
5613 MDB_node *leaf = mdb_node_search(&mc2,
5614 &mc->mc_dbx->md_name, &exact);
5615 if (!exact)
5616 return MDB_NOTFOUND;
5617 if ((leaf->mn_flags & (F_DUPDATA|F_SUBDATA)) != F_SUBDATA)
5618 return MDB_INCOMPATIBLE; /* not a named DB */
5619 rc = mdb_node_read(&mc2, leaf, &data);
5620 if (rc)
5621 return rc;
5622 memcpy(&flags, ((char *) data.mv_data + offsetof(MDB_db, md_flags)),
5623 sizeof(uint16_t));
5624 /* The txn may not know this DBI, or another process may
5625 * have dropped and recreated the DB with other flags.
5626 */
5627 if ((mc->mc_db->md_flags & PERSISTENT_FLAGS) != flags)
5628 return MDB_INCOMPATIBLE;
5629 memcpy(mc->mc_db, data.mv_data, sizeof(MDB_db));
5630 }
5631 *mc->mc_dbflag &= ~DB_STALE;
5632 }
5633 root = mc->mc_db->md_root;
5634
5635 if (root == P_INVALID) { /* Tree is empty. */
5636 DPUTS("tree is empty");
5637 return MDB_NOTFOUND;
5638 }
5639 }
5640
5641 mdb_cassert(mc, root > 1);
5642 if (!mc->mc_pg[0] || mc->mc_pg[0]->mp_pgno != root)
5643 if ((rc = mdb_page_get(mc, root, &mc->mc_pg[0], NULL)) != 0)
5644 return rc;
5645
5646 mc->mc_snum = 1;
5647 mc->mc_top = 0;
5648
5649 DPRINTF(("db %d root page %"Z"u has flags 0x%X",
5650 DDBI(mc), root, mc->mc_pg[0]->mp_flags));
5651
5652 if (flags & MDB_PS_MODIFY) {
5653 if ((rc = mdb_page_touch(mc)))
5654 return rc;
5655 }
5656
5657 if (flags & MDB_PS_ROOTONLY)
5658 return MDB_SUCCESS;
5659
5660 return mdb_page_search_root(mc, key, flags);
5661 }
5662
5663 static int
mdb_ovpage_free(MDB_cursor * mc,MDB_page * mp)5664 mdb_ovpage_free(MDB_cursor *mc, MDB_page *mp)
5665 {
5666 MDB_txn *txn = mc->mc_txn;
5667 pgno_t pg = mp->mp_pgno;
5668 unsigned x = 0, ovpages = mp->mp_pages;
5669 MDB_env *env = txn->mt_env;
5670 MDB_IDL sl = txn->mt_spill_pgs;
5671 MDB_ID pn = pg << 1;
5672 int rc;
5673
5674 DPRINTF(("free ov page %"Z"u (%d)", pg, ovpages));
5675 /* If the page is dirty or on the spill list we just acquired it,
5676 * so we should give it back to our current free list, if any.
5677 * Otherwise put it onto the list of pages we freed in this txn.
5678 *
5679 * Won't create me_pghead: me_pglast must be inited along with it.
5680 * Unsupported in nested txns: They would need to hide the page
5681 * range in ancestor txns' dirty and spilled lists.
5682 */
5683 if (env->me_pghead &&
5684 !txn->mt_parent &&
5685 ((mp->mp_flags & P_DIRTY) ||
5686 (sl && (x = mdb_midl_search(sl, pn)) <= sl[0] && sl[x] == pn)))
5687 {
5688 unsigned i, j;
5689 pgno_t *mop;
5690 MDB_ID2 *dl, ix, iy;
5691 rc = mdb_midl_need(&env->me_pghead, ovpages);
5692 if (rc)
5693 return rc;
5694 if (!(mp->mp_flags & P_DIRTY)) {
5695 /* This page is no longer spilled */
5696 if (x == sl[0])
5697 sl[0]--;
5698 else
5699 sl[x] |= 1;
5700 goto release;
5701 }
5702 /* Remove from dirty list */
5703 dl = txn->mt_u.dirty_list;
5704 x = dl[0].mid--;
5705 for (ix = dl[x]; ix.mptr != mp; ix = iy) {
5706 if (x > 1) {
5707 x--;
5708 iy = dl[x];
5709 dl[x] = ix;
5710 } else {
5711 mdb_cassert(mc, x > 1);
5712 j = ++(dl[0].mid);
5713 dl[j] = ix; /* Unsorted. OK when MDB_TXN_ERROR. */
5714 txn->mt_flags |= MDB_TXN_ERROR;
5715 return MDB_CORRUPTED;
5716 }
5717 }
5718 txn->mt_dirty_room++;
5719 if (!(env->me_flags & MDB_WRITEMAP))
5720 mdb_dpage_free(env, mp);
5721 release:
5722 /* Insert in me_pghead */
5723 mop = env->me_pghead;
5724 j = mop[0] + ovpages;
5725 for (i = mop[0]; i && mop[i] < pg; i--)
5726 mop[j--] = mop[i];
5727 while (j>i)
5728 mop[j--] = pg++;
5729 mop[0] += ovpages;
5730 } else {
5731 rc = mdb_midl_append_range(&txn->mt_free_pgs, pg, ovpages);
5732 if (rc)
5733 return rc;
5734 }
5735 mc->mc_db->md_overflow_pages -= ovpages;
5736 return 0;
5737 }
5738
5739 /** Return the data associated with a given node.
5740 * @param[in] mc The cursor for this operation.
5741 * @param[in] leaf The node being read.
5742 * @param[out] data Updated to point to the node's data.
5743 * @return 0 on success, non-zero on failure.
5744 */
5745 static int
mdb_node_read(MDB_cursor * mc,MDB_node * leaf,MDB_val * data)5746 mdb_node_read(MDB_cursor *mc, MDB_node *leaf, MDB_val *data)
5747 {
5748 MDB_page *omp; /* overflow page */
5749 pgno_t pgno;
5750 int rc;
5751
5752 if (!F_ISSET(leaf->mn_flags, F_BIGDATA)) {
5753 data->mv_size = NODEDSZ(leaf);
5754 data->mv_data = NODEDATA(leaf);
5755 return MDB_SUCCESS;
5756 }
5757
5758 /* Read overflow data.
5759 */
5760 data->mv_size = NODEDSZ(leaf);
5761 memcpy(&pgno, NODEDATA(leaf), sizeof(pgno));
5762 if ((rc = mdb_page_get(mc, pgno, &omp, NULL)) != 0) {
5763 DPRINTF(("read overflow page %"Z"u failed", pgno));
5764 return rc;
5765 }
5766 data->mv_data = METADATA(omp);
5767
5768 return MDB_SUCCESS;
5769 }
5770
5771 int
mdb_get(MDB_txn * txn,MDB_dbi dbi,MDB_val * key,MDB_val * data)5772 mdb_get(MDB_txn *txn, MDB_dbi dbi,
5773 MDB_val *key, MDB_val *data)
5774 {
5775 MDB_cursor mc;
5776 MDB_xcursor mx;
5777 int exact = 0;
5778 DKBUF;
5779
5780 DPRINTF(("===> get db %u key [%s]", dbi, DKEY(key)));
5781
5782 if (!key || !data || !TXN_DBI_EXIST(txn, dbi, DB_USRVALID))
5783 return EINVAL;
5784
5785 if (txn->mt_flags & MDB_TXN_BLOCKED)
5786 return MDB_BAD_TXN;
5787
5788 mdb_cursor_init(&mc, txn, dbi, &mx);
5789 return mdb_cursor_set(&mc, key, data, MDB_SET, &exact);
5790 }
5791
5792 /** Find a sibling for a page.
5793 * Replaces the page at the top of the cursor's stack with the
5794 * specified sibling, if one exists.
5795 * @param[in] mc The cursor for this operation.
5796 * @param[in] move_right Non-zero if the right sibling is requested,
5797 * otherwise the left sibling.
5798 * @return 0 on success, non-zero on failure.
5799 */
5800 static int
mdb_cursor_sibling(MDB_cursor * mc,int move_right)5801 mdb_cursor_sibling(MDB_cursor *mc, int move_right)
5802 {
5803 int rc;
5804 MDB_node *indx;
5805 MDB_page *mp;
5806
5807 if (mc->mc_snum < 2) {
5808 return MDB_NOTFOUND; /* root has no siblings */
5809 }
5810
5811 mdb_cursor_pop(mc);
5812 DPRINTF(("parent page is page %"Z"u, index %u",
5813 mc->mc_pg[mc->mc_top]->mp_pgno, mc->mc_ki[mc->mc_top]));
5814
5815 if (move_right ? (mc->mc_ki[mc->mc_top] + 1u >= NUMKEYS(mc->mc_pg[mc->mc_top]))
5816 : (mc->mc_ki[mc->mc_top] == 0)) {
5817 DPRINTF(("no more keys left, moving to %s sibling",
5818 move_right ? "right" : "left"));
5819 if ((rc = mdb_cursor_sibling(mc, move_right)) != MDB_SUCCESS) {
5820 /* undo cursor_pop before returning */
5821 mc->mc_top++;
5822 mc->mc_snum++;
5823 return rc;
5824 }
5825 } else {
5826 if (move_right)
5827 mc->mc_ki[mc->mc_top]++;
5828 else
5829 mc->mc_ki[mc->mc_top]--;
5830 DPRINTF(("just moving to %s index key %u",
5831 move_right ? "right" : "left", mc->mc_ki[mc->mc_top]));
5832 }
5833 mdb_cassert(mc, IS_BRANCH(mc->mc_pg[mc->mc_top]));
5834
5835 indx = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
5836 if ((rc = mdb_page_get(mc, NODEPGNO(indx), &mp, NULL)) != 0) {
5837 /* mc will be inconsistent if caller does mc_snum++ as above */
5838 mc->mc_flags &= ~(C_INITIALIZED|C_EOF);
5839 return rc;
5840 }
5841
5842 mdb_cursor_push(mc, mp);
5843 if (!move_right)
5844 mc->mc_ki[mc->mc_top] = NUMKEYS(mp)-1;
5845
5846 return MDB_SUCCESS;
5847 }
5848
5849 /** Move the cursor to the next data item. */
5850 static int
mdb_cursor_next(MDB_cursor * mc,MDB_val * key,MDB_val * data,MDB_cursor_op op)5851 mdb_cursor_next(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
5852 {
5853 MDB_page *mp;
5854 MDB_node *leaf;
5855 int rc;
5856
5857 if ((mc->mc_flags & C_DEL && op == MDB_NEXT_DUP))
5858 return MDB_NOTFOUND;
5859
5860 if (!(mc->mc_flags & C_INITIALIZED))
5861 return mdb_cursor_first(mc, key, data);
5862
5863 mp = mc->mc_pg[mc->mc_top];
5864
5865 if (mc->mc_flags & C_EOF) {
5866 if (mc->mc_ki[mc->mc_top] >= NUMKEYS(mp)-1)
5867 return MDB_NOTFOUND;
5868 mc->mc_flags ^= C_EOF;
5869 }
5870
5871 if (mc->mc_db->md_flags & MDB_DUPSORT) {
5872 leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
5873 if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
5874 if (op == MDB_NEXT || op == MDB_NEXT_DUP) {
5875 rc = mdb_cursor_next(&mc->mc_xcursor->mx_cursor, data, NULL, MDB_NEXT);
5876 if (op != MDB_NEXT || rc != MDB_NOTFOUND) {
5877 if (rc == MDB_SUCCESS)
5878 MDB_GET_KEY(leaf, key);
5879 return rc;
5880 }
5881 }
5882 } else {
5883 mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
5884 if (op == MDB_NEXT_DUP)
5885 return MDB_NOTFOUND;
5886 }
5887 }
5888
5889 DPRINTF(("cursor_next: top page is %"Z"u in cursor %p",
5890 mdb_dbg_pgno(mp), (void *) mc));
5891 if (mc->mc_flags & C_DEL) {
5892 mc->mc_flags ^= C_DEL;
5893 goto skip;
5894 }
5895
5896 if (mc->mc_ki[mc->mc_top] + 1u >= NUMKEYS(mp)) {
5897 DPUTS("=====> move to next sibling page");
5898 if ((rc = mdb_cursor_sibling(mc, 1)) != MDB_SUCCESS) {
5899 mc->mc_flags |= C_EOF;
5900 return rc;
5901 }
5902 mp = mc->mc_pg[mc->mc_top];
5903 DPRINTF(("next page is %"Z"u, key index %u", mp->mp_pgno, mc->mc_ki[mc->mc_top]));
5904 } else
5905 mc->mc_ki[mc->mc_top]++;
5906
5907 skip:
5908 DPRINTF(("==> cursor points to page %"Z"u with %u keys, key index %u",
5909 mdb_dbg_pgno(mp), NUMKEYS(mp), mc->mc_ki[mc->mc_top]));
5910
5911 if (IS_LEAF2(mp)) {
5912 key->mv_size = mc->mc_db->md_pad;
5913 key->mv_data = LEAF2KEY(mp, mc->mc_ki[mc->mc_top], key->mv_size);
5914 return MDB_SUCCESS;
5915 }
5916
5917 mdb_cassert(mc, IS_LEAF(mp));
5918 leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
5919
5920 if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
5921 mdb_xcursor_init1(mc, leaf);
5922 }
5923 if (data) {
5924 if ((rc = mdb_node_read(mc, leaf, data)) != MDB_SUCCESS)
5925 return rc;
5926
5927 if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
5928 rc = mdb_cursor_first(&mc->mc_xcursor->mx_cursor, data, NULL);
5929 if (rc != MDB_SUCCESS)
5930 return rc;
5931 }
5932 }
5933
5934 MDB_GET_KEY(leaf, key);
5935 return MDB_SUCCESS;
5936 }
5937
5938 /** Move the cursor to the previous data item. */
5939 static int
mdb_cursor_prev(MDB_cursor * mc,MDB_val * key,MDB_val * data,MDB_cursor_op op)5940 mdb_cursor_prev(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
5941 {
5942 MDB_page *mp;
5943 MDB_node *leaf;
5944 int rc;
5945
5946 if (!(mc->mc_flags & C_INITIALIZED)) {
5947 rc = mdb_cursor_last(mc, key, data);
5948 if (rc)
5949 return rc;
5950 mc->mc_ki[mc->mc_top]++;
5951 }
5952
5953 mp = mc->mc_pg[mc->mc_top];
5954
5955 if (mc->mc_db->md_flags & MDB_DUPSORT) {
5956 leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
5957 if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
5958 if (op == MDB_PREV || op == MDB_PREV_DUP) {
5959 rc = mdb_cursor_prev(&mc->mc_xcursor->mx_cursor, data, NULL, MDB_PREV);
5960 if (op != MDB_PREV || rc != MDB_NOTFOUND) {
5961 if (rc == MDB_SUCCESS) {
5962 MDB_GET_KEY(leaf, key);
5963 mc->mc_flags &= ~C_EOF;
5964 }
5965 return rc;
5966 }
5967 }
5968 } else {
5969 mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
5970 if (op == MDB_PREV_DUP)
5971 return MDB_NOTFOUND;
5972 }
5973 }
5974
5975 DPRINTF(("cursor_prev: top page is %"Z"u in cursor %p",
5976 mdb_dbg_pgno(mp), (void *) mc));
5977
5978 mc->mc_flags &= ~(C_EOF|C_DEL);
5979
5980 if (mc->mc_ki[mc->mc_top] == 0) {
5981 DPUTS("=====> move to prev sibling page");
5982 if ((rc = mdb_cursor_sibling(mc, 0)) != MDB_SUCCESS) {
5983 return rc;
5984 }
5985 mp = mc->mc_pg[mc->mc_top];
5986 mc->mc_ki[mc->mc_top] = NUMKEYS(mp) - 1;
5987 DPRINTF(("prev page is %"Z"u, key index %u", mp->mp_pgno, mc->mc_ki[mc->mc_top]));
5988 } else
5989 mc->mc_ki[mc->mc_top]--;
5990
5991 DPRINTF(("==> cursor points to page %"Z"u with %u keys, key index %u",
5992 mdb_dbg_pgno(mp), NUMKEYS(mp), mc->mc_ki[mc->mc_top]));
5993
5994 if (IS_LEAF2(mp)) {
5995 key->mv_size = mc->mc_db->md_pad;
5996 key->mv_data = LEAF2KEY(mp, mc->mc_ki[mc->mc_top], key->mv_size);
5997 return MDB_SUCCESS;
5998 }
5999
6000 mdb_cassert(mc, IS_LEAF(mp));
6001 leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
6002
6003 if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
6004 mdb_xcursor_init1(mc, leaf);
6005 }
6006 if (data) {
6007 if ((rc = mdb_node_read(mc, leaf, data)) != MDB_SUCCESS)
6008 return rc;
6009
6010 if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
6011 rc = mdb_cursor_last(&mc->mc_xcursor->mx_cursor, data, NULL);
6012 if (rc != MDB_SUCCESS)
6013 return rc;
6014 }
6015 }
6016
6017 MDB_GET_KEY(leaf, key);
6018 return MDB_SUCCESS;
6019 }
6020
6021 /** Set the cursor on a specific data item. */
6022 static int
mdb_cursor_set(MDB_cursor * mc,MDB_val * key,MDB_val * data,MDB_cursor_op op,int * exactp)6023 mdb_cursor_set(MDB_cursor *mc, MDB_val *key, MDB_val *data,
6024 MDB_cursor_op op, int *exactp)
6025 {
6026 int rc;
6027 MDB_page *mp;
6028 MDB_node *leaf = NULL;
6029 DKBUF;
6030
6031 if (key->mv_size == 0)
6032 return MDB_BAD_VALSIZE;
6033
6034 if (mc->mc_xcursor)
6035 mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
6036
6037 /* See if we're already on the right page */
6038 if (mc->mc_flags & C_INITIALIZED) {
6039 MDB_val nodekey;
6040
6041 mp = mc->mc_pg[mc->mc_top];
6042 if (!NUMKEYS(mp)) {
6043 mc->mc_ki[mc->mc_top] = 0;
6044 return MDB_NOTFOUND;
6045 }
6046 if (mp->mp_flags & P_LEAF2) {
6047 nodekey.mv_size = mc->mc_db->md_pad;
6048 nodekey.mv_data = LEAF2KEY(mp, 0, nodekey.mv_size);
6049 } else {
6050 leaf = NODEPTR(mp, 0);
6051 MDB_GET_KEY2(leaf, nodekey);
6052 }
6053 rc = mc->mc_dbx->md_cmp(key, &nodekey);
6054 if (rc == 0) {
6055 /* Probably happens rarely, but first node on the page
6056 * was the one we wanted.
6057 */
6058 mc->mc_ki[mc->mc_top] = 0;
6059 if (exactp)
6060 *exactp = 1;
6061 goto set1;
6062 }
6063 if (rc > 0) {
6064 unsigned int i;
6065 unsigned int nkeys = NUMKEYS(mp);
6066 if (nkeys > 1) {
6067 if (mp->mp_flags & P_LEAF2) {
6068 nodekey.mv_data = LEAF2KEY(mp,
6069 nkeys-1, nodekey.mv_size);
6070 } else {
6071 leaf = NODEPTR(mp, nkeys-1);
6072 MDB_GET_KEY2(leaf, nodekey);
6073 }
6074 rc = mc->mc_dbx->md_cmp(key, &nodekey);
6075 if (rc == 0) {
6076 /* last node was the one we wanted */
6077 mc->mc_ki[mc->mc_top] = nkeys-1;
6078 if (exactp)
6079 *exactp = 1;
6080 goto set1;
6081 }
6082 if (rc < 0) {
6083 if (mc->mc_ki[mc->mc_top] < NUMKEYS(mp)) {
6084 /* This is definitely the right page, skip search_page */
6085 if (mp->mp_flags & P_LEAF2) {
6086 nodekey.mv_data = LEAF2KEY(mp,
6087 mc->mc_ki[mc->mc_top], nodekey.mv_size);
6088 } else {
6089 leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
6090 MDB_GET_KEY2(leaf, nodekey);
6091 }
6092 rc = mc->mc_dbx->md_cmp(key, &nodekey);
6093 if (rc == 0) {
6094 /* current node was the one we wanted */
6095 if (exactp)
6096 *exactp = 1;
6097 goto set1;
6098 }
6099 }
6100 rc = 0;
6101 mc->mc_flags &= ~C_EOF;
6102 goto set2;
6103 }
6104 }
6105 /* If any parents have right-sibs, search.
6106 * Otherwise, there's nothing further.
6107 */
6108 for (i=0; i<mc->mc_top; i++)
6109 if (mc->mc_ki[i] <
6110 NUMKEYS(mc->mc_pg[i])-1)
6111 break;
6112 if (i == mc->mc_top) {
6113 /* There are no other pages */
6114 mc->mc_ki[mc->mc_top] = nkeys;
6115 return MDB_NOTFOUND;
6116 }
6117 }
6118 if (!mc->mc_top) {
6119 /* There are no other pages */
6120 mc->mc_ki[mc->mc_top] = 0;
6121 if (op == MDB_SET_RANGE && !exactp) {
6122 rc = 0;
6123 goto set1;
6124 } else
6125 return MDB_NOTFOUND;
6126 }
6127 } else {
6128 mc->mc_pg[0] = 0;
6129 }
6130
6131 rc = mdb_page_search(mc, key, 0);
6132 if (rc != MDB_SUCCESS)
6133 return rc;
6134
6135 mp = mc->mc_pg[mc->mc_top];
6136 mdb_cassert(mc, IS_LEAF(mp));
6137
6138 set2:
6139 leaf = mdb_node_search(mc, key, exactp);
6140 if (exactp != NULL && !*exactp) {
6141 /* MDB_SET specified and not an exact match. */
6142 return MDB_NOTFOUND;
6143 }
6144
6145 if (leaf == NULL) {
6146 DPUTS("===> inexact leaf not found, goto sibling");
6147 if ((rc = mdb_cursor_sibling(mc, 1)) != MDB_SUCCESS) {
6148 mc->mc_flags |= C_EOF;
6149 return rc; /* no entries matched */
6150 }
6151 mp = mc->mc_pg[mc->mc_top];
6152 mdb_cassert(mc, IS_LEAF(mp));
6153 leaf = NODEPTR(mp, 0);
6154 }
6155
6156 set1:
6157 mc->mc_flags |= C_INITIALIZED;
6158 mc->mc_flags &= ~C_EOF;
6159
6160 if (IS_LEAF2(mp)) {
6161 if (op == MDB_SET_RANGE || op == MDB_SET_KEY) {
6162 key->mv_size = mc->mc_db->md_pad;
6163 key->mv_data = LEAF2KEY(mp, mc->mc_ki[mc->mc_top], key->mv_size);
6164 }
6165 return MDB_SUCCESS;
6166 }
6167
6168 if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
6169 mdb_xcursor_init1(mc, leaf);
6170 }
6171 if (data) {
6172 if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
6173 if (op == MDB_SET || op == MDB_SET_KEY || op == MDB_SET_RANGE) {
6174 rc = mdb_cursor_first(&mc->mc_xcursor->mx_cursor, data, NULL);
6175 } else {
6176 int ex2, *ex2p;
6177 if (op == MDB_GET_BOTH) {
6178 ex2p = &ex2;
6179 ex2 = 0;
6180 } else {
6181 ex2p = NULL;
6182 }
6183 rc = mdb_cursor_set(&mc->mc_xcursor->mx_cursor, data, NULL, MDB_SET_RANGE, ex2p);
6184 if (rc != MDB_SUCCESS)
6185 return rc;
6186 }
6187 } else if (op == MDB_GET_BOTH || op == MDB_GET_BOTH_RANGE) {
6188 MDB_val olddata;
6189 MDB_cmp_func *dcmp;
6190 if ((rc = mdb_node_read(mc, leaf, &olddata)) != MDB_SUCCESS)
6191 return rc;
6192 dcmp = mc->mc_dbx->md_dcmp;
6193 #if UINT_MAX < SIZE_MAX
6194 if (dcmp == mdb_cmp_int && olddata.mv_size == sizeof(size_t))
6195 dcmp = mdb_cmp_clong;
6196 #endif
6197 rc = dcmp(data, &olddata);
6198 if (rc) {
6199 if (op == MDB_GET_BOTH || rc > 0)
6200 return MDB_NOTFOUND;
6201 rc = 0;
6202 }
6203 *data = olddata;
6204
6205 } else {
6206 if (mc->mc_xcursor)
6207 mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
6208 if ((rc = mdb_node_read(mc, leaf, data)) != MDB_SUCCESS)
6209 return rc;
6210 }
6211 }
6212
6213 /* The key already matches in all other cases */
6214 if (op == MDB_SET_RANGE || op == MDB_SET_KEY)
6215 MDB_GET_KEY(leaf, key);
6216 DPRINTF(("==> cursor placed on key [%s]", DKEY(key)));
6217
6218 return rc;
6219 }
6220
6221 /** Move the cursor to the first item in the database. */
6222 static int
mdb_cursor_first(MDB_cursor * mc,MDB_val * key,MDB_val * data)6223 mdb_cursor_first(MDB_cursor *mc, MDB_val *key, MDB_val *data)
6224 {
6225 int rc;
6226 MDB_node *leaf;
6227
6228 if (mc->mc_xcursor)
6229 mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
6230
6231 if (!(mc->mc_flags & C_INITIALIZED) || mc->mc_top) {
6232 rc = mdb_page_search(mc, NULL, MDB_PS_FIRST);
6233 if (rc != MDB_SUCCESS)
6234 return rc;
6235 }
6236 mdb_cassert(mc, IS_LEAF(mc->mc_pg[mc->mc_top]));
6237
6238 leaf = NODEPTR(mc->mc_pg[mc->mc_top], 0);
6239 mc->mc_flags |= C_INITIALIZED;
6240 mc->mc_flags &= ~C_EOF;
6241
6242 mc->mc_ki[mc->mc_top] = 0;
6243
6244 if (IS_LEAF2(mc->mc_pg[mc->mc_top])) {
6245 key->mv_size = mc->mc_db->md_pad;
6246 key->mv_data = LEAF2KEY(mc->mc_pg[mc->mc_top], 0, key->mv_size);
6247 return MDB_SUCCESS;
6248 }
6249
6250 if (data) {
6251 if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
6252 mdb_xcursor_init1(mc, leaf);
6253 rc = mdb_cursor_first(&mc->mc_xcursor->mx_cursor, data, NULL);
6254 if (rc)
6255 return rc;
6256 } else {
6257 if ((rc = mdb_node_read(mc, leaf, data)) != MDB_SUCCESS)
6258 return rc;
6259 }
6260 }
6261 MDB_GET_KEY(leaf, key);
6262 return MDB_SUCCESS;
6263 }
6264
6265 /** Move the cursor to the last item in the database. */
6266 static int
mdb_cursor_last(MDB_cursor * mc,MDB_val * key,MDB_val * data)6267 mdb_cursor_last(MDB_cursor *mc, MDB_val *key, MDB_val *data)
6268 {
6269 int rc;
6270 MDB_node *leaf;
6271
6272 if (mc->mc_xcursor)
6273 mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
6274
6275 if (!(mc->mc_flags & C_INITIALIZED) || mc->mc_top) {
6276 rc = mdb_page_search(mc, NULL, MDB_PS_LAST);
6277 if (rc != MDB_SUCCESS)
6278 return rc;
6279 }
6280 mdb_cassert(mc, IS_LEAF(mc->mc_pg[mc->mc_top]));
6281
6282 mc->mc_ki[mc->mc_top] = NUMKEYS(mc->mc_pg[mc->mc_top]) - 1;
6283 mc->mc_flags |= C_INITIALIZED|C_EOF;
6284 leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
6285
6286 if (IS_LEAF2(mc->mc_pg[mc->mc_top])) {
6287 key->mv_size = mc->mc_db->md_pad;
6288 key->mv_data = LEAF2KEY(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top], key->mv_size);
6289 return MDB_SUCCESS;
6290 }
6291
6292 if (data) {
6293 if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
6294 mdb_xcursor_init1(mc, leaf);
6295 rc = mdb_cursor_last(&mc->mc_xcursor->mx_cursor, data, NULL);
6296 if (rc)
6297 return rc;
6298 } else {
6299 if ((rc = mdb_node_read(mc, leaf, data)) != MDB_SUCCESS)
6300 return rc;
6301 }
6302 }
6303
6304 MDB_GET_KEY(leaf, key);
6305 return MDB_SUCCESS;
6306 }
6307
6308 int
mdb_cursor_get(MDB_cursor * mc,MDB_val * key,MDB_val * data,MDB_cursor_op op)6309 mdb_cursor_get(MDB_cursor *mc, MDB_val *key, MDB_val *data,
6310 MDB_cursor_op op)
6311 {
6312 int rc;
6313 int exact = 0;
6314 int (*mfunc)(MDB_cursor *mc, MDB_val *key, MDB_val *data);
6315
6316 if (mc == NULL)
6317 return EINVAL;
6318
6319 if (mc->mc_txn->mt_flags & MDB_TXN_BLOCKED)
6320 return MDB_BAD_TXN;
6321
6322 switch (op) {
6323 case MDB_GET_CURRENT:
6324 if (!(mc->mc_flags & C_INITIALIZED)) {
6325 rc = EINVAL;
6326 } else {
6327 MDB_page *mp = mc->mc_pg[mc->mc_top];
6328 int nkeys = NUMKEYS(mp);
6329 if (!nkeys || mc->mc_ki[mc->mc_top] >= nkeys) {
6330 mc->mc_ki[mc->mc_top] = nkeys;
6331 rc = MDB_NOTFOUND;
6332 break;
6333 }
6334 rc = MDB_SUCCESS;
6335 if (IS_LEAF2(mp)) {
6336 key->mv_size = mc->mc_db->md_pad;
6337 key->mv_data = LEAF2KEY(mp, mc->mc_ki[mc->mc_top], key->mv_size);
6338 } else {
6339 MDB_node *leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
6340 MDB_GET_KEY(leaf, key);
6341 if (data) {
6342 if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
6343 rc = mdb_cursor_get(&mc->mc_xcursor->mx_cursor, data, NULL, MDB_GET_CURRENT);
6344 } else {
6345 rc = mdb_node_read(mc, leaf, data);
6346 }
6347 }
6348 }
6349 }
6350 break;
6351 case MDB_GET_BOTH:
6352 case MDB_GET_BOTH_RANGE:
6353 if (data == NULL) {
6354 rc = EINVAL;
6355 break;
6356 }
6357 if (mc->mc_xcursor == NULL) {
6358 rc = MDB_INCOMPATIBLE;
6359 break;
6360 }
6361 /* FALLTHRU */
6362 case MDB_SET:
6363 case MDB_SET_KEY:
6364 case MDB_SET_RANGE:
6365 if (key == NULL) {
6366 rc = EINVAL;
6367 } else {
6368 rc = mdb_cursor_set(mc, key, data, op,
6369 op == MDB_SET_RANGE ? NULL : &exact);
6370 }
6371 break;
6372 case MDB_GET_MULTIPLE:
6373 if (data == NULL || !(mc->mc_flags & C_INITIALIZED)) {
6374 rc = EINVAL;
6375 break;
6376 }
6377 if (!(mc->mc_db->md_flags & MDB_DUPFIXED)) {
6378 rc = MDB_INCOMPATIBLE;
6379 break;
6380 }
6381 rc = MDB_SUCCESS;
6382 if (!(mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) ||
6383 (mc->mc_xcursor->mx_cursor.mc_flags & C_EOF))
6384 break;
6385 goto fetchm;
6386 case MDB_NEXT_MULTIPLE:
6387 if (data == NULL) {
6388 rc = EINVAL;
6389 break;
6390 }
6391 if (!(mc->mc_db->md_flags & MDB_DUPFIXED)) {
6392 rc = MDB_INCOMPATIBLE;
6393 break;
6394 }
6395 rc = mdb_cursor_next(mc, key, data, MDB_NEXT_DUP);
6396 if (rc == MDB_SUCCESS) {
6397 if (mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) {
6398 MDB_cursor *mx;
6399 fetchm:
6400 mx = &mc->mc_xcursor->mx_cursor;
6401 data->mv_size = NUMKEYS(mx->mc_pg[mx->mc_top]) *
6402 mx->mc_db->md_pad;
6403 data->mv_data = METADATA(mx->mc_pg[mx->mc_top]);
6404 mx->mc_ki[mx->mc_top] = NUMKEYS(mx->mc_pg[mx->mc_top])-1;
6405 } else {
6406 rc = MDB_NOTFOUND;
6407 }
6408 }
6409 break;
6410 case MDB_PREV_MULTIPLE:
6411 if (data == NULL) {
6412 rc = EINVAL;
6413 break;
6414 }
6415 if (!(mc->mc_db->md_flags & MDB_DUPFIXED)) {
6416 rc = MDB_INCOMPATIBLE;
6417 break;
6418 }
6419 if (!(mc->mc_flags & C_INITIALIZED))
6420 rc = mdb_cursor_last(mc, key, data);
6421 else
6422 rc = MDB_SUCCESS;
6423 if (rc == MDB_SUCCESS) {
6424 MDB_cursor *mx = &mc->mc_xcursor->mx_cursor;
6425 if (mx->mc_flags & C_INITIALIZED) {
6426 rc = mdb_cursor_sibling(mx, 0);
6427 if (rc == MDB_SUCCESS)
6428 goto fetchm;
6429 } else {
6430 rc = MDB_NOTFOUND;
6431 }
6432 }
6433 break;
6434 case MDB_NEXT:
6435 case MDB_NEXT_DUP:
6436 case MDB_NEXT_NODUP:
6437 rc = mdb_cursor_next(mc, key, data, op);
6438 break;
6439 case MDB_PREV:
6440 case MDB_PREV_DUP:
6441 case MDB_PREV_NODUP:
6442 rc = mdb_cursor_prev(mc, key, data, op);
6443 break;
6444 case MDB_FIRST:
6445 rc = mdb_cursor_first(mc, key, data);
6446 break;
6447 case MDB_FIRST_DUP:
6448 mfunc = mdb_cursor_first;
6449 mmove:
6450 if (data == NULL || !(mc->mc_flags & C_INITIALIZED)) {
6451 rc = EINVAL;
6452 break;
6453 }
6454 if (mc->mc_xcursor == NULL) {
6455 rc = MDB_INCOMPATIBLE;
6456 break;
6457 }
6458 if (mc->mc_ki[mc->mc_top] >= NUMKEYS(mc->mc_pg[mc->mc_top])) {
6459 mc->mc_ki[mc->mc_top] = NUMKEYS(mc->mc_pg[mc->mc_top]);
6460 rc = MDB_NOTFOUND;
6461 break;
6462 }
6463 {
6464 MDB_node *leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
6465 if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
6466 MDB_GET_KEY(leaf, key);
6467 rc = mdb_node_read(mc, leaf, data);
6468 break;
6469 }
6470 }
6471 if (!(mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)) {
6472 rc = EINVAL;
6473 break;
6474 }
6475 rc = mfunc(&mc->mc_xcursor->mx_cursor, data, NULL);
6476 break;
6477 case MDB_LAST:
6478 rc = mdb_cursor_last(mc, key, data);
6479 break;
6480 case MDB_LAST_DUP:
6481 mfunc = mdb_cursor_last;
6482 goto mmove;
6483 default:
6484 DPRINTF(("unhandled/unimplemented cursor operation %u", op));
6485 rc = EINVAL;
6486 break;
6487 }
6488
6489 if (mc->mc_flags & C_DEL)
6490 mc->mc_flags ^= C_DEL;
6491
6492 return rc;
6493 }
6494
6495 /** Touch all the pages in the cursor stack. Set mc_top.
6496 * Makes sure all the pages are writable, before attempting a write operation.
6497 * @param[in] mc The cursor to operate on.
6498 */
6499 static int
mdb_cursor_touch(MDB_cursor * mc)6500 mdb_cursor_touch(MDB_cursor *mc)
6501 {
6502 int rc = MDB_SUCCESS;
6503
6504 if (mc->mc_dbi >= CORE_DBS && !(*mc->mc_dbflag & (DB_DIRTY|DB_DUPDATA))) {
6505 /* Touch DB record of named DB */
6506 MDB_cursor mc2;
6507 MDB_xcursor mcx;
6508 if (TXN_DBI_CHANGED(mc->mc_txn, mc->mc_dbi))
6509 return MDB_BAD_DBI;
6510 mdb_cursor_init(&mc2, mc->mc_txn, MAIN_DBI, &mcx);
6511 rc = mdb_page_search(&mc2, &mc->mc_dbx->md_name, MDB_PS_MODIFY);
6512 if (rc)
6513 return rc;
6514 *mc->mc_dbflag |= DB_DIRTY;
6515 }
6516 mc->mc_top = 0;
6517 if (mc->mc_snum) {
6518 do {
6519 rc = mdb_page_touch(mc);
6520 } while (!rc && ++(mc->mc_top) < mc->mc_snum);
6521 mc->mc_top = mc->mc_snum-1;
6522 }
6523 return rc;
6524 }
6525
6526 /** Do not spill pages to disk if txn is getting full, may fail instead */
6527 #define MDB_NOSPILL 0x8000
6528
6529 int
mdb_cursor_put(MDB_cursor * mc,MDB_val * key,MDB_val * data,unsigned int flags)6530 mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
6531 unsigned int flags)
6532 {
6533 MDB_env *env;
6534 MDB_node *leaf = NULL;
6535 MDB_page *fp, *mp, *sub_root = NULL;
6536 uint16_t fp_flags;
6537 MDB_val xdata, *rdata, dkey, olddata;
6538 MDB_db dummy;
6539 int do_sub = 0, insert_key, insert_data;
6540 unsigned int mcount = 0, dcount = 0, nospill;
6541 size_t nsize;
6542 int rc, rc2;
6543 unsigned int nflags;
6544 DKBUF;
6545
6546 if (mc == NULL || key == NULL)
6547 return EINVAL;
6548
6549 env = mc->mc_txn->mt_env;
6550
6551 /* Check this first so counter will always be zero on any
6552 * early failures.
6553 */
6554 if (flags & MDB_MULTIPLE) {
6555 dcount = data[1].mv_size;
6556 data[1].mv_size = 0;
6557 if (!F_ISSET(mc->mc_db->md_flags, MDB_DUPFIXED))
6558 return MDB_INCOMPATIBLE;
6559 }
6560
6561 nospill = flags & MDB_NOSPILL;
6562 flags &= ~MDB_NOSPILL;
6563
6564 if (mc->mc_txn->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_BLOCKED))
6565 return (mc->mc_txn->mt_flags & MDB_TXN_RDONLY) ? EACCES : MDB_BAD_TXN;
6566
6567 if (key->mv_size-1 >= ENV_MAXKEY(env))
6568 return MDB_BAD_VALSIZE;
6569
6570 #if SIZE_MAX > MAXDATASIZE
6571 if (data->mv_size > ((mc->mc_db->md_flags & MDB_DUPSORT) ? ENV_MAXKEY(env) : MAXDATASIZE))
6572 return MDB_BAD_VALSIZE;
6573 #else
6574 if ((mc->mc_db->md_flags & MDB_DUPSORT) && data->mv_size > ENV_MAXKEY(env))
6575 return MDB_BAD_VALSIZE;
6576 #endif
6577
6578 DPRINTF(("==> put db %d key [%s], size %"Z"u, data size %"Z"u",
6579 DDBI(mc), DKEY(key), key ? key->mv_size : 0, data->mv_size));
6580
6581 dkey.mv_size = 0;
6582
6583 if (flags == MDB_CURRENT) {
6584 if (!(mc->mc_flags & C_INITIALIZED))
6585 return EINVAL;
6586 rc = MDB_SUCCESS;
6587 } else if (mc->mc_db->md_root == P_INVALID) {
6588 /* new database, cursor has nothing to point to */
6589 mc->mc_snum = 0;
6590 mc->mc_top = 0;
6591 mc->mc_flags &= ~C_INITIALIZED;
6592 rc = MDB_NO_ROOT;
6593 } else {
6594 int exact = 0;
6595 MDB_val d2;
6596 if (flags & MDB_APPEND) {
6597 MDB_val k2;
6598 rc = mdb_cursor_last(mc, &k2, &d2);
6599 if (rc == 0) {
6600 rc = mc->mc_dbx->md_cmp(key, &k2);
6601 if (rc > 0) {
6602 rc = MDB_NOTFOUND;
6603 mc->mc_ki[mc->mc_top]++;
6604 } else {
6605 /* new key is <= last key */
6606 rc = MDB_KEYEXIST;
6607 }
6608 }
6609 } else {
6610 rc = mdb_cursor_set(mc, key, &d2, MDB_SET, &exact);
6611 }
6612 if ((flags & MDB_NOOVERWRITE) && rc == 0) {
6613 DPRINTF(("duplicate key [%s]", DKEY(key)));
6614 *data = d2;
6615 return MDB_KEYEXIST;
6616 }
6617 if (rc && rc != MDB_NOTFOUND)
6618 return rc;
6619 }
6620
6621 if (mc->mc_flags & C_DEL)
6622 mc->mc_flags ^= C_DEL;
6623
6624 /* Cursor is positioned, check for room in the dirty list */
6625 if (!nospill) {
6626 if (flags & MDB_MULTIPLE) {
6627 rdata = &xdata;
6628 xdata.mv_size = data->mv_size * dcount;
6629 } else {
6630 rdata = data;
6631 }
6632 if ((rc2 = mdb_page_spill(mc, key, rdata)))
6633 return rc2;
6634 }
6635
6636 if (rc == MDB_NO_ROOT) {
6637 MDB_page *np;
6638 /* new database, write a root leaf page */
6639 DPUTS("allocating new root leaf page");
6640 if ((rc2 = mdb_page_new(mc, P_LEAF, 1, &np))) {
6641 return rc2;
6642 }
6643 mdb_cursor_push(mc, np);
6644 mc->mc_db->md_root = np->mp_pgno;
6645 mc->mc_db->md_depth++;
6646 *mc->mc_dbflag |= DB_DIRTY;
6647 if ((mc->mc_db->md_flags & (MDB_DUPSORT|MDB_DUPFIXED))
6648 == MDB_DUPFIXED)
6649 np->mp_flags |= P_LEAF2;
6650 mc->mc_flags |= C_INITIALIZED;
6651 } else {
6652 /* make sure all cursor pages are writable */
6653 rc2 = mdb_cursor_touch(mc);
6654 if (rc2)
6655 return rc2;
6656 }
6657
6658 insert_key = insert_data = rc;
6659 if (insert_key) {
6660 /* The key does not exist */
6661 DPRINTF(("inserting key at index %i", mc->mc_ki[mc->mc_top]));
6662 if ((mc->mc_db->md_flags & MDB_DUPSORT) &&
6663 LEAFSIZE(key, data) > env->me_nodemax)
6664 {
6665 /* Too big for a node, insert in sub-DB. Set up an empty
6666 * "old sub-page" for prep_subDB to expand to a full page.
6667 */
6668 fp_flags = P_LEAF|P_DIRTY;
6669 fp = env->me_pbuf;
6670 fp->mp_pad = data->mv_size; /* used if MDB_DUPFIXED */
6671 fp->mp_lower = fp->mp_upper = (PAGEHDRSZ-PAGEBASE);
6672 olddata.mv_size = PAGEHDRSZ;
6673 goto prep_subDB;
6674 }
6675 } else {
6676 /* there's only a key anyway, so this is a no-op */
6677 if (IS_LEAF2(mc->mc_pg[mc->mc_top])) {
6678 char *ptr;
6679 unsigned int ksize = mc->mc_db->md_pad;
6680 if (key->mv_size != ksize)
6681 return MDB_BAD_VALSIZE;
6682 ptr = LEAF2KEY(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top], ksize);
6683 memcpy(ptr, key->mv_data, ksize);
6684 fix_parent:
6685 /* if overwriting slot 0 of leaf, need to
6686 * update branch key if there is a parent page
6687 */
6688 if (mc->mc_top && !mc->mc_ki[mc->mc_top]) {
6689 unsigned short dtop = 1;
6690 mc->mc_top--;
6691 /* slot 0 is always an empty key, find real slot */
6692 while (mc->mc_top && !mc->mc_ki[mc->mc_top]) {
6693 mc->mc_top--;
6694 dtop++;
6695 }
6696 if (mc->mc_ki[mc->mc_top])
6697 rc2 = mdb_update_key(mc, key);
6698 else
6699 rc2 = MDB_SUCCESS;
6700 mc->mc_top += dtop;
6701 if (rc2)
6702 return rc2;
6703 }
6704 return MDB_SUCCESS;
6705 }
6706
6707 more:
6708 leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
6709 olddata.mv_size = NODEDSZ(leaf);
6710 olddata.mv_data = NODEDATA(leaf);
6711
6712 /* DB has dups? */
6713 if (F_ISSET(mc->mc_db->md_flags, MDB_DUPSORT)) {
6714 /* Prepare (sub-)page/sub-DB to accept the new item,
6715 * if needed. fp: old sub-page or a header faking
6716 * it. mp: new (sub-)page. offset: growth in page
6717 * size. xdata: node data with new page or DB.
6718 */
6719 unsigned i, offset = 0;
6720 mp = fp = xdata.mv_data = env->me_pbuf;
6721 mp->mp_pgno = mc->mc_pg[mc->mc_top]->mp_pgno;
6722
6723 /* Was a single item before, must convert now */
6724 if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
6725 MDB_cmp_func *dcmp;
6726 /* Just overwrite the current item */
6727 if (flags == MDB_CURRENT)
6728 goto current;
6729 dcmp = mc->mc_dbx->md_dcmp;
6730 #if UINT_MAX < SIZE_MAX
6731 if (dcmp == mdb_cmp_int && olddata.mv_size == sizeof(size_t))
6732 dcmp = mdb_cmp_clong;
6733 #endif
6734 /* does data match? */
6735 if (!dcmp(data, &olddata)) {
6736 if (flags & (MDB_NODUPDATA|MDB_APPENDDUP))
6737 return MDB_KEYEXIST;
6738 /* overwrite it */
6739 goto current;
6740 }
6741
6742 /* Back up original data item */
6743 dkey.mv_size = olddata.mv_size;
6744 dkey.mv_data = memcpy(fp+1, olddata.mv_data, olddata.mv_size);
6745
6746 /* Make sub-page header for the dup items, with dummy body */
6747 fp->mp_flags = P_LEAF|P_DIRTY|P_SUBP;
6748 fp->mp_lower = (PAGEHDRSZ-PAGEBASE);
6749 xdata.mv_size = PAGEHDRSZ + dkey.mv_size + data->mv_size;
6750 if (mc->mc_db->md_flags & MDB_DUPFIXED) {
6751 fp->mp_flags |= P_LEAF2;
6752 fp->mp_pad = data->mv_size;
6753 xdata.mv_size += 2 * data->mv_size; /* leave space for 2 more */
6754 } else {
6755 xdata.mv_size += 2 * (sizeof(indx_t) + NODESIZE) +
6756 (dkey.mv_size & 1) + (data->mv_size & 1);
6757 }
6758 fp->mp_upper = xdata.mv_size - PAGEBASE;
6759 olddata.mv_size = xdata.mv_size; /* pretend olddata is fp */
6760 } else if (leaf->mn_flags & F_SUBDATA) {
6761 /* Data is on sub-DB, just store it */
6762 flags |= F_DUPDATA|F_SUBDATA;
6763 goto put_sub;
6764 } else {
6765 /* Data is on sub-page */
6766 fp = olddata.mv_data;
6767 switch (flags) {
6768 default:
6769 if (!(mc->mc_db->md_flags & MDB_DUPFIXED)) {
6770 offset = EVEN(NODESIZE + sizeof(indx_t) +
6771 data->mv_size);
6772 break;
6773 }
6774 offset = fp->mp_pad;
6775 if (SIZELEFT(fp) < offset) {
6776 offset *= 4; /* space for 4 more */
6777 break;
6778 }
6779 /* FALLTHRU: Big enough MDB_DUPFIXED sub-page */
6780 case MDB_CURRENT:
6781 fp->mp_flags |= P_DIRTY;
6782 COPY_PGNO(fp->mp_pgno, mp->mp_pgno);
6783 mc->mc_xcursor->mx_cursor.mc_pg[0] = fp;
6784 flags |= F_DUPDATA;
6785 goto put_sub;
6786 }
6787 xdata.mv_size = olddata.mv_size + offset;
6788 }
6789
6790 fp_flags = fp->mp_flags;
6791 if (NODESIZE + NODEKSZ(leaf) + xdata.mv_size > env->me_nodemax) {
6792 /* Too big for a sub-page, convert to sub-DB */
6793 fp_flags &= ~P_SUBP;
6794 prep_subDB:
6795 if (mc->mc_db->md_flags & MDB_DUPFIXED) {
6796 fp_flags |= P_LEAF2;
6797 dummy.md_pad = fp->mp_pad;
6798 dummy.md_flags = MDB_DUPFIXED;
6799 if (mc->mc_db->md_flags & MDB_INTEGERDUP)
6800 dummy.md_flags |= MDB_INTEGERKEY;
6801 } else {
6802 dummy.md_pad = 0;
6803 dummy.md_flags = 0;
6804 }
6805 dummy.md_depth = 1;
6806 dummy.md_branch_pages = 0;
6807 dummy.md_leaf_pages = 1;
6808 dummy.md_overflow_pages = 0;
6809 dummy.md_entries = NUMKEYS(fp);
6810 xdata.mv_size = sizeof(MDB_db);
6811 xdata.mv_data = &dummy;
6812 if ((rc = mdb_page_alloc(mc, 1, &mp)))
6813 return rc;
6814 offset = env->me_psize - olddata.mv_size;
6815 flags |= F_DUPDATA|F_SUBDATA;
6816 dummy.md_root = mp->mp_pgno;
6817 sub_root = mp;
6818 }
6819 if (mp != fp) {
6820 mp->mp_flags = fp_flags | P_DIRTY;
6821 mp->mp_pad = fp->mp_pad;
6822 mp->mp_lower = fp->mp_lower;
6823 mp->mp_upper = fp->mp_upper + offset;
6824 if (fp_flags & P_LEAF2) {
6825 memcpy(METADATA(mp), METADATA(fp), NUMKEYS(fp) * fp->mp_pad);
6826 } else {
6827 memcpy((char *)mp + mp->mp_upper + PAGEBASE, (char *)fp + fp->mp_upper + PAGEBASE,
6828 olddata.mv_size - fp->mp_upper - PAGEBASE);
6829 memcpy((char *)(&mp->mp_ptrs), (char *)(&fp->mp_ptrs), NUMKEYS(fp) * sizeof(mp->mp_ptrs[0]));
6830 for (i=0; i<NUMKEYS(fp); i++)
6831 mp->mp_ptrs[i] += offset;
6832 }
6833 }
6834
6835 rdata = &xdata;
6836 flags |= F_DUPDATA;
6837 do_sub = 1;
6838 if (!insert_key)
6839 mdb_node_del(mc, 0);
6840 goto new_sub;
6841 }
6842 current:
6843 /* LMDB passes F_SUBDATA in 'flags' to write a DB record */
6844 if ((leaf->mn_flags ^ flags) & F_SUBDATA)
6845 return MDB_INCOMPATIBLE;
6846 /* overflow page overwrites need special handling */
6847 if (F_ISSET(leaf->mn_flags, F_BIGDATA)) {
6848 MDB_page *omp;
6849 pgno_t pg;
6850 int level, ovpages, dpages = OVPAGES(data->mv_size, env->me_psize);
6851
6852 memcpy(&pg, olddata.mv_data, sizeof(pg));
6853 if ((rc2 = mdb_page_get(mc, pg, &omp, &level)) != 0)
6854 return rc2;
6855 ovpages = omp->mp_pages;
6856
6857 /* Is the ov page large enough? */
6858 if (ovpages >= dpages) {
6859 if (!(omp->mp_flags & P_DIRTY) &&
6860 (level || (env->me_flags & MDB_WRITEMAP)))
6861 {
6862 rc = mdb_page_unspill(mc->mc_txn, omp, &omp);
6863 if (rc)
6864 return rc;
6865 level = 0; /* dirty in this txn or clean */
6866 }
6867 /* Is it dirty? */
6868 if (omp->mp_flags & P_DIRTY) {
6869 /* yes, overwrite it. Note in this case we don't
6870 * bother to try shrinking the page if the new data
6871 * is smaller than the overflow threshold.
6872 */
6873 if (level > 1) {
6874 /* It is writable only in a parent txn */
6875 size_t sz = (size_t) env->me_psize * ovpages, off;
6876 MDB_page *np = mdb_page_malloc(mc->mc_txn, ovpages);
6877 MDB_ID2 id2;
6878 if (!np)
6879 return ENOMEM;
6880 id2.mid = pg;
6881 id2.mptr = np;
6882 /* Note - this page is already counted in parent's dirty_room */
6883 rc2 = mdb_mid2l_insert(mc->mc_txn->mt_u.dirty_list, &id2);
6884 mdb_cassert(mc, rc2 == 0);
6885 /* Currently we make the page look as with put() in the
6886 * parent txn, in case the user peeks at MDB_RESERVEd
6887 * or unused parts. Some users treat ovpages specially.
6888 */
6889 if (!(flags & MDB_RESERVE)) {
6890 /* Skip the part where LMDB will put *data.
6891 * Copy end of page, adjusting alignment so
6892 * compiler may copy words instead of bytes.
6893 */
6894 off = (PAGEHDRSZ + data->mv_size) & -sizeof(size_t);
6895 memcpy((size_t *)((char *)np + off),
6896 (size_t *)((char *)omp + off), sz - off);
6897 sz = PAGEHDRSZ;
6898 }
6899 memcpy(np, omp, sz); /* Copy beginning of page */
6900 omp = np;
6901 }
6902 SETDSZ(leaf, data->mv_size);
6903 if (F_ISSET(flags, MDB_RESERVE))
6904 data->mv_data = METADATA(omp);
6905 else
6906 memcpy(METADATA(omp), data->mv_data, data->mv_size);
6907 return MDB_SUCCESS;
6908 }
6909 }
6910 if ((rc2 = mdb_ovpage_free(mc, omp)) != MDB_SUCCESS)
6911 return rc2;
6912 } else if (data->mv_size == olddata.mv_size) {
6913 /* same size, just replace it. Note that we could
6914 * also reuse this node if the new data is smaller,
6915 * but instead we opt to shrink the node in that case.
6916 */
6917 if (F_ISSET(flags, MDB_RESERVE))
6918 data->mv_data = olddata.mv_data;
6919 else if (!(mc->mc_flags & C_SUB))
6920 memcpy(olddata.mv_data, data->mv_data, data->mv_size);
6921 else {
6922 memcpy(NODEKEY(leaf), key->mv_data, key->mv_size);
6923 goto fix_parent;
6924 }
6925 return MDB_SUCCESS;
6926 }
6927 mdb_node_del(mc, 0);
6928 }
6929
6930 rdata = data;
6931
6932 new_sub:
6933 nflags = flags & NODE_ADD_FLAGS;
6934 nsize = IS_LEAF2(mc->mc_pg[mc->mc_top]) ? key->mv_size : mdb_leaf_size(env, key, rdata);
6935 if (SIZELEFT(mc->mc_pg[mc->mc_top]) < nsize) {
6936 if (( flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA )
6937 nflags &= ~MDB_APPEND; /* sub-page may need room to grow */
6938 if (!insert_key)
6939 nflags |= MDB_SPLIT_REPLACE;
6940 rc = mdb_page_split(mc, key, rdata, P_INVALID, nflags);
6941 } else {
6942 /* There is room already in this leaf page. */
6943 rc = mdb_node_add(mc, mc->mc_ki[mc->mc_top], key, rdata, 0, nflags);
6944 if (rc == 0) {
6945 /* Adjust other cursors pointing to mp */
6946 MDB_cursor *m2, *m3;
6947 MDB_dbi dbi = mc->mc_dbi;
6948 unsigned i = mc->mc_top;
6949 MDB_page *mp = mc->mc_pg[i];
6950
6951 for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
6952 if (mc->mc_flags & C_SUB)
6953 m3 = &m2->mc_xcursor->mx_cursor;
6954 else
6955 m3 = m2;
6956 if (m3 == mc || m3->mc_snum < mc->mc_snum || m3->mc_pg[i] != mp) continue;
6957 if (m3->mc_ki[i] >= mc->mc_ki[i] && insert_key) {
6958 m3->mc_ki[i]++;
6959 }
6960 XCURSOR_REFRESH(m3, i, mp);
6961 }
6962 }
6963 }
6964
6965 if (rc == MDB_SUCCESS) {
6966 /* Now store the actual data in the child DB. Note that we're
6967 * storing the user data in the keys field, so there are strict
6968 * size limits on dupdata. The actual data fields of the child
6969 * DB are all zero size.
6970 */
6971 if (do_sub) {
6972 int xflags, new_dupdata;
6973 size_t ecount;
6974 put_sub:
6975 xdata.mv_size = 0;
6976 xdata.mv_data = "";
6977 leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
6978 if (flags & MDB_CURRENT) {
6979 xflags = MDB_CURRENT|MDB_NOSPILL;
6980 } else {
6981 mdb_xcursor_init1(mc, leaf);
6982 xflags = (flags & MDB_NODUPDATA) ?
6983 MDB_NOOVERWRITE|MDB_NOSPILL : MDB_NOSPILL;
6984 }
6985 if (sub_root)
6986 mc->mc_xcursor->mx_cursor.mc_pg[0] = sub_root;
6987 new_dupdata = (int)dkey.mv_size;
6988 /* converted, write the original data first */
6989 if (dkey.mv_size) {
6990 rc = mdb_cursor_put(&mc->mc_xcursor->mx_cursor, &dkey, &xdata, xflags);
6991 if (rc)
6992 goto bad_sub;
6993 /* we've done our job */
6994 dkey.mv_size = 0;
6995 }
6996 if (!(leaf->mn_flags & F_SUBDATA) || sub_root) {
6997 /* Adjust other cursors pointing to mp */
6998 MDB_cursor *m2;
6999 MDB_xcursor *mx = mc->mc_xcursor;
7000 unsigned i = mc->mc_top;
7001 MDB_page *mp = mc->mc_pg[i];
7002
7003 for (m2 = mc->mc_txn->mt_cursors[mc->mc_dbi]; m2; m2=m2->mc_next) {
7004 if (m2 == mc || m2->mc_snum < mc->mc_snum) continue;
7005 if (!(m2->mc_flags & C_INITIALIZED)) continue;
7006 if (m2->mc_pg[i] == mp) {
7007 if (m2->mc_ki[i] == mc->mc_ki[i]) {
7008 mdb_xcursor_init2(m2, mx, new_dupdata);
7009 } else if (!insert_key) {
7010 XCURSOR_REFRESH(m2, i, mp);
7011 }
7012 }
7013 }
7014 }
7015 ecount = mc->mc_xcursor->mx_db.md_entries;
7016 if (flags & MDB_APPENDDUP)
7017 xflags |= MDB_APPEND;
7018 rc = mdb_cursor_put(&mc->mc_xcursor->mx_cursor, data, &xdata, xflags);
7019 if (flags & F_SUBDATA) {
7020 void *db = NODEDATA(leaf);
7021 memcpy(db, &mc->mc_xcursor->mx_db, sizeof(MDB_db));
7022 }
7023 insert_data = mc->mc_xcursor->mx_db.md_entries - ecount;
7024 }
7025 /* Increment count unless we just replaced an existing item. */
7026 if (insert_data)
7027 mc->mc_db->md_entries++;
7028 if (insert_key) {
7029 /* Invalidate txn if we created an empty sub-DB */
7030 if (rc)
7031 goto bad_sub;
7032 /* If we succeeded and the key didn't exist before,
7033 * make sure the cursor is marked valid.
7034 */
7035 mc->mc_flags |= C_INITIALIZED;
7036 }
7037 if (flags & MDB_MULTIPLE) {
7038 if (!rc) {
7039 mcount++;
7040 /* let caller know how many succeeded, if any */
7041 data[1].mv_size = mcount;
7042 if (mcount < dcount) {
7043 data[0].mv_data = (char *)data[0].mv_data + data[0].mv_size;
7044 insert_key = insert_data = 0;
7045 goto more;
7046 }
7047 }
7048 }
7049 return rc;
7050 bad_sub:
7051 if (rc == MDB_KEYEXIST) /* should not happen, we deleted that item */
7052 rc = MDB_CORRUPTED;
7053 }
7054 mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
7055 return rc;
7056 }
7057
7058 int
mdb_cursor_del(MDB_cursor * mc,unsigned int flags)7059 mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
7060 {
7061 MDB_node *leaf;
7062 MDB_page *mp;
7063 int rc;
7064
7065 if (mc->mc_txn->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_BLOCKED))
7066 return (mc->mc_txn->mt_flags & MDB_TXN_RDONLY) ? EACCES : MDB_BAD_TXN;
7067
7068 if (!(mc->mc_flags & C_INITIALIZED))
7069 return EINVAL;
7070
7071 if (mc->mc_ki[mc->mc_top] >= NUMKEYS(mc->mc_pg[mc->mc_top]))
7072 return MDB_NOTFOUND;
7073
7074 if (!(flags & MDB_NOSPILL) && (rc = mdb_page_spill(mc, NULL, NULL)))
7075 return rc;
7076
7077 rc = mdb_cursor_touch(mc);
7078 if (rc)
7079 return rc;
7080
7081 mp = mc->mc_pg[mc->mc_top];
7082 if (IS_LEAF2(mp))
7083 goto del_key;
7084 leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
7085
7086 if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
7087 if (flags & MDB_NODUPDATA) {
7088 /* mdb_cursor_del0() will subtract the final entry */
7089 mc->mc_db->md_entries -= mc->mc_xcursor->mx_db.md_entries - 1;
7090 mc->mc_xcursor->mx_cursor.mc_flags &= ~C_INITIALIZED;
7091 } else {
7092 if (!F_ISSET(leaf->mn_flags, F_SUBDATA)) {
7093 mc->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf);
7094 }
7095 rc = mdb_cursor_del(&mc->mc_xcursor->mx_cursor, MDB_NOSPILL);
7096 if (rc)
7097 return rc;
7098 /* If sub-DB still has entries, we're done */
7099 if (mc->mc_xcursor->mx_db.md_entries) {
7100 if (leaf->mn_flags & F_SUBDATA) {
7101 /* update subDB info */
7102 void *db = NODEDATA(leaf);
7103 memcpy(db, &mc->mc_xcursor->mx_db, sizeof(MDB_db));
7104 } else {
7105 MDB_cursor *m2;
7106 /* shrink fake page */
7107 mdb_node_shrink(mp, mc->mc_ki[mc->mc_top]);
7108 leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
7109 mc->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf);
7110 /* fix other sub-DB cursors pointed at fake pages on this page */
7111 for (m2 = mc->mc_txn->mt_cursors[mc->mc_dbi]; m2; m2=m2->mc_next) {
7112 if (m2 == mc || m2->mc_snum < mc->mc_snum) continue;
7113 if (!(m2->mc_flags & C_INITIALIZED)) continue;
7114 if (m2->mc_pg[mc->mc_top] == mp) {
7115 XCURSOR_REFRESH(m2, mc->mc_top, mp);
7116 }
7117 }
7118 }
7119 mc->mc_db->md_entries--;
7120 return rc;
7121 } else {
7122 mc->mc_xcursor->mx_cursor.mc_flags &= ~C_INITIALIZED;
7123 }
7124 /* otherwise fall thru and delete the sub-DB */
7125 }
7126
7127 if (leaf->mn_flags & F_SUBDATA) {
7128 /* add all the child DB's pages to the free list */
7129 rc = mdb_drop0(&mc->mc_xcursor->mx_cursor, 0);
7130 if (rc)
7131 goto fail;
7132 }
7133 }
7134 /* LMDB passes F_SUBDATA in 'flags' to delete a DB record */
7135 else if ((leaf->mn_flags ^ flags) & F_SUBDATA) {
7136 rc = MDB_INCOMPATIBLE;
7137 goto fail;
7138 }
7139
7140 /* add overflow pages to free list */
7141 if (F_ISSET(leaf->mn_flags, F_BIGDATA)) {
7142 MDB_page *omp;
7143 pgno_t pg;
7144
7145 memcpy(&pg, NODEDATA(leaf), sizeof(pg));
7146 if ((rc = mdb_page_get(mc, pg, &omp, NULL)) ||
7147 (rc = mdb_ovpage_free(mc, omp)))
7148 goto fail;
7149 }
7150
7151 del_key:
7152 return mdb_cursor_del0(mc);
7153
7154 fail:
7155 mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
7156 return rc;
7157 }
7158
7159 /** Allocate and initialize new pages for a database.
7160 * Set #MDB_TXN_ERROR on failure.
7161 * @param[in] mc a cursor on the database being added to.
7162 * @param[in] flags flags defining what type of page is being allocated.
7163 * @param[in] num the number of pages to allocate. This is usually 1,
7164 * unless allocating overflow pages for a large record.
7165 * @param[out] mp Address of a page, or NULL on failure.
7166 * @return 0 on success, non-zero on failure.
7167 */
7168 static int
mdb_page_new(MDB_cursor * mc,uint32_t flags,int num,MDB_page ** mp)7169 mdb_page_new(MDB_cursor *mc, uint32_t flags, int num, MDB_page **mp)
7170 {
7171 MDB_page *np;
7172 int rc;
7173
7174 if ((rc = mdb_page_alloc(mc, num, &np)))
7175 return rc;
7176 DPRINTF(("allocated new mpage %"Z"u, page size %u",
7177 np->mp_pgno, mc->mc_txn->mt_env->me_psize));
7178 np->mp_flags = flags | P_DIRTY;
7179 np->mp_lower = (PAGEHDRSZ-PAGEBASE);
7180 np->mp_upper = mc->mc_txn->mt_env->me_psize - PAGEBASE;
7181
7182 if (IS_BRANCH(np))
7183 mc->mc_db->md_branch_pages++;
7184 else if (IS_LEAF(np))
7185 mc->mc_db->md_leaf_pages++;
7186 else if (IS_OVERFLOW(np)) {
7187 mc->mc_db->md_overflow_pages += num;
7188 np->mp_pages = num;
7189 }
7190 *mp = np;
7191
7192 return 0;
7193 }
7194
7195 /** Calculate the size of a leaf node.
7196 * The size depends on the environment's page size; if a data item
7197 * is too large it will be put onto an overflow page and the node
7198 * size will only include the key and not the data. Sizes are always
7199 * rounded up to an even number of bytes, to guarantee 2-byte alignment
7200 * of the #MDB_node headers.
7201 * @param[in] env The environment handle.
7202 * @param[in] key The key for the node.
7203 * @param[in] data The data for the node.
7204 * @return The number of bytes needed to store the node.
7205 */
7206 static size_t
mdb_leaf_size(MDB_env * env,MDB_val * key,MDB_val * data)7207 mdb_leaf_size(MDB_env *env, MDB_val *key, MDB_val *data)
7208 {
7209 size_t sz;
7210
7211 sz = LEAFSIZE(key, data);
7212 if (sz > env->me_nodemax) {
7213 /* put on overflow page */
7214 sz -= data->mv_size - sizeof(pgno_t);
7215 }
7216
7217 return EVEN(sz + sizeof(indx_t));
7218 }
7219
7220 /** Calculate the size of a branch node.
7221 * The size should depend on the environment's page size but since
7222 * we currently don't support spilling large keys onto overflow
7223 * pages, it's simply the size of the #MDB_node header plus the
7224 * size of the key. Sizes are always rounded up to an even number
7225 * of bytes, to guarantee 2-byte alignment of the #MDB_node headers.
7226 * @param[in] env The environment handle.
7227 * @param[in] key The key for the node.
7228 * @return The number of bytes needed to store the node.
7229 */
7230 static size_t
mdb_branch_size(MDB_env * env,MDB_val * key)7231 mdb_branch_size(MDB_env *env, MDB_val *key)
7232 {
7233 size_t sz;
7234
7235 sz = INDXSIZE(key);
7236 if (sz > env->me_nodemax) {
7237 /* put on overflow page */
7238 /* not implemented */
7239 /* sz -= key->size - sizeof(pgno_t); */
7240 }
7241
7242 return sz + sizeof(indx_t);
7243 }
7244
7245 /** Add a node to the page pointed to by the cursor.
7246 * Set #MDB_TXN_ERROR on failure.
7247 * @param[in] mc The cursor for this operation.
7248 * @param[in] indx The index on the page where the new node should be added.
7249 * @param[in] key The key for the new node.
7250 * @param[in] data The data for the new node, if any.
7251 * @param[in] pgno The page number, if adding a branch node.
7252 * @param[in] flags Flags for the node.
7253 * @return 0 on success, non-zero on failure. Possible errors are:
7254 * <ul>
7255 * <li>ENOMEM - failed to allocate overflow pages for the node.
7256 * <li>MDB_PAGE_FULL - there is insufficient room in the page. This error
7257 * should never happen since all callers already calculate the
7258 * page's free space before calling this function.
7259 * </ul>
7260 */
7261 static int
mdb_node_add(MDB_cursor * mc,indx_t indx,MDB_val * key,MDB_val * data,pgno_t pgno,unsigned int flags)7262 mdb_node_add(MDB_cursor *mc, indx_t indx,
7263 MDB_val *key, MDB_val *data, pgno_t pgno, unsigned int flags)
7264 {
7265 unsigned int i;
7266 size_t node_size = NODESIZE;
7267 ssize_t room;
7268 indx_t ofs;
7269 MDB_node *node;
7270 MDB_page *mp = mc->mc_pg[mc->mc_top];
7271 MDB_page *ofp = NULL; /* overflow page */
7272 void *ndata;
7273 DKBUF;
7274
7275 mdb_cassert(mc, mp->mp_upper >= mp->mp_lower);
7276
7277 DPRINTF(("add to %s %spage %"Z"u index %i, data size %"Z"u key size %"Z"u [%s]",
7278 IS_LEAF(mp) ? "leaf" : "branch",
7279 IS_SUBP(mp) ? "sub-" : "",
7280 mdb_dbg_pgno(mp), indx, data ? data->mv_size : 0,
7281 key ? key->mv_size : 0, key ? DKEY(key) : "null"));
7282
7283 if (IS_LEAF2(mp)) {
7284 /* Move higher keys up one slot. */
7285 int ksize = mc->mc_db->md_pad, dif;
7286 char *ptr = LEAF2KEY(mp, indx, ksize);
7287 dif = NUMKEYS(mp) - indx;
7288 if (dif > 0)
7289 memmove(ptr+ksize, ptr, dif*ksize);
7290 /* insert new key */
7291 memcpy(ptr, key->mv_data, ksize);
7292
7293 /* Just using these for counting */
7294 mp->mp_lower += sizeof(indx_t);
7295 mp->mp_upper -= ksize - sizeof(indx_t);
7296 return MDB_SUCCESS;
7297 }
7298
7299 room = (ssize_t)SIZELEFT(mp) - (ssize_t)sizeof(indx_t);
7300 if (key != NULL)
7301 node_size += key->mv_size;
7302 if (IS_LEAF(mp)) {
7303 mdb_cassert(mc, key && data);
7304 if (F_ISSET(flags, F_BIGDATA)) {
7305 /* Data already on overflow page. */
7306 node_size += sizeof(pgno_t);
7307 } else if (node_size + data->mv_size > mc->mc_txn->mt_env->me_nodemax) {
7308 int ovpages = OVPAGES(data->mv_size, mc->mc_txn->mt_env->me_psize);
7309 int rc;
7310 /* Put data on overflow page. */
7311 DPRINTF(("data size is %"Z"u, node would be %"Z"u, put data on overflow page",
7312 data->mv_size, node_size+data->mv_size));
7313 node_size = EVEN(node_size + sizeof(pgno_t));
7314 if ((ssize_t)node_size > room)
7315 goto full;
7316 if ((rc = mdb_page_new(mc, P_OVERFLOW, ovpages, &ofp)))
7317 return rc;
7318 DPRINTF(("allocated overflow page %"Z"u", ofp->mp_pgno));
7319 flags |= F_BIGDATA;
7320 goto update;
7321 } else {
7322 node_size += data->mv_size;
7323 }
7324 }
7325 node_size = EVEN(node_size);
7326 if ((ssize_t)node_size > room)
7327 goto full;
7328
7329 update:
7330 /* Move higher pointers up one slot. */
7331 for (i = NUMKEYS(mp); i > indx; i--)
7332 mp->mp_ptrs[i] = mp->mp_ptrs[i - 1];
7333
7334 /* Adjust free space offsets. */
7335 ofs = mp->mp_upper - node_size;
7336 mdb_cassert(mc, ofs >= mp->mp_lower + sizeof(indx_t));
7337 mp->mp_ptrs[indx] = ofs;
7338 mp->mp_upper = ofs;
7339 mp->mp_lower += sizeof(indx_t);
7340
7341 /* Write the node data. */
7342 node = NODEPTR(mp, indx);
7343 node->mn_ksize = (key == NULL) ? 0 : key->mv_size;
7344 node->mn_flags = flags;
7345 if (IS_LEAF(mp))
7346 SETDSZ(node,data->mv_size);
7347 else
7348 SETPGNO(node,pgno);
7349
7350 if (key)
7351 memcpy(NODEKEY(node), key->mv_data, key->mv_size);
7352
7353 if (IS_LEAF(mp)) {
7354 ndata = NODEDATA(node);
7355 if (ofp == NULL) {
7356 if (F_ISSET(flags, F_BIGDATA))
7357 memcpy(ndata, data->mv_data, sizeof(pgno_t));
7358 else if (F_ISSET(flags, MDB_RESERVE))
7359 data->mv_data = ndata;
7360 else
7361 memcpy(ndata, data->mv_data, data->mv_size);
7362 } else {
7363 memcpy(ndata, &ofp->mp_pgno, sizeof(pgno_t));
7364 ndata = METADATA(ofp);
7365 if (F_ISSET(flags, MDB_RESERVE))
7366 data->mv_data = ndata;
7367 else
7368 memcpy(ndata, data->mv_data, data->mv_size);
7369 }
7370 }
7371
7372 return MDB_SUCCESS;
7373
7374 full:
7375 DPRINTF(("not enough room in page %"Z"u, got %u ptrs",
7376 mdb_dbg_pgno(mp), NUMKEYS(mp)));
7377 DPRINTF(("upper-lower = %u - %u = %"Z"d", mp->mp_upper,mp->mp_lower,room));
7378 DPRINTF(("node size = %"Z"u", node_size));
7379 mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
7380 return MDB_PAGE_FULL;
7381 }
7382
7383 /** Delete the specified node from a page.
7384 * @param[in] mc Cursor pointing to the node to delete.
7385 * @param[in] ksize The size of a node. Only used if the page is
7386 * part of a #MDB_DUPFIXED database.
7387 */
7388 static void
mdb_node_del(MDB_cursor * mc,int ksize)7389 mdb_node_del(MDB_cursor *mc, int ksize)
7390 {
7391 MDB_page *mp = mc->mc_pg[mc->mc_top];
7392 indx_t indx = mc->mc_ki[mc->mc_top];
7393 unsigned int sz;
7394 indx_t i, j, numkeys, ptr;
7395 MDB_node *node;
7396 char *base;
7397
7398 DPRINTF(("delete node %u on %s page %"Z"u", indx,
7399 IS_LEAF(mp) ? "leaf" : "branch", mdb_dbg_pgno(mp)));
7400 numkeys = NUMKEYS(mp);
7401 mdb_cassert(mc, indx < numkeys);
7402
7403 if (IS_LEAF2(mp)) {
7404 int x = numkeys - 1 - indx;
7405 base = LEAF2KEY(mp, indx, ksize);
7406 if (x)
7407 memmove(base, base + ksize, x * ksize);
7408 mp->mp_lower -= sizeof(indx_t);
7409 mp->mp_upper += ksize - sizeof(indx_t);
7410 return;
7411 }
7412
7413 node = NODEPTR(mp, indx);
7414 sz = NODESIZE + node->mn_ksize;
7415 if (IS_LEAF(mp)) {
7416 if (F_ISSET(node->mn_flags, F_BIGDATA))
7417 sz += sizeof(pgno_t);
7418 else
7419 sz += NODEDSZ(node);
7420 }
7421 sz = EVEN(sz);
7422
7423 ptr = mp->mp_ptrs[indx];
7424 for (i = j = 0; i < numkeys; i++) {
7425 if (i != indx) {
7426 mp->mp_ptrs[j] = mp->mp_ptrs[i];
7427 if (mp->mp_ptrs[i] < ptr)
7428 mp->mp_ptrs[j] += sz;
7429 j++;
7430 }
7431 }
7432
7433 base = (char *)mp + mp->mp_upper + PAGEBASE;
7434 memmove(base + sz, base, ptr - mp->mp_upper);
7435
7436 mp->mp_lower -= sizeof(indx_t);
7437 mp->mp_upper += sz;
7438 }
7439
7440 /** Compact the main page after deleting a node on a subpage.
7441 * @param[in] mp The main page to operate on.
7442 * @param[in] indx The index of the subpage on the main page.
7443 */
7444 static void
mdb_node_shrink(MDB_page * mp,indx_t indx)7445 mdb_node_shrink(MDB_page *mp, indx_t indx)
7446 {
7447 MDB_node *node;
7448 MDB_page *sp, *xp;
7449 char *base;
7450 indx_t delta, nsize, len, ptr;
7451 int i;
7452
7453 node = NODEPTR(mp, indx);
7454 sp = (MDB_page *)NODEDATA(node);
7455 delta = SIZELEFT(sp);
7456 nsize = NODEDSZ(node) - delta;
7457
7458 /* Prepare to shift upward, set len = length(subpage part to shift) */
7459 if (IS_LEAF2(sp)) {
7460 len = nsize;
7461 if (nsize & 1)
7462 return; /* do not make the node uneven-sized */
7463 } else {
7464 xp = (MDB_page *)((char *)sp + delta); /* destination subpage */
7465 for (i = NUMKEYS(sp); --i >= 0; )
7466 xp->mp_ptrs[i] = sp->mp_ptrs[i] - delta;
7467 len = PAGEHDRSZ;
7468 }
7469 sp->mp_upper = sp->mp_lower;
7470 COPY_PGNO(sp->mp_pgno, mp->mp_pgno);
7471 SETDSZ(node, nsize);
7472
7473 /* Shift <lower nodes...initial part of subpage> upward */
7474 base = (char *)mp + mp->mp_upper + PAGEBASE;
7475 memmove(base + delta, base, (char *)sp + len - base);
7476
7477 ptr = mp->mp_ptrs[indx];
7478 for (i = NUMKEYS(mp); --i >= 0; ) {
7479 if (mp->mp_ptrs[i] <= ptr)
7480 mp->mp_ptrs[i] += delta;
7481 }
7482 mp->mp_upper += delta;
7483 }
7484
7485 /** Initial setup of a sorted-dups cursor.
7486 * Sorted duplicates are implemented as a sub-database for the given key.
7487 * The duplicate data items are actually keys of the sub-database.
7488 * Operations on the duplicate data items are performed using a sub-cursor
7489 * initialized when the sub-database is first accessed. This function does
7490 * the preliminary setup of the sub-cursor, filling in the fields that
7491 * depend only on the parent DB.
7492 * @param[in] mc The main cursor whose sorted-dups cursor is to be initialized.
7493 */
7494 static void
mdb_xcursor_init0(MDB_cursor * mc)7495 mdb_xcursor_init0(MDB_cursor *mc)
7496 {
7497 MDB_xcursor *mx = mc->mc_xcursor;
7498
7499 mx->mx_cursor.mc_xcursor = NULL;
7500 mx->mx_cursor.mc_txn = mc->mc_txn;
7501 mx->mx_cursor.mc_db = &mx->mx_db;
7502 mx->mx_cursor.mc_dbx = &mx->mx_dbx;
7503 mx->mx_cursor.mc_dbi = mc->mc_dbi;
7504 mx->mx_cursor.mc_dbflag = &mx->mx_dbflag;
7505 mx->mx_cursor.mc_snum = 0;
7506 mx->mx_cursor.mc_top = 0;
7507 mx->mx_cursor.mc_flags = C_SUB;
7508 mx->mx_dbx.md_name.mv_size = 0;
7509 mx->mx_dbx.md_name.mv_data = NULL;
7510 mx->mx_dbx.md_cmp = mc->mc_dbx->md_dcmp;
7511 mx->mx_dbx.md_dcmp = NULL;
7512 mx->mx_dbx.md_rel = mc->mc_dbx->md_rel;
7513 }
7514
7515 /** Final setup of a sorted-dups cursor.
7516 * Sets up the fields that depend on the data from the main cursor.
7517 * @param[in] mc The main cursor whose sorted-dups cursor is to be initialized.
7518 * @param[in] node The data containing the #MDB_db record for the
7519 * sorted-dup database.
7520 */
7521 static void
mdb_xcursor_init1(MDB_cursor * mc,MDB_node * node)7522 mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node)
7523 {
7524 MDB_xcursor *mx = mc->mc_xcursor;
7525
7526 if (node->mn_flags & F_SUBDATA) {
7527 memcpy(&mx->mx_db, NODEDATA(node), sizeof(MDB_db));
7528 mx->mx_cursor.mc_pg[0] = 0;
7529 mx->mx_cursor.mc_snum = 0;
7530 mx->mx_cursor.mc_top = 0;
7531 mx->mx_cursor.mc_flags = C_SUB;
7532 } else {
7533 MDB_page *fp = NODEDATA(node);
7534 mx->mx_db.md_pad = 0;
7535 mx->mx_db.md_flags = 0;
7536 mx->mx_db.md_depth = 1;
7537 mx->mx_db.md_branch_pages = 0;
7538 mx->mx_db.md_leaf_pages = 1;
7539 mx->mx_db.md_overflow_pages = 0;
7540 mx->mx_db.md_entries = NUMKEYS(fp);
7541 COPY_PGNO(mx->mx_db.md_root, fp->mp_pgno);
7542 mx->mx_cursor.mc_snum = 1;
7543 mx->mx_cursor.mc_top = 0;
7544 mx->mx_cursor.mc_flags = C_INITIALIZED|C_SUB;
7545 mx->mx_cursor.mc_pg[0] = fp;
7546 mx->mx_cursor.mc_ki[0] = 0;
7547 if (mc->mc_db->md_flags & MDB_DUPFIXED) {
7548 mx->mx_db.md_flags = MDB_DUPFIXED;
7549 mx->mx_db.md_pad = fp->mp_pad;
7550 if (mc->mc_db->md_flags & MDB_INTEGERDUP)
7551 mx->mx_db.md_flags |= MDB_INTEGERKEY;
7552 }
7553 }
7554 DPRINTF(("Sub-db -%u root page %"Z"u", mx->mx_cursor.mc_dbi,
7555 mx->mx_db.md_root));
7556 mx->mx_dbflag = DB_VALID|DB_USRVALID|DB_DUPDATA;
7557 #if UINT_MAX < SIZE_MAX
7558 if (mx->mx_dbx.md_cmp == mdb_cmp_int && mx->mx_db.md_pad == sizeof(size_t))
7559 mx->mx_dbx.md_cmp = mdb_cmp_clong;
7560 #endif
7561 }
7562
7563
7564 /** Fixup a sorted-dups cursor due to underlying update.
7565 * Sets up some fields that depend on the data from the main cursor.
7566 * Almost the same as init1, but skips initialization steps if the
7567 * xcursor had already been used.
7568 * @param[in] mc The main cursor whose sorted-dups cursor is to be fixed up.
7569 * @param[in] src_mx The xcursor of an up-to-date cursor.
7570 * @param[in] new_dupdata True if converting from a non-#F_DUPDATA item.
7571 */
7572 static void
mdb_xcursor_init2(MDB_cursor * mc,MDB_xcursor * src_mx,int new_dupdata)7573 mdb_xcursor_init2(MDB_cursor *mc, MDB_xcursor *src_mx, int new_dupdata)
7574 {
7575 MDB_xcursor *mx = mc->mc_xcursor;
7576
7577 if (new_dupdata) {
7578 mx->mx_cursor.mc_snum = 1;
7579 mx->mx_cursor.mc_top = 0;
7580 mx->mx_cursor.mc_flags |= C_INITIALIZED;
7581 mx->mx_cursor.mc_ki[0] = 0;
7582 mx->mx_dbflag = DB_VALID|DB_USRVALID|DB_DUPDATA;
7583 #if UINT_MAX < SIZE_MAX
7584 mx->mx_dbx.md_cmp = src_mx->mx_dbx.md_cmp;
7585 #endif
7586 } else if (!(mx->mx_cursor.mc_flags & C_INITIALIZED)) {
7587 return;
7588 }
7589 mx->mx_db = src_mx->mx_db;
7590 mx->mx_cursor.mc_pg[0] = src_mx->mx_cursor.mc_pg[0];
7591 DPRINTF(("Sub-db -%u root page %"Z"u", mx->mx_cursor.mc_dbi,
7592 mx->mx_db.md_root));
7593 }
7594
7595 /** Initialize a cursor for a given transaction and database. */
7596 static void
mdb_cursor_init(MDB_cursor * mc,MDB_txn * txn,MDB_dbi dbi,MDB_xcursor * mx)7597 mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
7598 {
7599 mc->mc_next = NULL;
7600 mc->mc_backup = NULL;
7601 mc->mc_dbi = dbi;
7602 mc->mc_txn = txn;
7603 mc->mc_db = &txn->mt_dbs[dbi];
7604 mc->mc_dbx = &txn->mt_dbxs[dbi];
7605 mc->mc_dbflag = &txn->mt_dbflags[dbi];
7606 mc->mc_snum = 0;
7607 mc->mc_top = 0;
7608 mc->mc_pg[0] = 0;
7609 mc->mc_ki[0] = 0;
7610 mc->mc_flags = 0;
7611 if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
7612 mdb_tassert(txn, mx != NULL);
7613 mc->mc_xcursor = mx;
7614 mdb_xcursor_init0(mc);
7615 } else {
7616 mc->mc_xcursor = NULL;
7617 }
7618 if (*mc->mc_dbflag & DB_STALE) {
7619 mdb_page_search(mc, NULL, MDB_PS_ROOTONLY);
7620 }
7621 }
7622
7623 int
mdb_cursor_open(MDB_txn * txn,MDB_dbi dbi,MDB_cursor ** ret)7624 mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **ret)
7625 {
7626 MDB_cursor *mc;
7627 size_t size = sizeof(MDB_cursor);
7628
7629 if (!ret || !TXN_DBI_EXIST(txn, dbi, DB_VALID))
7630 return EINVAL;
7631
7632 if (txn->mt_flags & MDB_TXN_BLOCKED)
7633 return MDB_BAD_TXN;
7634
7635 if (dbi == FREE_DBI && !F_ISSET(txn->mt_flags, MDB_TXN_RDONLY))
7636 return EINVAL;
7637
7638 if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT)
7639 size += sizeof(MDB_xcursor);
7640
7641 if ((mc = malloc(size)) != NULL) {
7642 mdb_cursor_init(mc, txn, dbi, (MDB_xcursor *)(mc + 1));
7643 if (txn->mt_cursors) {
7644 mc->mc_next = txn->mt_cursors[dbi];
7645 txn->mt_cursors[dbi] = mc;
7646 mc->mc_flags |= C_UNTRACK;
7647 }
7648 } else {
7649 return ENOMEM;
7650 }
7651
7652 *ret = mc;
7653
7654 return MDB_SUCCESS;
7655 }
7656
7657 int
mdb_cursor_renew(MDB_txn * txn,MDB_cursor * mc)7658 mdb_cursor_renew(MDB_txn *txn, MDB_cursor *mc)
7659 {
7660 if (!mc || !TXN_DBI_EXIST(txn, mc->mc_dbi, DB_VALID))
7661 return EINVAL;
7662
7663 if ((mc->mc_flags & C_UNTRACK) || txn->mt_cursors)
7664 return EINVAL;
7665
7666 if (txn->mt_flags & MDB_TXN_BLOCKED)
7667 return MDB_BAD_TXN;
7668
7669 mdb_cursor_init(mc, txn, mc->mc_dbi, mc->mc_xcursor);
7670 return MDB_SUCCESS;
7671 }
7672
7673 /* Return the count of duplicate data items for the current key */
7674 int
mdb_cursor_count(MDB_cursor * mc,size_t * countp)7675 mdb_cursor_count(MDB_cursor *mc, size_t *countp)
7676 {
7677 MDB_node *leaf;
7678
7679 if (mc == NULL || countp == NULL)
7680 return EINVAL;
7681
7682 if (mc->mc_xcursor == NULL)
7683 return MDB_INCOMPATIBLE;
7684
7685 if (mc->mc_txn->mt_flags & MDB_TXN_BLOCKED)
7686 return MDB_BAD_TXN;
7687
7688 if (!(mc->mc_flags & C_INITIALIZED))
7689 return EINVAL;
7690
7691 if (!mc->mc_snum)
7692 return MDB_NOTFOUND;
7693
7694 if (mc->mc_flags & C_EOF) {
7695 if (mc->mc_ki[mc->mc_top] >= NUMKEYS(mc->mc_pg[mc->mc_top]))
7696 return MDB_NOTFOUND;
7697 mc->mc_flags ^= C_EOF;
7698 }
7699
7700 leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
7701 if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
7702 *countp = 1;
7703 } else {
7704 if (!(mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED))
7705 return EINVAL;
7706
7707 *countp = mc->mc_xcursor->mx_db.md_entries;
7708 }
7709 return MDB_SUCCESS;
7710 }
7711
7712 void
mdb_cursor_close(MDB_cursor * mc)7713 mdb_cursor_close(MDB_cursor *mc)
7714 {
7715 if (mc && !mc->mc_backup) {
7716 /* remove from txn, if tracked */
7717 if ((mc->mc_flags & C_UNTRACK) && mc->mc_txn->mt_cursors) {
7718 MDB_cursor **prev = &mc->mc_txn->mt_cursors[mc->mc_dbi];
7719 while (*prev && *prev != mc) prev = &(*prev)->mc_next;
7720 if (*prev == mc)
7721 *prev = mc->mc_next;
7722 }
7723 free(mc);
7724 }
7725 }
7726
7727 MDB_txn *
mdb_cursor_txn(MDB_cursor * mc)7728 mdb_cursor_txn(MDB_cursor *mc)
7729 {
7730 if (!mc) return NULL;
7731 return mc->mc_txn;
7732 }
7733
7734 MDB_dbi
mdb_cursor_dbi(MDB_cursor * mc)7735 mdb_cursor_dbi(MDB_cursor *mc)
7736 {
7737 return mc->mc_dbi;
7738 }
7739
7740 /** Replace the key for a branch node with a new key.
7741 * Set #MDB_TXN_ERROR on failure.
7742 * @param[in] mc Cursor pointing to the node to operate on.
7743 * @param[in] key The new key to use.
7744 * @return 0 on success, non-zero on failure.
7745 */
7746 static int
mdb_update_key(MDB_cursor * mc,MDB_val * key)7747 mdb_update_key(MDB_cursor *mc, MDB_val *key)
7748 {
7749 MDB_page *mp;
7750 MDB_node *node;
7751 char *base;
7752 size_t len;
7753 int delta, ksize, oksize;
7754 indx_t ptr, i, numkeys, indx;
7755 DKBUF;
7756
7757 indx = mc->mc_ki[mc->mc_top];
7758 mp = mc->mc_pg[mc->mc_top];
7759 node = NODEPTR(mp, indx);
7760 ptr = mp->mp_ptrs[indx];
7761 #if MDB_DEBUG
7762 {
7763 MDB_val k2;
7764 char kbuf2[DKBUF_MAXKEYSIZE*2+1];
7765 k2.mv_data = NODEKEY(node);
7766 k2.mv_size = node->mn_ksize;
7767 DPRINTF(("update key %u (ofs %u) [%s] to [%s] on page %"Z"u",
7768 indx, ptr,
7769 mdb_dkey(&k2, kbuf2),
7770 DKEY(key),
7771 mp->mp_pgno));
7772 }
7773 #endif
7774
7775 /* Sizes must be 2-byte aligned. */
7776 ksize = EVEN(key->mv_size);
7777 oksize = EVEN(node->mn_ksize);
7778 delta = ksize - oksize;
7779
7780 /* Shift node contents if EVEN(key length) changed. */
7781 if (delta) {
7782 if (delta > 0 && SIZELEFT(mp) < delta) {
7783 pgno_t pgno;
7784 /* not enough space left, do a delete and split */
7785 DPRINTF(("Not enough room, delta = %d, splitting...", delta));
7786 pgno = NODEPGNO(node);
7787 mdb_node_del(mc, 0);
7788 return mdb_page_split(mc, key, NULL, pgno, MDB_SPLIT_REPLACE);
7789 }
7790
7791 numkeys = NUMKEYS(mp);
7792 for (i = 0; i < numkeys; i++) {
7793 if (mp->mp_ptrs[i] <= ptr)
7794 mp->mp_ptrs[i] -= delta;
7795 }
7796
7797 base = (char *)mp + mp->mp_upper + PAGEBASE;
7798 len = ptr - mp->mp_upper + NODESIZE;
7799 memmove(base - delta, base, len);
7800 mp->mp_upper -= delta;
7801
7802 node = NODEPTR(mp, indx);
7803 }
7804
7805 /* But even if no shift was needed, update ksize */
7806 if (node->mn_ksize != key->mv_size)
7807 node->mn_ksize = key->mv_size;
7808
7809 if (key->mv_size)
7810 memcpy(NODEKEY(node), key->mv_data, key->mv_size);
7811
7812 return MDB_SUCCESS;
7813 }
7814
7815 static void
7816 mdb_cursor_copy(const MDB_cursor *csrc, MDB_cursor *cdst);
7817
7818 /** Perform \b act while tracking temporary cursor \b mn */
7819 #define WITH_CURSOR_TRACKING(mn, act) do { \
7820 MDB_cursor dummy, *tracked, **tp = &(mn).mc_txn->mt_cursors[mn.mc_dbi]; \
7821 if ((mn).mc_flags & C_SUB) { \
7822 dummy.mc_flags = C_INITIALIZED; \
7823 dummy.mc_xcursor = (MDB_xcursor *)&(mn); \
7824 tracked = &dummy; \
7825 } else { \
7826 tracked = &(mn); \
7827 } \
7828 tracked->mc_next = *tp; \
7829 *tp = tracked; \
7830 { act; } \
7831 *tp = tracked->mc_next; \
7832 } while (0)
7833
7834 /** Move a node from csrc to cdst.
7835 */
7836 static int
mdb_node_move(MDB_cursor * csrc,MDB_cursor * cdst,int fromleft)7837 mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst, int fromleft)
7838 {
7839 MDB_node *srcnode;
7840 MDB_val key, data;
7841 pgno_t srcpg;
7842 MDB_cursor mn;
7843 int rc;
7844 unsigned short flags;
7845
7846 DKBUF;
7847
7848 /* Mark src and dst as dirty. */
7849 if ((rc = mdb_page_touch(csrc)) ||
7850 (rc = mdb_page_touch(cdst)))
7851 return rc;
7852
7853 if (IS_LEAF2(csrc->mc_pg[csrc->mc_top])) {
7854 key.mv_size = csrc->mc_db->md_pad;
7855 key.mv_data = LEAF2KEY(csrc->mc_pg[csrc->mc_top], csrc->mc_ki[csrc->mc_top], key.mv_size);
7856 data.mv_size = 0;
7857 data.mv_data = NULL;
7858 srcpg = 0;
7859 flags = 0;
7860 } else {
7861 srcnode = NODEPTR(csrc->mc_pg[csrc->mc_top], csrc->mc_ki[csrc->mc_top]);
7862 mdb_cassert(csrc, !((size_t)srcnode & 1));
7863 srcpg = NODEPGNO(srcnode);
7864 flags = srcnode->mn_flags;
7865 if (csrc->mc_ki[csrc->mc_top] == 0 && IS_BRANCH(csrc->mc_pg[csrc->mc_top])) {
7866 unsigned int snum = csrc->mc_snum;
7867 MDB_node *s2;
7868 /* must find the lowest key below src */
7869 rc = mdb_page_search_lowest(csrc);
7870 if (rc)
7871 return rc;
7872 if (IS_LEAF2(csrc->mc_pg[csrc->mc_top])) {
7873 key.mv_size = csrc->mc_db->md_pad;
7874 key.mv_data = LEAF2KEY(csrc->mc_pg[csrc->mc_top], 0, key.mv_size);
7875 } else {
7876 s2 = NODEPTR(csrc->mc_pg[csrc->mc_top], 0);
7877 key.mv_size = NODEKSZ(s2);
7878 key.mv_data = NODEKEY(s2);
7879 }
7880 csrc->mc_snum = snum--;
7881 csrc->mc_top = snum;
7882 } else {
7883 key.mv_size = NODEKSZ(srcnode);
7884 key.mv_data = NODEKEY(srcnode);
7885 }
7886 data.mv_size = NODEDSZ(srcnode);
7887 data.mv_data = NODEDATA(srcnode);
7888 }
7889 mn.mc_xcursor = NULL;
7890 if (IS_BRANCH(cdst->mc_pg[cdst->mc_top]) && cdst->mc_ki[cdst->mc_top] == 0) {
7891 unsigned int snum = cdst->mc_snum;
7892 MDB_node *s2;
7893 MDB_val bkey;
7894 /* must find the lowest key below dst */
7895 mdb_cursor_copy(cdst, &mn);
7896 rc = mdb_page_search_lowest(&mn);
7897 if (rc)
7898 return rc;
7899 if (IS_LEAF2(mn.mc_pg[mn.mc_top])) {
7900 bkey.mv_size = mn.mc_db->md_pad;
7901 bkey.mv_data = LEAF2KEY(mn.mc_pg[mn.mc_top], 0, bkey.mv_size);
7902 } else {
7903 s2 = NODEPTR(mn.mc_pg[mn.mc_top], 0);
7904 bkey.mv_size = NODEKSZ(s2);
7905 bkey.mv_data = NODEKEY(s2);
7906 }
7907 mn.mc_snum = snum--;
7908 mn.mc_top = snum;
7909 mn.mc_ki[snum] = 0;
7910 rc = mdb_update_key(&mn, &bkey);
7911 if (rc)
7912 return rc;
7913 }
7914
7915 DPRINTF(("moving %s node %u [%s] on page %"Z"u to node %u on page %"Z"u",
7916 IS_LEAF(csrc->mc_pg[csrc->mc_top]) ? "leaf" : "branch",
7917 csrc->mc_ki[csrc->mc_top],
7918 DKEY(&key),
7919 csrc->mc_pg[csrc->mc_top]->mp_pgno,
7920 cdst->mc_ki[cdst->mc_top], cdst->mc_pg[cdst->mc_top]->mp_pgno));
7921
7922 /* Add the node to the destination page.
7923 */
7924 rc = mdb_node_add(cdst, cdst->mc_ki[cdst->mc_top], &key, &data, srcpg, flags);
7925 if (rc != MDB_SUCCESS)
7926 return rc;
7927
7928 /* Delete the node from the source page.
7929 */
7930 mdb_node_del(csrc, key.mv_size);
7931
7932 {
7933 /* Adjust other cursors pointing to mp */
7934 MDB_cursor *m2, *m3;
7935 MDB_dbi dbi = csrc->mc_dbi;
7936 MDB_page *mpd, *mps;
7937
7938 mps = csrc->mc_pg[csrc->mc_top];
7939 /* If we're adding on the left, bump others up */
7940 if (fromleft) {
7941 mpd = cdst->mc_pg[csrc->mc_top];
7942 for (m2 = csrc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
7943 if (csrc->mc_flags & C_SUB)
7944 m3 = &m2->mc_xcursor->mx_cursor;
7945 else
7946 m3 = m2;
7947 if (!(m3->mc_flags & C_INITIALIZED) || m3->mc_top < csrc->mc_top)
7948 continue;
7949 if (m3 != cdst &&
7950 m3->mc_pg[csrc->mc_top] == mpd &&
7951 m3->mc_ki[csrc->mc_top] >= cdst->mc_ki[csrc->mc_top]) {
7952 m3->mc_ki[csrc->mc_top]++;
7953 }
7954 if (m3 !=csrc &&
7955 m3->mc_pg[csrc->mc_top] == mps &&
7956 m3->mc_ki[csrc->mc_top] == csrc->mc_ki[csrc->mc_top]) {
7957 m3->mc_pg[csrc->mc_top] = cdst->mc_pg[cdst->mc_top];
7958 m3->mc_ki[csrc->mc_top] = cdst->mc_ki[cdst->mc_top];
7959 m3->mc_ki[csrc->mc_top-1]++;
7960 }
7961 if (IS_LEAF(mps))
7962 XCURSOR_REFRESH(m3, csrc->mc_top, m3->mc_pg[csrc->mc_top]);
7963 }
7964 } else
7965 /* Adding on the right, bump others down */
7966 {
7967 for (m2 = csrc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
7968 if (csrc->mc_flags & C_SUB)
7969 m3 = &m2->mc_xcursor->mx_cursor;
7970 else
7971 m3 = m2;
7972 if (m3 == csrc) continue;
7973 if (!(m3->mc_flags & C_INITIALIZED) || m3->mc_top < csrc->mc_top)
7974 continue;
7975 if (m3->mc_pg[csrc->mc_top] == mps) {
7976 if (!m3->mc_ki[csrc->mc_top]) {
7977 m3->mc_pg[csrc->mc_top] = cdst->mc_pg[cdst->mc_top];
7978 m3->mc_ki[csrc->mc_top] = cdst->mc_ki[cdst->mc_top];
7979 m3->mc_ki[csrc->mc_top-1]--;
7980 } else {
7981 m3->mc_ki[csrc->mc_top]--;
7982 }
7983 if (IS_LEAF(mps))
7984 XCURSOR_REFRESH(m3, csrc->mc_top, m3->mc_pg[csrc->mc_top]);
7985 }
7986 }
7987 }
7988 }
7989
7990 /* Update the parent separators.
7991 */
7992 if (csrc->mc_ki[csrc->mc_top] == 0) {
7993 if (csrc->mc_ki[csrc->mc_top-1] != 0) {
7994 if (IS_LEAF2(csrc->mc_pg[csrc->mc_top])) {
7995 key.mv_data = LEAF2KEY(csrc->mc_pg[csrc->mc_top], 0, key.mv_size);
7996 } else {
7997 srcnode = NODEPTR(csrc->mc_pg[csrc->mc_top], 0);
7998 key.mv_size = NODEKSZ(srcnode);
7999 key.mv_data = NODEKEY(srcnode);
8000 }
8001 DPRINTF(("update separator for source page %"Z"u to [%s]",
8002 csrc->mc_pg[csrc->mc_top]->mp_pgno, DKEY(&key)));
8003 mdb_cursor_copy(csrc, &mn);
8004 mn.mc_snum--;
8005 mn.mc_top--;
8006 /* We want mdb_rebalance to find mn when doing fixups */
8007 WITH_CURSOR_TRACKING(mn,
8008 rc = mdb_update_key(&mn, &key));
8009 if (rc)
8010 return rc;
8011 }
8012 if (IS_BRANCH(csrc->mc_pg[csrc->mc_top])) {
8013 MDB_val nullkey;
8014 indx_t ix = csrc->mc_ki[csrc->mc_top];
8015 nullkey.mv_size = 0;
8016 csrc->mc_ki[csrc->mc_top] = 0;
8017 rc = mdb_update_key(csrc, &nullkey);
8018 csrc->mc_ki[csrc->mc_top] = ix;
8019 mdb_cassert(csrc, rc == MDB_SUCCESS);
8020 }
8021 }
8022
8023 if (cdst->mc_ki[cdst->mc_top] == 0) {
8024 if (cdst->mc_ki[cdst->mc_top-1] != 0) {
8025 if (IS_LEAF2(csrc->mc_pg[csrc->mc_top])) {
8026 key.mv_data = LEAF2KEY(cdst->mc_pg[cdst->mc_top], 0, key.mv_size);
8027 } else {
8028 srcnode = NODEPTR(cdst->mc_pg[cdst->mc_top], 0);
8029 key.mv_size = NODEKSZ(srcnode);
8030 key.mv_data = NODEKEY(srcnode);
8031 }
8032 DPRINTF(("update separator for destination page %"Z"u to [%s]",
8033 cdst->mc_pg[cdst->mc_top]->mp_pgno, DKEY(&key)));
8034 mdb_cursor_copy(cdst, &mn);
8035 mn.mc_snum--;
8036 mn.mc_top--;
8037 /* We want mdb_rebalance to find mn when doing fixups */
8038 WITH_CURSOR_TRACKING(mn,
8039 rc = mdb_update_key(&mn, &key));
8040 if (rc)
8041 return rc;
8042 }
8043 if (IS_BRANCH(cdst->mc_pg[cdst->mc_top])) {
8044 MDB_val nullkey;
8045 indx_t ix = cdst->mc_ki[cdst->mc_top];
8046 nullkey.mv_size = 0;
8047 cdst->mc_ki[cdst->mc_top] = 0;
8048 rc = mdb_update_key(cdst, &nullkey);
8049 cdst->mc_ki[cdst->mc_top] = ix;
8050 mdb_cassert(cdst, rc == MDB_SUCCESS);
8051 }
8052 }
8053
8054 return MDB_SUCCESS;
8055 }
8056
8057 /** Merge one page into another.
8058 * The nodes from the page pointed to by \b csrc will
8059 * be copied to the page pointed to by \b cdst and then
8060 * the \b csrc page will be freed.
8061 * @param[in] csrc Cursor pointing to the source page.
8062 * @param[in] cdst Cursor pointing to the destination page.
8063 * @return 0 on success, non-zero on failure.
8064 */
8065 static int
mdb_page_merge(MDB_cursor * csrc,MDB_cursor * cdst)8066 mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
8067 {
8068 MDB_page *psrc, *pdst;
8069 MDB_node *srcnode;
8070 MDB_val key, data;
8071 unsigned nkeys;
8072 int rc;
8073 indx_t i, j;
8074
8075 psrc = csrc->mc_pg[csrc->mc_top];
8076 pdst = cdst->mc_pg[cdst->mc_top];
8077
8078 DPRINTF(("merging page %"Z"u into %"Z"u", psrc->mp_pgno, pdst->mp_pgno));
8079
8080 mdb_cassert(csrc, csrc->mc_snum > 1); /* can't merge root page */
8081 mdb_cassert(csrc, cdst->mc_snum > 1);
8082
8083 /* Mark dst as dirty. */
8084 if ((rc = mdb_page_touch(cdst)))
8085 return rc;
8086
8087 /* get dst page again now that we've touched it. */
8088 pdst = cdst->mc_pg[cdst->mc_top];
8089
8090 /* Move all nodes from src to dst.
8091 */
8092 j = nkeys = NUMKEYS(pdst);
8093 if (IS_LEAF2(psrc)) {
8094 key.mv_size = csrc->mc_db->md_pad;
8095 key.mv_data = METADATA(psrc);
8096 for (i = 0; i < NUMKEYS(psrc); i++, j++) {
8097 rc = mdb_node_add(cdst, j, &key, NULL, 0, 0);
8098 if (rc != MDB_SUCCESS)
8099 return rc;
8100 key.mv_data = (char *)key.mv_data + key.mv_size;
8101 }
8102 } else {
8103 for (i = 0; i < NUMKEYS(psrc); i++, j++) {
8104 srcnode = NODEPTR(psrc, i);
8105 if (i == 0 && IS_BRANCH(psrc)) {
8106 MDB_cursor mn;
8107 MDB_node *s2;
8108 mdb_cursor_copy(csrc, &mn);
8109 mn.mc_xcursor = NULL;
8110 /* must find the lowest key below src */
8111 rc = mdb_page_search_lowest(&mn);
8112 if (rc)
8113 return rc;
8114 if (IS_LEAF2(mn.mc_pg[mn.mc_top])) {
8115 key.mv_size = mn.mc_db->md_pad;
8116 key.mv_data = LEAF2KEY(mn.mc_pg[mn.mc_top], 0, key.mv_size);
8117 } else {
8118 s2 = NODEPTR(mn.mc_pg[mn.mc_top], 0);
8119 key.mv_size = NODEKSZ(s2);
8120 key.mv_data = NODEKEY(s2);
8121 }
8122 } else {
8123 key.mv_size = srcnode->mn_ksize;
8124 key.mv_data = NODEKEY(srcnode);
8125 }
8126
8127 data.mv_size = NODEDSZ(srcnode);
8128 data.mv_data = NODEDATA(srcnode);
8129 rc = mdb_node_add(cdst, j, &key, &data, NODEPGNO(srcnode), srcnode->mn_flags);
8130 if (rc != MDB_SUCCESS)
8131 return rc;
8132 }
8133 }
8134
8135 DPRINTF(("dst page %"Z"u now has %u keys (%.1f%% filled)",
8136 pdst->mp_pgno, NUMKEYS(pdst),
8137 (float)PAGEFILL(cdst->mc_txn->mt_env, pdst) / 10));
8138
8139 /* Unlink the src page from parent and add to free list.
8140 */
8141 csrc->mc_top--;
8142 mdb_node_del(csrc, 0);
8143 if (csrc->mc_ki[csrc->mc_top] == 0) {
8144 key.mv_size = 0;
8145 rc = mdb_update_key(csrc, &key);
8146 if (rc) {
8147 csrc->mc_top++;
8148 return rc;
8149 }
8150 }
8151 csrc->mc_top++;
8152
8153 psrc = csrc->mc_pg[csrc->mc_top];
8154 /* If not operating on FreeDB, allow this page to be reused
8155 * in this txn. Otherwise just add to free list.
8156 */
8157 rc = mdb_page_loose(csrc, psrc);
8158 if (rc)
8159 return rc;
8160 if (IS_LEAF(psrc))
8161 csrc->mc_db->md_leaf_pages--;
8162 else
8163 csrc->mc_db->md_branch_pages--;
8164 {
8165 /* Adjust other cursors pointing to mp */
8166 MDB_cursor *m2, *m3;
8167 MDB_dbi dbi = csrc->mc_dbi;
8168 unsigned int top = csrc->mc_top;
8169
8170 for (m2 = csrc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
8171 if (csrc->mc_flags & C_SUB)
8172 m3 = &m2->mc_xcursor->mx_cursor;
8173 else
8174 m3 = m2;
8175 if (m3 == csrc) continue;
8176 if (m3->mc_snum < csrc->mc_snum) continue;
8177 if (m3->mc_pg[top] == psrc) {
8178 m3->mc_pg[top] = pdst;
8179 m3->mc_ki[top] += nkeys;
8180 m3->mc_ki[top-1] = cdst->mc_ki[top-1];
8181 } else if (m3->mc_pg[top-1] == csrc->mc_pg[top-1] &&
8182 m3->mc_ki[top-1] > csrc->mc_ki[top-1]) {
8183 m3->mc_ki[top-1]--;
8184 }
8185 if (IS_LEAF(psrc))
8186 XCURSOR_REFRESH(m3, top, m3->mc_pg[top]);
8187 }
8188 }
8189 {
8190 unsigned int snum = cdst->mc_snum;
8191 uint16_t depth = cdst->mc_db->md_depth;
8192 mdb_cursor_pop(cdst);
8193 rc = mdb_rebalance(cdst);
8194 /* Did the tree height change? */
8195 if (depth != cdst->mc_db->md_depth)
8196 snum += cdst->mc_db->md_depth - depth;
8197 cdst->mc_snum = snum;
8198 cdst->mc_top = snum-1;
8199 }
8200 return rc;
8201 }
8202
8203 /** Copy the contents of a cursor.
8204 * @param[in] csrc The cursor to copy from.
8205 * @param[out] cdst The cursor to copy to.
8206 */
8207 static void
mdb_cursor_copy(const MDB_cursor * csrc,MDB_cursor * cdst)8208 mdb_cursor_copy(const MDB_cursor *csrc, MDB_cursor *cdst)
8209 {
8210 unsigned int i;
8211
8212 cdst->mc_txn = csrc->mc_txn;
8213 cdst->mc_dbi = csrc->mc_dbi;
8214 cdst->mc_db = csrc->mc_db;
8215 cdst->mc_dbx = csrc->mc_dbx;
8216 cdst->mc_snum = csrc->mc_snum;
8217 cdst->mc_top = csrc->mc_top;
8218 cdst->mc_flags = csrc->mc_flags;
8219
8220 for (i=0; i<csrc->mc_snum; i++) {
8221 cdst->mc_pg[i] = csrc->mc_pg[i];
8222 cdst->mc_ki[i] = csrc->mc_ki[i];
8223 }
8224 }
8225
8226 /** Rebalance the tree after a delete operation.
8227 * @param[in] mc Cursor pointing to the page where rebalancing
8228 * should begin.
8229 * @return 0 on success, non-zero on failure.
8230 */
8231 static int
mdb_rebalance(MDB_cursor * mc)8232 mdb_rebalance(MDB_cursor *mc)
8233 {
8234 MDB_node *node;
8235 int rc, fromleft;
8236 unsigned int ptop, minkeys, thresh;
8237 MDB_cursor mn;
8238 indx_t oldki;
8239
8240 if (IS_BRANCH(mc->mc_pg[mc->mc_top])) {
8241 minkeys = 2;
8242 thresh = 1;
8243 } else {
8244 minkeys = 1;
8245 thresh = FILL_THRESHOLD;
8246 }
8247 DPRINTF(("rebalancing %s page %"Z"u (has %u keys, %.1f%% full)",
8248 IS_LEAF(mc->mc_pg[mc->mc_top]) ? "leaf" : "branch",
8249 mdb_dbg_pgno(mc->mc_pg[mc->mc_top]), NUMKEYS(mc->mc_pg[mc->mc_top]),
8250 (float)PAGEFILL(mc->mc_txn->mt_env, mc->mc_pg[mc->mc_top]) / 10));
8251
8252 if (PAGEFILL(mc->mc_txn->mt_env, mc->mc_pg[mc->mc_top]) >= thresh &&
8253 NUMKEYS(mc->mc_pg[mc->mc_top]) >= minkeys) {
8254 DPRINTF(("no need to rebalance page %"Z"u, above fill threshold",
8255 mdb_dbg_pgno(mc->mc_pg[mc->mc_top])));
8256 return MDB_SUCCESS;
8257 }
8258
8259 if (mc->mc_snum < 2) {
8260 MDB_page *mp = mc->mc_pg[0];
8261 if (IS_SUBP(mp)) {
8262 DPUTS("Can't rebalance a subpage, ignoring");
8263 return MDB_SUCCESS;
8264 }
8265 if (NUMKEYS(mp) == 0) {
8266 DPUTS("tree is completely empty");
8267 mc->mc_db->md_root = P_INVALID;
8268 mc->mc_db->md_depth = 0;
8269 mc->mc_db->md_leaf_pages = 0;
8270 rc = mdb_midl_append(&mc->mc_txn->mt_free_pgs, mp->mp_pgno);
8271 if (rc)
8272 return rc;
8273 /* Adjust cursors pointing to mp */
8274 mc->mc_snum = 0;
8275 mc->mc_top = 0;
8276 mc->mc_flags &= ~C_INITIALIZED;
8277 {
8278 MDB_cursor *m2, *m3;
8279 MDB_dbi dbi = mc->mc_dbi;
8280
8281 for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
8282 if (mc->mc_flags & C_SUB)
8283 m3 = &m2->mc_xcursor->mx_cursor;
8284 else
8285 m3 = m2;
8286 if (!(m3->mc_flags & C_INITIALIZED) || (m3->mc_snum < mc->mc_snum))
8287 continue;
8288 if (m3->mc_pg[0] == mp) {
8289 m3->mc_snum = 0;
8290 m3->mc_top = 0;
8291 m3->mc_flags &= ~C_INITIALIZED;
8292 }
8293 }
8294 }
8295 } else if (IS_BRANCH(mp) && NUMKEYS(mp) == 1) {
8296 int i;
8297 DPUTS("collapsing root page!");
8298 rc = mdb_midl_append(&mc->mc_txn->mt_free_pgs, mp->mp_pgno);
8299 if (rc)
8300 return rc;
8301 mc->mc_db->md_root = NODEPGNO(NODEPTR(mp, 0));
8302 rc = mdb_page_get(mc, mc->mc_db->md_root, &mc->mc_pg[0], NULL);
8303 if (rc)
8304 return rc;
8305 mc->mc_db->md_depth--;
8306 mc->mc_db->md_branch_pages--;
8307 mc->mc_ki[0] = mc->mc_ki[1];
8308 for (i = 1; i<mc->mc_db->md_depth; i++) {
8309 mc->mc_pg[i] = mc->mc_pg[i+1];
8310 mc->mc_ki[i] = mc->mc_ki[i+1];
8311 }
8312 {
8313 /* Adjust other cursors pointing to mp */
8314 MDB_cursor *m2, *m3;
8315 MDB_dbi dbi = mc->mc_dbi;
8316
8317 for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
8318 if (mc->mc_flags & C_SUB)
8319 m3 = &m2->mc_xcursor->mx_cursor;
8320 else
8321 m3 = m2;
8322 if (m3 == mc) continue;
8323 if (!(m3->mc_flags & C_INITIALIZED))
8324 continue;
8325 if (m3->mc_pg[0] == mp) {
8326 for (i=0; i<mc->mc_db->md_depth; i++) {
8327 m3->mc_pg[i] = m3->mc_pg[i+1];
8328 m3->mc_ki[i] = m3->mc_ki[i+1];
8329 }
8330 m3->mc_snum--;
8331 m3->mc_top--;
8332 }
8333 }
8334 }
8335 } else
8336 DPUTS("root page doesn't need rebalancing");
8337 return MDB_SUCCESS;
8338 }
8339
8340 /* The parent (branch page) must have at least 2 pointers,
8341 * otherwise the tree is invalid.
8342 */
8343 ptop = mc->mc_top-1;
8344 mdb_cassert(mc, NUMKEYS(mc->mc_pg[ptop]) > 1);
8345
8346 /* Leaf page fill factor is below the threshold.
8347 * Try to move keys from left or right neighbor, or
8348 * merge with a neighbor page.
8349 */
8350
8351 /* Find neighbors.
8352 */
8353 mdb_cursor_copy(mc, &mn);
8354 mn.mc_xcursor = NULL;
8355
8356 oldki = mc->mc_ki[mc->mc_top];
8357 if (mc->mc_ki[ptop] == 0) {
8358 /* We're the leftmost leaf in our parent.
8359 */
8360 DPUTS("reading right neighbor");
8361 mn.mc_ki[ptop]++;
8362 node = NODEPTR(mc->mc_pg[ptop], mn.mc_ki[ptop]);
8363 rc = mdb_page_get(mc, NODEPGNO(node), &mn.mc_pg[mn.mc_top], NULL);
8364 if (rc)
8365 return rc;
8366 mn.mc_ki[mn.mc_top] = 0;
8367 mc->mc_ki[mc->mc_top] = NUMKEYS(mc->mc_pg[mc->mc_top]);
8368 fromleft = 0;
8369 } else {
8370 /* There is at least one neighbor to the left.
8371 */
8372 DPUTS("reading left neighbor");
8373 mn.mc_ki[ptop]--;
8374 node = NODEPTR(mc->mc_pg[ptop], mn.mc_ki[ptop]);
8375 rc = mdb_page_get(mc, NODEPGNO(node), &mn.mc_pg[mn.mc_top], NULL);
8376 if (rc)
8377 return rc;
8378 mn.mc_ki[mn.mc_top] = NUMKEYS(mn.mc_pg[mn.mc_top]) - 1;
8379 mc->mc_ki[mc->mc_top] = 0;
8380 fromleft = 1;
8381 }
8382
8383 DPRINTF(("found neighbor page %"Z"u (%u keys, %.1f%% full)",
8384 mn.mc_pg[mn.mc_top]->mp_pgno, NUMKEYS(mn.mc_pg[mn.mc_top]),
8385 (float)PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) / 10));
8386
8387 /* If the neighbor page is above threshold and has enough keys,
8388 * move one key from it. Otherwise we should try to merge them.
8389 * (A branch page must never have less than 2 keys.)
8390 */
8391 if (PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) >= thresh && NUMKEYS(mn.mc_pg[mn.mc_top]) > minkeys) {
8392 rc = mdb_node_move(&mn, mc, fromleft);
8393 if (fromleft) {
8394 /* if we inserted on left, bump position up */
8395 oldki++;
8396 }
8397 } else {
8398 if (!fromleft) {
8399 rc = mdb_page_merge(&mn, mc);
8400 } else {
8401 oldki += NUMKEYS(mn.mc_pg[mn.mc_top]);
8402 mn.mc_ki[mn.mc_top] += mc->mc_ki[mn.mc_top] + 1;
8403 /* We want mdb_rebalance to find mn when doing fixups */
8404 WITH_CURSOR_TRACKING(mn,
8405 rc = mdb_page_merge(mc, &mn));
8406 mdb_cursor_copy(&mn, mc);
8407 }
8408 mc->mc_flags &= ~C_EOF;
8409 }
8410 mc->mc_ki[mc->mc_top] = oldki;
8411 return rc;
8412 }
8413
8414 /** Complete a delete operation started by #mdb_cursor_del(). */
8415 static int
mdb_cursor_del0(MDB_cursor * mc)8416 mdb_cursor_del0(MDB_cursor *mc)
8417 {
8418 int rc;
8419 MDB_page *mp;
8420 indx_t ki;
8421 unsigned int nkeys;
8422 MDB_cursor *m2, *m3;
8423 MDB_dbi dbi = mc->mc_dbi;
8424
8425 ki = mc->mc_ki[mc->mc_top];
8426 mp = mc->mc_pg[mc->mc_top];
8427 mdb_node_del(mc, mc->mc_db->md_pad);
8428 mc->mc_db->md_entries--;
8429 {
8430 /* Adjust other cursors pointing to mp */
8431 for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
8432 m3 = (mc->mc_flags & C_SUB) ? &m2->mc_xcursor->mx_cursor : m2;
8433 if (! (m2->mc_flags & m3->mc_flags & C_INITIALIZED))
8434 continue;
8435 if (m3 == mc || m3->mc_snum < mc->mc_snum)
8436 continue;
8437 if (m3->mc_pg[mc->mc_top] == mp) {
8438 if (m3->mc_ki[mc->mc_top] == ki) {
8439 m3->mc_flags |= C_DEL;
8440 if (mc->mc_db->md_flags & MDB_DUPSORT) {
8441 /* Sub-cursor referred into dataset which is gone */
8442 m3->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
8443 }
8444 continue;
8445 } else if (m3->mc_ki[mc->mc_top] > ki) {
8446 m3->mc_ki[mc->mc_top]--;
8447 }
8448 XCURSOR_REFRESH(m3, mc->mc_top, mp);
8449 }
8450 }
8451 }
8452 rc = mdb_rebalance(mc);
8453
8454 if (rc == MDB_SUCCESS) {
8455 /* DB is totally empty now, just bail out.
8456 * Other cursors adjustments were already done
8457 * by mdb_rebalance and aren't needed here.
8458 */
8459 if (!mc->mc_snum)
8460 return rc;
8461
8462 mp = mc->mc_pg[mc->mc_top];
8463 nkeys = NUMKEYS(mp);
8464
8465 /* Adjust other cursors pointing to mp */
8466 for (m2 = mc->mc_txn->mt_cursors[dbi]; !rc && m2; m2=m2->mc_next) {
8467 m3 = (mc->mc_flags & C_SUB) ? &m2->mc_xcursor->mx_cursor : m2;
8468 if (! (m2->mc_flags & m3->mc_flags & C_INITIALIZED))
8469 continue;
8470 if (m3->mc_snum < mc->mc_snum)
8471 continue;
8472 if (m3->mc_pg[mc->mc_top] == mp) {
8473 /* if m3 points past last node in page, find next sibling */
8474 if (m3->mc_ki[mc->mc_top] >= mc->mc_ki[mc->mc_top]) {
8475 if (m3->mc_ki[mc->mc_top] >= nkeys) {
8476 rc = mdb_cursor_sibling(m3, 1);
8477 if (rc == MDB_NOTFOUND) {
8478 m3->mc_flags |= C_EOF;
8479 rc = MDB_SUCCESS;
8480 continue;
8481 }
8482 }
8483 if (mc->mc_db->md_flags & MDB_DUPSORT) {
8484 MDB_node *node = NODEPTR(m3->mc_pg[m3->mc_top], m3->mc_ki[m3->mc_top]);
8485 /* If this node has dupdata, it may need to be reinited
8486 * because its data has moved.
8487 * If the xcursor was not initd it must be reinited.
8488 * Else if node points to a subDB, nothing is needed.
8489 * Else (xcursor was initd, not a subDB) needs mc_pg[0] reset.
8490 */
8491 if (node->mn_flags & F_DUPDATA) {
8492 if (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) {
8493 if (!(node->mn_flags & F_SUBDATA))
8494 m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
8495 } else {
8496 mdb_xcursor_init1(m3, node);
8497 m3->mc_xcursor->mx_cursor.mc_flags |= C_DEL;
8498 }
8499 }
8500 }
8501 }
8502 }
8503 }
8504 mc->mc_flags |= C_DEL;
8505 }
8506
8507 if (rc)
8508 mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
8509 return rc;
8510 }
8511
8512 int
mdb_del(MDB_txn * txn,MDB_dbi dbi,MDB_val * key,MDB_val * data)8513 mdb_del(MDB_txn *txn, MDB_dbi dbi,
8514 MDB_val *key, MDB_val *data)
8515 {
8516 if (!key || !TXN_DBI_EXIST(txn, dbi, DB_USRVALID))
8517 return EINVAL;
8518
8519 if (txn->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_BLOCKED))
8520 return (txn->mt_flags & MDB_TXN_RDONLY) ? EACCES : MDB_BAD_TXN;
8521
8522 if (!F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
8523 /* must ignore any data */
8524 data = NULL;
8525 }
8526
8527 return mdb_del0(txn, dbi, key, data, 0);
8528 }
8529
8530 static int
mdb_del0(MDB_txn * txn,MDB_dbi dbi,MDB_val * key,MDB_val * data,unsigned flags)8531 mdb_del0(MDB_txn *txn, MDB_dbi dbi,
8532 MDB_val *key, MDB_val *data, unsigned flags)
8533 {
8534 MDB_cursor mc;
8535 MDB_xcursor mx;
8536 MDB_cursor_op op;
8537 MDB_val rdata, *xdata;
8538 int rc, exact = 0;
8539 DKBUF;
8540
8541 DPRINTF(("====> delete db %u key [%s]", dbi, DKEY(key)));
8542
8543 mdb_cursor_init(&mc, txn, dbi, &mx);
8544
8545 if (data) {
8546 op = MDB_GET_BOTH;
8547 rdata = *data;
8548 xdata = &rdata;
8549 } else {
8550 op = MDB_SET;
8551 xdata = NULL;
8552 flags |= MDB_NODUPDATA;
8553 }
8554 rc = mdb_cursor_set(&mc, key, xdata, op, &exact);
8555 if (rc == 0) {
8556 /* let mdb_page_split know about this cursor if needed:
8557 * delete will trigger a rebalance; if it needs to move
8558 * a node from one page to another, it will have to
8559 * update the parent's separator key(s). If the new sepkey
8560 * is larger than the current one, the parent page may
8561 * run out of space, triggering a split. We need this
8562 * cursor to be consistent until the end of the rebalance.
8563 */
8564 mc.mc_flags |= C_UNTRACK;
8565 mc.mc_next = txn->mt_cursors[dbi];
8566 txn->mt_cursors[dbi] = &mc;
8567 rc = mdb_cursor_del(&mc, flags);
8568 txn->mt_cursors[dbi] = mc.mc_next;
8569 }
8570 return rc;
8571 }
8572
8573 /** Split a page and insert a new node.
8574 * Set #MDB_TXN_ERROR on failure.
8575 * @param[in,out] mc Cursor pointing to the page and desired insertion index.
8576 * The cursor will be updated to point to the actual page and index where
8577 * the node got inserted after the split.
8578 * @param[in] newkey The key for the newly inserted node.
8579 * @param[in] newdata The data for the newly inserted node.
8580 * @param[in] newpgno The page number, if the new node is a branch node.
8581 * @param[in] nflags The #NODE_ADD_FLAGS for the new node.
8582 * @return 0 on success, non-zero on failure.
8583 */
8584 static int
mdb_page_split(MDB_cursor * mc,MDB_val * newkey,MDB_val * newdata,pgno_t newpgno,unsigned int nflags)8585 mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno,
8586 unsigned int nflags)
8587 {
8588 unsigned int flags;
8589 int rc = MDB_SUCCESS, new_root = 0, did_split = 0;
8590 indx_t newindx;
8591 pgno_t pgno = 0;
8592 int i, j, split_indx, nkeys, pmax;
8593 MDB_env *env = mc->mc_txn->mt_env;
8594 MDB_node *node;
8595 MDB_val sepkey, rkey, xdata, *rdata = &xdata;
8596 MDB_page *copy = NULL;
8597 MDB_page *mp, *rp, *pp;
8598 int ptop;
8599 MDB_cursor mn;
8600 DKBUF;
8601
8602 mp = mc->mc_pg[mc->mc_top];
8603 newindx = mc->mc_ki[mc->mc_top];
8604 nkeys = NUMKEYS(mp);
8605
8606 DPRINTF(("-----> splitting %s page %"Z"u and adding [%s] at index %i/%i",
8607 IS_LEAF(mp) ? "leaf" : "branch", mp->mp_pgno,
8608 DKEY(newkey), mc->mc_ki[mc->mc_top], nkeys));
8609
8610 /* Create a right sibling. */
8611 if ((rc = mdb_page_new(mc, mp->mp_flags, 1, &rp)))
8612 return rc;
8613 rp->mp_pad = mp->mp_pad;
8614 DPRINTF(("new right sibling: page %"Z"u", rp->mp_pgno));
8615
8616 /* Usually when splitting the root page, the cursor
8617 * height is 1. But when called from mdb_update_key,
8618 * the cursor height may be greater because it walks
8619 * up the stack while finding the branch slot to update.
8620 */
8621 if (mc->mc_top < 1) {
8622 if ((rc = mdb_page_new(mc, P_BRANCH, 1, &pp)))
8623 goto done;
8624 /* shift current top to make room for new parent */
8625 for (i=mc->mc_snum; i>0; i--) {
8626 mc->mc_pg[i] = mc->mc_pg[i-1];
8627 mc->mc_ki[i] = mc->mc_ki[i-1];
8628 }
8629 mc->mc_pg[0] = pp;
8630 mc->mc_ki[0] = 0;
8631 mc->mc_db->md_root = pp->mp_pgno;
8632 DPRINTF(("root split! new root = %"Z"u", pp->mp_pgno));
8633 new_root = mc->mc_db->md_depth++;
8634
8635 /* Add left (implicit) pointer. */
8636 if ((rc = mdb_node_add(mc, 0, NULL, NULL, mp->mp_pgno, 0)) != MDB_SUCCESS) {
8637 /* undo the pre-push */
8638 mc->mc_pg[0] = mc->mc_pg[1];
8639 mc->mc_ki[0] = mc->mc_ki[1];
8640 mc->mc_db->md_root = mp->mp_pgno;
8641 mc->mc_db->md_depth--;
8642 goto done;
8643 }
8644 mc->mc_snum++;
8645 mc->mc_top++;
8646 ptop = 0;
8647 } else {
8648 ptop = mc->mc_top-1;
8649 DPRINTF(("parent branch page is %"Z"u", mc->mc_pg[ptop]->mp_pgno));
8650 }
8651
8652 mdb_cursor_copy(mc, &mn);
8653 mn.mc_xcursor = NULL;
8654 mn.mc_pg[mn.mc_top] = rp;
8655 mn.mc_ki[ptop] = mc->mc_ki[ptop]+1;
8656
8657 if (nflags & MDB_APPEND) {
8658 mn.mc_ki[mn.mc_top] = 0;
8659 sepkey = *newkey;
8660 split_indx = newindx;
8661 nkeys = 0;
8662 } else {
8663
8664 split_indx = (nkeys+1) / 2;
8665
8666 if (IS_LEAF2(rp)) {
8667 char *split, *ins;
8668 int x;
8669 unsigned int lsize, rsize, ksize;
8670 /* Move half of the keys to the right sibling */
8671 x = mc->mc_ki[mc->mc_top] - split_indx;
8672 ksize = mc->mc_db->md_pad;
8673 split = LEAF2KEY(mp, split_indx, ksize);
8674 rsize = (nkeys - split_indx) * ksize;
8675 lsize = (nkeys - split_indx) * sizeof(indx_t);
8676 mp->mp_lower -= lsize;
8677 rp->mp_lower += lsize;
8678 mp->mp_upper += rsize - lsize;
8679 rp->mp_upper -= rsize - lsize;
8680 sepkey.mv_size = ksize;
8681 if (newindx == split_indx) {
8682 sepkey.mv_data = newkey->mv_data;
8683 } else {
8684 sepkey.mv_data = split;
8685 }
8686 if (x<0) {
8687 ins = LEAF2KEY(mp, mc->mc_ki[mc->mc_top], ksize);
8688 memcpy(rp->mp_ptrs, split, rsize);
8689 sepkey.mv_data = rp->mp_ptrs;
8690 memmove(ins+ksize, ins, (split_indx - mc->mc_ki[mc->mc_top]) * ksize);
8691 memcpy(ins, newkey->mv_data, ksize);
8692 mp->mp_lower += sizeof(indx_t);
8693 mp->mp_upper -= ksize - sizeof(indx_t);
8694 } else {
8695 if (x)
8696 memcpy(rp->mp_ptrs, split, x * ksize);
8697 ins = LEAF2KEY(rp, x, ksize);
8698 memcpy(ins, newkey->mv_data, ksize);
8699 memcpy(ins+ksize, split + x * ksize, rsize - x * ksize);
8700 rp->mp_lower += sizeof(indx_t);
8701 rp->mp_upper -= ksize - sizeof(indx_t);
8702 mc->mc_ki[mc->mc_top] = x;
8703 }
8704 } else {
8705 int psize, nsize, k;
8706 /* Maximum free space in an empty page */
8707 pmax = env->me_psize - PAGEHDRSZ;
8708 if (IS_LEAF(mp))
8709 nsize = mdb_leaf_size(env, newkey, newdata);
8710 else
8711 nsize = mdb_branch_size(env, newkey);
8712 nsize = EVEN(nsize);
8713
8714 /* grab a page to hold a temporary copy */
8715 copy = mdb_page_malloc(mc->mc_txn, 1);
8716 if (copy == NULL) {
8717 rc = ENOMEM;
8718 goto done;
8719 }
8720 copy->mp_pgno = mp->mp_pgno;
8721 copy->mp_flags = mp->mp_flags;
8722 copy->mp_lower = (PAGEHDRSZ-PAGEBASE);
8723 copy->mp_upper = env->me_psize - PAGEBASE;
8724
8725 /* prepare to insert */
8726 for (i=0, j=0; i<nkeys; i++) {
8727 if (i == newindx) {
8728 copy->mp_ptrs[j++] = 0;
8729 }
8730 copy->mp_ptrs[j++] = mp->mp_ptrs[i];
8731 }
8732
8733 /* When items are relatively large the split point needs
8734 * to be checked, because being off-by-one will make the
8735 * difference between success or failure in mdb_node_add.
8736 *
8737 * It's also relevant if a page happens to be laid out
8738 * such that one half of its nodes are all "small" and
8739 * the other half of its nodes are "large." If the new
8740 * item is also "large" and falls on the half with
8741 * "large" nodes, it also may not fit.
8742 *
8743 * As a final tweak, if the new item goes on the last
8744 * spot on the page (and thus, onto the new page), bias
8745 * the split so the new page is emptier than the old page.
8746 * This yields better packing during sequential inserts.
8747 */
8748 if (nkeys < 32 || nsize > pmax/16 || newindx >= nkeys) {
8749 /* Find split point */
8750 psize = 0;
8751 if (newindx <= split_indx || newindx >= nkeys) {
8752 i = 0; j = 1;
8753 k = newindx >= nkeys ? nkeys : split_indx+1+IS_LEAF(mp);
8754 } else {
8755 i = nkeys; j = -1;
8756 k = split_indx-1;
8757 }
8758 for (; i!=k; i+=j) {
8759 if (i == newindx) {
8760 psize += nsize;
8761 node = NULL;
8762 } else {
8763 node = (MDB_node *)((char *)mp + copy->mp_ptrs[i] + PAGEBASE);
8764 psize += NODESIZE + NODEKSZ(node) + sizeof(indx_t);
8765 if (IS_LEAF(mp)) {
8766 if (F_ISSET(node->mn_flags, F_BIGDATA))
8767 psize += sizeof(pgno_t);
8768 else
8769 psize += NODEDSZ(node);
8770 }
8771 psize = EVEN(psize);
8772 }
8773 if (psize > pmax || i == k-j) {
8774 split_indx = i + (j<0);
8775 break;
8776 }
8777 }
8778 }
8779 if (split_indx == newindx) {
8780 sepkey.mv_size = newkey->mv_size;
8781 sepkey.mv_data = newkey->mv_data;
8782 } else {
8783 node = (MDB_node *)((char *)mp + copy->mp_ptrs[split_indx] + PAGEBASE);
8784 sepkey.mv_size = node->mn_ksize;
8785 sepkey.mv_data = NODEKEY(node);
8786 }
8787 }
8788 }
8789
8790 DPRINTF(("separator is %d [%s]", split_indx, DKEY(&sepkey)));
8791
8792 /* Copy separator key to the parent.
8793 */
8794 if (SIZELEFT(mn.mc_pg[ptop]) < mdb_branch_size(env, &sepkey)) {
8795 int snum = mc->mc_snum;
8796 mn.mc_snum--;
8797 mn.mc_top--;
8798 did_split = 1;
8799 /* We want other splits to find mn when doing fixups */
8800 WITH_CURSOR_TRACKING(mn,
8801 rc = mdb_page_split(&mn, &sepkey, NULL, rp->mp_pgno, 0));
8802 if (rc)
8803 goto done;
8804
8805 /* root split? */
8806 if (mc->mc_snum > snum) {
8807 ptop++;
8808 }
8809 /* Right page might now have changed parent.
8810 * Check if left page also changed parent.
8811 */
8812 if (mn.mc_pg[ptop] != mc->mc_pg[ptop] &&
8813 mc->mc_ki[ptop] >= NUMKEYS(mc->mc_pg[ptop])) {
8814 for (i=0; i<ptop; i++) {
8815 mc->mc_pg[i] = mn.mc_pg[i];
8816 mc->mc_ki[i] = mn.mc_ki[i];
8817 }
8818 mc->mc_pg[ptop] = mn.mc_pg[ptop];
8819 if (mn.mc_ki[ptop]) {
8820 mc->mc_ki[ptop] = mn.mc_ki[ptop] - 1;
8821 } else {
8822 /* find right page's left sibling */
8823 mc->mc_ki[ptop] = mn.mc_ki[ptop];
8824 mdb_cursor_sibling(mc, 0);
8825 }
8826 }
8827 } else {
8828 mn.mc_top--;
8829 rc = mdb_node_add(&mn, mn.mc_ki[ptop], &sepkey, NULL, rp->mp_pgno, 0);
8830 mn.mc_top++;
8831 }
8832 if (rc != MDB_SUCCESS) {
8833 goto done;
8834 }
8835 if (nflags & MDB_APPEND) {
8836 mc->mc_pg[mc->mc_top] = rp;
8837 mc->mc_ki[mc->mc_top] = 0;
8838 rc = mdb_node_add(mc, 0, newkey, newdata, newpgno, nflags);
8839 if (rc)
8840 goto done;
8841 for (i=0; i<mc->mc_top; i++)
8842 mc->mc_ki[i] = mn.mc_ki[i];
8843 } else if (!IS_LEAF2(mp)) {
8844 /* Move nodes */
8845 mc->mc_pg[mc->mc_top] = rp;
8846 i = split_indx;
8847 j = 0;
8848 do {
8849 if (i == newindx) {
8850 rkey.mv_data = newkey->mv_data;
8851 rkey.mv_size = newkey->mv_size;
8852 if (IS_LEAF(mp)) {
8853 rdata = newdata;
8854 } else
8855 pgno = newpgno;
8856 flags = nflags;
8857 /* Update index for the new key. */
8858 mc->mc_ki[mc->mc_top] = j;
8859 } else {
8860 node = (MDB_node *)((char *)mp + copy->mp_ptrs[i] + PAGEBASE);
8861 rkey.mv_data = NODEKEY(node);
8862 rkey.mv_size = node->mn_ksize;
8863 if (IS_LEAF(mp)) {
8864 xdata.mv_data = NODEDATA(node);
8865 xdata.mv_size = NODEDSZ(node);
8866 rdata = &xdata;
8867 } else
8868 pgno = NODEPGNO(node);
8869 flags = node->mn_flags;
8870 }
8871
8872 if (!IS_LEAF(mp) && j == 0) {
8873 /* First branch index doesn't need key data. */
8874 rkey.mv_size = 0;
8875 }
8876
8877 rc = mdb_node_add(mc, j, &rkey, rdata, pgno, flags);
8878 if (rc)
8879 goto done;
8880 if (i == nkeys) {
8881 i = 0;
8882 j = 0;
8883 mc->mc_pg[mc->mc_top] = copy;
8884 } else {
8885 i++;
8886 j++;
8887 }
8888 } while (i != split_indx);
8889
8890 nkeys = NUMKEYS(copy);
8891 for (i=0; i<nkeys; i++)
8892 mp->mp_ptrs[i] = copy->mp_ptrs[i];
8893 mp->mp_lower = copy->mp_lower;
8894 mp->mp_upper = copy->mp_upper;
8895 memcpy(NODEPTR(mp, nkeys-1), NODEPTR(copy, nkeys-1),
8896 env->me_psize - copy->mp_upper - PAGEBASE);
8897
8898 /* reset back to original page */
8899 if (newindx < split_indx) {
8900 mc->mc_pg[mc->mc_top] = mp;
8901 } else {
8902 mc->mc_pg[mc->mc_top] = rp;
8903 mc->mc_ki[ptop]++;
8904 /* Make sure mc_ki is still valid.
8905 */
8906 if (mn.mc_pg[ptop] != mc->mc_pg[ptop] &&
8907 mc->mc_ki[ptop] >= NUMKEYS(mc->mc_pg[ptop])) {
8908 for (i=0; i<=ptop; i++) {
8909 mc->mc_pg[i] = mn.mc_pg[i];
8910 mc->mc_ki[i] = mn.mc_ki[i];
8911 }
8912 }
8913 }
8914 if (nflags & MDB_RESERVE) {
8915 node = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
8916 if (!(node->mn_flags & F_BIGDATA))
8917 newdata->mv_data = NODEDATA(node);
8918 }
8919 } else {
8920 if (newindx >= split_indx) {
8921 mc->mc_pg[mc->mc_top] = rp;
8922 mc->mc_ki[ptop]++;
8923 /* Make sure mc_ki is still valid.
8924 */
8925 if (mn.mc_pg[ptop] != mc->mc_pg[ptop] &&
8926 mc->mc_ki[ptop] >= NUMKEYS(mc->mc_pg[ptop])) {
8927 for (i=0; i<=ptop; i++) {
8928 mc->mc_pg[i] = mn.mc_pg[i];
8929 mc->mc_ki[i] = mn.mc_ki[i];
8930 }
8931 }
8932 }
8933 }
8934
8935 {
8936 /* Adjust other cursors pointing to mp */
8937 MDB_cursor *m2, *m3;
8938 MDB_dbi dbi = mc->mc_dbi;
8939 nkeys = NUMKEYS(mp);
8940
8941 for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
8942 if (mc->mc_flags & C_SUB)
8943 m3 = &m2->mc_xcursor->mx_cursor;
8944 else
8945 m3 = m2;
8946 if (m3 == mc)
8947 continue;
8948 if (!(m2->mc_flags & m3->mc_flags & C_INITIALIZED))
8949 continue;
8950 if (new_root) {
8951 int k;
8952 /* sub cursors may be on different DB */
8953 if (m3->mc_pg[0] != mp)
8954 continue;
8955 /* root split */
8956 for (k=new_root; k>=0; k--) {
8957 m3->mc_ki[k+1] = m3->mc_ki[k];
8958 m3->mc_pg[k+1] = m3->mc_pg[k];
8959 }
8960 if (m3->mc_ki[0] >= nkeys) {
8961 m3->mc_ki[0] = 1;
8962 } else {
8963 m3->mc_ki[0] = 0;
8964 }
8965 m3->mc_pg[0] = mc->mc_pg[0];
8966 m3->mc_snum++;
8967 m3->mc_top++;
8968 }
8969 if (m3->mc_top >= mc->mc_top && m3->mc_pg[mc->mc_top] == mp) {
8970 if (m3->mc_ki[mc->mc_top] >= newindx && !(nflags & MDB_SPLIT_REPLACE))
8971 m3->mc_ki[mc->mc_top]++;
8972 if (m3->mc_ki[mc->mc_top] >= nkeys) {
8973 m3->mc_pg[mc->mc_top] = rp;
8974 m3->mc_ki[mc->mc_top] -= nkeys;
8975 for (i=0; i<mc->mc_top; i++) {
8976 m3->mc_ki[i] = mn.mc_ki[i];
8977 m3->mc_pg[i] = mn.mc_pg[i];
8978 }
8979 }
8980 } else if (!did_split && m3->mc_top >= ptop && m3->mc_pg[ptop] == mc->mc_pg[ptop] &&
8981 m3->mc_ki[ptop] >= mc->mc_ki[ptop]) {
8982 m3->mc_ki[ptop]++;
8983 }
8984 if (IS_LEAF(mp))
8985 XCURSOR_REFRESH(m3, mc->mc_top, m3->mc_pg[mc->mc_top]);
8986 }
8987 }
8988 DPRINTF(("mp left: %d, rp left: %d", SIZELEFT(mp), SIZELEFT(rp)));
8989
8990 done:
8991 if (copy) /* tmp page */
8992 mdb_page_free(env, copy);
8993 if (rc)
8994 mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
8995 return rc;
8996 }
8997
8998 int
mdb_put(MDB_txn * txn,MDB_dbi dbi,MDB_val * key,MDB_val * data,unsigned int flags)8999 mdb_put(MDB_txn *txn, MDB_dbi dbi,
9000 MDB_val *key, MDB_val *data, unsigned int flags)
9001 {
9002 MDB_cursor mc;
9003 MDB_xcursor mx;
9004 int rc;
9005
9006 if (!key || !data || !TXN_DBI_EXIST(txn, dbi, DB_USRVALID))
9007 return EINVAL;
9008
9009 if (flags & ~(MDB_NOOVERWRITE|MDB_NODUPDATA|MDB_RESERVE|MDB_APPEND|MDB_APPENDDUP))
9010 return EINVAL;
9011
9012 if (txn->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_BLOCKED))
9013 return (txn->mt_flags & MDB_TXN_RDONLY) ? EACCES : MDB_BAD_TXN;
9014
9015 mdb_cursor_init(&mc, txn, dbi, &mx);
9016 mc.mc_next = txn->mt_cursors[dbi];
9017 txn->mt_cursors[dbi] = &mc;
9018 rc = mdb_cursor_put(&mc, key, data, flags);
9019 txn->mt_cursors[dbi] = mc.mc_next;
9020 return rc;
9021 }
9022
9023 #ifndef MDB_WBUF
9024 #define MDB_WBUF (1024*1024)
9025 #endif
9026 #define MDB_EOF 0x10 /**< #mdb_env_copyfd1() is done reading */
9027
9028 /** State needed for a double-buffering compacting copy. */
9029 typedef struct mdb_copy {
9030 MDB_env *mc_env;
9031 MDB_txn *mc_txn;
9032 pthread_mutex_t mc_mutex;
9033 pthread_cond_t mc_cond; /**< Condition variable for #mc_new */
9034 char *mc_wbuf[2];
9035 char *mc_over[2];
9036 int mc_wlen[2];
9037 int mc_olen[2];
9038 pgno_t mc_next_pgno;
9039 HANDLE mc_fd;
9040 int mc_toggle; /**< Buffer number in provider */
9041 int mc_new; /**< (0-2 buffers to write) | (#MDB_EOF at end) */
9042 /** Error code. Never cleared if set. Both threads can set nonzero
9043 * to fail the copy. Not mutex-protected, LMDB expects atomic int.
9044 */
9045 volatile int mc_error;
9046 } mdb_copy;
9047
9048 /** Dedicated writer thread for compacting copy. */
9049 static THREAD_RET ESECT CALL_CONV
mdb_env_copythr(void * arg)9050 mdb_env_copythr(void *arg)
9051 {
9052 mdb_copy *my = arg;
9053 char *ptr;
9054 int toggle = 0, wsize, rc;
9055 #ifdef _WIN32
9056 DWORD len;
9057 #define DO_WRITE(rc, fd, ptr, w2, len) rc = WriteFile(fd, ptr, w2, &len, NULL)
9058 #else
9059 int len;
9060 #define DO_WRITE(rc, fd, ptr, w2, len) len = write(fd, ptr, w2); rc = (len >= 0)
9061 #ifdef SIGPIPE
9062 sigset_t set;
9063 sigemptyset(&set);
9064 sigaddset(&set, SIGPIPE);
9065 if ((rc = pthread_sigmask(SIG_BLOCK, &set, NULL)) != 0)
9066 my->mc_error = rc;
9067 #endif
9068 #endif
9069
9070 pthread_mutex_lock(&my->mc_mutex);
9071 for(;;) {
9072 while (!my->mc_new)
9073 pthread_cond_wait(&my->mc_cond, &my->mc_mutex);
9074 if (my->mc_new == 0 + MDB_EOF) /* 0 buffers, just EOF */
9075 break;
9076 wsize = my->mc_wlen[toggle];
9077 ptr = my->mc_wbuf[toggle];
9078 again:
9079 rc = MDB_SUCCESS;
9080 while (wsize > 0 && !my->mc_error) {
9081 DO_WRITE(rc, my->mc_fd, ptr, wsize, len);
9082 if (!rc) {
9083 rc = ErrCode();
9084 #if defined(SIGPIPE) && !defined(_WIN32)
9085 if (rc == EPIPE) {
9086 /* Collect the pending SIGPIPE, otherwise at least OS X
9087 * gives it to the process on thread-exit (ITS#8504).
9088 */
9089 int tmp;
9090 sigwait(&set, &tmp);
9091 }
9092 #endif
9093 break;
9094 } else if (len > 0) {
9095 rc = MDB_SUCCESS;
9096 ptr += len;
9097 wsize -= len;
9098 continue;
9099 } else {
9100 rc = EIO;
9101 break;
9102 }
9103 }
9104 if (rc) {
9105 my->mc_error = rc;
9106 }
9107 /* If there's an overflow page tail, write it too */
9108 if (my->mc_olen[toggle]) {
9109 wsize = my->mc_olen[toggle];
9110 ptr = my->mc_over[toggle];
9111 my->mc_olen[toggle] = 0;
9112 goto again;
9113 }
9114 my->mc_wlen[toggle] = 0;
9115 toggle ^= 1;
9116 /* Return the empty buffer to provider */
9117 my->mc_new--;
9118 pthread_cond_signal(&my->mc_cond);
9119 }
9120 pthread_mutex_unlock(&my->mc_mutex);
9121 return (THREAD_RET)0;
9122 #undef DO_WRITE
9123 }
9124
9125 /** Give buffer and/or #MDB_EOF to writer thread, await unused buffer.
9126 *
9127 * @param[in] my control structure.
9128 * @param[in] adjust (1 to hand off 1 buffer) | (MDB_EOF when ending).
9129 */
9130 static int ESECT
mdb_env_cthr_toggle(mdb_copy * my,int adjust)9131 mdb_env_cthr_toggle(mdb_copy *my, int adjust)
9132 {
9133 pthread_mutex_lock(&my->mc_mutex);
9134 my->mc_new += adjust;
9135 pthread_cond_signal(&my->mc_cond);
9136 while (my->mc_new & 2) /* both buffers in use */
9137 pthread_cond_wait(&my->mc_cond, &my->mc_mutex);
9138 pthread_mutex_unlock(&my->mc_mutex);
9139
9140 my->mc_toggle ^= (adjust & 1);
9141 /* Both threads reset mc_wlen, to be safe from threading errors */
9142 my->mc_wlen[my->mc_toggle] = 0;
9143 return my->mc_error;
9144 }
9145
9146 /** Depth-first tree traversal for compacting copy.
9147 * @param[in] my control structure.
9148 * @param[in,out] pg database root.
9149 * @param[in] flags includes #F_DUPDATA if it is a sorted-duplicate sub-DB.
9150 */
9151 static int ESECT
mdb_env_cwalk(mdb_copy * my,pgno_t * pg,int flags)9152 mdb_env_cwalk(mdb_copy *my, pgno_t *pg, int flags)
9153 {
9154 MDB_cursor mc = {0};
9155 MDB_node *ni;
9156 MDB_page *mo, *mp, *leaf;
9157 char *buf, *ptr;
9158 int rc, toggle;
9159 unsigned int i;
9160
9161 /* Empty DB, nothing to do */
9162 if (*pg == P_INVALID)
9163 return MDB_SUCCESS;
9164
9165 mc.mc_snum = 1;
9166 mc.mc_txn = my->mc_txn;
9167
9168 rc = mdb_page_get(&mc, *pg, &mc.mc_pg[0], NULL);
9169 if (rc)
9170 return rc;
9171 rc = mdb_page_search_root(&mc, NULL, MDB_PS_FIRST);
9172 if (rc)
9173 return rc;
9174
9175 /* Make cursor pages writable */
9176 buf = ptr = malloc(my->mc_env->me_psize * mc.mc_snum);
9177 if (buf == NULL)
9178 return ENOMEM;
9179
9180 for (i=0; i<mc.mc_top; i++) {
9181 mdb_page_copy((MDB_page *)ptr, mc.mc_pg[i], my->mc_env->me_psize);
9182 mc.mc_pg[i] = (MDB_page *)ptr;
9183 ptr += my->mc_env->me_psize;
9184 }
9185
9186 /* This is writable space for a leaf page. Usually not needed. */
9187 leaf = (MDB_page *)ptr;
9188
9189 toggle = my->mc_toggle;
9190 while (mc.mc_snum > 0) {
9191 unsigned n;
9192 mp = mc.mc_pg[mc.mc_top];
9193 n = NUMKEYS(mp);
9194
9195 if (IS_LEAF(mp)) {
9196 if (!IS_LEAF2(mp) && !(flags & F_DUPDATA)) {
9197 for (i=0; i<n; i++) {
9198 ni = NODEPTR(mp, i);
9199 if (ni->mn_flags & F_BIGDATA) {
9200 MDB_page *omp;
9201 pgno_t pg;
9202
9203 /* Need writable leaf */
9204 if (mp != leaf) {
9205 mc.mc_pg[mc.mc_top] = leaf;
9206 mdb_page_copy(leaf, mp, my->mc_env->me_psize);
9207 mp = leaf;
9208 ni = NODEPTR(mp, i);
9209 }
9210
9211 memcpy(&pg, NODEDATA(ni), sizeof(pg));
9212 memcpy(NODEDATA(ni), &my->mc_next_pgno, sizeof(pgno_t));
9213 rc = mdb_page_get(&mc, pg, &omp, NULL);
9214 if (rc)
9215 goto done;
9216 if (my->mc_wlen[toggle] >= MDB_WBUF) {
9217 rc = mdb_env_cthr_toggle(my, 1);
9218 if (rc)
9219 goto done;
9220 toggle = my->mc_toggle;
9221 }
9222 mo = (MDB_page *)(my->mc_wbuf[toggle] + my->mc_wlen[toggle]);
9223 memcpy(mo, omp, my->mc_env->me_psize);
9224 mo->mp_pgno = my->mc_next_pgno;
9225 my->mc_next_pgno += omp->mp_pages;
9226 my->mc_wlen[toggle] += my->mc_env->me_psize;
9227 if (omp->mp_pages > 1) {
9228 my->mc_olen[toggle] = my->mc_env->me_psize * (omp->mp_pages - 1);
9229 my->mc_over[toggle] = (char *)omp + my->mc_env->me_psize;
9230 rc = mdb_env_cthr_toggle(my, 1);
9231 if (rc)
9232 goto done;
9233 toggle = my->mc_toggle;
9234 }
9235 } else if (ni->mn_flags & F_SUBDATA) {
9236 MDB_db db;
9237
9238 /* Need writable leaf */
9239 if (mp != leaf) {
9240 mc.mc_pg[mc.mc_top] = leaf;
9241 mdb_page_copy(leaf, mp, my->mc_env->me_psize);
9242 mp = leaf;
9243 ni = NODEPTR(mp, i);
9244 }
9245
9246 memcpy(&db, NODEDATA(ni), sizeof(db));
9247 my->mc_toggle = toggle;
9248 rc = mdb_env_cwalk(my, &db.md_root, ni->mn_flags & F_DUPDATA);
9249 if (rc)
9250 goto done;
9251 toggle = my->mc_toggle;
9252 memcpy(NODEDATA(ni), &db, sizeof(db));
9253 }
9254 }
9255 }
9256 } else {
9257 mc.mc_ki[mc.mc_top]++;
9258 if (mc.mc_ki[mc.mc_top] < n) {
9259 pgno_t pg;
9260 again:
9261 ni = NODEPTR(mp, mc.mc_ki[mc.mc_top]);
9262 pg = NODEPGNO(ni);
9263 rc = mdb_page_get(&mc, pg, &mp, NULL);
9264 if (rc)
9265 goto done;
9266 mc.mc_top++;
9267 mc.mc_snum++;
9268 mc.mc_ki[mc.mc_top] = 0;
9269 if (IS_BRANCH(mp)) {
9270 /* Whenever we advance to a sibling branch page,
9271 * we must proceed all the way down to its first leaf.
9272 */
9273 mdb_page_copy(mc.mc_pg[mc.mc_top], mp, my->mc_env->me_psize);
9274 goto again;
9275 } else
9276 mc.mc_pg[mc.mc_top] = mp;
9277 continue;
9278 }
9279 }
9280 if (my->mc_wlen[toggle] >= MDB_WBUF) {
9281 rc = mdb_env_cthr_toggle(my, 1);
9282 if (rc)
9283 goto done;
9284 toggle = my->mc_toggle;
9285 }
9286 mo = (MDB_page *)(my->mc_wbuf[toggle] + my->mc_wlen[toggle]);
9287 mdb_page_copy(mo, mp, my->mc_env->me_psize);
9288 mo->mp_pgno = my->mc_next_pgno++;
9289 my->mc_wlen[toggle] += my->mc_env->me_psize;
9290 if (mc.mc_top) {
9291 /* Update parent if there is one */
9292 ni = NODEPTR(mc.mc_pg[mc.mc_top-1], mc.mc_ki[mc.mc_top-1]);
9293 SETPGNO(ni, mo->mp_pgno);
9294 mdb_cursor_pop(&mc);
9295 } else {
9296 /* Otherwise we're done */
9297 *pg = mo->mp_pgno;
9298 break;
9299 }
9300 }
9301 done:
9302 free(buf);
9303 return rc;
9304 }
9305
9306 /** Copy environment with compaction. */
9307 static int ESECT
mdb_env_copyfd1(MDB_env * env,HANDLE fd)9308 mdb_env_copyfd1(MDB_env *env, HANDLE fd)
9309 {
9310 MDB_meta *mm;
9311 MDB_page *mp;
9312 mdb_copy my = {0};
9313 MDB_txn *txn = NULL;
9314 pthread_t thr;
9315 pgno_t root, new_root;
9316 int rc = MDB_SUCCESS;
9317
9318 #ifdef _WIN32
9319 if (!(my.mc_mutex = CreateMutex(NULL, FALSE, NULL)) ||
9320 !(my.mc_cond = CreateEvent(NULL, FALSE, FALSE, NULL))) {
9321 rc = ErrCode();
9322 goto done;
9323 }
9324 my.mc_wbuf[0] = _aligned_malloc(MDB_WBUF*2, env->me_os_psize);
9325 if (my.mc_wbuf[0] == NULL) {
9326 /* _aligned_malloc() sets errno, but we use Windows error codes */
9327 rc = ERROR_NOT_ENOUGH_MEMORY;
9328 goto done;
9329 }
9330 #else
9331 if ((rc = pthread_mutex_init(&my.mc_mutex, NULL)) != 0)
9332 return rc;
9333 if ((rc = pthread_cond_init(&my.mc_cond, NULL)) != 0)
9334 goto done2;
9335 #ifdef HAVE_MEMALIGN
9336 my.mc_wbuf[0] = memalign(env->me_os_psize, MDB_WBUF*2);
9337 if (my.mc_wbuf[0] == NULL) {
9338 rc = errno;
9339 goto done;
9340 }
9341 #else
9342 {
9343 void *p;
9344 if ((rc = posix_memalign(&p, env->me_os_psize, MDB_WBUF*2)) != 0)
9345 goto done;
9346 my.mc_wbuf[0] = p;
9347 }
9348 #endif
9349 #endif
9350 memset(my.mc_wbuf[0], 0, MDB_WBUF*2);
9351 my.mc_wbuf[1] = my.mc_wbuf[0] + MDB_WBUF;
9352 my.mc_next_pgno = NUM_METAS;
9353 my.mc_env = env;
9354 my.mc_fd = fd;
9355 rc = THREAD_CREATE(thr, mdb_env_copythr, &my);
9356 if (rc)
9357 goto done;
9358
9359 rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
9360 if (rc)
9361 goto finish;
9362
9363 mp = (MDB_page *)my.mc_wbuf[0];
9364 memset(mp, 0, NUM_METAS * env->me_psize);
9365 mp->mp_pgno = 0;
9366 mp->mp_flags = P_META;
9367 mm = (MDB_meta *)METADATA(mp);
9368 mdb_env_init_meta0(env, mm);
9369 mm->mm_address = env->me_metas[0]->mm_address;
9370
9371 mp = (MDB_page *)(my.mc_wbuf[0] + env->me_psize);
9372 mp->mp_pgno = 1;
9373 mp->mp_flags = P_META;
9374 *(MDB_meta *)METADATA(mp) = *mm;
9375 mm = (MDB_meta *)METADATA(mp);
9376
9377 /* Set metapage 1 with current main DB */
9378 root = new_root = txn->mt_dbs[MAIN_DBI].md_root;
9379 if (root != P_INVALID) {
9380 /* Count free pages + freeDB pages. Subtract from last_pg
9381 * to find the new last_pg, which also becomes the new root.
9382 */
9383 MDB_ID freecount = 0;
9384 MDB_cursor mc;
9385 MDB_val key, data;
9386 mdb_cursor_init(&mc, txn, FREE_DBI, NULL);
9387 while ((rc = mdb_cursor_get(&mc, &key, &data, MDB_NEXT)) == 0)
9388 freecount += *(MDB_ID *)data.mv_data;
9389 if (rc != MDB_NOTFOUND)
9390 goto finish;
9391 freecount += txn->mt_dbs[FREE_DBI].md_branch_pages +
9392 txn->mt_dbs[FREE_DBI].md_leaf_pages +
9393 txn->mt_dbs[FREE_DBI].md_overflow_pages;
9394
9395 new_root = txn->mt_next_pgno - 1 - freecount;
9396 mm->mm_last_pg = new_root;
9397 mm->mm_dbs[MAIN_DBI] = txn->mt_dbs[MAIN_DBI];
9398 mm->mm_dbs[MAIN_DBI].md_root = new_root;
9399 } else {
9400 /* When the DB is empty, handle it specially to
9401 * fix any breakage like page leaks from ITS#8174.
9402 */
9403 mm->mm_dbs[MAIN_DBI].md_flags = txn->mt_dbs[MAIN_DBI].md_flags;
9404 }
9405 if (root != P_INVALID || mm->mm_dbs[MAIN_DBI].md_flags) {
9406 mm->mm_txnid = 1; /* use metapage 1 */
9407 }
9408
9409 my.mc_wlen[0] = env->me_psize * NUM_METAS;
9410 my.mc_txn = txn;
9411 rc = mdb_env_cwalk(&my, &root, 0);
9412 if (rc == MDB_SUCCESS && root != new_root) {
9413 rc = MDB_INCOMPATIBLE; /* page leak or corrupt DB */
9414 }
9415
9416 finish:
9417 if (rc)
9418 my.mc_error = rc;
9419 mdb_env_cthr_toggle(&my, 1 | MDB_EOF);
9420 rc = THREAD_FINISH(thr);
9421 mdb_txn_abort(txn);
9422
9423 done:
9424 #ifdef _WIN32
9425 if (my.mc_wbuf[0]) _aligned_free(my.mc_wbuf[0]);
9426 if (my.mc_cond) CloseHandle(my.mc_cond);
9427 if (my.mc_mutex) CloseHandle(my.mc_mutex);
9428 #else
9429 free(my.mc_wbuf[0]);
9430 pthread_cond_destroy(&my.mc_cond);
9431 done2:
9432 pthread_mutex_destroy(&my.mc_mutex);
9433 #endif
9434 return rc ? rc : my.mc_error;
9435 }
9436
9437 /** Copy environment as-is. */
9438 static int ESECT
mdb_env_copyfd0(MDB_env * env,HANDLE fd)9439 mdb_env_copyfd0(MDB_env *env, HANDLE fd)
9440 {
9441 MDB_txn *txn = NULL;
9442 mdb_mutexref_t wmutex = NULL;
9443 int rc;
9444 size_t wsize, w3;
9445 char *ptr;
9446 #ifdef _WIN32
9447 DWORD len, w2;
9448 #define DO_WRITE(rc, fd, ptr, w2, len) rc = WriteFile(fd, ptr, w2, &len, NULL)
9449 #else
9450 ssize_t len;
9451 size_t w2;
9452 #define DO_WRITE(rc, fd, ptr, w2, len) len = write(fd, ptr, w2); rc = (len >= 0)
9453 #endif
9454
9455 /* Do the lock/unlock of the reader mutex before starting the
9456 * write txn. Otherwise other read txns could block writers.
9457 */
9458 rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
9459 if (rc)
9460 return rc;
9461
9462 if (env->me_txns) {
9463 /* We must start the actual read txn after blocking writers */
9464 mdb_txn_end(txn, MDB_END_RESET_TMP);
9465
9466 /* Temporarily block writers until we snapshot the meta pages */
9467 wmutex = env->me_wmutex;
9468 if (LOCK_MUTEX(rc, env, wmutex))
9469 goto leave;
9470
9471 rc = mdb_txn_renew0(txn);
9472 if (rc) {
9473 UNLOCK_MUTEX(wmutex);
9474 goto leave;
9475 }
9476 }
9477
9478 wsize = env->me_psize * NUM_METAS;
9479 ptr = env->me_map;
9480 w2 = wsize;
9481 while (w2 > 0) {
9482 DO_WRITE(rc, fd, ptr, w2, len);
9483 if (!rc) {
9484 rc = ErrCode();
9485 break;
9486 } else if (len > 0) {
9487 rc = MDB_SUCCESS;
9488 ptr += len;
9489 w2 -= len;
9490 continue;
9491 } else {
9492 /* Non-blocking or async handles are not supported */
9493 rc = EIO;
9494 break;
9495 }
9496 }
9497 if (wmutex)
9498 UNLOCK_MUTEX(wmutex);
9499
9500 if (rc)
9501 goto leave;
9502
9503 w3 = txn->mt_next_pgno * env->me_psize;
9504 {
9505 size_t fsize = 0;
9506 if ((rc = mdb_fsize(env->me_fd, &fsize)))
9507 goto leave;
9508 if (w3 > fsize)
9509 w3 = fsize;
9510 }
9511 wsize = w3 - wsize;
9512 while (wsize > 0) {
9513 if (wsize > MAX_WRITE)
9514 w2 = MAX_WRITE;
9515 else
9516 w2 = wsize;
9517 DO_WRITE(rc, fd, ptr, w2, len);
9518 if (!rc) {
9519 rc = ErrCode();
9520 break;
9521 } else if (len > 0) {
9522 rc = MDB_SUCCESS;
9523 ptr += len;
9524 wsize -= len;
9525 continue;
9526 } else {
9527 rc = EIO;
9528 break;
9529 }
9530 }
9531
9532 leave:
9533 mdb_txn_abort(txn);
9534 return rc;
9535 }
9536
9537 int ESECT
mdb_env_copyfd2(MDB_env * env,HANDLE fd,unsigned int flags)9538 mdb_env_copyfd2(MDB_env *env, HANDLE fd, unsigned int flags)
9539 {
9540 if (flags & MDB_CP_COMPACT)
9541 return mdb_env_copyfd1(env, fd);
9542 else
9543 return mdb_env_copyfd0(env, fd);
9544 }
9545
9546 int ESECT
mdb_env_copyfd(MDB_env * env,HANDLE fd)9547 mdb_env_copyfd(MDB_env *env, HANDLE fd)
9548 {
9549 return mdb_env_copyfd2(env, fd, 0);
9550 }
9551
9552 int ESECT
mdb_env_copy2(MDB_env * env,const char * path,unsigned int flags)9553 mdb_env_copy2(MDB_env *env, const char *path, unsigned int flags)
9554 {
9555 int rc;
9556 MDB_name fname;
9557 HANDLE newfd = INVALID_HANDLE_VALUE;
9558
9559 rc = mdb_fname_init(path, env->me_flags | MDB_NOLOCK, &fname);
9560 if (rc == MDB_SUCCESS) {
9561 rc = mdb_fopen(env, &fname, MDB_O_COPY, 0666, &newfd);
9562 mdb_fname_destroy(fname);
9563 }
9564 if (rc == MDB_SUCCESS) {
9565 rc = mdb_env_copyfd2(env, newfd, flags);
9566 if (close(newfd) < 0 && rc == MDB_SUCCESS)
9567 rc = ErrCode();
9568 }
9569 return rc;
9570 }
9571
9572 int ESECT
mdb_env_copy(MDB_env * env,const char * path)9573 mdb_env_copy(MDB_env *env, const char *path)
9574 {
9575 return mdb_env_copy2(env, path, 0);
9576 }
9577
9578 int ESECT
mdb_env_set_flags(MDB_env * env,unsigned int flag,int onoff)9579 mdb_env_set_flags(MDB_env *env, unsigned int flag, int onoff)
9580 {
9581 if (flag & ~CHANGEABLE)
9582 return EINVAL;
9583 if (onoff)
9584 env->me_flags |= flag;
9585 else
9586 env->me_flags &= ~flag;
9587 return MDB_SUCCESS;
9588 }
9589
9590 int ESECT
mdb_env_get_flags(MDB_env * env,unsigned int * arg)9591 mdb_env_get_flags(MDB_env *env, unsigned int *arg)
9592 {
9593 if (!env || !arg)
9594 return EINVAL;
9595
9596 *arg = env->me_flags & (CHANGEABLE|CHANGELESS);
9597 return MDB_SUCCESS;
9598 }
9599
9600 int ESECT
mdb_env_set_userctx(MDB_env * env,void * ctx)9601 mdb_env_set_userctx(MDB_env *env, void *ctx)
9602 {
9603 if (!env)
9604 return EINVAL;
9605 env->me_userctx = ctx;
9606 return MDB_SUCCESS;
9607 }
9608
9609 void * ESECT
mdb_env_get_userctx(MDB_env * env)9610 mdb_env_get_userctx(MDB_env *env)
9611 {
9612 return env ? env->me_userctx : NULL;
9613 }
9614
9615 int ESECT
mdb_env_set_assert(MDB_env * env,MDB_assert_func * func)9616 mdb_env_set_assert(MDB_env *env, MDB_assert_func *func)
9617 {
9618 if (!env)
9619 return EINVAL;
9620 #ifndef NDEBUG
9621 env->me_assert_func = func;
9622 #endif
9623 return MDB_SUCCESS;
9624 }
9625
9626 int ESECT
mdb_env_get_path(MDB_env * env,const char ** arg)9627 mdb_env_get_path(MDB_env *env, const char **arg)
9628 {
9629 if (!env || !arg)
9630 return EINVAL;
9631
9632 *arg = env->me_path;
9633 return MDB_SUCCESS;
9634 }
9635
9636 int ESECT
mdb_env_get_fd(MDB_env * env,mdb_filehandle_t * arg)9637 mdb_env_get_fd(MDB_env *env, mdb_filehandle_t *arg)
9638 {
9639 if (!env || !arg)
9640 return EINVAL;
9641
9642 *arg = env->me_fd;
9643 return MDB_SUCCESS;
9644 }
9645
9646 /** Common code for #mdb_stat() and #mdb_env_stat().
9647 * @param[in] env the environment to operate in.
9648 * @param[in] db the #MDB_db record containing the stats to return.
9649 * @param[out] arg the address of an #MDB_stat structure to receive the stats.
9650 * @return 0, this function always succeeds.
9651 */
9652 static int ESECT
mdb_stat0(MDB_env * env,MDB_db * db,MDB_stat * arg)9653 mdb_stat0(MDB_env *env, MDB_db *db, MDB_stat *arg)
9654 {
9655 arg->ms_psize = env->me_psize;
9656 arg->ms_depth = db->md_depth;
9657 arg->ms_branch_pages = db->md_branch_pages;
9658 arg->ms_leaf_pages = db->md_leaf_pages;
9659 arg->ms_overflow_pages = db->md_overflow_pages;
9660 arg->ms_entries = db->md_entries;
9661
9662 return MDB_SUCCESS;
9663 }
9664
9665 int ESECT
mdb_env_stat(MDB_env * env,MDB_stat * arg)9666 mdb_env_stat(MDB_env *env, MDB_stat *arg)
9667 {
9668 MDB_meta *meta;
9669
9670 if (env == NULL || arg == NULL)
9671 return EINVAL;
9672
9673 meta = mdb_env_pick_meta(env);
9674
9675 return mdb_stat0(env, &meta->mm_dbs[MAIN_DBI], arg);
9676 }
9677
9678 int ESECT
mdb_env_info(MDB_env * env,MDB_envinfo * arg)9679 mdb_env_info(MDB_env *env, MDB_envinfo *arg)
9680 {
9681 MDB_meta *meta;
9682
9683 if (env == NULL || arg == NULL)
9684 return EINVAL;
9685
9686 meta = mdb_env_pick_meta(env);
9687 arg->me_mapaddr = meta->mm_address;
9688 arg->me_last_pgno = meta->mm_last_pg;
9689 arg->me_last_txnid = meta->mm_txnid;
9690
9691 arg->me_mapsize = env->me_mapsize;
9692 arg->me_maxreaders = env->me_maxreaders;
9693 arg->me_numreaders = env->me_txns ? env->me_txns->mti_numreaders : 0;
9694 return MDB_SUCCESS;
9695 }
9696
9697 /** Set the default comparison functions for a database.
9698 * Called immediately after a database is opened to set the defaults.
9699 * The user can then override them with #mdb_set_compare() or
9700 * #mdb_set_dupsort().
9701 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
9702 * @param[in] dbi A database handle returned by #mdb_dbi_open()
9703 */
9704 static void
mdb_default_cmp(MDB_txn * txn,MDB_dbi dbi)9705 mdb_default_cmp(MDB_txn *txn, MDB_dbi dbi)
9706 {
9707 uint16_t f = txn->mt_dbs[dbi].md_flags;
9708
9709 txn->mt_dbxs[dbi].md_cmp =
9710 (f & MDB_REVERSEKEY) ? mdb_cmp_memnr :
9711 (f & MDB_INTEGERKEY) ? mdb_cmp_cint : mdb_cmp_memn;
9712
9713 txn->mt_dbxs[dbi].md_dcmp =
9714 !(f & MDB_DUPSORT) ? 0 :
9715 ((f & MDB_INTEGERDUP)
9716 ? ((f & MDB_DUPFIXED) ? mdb_cmp_int : mdb_cmp_cint)
9717 : ((f & MDB_REVERSEDUP) ? mdb_cmp_memnr : mdb_cmp_memn));
9718 }
9719
mdb_dbi_open(MDB_txn * txn,const char * name,unsigned int flags,MDB_dbi * dbi)9720 int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
9721 {
9722 MDB_val key, data;
9723 MDB_dbi i;
9724 MDB_cursor mc;
9725 MDB_db dummy;
9726 int rc, dbflag, exact;
9727 unsigned int unused = 0, seq;
9728 char *namedup;
9729 size_t len;
9730
9731 if (flags & ~VALID_FLAGS)
9732 return EINVAL;
9733 if (txn->mt_flags & MDB_TXN_BLOCKED)
9734 return MDB_BAD_TXN;
9735
9736 /* main DB? */
9737 if (!name) {
9738 *dbi = MAIN_DBI;
9739 if (flags & PERSISTENT_FLAGS) {
9740 uint16_t f2 = flags & PERSISTENT_FLAGS;
9741 /* make sure flag changes get committed */
9742 if ((txn->mt_dbs[MAIN_DBI].md_flags | f2) != txn->mt_dbs[MAIN_DBI].md_flags) {
9743 txn->mt_dbs[MAIN_DBI].md_flags |= f2;
9744 txn->mt_flags |= MDB_TXN_DIRTY;
9745 }
9746 }
9747 mdb_default_cmp(txn, MAIN_DBI);
9748 return MDB_SUCCESS;
9749 }
9750
9751 if (txn->mt_dbxs[MAIN_DBI].md_cmp == NULL) {
9752 mdb_default_cmp(txn, MAIN_DBI);
9753 }
9754
9755 /* Is the DB already open? */
9756 len = strlen(name);
9757 for (i=CORE_DBS; i<txn->mt_numdbs; i++) {
9758 if (!txn->mt_dbxs[i].md_name.mv_size) {
9759 /* Remember this free slot */
9760 if (!unused) unused = i;
9761 continue;
9762 }
9763 if (len == txn->mt_dbxs[i].md_name.mv_size &&
9764 !strncmp(name, txn->mt_dbxs[i].md_name.mv_data, len)) {
9765 *dbi = i;
9766 return MDB_SUCCESS;
9767 }
9768 }
9769
9770 /* If no free slot and max hit, fail */
9771 if (!unused && txn->mt_numdbs >= txn->mt_env->me_maxdbs)
9772 return MDB_DBS_FULL;
9773
9774 /* Cannot mix named databases with some mainDB flags */
9775 if (txn->mt_dbs[MAIN_DBI].md_flags & (MDB_DUPSORT|MDB_INTEGERKEY))
9776 return (flags & MDB_CREATE) ? MDB_INCOMPATIBLE : MDB_NOTFOUND;
9777
9778 /* Find the DB info */
9779 dbflag = DB_NEW|DB_VALID|DB_USRVALID;
9780 exact = 0;
9781 key.mv_size = len;
9782 key.mv_data = (void *)name;
9783 mdb_cursor_init(&mc, txn, MAIN_DBI, NULL);
9784 rc = mdb_cursor_set(&mc, &key, &data, MDB_SET, &exact);
9785 if (rc == MDB_SUCCESS) {
9786 /* make sure this is actually a DB */
9787 MDB_node *node = NODEPTR(mc.mc_pg[mc.mc_top], mc.mc_ki[mc.mc_top]);
9788 if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) != F_SUBDATA)
9789 return MDB_INCOMPATIBLE;
9790 } else {
9791 if (rc != MDB_NOTFOUND || !(flags & MDB_CREATE))
9792 return rc;
9793 if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY))
9794 return EACCES;
9795 }
9796
9797 /* Done here so we cannot fail after creating a new DB */
9798 if ((namedup = strdup(name)) == NULL)
9799 return ENOMEM;
9800
9801 if (rc) {
9802 /* MDB_NOTFOUND and MDB_CREATE: Create new DB */
9803 data.mv_size = sizeof(MDB_db);
9804 data.mv_data = &dummy;
9805 memset(&dummy, 0, sizeof(dummy));
9806 dummy.md_root = P_INVALID;
9807 dummy.md_flags = flags & PERSISTENT_FLAGS;
9808 WITH_CURSOR_TRACKING(mc,
9809 rc = mdb_cursor_put(&mc, &key, &data, F_SUBDATA));
9810 dbflag |= DB_DIRTY;
9811 }
9812
9813 if (rc) {
9814 free(namedup);
9815 } else {
9816 /* Got info, register DBI in this txn */
9817 unsigned int slot = unused ? unused : txn->mt_numdbs;
9818 txn->mt_dbxs[slot].md_name.mv_data = namedup;
9819 txn->mt_dbxs[slot].md_name.mv_size = len;
9820 txn->mt_dbxs[slot].md_rel = NULL;
9821 txn->mt_dbflags[slot] = dbflag;
9822 /* txn-> and env-> are the same in read txns, use
9823 * tmp variable to avoid undefined assignment
9824 */
9825 seq = ++txn->mt_env->me_dbiseqs[slot];
9826 txn->mt_dbiseqs[slot] = seq;
9827
9828 memcpy(&txn->mt_dbs[slot], data.mv_data, sizeof(MDB_db));
9829 *dbi = slot;
9830 mdb_default_cmp(txn, slot);
9831 if (!unused) {
9832 txn->mt_numdbs++;
9833 }
9834 }
9835
9836 return rc;
9837 }
9838
9839 int ESECT
mdb_stat(MDB_txn * txn,MDB_dbi dbi,MDB_stat * arg)9840 mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *arg)
9841 {
9842 if (!arg || !TXN_DBI_EXIST(txn, dbi, DB_VALID))
9843 return EINVAL;
9844
9845 if (txn->mt_flags & MDB_TXN_BLOCKED)
9846 return MDB_BAD_TXN;
9847
9848 if (txn->mt_dbflags[dbi] & DB_STALE) {
9849 MDB_cursor mc;
9850 MDB_xcursor mx;
9851 /* Stale, must read the DB's root. cursor_init does it for us. */
9852 mdb_cursor_init(&mc, txn, dbi, &mx);
9853 }
9854 return mdb_stat0(txn->mt_env, &txn->mt_dbs[dbi], arg);
9855 }
9856
mdb_dbi_close(MDB_env * env,MDB_dbi dbi)9857 void mdb_dbi_close(MDB_env *env, MDB_dbi dbi)
9858 {
9859 char *ptr;
9860 if (dbi < CORE_DBS || dbi >= env->me_maxdbs)
9861 return;
9862 ptr = env->me_dbxs[dbi].md_name.mv_data;
9863 /* If there was no name, this was already closed */
9864 if (ptr) {
9865 env->me_dbxs[dbi].md_name.mv_data = NULL;
9866 env->me_dbxs[dbi].md_name.mv_size = 0;
9867 env->me_dbflags[dbi] = 0;
9868 env->me_dbiseqs[dbi]++;
9869 free(ptr);
9870 }
9871 }
9872
mdb_dbi_flags(MDB_txn * txn,MDB_dbi dbi,unsigned int * flags)9873 int mdb_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned int *flags)
9874 {
9875 /* We could return the flags for the FREE_DBI too but what's the point? */
9876 if (!TXN_DBI_EXIST(txn, dbi, DB_USRVALID))
9877 return EINVAL;
9878 *flags = txn->mt_dbs[dbi].md_flags & PERSISTENT_FLAGS;
9879 return MDB_SUCCESS;
9880 }
9881
9882 /** Add all the DB's pages to the free list.
9883 * @param[in] mc Cursor on the DB to free.
9884 * @param[in] subs non-Zero to check for sub-DBs in this DB.
9885 * @return 0 on success, non-zero on failure.
9886 */
9887 static int
mdb_drop0(MDB_cursor * mc,int subs)9888 mdb_drop0(MDB_cursor *mc, int subs)
9889 {
9890 int rc;
9891
9892 rc = mdb_page_search(mc, NULL, MDB_PS_FIRST);
9893 if (rc == MDB_SUCCESS) {
9894 MDB_txn *txn = mc->mc_txn;
9895 MDB_node *ni;
9896 MDB_cursor mx;
9897 unsigned int i;
9898
9899 /* DUPSORT sub-DBs have no ovpages/DBs. Omit scanning leaves.
9900 * This also avoids any P_LEAF2 pages, which have no nodes.
9901 * Also if the DB doesn't have sub-DBs and has no overflow
9902 * pages, omit scanning leaves.
9903 */
9904 if ((mc->mc_flags & C_SUB) ||
9905 (!subs && !mc->mc_db->md_overflow_pages))
9906 mdb_cursor_pop(mc);
9907
9908 mdb_cursor_copy(mc, &mx);
9909 while (mc->mc_snum > 0) {
9910 MDB_page *mp = mc->mc_pg[mc->mc_top];
9911 unsigned n = NUMKEYS(mp);
9912 if (IS_LEAF(mp)) {
9913 for (i=0; i<n; i++) {
9914 ni = NODEPTR(mp, i);
9915 if (ni->mn_flags & F_BIGDATA) {
9916 MDB_page *omp;
9917 pgno_t pg;
9918 memcpy(&pg, NODEDATA(ni), sizeof(pg));
9919 rc = mdb_page_get(mc, pg, &omp, NULL);
9920 if (rc != 0)
9921 goto done;
9922 mdb_cassert(mc, IS_OVERFLOW(omp));
9923 rc = mdb_midl_append_range(&txn->mt_free_pgs,
9924 pg, omp->mp_pages);
9925 if (rc)
9926 goto done;
9927 mc->mc_db->md_overflow_pages -= omp->mp_pages;
9928 if (!mc->mc_db->md_overflow_pages && !subs)
9929 break;
9930 } else if (subs && (ni->mn_flags & F_SUBDATA)) {
9931 mdb_xcursor_init1(mc, ni);
9932 rc = mdb_drop0(&mc->mc_xcursor->mx_cursor, 0);
9933 if (rc)
9934 goto done;
9935 }
9936 }
9937 if (!subs && !mc->mc_db->md_overflow_pages)
9938 goto pop;
9939 } else {
9940 if ((rc = mdb_midl_need(&txn->mt_free_pgs, n)) != 0)
9941 goto done;
9942 for (i=0; i<n; i++) {
9943 pgno_t pg;
9944 ni = NODEPTR(mp, i);
9945 pg = NODEPGNO(ni);
9946 /* free it */
9947 mdb_midl_xappend(txn->mt_free_pgs, pg);
9948 }
9949 }
9950 if (!mc->mc_top)
9951 break;
9952 mc->mc_ki[mc->mc_top] = i;
9953 rc = mdb_cursor_sibling(mc, 1);
9954 if (rc) {
9955 if (rc != MDB_NOTFOUND)
9956 goto done;
9957 /* no more siblings, go back to beginning
9958 * of previous level.
9959 */
9960 pop:
9961 mdb_cursor_pop(mc);
9962 mc->mc_ki[0] = 0;
9963 for (i=1; i<mc->mc_snum; i++) {
9964 mc->mc_ki[i] = 0;
9965 mc->mc_pg[i] = mx.mc_pg[i];
9966 }
9967 }
9968 }
9969 /* free it */
9970 rc = mdb_midl_append(&txn->mt_free_pgs, mc->mc_db->md_root);
9971 done:
9972 if (rc)
9973 txn->mt_flags |= MDB_TXN_ERROR;
9974 } else if (rc == MDB_NOTFOUND) {
9975 rc = MDB_SUCCESS;
9976 }
9977 mc->mc_flags &= ~C_INITIALIZED;
9978 return rc;
9979 }
9980
mdb_drop(MDB_txn * txn,MDB_dbi dbi,int del)9981 int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del)
9982 {
9983 MDB_cursor *mc, *m2;
9984 int rc;
9985
9986 if ((unsigned)del > 1 || !TXN_DBI_EXIST(txn, dbi, DB_USRVALID))
9987 return EINVAL;
9988
9989 if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY))
9990 return EACCES;
9991
9992 if (TXN_DBI_CHANGED(txn, dbi))
9993 return MDB_BAD_DBI;
9994
9995 rc = mdb_cursor_open(txn, dbi, &mc);
9996 if (rc)
9997 return rc;
9998
9999 rc = mdb_drop0(mc, mc->mc_db->md_flags & MDB_DUPSORT);
10000 /* Invalidate the dropped DB's cursors */
10001 for (m2 = txn->mt_cursors[dbi]; m2; m2 = m2->mc_next)
10002 m2->mc_flags &= ~(C_INITIALIZED|C_EOF);
10003 if (rc)
10004 goto leave;
10005
10006 /* Can't delete the main DB */
10007 if (del && dbi >= CORE_DBS) {
10008 rc = mdb_del0(txn, MAIN_DBI, &mc->mc_dbx->md_name, NULL, F_SUBDATA);
10009 if (!rc) {
10010 txn->mt_dbflags[dbi] = DB_STALE;
10011 mdb_dbi_close(txn->mt_env, dbi);
10012 } else {
10013 txn->mt_flags |= MDB_TXN_ERROR;
10014 }
10015 } else {
10016 /* reset the DB record, mark it dirty */
10017 txn->mt_dbflags[dbi] |= DB_DIRTY;
10018 txn->mt_dbs[dbi].md_depth = 0;
10019 txn->mt_dbs[dbi].md_branch_pages = 0;
10020 txn->mt_dbs[dbi].md_leaf_pages = 0;
10021 txn->mt_dbs[dbi].md_overflow_pages = 0;
10022 txn->mt_dbs[dbi].md_entries = 0;
10023 txn->mt_dbs[dbi].md_root = P_INVALID;
10024
10025 txn->mt_flags |= MDB_TXN_DIRTY;
10026 }
10027 leave:
10028 mdb_cursor_close(mc);
10029 return rc;
10030 }
10031
mdb_set_compare(MDB_txn * txn,MDB_dbi dbi,MDB_cmp_func * cmp)10032 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
10033 {
10034 if (!TXN_DBI_EXIST(txn, dbi, DB_USRVALID))
10035 return EINVAL;
10036
10037 txn->mt_dbxs[dbi].md_cmp = cmp;
10038 return MDB_SUCCESS;
10039 }
10040
mdb_set_dupsort(MDB_txn * txn,MDB_dbi dbi,MDB_cmp_func * cmp)10041 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
10042 {
10043 if (!TXN_DBI_EXIST(txn, dbi, DB_USRVALID))
10044 return EINVAL;
10045
10046 txn->mt_dbxs[dbi].md_dcmp = cmp;
10047 return MDB_SUCCESS;
10048 }
10049
mdb_set_relfunc(MDB_txn * txn,MDB_dbi dbi,MDB_rel_func * rel)10050 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel)
10051 {
10052 if (!TXN_DBI_EXIST(txn, dbi, DB_USRVALID))
10053 return EINVAL;
10054
10055 txn->mt_dbxs[dbi].md_rel = rel;
10056 return MDB_SUCCESS;
10057 }
10058
mdb_set_relctx(MDB_txn * txn,MDB_dbi dbi,void * ctx)10059 int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx)
10060 {
10061 if (!TXN_DBI_EXIST(txn, dbi, DB_USRVALID))
10062 return EINVAL;
10063
10064 txn->mt_dbxs[dbi].md_relctx = ctx;
10065 return MDB_SUCCESS;
10066 }
10067
10068 int ESECT
mdb_env_get_maxkeysize(MDB_env * env)10069 mdb_env_get_maxkeysize(MDB_env *env)
10070 {
10071 return ENV_MAXKEY(env);
10072 }
10073
10074 int ESECT
mdb_reader_list(MDB_env * env,MDB_msg_func * func,void * ctx)10075 mdb_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx)
10076 {
10077 unsigned int i, rdrs;
10078 MDB_reader *mr;
10079 char buf[64];
10080 int rc = 0, first = 1;
10081
10082 if (!env || !func)
10083 return -1;
10084 if (!env->me_txns) {
10085 return func("(no reader locks)\n", ctx);
10086 }
10087 rdrs = env->me_txns->mti_numreaders;
10088 mr = env->me_txns->mti_readers;
10089 for (i=0; i<rdrs; i++) {
10090 if (mr[i].mr_pid) {
10091 txnid_t txnid = mr[i].mr_txnid;
10092 sprintf(buf, txnid == (txnid_t)-1 ?
10093 "%10d %"Z"x -\n" : "%10d %"Z"x %"Z"u\n",
10094 (int)mr[i].mr_pid, (size_t)mr[i].mr_tid, txnid);
10095 if (first) {
10096 first = 0;
10097 rc = func(" pid thread txnid\n", ctx);
10098 if (rc < 0)
10099 break;
10100 }
10101 rc = func(buf, ctx);
10102 if (rc < 0)
10103 break;
10104 }
10105 }
10106 if (first) {
10107 rc = func("(no active readers)\n", ctx);
10108 }
10109 return rc;
10110 }
10111
10112 /** Insert pid into list if not already present.
10113 * return -1 if already present.
10114 */
10115 static int ESECT
mdb_pid_insert(MDB_PID_T * ids,MDB_PID_T pid)10116 mdb_pid_insert(MDB_PID_T *ids, MDB_PID_T pid)
10117 {
10118 /* binary search of pid in list */
10119 unsigned base = 0;
10120 unsigned cursor = 1;
10121 int val = 0;
10122 unsigned n = ids[0];
10123
10124 while( 0 < n ) {
10125 unsigned pivot = n >> 1;
10126 cursor = base + pivot + 1;
10127 val = pid - ids[cursor];
10128
10129 if( val < 0 ) {
10130 n = pivot;
10131
10132 } else if ( val > 0 ) {
10133 base = cursor;
10134 n -= pivot + 1;
10135
10136 } else {
10137 /* found, so it's a duplicate */
10138 return -1;
10139 }
10140 }
10141
10142 if( val > 0 ) {
10143 ++cursor;
10144 }
10145 ids[0]++;
10146 for (n = ids[0]; n > cursor; n--)
10147 ids[n] = ids[n-1];
10148 ids[n] = pid;
10149 return 0;
10150 }
10151
10152 int ESECT
mdb_reader_check(MDB_env * env,int * dead)10153 mdb_reader_check(MDB_env *env, int *dead)
10154 {
10155 if (!env)
10156 return EINVAL;
10157 if (dead)
10158 *dead = 0;
10159 return env->me_txns ? mdb_reader_check0(env, 0, dead) : MDB_SUCCESS;
10160 }
10161
10162 /** As #mdb_reader_check(). \b rlocked is set if caller locked #me_rmutex. */
10163 static int ESECT
mdb_reader_check0(MDB_env * env,int rlocked,int * dead)10164 mdb_reader_check0(MDB_env *env, int rlocked, int *dead)
10165 {
10166 mdb_mutexref_t rmutex = rlocked ? NULL : env->me_rmutex;
10167 unsigned int i, j, rdrs;
10168 MDB_reader *mr;
10169 MDB_PID_T *pids, pid;
10170 int rc = MDB_SUCCESS, count = 0;
10171
10172 rdrs = env->me_txns->mti_numreaders;
10173 pids = malloc((rdrs+1) * sizeof(MDB_PID_T));
10174 if (!pids)
10175 return ENOMEM;
10176 pids[0] = 0;
10177 mr = env->me_txns->mti_readers;
10178 for (i=0; i<rdrs; i++) {
10179 pid = mr[i].mr_pid;
10180 if (pid && pid != env->me_pid) {
10181 if (mdb_pid_insert(pids, pid) == 0) {
10182 if (!mdb_reader_pid(env, Pidcheck, pid)) {
10183 /* Stale reader found */
10184 j = i;
10185 if (rmutex) {
10186 if ((rc = LOCK_MUTEX0(rmutex)) != 0) {
10187 if ((rc = mdb_mutex_failed(env, rmutex, rc)))
10188 break;
10189 rdrs = 0; /* the above checked all readers */
10190 } else {
10191 /* Recheck, a new process may have reused pid */
10192 if (mdb_reader_pid(env, Pidcheck, pid))
10193 j = rdrs;
10194 }
10195 }
10196 for (; j<rdrs; j++)
10197 if (mr[j].mr_pid == pid) {
10198 DPRINTF(("clear stale reader pid %u txn %"Z"d",
10199 (unsigned) pid, mr[j].mr_txnid));
10200 mr[j].mr_pid = 0;
10201 count++;
10202 }
10203 if (rmutex)
10204 UNLOCK_MUTEX(rmutex);
10205 }
10206 }
10207 }
10208 }
10209 free(pids);
10210 if (dead)
10211 *dead = count;
10212 return rc;
10213 }
10214
10215 #ifdef MDB_ROBUST_SUPPORTED
10216 /** Handle #LOCK_MUTEX0() failure.
10217 * Try to repair the lock file if the mutex owner died.
10218 * @param[in] env the environment handle
10219 * @param[in] mutex LOCK_MUTEX0() mutex
10220 * @param[in] rc LOCK_MUTEX0() error (nonzero)
10221 * @return 0 on success with the mutex locked, or an error code on failure.
10222 */
10223 static int ESECT
mdb_mutex_failed(MDB_env * env,mdb_mutexref_t mutex,int rc)10224 mdb_mutex_failed(MDB_env *env, mdb_mutexref_t mutex, int rc)
10225 {
10226 int rlocked, rc2;
10227 MDB_meta *meta;
10228
10229 if (rc == MDB_OWNERDEAD) {
10230 /* We own the mutex. Clean up after dead previous owner. */
10231 rc = MDB_SUCCESS;
10232 rlocked = (mutex == env->me_rmutex);
10233 if (!rlocked) {
10234 /* Keep mti_txnid updated, otherwise next writer can
10235 * overwrite data which latest meta page refers to.
10236 */
10237 meta = mdb_env_pick_meta(env);
10238 env->me_txns->mti_txnid = meta->mm_txnid;
10239 /* env is hosed if the dead thread was ours */
10240 if (env->me_txn) {
10241 env->me_flags |= MDB_FATAL_ERROR;
10242 env->me_txn = NULL;
10243 rc = MDB_PANIC;
10244 }
10245 }
10246 DPRINTF(("%cmutex owner died, %s", (rlocked ? 'r' : 'w'),
10247 (rc ? "this process' env is hosed" : "recovering")));
10248 rc2 = mdb_reader_check0(env, rlocked, NULL);
10249 if (rc2 == 0)
10250 rc2 = mdb_mutex_consistent(mutex);
10251 if (rc || (rc = rc2)) {
10252 DPRINTF(("LOCK_MUTEX recovery failed, %s", mdb_strerror(rc)));
10253 UNLOCK_MUTEX(mutex);
10254 }
10255 } else {
10256 #ifdef _WIN32
10257 rc = ErrCode();
10258 #endif
10259 DPRINTF(("LOCK_MUTEX failed, %s", mdb_strerror(rc)));
10260 }
10261
10262 return rc;
10263 }
10264 #endif /* MDB_ROBUST_SUPPORTED */
10265
10266 #if defined(_WIN32)
10267 /** Convert \b src to new wchar_t[] string with room for \b xtra extra chars */
10268 static int ESECT
utf8_to_utf16(const char * src,MDB_name * dst,int xtra)10269 utf8_to_utf16(const char *src, MDB_name *dst, int xtra)
10270 {
10271 int rc, need = 0;
10272 wchar_t *result = NULL;
10273 for (;;) { /* malloc result, then fill it in */
10274 need = MultiByteToWideChar(CP_UTF8, 0, src, -1, result, need);
10275 if (!need) {
10276 rc = ErrCode();
10277 free(result);
10278 return rc;
10279 }
10280 if (!result) {
10281 result = malloc(sizeof(wchar_t) * (need + xtra));
10282 if (!result)
10283 return ENOMEM;
10284 continue;
10285 }
10286 dst->mn_alloced = 1;
10287 dst->mn_len = need - 1;
10288 dst->mn_val = result;
10289 return MDB_SUCCESS;
10290 }
10291 }
10292 #endif /* defined(_WIN32) */
10293 /** @} */
10294