1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker URL: svn://svnanon.samba.org/samba/branches/SAMBA_4_0/source/lib/tdb/common
3*6a54128fSAndroid Build Coastguard Worker Rev: 23590
4*6a54128fSAndroid Build Coastguard Worker Last Changed Date: 2007-06-22 13:36:10 -0400 (Fri, 22 Jun 2007)
5*6a54128fSAndroid Build Coastguard Worker */
6*6a54128fSAndroid Build Coastguard Worker /*
7*6a54128fSAndroid Build Coastguard Worker trivial database library - standalone version
8*6a54128fSAndroid Build Coastguard Worker
9*6a54128fSAndroid Build Coastguard Worker Copyright (C) Andrew Tridgell 1999-2005
10*6a54128fSAndroid Build Coastguard Worker Copyright (C) Jeremy Allison 2000-2006
11*6a54128fSAndroid Build Coastguard Worker Copyright (C) Paul `Rusty' Russell 2000
12*6a54128fSAndroid Build Coastguard Worker
13*6a54128fSAndroid Build Coastguard Worker ** NOTE! The following LGPL license applies to the tdb
14*6a54128fSAndroid Build Coastguard Worker ** library. This does NOT imply that all of Samba is released
15*6a54128fSAndroid Build Coastguard Worker ** under the LGPL
16*6a54128fSAndroid Build Coastguard Worker
17*6a54128fSAndroid Build Coastguard Worker This library is free software; you can redistribute it and/or
18*6a54128fSAndroid Build Coastguard Worker modify it under the terms of the GNU Lesser General Public
19*6a54128fSAndroid Build Coastguard Worker License as published by the Free Software Foundation; either
20*6a54128fSAndroid Build Coastguard Worker version 2 of the License, or (at your option) any later version.
21*6a54128fSAndroid Build Coastguard Worker
22*6a54128fSAndroid Build Coastguard Worker This library is distributed in the hope that it will be useful,
23*6a54128fSAndroid Build Coastguard Worker but WITHOUT ANY WARRANTY; without even the implied warranty of
24*6a54128fSAndroid Build Coastguard Worker MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25*6a54128fSAndroid Build Coastguard Worker Lesser General Public License for more details.
26*6a54128fSAndroid Build Coastguard Worker
27*6a54128fSAndroid Build Coastguard Worker You should have received a copy of the GNU Lesser General Public
28*6a54128fSAndroid Build Coastguard Worker License along with this library; if not, write to the Free Software
29*6a54128fSAndroid Build Coastguard Worker Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30*6a54128fSAndroid Build Coastguard Worker */
31*6a54128fSAndroid Build Coastguard Worker
32*6a54128fSAndroid Build Coastguard Worker #ifdef CONFIG_STAND_ALONE
33*6a54128fSAndroid Build Coastguard Worker #define HAVE_MMAP
34*6a54128fSAndroid Build Coastguard Worker #define HAVE_STRDUP
35*6a54128fSAndroid Build Coastguard Worker #define HAVE_SYS_MMAN_H
36*6a54128fSAndroid Build Coastguard Worker #define HAVE_UTIME_H
37*6a54128fSAndroid Build Coastguard Worker #define HAVE_UTIME
38*6a54128fSAndroid Build Coastguard Worker #endif
39*6a54128fSAndroid Build Coastguard Worker #ifndef __FreeBSD__
40*6a54128fSAndroid Build Coastguard Worker #define _XOPEN_SOURCE 600
41*6a54128fSAndroid Build Coastguard Worker #endif
42*6a54128fSAndroid Build Coastguard Worker
43*6a54128fSAndroid Build Coastguard Worker #include "config.h"
44*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
45*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
46*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
47*6a54128fSAndroid Build Coastguard Worker #include <stdarg.h>
48*6a54128fSAndroid Build Coastguard Worker #include <stddef.h>
49*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
50*6a54128fSAndroid Build Coastguard Worker #include <string.h>
51*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_SELECT_H
52*6a54128fSAndroid Build Coastguard Worker #include <sys/select.h>
53*6a54128fSAndroid Build Coastguard Worker #endif
54*6a54128fSAndroid Build Coastguard Worker #include <sys/time.h>
55*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
56*6a54128fSAndroid Build Coastguard Worker #include <time.h>
57*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_UTIME_H
58*6a54128fSAndroid Build Coastguard Worker #include <utime.h>
59*6a54128fSAndroid Build Coastguard Worker #endif
60*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
61*6a54128fSAndroid Build Coastguard Worker #include <sys/file.h>
62*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
63*6a54128fSAndroid Build Coastguard Worker
64*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_MMAN_H
65*6a54128fSAndroid Build Coastguard Worker #include <sys/mman.h>
66*6a54128fSAndroid Build Coastguard Worker #endif
67*6a54128fSAndroid Build Coastguard Worker
68*6a54128fSAndroid Build Coastguard Worker #ifdef __GNUC__
69*6a54128fSAndroid Build Coastguard Worker #define EXT2FS_ATTR(x) __attribute__(x)
70*6a54128fSAndroid Build Coastguard Worker #else
71*6a54128fSAndroid Build Coastguard Worker #define EXT2FS_ATTR(x)
72*6a54128fSAndroid Build Coastguard Worker #endif
73*6a54128fSAndroid Build Coastguard Worker
74*6a54128fSAndroid Build Coastguard Worker #ifndef MAP_FILE
75*6a54128fSAndroid Build Coastguard Worker #define MAP_FILE 0
76*6a54128fSAndroid Build Coastguard Worker #endif
77*6a54128fSAndroid Build Coastguard Worker
78*6a54128fSAndroid Build Coastguard Worker #ifndef MAP_FAILED
79*6a54128fSAndroid Build Coastguard Worker #define MAP_FAILED ((void *)-1)
80*6a54128fSAndroid Build Coastguard Worker #endif
81*6a54128fSAndroid Build Coastguard Worker
82*6a54128fSAndroid Build Coastguard Worker #ifndef HAVE_STRDUP
83*6a54128fSAndroid Build Coastguard Worker #define strdup rep_strdup
rep_strdup(const char * s)84*6a54128fSAndroid Build Coastguard Worker static char *rep_strdup(const char *s)
85*6a54128fSAndroid Build Coastguard Worker {
86*6a54128fSAndroid Build Coastguard Worker char *ret;
87*6a54128fSAndroid Build Coastguard Worker int length;
88*6a54128fSAndroid Build Coastguard Worker
89*6a54128fSAndroid Build Coastguard Worker if (!s)
90*6a54128fSAndroid Build Coastguard Worker return NULL;
91*6a54128fSAndroid Build Coastguard Worker length = strlen(s);
92*6a54128fSAndroid Build Coastguard Worker ret = malloc(length + 1);
93*6a54128fSAndroid Build Coastguard Worker if (ret) {
94*6a54128fSAndroid Build Coastguard Worker strncpy(ret, s, length);
95*6a54128fSAndroid Build Coastguard Worker ret[length] = '\0';
96*6a54128fSAndroid Build Coastguard Worker }
97*6a54128fSAndroid Build Coastguard Worker return ret;
98*6a54128fSAndroid Build Coastguard Worker }
99*6a54128fSAndroid Build Coastguard Worker #endif
100*6a54128fSAndroid Build Coastguard Worker
101*6a54128fSAndroid Build Coastguard Worker #ifndef PRINTF_ATTRIBUTE
102*6a54128fSAndroid Build Coastguard Worker #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
103*6a54128fSAndroid Build Coastguard Worker /** Use gcc attribute to check printf fns. a1 is the 1-based index of
104*6a54128fSAndroid Build Coastguard Worker * the parameter containing the format, and a2 the index of the first
105*6a54128fSAndroid Build Coastguard Worker * argument. Note that some gcc 2.x versions don't handle this
106*6a54128fSAndroid Build Coastguard Worker * properly **/
107*6a54128fSAndroid Build Coastguard Worker #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
108*6a54128fSAndroid Build Coastguard Worker #else
109*6a54128fSAndroid Build Coastguard Worker #define PRINTF_ATTRIBUTE(a1, a2)
110*6a54128fSAndroid Build Coastguard Worker #endif
111*6a54128fSAndroid Build Coastguard Worker #endif
112*6a54128fSAndroid Build Coastguard Worker
113*6a54128fSAndroid Build Coastguard Worker typedef int bool;
114*6a54128fSAndroid Build Coastguard Worker
115*6a54128fSAndroid Build Coastguard Worker #include "tdb.h"
116*6a54128fSAndroid Build Coastguard Worker
117*6a54128fSAndroid Build Coastguard Worker static TDB_DATA tdb_null;
118*6a54128fSAndroid Build Coastguard Worker
119*6a54128fSAndroid Build Coastguard Worker #ifndef u32
120*6a54128fSAndroid Build Coastguard Worker #define u32 unsigned
121*6a54128fSAndroid Build Coastguard Worker #endif
122*6a54128fSAndroid Build Coastguard Worker
123*6a54128fSAndroid Build Coastguard Worker typedef u32 tdb_len_t;
124*6a54128fSAndroid Build Coastguard Worker typedef u32 tdb_off_t;
125*6a54128fSAndroid Build Coastguard Worker
126*6a54128fSAndroid Build Coastguard Worker #ifndef offsetof
127*6a54128fSAndroid Build Coastguard Worker #define offsetof(t,f) ((unsigned int)&((t *)0)->f)
128*6a54128fSAndroid Build Coastguard Worker #endif
129*6a54128fSAndroid Build Coastguard Worker
130*6a54128fSAndroid Build Coastguard Worker #define TDB_MAGIC_FOOD "TDB file\n"
131*6a54128fSAndroid Build Coastguard Worker #define TDB_VERSION (0x26011967 + 6)
132*6a54128fSAndroid Build Coastguard Worker #define TDB_MAGIC (0x26011999U)
133*6a54128fSAndroid Build Coastguard Worker #define TDB_FREE_MAGIC (~TDB_MAGIC)
134*6a54128fSAndroid Build Coastguard Worker #define TDB_DEAD_MAGIC (0xFEE1DEAD)
135*6a54128fSAndroid Build Coastguard Worker #define TDB_RECOVERY_MAGIC (0xf53bc0e7U)
136*6a54128fSAndroid Build Coastguard Worker #define TDB_ALIGNMENT 4
137*6a54128fSAndroid Build Coastguard Worker #define MIN_REC_SIZE (2*sizeof(struct list_struct) + TDB_ALIGNMENT)
138*6a54128fSAndroid Build Coastguard Worker #define DEFAULT_HASH_SIZE 131
139*6a54128fSAndroid Build Coastguard Worker #define FREELIST_TOP (sizeof(struct tdb_header))
140*6a54128fSAndroid Build Coastguard Worker #define TDB_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1))
141*6a54128fSAndroid Build Coastguard Worker #define TDB_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
142*6a54128fSAndroid Build Coastguard Worker #define TDB_DEAD(r) ((r)->magic == TDB_DEAD_MAGIC)
143*6a54128fSAndroid Build Coastguard Worker #define TDB_BAD_MAGIC(r) ((r)->magic != TDB_MAGIC && !TDB_DEAD(r))
144*6a54128fSAndroid Build Coastguard Worker #define TDB_HASH_TOP(hash) (FREELIST_TOP + (BUCKET(hash)+1)*sizeof(tdb_off_t))
145*6a54128fSAndroid Build Coastguard Worker #define TDB_HASHTABLE_SIZE(tdb) ((tdb->header.hash_size+1)*sizeof(tdb_off_t))
146*6a54128fSAndroid Build Coastguard Worker #define TDB_DATA_START(hash_size) TDB_HASH_TOP(hash_size-1)
147*6a54128fSAndroid Build Coastguard Worker #define TDB_RECOVERY_HEAD offsetof(struct tdb_header, recovery_start)
148*6a54128fSAndroid Build Coastguard Worker #define TDB_SEQNUM_OFS offsetof(struct tdb_header, sequence_number)
149*6a54128fSAndroid Build Coastguard Worker #define TDB_PAD_BYTE 0x42
150*6a54128fSAndroid Build Coastguard Worker #define TDB_PAD_U32 0x42424242
151*6a54128fSAndroid Build Coastguard Worker
152*6a54128fSAndroid Build Coastguard Worker /* NB assumes there is a local variable called "tdb" that is the
153*6a54128fSAndroid Build Coastguard Worker * current context, also takes doubly-parenthesized print-style
154*6a54128fSAndroid Build Coastguard Worker * argument. */
155*6a54128fSAndroid Build Coastguard Worker #define TDB_LOG(x) tdb->log.log_fn x
156*6a54128fSAndroid Build Coastguard Worker
157*6a54128fSAndroid Build Coastguard Worker /* lock offsets */
158*6a54128fSAndroid Build Coastguard Worker #define GLOBAL_LOCK 0
159*6a54128fSAndroid Build Coastguard Worker #define ACTIVE_LOCK 4
160*6a54128fSAndroid Build Coastguard Worker #define TRANSACTION_LOCK 8
161*6a54128fSAndroid Build Coastguard Worker
162*6a54128fSAndroid Build Coastguard Worker /* free memory if the pointer is valid and zero the pointer */
163*6a54128fSAndroid Build Coastguard Worker #ifndef SAFE_FREE
164*6a54128fSAndroid Build Coastguard Worker #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
165*6a54128fSAndroid Build Coastguard Worker #endif
166*6a54128fSAndroid Build Coastguard Worker
167*6a54128fSAndroid Build Coastguard Worker #define BUCKET(hash) ((hash) % tdb->header.hash_size)
168*6a54128fSAndroid Build Coastguard Worker
169*6a54128fSAndroid Build Coastguard Worker #define DOCONV() (tdb->flags & TDB_CONVERT)
170*6a54128fSAndroid Build Coastguard Worker #define CONVERT(x) (DOCONV() ? tdb_convert(&x, sizeof(x)) : &x)
171*6a54128fSAndroid Build Coastguard Worker
172*6a54128fSAndroid Build Coastguard Worker
173*6a54128fSAndroid Build Coastguard Worker /* the body of the database is made of one list_struct for the free space
174*6a54128fSAndroid Build Coastguard Worker plus a separate data list for each hash value */
175*6a54128fSAndroid Build Coastguard Worker struct list_struct {
176*6a54128fSAndroid Build Coastguard Worker tdb_off_t next; /* offset of the next record in the list */
177*6a54128fSAndroid Build Coastguard Worker tdb_len_t rec_len; /* total byte length of record */
178*6a54128fSAndroid Build Coastguard Worker tdb_len_t key_len; /* byte length of key */
179*6a54128fSAndroid Build Coastguard Worker tdb_len_t data_len; /* byte length of data */
180*6a54128fSAndroid Build Coastguard Worker u32 full_hash; /* the full 32 bit hash of the key */
181*6a54128fSAndroid Build Coastguard Worker u32 magic; /* try to catch errors */
182*6a54128fSAndroid Build Coastguard Worker /* the following union is implied:
183*6a54128fSAndroid Build Coastguard Worker union {
184*6a54128fSAndroid Build Coastguard Worker char record[rec_len];
185*6a54128fSAndroid Build Coastguard Worker struct {
186*6a54128fSAndroid Build Coastguard Worker char key[key_len];
187*6a54128fSAndroid Build Coastguard Worker char data[data_len];
188*6a54128fSAndroid Build Coastguard Worker }
189*6a54128fSAndroid Build Coastguard Worker u32 totalsize; (tailer)
190*6a54128fSAndroid Build Coastguard Worker }
191*6a54128fSAndroid Build Coastguard Worker */
192*6a54128fSAndroid Build Coastguard Worker };
193*6a54128fSAndroid Build Coastguard Worker
194*6a54128fSAndroid Build Coastguard Worker
195*6a54128fSAndroid Build Coastguard Worker /* this is stored at the front of every database */
196*6a54128fSAndroid Build Coastguard Worker struct tdb_header {
197*6a54128fSAndroid Build Coastguard Worker char magic_food[32]; /* for /etc/magic */
198*6a54128fSAndroid Build Coastguard Worker u32 version; /* version of the code */
199*6a54128fSAndroid Build Coastguard Worker u32 hash_size; /* number of hash entries */
200*6a54128fSAndroid Build Coastguard Worker tdb_off_t rwlocks; /* obsolete - kept to detect old formats */
201*6a54128fSAndroid Build Coastguard Worker tdb_off_t recovery_start; /* offset of transaction recovery region */
202*6a54128fSAndroid Build Coastguard Worker tdb_off_t sequence_number; /* used when TDB_SEQNUM is set */
203*6a54128fSAndroid Build Coastguard Worker tdb_off_t reserved[29];
204*6a54128fSAndroid Build Coastguard Worker };
205*6a54128fSAndroid Build Coastguard Worker
206*6a54128fSAndroid Build Coastguard Worker struct tdb_lock_type {
207*6a54128fSAndroid Build Coastguard Worker int list;
208*6a54128fSAndroid Build Coastguard Worker u32 count;
209*6a54128fSAndroid Build Coastguard Worker u32 ltype;
210*6a54128fSAndroid Build Coastguard Worker };
211*6a54128fSAndroid Build Coastguard Worker
212*6a54128fSAndroid Build Coastguard Worker struct tdb_traverse_lock {
213*6a54128fSAndroid Build Coastguard Worker struct tdb_traverse_lock *next;
214*6a54128fSAndroid Build Coastguard Worker u32 off;
215*6a54128fSAndroid Build Coastguard Worker u32 hash;
216*6a54128fSAndroid Build Coastguard Worker int lock_rw;
217*6a54128fSAndroid Build Coastguard Worker };
218*6a54128fSAndroid Build Coastguard Worker
219*6a54128fSAndroid Build Coastguard Worker
220*6a54128fSAndroid Build Coastguard Worker struct tdb_methods {
221*6a54128fSAndroid Build Coastguard Worker int (*tdb_read)(struct tdb_context *, tdb_off_t , void *, tdb_len_t , int );
222*6a54128fSAndroid Build Coastguard Worker int (*tdb_write)(struct tdb_context *, tdb_off_t, const void *, tdb_len_t);
223*6a54128fSAndroid Build Coastguard Worker void (*next_hash_chain)(struct tdb_context *, u32 *);
224*6a54128fSAndroid Build Coastguard Worker int (*tdb_oob)(struct tdb_context *, tdb_off_t , int );
225*6a54128fSAndroid Build Coastguard Worker int (*tdb_expand_file)(struct tdb_context *, tdb_off_t , tdb_off_t );
226*6a54128fSAndroid Build Coastguard Worker int (*tdb_brlock)(struct tdb_context *, tdb_off_t , int, int, int, size_t);
227*6a54128fSAndroid Build Coastguard Worker };
228*6a54128fSAndroid Build Coastguard Worker
229*6a54128fSAndroid Build Coastguard Worker struct tdb_context {
230*6a54128fSAndroid Build Coastguard Worker char *name; /* the name of the database */
231*6a54128fSAndroid Build Coastguard Worker void *map_ptr; /* where it is currently mapped */
232*6a54128fSAndroid Build Coastguard Worker int fd; /* open file descriptor for the database */
233*6a54128fSAndroid Build Coastguard Worker tdb_len_t map_size; /* how much space has been mapped */
234*6a54128fSAndroid Build Coastguard Worker int read_only; /* opened read-only */
235*6a54128fSAndroid Build Coastguard Worker int traverse_read; /* read-only traversal */
236*6a54128fSAndroid Build Coastguard Worker struct tdb_lock_type global_lock;
237*6a54128fSAndroid Build Coastguard Worker int num_lockrecs;
238*6a54128fSAndroid Build Coastguard Worker struct tdb_lock_type *lockrecs; /* only real locks, all with count>0 */
239*6a54128fSAndroid Build Coastguard Worker enum TDB_ERROR ecode; /* error code for last tdb error */
240*6a54128fSAndroid Build Coastguard Worker struct tdb_header header; /* a cached copy of the header */
241*6a54128fSAndroid Build Coastguard Worker u32 flags; /* the flags passed to tdb_open */
242*6a54128fSAndroid Build Coastguard Worker struct tdb_traverse_lock travlocks; /* current traversal locks */
243*6a54128fSAndroid Build Coastguard Worker struct tdb_context *next; /* all tdbs to avoid multiple opens */
244*6a54128fSAndroid Build Coastguard Worker dev_t device; /* uniquely identifies this tdb */
245*6a54128fSAndroid Build Coastguard Worker ino_t inode; /* uniquely identifies this tdb */
246*6a54128fSAndroid Build Coastguard Worker struct tdb_logging_context log;
247*6a54128fSAndroid Build Coastguard Worker unsigned int (*hash_fn)(TDB_DATA *key);
248*6a54128fSAndroid Build Coastguard Worker int open_flags; /* flags used in the open - needed by reopen */
249*6a54128fSAndroid Build Coastguard Worker unsigned int num_locks; /* number of chain locks held */
250*6a54128fSAndroid Build Coastguard Worker const struct tdb_methods *methods;
251*6a54128fSAndroid Build Coastguard Worker struct tdb_transaction *transaction;
252*6a54128fSAndroid Build Coastguard Worker int page_size;
253*6a54128fSAndroid Build Coastguard Worker int max_dead_records;
254*6a54128fSAndroid Build Coastguard Worker bool have_transaction_lock;
255*6a54128fSAndroid Build Coastguard Worker tdb_len_t real_map_size; /* how much space has been mapped */
256*6a54128fSAndroid Build Coastguard Worker };
257*6a54128fSAndroid Build Coastguard Worker
258*6a54128fSAndroid Build Coastguard Worker
259*6a54128fSAndroid Build Coastguard Worker /*
260*6a54128fSAndroid Build Coastguard Worker internal prototypes
261*6a54128fSAndroid Build Coastguard Worker */
262*6a54128fSAndroid Build Coastguard Worker static int tdb_munmap(struct tdb_context *tdb);
263*6a54128fSAndroid Build Coastguard Worker static void tdb_mmap(struct tdb_context *tdb);
264*6a54128fSAndroid Build Coastguard Worker static int tdb_lock(struct tdb_context *tdb, int list, int ltype);
265*6a54128fSAndroid Build Coastguard Worker int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype);
266*6a54128fSAndroid Build Coastguard Worker static int tdb_unlock(struct tdb_context *tdb, int list, int ltype);
267*6a54128fSAndroid Build Coastguard Worker static int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset, int rw_type, int lck_type, int probe, size_t len);
268*6a54128fSAndroid Build Coastguard Worker static int tdb_transaction_lock(struct tdb_context *tdb, int ltype);
269*6a54128fSAndroid Build Coastguard Worker static int tdb_transaction_unlock(struct tdb_context *tdb);
270*6a54128fSAndroid Build Coastguard Worker static int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len);
271*6a54128fSAndroid Build Coastguard Worker static int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off);
272*6a54128fSAndroid Build Coastguard Worker static int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off);
273*6a54128fSAndroid Build Coastguard Worker static int tdb_ofs_read(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
274*6a54128fSAndroid Build Coastguard Worker static int tdb_ofs_write(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
275*6a54128fSAndroid Build Coastguard Worker static void *tdb_convert(void *buf, u32 size);
276*6a54128fSAndroid Build Coastguard Worker static int tdb_free(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec);
277*6a54128fSAndroid Build Coastguard Worker static tdb_off_t tdb_allocate(struct tdb_context *tdb, tdb_len_t length, struct list_struct *rec);
278*6a54128fSAndroid Build Coastguard Worker static int tdb_ofs_read(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
279*6a54128fSAndroid Build Coastguard Worker static int tdb_ofs_write(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
280*6a54128fSAndroid Build Coastguard Worker static int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off);
281*6a54128fSAndroid Build Coastguard Worker static int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off);
282*6a54128fSAndroid Build Coastguard Worker static int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec);
283*6a54128fSAndroid Build Coastguard Worker static int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec);
284*6a54128fSAndroid Build Coastguard Worker static int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct *rec);
285*6a54128fSAndroid Build Coastguard Worker static unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len);
286*6a54128fSAndroid Build Coastguard Worker static int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
287*6a54128fSAndroid Build Coastguard Worker tdb_off_t offset, tdb_len_t len,
288*6a54128fSAndroid Build Coastguard Worker int (*parser)(TDB_DATA key, TDB_DATA data,
289*6a54128fSAndroid Build Coastguard Worker void *private_data),
290*6a54128fSAndroid Build Coastguard Worker void *private_data);
291*6a54128fSAndroid Build Coastguard Worker static tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, u32 hash, int locktype,
292*6a54128fSAndroid Build Coastguard Worker struct list_struct *rec);
293*6a54128fSAndroid Build Coastguard Worker static void tdb_io_init(struct tdb_context *tdb);
294*6a54128fSAndroid Build Coastguard Worker static int tdb_expand(struct tdb_context *tdb, tdb_off_t size);
295*6a54128fSAndroid Build Coastguard Worker static int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off,
296*6a54128fSAndroid Build Coastguard Worker struct list_struct *rec);
297*6a54128fSAndroid Build Coastguard Worker
298*6a54128fSAndroid Build Coastguard Worker
299*6a54128fSAndroid Build Coastguard Worker /* file: error.c */
300*6a54128fSAndroid Build Coastguard Worker
tdb_error(struct tdb_context * tdb)301*6a54128fSAndroid Build Coastguard Worker enum TDB_ERROR tdb_error(struct tdb_context *tdb)
302*6a54128fSAndroid Build Coastguard Worker {
303*6a54128fSAndroid Build Coastguard Worker return tdb->ecode;
304*6a54128fSAndroid Build Coastguard Worker }
305*6a54128fSAndroid Build Coastguard Worker
306*6a54128fSAndroid Build Coastguard Worker static struct tdb_errname {
307*6a54128fSAndroid Build Coastguard Worker enum TDB_ERROR ecode; const char *estring;
308*6a54128fSAndroid Build Coastguard Worker } emap[] = { {TDB_SUCCESS, "Success"},
309*6a54128fSAndroid Build Coastguard Worker {TDB_ERR_CORRUPT, "Corrupt database"},
310*6a54128fSAndroid Build Coastguard Worker {TDB_ERR_IO, "IO Error"},
311*6a54128fSAndroid Build Coastguard Worker {TDB_ERR_LOCK, "Locking error"},
312*6a54128fSAndroid Build Coastguard Worker {TDB_ERR_OOM, "Out of memory"},
313*6a54128fSAndroid Build Coastguard Worker {TDB_ERR_EXISTS, "Record exists"},
314*6a54128fSAndroid Build Coastguard Worker {TDB_ERR_NOLOCK, "Lock exists on other keys"},
315*6a54128fSAndroid Build Coastguard Worker {TDB_ERR_EINVAL, "Invalid parameter"},
316*6a54128fSAndroid Build Coastguard Worker {TDB_ERR_NOEXIST, "Record does not exist"},
317*6a54128fSAndroid Build Coastguard Worker {TDB_ERR_RDONLY, "write not permitted"} };
318*6a54128fSAndroid Build Coastguard Worker
319*6a54128fSAndroid Build Coastguard Worker /* Error string for the last tdb error */
tdb_errorstr(struct tdb_context * tdb)320*6a54128fSAndroid Build Coastguard Worker const char *tdb_errorstr(struct tdb_context *tdb)
321*6a54128fSAndroid Build Coastguard Worker {
322*6a54128fSAndroid Build Coastguard Worker u32 i;
323*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < sizeof(emap) / sizeof(struct tdb_errname); i++)
324*6a54128fSAndroid Build Coastguard Worker if (tdb->ecode == emap[i].ecode)
325*6a54128fSAndroid Build Coastguard Worker return emap[i].estring;
326*6a54128fSAndroid Build Coastguard Worker return "Invalid error code";
327*6a54128fSAndroid Build Coastguard Worker }
328*6a54128fSAndroid Build Coastguard Worker
329*6a54128fSAndroid Build Coastguard Worker /* file: lock.c */
330*6a54128fSAndroid Build Coastguard Worker
331*6a54128fSAndroid Build Coastguard Worker #define TDB_MARK_LOCK 0x80000000
332*6a54128fSAndroid Build Coastguard Worker
333*6a54128fSAndroid Build Coastguard Worker /* a byte range locking function - return 0 on success
334*6a54128fSAndroid Build Coastguard Worker this functions locks/unlocks 1 byte at the specified offset.
335*6a54128fSAndroid Build Coastguard Worker
336*6a54128fSAndroid Build Coastguard Worker On error, errno is also set so that errors are passed back properly
337*6a54128fSAndroid Build Coastguard Worker through tdb_open().
338*6a54128fSAndroid Build Coastguard Worker
339*6a54128fSAndroid Build Coastguard Worker note that a len of zero means lock to end of file
340*6a54128fSAndroid Build Coastguard Worker */
tdb_brlock(struct tdb_context * tdb,tdb_off_t offset,int rw_type,int lck_type,int probe,size_t len)341*6a54128fSAndroid Build Coastguard Worker int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset,
342*6a54128fSAndroid Build Coastguard Worker int rw_type, int lck_type, int probe, size_t len)
343*6a54128fSAndroid Build Coastguard Worker {
344*6a54128fSAndroid Build Coastguard Worker struct flock fl;
345*6a54128fSAndroid Build Coastguard Worker int ret;
346*6a54128fSAndroid Build Coastguard Worker
347*6a54128fSAndroid Build Coastguard Worker if (tdb->flags & TDB_NOLOCK) {
348*6a54128fSAndroid Build Coastguard Worker return 0;
349*6a54128fSAndroid Build Coastguard Worker }
350*6a54128fSAndroid Build Coastguard Worker
351*6a54128fSAndroid Build Coastguard Worker if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
352*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_RDONLY;
353*6a54128fSAndroid Build Coastguard Worker return -1;
354*6a54128fSAndroid Build Coastguard Worker }
355*6a54128fSAndroid Build Coastguard Worker
356*6a54128fSAndroid Build Coastguard Worker fl.l_type = rw_type;
357*6a54128fSAndroid Build Coastguard Worker fl.l_whence = SEEK_SET;
358*6a54128fSAndroid Build Coastguard Worker fl.l_start = offset;
359*6a54128fSAndroid Build Coastguard Worker fl.l_len = len;
360*6a54128fSAndroid Build Coastguard Worker fl.l_pid = 0;
361*6a54128fSAndroid Build Coastguard Worker
362*6a54128fSAndroid Build Coastguard Worker do {
363*6a54128fSAndroid Build Coastguard Worker ret = fcntl(tdb->fd,lck_type,&fl);
364*6a54128fSAndroid Build Coastguard Worker } while (ret == -1 && errno == EINTR);
365*6a54128fSAndroid Build Coastguard Worker
366*6a54128fSAndroid Build Coastguard Worker if (ret == -1) {
367*6a54128fSAndroid Build Coastguard Worker /* Generic lock error. errno set by fcntl.
368*6a54128fSAndroid Build Coastguard Worker * EAGAIN is an expected return from non-blocking
369*6a54128fSAndroid Build Coastguard Worker * locks. */
370*6a54128fSAndroid Build Coastguard Worker if (!probe && lck_type != F_SETLK) {
371*6a54128fSAndroid Build Coastguard Worker /* Ensure error code is set for log fun to examine. */
372*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_LOCK;
373*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d\n",
374*6a54128fSAndroid Build Coastguard Worker tdb->fd, offset, rw_type, lck_type, (int)len));
375*6a54128fSAndroid Build Coastguard Worker }
376*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_LOCK, -1);
377*6a54128fSAndroid Build Coastguard Worker }
378*6a54128fSAndroid Build Coastguard Worker return 0;
379*6a54128fSAndroid Build Coastguard Worker }
380*6a54128fSAndroid Build Coastguard Worker
381*6a54128fSAndroid Build Coastguard Worker
382*6a54128fSAndroid Build Coastguard Worker /*
383*6a54128fSAndroid Build Coastguard Worker upgrade a read lock to a write lock. This needs to be handled in a
384*6a54128fSAndroid Build Coastguard Worker special way as some OSes (such as solaris) have too conservative
385*6a54128fSAndroid Build Coastguard Worker deadlock detection and claim a deadlock when progress can be
386*6a54128fSAndroid Build Coastguard Worker made. For those OSes we may loop for a while.
387*6a54128fSAndroid Build Coastguard Worker */
tdb_brlock_upgrade(struct tdb_context * tdb,tdb_off_t offset,size_t len)388*6a54128fSAndroid Build Coastguard Worker int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len)
389*6a54128fSAndroid Build Coastguard Worker {
390*6a54128fSAndroid Build Coastguard Worker int count = 1000;
391*6a54128fSAndroid Build Coastguard Worker while (count--) {
392*6a54128fSAndroid Build Coastguard Worker struct timeval tv;
393*6a54128fSAndroid Build Coastguard Worker if (tdb_brlock(tdb, offset, F_WRLCK, F_SETLKW, 1, len) == 0) {
394*6a54128fSAndroid Build Coastguard Worker return 0;
395*6a54128fSAndroid Build Coastguard Worker }
396*6a54128fSAndroid Build Coastguard Worker if (errno != EDEADLK) {
397*6a54128fSAndroid Build Coastguard Worker break;
398*6a54128fSAndroid Build Coastguard Worker }
399*6a54128fSAndroid Build Coastguard Worker /* sleep for as short a time as we can - more portable than usleep() */
400*6a54128fSAndroid Build Coastguard Worker tv.tv_sec = 0;
401*6a54128fSAndroid Build Coastguard Worker tv.tv_usec = 1;
402*6a54128fSAndroid Build Coastguard Worker select(0, NULL, NULL, NULL, &tv);
403*6a54128fSAndroid Build Coastguard Worker }
404*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock_upgrade failed at offset %d\n", offset));
405*6a54128fSAndroid Build Coastguard Worker return -1;
406*6a54128fSAndroid Build Coastguard Worker }
407*6a54128fSAndroid Build Coastguard Worker
408*6a54128fSAndroid Build Coastguard Worker
409*6a54128fSAndroid Build Coastguard Worker /* lock a list in the database. list -1 is the alloc list */
_tdb_lock(struct tdb_context * tdb,int list,int ltype,int op)410*6a54128fSAndroid Build Coastguard Worker static int _tdb_lock(struct tdb_context *tdb, int list, int ltype, int op)
411*6a54128fSAndroid Build Coastguard Worker {
412*6a54128fSAndroid Build Coastguard Worker struct tdb_lock_type *new_lck;
413*6a54128fSAndroid Build Coastguard Worker int i;
414*6a54128fSAndroid Build Coastguard Worker bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
415*6a54128fSAndroid Build Coastguard Worker
416*6a54128fSAndroid Build Coastguard Worker ltype &= ~TDB_MARK_LOCK;
417*6a54128fSAndroid Build Coastguard Worker
418*6a54128fSAndroid Build Coastguard Worker /* a global lock allows us to avoid per chain locks */
419*6a54128fSAndroid Build Coastguard Worker if (tdb->global_lock.count &&
420*6a54128fSAndroid Build Coastguard Worker ((u32)ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
421*6a54128fSAndroid Build Coastguard Worker return 0;
422*6a54128fSAndroid Build Coastguard Worker }
423*6a54128fSAndroid Build Coastguard Worker
424*6a54128fSAndroid Build Coastguard Worker if (tdb->global_lock.count) {
425*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_LOCK, -1);
426*6a54128fSAndroid Build Coastguard Worker }
427*6a54128fSAndroid Build Coastguard Worker
428*6a54128fSAndroid Build Coastguard Worker if (list < -1 || list >= (int)tdb->header.hash_size) {
429*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid list %d for ltype=%d\n",
430*6a54128fSAndroid Build Coastguard Worker list, ltype));
431*6a54128fSAndroid Build Coastguard Worker return -1;
432*6a54128fSAndroid Build Coastguard Worker }
433*6a54128fSAndroid Build Coastguard Worker if (tdb->flags & TDB_NOLOCK)
434*6a54128fSAndroid Build Coastguard Worker return 0;
435*6a54128fSAndroid Build Coastguard Worker
436*6a54128fSAndroid Build Coastguard Worker for (i=0; i<tdb->num_lockrecs; i++) {
437*6a54128fSAndroid Build Coastguard Worker if (tdb->lockrecs[i].list == list) {
438*6a54128fSAndroid Build Coastguard Worker if (tdb->lockrecs[i].count == 0) {
439*6a54128fSAndroid Build Coastguard Worker /*
440*6a54128fSAndroid Build Coastguard Worker * Can't happen, see tdb_unlock(). It should
441*6a54128fSAndroid Build Coastguard Worker * be an assert.
442*6a54128fSAndroid Build Coastguard Worker */
443*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock: "
444*6a54128fSAndroid Build Coastguard Worker "lck->count == 0 for list %d", list));
445*6a54128fSAndroid Build Coastguard Worker }
446*6a54128fSAndroid Build Coastguard Worker /*
447*6a54128fSAndroid Build Coastguard Worker * Just increment the in-memory struct, posix locks
448*6a54128fSAndroid Build Coastguard Worker * don't stack.
449*6a54128fSAndroid Build Coastguard Worker */
450*6a54128fSAndroid Build Coastguard Worker tdb->lockrecs[i].count++;
451*6a54128fSAndroid Build Coastguard Worker return 0;
452*6a54128fSAndroid Build Coastguard Worker }
453*6a54128fSAndroid Build Coastguard Worker }
454*6a54128fSAndroid Build Coastguard Worker
455*6a54128fSAndroid Build Coastguard Worker new_lck = (struct tdb_lock_type *)realloc(
456*6a54128fSAndroid Build Coastguard Worker tdb->lockrecs,
457*6a54128fSAndroid Build Coastguard Worker sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
458*6a54128fSAndroid Build Coastguard Worker if (new_lck == NULL) {
459*6a54128fSAndroid Build Coastguard Worker errno = ENOMEM;
460*6a54128fSAndroid Build Coastguard Worker return -1;
461*6a54128fSAndroid Build Coastguard Worker }
462*6a54128fSAndroid Build Coastguard Worker tdb->lockrecs = new_lck;
463*6a54128fSAndroid Build Coastguard Worker
464*6a54128fSAndroid Build Coastguard Worker /* Since fcntl locks don't nest, we do a lock for the first one,
465*6a54128fSAndroid Build Coastguard Worker and simply bump the count for future ones */
466*6a54128fSAndroid Build Coastguard Worker if (!mark_lock &&
467*6a54128fSAndroid Build Coastguard Worker tdb->methods->tdb_brlock(tdb,FREELIST_TOP+4*list, ltype, op,
468*6a54128fSAndroid Build Coastguard Worker 0, 1)) {
469*6a54128fSAndroid Build Coastguard Worker return -1;
470*6a54128fSAndroid Build Coastguard Worker }
471*6a54128fSAndroid Build Coastguard Worker
472*6a54128fSAndroid Build Coastguard Worker tdb->num_locks++;
473*6a54128fSAndroid Build Coastguard Worker
474*6a54128fSAndroid Build Coastguard Worker tdb->lockrecs[tdb->num_lockrecs].list = list;
475*6a54128fSAndroid Build Coastguard Worker tdb->lockrecs[tdb->num_lockrecs].count = 1;
476*6a54128fSAndroid Build Coastguard Worker tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
477*6a54128fSAndroid Build Coastguard Worker tdb->num_lockrecs += 1;
478*6a54128fSAndroid Build Coastguard Worker
479*6a54128fSAndroid Build Coastguard Worker return 0;
480*6a54128fSAndroid Build Coastguard Worker }
481*6a54128fSAndroid Build Coastguard Worker
482*6a54128fSAndroid Build Coastguard Worker /* lock a list in the database. list -1 is the alloc list */
tdb_lock(struct tdb_context * tdb,int list,int ltype)483*6a54128fSAndroid Build Coastguard Worker int tdb_lock(struct tdb_context *tdb, int list, int ltype)
484*6a54128fSAndroid Build Coastguard Worker {
485*6a54128fSAndroid Build Coastguard Worker int ret;
486*6a54128fSAndroid Build Coastguard Worker ret = _tdb_lock(tdb, list, ltype, F_SETLKW);
487*6a54128fSAndroid Build Coastguard Worker if (ret) {
488*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock failed on list %d "
489*6a54128fSAndroid Build Coastguard Worker "ltype=%d (%s)\n", list, ltype, strerror(errno)));
490*6a54128fSAndroid Build Coastguard Worker }
491*6a54128fSAndroid Build Coastguard Worker return ret;
492*6a54128fSAndroid Build Coastguard Worker }
493*6a54128fSAndroid Build Coastguard Worker
494*6a54128fSAndroid Build Coastguard Worker /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
tdb_lock_nonblock(struct tdb_context * tdb,int list,int ltype)495*6a54128fSAndroid Build Coastguard Worker int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype)
496*6a54128fSAndroid Build Coastguard Worker {
497*6a54128fSAndroid Build Coastguard Worker return _tdb_lock(tdb, list, ltype, F_SETLK);
498*6a54128fSAndroid Build Coastguard Worker }
499*6a54128fSAndroid Build Coastguard Worker
500*6a54128fSAndroid Build Coastguard Worker
501*6a54128fSAndroid Build Coastguard Worker /* unlock the database: returns void because it's too late for errors. */
502*6a54128fSAndroid Build Coastguard Worker /* changed to return int it may be interesting to know there
503*6a54128fSAndroid Build Coastguard Worker has been an error --simo */
tdb_unlock(struct tdb_context * tdb,int list,int ltype)504*6a54128fSAndroid Build Coastguard Worker int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
505*6a54128fSAndroid Build Coastguard Worker {
506*6a54128fSAndroid Build Coastguard Worker int ret = -1;
507*6a54128fSAndroid Build Coastguard Worker int i;
508*6a54128fSAndroid Build Coastguard Worker struct tdb_lock_type *lck = NULL;
509*6a54128fSAndroid Build Coastguard Worker bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
510*6a54128fSAndroid Build Coastguard Worker
511*6a54128fSAndroid Build Coastguard Worker ltype &= ~TDB_MARK_LOCK;
512*6a54128fSAndroid Build Coastguard Worker
513*6a54128fSAndroid Build Coastguard Worker /* a global lock allows us to avoid per chain locks */
514*6a54128fSAndroid Build Coastguard Worker if (tdb->global_lock.count &&
515*6a54128fSAndroid Build Coastguard Worker ((u32)ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
516*6a54128fSAndroid Build Coastguard Worker return 0;
517*6a54128fSAndroid Build Coastguard Worker }
518*6a54128fSAndroid Build Coastguard Worker
519*6a54128fSAndroid Build Coastguard Worker if (tdb->global_lock.count) {
520*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_LOCK, -1);
521*6a54128fSAndroid Build Coastguard Worker }
522*6a54128fSAndroid Build Coastguard Worker
523*6a54128fSAndroid Build Coastguard Worker if (tdb->flags & TDB_NOLOCK)
524*6a54128fSAndroid Build Coastguard Worker return 0;
525*6a54128fSAndroid Build Coastguard Worker
526*6a54128fSAndroid Build Coastguard Worker /* Sanity checks */
527*6a54128fSAndroid Build Coastguard Worker if (list < -1 || list >= (int)tdb->header.hash_size) {
528*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size));
529*6a54128fSAndroid Build Coastguard Worker return ret;
530*6a54128fSAndroid Build Coastguard Worker }
531*6a54128fSAndroid Build Coastguard Worker
532*6a54128fSAndroid Build Coastguard Worker for (i=0; i<tdb->num_lockrecs; i++) {
533*6a54128fSAndroid Build Coastguard Worker if (tdb->lockrecs[i].list == list) {
534*6a54128fSAndroid Build Coastguard Worker lck = &tdb->lockrecs[i];
535*6a54128fSAndroid Build Coastguard Worker break;
536*6a54128fSAndroid Build Coastguard Worker }
537*6a54128fSAndroid Build Coastguard Worker }
538*6a54128fSAndroid Build Coastguard Worker
539*6a54128fSAndroid Build Coastguard Worker if ((lck == NULL) || (lck->count == 0)) {
540*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: count is 0\n"));
541*6a54128fSAndroid Build Coastguard Worker return -1;
542*6a54128fSAndroid Build Coastguard Worker }
543*6a54128fSAndroid Build Coastguard Worker
544*6a54128fSAndroid Build Coastguard Worker if (lck->count > 1) {
545*6a54128fSAndroid Build Coastguard Worker lck->count--;
546*6a54128fSAndroid Build Coastguard Worker return 0;
547*6a54128fSAndroid Build Coastguard Worker }
548*6a54128fSAndroid Build Coastguard Worker
549*6a54128fSAndroid Build Coastguard Worker /*
550*6a54128fSAndroid Build Coastguard Worker * This lock has count==1 left, so we need to unlock it in the
551*6a54128fSAndroid Build Coastguard Worker * kernel. We don't bother with decrementing the in-memory array
552*6a54128fSAndroid Build Coastguard Worker * element, we're about to overwrite it with the last array element
553*6a54128fSAndroid Build Coastguard Worker * anyway.
554*6a54128fSAndroid Build Coastguard Worker */
555*6a54128fSAndroid Build Coastguard Worker
556*6a54128fSAndroid Build Coastguard Worker if (mark_lock) {
557*6a54128fSAndroid Build Coastguard Worker ret = 0;
558*6a54128fSAndroid Build Coastguard Worker } else {
559*6a54128fSAndroid Build Coastguard Worker ret = tdb->methods->tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK,
560*6a54128fSAndroid Build Coastguard Worker F_SETLKW, 0, 1);
561*6a54128fSAndroid Build Coastguard Worker }
562*6a54128fSAndroid Build Coastguard Worker tdb->num_locks--;
563*6a54128fSAndroid Build Coastguard Worker
564*6a54128fSAndroid Build Coastguard Worker /*
565*6a54128fSAndroid Build Coastguard Worker * Shrink the array by overwriting the element just unlocked with the
566*6a54128fSAndroid Build Coastguard Worker * last array element.
567*6a54128fSAndroid Build Coastguard Worker */
568*6a54128fSAndroid Build Coastguard Worker
569*6a54128fSAndroid Build Coastguard Worker if (tdb->num_lockrecs > 1) {
570*6a54128fSAndroid Build Coastguard Worker *lck = tdb->lockrecs[tdb->num_lockrecs-1];
571*6a54128fSAndroid Build Coastguard Worker }
572*6a54128fSAndroid Build Coastguard Worker tdb->num_lockrecs -= 1;
573*6a54128fSAndroid Build Coastguard Worker
574*6a54128fSAndroid Build Coastguard Worker /*
575*6a54128fSAndroid Build Coastguard Worker * We don't bother with realloc when the array shrinks, but if we have
576*6a54128fSAndroid Build Coastguard Worker * a completely idle tdb we should get rid of the locked array.
577*6a54128fSAndroid Build Coastguard Worker */
578*6a54128fSAndroid Build Coastguard Worker
579*6a54128fSAndroid Build Coastguard Worker if (tdb->num_lockrecs == 0) {
580*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb->lockrecs);
581*6a54128fSAndroid Build Coastguard Worker }
582*6a54128fSAndroid Build Coastguard Worker
583*6a54128fSAndroid Build Coastguard Worker if (ret)
584*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: An error occurred unlocking!\n"));
585*6a54128fSAndroid Build Coastguard Worker return ret;
586*6a54128fSAndroid Build Coastguard Worker }
587*6a54128fSAndroid Build Coastguard Worker
588*6a54128fSAndroid Build Coastguard Worker /*
589*6a54128fSAndroid Build Coastguard Worker get the transaction lock
590*6a54128fSAndroid Build Coastguard Worker */
tdb_transaction_lock(struct tdb_context * tdb,int ltype)591*6a54128fSAndroid Build Coastguard Worker int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
592*6a54128fSAndroid Build Coastguard Worker {
593*6a54128fSAndroid Build Coastguard Worker if (tdb->have_transaction_lock || tdb->global_lock.count) {
594*6a54128fSAndroid Build Coastguard Worker return 0;
595*6a54128fSAndroid Build Coastguard Worker }
596*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, ltype,
597*6a54128fSAndroid Build Coastguard Worker F_SETLKW, 0, 1) == -1) {
598*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_lock: failed to get transaction lock\n"));
599*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_LOCK;
600*6a54128fSAndroid Build Coastguard Worker return -1;
601*6a54128fSAndroid Build Coastguard Worker }
602*6a54128fSAndroid Build Coastguard Worker tdb->have_transaction_lock = 1;
603*6a54128fSAndroid Build Coastguard Worker return 0;
604*6a54128fSAndroid Build Coastguard Worker }
605*6a54128fSAndroid Build Coastguard Worker
606*6a54128fSAndroid Build Coastguard Worker /*
607*6a54128fSAndroid Build Coastguard Worker release the transaction lock
608*6a54128fSAndroid Build Coastguard Worker */
tdb_transaction_unlock(struct tdb_context * tdb)609*6a54128fSAndroid Build Coastguard Worker int tdb_transaction_unlock(struct tdb_context *tdb)
610*6a54128fSAndroid Build Coastguard Worker {
611*6a54128fSAndroid Build Coastguard Worker int ret;
612*6a54128fSAndroid Build Coastguard Worker if (!tdb->have_transaction_lock) {
613*6a54128fSAndroid Build Coastguard Worker return 0;
614*6a54128fSAndroid Build Coastguard Worker }
615*6a54128fSAndroid Build Coastguard Worker ret = tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, F_UNLCK, F_SETLKW, 0, 1);
616*6a54128fSAndroid Build Coastguard Worker if (ret == 0) {
617*6a54128fSAndroid Build Coastguard Worker tdb->have_transaction_lock = 0;
618*6a54128fSAndroid Build Coastguard Worker }
619*6a54128fSAndroid Build Coastguard Worker return ret;
620*6a54128fSAndroid Build Coastguard Worker }
621*6a54128fSAndroid Build Coastguard Worker
622*6a54128fSAndroid Build Coastguard Worker
623*6a54128fSAndroid Build Coastguard Worker
624*6a54128fSAndroid Build Coastguard Worker
625*6a54128fSAndroid Build Coastguard Worker /* lock/unlock entire database */
_tdb_lockall(struct tdb_context * tdb,int ltype,int op)626*6a54128fSAndroid Build Coastguard Worker static int _tdb_lockall(struct tdb_context *tdb, int ltype, int op)
627*6a54128fSAndroid Build Coastguard Worker {
628*6a54128fSAndroid Build Coastguard Worker bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
629*6a54128fSAndroid Build Coastguard Worker
630*6a54128fSAndroid Build Coastguard Worker ltype &= ~TDB_MARK_LOCK;
631*6a54128fSAndroid Build Coastguard Worker
632*6a54128fSAndroid Build Coastguard Worker /* There are no locks on read-only dbs */
633*6a54128fSAndroid Build Coastguard Worker if (tdb->read_only || tdb->traverse_read)
634*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_LOCK, -1);
635*6a54128fSAndroid Build Coastguard Worker
636*6a54128fSAndroid Build Coastguard Worker if (tdb->global_lock.count && tdb->global_lock.ltype == (u32)ltype) {
637*6a54128fSAndroid Build Coastguard Worker tdb->global_lock.count++;
638*6a54128fSAndroid Build Coastguard Worker return 0;
639*6a54128fSAndroid Build Coastguard Worker }
640*6a54128fSAndroid Build Coastguard Worker
641*6a54128fSAndroid Build Coastguard Worker if (tdb->global_lock.count) {
642*6a54128fSAndroid Build Coastguard Worker /* a global lock of a different type exists */
643*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_LOCK, -1);
644*6a54128fSAndroid Build Coastguard Worker }
645*6a54128fSAndroid Build Coastguard Worker
646*6a54128fSAndroid Build Coastguard Worker if (tdb->num_locks != 0) {
647*6a54128fSAndroid Build Coastguard Worker /* can't combine global and chain locks */
648*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_LOCK, -1);
649*6a54128fSAndroid Build Coastguard Worker }
650*6a54128fSAndroid Build Coastguard Worker
651*6a54128fSAndroid Build Coastguard Worker if (!mark_lock &&
652*6a54128fSAndroid Build Coastguard Worker tdb->methods->tdb_brlock(tdb, FREELIST_TOP, ltype, op,
653*6a54128fSAndroid Build Coastguard Worker 0, 4*tdb->header.hash_size)) {
654*6a54128fSAndroid Build Coastguard Worker if (op == F_SETLKW) {
655*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lockall failed (%s)\n", strerror(errno)));
656*6a54128fSAndroid Build Coastguard Worker }
657*6a54128fSAndroid Build Coastguard Worker return -1;
658*6a54128fSAndroid Build Coastguard Worker }
659*6a54128fSAndroid Build Coastguard Worker
660*6a54128fSAndroid Build Coastguard Worker tdb->global_lock.count = 1;
661*6a54128fSAndroid Build Coastguard Worker tdb->global_lock.ltype = ltype;
662*6a54128fSAndroid Build Coastguard Worker
663*6a54128fSAndroid Build Coastguard Worker return 0;
664*6a54128fSAndroid Build Coastguard Worker }
665*6a54128fSAndroid Build Coastguard Worker
666*6a54128fSAndroid Build Coastguard Worker
667*6a54128fSAndroid Build Coastguard Worker
668*6a54128fSAndroid Build Coastguard Worker /* unlock entire db */
_tdb_unlockall(struct tdb_context * tdb,int ltype)669*6a54128fSAndroid Build Coastguard Worker static int _tdb_unlockall(struct tdb_context *tdb, int ltype)
670*6a54128fSAndroid Build Coastguard Worker {
671*6a54128fSAndroid Build Coastguard Worker bool mark_lock = ((ltype & TDB_MARK_LOCK) == TDB_MARK_LOCK);
672*6a54128fSAndroid Build Coastguard Worker
673*6a54128fSAndroid Build Coastguard Worker ltype &= ~TDB_MARK_LOCK;
674*6a54128fSAndroid Build Coastguard Worker
675*6a54128fSAndroid Build Coastguard Worker /* There are no locks on read-only dbs */
676*6a54128fSAndroid Build Coastguard Worker if (tdb->read_only || tdb->traverse_read) {
677*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_LOCK, -1);
678*6a54128fSAndroid Build Coastguard Worker }
679*6a54128fSAndroid Build Coastguard Worker
680*6a54128fSAndroid Build Coastguard Worker if (tdb->global_lock.ltype != (u32)ltype ||
681*6a54128fSAndroid Build Coastguard Worker tdb->global_lock.count == 0) {
682*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_LOCK, -1);
683*6a54128fSAndroid Build Coastguard Worker }
684*6a54128fSAndroid Build Coastguard Worker
685*6a54128fSAndroid Build Coastguard Worker if (tdb->global_lock.count > 1) {
686*6a54128fSAndroid Build Coastguard Worker tdb->global_lock.count--;
687*6a54128fSAndroid Build Coastguard Worker return 0;
688*6a54128fSAndroid Build Coastguard Worker }
689*6a54128fSAndroid Build Coastguard Worker
690*6a54128fSAndroid Build Coastguard Worker if (!mark_lock &&
691*6a54128fSAndroid Build Coastguard Worker tdb->methods->tdb_brlock(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW,
692*6a54128fSAndroid Build Coastguard Worker 0, 4*tdb->header.hash_size)) {
693*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
694*6a54128fSAndroid Build Coastguard Worker return -1;
695*6a54128fSAndroid Build Coastguard Worker }
696*6a54128fSAndroid Build Coastguard Worker
697*6a54128fSAndroid Build Coastguard Worker tdb->global_lock.count = 0;
698*6a54128fSAndroid Build Coastguard Worker tdb->global_lock.ltype = 0;
699*6a54128fSAndroid Build Coastguard Worker
700*6a54128fSAndroid Build Coastguard Worker return 0;
701*6a54128fSAndroid Build Coastguard Worker }
702*6a54128fSAndroid Build Coastguard Worker
703*6a54128fSAndroid Build Coastguard Worker /* lock entire database with write lock */
tdb_lockall(struct tdb_context * tdb)704*6a54128fSAndroid Build Coastguard Worker int tdb_lockall(struct tdb_context *tdb)
705*6a54128fSAndroid Build Coastguard Worker {
706*6a54128fSAndroid Build Coastguard Worker return _tdb_lockall(tdb, F_WRLCK, F_SETLKW);
707*6a54128fSAndroid Build Coastguard Worker }
708*6a54128fSAndroid Build Coastguard Worker
709*6a54128fSAndroid Build Coastguard Worker /* lock entire database with write lock - mark only */
tdb_lockall_mark(struct tdb_context * tdb)710*6a54128fSAndroid Build Coastguard Worker int tdb_lockall_mark(struct tdb_context *tdb)
711*6a54128fSAndroid Build Coastguard Worker {
712*6a54128fSAndroid Build Coastguard Worker return _tdb_lockall(tdb, F_WRLCK | TDB_MARK_LOCK, F_SETLKW);
713*6a54128fSAndroid Build Coastguard Worker }
714*6a54128fSAndroid Build Coastguard Worker
715*6a54128fSAndroid Build Coastguard Worker /* unlock entire database with write lock - unmark only */
tdb_lockall_unmark(struct tdb_context * tdb)716*6a54128fSAndroid Build Coastguard Worker int tdb_lockall_unmark(struct tdb_context *tdb)
717*6a54128fSAndroid Build Coastguard Worker {
718*6a54128fSAndroid Build Coastguard Worker return _tdb_unlockall(tdb, F_WRLCK | TDB_MARK_LOCK);
719*6a54128fSAndroid Build Coastguard Worker }
720*6a54128fSAndroid Build Coastguard Worker
721*6a54128fSAndroid Build Coastguard Worker /* lock entire database with write lock - nonblocking variant */
tdb_lockall_nonblock(struct tdb_context * tdb)722*6a54128fSAndroid Build Coastguard Worker int tdb_lockall_nonblock(struct tdb_context *tdb)
723*6a54128fSAndroid Build Coastguard Worker {
724*6a54128fSAndroid Build Coastguard Worker return _tdb_lockall(tdb, F_WRLCK, F_SETLK);
725*6a54128fSAndroid Build Coastguard Worker }
726*6a54128fSAndroid Build Coastguard Worker
727*6a54128fSAndroid Build Coastguard Worker /* unlock entire database with write lock */
tdb_unlockall(struct tdb_context * tdb)728*6a54128fSAndroid Build Coastguard Worker int tdb_unlockall(struct tdb_context *tdb)
729*6a54128fSAndroid Build Coastguard Worker {
730*6a54128fSAndroid Build Coastguard Worker return _tdb_unlockall(tdb, F_WRLCK);
731*6a54128fSAndroid Build Coastguard Worker }
732*6a54128fSAndroid Build Coastguard Worker
733*6a54128fSAndroid Build Coastguard Worker /* lock entire database with read lock */
tdb_lockall_read(struct tdb_context * tdb)734*6a54128fSAndroid Build Coastguard Worker int tdb_lockall_read(struct tdb_context *tdb)
735*6a54128fSAndroid Build Coastguard Worker {
736*6a54128fSAndroid Build Coastguard Worker return _tdb_lockall(tdb, F_RDLCK, F_SETLKW);
737*6a54128fSAndroid Build Coastguard Worker }
738*6a54128fSAndroid Build Coastguard Worker
739*6a54128fSAndroid Build Coastguard Worker /* lock entire database with read lock - nonblock variant */
tdb_lockall_read_nonblock(struct tdb_context * tdb)740*6a54128fSAndroid Build Coastguard Worker int tdb_lockall_read_nonblock(struct tdb_context *tdb)
741*6a54128fSAndroid Build Coastguard Worker {
742*6a54128fSAndroid Build Coastguard Worker return _tdb_lockall(tdb, F_RDLCK, F_SETLK);
743*6a54128fSAndroid Build Coastguard Worker }
744*6a54128fSAndroid Build Coastguard Worker
745*6a54128fSAndroid Build Coastguard Worker /* unlock entire database with read lock */
tdb_unlockall_read(struct tdb_context * tdb)746*6a54128fSAndroid Build Coastguard Worker int tdb_unlockall_read(struct tdb_context *tdb)
747*6a54128fSAndroid Build Coastguard Worker {
748*6a54128fSAndroid Build Coastguard Worker return _tdb_unlockall(tdb, F_RDLCK);
749*6a54128fSAndroid Build Coastguard Worker }
750*6a54128fSAndroid Build Coastguard Worker
751*6a54128fSAndroid Build Coastguard Worker /* lock/unlock one hash chain. This is meant to be used to reduce
752*6a54128fSAndroid Build Coastguard Worker contention - it cannot guarantee how many records will be locked */
tdb_chainlock(struct tdb_context * tdb,TDB_DATA key)753*6a54128fSAndroid Build Coastguard Worker int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
754*6a54128fSAndroid Build Coastguard Worker {
755*6a54128fSAndroid Build Coastguard Worker return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
756*6a54128fSAndroid Build Coastguard Worker }
757*6a54128fSAndroid Build Coastguard Worker
758*6a54128fSAndroid Build Coastguard Worker /* lock/unlock one hash chain, non-blocking. This is meant to be used
759*6a54128fSAndroid Build Coastguard Worker to reduce contention - it cannot guarantee how many records will be
760*6a54128fSAndroid Build Coastguard Worker locked */
tdb_chainlock_nonblock(struct tdb_context * tdb,TDB_DATA key)761*6a54128fSAndroid Build Coastguard Worker int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
762*6a54128fSAndroid Build Coastguard Worker {
763*6a54128fSAndroid Build Coastguard Worker return tdb_lock_nonblock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
764*6a54128fSAndroid Build Coastguard Worker }
765*6a54128fSAndroid Build Coastguard Worker
766*6a54128fSAndroid Build Coastguard Worker /* mark a chain as locked without actually locking it. Warning! use with great caution! */
tdb_chainlock_mark(struct tdb_context * tdb,TDB_DATA key)767*6a54128fSAndroid Build Coastguard Worker int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key)
768*6a54128fSAndroid Build Coastguard Worker {
769*6a54128fSAndroid Build Coastguard Worker return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK | TDB_MARK_LOCK);
770*6a54128fSAndroid Build Coastguard Worker }
771*6a54128fSAndroid Build Coastguard Worker
772*6a54128fSAndroid Build Coastguard Worker /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
tdb_chainlock_unmark(struct tdb_context * tdb,TDB_DATA key)773*6a54128fSAndroid Build Coastguard Worker int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key)
774*6a54128fSAndroid Build Coastguard Worker {
775*6a54128fSAndroid Build Coastguard Worker return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK | TDB_MARK_LOCK);
776*6a54128fSAndroid Build Coastguard Worker }
777*6a54128fSAndroid Build Coastguard Worker
tdb_chainunlock(struct tdb_context * tdb,TDB_DATA key)778*6a54128fSAndroid Build Coastguard Worker int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
779*6a54128fSAndroid Build Coastguard Worker {
780*6a54128fSAndroid Build Coastguard Worker return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
781*6a54128fSAndroid Build Coastguard Worker }
782*6a54128fSAndroid Build Coastguard Worker
tdb_chainlock_read(struct tdb_context * tdb,TDB_DATA key)783*6a54128fSAndroid Build Coastguard Worker int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
784*6a54128fSAndroid Build Coastguard Worker {
785*6a54128fSAndroid Build Coastguard Worker return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
786*6a54128fSAndroid Build Coastguard Worker }
787*6a54128fSAndroid Build Coastguard Worker
tdb_chainunlock_read(struct tdb_context * tdb,TDB_DATA key)788*6a54128fSAndroid Build Coastguard Worker int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
789*6a54128fSAndroid Build Coastguard Worker {
790*6a54128fSAndroid Build Coastguard Worker return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
791*6a54128fSAndroid Build Coastguard Worker }
792*6a54128fSAndroid Build Coastguard Worker
793*6a54128fSAndroid Build Coastguard Worker
794*6a54128fSAndroid Build Coastguard Worker
795*6a54128fSAndroid Build Coastguard Worker /* record lock stops delete underneath */
tdb_lock_record(struct tdb_context * tdb,tdb_off_t off)796*6a54128fSAndroid Build Coastguard Worker int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
797*6a54128fSAndroid Build Coastguard Worker {
798*6a54128fSAndroid Build Coastguard Worker return off ? tdb->methods->tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0, 1) : 0;
799*6a54128fSAndroid Build Coastguard Worker }
800*6a54128fSAndroid Build Coastguard Worker
801*6a54128fSAndroid Build Coastguard Worker /*
802*6a54128fSAndroid Build Coastguard Worker Write locks override our own fcntl readlocks, so check it here.
803*6a54128fSAndroid Build Coastguard Worker Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
804*6a54128fSAndroid Build Coastguard Worker an error to fail to get the lock here.
805*6a54128fSAndroid Build Coastguard Worker */
tdb_write_lock_record(struct tdb_context * tdb,tdb_off_t off)806*6a54128fSAndroid Build Coastguard Worker int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
807*6a54128fSAndroid Build Coastguard Worker {
808*6a54128fSAndroid Build Coastguard Worker struct tdb_traverse_lock *i;
809*6a54128fSAndroid Build Coastguard Worker for (i = &tdb->travlocks; i; i = i->next)
810*6a54128fSAndroid Build Coastguard Worker if (i->off == off)
811*6a54128fSAndroid Build Coastguard Worker return -1;
812*6a54128fSAndroid Build Coastguard Worker return tdb->methods->tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1, 1);
813*6a54128fSAndroid Build Coastguard Worker }
814*6a54128fSAndroid Build Coastguard Worker
815*6a54128fSAndroid Build Coastguard Worker /*
816*6a54128fSAndroid Build Coastguard Worker Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
817*6a54128fSAndroid Build Coastguard Worker an error to fail to get the lock here.
818*6a54128fSAndroid Build Coastguard Worker */
tdb_write_unlock_record(struct tdb_context * tdb,tdb_off_t off)819*6a54128fSAndroid Build Coastguard Worker int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
820*6a54128fSAndroid Build Coastguard Worker {
821*6a54128fSAndroid Build Coastguard Worker return tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0, 1);
822*6a54128fSAndroid Build Coastguard Worker }
823*6a54128fSAndroid Build Coastguard Worker
824*6a54128fSAndroid Build Coastguard Worker /* fcntl locks don't stack: avoid unlocking someone else's */
tdb_unlock_record(struct tdb_context * tdb,tdb_off_t off)825*6a54128fSAndroid Build Coastguard Worker int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
826*6a54128fSAndroid Build Coastguard Worker {
827*6a54128fSAndroid Build Coastguard Worker struct tdb_traverse_lock *i;
828*6a54128fSAndroid Build Coastguard Worker u32 count = 0;
829*6a54128fSAndroid Build Coastguard Worker
830*6a54128fSAndroid Build Coastguard Worker if (off == 0)
831*6a54128fSAndroid Build Coastguard Worker return 0;
832*6a54128fSAndroid Build Coastguard Worker for (i = &tdb->travlocks; i; i = i->next)
833*6a54128fSAndroid Build Coastguard Worker if (i->off == off)
834*6a54128fSAndroid Build Coastguard Worker count++;
835*6a54128fSAndroid Build Coastguard Worker return (count == 1 ? tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0, 1) : 0);
836*6a54128fSAndroid Build Coastguard Worker }
837*6a54128fSAndroid Build Coastguard Worker
838*6a54128fSAndroid Build Coastguard Worker /* file: io.c */
839*6a54128fSAndroid Build Coastguard Worker
840*6a54128fSAndroid Build Coastguard Worker /* check for an out of bounds access - if it is out of bounds then
841*6a54128fSAndroid Build Coastguard Worker see if the database has been expanded by someone else and expand
842*6a54128fSAndroid Build Coastguard Worker if necessary
843*6a54128fSAndroid Build Coastguard Worker note that "len" is the minimum length needed for the db
844*6a54128fSAndroid Build Coastguard Worker */
tdb_oob(struct tdb_context * tdb,tdb_off_t len,int probe)845*6a54128fSAndroid Build Coastguard Worker static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
846*6a54128fSAndroid Build Coastguard Worker {
847*6a54128fSAndroid Build Coastguard Worker struct stat st;
848*6a54128fSAndroid Build Coastguard Worker if (len <= tdb->map_size)
849*6a54128fSAndroid Build Coastguard Worker return 0;
850*6a54128fSAndroid Build Coastguard Worker if (tdb->flags & TDB_INTERNAL) {
851*6a54128fSAndroid Build Coastguard Worker if (!probe) {
852*6a54128fSAndroid Build Coastguard Worker /* Ensure ecode is set for log fn. */
853*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
854*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond internal malloc size %d\n",
855*6a54128fSAndroid Build Coastguard Worker (int)len, (int)tdb->map_size));
856*6a54128fSAndroid Build Coastguard Worker }
857*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_IO, -1);
858*6a54128fSAndroid Build Coastguard Worker }
859*6a54128fSAndroid Build Coastguard Worker
860*6a54128fSAndroid Build Coastguard Worker if (fstat(tdb->fd, &st) == -1) {
861*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_IO, -1);
862*6a54128fSAndroid Build Coastguard Worker }
863*6a54128fSAndroid Build Coastguard Worker
864*6a54128fSAndroid Build Coastguard Worker if (st.st_size < (off_t)len) {
865*6a54128fSAndroid Build Coastguard Worker if (!probe) {
866*6a54128fSAndroid Build Coastguard Worker /* Ensure ecode is set for log fn. */
867*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
868*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond eof at %d\n",
869*6a54128fSAndroid Build Coastguard Worker (int)len, (int)st.st_size));
870*6a54128fSAndroid Build Coastguard Worker }
871*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_IO, -1);
872*6a54128fSAndroid Build Coastguard Worker }
873*6a54128fSAndroid Build Coastguard Worker
874*6a54128fSAndroid Build Coastguard Worker /* Unmap, update size, remap */
875*6a54128fSAndroid Build Coastguard Worker if (tdb_munmap(tdb) == -1)
876*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_IO, -1);
877*6a54128fSAndroid Build Coastguard Worker tdb->map_size = st.st_size;
878*6a54128fSAndroid Build Coastguard Worker tdb_mmap(tdb);
879*6a54128fSAndroid Build Coastguard Worker return 0;
880*6a54128fSAndroid Build Coastguard Worker }
881*6a54128fSAndroid Build Coastguard Worker
882*6a54128fSAndroid Build Coastguard Worker /* write a lump of data at a specified offset */
tdb_write(struct tdb_context * tdb,tdb_off_t off,const void * buf,tdb_len_t len)883*6a54128fSAndroid Build Coastguard Worker static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
884*6a54128fSAndroid Build Coastguard Worker const void *buf, tdb_len_t len)
885*6a54128fSAndroid Build Coastguard Worker {
886*6a54128fSAndroid Build Coastguard Worker if (len == 0) {
887*6a54128fSAndroid Build Coastguard Worker return 0;
888*6a54128fSAndroid Build Coastguard Worker }
889*6a54128fSAndroid Build Coastguard Worker
890*6a54128fSAndroid Build Coastguard Worker if (tdb->read_only || tdb->traverse_read) {
891*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_RDONLY;
892*6a54128fSAndroid Build Coastguard Worker return -1;
893*6a54128fSAndroid Build Coastguard Worker }
894*6a54128fSAndroid Build Coastguard Worker
895*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_oob(tdb, off + len, 0) != 0)
896*6a54128fSAndroid Build Coastguard Worker return -1;
897*6a54128fSAndroid Build Coastguard Worker
898*6a54128fSAndroid Build Coastguard Worker if (tdb->map_ptr) {
899*6a54128fSAndroid Build Coastguard Worker memcpy(off + (char *)tdb->map_ptr, buf, len);
900*6a54128fSAndroid Build Coastguard Worker } else if (pwrite(tdb->fd, buf, len, off) != (ssize_t)len) {
901*6a54128fSAndroid Build Coastguard Worker /* Ensure ecode is set for log fn. */
902*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
903*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_write failed at %d len=%d (%s)\n",
904*6a54128fSAndroid Build Coastguard Worker off, len, strerror(errno)));
905*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_IO, -1);
906*6a54128fSAndroid Build Coastguard Worker }
907*6a54128fSAndroid Build Coastguard Worker return 0;
908*6a54128fSAndroid Build Coastguard Worker }
909*6a54128fSAndroid Build Coastguard Worker
910*6a54128fSAndroid Build Coastguard Worker /* Endian conversion: we only ever deal with 4 byte quantities */
tdb_convert(void * buf,u32 size)911*6a54128fSAndroid Build Coastguard Worker void *tdb_convert(void *buf, u32 size)
912*6a54128fSAndroid Build Coastguard Worker {
913*6a54128fSAndroid Build Coastguard Worker u32 i, *p = (u32 *)buf;
914*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < size / 4; i++)
915*6a54128fSAndroid Build Coastguard Worker p[i] = TDB_BYTEREV(p[i]);
916*6a54128fSAndroid Build Coastguard Worker return buf;
917*6a54128fSAndroid Build Coastguard Worker }
918*6a54128fSAndroid Build Coastguard Worker
919*6a54128fSAndroid Build Coastguard Worker
920*6a54128fSAndroid Build Coastguard Worker /* read a lump of data at a specified offset, maybe convert */
tdb_read(struct tdb_context * tdb,tdb_off_t off,void * buf,tdb_len_t len,int cv)921*6a54128fSAndroid Build Coastguard Worker static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
922*6a54128fSAndroid Build Coastguard Worker tdb_len_t len, int cv)
923*6a54128fSAndroid Build Coastguard Worker {
924*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_oob(tdb, off + len, 0) != 0) {
925*6a54128fSAndroid Build Coastguard Worker return -1;
926*6a54128fSAndroid Build Coastguard Worker }
927*6a54128fSAndroid Build Coastguard Worker
928*6a54128fSAndroid Build Coastguard Worker if (tdb->map_ptr) {
929*6a54128fSAndroid Build Coastguard Worker memcpy(buf, off + (char *)tdb->map_ptr, len);
930*6a54128fSAndroid Build Coastguard Worker } else {
931*6a54128fSAndroid Build Coastguard Worker ssize_t ret = pread(tdb->fd, buf, len, off);
932*6a54128fSAndroid Build Coastguard Worker if (ret != (ssize_t)len) {
933*6a54128fSAndroid Build Coastguard Worker /* Ensure ecode is set for log fn. */
934*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
935*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_read failed at %d "
936*6a54128fSAndroid Build Coastguard Worker "len=%d ret=%d (%s) map_size=%d\n",
937*6a54128fSAndroid Build Coastguard Worker (int)off, (int)len, (int)ret, strerror(errno),
938*6a54128fSAndroid Build Coastguard Worker (int)tdb->map_size));
939*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_IO, -1);
940*6a54128fSAndroid Build Coastguard Worker }
941*6a54128fSAndroid Build Coastguard Worker }
942*6a54128fSAndroid Build Coastguard Worker if (cv) {
943*6a54128fSAndroid Build Coastguard Worker tdb_convert(buf, len);
944*6a54128fSAndroid Build Coastguard Worker }
945*6a54128fSAndroid Build Coastguard Worker return 0;
946*6a54128fSAndroid Build Coastguard Worker }
947*6a54128fSAndroid Build Coastguard Worker
948*6a54128fSAndroid Build Coastguard Worker
949*6a54128fSAndroid Build Coastguard Worker
950*6a54128fSAndroid Build Coastguard Worker /*
951*6a54128fSAndroid Build Coastguard Worker do an unlocked scan of the hash table heads to find the next non-zero head. The value
952*6a54128fSAndroid Build Coastguard Worker will then be confirmed with the lock held
953*6a54128fSAndroid Build Coastguard Worker */
tdb_next_hash_chain(struct tdb_context * tdb,u32 * chain)954*6a54128fSAndroid Build Coastguard Worker static void tdb_next_hash_chain(struct tdb_context *tdb, u32 *chain)
955*6a54128fSAndroid Build Coastguard Worker {
956*6a54128fSAndroid Build Coastguard Worker u32 h = *chain;
957*6a54128fSAndroid Build Coastguard Worker if (tdb->map_ptr) {
958*6a54128fSAndroid Build Coastguard Worker for (;h < tdb->header.hash_size;h++) {
959*6a54128fSAndroid Build Coastguard Worker if (0 != *(u32 *)(TDB_HASH_TOP(h) + (unsigned char *)tdb->map_ptr)) {
960*6a54128fSAndroid Build Coastguard Worker break;
961*6a54128fSAndroid Build Coastguard Worker }
962*6a54128fSAndroid Build Coastguard Worker }
963*6a54128fSAndroid Build Coastguard Worker } else {
964*6a54128fSAndroid Build Coastguard Worker u32 off=0;
965*6a54128fSAndroid Build Coastguard Worker for (;h < tdb->header.hash_size;h++) {
966*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, TDB_HASH_TOP(h), &off) != 0 || off != 0) {
967*6a54128fSAndroid Build Coastguard Worker break;
968*6a54128fSAndroid Build Coastguard Worker }
969*6a54128fSAndroid Build Coastguard Worker }
970*6a54128fSAndroid Build Coastguard Worker }
971*6a54128fSAndroid Build Coastguard Worker (*chain) = h;
972*6a54128fSAndroid Build Coastguard Worker }
973*6a54128fSAndroid Build Coastguard Worker
974*6a54128fSAndroid Build Coastguard Worker
tdb_munmap(struct tdb_context * tdb)975*6a54128fSAndroid Build Coastguard Worker int tdb_munmap(struct tdb_context *tdb)
976*6a54128fSAndroid Build Coastguard Worker {
977*6a54128fSAndroid Build Coastguard Worker if (tdb->flags & TDB_INTERNAL)
978*6a54128fSAndroid Build Coastguard Worker return 0;
979*6a54128fSAndroid Build Coastguard Worker
980*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_MMAP
981*6a54128fSAndroid Build Coastguard Worker if (tdb->map_ptr) {
982*6a54128fSAndroid Build Coastguard Worker int ret = munmap(tdb->map_ptr, tdb->real_map_size);
983*6a54128fSAndroid Build Coastguard Worker if (ret != 0)
984*6a54128fSAndroid Build Coastguard Worker return ret;
985*6a54128fSAndroid Build Coastguard Worker tdb->real_map_size = 0;
986*6a54128fSAndroid Build Coastguard Worker }
987*6a54128fSAndroid Build Coastguard Worker #endif
988*6a54128fSAndroid Build Coastguard Worker tdb->map_ptr = NULL;
989*6a54128fSAndroid Build Coastguard Worker return 0;
990*6a54128fSAndroid Build Coastguard Worker }
991*6a54128fSAndroid Build Coastguard Worker
tdb_mmap(struct tdb_context * tdb)992*6a54128fSAndroid Build Coastguard Worker void tdb_mmap(struct tdb_context *tdb)
993*6a54128fSAndroid Build Coastguard Worker {
994*6a54128fSAndroid Build Coastguard Worker if (tdb->flags & TDB_INTERNAL)
995*6a54128fSAndroid Build Coastguard Worker return;
996*6a54128fSAndroid Build Coastguard Worker
997*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_MMAP
998*6a54128fSAndroid Build Coastguard Worker if (!(tdb->flags & TDB_NOMMAP)) {
999*6a54128fSAndroid Build Coastguard Worker tdb->map_ptr = mmap(NULL, tdb->map_size,
1000*6a54128fSAndroid Build Coastguard Worker PROT_READ|(tdb->read_only? 0:PROT_WRITE),
1001*6a54128fSAndroid Build Coastguard Worker MAP_SHARED|MAP_FILE, tdb->fd, 0);
1002*6a54128fSAndroid Build Coastguard Worker
1003*6a54128fSAndroid Build Coastguard Worker /*
1004*6a54128fSAndroid Build Coastguard Worker * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!!
1005*6a54128fSAndroid Build Coastguard Worker */
1006*6a54128fSAndroid Build Coastguard Worker
1007*6a54128fSAndroid Build Coastguard Worker if (tdb->map_ptr == MAP_FAILED) {
1008*6a54128fSAndroid Build Coastguard Worker tdb->real_map_size = 0;
1009*6a54128fSAndroid Build Coastguard Worker tdb->map_ptr = NULL;
1010*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_mmap failed for size %d (%s)\n",
1011*6a54128fSAndroid Build Coastguard Worker tdb->map_size, strerror(errno)));
1012*6a54128fSAndroid Build Coastguard Worker }
1013*6a54128fSAndroid Build Coastguard Worker tdb->real_map_size = tdb->map_size;
1014*6a54128fSAndroid Build Coastguard Worker } else {
1015*6a54128fSAndroid Build Coastguard Worker tdb->map_ptr = NULL;
1016*6a54128fSAndroid Build Coastguard Worker }
1017*6a54128fSAndroid Build Coastguard Worker #else
1018*6a54128fSAndroid Build Coastguard Worker tdb->map_ptr = NULL;
1019*6a54128fSAndroid Build Coastguard Worker #endif
1020*6a54128fSAndroid Build Coastguard Worker }
1021*6a54128fSAndroid Build Coastguard Worker
1022*6a54128fSAndroid Build Coastguard Worker /* expand a file. we prefer to use ftruncate, as that is what posix
1023*6a54128fSAndroid Build Coastguard Worker says to use for mmap expansion */
tdb_expand_file(struct tdb_context * tdb,tdb_off_t size,tdb_off_t addition)1024*6a54128fSAndroid Build Coastguard Worker static int tdb_expand_file(struct tdb_context *tdb, tdb_off_t size, tdb_off_t addition)
1025*6a54128fSAndroid Build Coastguard Worker {
1026*6a54128fSAndroid Build Coastguard Worker char buf[1024];
1027*6a54128fSAndroid Build Coastguard Worker
1028*6a54128fSAndroid Build Coastguard Worker if (tdb->read_only || tdb->traverse_read) {
1029*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_RDONLY;
1030*6a54128fSAndroid Build Coastguard Worker return -1;
1031*6a54128fSAndroid Build Coastguard Worker }
1032*6a54128fSAndroid Build Coastguard Worker
1033*6a54128fSAndroid Build Coastguard Worker if (ftruncate(tdb->fd, size+addition) == -1) {
1034*6a54128fSAndroid Build Coastguard Worker char b = 0;
1035*6a54128fSAndroid Build Coastguard Worker if (pwrite(tdb->fd, &b, 1, (size+addition) - 1) != 1) {
1036*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file to %d failed (%s)\n",
1037*6a54128fSAndroid Build Coastguard Worker size+addition, strerror(errno)));
1038*6a54128fSAndroid Build Coastguard Worker return -1;
1039*6a54128fSAndroid Build Coastguard Worker }
1040*6a54128fSAndroid Build Coastguard Worker }
1041*6a54128fSAndroid Build Coastguard Worker
1042*6a54128fSAndroid Build Coastguard Worker /* now fill the file with something. This ensures that the
1043*6a54128fSAndroid Build Coastguard Worker file isn't sparse, which would be very bad if we ran out of
1044*6a54128fSAndroid Build Coastguard Worker disk. This must be done with write, not via mmap */
1045*6a54128fSAndroid Build Coastguard Worker memset(buf, TDB_PAD_BYTE, sizeof(buf));
1046*6a54128fSAndroid Build Coastguard Worker while (addition) {
1047*6a54128fSAndroid Build Coastguard Worker int n = addition>sizeof(buf)?sizeof(buf):addition;
1048*6a54128fSAndroid Build Coastguard Worker int ret = pwrite(tdb->fd, buf, n, size);
1049*6a54128fSAndroid Build Coastguard Worker if (ret != n) {
1050*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file write of %d failed (%s)\n",
1051*6a54128fSAndroid Build Coastguard Worker n, strerror(errno)));
1052*6a54128fSAndroid Build Coastguard Worker return -1;
1053*6a54128fSAndroid Build Coastguard Worker }
1054*6a54128fSAndroid Build Coastguard Worker addition -= n;
1055*6a54128fSAndroid Build Coastguard Worker size += n;
1056*6a54128fSAndroid Build Coastguard Worker }
1057*6a54128fSAndroid Build Coastguard Worker return 0;
1058*6a54128fSAndroid Build Coastguard Worker }
1059*6a54128fSAndroid Build Coastguard Worker
1060*6a54128fSAndroid Build Coastguard Worker
1061*6a54128fSAndroid Build Coastguard Worker /* expand the database at least size bytes by expanding the underlying
1062*6a54128fSAndroid Build Coastguard Worker file and doing the mmap again if necessary */
tdb_expand(struct tdb_context * tdb,tdb_off_t size)1063*6a54128fSAndroid Build Coastguard Worker int tdb_expand(struct tdb_context *tdb, tdb_off_t size)
1064*6a54128fSAndroid Build Coastguard Worker {
1065*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
1066*6a54128fSAndroid Build Coastguard Worker tdb_off_t offset;
1067*6a54128fSAndroid Build Coastguard Worker
1068*6a54128fSAndroid Build Coastguard Worker if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
1069*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "lock failed in tdb_expand\n"));
1070*6a54128fSAndroid Build Coastguard Worker return -1;
1071*6a54128fSAndroid Build Coastguard Worker }
1072*6a54128fSAndroid Build Coastguard Worker
1073*6a54128fSAndroid Build Coastguard Worker /* must know about any previous expansions by another process */
1074*6a54128fSAndroid Build Coastguard Worker tdb->methods->tdb_oob(tdb, tdb->map_size + 1, 1);
1075*6a54128fSAndroid Build Coastguard Worker
1076*6a54128fSAndroid Build Coastguard Worker /* always make room for at least 10 more records, and round
1077*6a54128fSAndroid Build Coastguard Worker the database up to a multiple of the page size */
1078*6a54128fSAndroid Build Coastguard Worker size = TDB_ALIGN(tdb->map_size + size*10, tdb->page_size) - tdb->map_size;
1079*6a54128fSAndroid Build Coastguard Worker
1080*6a54128fSAndroid Build Coastguard Worker if (!(tdb->flags & TDB_INTERNAL))
1081*6a54128fSAndroid Build Coastguard Worker tdb_munmap(tdb);
1082*6a54128fSAndroid Build Coastguard Worker
1083*6a54128fSAndroid Build Coastguard Worker /*
1084*6a54128fSAndroid Build Coastguard Worker * We must ensure the file is unmapped before doing this
1085*6a54128fSAndroid Build Coastguard Worker * to ensure consistency with systems like OpenBSD where
1086*6a54128fSAndroid Build Coastguard Worker * writes and mmaps are not consistent.
1087*6a54128fSAndroid Build Coastguard Worker */
1088*6a54128fSAndroid Build Coastguard Worker
1089*6a54128fSAndroid Build Coastguard Worker /* expand the file itself */
1090*6a54128fSAndroid Build Coastguard Worker if (!(tdb->flags & TDB_INTERNAL)) {
1091*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_expand_file(tdb, tdb->map_size, size) != 0)
1092*6a54128fSAndroid Build Coastguard Worker goto fail;
1093*6a54128fSAndroid Build Coastguard Worker }
1094*6a54128fSAndroid Build Coastguard Worker
1095*6a54128fSAndroid Build Coastguard Worker tdb->map_size += size;
1096*6a54128fSAndroid Build Coastguard Worker
1097*6a54128fSAndroid Build Coastguard Worker if (tdb->flags & TDB_INTERNAL) {
1098*6a54128fSAndroid Build Coastguard Worker char *new_map_ptr = (char *)realloc(tdb->map_ptr,
1099*6a54128fSAndroid Build Coastguard Worker tdb->map_size);
1100*6a54128fSAndroid Build Coastguard Worker if (!new_map_ptr) {
1101*6a54128fSAndroid Build Coastguard Worker tdb->map_size -= size;
1102*6a54128fSAndroid Build Coastguard Worker goto fail;
1103*6a54128fSAndroid Build Coastguard Worker }
1104*6a54128fSAndroid Build Coastguard Worker tdb->map_ptr = new_map_ptr;
1105*6a54128fSAndroid Build Coastguard Worker } else {
1106*6a54128fSAndroid Build Coastguard Worker /*
1107*6a54128fSAndroid Build Coastguard Worker * We must ensure the file is remapped before adding the space
1108*6a54128fSAndroid Build Coastguard Worker * to ensure consistency with systems like OpenBSD where
1109*6a54128fSAndroid Build Coastguard Worker * writes and mmaps are not consistent.
1110*6a54128fSAndroid Build Coastguard Worker */
1111*6a54128fSAndroid Build Coastguard Worker
1112*6a54128fSAndroid Build Coastguard Worker /* We're ok if the mmap fails as we'll fallback to read/write */
1113*6a54128fSAndroid Build Coastguard Worker tdb_mmap(tdb);
1114*6a54128fSAndroid Build Coastguard Worker }
1115*6a54128fSAndroid Build Coastguard Worker
1116*6a54128fSAndroid Build Coastguard Worker /* form a new freelist record */
1117*6a54128fSAndroid Build Coastguard Worker memset(&rec,'\0',sizeof(rec));
1118*6a54128fSAndroid Build Coastguard Worker rec.rec_len = size - sizeof(rec);
1119*6a54128fSAndroid Build Coastguard Worker
1120*6a54128fSAndroid Build Coastguard Worker /* link it into the free list */
1121*6a54128fSAndroid Build Coastguard Worker offset = tdb->map_size - size;
1122*6a54128fSAndroid Build Coastguard Worker if (tdb_free(tdb, offset, &rec) == -1)
1123*6a54128fSAndroid Build Coastguard Worker goto fail;
1124*6a54128fSAndroid Build Coastguard Worker
1125*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, -1, F_WRLCK);
1126*6a54128fSAndroid Build Coastguard Worker return 0;
1127*6a54128fSAndroid Build Coastguard Worker fail:
1128*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, -1, F_WRLCK);
1129*6a54128fSAndroid Build Coastguard Worker return -1;
1130*6a54128fSAndroid Build Coastguard Worker }
1131*6a54128fSAndroid Build Coastguard Worker
1132*6a54128fSAndroid Build Coastguard Worker /* read/write a tdb_off_t */
tdb_ofs_read(struct tdb_context * tdb,tdb_off_t offset,tdb_off_t * d)1133*6a54128fSAndroid Build Coastguard Worker int tdb_ofs_read(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d)
1134*6a54128fSAndroid Build Coastguard Worker {
1135*6a54128fSAndroid Build Coastguard Worker return tdb->methods->tdb_read(tdb, offset, (char*)d, sizeof(*d), DOCONV());
1136*6a54128fSAndroid Build Coastguard Worker }
1137*6a54128fSAndroid Build Coastguard Worker
tdb_ofs_write(struct tdb_context * tdb,tdb_off_t offset,tdb_off_t * d)1138*6a54128fSAndroid Build Coastguard Worker int tdb_ofs_write(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d)
1139*6a54128fSAndroid Build Coastguard Worker {
1140*6a54128fSAndroid Build Coastguard Worker tdb_off_t off = *d;
1141*6a54128fSAndroid Build Coastguard Worker return tdb->methods->tdb_write(tdb, offset, CONVERT(off), sizeof(*d));
1142*6a54128fSAndroid Build Coastguard Worker }
1143*6a54128fSAndroid Build Coastguard Worker
1144*6a54128fSAndroid Build Coastguard Worker
1145*6a54128fSAndroid Build Coastguard Worker /* read a lump of data, allocating the space for it */
tdb_alloc_read(struct tdb_context * tdb,tdb_off_t offset,tdb_len_t len)1146*6a54128fSAndroid Build Coastguard Worker unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len)
1147*6a54128fSAndroid Build Coastguard Worker {
1148*6a54128fSAndroid Build Coastguard Worker unsigned char *buf;
1149*6a54128fSAndroid Build Coastguard Worker
1150*6a54128fSAndroid Build Coastguard Worker /* some systems don't like zero length malloc */
1151*6a54128fSAndroid Build Coastguard Worker if (len == 0) {
1152*6a54128fSAndroid Build Coastguard Worker len = 1;
1153*6a54128fSAndroid Build Coastguard Worker }
1154*6a54128fSAndroid Build Coastguard Worker
1155*6a54128fSAndroid Build Coastguard Worker if (!(buf = (unsigned char *)malloc(len))) {
1156*6a54128fSAndroid Build Coastguard Worker /* Ensure ecode is set for log fn. */
1157*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_OOM;
1158*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_alloc_read malloc failed len=%d (%s)\n",
1159*6a54128fSAndroid Build Coastguard Worker len, strerror(errno)));
1160*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_OOM, buf);
1161*6a54128fSAndroid Build Coastguard Worker }
1162*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_read(tdb, offset, buf, len, 0) == -1) {
1163*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(buf);
1164*6a54128fSAndroid Build Coastguard Worker return NULL;
1165*6a54128fSAndroid Build Coastguard Worker }
1166*6a54128fSAndroid Build Coastguard Worker return buf;
1167*6a54128fSAndroid Build Coastguard Worker }
1168*6a54128fSAndroid Build Coastguard Worker
1169*6a54128fSAndroid Build Coastguard Worker /* Give a piece of tdb data to a parser */
1170*6a54128fSAndroid Build Coastguard Worker
tdb_parse_data(struct tdb_context * tdb,TDB_DATA key,tdb_off_t offset,tdb_len_t len,int (* parser)(TDB_DATA key,TDB_DATA data,void * private_data),void * private_data)1171*6a54128fSAndroid Build Coastguard Worker int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
1172*6a54128fSAndroid Build Coastguard Worker tdb_off_t offset, tdb_len_t len,
1173*6a54128fSAndroid Build Coastguard Worker int (*parser)(TDB_DATA key, TDB_DATA data,
1174*6a54128fSAndroid Build Coastguard Worker void *private_data),
1175*6a54128fSAndroid Build Coastguard Worker void *private_data)
1176*6a54128fSAndroid Build Coastguard Worker {
1177*6a54128fSAndroid Build Coastguard Worker TDB_DATA data;
1178*6a54128fSAndroid Build Coastguard Worker int result;
1179*6a54128fSAndroid Build Coastguard Worker
1180*6a54128fSAndroid Build Coastguard Worker data.dsize = len;
1181*6a54128fSAndroid Build Coastguard Worker
1182*6a54128fSAndroid Build Coastguard Worker if ((tdb->transaction == NULL) && (tdb->map_ptr != NULL)) {
1183*6a54128fSAndroid Build Coastguard Worker /*
1184*6a54128fSAndroid Build Coastguard Worker * Optimize by avoiding the malloc/memcpy/free, point the
1185*6a54128fSAndroid Build Coastguard Worker * parser directly at the mmap area.
1186*6a54128fSAndroid Build Coastguard Worker */
1187*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_oob(tdb, offset+len, 0) != 0) {
1188*6a54128fSAndroid Build Coastguard Worker return -1;
1189*6a54128fSAndroid Build Coastguard Worker }
1190*6a54128fSAndroid Build Coastguard Worker data.dptr = offset + (unsigned char *)tdb->map_ptr;
1191*6a54128fSAndroid Build Coastguard Worker return parser(key, data, private_data);
1192*6a54128fSAndroid Build Coastguard Worker }
1193*6a54128fSAndroid Build Coastguard Worker
1194*6a54128fSAndroid Build Coastguard Worker if (!(data.dptr = tdb_alloc_read(tdb, offset, len))) {
1195*6a54128fSAndroid Build Coastguard Worker return -1;
1196*6a54128fSAndroid Build Coastguard Worker }
1197*6a54128fSAndroid Build Coastguard Worker
1198*6a54128fSAndroid Build Coastguard Worker result = parser(key, data, private_data);
1199*6a54128fSAndroid Build Coastguard Worker free(data.dptr);
1200*6a54128fSAndroid Build Coastguard Worker return result;
1201*6a54128fSAndroid Build Coastguard Worker }
1202*6a54128fSAndroid Build Coastguard Worker
1203*6a54128fSAndroid Build Coastguard Worker /* read/write a record */
tdb_rec_read(struct tdb_context * tdb,tdb_off_t offset,struct list_struct * rec)1204*6a54128fSAndroid Build Coastguard Worker int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
1205*6a54128fSAndroid Build Coastguard Worker {
1206*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_read(tdb, offset, rec, sizeof(*rec),DOCONV()) == -1)
1207*6a54128fSAndroid Build Coastguard Worker return -1;
1208*6a54128fSAndroid Build Coastguard Worker if (TDB_BAD_MAGIC(rec)) {
1209*6a54128fSAndroid Build Coastguard Worker /* Ensure ecode is set for log fn. */
1210*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_CORRUPT;
1211*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_rec_read bad magic 0x%x at offset=%d\n", rec->magic, offset));
1212*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
1213*6a54128fSAndroid Build Coastguard Worker }
1214*6a54128fSAndroid Build Coastguard Worker return tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0);
1215*6a54128fSAndroid Build Coastguard Worker }
1216*6a54128fSAndroid Build Coastguard Worker
tdb_rec_write(struct tdb_context * tdb,tdb_off_t offset,struct list_struct * rec)1217*6a54128fSAndroid Build Coastguard Worker int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
1218*6a54128fSAndroid Build Coastguard Worker {
1219*6a54128fSAndroid Build Coastguard Worker struct list_struct r = *rec;
1220*6a54128fSAndroid Build Coastguard Worker return tdb->methods->tdb_write(tdb, offset, CONVERT(r), sizeof(r));
1221*6a54128fSAndroid Build Coastguard Worker }
1222*6a54128fSAndroid Build Coastguard Worker
1223*6a54128fSAndroid Build Coastguard Worker static const struct tdb_methods io_methods = {
1224*6a54128fSAndroid Build Coastguard Worker tdb_read,
1225*6a54128fSAndroid Build Coastguard Worker tdb_write,
1226*6a54128fSAndroid Build Coastguard Worker tdb_next_hash_chain,
1227*6a54128fSAndroid Build Coastguard Worker tdb_oob,
1228*6a54128fSAndroid Build Coastguard Worker tdb_expand_file,
1229*6a54128fSAndroid Build Coastguard Worker tdb_brlock
1230*6a54128fSAndroid Build Coastguard Worker };
1231*6a54128fSAndroid Build Coastguard Worker
1232*6a54128fSAndroid Build Coastguard Worker /*
1233*6a54128fSAndroid Build Coastguard Worker initialise the default methods table
1234*6a54128fSAndroid Build Coastguard Worker */
tdb_io_init(struct tdb_context * tdb)1235*6a54128fSAndroid Build Coastguard Worker void tdb_io_init(struct tdb_context *tdb)
1236*6a54128fSAndroid Build Coastguard Worker {
1237*6a54128fSAndroid Build Coastguard Worker tdb->methods = &io_methods;
1238*6a54128fSAndroid Build Coastguard Worker }
1239*6a54128fSAndroid Build Coastguard Worker
1240*6a54128fSAndroid Build Coastguard Worker /* file: transaction.c */
1241*6a54128fSAndroid Build Coastguard Worker
1242*6a54128fSAndroid Build Coastguard Worker /*
1243*6a54128fSAndroid Build Coastguard Worker transaction design:
1244*6a54128fSAndroid Build Coastguard Worker
1245*6a54128fSAndroid Build Coastguard Worker - only allow a single transaction at a time per database. This makes
1246*6a54128fSAndroid Build Coastguard Worker using the transaction API simpler, as otherwise the caller would
1247*6a54128fSAndroid Build Coastguard Worker have to cope with temporary failures in transactions that conflict
1248*6a54128fSAndroid Build Coastguard Worker with other current transactions
1249*6a54128fSAndroid Build Coastguard Worker
1250*6a54128fSAndroid Build Coastguard Worker - keep the transaction recovery information in the same file as the
1251*6a54128fSAndroid Build Coastguard Worker database, using a special 'transaction recovery' record pointed at
1252*6a54128fSAndroid Build Coastguard Worker by the header. This removes the need for extra journal files as
1253*6a54128fSAndroid Build Coastguard Worker used by some other databases
1254*6a54128fSAndroid Build Coastguard Worker
1255*6a54128fSAndroid Build Coastguard Worker - dynamically allocated the transaction recover record, re-using it
1256*6a54128fSAndroid Build Coastguard Worker for subsequent transactions. If a larger record is needed then
1257*6a54128fSAndroid Build Coastguard Worker tdb_free() the old record to place it on the normal tdb freelist
1258*6a54128fSAndroid Build Coastguard Worker before allocating the new record
1259*6a54128fSAndroid Build Coastguard Worker
1260*6a54128fSAndroid Build Coastguard Worker - during transactions, keep a linked list of writes all that have
1261*6a54128fSAndroid Build Coastguard Worker been performed by intercepting all tdb_write() calls. The hooked
1262*6a54128fSAndroid Build Coastguard Worker transaction versions of tdb_read() and tdb_write() check this
1263*6a54128fSAndroid Build Coastguard Worker linked list and try to use the elements of the list in preference
1264*6a54128fSAndroid Build Coastguard Worker to the real database.
1265*6a54128fSAndroid Build Coastguard Worker
1266*6a54128fSAndroid Build Coastguard Worker - don't allow any locks to be held when a transaction starts,
1267*6a54128fSAndroid Build Coastguard Worker otherwise we can end up with deadlock (plus lack of lock nesting
1268*6a54128fSAndroid Build Coastguard Worker in posix locks would mean the lock is lost)
1269*6a54128fSAndroid Build Coastguard Worker
1270*6a54128fSAndroid Build Coastguard Worker - if the caller gains a lock during the transaction but doesn't
1271*6a54128fSAndroid Build Coastguard Worker release it then fail the commit
1272*6a54128fSAndroid Build Coastguard Worker
1273*6a54128fSAndroid Build Coastguard Worker - allow for nested calls to tdb_transaction_start(), re-using the
1274*6a54128fSAndroid Build Coastguard Worker existing transaction record. If the inner transaction is cancelled
1275*6a54128fSAndroid Build Coastguard Worker then a subsequent commit will fail
1276*6a54128fSAndroid Build Coastguard Worker
1277*6a54128fSAndroid Build Coastguard Worker - keep a mirrored copy of the tdb hash chain heads to allow for the
1278*6a54128fSAndroid Build Coastguard Worker fast hash heads scan on traverse, updating the mirrored copy in
1279*6a54128fSAndroid Build Coastguard Worker the transaction version of tdb_write
1280*6a54128fSAndroid Build Coastguard Worker
1281*6a54128fSAndroid Build Coastguard Worker - allow callers to mix transaction and non-transaction use of tdb,
1282*6a54128fSAndroid Build Coastguard Worker although once a transaction is started then an exclusive lock is
1283*6a54128fSAndroid Build Coastguard Worker gained until the transaction is committed or cancelled
1284*6a54128fSAndroid Build Coastguard Worker
1285*6a54128fSAndroid Build Coastguard Worker - the commit strategy involves first saving away all modified data
1286*6a54128fSAndroid Build Coastguard Worker into a linearised buffer in the transaction recovery area, then
1287*6a54128fSAndroid Build Coastguard Worker marking the transaction recovery area with a magic value to
1288*6a54128fSAndroid Build Coastguard Worker indicate a valid recovery record. In total 4 fsync/msync calls are
1289*6a54128fSAndroid Build Coastguard Worker needed per commit to prevent race conditions. It might be possible
1290*6a54128fSAndroid Build Coastguard Worker to reduce this to 3 or even 2 with some more work.
1291*6a54128fSAndroid Build Coastguard Worker
1292*6a54128fSAndroid Build Coastguard Worker - check for a valid recovery record on open of the tdb, while the
1293*6a54128fSAndroid Build Coastguard Worker global lock is held. Automatically recover from the transaction
1294*6a54128fSAndroid Build Coastguard Worker recovery area if needed, then continue with the open as
1295*6a54128fSAndroid Build Coastguard Worker usual. This allows for smooth crash recovery with no administrator
1296*6a54128fSAndroid Build Coastguard Worker intervention.
1297*6a54128fSAndroid Build Coastguard Worker
1298*6a54128fSAndroid Build Coastguard Worker - if TDB_NOSYNC is passed to flags in tdb_open then transactions are
1299*6a54128fSAndroid Build Coastguard Worker still available, but no transaction recovery area is used and no
1300*6a54128fSAndroid Build Coastguard Worker fsync/msync calls are made.
1301*6a54128fSAndroid Build Coastguard Worker
1302*6a54128fSAndroid Build Coastguard Worker */
1303*6a54128fSAndroid Build Coastguard Worker
1304*6a54128fSAndroid Build Coastguard Worker struct tdb_transaction_el {
1305*6a54128fSAndroid Build Coastguard Worker struct tdb_transaction_el *next, *prev;
1306*6a54128fSAndroid Build Coastguard Worker tdb_off_t offset;
1307*6a54128fSAndroid Build Coastguard Worker tdb_len_t length;
1308*6a54128fSAndroid Build Coastguard Worker unsigned char *data;
1309*6a54128fSAndroid Build Coastguard Worker };
1310*6a54128fSAndroid Build Coastguard Worker
1311*6a54128fSAndroid Build Coastguard Worker /*
1312*6a54128fSAndroid Build Coastguard Worker hold the context of any current transaction
1313*6a54128fSAndroid Build Coastguard Worker */
1314*6a54128fSAndroid Build Coastguard Worker struct tdb_transaction {
1315*6a54128fSAndroid Build Coastguard Worker /* we keep a mirrored copy of the tdb hash heads here so
1316*6a54128fSAndroid Build Coastguard Worker tdb_next_hash_chain() can operate efficiently */
1317*6a54128fSAndroid Build Coastguard Worker u32 *hash_heads;
1318*6a54128fSAndroid Build Coastguard Worker
1319*6a54128fSAndroid Build Coastguard Worker /* the original io methods - used to do IOs to the real db */
1320*6a54128fSAndroid Build Coastguard Worker const struct tdb_methods *io_methods;
1321*6a54128fSAndroid Build Coastguard Worker
1322*6a54128fSAndroid Build Coastguard Worker /* the list of transaction elements. We use a doubly linked
1323*6a54128fSAndroid Build Coastguard Worker list with a last pointer to allow us to keep the list
1324*6a54128fSAndroid Build Coastguard Worker ordered, with first element at the front of the list. It
1325*6a54128fSAndroid Build Coastguard Worker needs to be doubly linked as the read/write traversals need
1326*6a54128fSAndroid Build Coastguard Worker to be backwards, while the commit needs to be forwards */
1327*6a54128fSAndroid Build Coastguard Worker struct tdb_transaction_el *elements, *elements_last;
1328*6a54128fSAndroid Build Coastguard Worker
1329*6a54128fSAndroid Build Coastguard Worker /* non-zero when an internal transaction error has
1330*6a54128fSAndroid Build Coastguard Worker occurred. All write operations will then fail until the
1331*6a54128fSAndroid Build Coastguard Worker transaction is ended */
1332*6a54128fSAndroid Build Coastguard Worker int transaction_error;
1333*6a54128fSAndroid Build Coastguard Worker
1334*6a54128fSAndroid Build Coastguard Worker /* when inside a transaction we need to keep track of any
1335*6a54128fSAndroid Build Coastguard Worker nested tdb_transaction_start() calls, as these are allowed,
1336*6a54128fSAndroid Build Coastguard Worker but don't create a new transaction */
1337*6a54128fSAndroid Build Coastguard Worker int nesting;
1338*6a54128fSAndroid Build Coastguard Worker
1339*6a54128fSAndroid Build Coastguard Worker /* old file size before transaction */
1340*6a54128fSAndroid Build Coastguard Worker tdb_len_t old_map_size;
1341*6a54128fSAndroid Build Coastguard Worker };
1342*6a54128fSAndroid Build Coastguard Worker
1343*6a54128fSAndroid Build Coastguard Worker
1344*6a54128fSAndroid Build Coastguard Worker /*
1345*6a54128fSAndroid Build Coastguard Worker read while in a transaction. We need to check first if the data is in our list
1346*6a54128fSAndroid Build Coastguard Worker of transaction elements, then if not do a real read
1347*6a54128fSAndroid Build Coastguard Worker */
transaction_read(struct tdb_context * tdb,tdb_off_t off,void * buf,tdb_len_t len,int cv)1348*6a54128fSAndroid Build Coastguard Worker static int transaction_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
1349*6a54128fSAndroid Build Coastguard Worker tdb_len_t len, int cv)
1350*6a54128fSAndroid Build Coastguard Worker {
1351*6a54128fSAndroid Build Coastguard Worker struct tdb_transaction_el *el;
1352*6a54128fSAndroid Build Coastguard Worker
1353*6a54128fSAndroid Build Coastguard Worker /* we need to walk the list backwards to get the most recent data */
1354*6a54128fSAndroid Build Coastguard Worker for (el=tdb->transaction->elements_last;el;el=el->prev) {
1355*6a54128fSAndroid Build Coastguard Worker tdb_len_t partial;
1356*6a54128fSAndroid Build Coastguard Worker
1357*6a54128fSAndroid Build Coastguard Worker if (off+len <= el->offset) {
1358*6a54128fSAndroid Build Coastguard Worker continue;
1359*6a54128fSAndroid Build Coastguard Worker }
1360*6a54128fSAndroid Build Coastguard Worker if (off >= el->offset + el->length) {
1361*6a54128fSAndroid Build Coastguard Worker continue;
1362*6a54128fSAndroid Build Coastguard Worker }
1363*6a54128fSAndroid Build Coastguard Worker
1364*6a54128fSAndroid Build Coastguard Worker /* an overlapping read - needs to be split into up to
1365*6a54128fSAndroid Build Coastguard Worker 2 reads and a memcpy */
1366*6a54128fSAndroid Build Coastguard Worker if (off < el->offset) {
1367*6a54128fSAndroid Build Coastguard Worker partial = el->offset - off;
1368*6a54128fSAndroid Build Coastguard Worker if (transaction_read(tdb, off, buf, partial, cv) != 0) {
1369*6a54128fSAndroid Build Coastguard Worker goto fail;
1370*6a54128fSAndroid Build Coastguard Worker }
1371*6a54128fSAndroid Build Coastguard Worker len -= partial;
1372*6a54128fSAndroid Build Coastguard Worker off += partial;
1373*6a54128fSAndroid Build Coastguard Worker buf = (void *)(partial + (char *)buf);
1374*6a54128fSAndroid Build Coastguard Worker }
1375*6a54128fSAndroid Build Coastguard Worker if (off + len <= el->offset + el->length) {
1376*6a54128fSAndroid Build Coastguard Worker partial = len;
1377*6a54128fSAndroid Build Coastguard Worker } else {
1378*6a54128fSAndroid Build Coastguard Worker partial = el->offset + el->length - off;
1379*6a54128fSAndroid Build Coastguard Worker }
1380*6a54128fSAndroid Build Coastguard Worker memcpy(buf, el->data + (off - el->offset), partial);
1381*6a54128fSAndroid Build Coastguard Worker if (cv) {
1382*6a54128fSAndroid Build Coastguard Worker tdb_convert(buf, len);
1383*6a54128fSAndroid Build Coastguard Worker }
1384*6a54128fSAndroid Build Coastguard Worker len -= partial;
1385*6a54128fSAndroid Build Coastguard Worker off += partial;
1386*6a54128fSAndroid Build Coastguard Worker buf = (void *)(partial + (char *)buf);
1387*6a54128fSAndroid Build Coastguard Worker
1388*6a54128fSAndroid Build Coastguard Worker if (len != 0 && transaction_read(tdb, off, buf, len, cv) != 0) {
1389*6a54128fSAndroid Build Coastguard Worker goto fail;
1390*6a54128fSAndroid Build Coastguard Worker }
1391*6a54128fSAndroid Build Coastguard Worker
1392*6a54128fSAndroid Build Coastguard Worker return 0;
1393*6a54128fSAndroid Build Coastguard Worker }
1394*6a54128fSAndroid Build Coastguard Worker
1395*6a54128fSAndroid Build Coastguard Worker /* its not in the transaction elements - do a real read */
1396*6a54128fSAndroid Build Coastguard Worker return tdb->transaction->io_methods->tdb_read(tdb, off, buf, len, cv);
1397*6a54128fSAndroid Build Coastguard Worker
1398*6a54128fSAndroid Build Coastguard Worker fail:
1399*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "transaction_read: failed at off=%d len=%d\n", off, len));
1400*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
1401*6a54128fSAndroid Build Coastguard Worker tdb->transaction->transaction_error = 1;
1402*6a54128fSAndroid Build Coastguard Worker return -1;
1403*6a54128fSAndroid Build Coastguard Worker }
1404*6a54128fSAndroid Build Coastguard Worker
1405*6a54128fSAndroid Build Coastguard Worker
1406*6a54128fSAndroid Build Coastguard Worker /*
1407*6a54128fSAndroid Build Coastguard Worker write while in a transaction
1408*6a54128fSAndroid Build Coastguard Worker */
transaction_write(struct tdb_context * tdb,tdb_off_t off,const void * buf,tdb_len_t len)1409*6a54128fSAndroid Build Coastguard Worker static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
1410*6a54128fSAndroid Build Coastguard Worker const void *buf, tdb_len_t len)
1411*6a54128fSAndroid Build Coastguard Worker {
1412*6a54128fSAndroid Build Coastguard Worker struct tdb_transaction_el *el, *best_el=NULL;
1413*6a54128fSAndroid Build Coastguard Worker
1414*6a54128fSAndroid Build Coastguard Worker if (len == 0) {
1415*6a54128fSAndroid Build Coastguard Worker return 0;
1416*6a54128fSAndroid Build Coastguard Worker }
1417*6a54128fSAndroid Build Coastguard Worker
1418*6a54128fSAndroid Build Coastguard Worker /* if the write is to a hash head, then update the transaction
1419*6a54128fSAndroid Build Coastguard Worker hash heads */
1420*6a54128fSAndroid Build Coastguard Worker if (len == sizeof(tdb_off_t) && off >= FREELIST_TOP &&
1421*6a54128fSAndroid Build Coastguard Worker off < FREELIST_TOP+TDB_HASHTABLE_SIZE(tdb)) {
1422*6a54128fSAndroid Build Coastguard Worker u32 chain = (off-FREELIST_TOP) / sizeof(tdb_off_t);
1423*6a54128fSAndroid Build Coastguard Worker memcpy(&tdb->transaction->hash_heads[chain], buf, len);
1424*6a54128fSAndroid Build Coastguard Worker }
1425*6a54128fSAndroid Build Coastguard Worker
1426*6a54128fSAndroid Build Coastguard Worker /* first see if we can replace an existing entry */
1427*6a54128fSAndroid Build Coastguard Worker for (el=tdb->transaction->elements_last;el;el=el->prev) {
1428*6a54128fSAndroid Build Coastguard Worker tdb_len_t partial;
1429*6a54128fSAndroid Build Coastguard Worker
1430*6a54128fSAndroid Build Coastguard Worker if (best_el == NULL && off == el->offset+el->length) {
1431*6a54128fSAndroid Build Coastguard Worker best_el = el;
1432*6a54128fSAndroid Build Coastguard Worker }
1433*6a54128fSAndroid Build Coastguard Worker
1434*6a54128fSAndroid Build Coastguard Worker if (off+len <= el->offset) {
1435*6a54128fSAndroid Build Coastguard Worker continue;
1436*6a54128fSAndroid Build Coastguard Worker }
1437*6a54128fSAndroid Build Coastguard Worker if (off >= el->offset + el->length) {
1438*6a54128fSAndroid Build Coastguard Worker continue;
1439*6a54128fSAndroid Build Coastguard Worker }
1440*6a54128fSAndroid Build Coastguard Worker
1441*6a54128fSAndroid Build Coastguard Worker /* an overlapping write - needs to be split into up to
1442*6a54128fSAndroid Build Coastguard Worker 2 writes and a memcpy */
1443*6a54128fSAndroid Build Coastguard Worker if (off < el->offset) {
1444*6a54128fSAndroid Build Coastguard Worker partial = el->offset - off;
1445*6a54128fSAndroid Build Coastguard Worker if (transaction_write(tdb, off, buf, partial) != 0) {
1446*6a54128fSAndroid Build Coastguard Worker goto fail;
1447*6a54128fSAndroid Build Coastguard Worker }
1448*6a54128fSAndroid Build Coastguard Worker len -= partial;
1449*6a54128fSAndroid Build Coastguard Worker off += partial;
1450*6a54128fSAndroid Build Coastguard Worker buf = (const void *)(partial + (const char *)buf);
1451*6a54128fSAndroid Build Coastguard Worker }
1452*6a54128fSAndroid Build Coastguard Worker if (off + len <= el->offset + el->length) {
1453*6a54128fSAndroid Build Coastguard Worker partial = len;
1454*6a54128fSAndroid Build Coastguard Worker } else {
1455*6a54128fSAndroid Build Coastguard Worker partial = el->offset + el->length - off;
1456*6a54128fSAndroid Build Coastguard Worker }
1457*6a54128fSAndroid Build Coastguard Worker memcpy(el->data + (off - el->offset), buf, partial);
1458*6a54128fSAndroid Build Coastguard Worker len -= partial;
1459*6a54128fSAndroid Build Coastguard Worker off += partial;
1460*6a54128fSAndroid Build Coastguard Worker buf = (const void *)(partial + (const char *)buf);
1461*6a54128fSAndroid Build Coastguard Worker
1462*6a54128fSAndroid Build Coastguard Worker if (len != 0 && transaction_write(tdb, off, buf, len) != 0) {
1463*6a54128fSAndroid Build Coastguard Worker goto fail;
1464*6a54128fSAndroid Build Coastguard Worker }
1465*6a54128fSAndroid Build Coastguard Worker
1466*6a54128fSAndroid Build Coastguard Worker return 0;
1467*6a54128fSAndroid Build Coastguard Worker }
1468*6a54128fSAndroid Build Coastguard Worker
1469*6a54128fSAndroid Build Coastguard Worker /* see if we can append the new entry to an existing entry */
1470*6a54128fSAndroid Build Coastguard Worker if (best_el && best_el->offset + best_el->length == off &&
1471*6a54128fSAndroid Build Coastguard Worker (off+len < tdb->transaction->old_map_size ||
1472*6a54128fSAndroid Build Coastguard Worker off > tdb->transaction->old_map_size)) {
1473*6a54128fSAndroid Build Coastguard Worker unsigned char *data = best_el->data;
1474*6a54128fSAndroid Build Coastguard Worker el = best_el;
1475*6a54128fSAndroid Build Coastguard Worker el->data = (unsigned char *)realloc(el->data,
1476*6a54128fSAndroid Build Coastguard Worker el->length + len);
1477*6a54128fSAndroid Build Coastguard Worker if (el->data == NULL) {
1478*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_OOM;
1479*6a54128fSAndroid Build Coastguard Worker tdb->transaction->transaction_error = 1;
1480*6a54128fSAndroid Build Coastguard Worker el->data = data;
1481*6a54128fSAndroid Build Coastguard Worker return -1;
1482*6a54128fSAndroid Build Coastguard Worker }
1483*6a54128fSAndroid Build Coastguard Worker if (buf) {
1484*6a54128fSAndroid Build Coastguard Worker memcpy(el->data + el->length, buf, len);
1485*6a54128fSAndroid Build Coastguard Worker } else {
1486*6a54128fSAndroid Build Coastguard Worker memset(el->data + el->length, TDB_PAD_BYTE, len);
1487*6a54128fSAndroid Build Coastguard Worker }
1488*6a54128fSAndroid Build Coastguard Worker el->length += len;
1489*6a54128fSAndroid Build Coastguard Worker return 0;
1490*6a54128fSAndroid Build Coastguard Worker }
1491*6a54128fSAndroid Build Coastguard Worker
1492*6a54128fSAndroid Build Coastguard Worker /* add a new entry at the end of the list */
1493*6a54128fSAndroid Build Coastguard Worker el = (struct tdb_transaction_el *)malloc(sizeof(*el));
1494*6a54128fSAndroid Build Coastguard Worker if (el == NULL) {
1495*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_OOM;
1496*6a54128fSAndroid Build Coastguard Worker tdb->transaction->transaction_error = 1;
1497*6a54128fSAndroid Build Coastguard Worker return -1;
1498*6a54128fSAndroid Build Coastguard Worker }
1499*6a54128fSAndroid Build Coastguard Worker el->next = NULL;
1500*6a54128fSAndroid Build Coastguard Worker el->prev = tdb->transaction->elements_last;
1501*6a54128fSAndroid Build Coastguard Worker el->offset = off;
1502*6a54128fSAndroid Build Coastguard Worker el->length = len;
1503*6a54128fSAndroid Build Coastguard Worker el->data = (unsigned char *)malloc(len);
1504*6a54128fSAndroid Build Coastguard Worker if (el->data == NULL) {
1505*6a54128fSAndroid Build Coastguard Worker free(el);
1506*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_OOM;
1507*6a54128fSAndroid Build Coastguard Worker tdb->transaction->transaction_error = 1;
1508*6a54128fSAndroid Build Coastguard Worker return -1;
1509*6a54128fSAndroid Build Coastguard Worker }
1510*6a54128fSAndroid Build Coastguard Worker if (buf) {
1511*6a54128fSAndroid Build Coastguard Worker memcpy(el->data, buf, len);
1512*6a54128fSAndroid Build Coastguard Worker } else {
1513*6a54128fSAndroid Build Coastguard Worker memset(el->data, TDB_PAD_BYTE, len);
1514*6a54128fSAndroid Build Coastguard Worker }
1515*6a54128fSAndroid Build Coastguard Worker if (el->prev) {
1516*6a54128fSAndroid Build Coastguard Worker el->prev->next = el;
1517*6a54128fSAndroid Build Coastguard Worker } else {
1518*6a54128fSAndroid Build Coastguard Worker tdb->transaction->elements = el;
1519*6a54128fSAndroid Build Coastguard Worker }
1520*6a54128fSAndroid Build Coastguard Worker tdb->transaction->elements_last = el;
1521*6a54128fSAndroid Build Coastguard Worker return 0;
1522*6a54128fSAndroid Build Coastguard Worker
1523*6a54128fSAndroid Build Coastguard Worker fail:
1524*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "transaction_write: failed at off=%d len=%d\n", off, len));
1525*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
1526*6a54128fSAndroid Build Coastguard Worker tdb->transaction->transaction_error = 1;
1527*6a54128fSAndroid Build Coastguard Worker return -1;
1528*6a54128fSAndroid Build Coastguard Worker }
1529*6a54128fSAndroid Build Coastguard Worker
1530*6a54128fSAndroid Build Coastguard Worker /*
1531*6a54128fSAndroid Build Coastguard Worker accelerated hash chain head search, using the cached hash heads
1532*6a54128fSAndroid Build Coastguard Worker */
transaction_next_hash_chain(struct tdb_context * tdb,u32 * chain)1533*6a54128fSAndroid Build Coastguard Worker static void transaction_next_hash_chain(struct tdb_context *tdb, u32 *chain)
1534*6a54128fSAndroid Build Coastguard Worker {
1535*6a54128fSAndroid Build Coastguard Worker u32 h = *chain;
1536*6a54128fSAndroid Build Coastguard Worker for (;h < tdb->header.hash_size;h++) {
1537*6a54128fSAndroid Build Coastguard Worker /* the +1 takes account of the freelist */
1538*6a54128fSAndroid Build Coastguard Worker if (0 != tdb->transaction->hash_heads[h+1]) {
1539*6a54128fSAndroid Build Coastguard Worker break;
1540*6a54128fSAndroid Build Coastguard Worker }
1541*6a54128fSAndroid Build Coastguard Worker }
1542*6a54128fSAndroid Build Coastguard Worker (*chain) = h;
1543*6a54128fSAndroid Build Coastguard Worker }
1544*6a54128fSAndroid Build Coastguard Worker
1545*6a54128fSAndroid Build Coastguard Worker /*
1546*6a54128fSAndroid Build Coastguard Worker out of bounds check during a transaction
1547*6a54128fSAndroid Build Coastguard Worker */
transaction_oob(struct tdb_context * tdb,tdb_off_t len,int probe EXT2FS_ATTR ((unused)))1548*6a54128fSAndroid Build Coastguard Worker static int transaction_oob(struct tdb_context *tdb, tdb_off_t len,
1549*6a54128fSAndroid Build Coastguard Worker int probe EXT2FS_ATTR((unused)))
1550*6a54128fSAndroid Build Coastguard Worker {
1551*6a54128fSAndroid Build Coastguard Worker if (len <= tdb->map_size) {
1552*6a54128fSAndroid Build Coastguard Worker return 0;
1553*6a54128fSAndroid Build Coastguard Worker }
1554*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_IO, -1);
1555*6a54128fSAndroid Build Coastguard Worker }
1556*6a54128fSAndroid Build Coastguard Worker
1557*6a54128fSAndroid Build Coastguard Worker /*
1558*6a54128fSAndroid Build Coastguard Worker transaction version of tdb_expand().
1559*6a54128fSAndroid Build Coastguard Worker */
transaction_expand_file(struct tdb_context * tdb,tdb_off_t size,tdb_off_t addition)1560*6a54128fSAndroid Build Coastguard Worker static int transaction_expand_file(struct tdb_context *tdb, tdb_off_t size,
1561*6a54128fSAndroid Build Coastguard Worker tdb_off_t addition)
1562*6a54128fSAndroid Build Coastguard Worker {
1563*6a54128fSAndroid Build Coastguard Worker /* add a write to the transaction elements, so subsequent
1564*6a54128fSAndroid Build Coastguard Worker reads see the zero data */
1565*6a54128fSAndroid Build Coastguard Worker if (transaction_write(tdb, size, NULL, addition) != 0) {
1566*6a54128fSAndroid Build Coastguard Worker return -1;
1567*6a54128fSAndroid Build Coastguard Worker }
1568*6a54128fSAndroid Build Coastguard Worker
1569*6a54128fSAndroid Build Coastguard Worker return 0;
1570*6a54128fSAndroid Build Coastguard Worker }
1571*6a54128fSAndroid Build Coastguard Worker
1572*6a54128fSAndroid Build Coastguard Worker /*
1573*6a54128fSAndroid Build Coastguard Worker brlock during a transaction - ignore them
1574*6a54128fSAndroid Build Coastguard Worker */
transaction_brlock(struct tdb_context * tdb EXT2FS_ATTR ((unused)),tdb_off_t offset EXT2FS_ATTR ((unused)),int rw_type EXT2FS_ATTR ((unused)),int lck_type EXT2FS_ATTR ((unused)),int probe EXT2FS_ATTR ((unused)),size_t len EXT2FS_ATTR ((unused)))1575*6a54128fSAndroid Build Coastguard Worker static int transaction_brlock(struct tdb_context *tdb EXT2FS_ATTR((unused)),
1576*6a54128fSAndroid Build Coastguard Worker tdb_off_t offset EXT2FS_ATTR((unused)),
1577*6a54128fSAndroid Build Coastguard Worker int rw_type EXT2FS_ATTR((unused)),
1578*6a54128fSAndroid Build Coastguard Worker int lck_type EXT2FS_ATTR((unused)),
1579*6a54128fSAndroid Build Coastguard Worker int probe EXT2FS_ATTR((unused)),
1580*6a54128fSAndroid Build Coastguard Worker size_t len EXT2FS_ATTR((unused)))
1581*6a54128fSAndroid Build Coastguard Worker {
1582*6a54128fSAndroid Build Coastguard Worker return 0;
1583*6a54128fSAndroid Build Coastguard Worker }
1584*6a54128fSAndroid Build Coastguard Worker
1585*6a54128fSAndroid Build Coastguard Worker static const struct tdb_methods transaction_methods = {
1586*6a54128fSAndroid Build Coastguard Worker transaction_read,
1587*6a54128fSAndroid Build Coastguard Worker transaction_write,
1588*6a54128fSAndroid Build Coastguard Worker transaction_next_hash_chain,
1589*6a54128fSAndroid Build Coastguard Worker transaction_oob,
1590*6a54128fSAndroid Build Coastguard Worker transaction_expand_file,
1591*6a54128fSAndroid Build Coastguard Worker transaction_brlock
1592*6a54128fSAndroid Build Coastguard Worker };
1593*6a54128fSAndroid Build Coastguard Worker
1594*6a54128fSAndroid Build Coastguard Worker
1595*6a54128fSAndroid Build Coastguard Worker /*
1596*6a54128fSAndroid Build Coastguard Worker start a tdb transaction. No token is returned, as only a single
1597*6a54128fSAndroid Build Coastguard Worker transaction is allowed to be pending per tdb_context
1598*6a54128fSAndroid Build Coastguard Worker */
tdb_transaction_start(struct tdb_context * tdb)1599*6a54128fSAndroid Build Coastguard Worker int tdb_transaction_start(struct tdb_context *tdb)
1600*6a54128fSAndroid Build Coastguard Worker {
1601*6a54128fSAndroid Build Coastguard Worker /* some sanity checks */
1602*6a54128fSAndroid Build Coastguard Worker if (tdb->read_only || (tdb->flags & TDB_INTERNAL) || tdb->traverse_read) {
1603*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: cannot start a transaction on a read-only or internal db\n"));
1604*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_EINVAL;
1605*6a54128fSAndroid Build Coastguard Worker return -1;
1606*6a54128fSAndroid Build Coastguard Worker }
1607*6a54128fSAndroid Build Coastguard Worker
1608*6a54128fSAndroid Build Coastguard Worker /* cope with nested tdb_transaction_start() calls */
1609*6a54128fSAndroid Build Coastguard Worker if (tdb->transaction != NULL) {
1610*6a54128fSAndroid Build Coastguard Worker tdb->transaction->nesting++;
1611*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_TRACE, "tdb_transaction_start: nesting %d\n",
1612*6a54128fSAndroid Build Coastguard Worker tdb->transaction->nesting));
1613*6a54128fSAndroid Build Coastguard Worker return 0;
1614*6a54128fSAndroid Build Coastguard Worker }
1615*6a54128fSAndroid Build Coastguard Worker
1616*6a54128fSAndroid Build Coastguard Worker if (tdb->num_locks != 0 || tdb->global_lock.count) {
1617*6a54128fSAndroid Build Coastguard Worker /* the caller must not have any locks when starting a
1618*6a54128fSAndroid Build Coastguard Worker transaction as otherwise we'll be screwed by lack
1619*6a54128fSAndroid Build Coastguard Worker of nested locks in posix */
1620*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: cannot start a transaction with locks held\n"));
1621*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_LOCK;
1622*6a54128fSAndroid Build Coastguard Worker return -1;
1623*6a54128fSAndroid Build Coastguard Worker }
1624*6a54128fSAndroid Build Coastguard Worker
1625*6a54128fSAndroid Build Coastguard Worker if (tdb->travlocks.next != NULL) {
1626*6a54128fSAndroid Build Coastguard Worker /* you cannot use transactions inside a traverse (although you can use
1627*6a54128fSAndroid Build Coastguard Worker traverse inside a transaction) as otherwise you can end up with
1628*6a54128fSAndroid Build Coastguard Worker deadlock */
1629*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: cannot start a transaction within a traverse\n"));
1630*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_LOCK;
1631*6a54128fSAndroid Build Coastguard Worker return -1;
1632*6a54128fSAndroid Build Coastguard Worker }
1633*6a54128fSAndroid Build Coastguard Worker
1634*6a54128fSAndroid Build Coastguard Worker tdb->transaction = (struct tdb_transaction *)
1635*6a54128fSAndroid Build Coastguard Worker calloc(sizeof(struct tdb_transaction), 1);
1636*6a54128fSAndroid Build Coastguard Worker if (tdb->transaction == NULL) {
1637*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_OOM;
1638*6a54128fSAndroid Build Coastguard Worker return -1;
1639*6a54128fSAndroid Build Coastguard Worker }
1640*6a54128fSAndroid Build Coastguard Worker
1641*6a54128fSAndroid Build Coastguard Worker /* get the transaction write lock. This is a blocking lock. As
1642*6a54128fSAndroid Build Coastguard Worker discussed with Volker, there are a number of ways we could
1643*6a54128fSAndroid Build Coastguard Worker make this async, which we will probably do in the future */
1644*6a54128fSAndroid Build Coastguard Worker if (tdb_transaction_lock(tdb, F_WRLCK) == -1) {
1645*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb->transaction);
1646*6a54128fSAndroid Build Coastguard Worker return -1;
1647*6a54128fSAndroid Build Coastguard Worker }
1648*6a54128fSAndroid Build Coastguard Worker
1649*6a54128fSAndroid Build Coastguard Worker /* get a read lock from the freelist to the end of file. This
1650*6a54128fSAndroid Build Coastguard Worker is upgraded to a write lock during the commit */
1651*6a54128fSAndroid Build Coastguard Worker if (tdb_brlock(tdb, FREELIST_TOP, F_RDLCK, F_SETLKW, 0, 0) == -1) {
1652*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: failed to get hash locks\n"));
1653*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_LOCK;
1654*6a54128fSAndroid Build Coastguard Worker goto fail;
1655*6a54128fSAndroid Build Coastguard Worker }
1656*6a54128fSAndroid Build Coastguard Worker
1657*6a54128fSAndroid Build Coastguard Worker /* setup a copy of the hash table heads so the hash scan in
1658*6a54128fSAndroid Build Coastguard Worker traverse can be fast */
1659*6a54128fSAndroid Build Coastguard Worker tdb->transaction->hash_heads = (u32 *)
1660*6a54128fSAndroid Build Coastguard Worker calloc(tdb->header.hash_size+1, sizeof(u32));
1661*6a54128fSAndroid Build Coastguard Worker if (tdb->transaction->hash_heads == NULL) {
1662*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_OOM;
1663*6a54128fSAndroid Build Coastguard Worker goto fail;
1664*6a54128fSAndroid Build Coastguard Worker }
1665*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_read(tdb, FREELIST_TOP, tdb->transaction->hash_heads,
1666*6a54128fSAndroid Build Coastguard Worker TDB_HASHTABLE_SIZE(tdb), 0) != 0) {
1667*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_start: failed to read hash heads\n"));
1668*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
1669*6a54128fSAndroid Build Coastguard Worker goto fail;
1670*6a54128fSAndroid Build Coastguard Worker }
1671*6a54128fSAndroid Build Coastguard Worker
1672*6a54128fSAndroid Build Coastguard Worker /* make sure we know about any file expansions already done by
1673*6a54128fSAndroid Build Coastguard Worker anyone else */
1674*6a54128fSAndroid Build Coastguard Worker tdb->methods->tdb_oob(tdb, tdb->map_size + 1, 1);
1675*6a54128fSAndroid Build Coastguard Worker tdb->transaction->old_map_size = tdb->map_size;
1676*6a54128fSAndroid Build Coastguard Worker
1677*6a54128fSAndroid Build Coastguard Worker /* finally hook the io methods, replacing them with
1678*6a54128fSAndroid Build Coastguard Worker transaction specific methods */
1679*6a54128fSAndroid Build Coastguard Worker tdb->transaction->io_methods = tdb->methods;
1680*6a54128fSAndroid Build Coastguard Worker tdb->methods = &transaction_methods;
1681*6a54128fSAndroid Build Coastguard Worker
1682*6a54128fSAndroid Build Coastguard Worker /* by calling this transaction write here, we ensure that we don't grow the
1683*6a54128fSAndroid Build Coastguard Worker transaction linked list due to hash table updates */
1684*6a54128fSAndroid Build Coastguard Worker if (transaction_write(tdb, FREELIST_TOP, tdb->transaction->hash_heads,
1685*6a54128fSAndroid Build Coastguard Worker TDB_HASHTABLE_SIZE(tdb)) != 0) {
1686*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_start: failed to prime hash table\n"));
1687*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
1688*6a54128fSAndroid Build Coastguard Worker tdb->methods = tdb->transaction->io_methods;
1689*6a54128fSAndroid Build Coastguard Worker goto fail;
1690*6a54128fSAndroid Build Coastguard Worker }
1691*6a54128fSAndroid Build Coastguard Worker
1692*6a54128fSAndroid Build Coastguard Worker return 0;
1693*6a54128fSAndroid Build Coastguard Worker
1694*6a54128fSAndroid Build Coastguard Worker fail:
1695*6a54128fSAndroid Build Coastguard Worker tdb_brlock(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW, 0, 0);
1696*6a54128fSAndroid Build Coastguard Worker tdb_transaction_unlock(tdb);
1697*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb->transaction->hash_heads);
1698*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb->transaction);
1699*6a54128fSAndroid Build Coastguard Worker return -1;
1700*6a54128fSAndroid Build Coastguard Worker }
1701*6a54128fSAndroid Build Coastguard Worker
1702*6a54128fSAndroid Build Coastguard Worker
1703*6a54128fSAndroid Build Coastguard Worker /*
1704*6a54128fSAndroid Build Coastguard Worker cancel the current transaction
1705*6a54128fSAndroid Build Coastguard Worker */
tdb_transaction_cancel(struct tdb_context * tdb)1706*6a54128fSAndroid Build Coastguard Worker int tdb_transaction_cancel(struct tdb_context *tdb)
1707*6a54128fSAndroid Build Coastguard Worker {
1708*6a54128fSAndroid Build Coastguard Worker if (tdb->transaction == NULL) {
1709*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_cancel: no transaction\n"));
1710*6a54128fSAndroid Build Coastguard Worker return -1;
1711*6a54128fSAndroid Build Coastguard Worker }
1712*6a54128fSAndroid Build Coastguard Worker
1713*6a54128fSAndroid Build Coastguard Worker if (tdb->transaction->nesting != 0) {
1714*6a54128fSAndroid Build Coastguard Worker tdb->transaction->transaction_error = 1;
1715*6a54128fSAndroid Build Coastguard Worker tdb->transaction->nesting--;
1716*6a54128fSAndroid Build Coastguard Worker return 0;
1717*6a54128fSAndroid Build Coastguard Worker }
1718*6a54128fSAndroid Build Coastguard Worker
1719*6a54128fSAndroid Build Coastguard Worker tdb->map_size = tdb->transaction->old_map_size;
1720*6a54128fSAndroid Build Coastguard Worker
1721*6a54128fSAndroid Build Coastguard Worker /* free all the transaction elements */
1722*6a54128fSAndroid Build Coastguard Worker while (tdb->transaction->elements) {
1723*6a54128fSAndroid Build Coastguard Worker struct tdb_transaction_el *el = tdb->transaction->elements;
1724*6a54128fSAndroid Build Coastguard Worker tdb->transaction->elements = el->next;
1725*6a54128fSAndroid Build Coastguard Worker free(el->data);
1726*6a54128fSAndroid Build Coastguard Worker free(el);
1727*6a54128fSAndroid Build Coastguard Worker }
1728*6a54128fSAndroid Build Coastguard Worker
1729*6a54128fSAndroid Build Coastguard Worker /* remove any global lock created during the transaction */
1730*6a54128fSAndroid Build Coastguard Worker if (tdb->global_lock.count != 0) {
1731*6a54128fSAndroid Build Coastguard Worker tdb_brlock(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW, 0, 4*tdb->header.hash_size);
1732*6a54128fSAndroid Build Coastguard Worker tdb->global_lock.count = 0;
1733*6a54128fSAndroid Build Coastguard Worker }
1734*6a54128fSAndroid Build Coastguard Worker
1735*6a54128fSAndroid Build Coastguard Worker /* remove any locks created during the transaction */
1736*6a54128fSAndroid Build Coastguard Worker if (tdb->num_locks != 0) {
1737*6a54128fSAndroid Build Coastguard Worker int i;
1738*6a54128fSAndroid Build Coastguard Worker for (i=0;i<tdb->num_lockrecs;i++) {
1739*6a54128fSAndroid Build Coastguard Worker tdb_brlock(tdb,FREELIST_TOP+4*tdb->lockrecs[i].list,
1740*6a54128fSAndroid Build Coastguard Worker F_UNLCK,F_SETLKW, 0, 1);
1741*6a54128fSAndroid Build Coastguard Worker }
1742*6a54128fSAndroid Build Coastguard Worker tdb->num_locks = 0;
1743*6a54128fSAndroid Build Coastguard Worker tdb->num_lockrecs = 0;
1744*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb->lockrecs);
1745*6a54128fSAndroid Build Coastguard Worker }
1746*6a54128fSAndroid Build Coastguard Worker
1747*6a54128fSAndroid Build Coastguard Worker /* restore the normal io methods */
1748*6a54128fSAndroid Build Coastguard Worker tdb->methods = tdb->transaction->io_methods;
1749*6a54128fSAndroid Build Coastguard Worker
1750*6a54128fSAndroid Build Coastguard Worker tdb_brlock(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW, 0, 0);
1751*6a54128fSAndroid Build Coastguard Worker tdb_transaction_unlock(tdb);
1752*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb->transaction->hash_heads);
1753*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb->transaction);
1754*6a54128fSAndroid Build Coastguard Worker
1755*6a54128fSAndroid Build Coastguard Worker return 0;
1756*6a54128fSAndroid Build Coastguard Worker }
1757*6a54128fSAndroid Build Coastguard Worker
1758*6a54128fSAndroid Build Coastguard Worker /*
1759*6a54128fSAndroid Build Coastguard Worker sync to disk
1760*6a54128fSAndroid Build Coastguard Worker */
transaction_sync(struct tdb_context * tdb,tdb_off_t offset,tdb_len_t length)1761*6a54128fSAndroid Build Coastguard Worker static int transaction_sync(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t length)
1762*6a54128fSAndroid Build Coastguard Worker {
1763*6a54128fSAndroid Build Coastguard Worker if (fsync(tdb->fd) != 0) {
1764*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
1765*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction: fsync failed\n"));
1766*6a54128fSAndroid Build Coastguard Worker return -1;
1767*6a54128fSAndroid Build Coastguard Worker }
1768*6a54128fSAndroid Build Coastguard Worker #if defined(HAVE_MSYNC) && defined(MS_SYNC)
1769*6a54128fSAndroid Build Coastguard Worker if (tdb->map_ptr) {
1770*6a54128fSAndroid Build Coastguard Worker tdb_off_t moffset = offset & ~(tdb->page_size-1);
1771*6a54128fSAndroid Build Coastguard Worker if (msync(moffset + (char *)tdb->map_ptr,
1772*6a54128fSAndroid Build Coastguard Worker length + (offset - moffset), MS_SYNC) != 0) {
1773*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
1774*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction: msync failed - %s\n",
1775*6a54128fSAndroid Build Coastguard Worker strerror(errno)));
1776*6a54128fSAndroid Build Coastguard Worker return -1;
1777*6a54128fSAndroid Build Coastguard Worker }
1778*6a54128fSAndroid Build Coastguard Worker }
1779*6a54128fSAndroid Build Coastguard Worker #endif
1780*6a54128fSAndroid Build Coastguard Worker return 0;
1781*6a54128fSAndroid Build Coastguard Worker }
1782*6a54128fSAndroid Build Coastguard Worker
1783*6a54128fSAndroid Build Coastguard Worker
1784*6a54128fSAndroid Build Coastguard Worker /*
1785*6a54128fSAndroid Build Coastguard Worker work out how much space the linearised recovery data will consume
1786*6a54128fSAndroid Build Coastguard Worker */
tdb_recovery_size(struct tdb_context * tdb)1787*6a54128fSAndroid Build Coastguard Worker static tdb_len_t tdb_recovery_size(struct tdb_context *tdb)
1788*6a54128fSAndroid Build Coastguard Worker {
1789*6a54128fSAndroid Build Coastguard Worker struct tdb_transaction_el *el;
1790*6a54128fSAndroid Build Coastguard Worker tdb_len_t recovery_size = 0;
1791*6a54128fSAndroid Build Coastguard Worker
1792*6a54128fSAndroid Build Coastguard Worker recovery_size = sizeof(u32);
1793*6a54128fSAndroid Build Coastguard Worker for (el=tdb->transaction->elements;el;el=el->next) {
1794*6a54128fSAndroid Build Coastguard Worker if (el->offset >= tdb->transaction->old_map_size) {
1795*6a54128fSAndroid Build Coastguard Worker continue;
1796*6a54128fSAndroid Build Coastguard Worker }
1797*6a54128fSAndroid Build Coastguard Worker recovery_size += 2*sizeof(tdb_off_t) + el->length;
1798*6a54128fSAndroid Build Coastguard Worker }
1799*6a54128fSAndroid Build Coastguard Worker
1800*6a54128fSAndroid Build Coastguard Worker return recovery_size;
1801*6a54128fSAndroid Build Coastguard Worker }
1802*6a54128fSAndroid Build Coastguard Worker
1803*6a54128fSAndroid Build Coastguard Worker /*
1804*6a54128fSAndroid Build Coastguard Worker allocate the recovery area, or use an existing recovery area if it is
1805*6a54128fSAndroid Build Coastguard Worker large enough
1806*6a54128fSAndroid Build Coastguard Worker */
tdb_recovery_allocate(struct tdb_context * tdb,tdb_len_t * recovery_size,tdb_off_t * recovery_offset,tdb_len_t * recovery_max_size)1807*6a54128fSAndroid Build Coastguard Worker static int tdb_recovery_allocate(struct tdb_context *tdb,
1808*6a54128fSAndroid Build Coastguard Worker tdb_len_t *recovery_size,
1809*6a54128fSAndroid Build Coastguard Worker tdb_off_t *recovery_offset,
1810*6a54128fSAndroid Build Coastguard Worker tdb_len_t *recovery_max_size)
1811*6a54128fSAndroid Build Coastguard Worker {
1812*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
1813*6a54128fSAndroid Build Coastguard Worker const struct tdb_methods *methods = tdb->transaction->io_methods;
1814*6a54128fSAndroid Build Coastguard Worker tdb_off_t recovery_head;
1815*6a54128fSAndroid Build Coastguard Worker
1816*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
1817*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to read recovery head\n"));
1818*6a54128fSAndroid Build Coastguard Worker return -1;
1819*6a54128fSAndroid Build Coastguard Worker }
1820*6a54128fSAndroid Build Coastguard Worker
1821*6a54128fSAndroid Build Coastguard Worker rec.rec_len = 0;
1822*6a54128fSAndroid Build Coastguard Worker
1823*6a54128fSAndroid Build Coastguard Worker if (recovery_head != 0 &&
1824*6a54128fSAndroid Build Coastguard Worker methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) {
1825*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to read recovery record\n"));
1826*6a54128fSAndroid Build Coastguard Worker return -1;
1827*6a54128fSAndroid Build Coastguard Worker }
1828*6a54128fSAndroid Build Coastguard Worker
1829*6a54128fSAndroid Build Coastguard Worker *recovery_size = tdb_recovery_size(tdb);
1830*6a54128fSAndroid Build Coastguard Worker
1831*6a54128fSAndroid Build Coastguard Worker if (recovery_head != 0 && *recovery_size <= rec.rec_len) {
1832*6a54128fSAndroid Build Coastguard Worker /* it fits in the existing area */
1833*6a54128fSAndroid Build Coastguard Worker *recovery_max_size = rec.rec_len;
1834*6a54128fSAndroid Build Coastguard Worker *recovery_offset = recovery_head;
1835*6a54128fSAndroid Build Coastguard Worker return 0;
1836*6a54128fSAndroid Build Coastguard Worker }
1837*6a54128fSAndroid Build Coastguard Worker
1838*6a54128fSAndroid Build Coastguard Worker /* we need to free up the old recovery area, then allocate a
1839*6a54128fSAndroid Build Coastguard Worker new one at the end of the file. Note that we cannot use
1840*6a54128fSAndroid Build Coastguard Worker tdb_allocate() to allocate the new one as that might return
1841*6a54128fSAndroid Build Coastguard Worker us an area that is being currently used (as of the start of
1842*6a54128fSAndroid Build Coastguard Worker the transaction) */
1843*6a54128fSAndroid Build Coastguard Worker if (recovery_head != 0) {
1844*6a54128fSAndroid Build Coastguard Worker if (tdb_free(tdb, recovery_head, &rec) == -1) {
1845*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to free previous recovery area\n"));
1846*6a54128fSAndroid Build Coastguard Worker return -1;
1847*6a54128fSAndroid Build Coastguard Worker }
1848*6a54128fSAndroid Build Coastguard Worker }
1849*6a54128fSAndroid Build Coastguard Worker
1850*6a54128fSAndroid Build Coastguard Worker /* the tdb_free() call might have increased the recovery size */
1851*6a54128fSAndroid Build Coastguard Worker *recovery_size = tdb_recovery_size(tdb);
1852*6a54128fSAndroid Build Coastguard Worker
1853*6a54128fSAndroid Build Coastguard Worker /* round up to a multiple of page size */
1854*6a54128fSAndroid Build Coastguard Worker *recovery_max_size = TDB_ALIGN(sizeof(rec) + *recovery_size, tdb->page_size) - sizeof(rec);
1855*6a54128fSAndroid Build Coastguard Worker *recovery_offset = tdb->map_size;
1856*6a54128fSAndroid Build Coastguard Worker recovery_head = *recovery_offset;
1857*6a54128fSAndroid Build Coastguard Worker
1858*6a54128fSAndroid Build Coastguard Worker if (methods->tdb_expand_file(tdb, tdb->transaction->old_map_size,
1859*6a54128fSAndroid Build Coastguard Worker (tdb->map_size - tdb->transaction->old_map_size) +
1860*6a54128fSAndroid Build Coastguard Worker sizeof(rec) + *recovery_max_size) == -1) {
1861*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to create recovery area\n"));
1862*6a54128fSAndroid Build Coastguard Worker return -1;
1863*6a54128fSAndroid Build Coastguard Worker }
1864*6a54128fSAndroid Build Coastguard Worker
1865*6a54128fSAndroid Build Coastguard Worker /* remap the file (if using mmap) */
1866*6a54128fSAndroid Build Coastguard Worker methods->tdb_oob(tdb, tdb->map_size + 1, 1);
1867*6a54128fSAndroid Build Coastguard Worker
1868*6a54128fSAndroid Build Coastguard Worker /* we have to reset the old map size so that we don't try to expand the file
1869*6a54128fSAndroid Build Coastguard Worker again in the transaction commit, which would destroy the recovery area */
1870*6a54128fSAndroid Build Coastguard Worker tdb->transaction->old_map_size = tdb->map_size;
1871*6a54128fSAndroid Build Coastguard Worker
1872*6a54128fSAndroid Build Coastguard Worker /* write the recovery header offset and sync - we can sync without a race here
1873*6a54128fSAndroid Build Coastguard Worker as the magic ptr in the recovery record has not been set */
1874*6a54128fSAndroid Build Coastguard Worker CONVERT(recovery_head);
1875*6a54128fSAndroid Build Coastguard Worker if (methods->tdb_write(tdb, TDB_RECOVERY_HEAD,
1876*6a54128fSAndroid Build Coastguard Worker &recovery_head, sizeof(tdb_off_t)) == -1) {
1877*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to write recovery head\n"));
1878*6a54128fSAndroid Build Coastguard Worker return -1;
1879*6a54128fSAndroid Build Coastguard Worker }
1880*6a54128fSAndroid Build Coastguard Worker
1881*6a54128fSAndroid Build Coastguard Worker return 0;
1882*6a54128fSAndroid Build Coastguard Worker }
1883*6a54128fSAndroid Build Coastguard Worker
1884*6a54128fSAndroid Build Coastguard Worker
1885*6a54128fSAndroid Build Coastguard Worker /*
1886*6a54128fSAndroid Build Coastguard Worker setup the recovery data that will be used on a crash during commit
1887*6a54128fSAndroid Build Coastguard Worker */
transaction_setup_recovery(struct tdb_context * tdb,tdb_off_t * magic_offset)1888*6a54128fSAndroid Build Coastguard Worker static int transaction_setup_recovery(struct tdb_context *tdb,
1889*6a54128fSAndroid Build Coastguard Worker tdb_off_t *magic_offset)
1890*6a54128fSAndroid Build Coastguard Worker {
1891*6a54128fSAndroid Build Coastguard Worker struct tdb_transaction_el *el;
1892*6a54128fSAndroid Build Coastguard Worker tdb_len_t recovery_size;
1893*6a54128fSAndroid Build Coastguard Worker unsigned char *data, *p;
1894*6a54128fSAndroid Build Coastguard Worker const struct tdb_methods *methods = tdb->transaction->io_methods;
1895*6a54128fSAndroid Build Coastguard Worker struct list_struct *rec;
1896*6a54128fSAndroid Build Coastguard Worker tdb_off_t recovery_offset, recovery_max_size;
1897*6a54128fSAndroid Build Coastguard Worker tdb_off_t old_map_size = tdb->transaction->old_map_size;
1898*6a54128fSAndroid Build Coastguard Worker u32 magic, tailer;
1899*6a54128fSAndroid Build Coastguard Worker
1900*6a54128fSAndroid Build Coastguard Worker /*
1901*6a54128fSAndroid Build Coastguard Worker check that the recovery area has enough space
1902*6a54128fSAndroid Build Coastguard Worker */
1903*6a54128fSAndroid Build Coastguard Worker if (tdb_recovery_allocate(tdb, &recovery_size,
1904*6a54128fSAndroid Build Coastguard Worker &recovery_offset, &recovery_max_size) == -1) {
1905*6a54128fSAndroid Build Coastguard Worker return -1;
1906*6a54128fSAndroid Build Coastguard Worker }
1907*6a54128fSAndroid Build Coastguard Worker
1908*6a54128fSAndroid Build Coastguard Worker data = (unsigned char *)malloc(recovery_size + sizeof(*rec));
1909*6a54128fSAndroid Build Coastguard Worker if (data == NULL) {
1910*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_OOM;
1911*6a54128fSAndroid Build Coastguard Worker return -1;
1912*6a54128fSAndroid Build Coastguard Worker }
1913*6a54128fSAndroid Build Coastguard Worker
1914*6a54128fSAndroid Build Coastguard Worker rec = (struct list_struct *)data;
1915*6a54128fSAndroid Build Coastguard Worker memset(rec, 0, sizeof(*rec));
1916*6a54128fSAndroid Build Coastguard Worker
1917*6a54128fSAndroid Build Coastguard Worker rec->magic = 0;
1918*6a54128fSAndroid Build Coastguard Worker rec->data_len = recovery_size;
1919*6a54128fSAndroid Build Coastguard Worker rec->rec_len = recovery_max_size;
1920*6a54128fSAndroid Build Coastguard Worker rec->key_len = old_map_size;
1921*6a54128fSAndroid Build Coastguard Worker CONVERT(rec);
1922*6a54128fSAndroid Build Coastguard Worker
1923*6a54128fSAndroid Build Coastguard Worker /* build the recovery data into a single blob to allow us to do a single
1924*6a54128fSAndroid Build Coastguard Worker large write, which should be more efficient */
1925*6a54128fSAndroid Build Coastguard Worker p = data + sizeof(*rec);
1926*6a54128fSAndroid Build Coastguard Worker for (el=tdb->transaction->elements;el;el=el->next) {
1927*6a54128fSAndroid Build Coastguard Worker if (el->offset >= old_map_size) {
1928*6a54128fSAndroid Build Coastguard Worker continue;
1929*6a54128fSAndroid Build Coastguard Worker }
1930*6a54128fSAndroid Build Coastguard Worker if (el->offset + el->length > tdb->transaction->old_map_size) {
1931*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_setup_recovery: transaction data over new region boundary\n"));
1932*6a54128fSAndroid Build Coastguard Worker free(data);
1933*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_CORRUPT;
1934*6a54128fSAndroid Build Coastguard Worker return -1;
1935*6a54128fSAndroid Build Coastguard Worker }
1936*6a54128fSAndroid Build Coastguard Worker memcpy(p, &el->offset, 4);
1937*6a54128fSAndroid Build Coastguard Worker memcpy(p+4, &el->length, 4);
1938*6a54128fSAndroid Build Coastguard Worker if (DOCONV()) {
1939*6a54128fSAndroid Build Coastguard Worker tdb_convert(p, 8);
1940*6a54128fSAndroid Build Coastguard Worker }
1941*6a54128fSAndroid Build Coastguard Worker /* the recovery area contains the old data, not the
1942*6a54128fSAndroid Build Coastguard Worker new data, so we have to call the original tdb_read
1943*6a54128fSAndroid Build Coastguard Worker method to get it */
1944*6a54128fSAndroid Build Coastguard Worker if (methods->tdb_read(tdb, el->offset, p + 8, el->length, 0) != 0) {
1945*6a54128fSAndroid Build Coastguard Worker free(data);
1946*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
1947*6a54128fSAndroid Build Coastguard Worker return -1;
1948*6a54128fSAndroid Build Coastguard Worker }
1949*6a54128fSAndroid Build Coastguard Worker p += 8 + el->length;
1950*6a54128fSAndroid Build Coastguard Worker }
1951*6a54128fSAndroid Build Coastguard Worker
1952*6a54128fSAndroid Build Coastguard Worker /* and the tailer */
1953*6a54128fSAndroid Build Coastguard Worker tailer = sizeof(*rec) + recovery_max_size;
1954*6a54128fSAndroid Build Coastguard Worker memcpy(p, &tailer, 4);
1955*6a54128fSAndroid Build Coastguard Worker CONVERT(p);
1956*6a54128fSAndroid Build Coastguard Worker
1957*6a54128fSAndroid Build Coastguard Worker /* write the recovery data to the recovery area */
1958*6a54128fSAndroid Build Coastguard Worker if (methods->tdb_write(tdb, recovery_offset, data, sizeof(*rec) + recovery_size) == -1) {
1959*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_setup_recovery: failed to write recovery data\n"));
1960*6a54128fSAndroid Build Coastguard Worker free(data);
1961*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
1962*6a54128fSAndroid Build Coastguard Worker return -1;
1963*6a54128fSAndroid Build Coastguard Worker }
1964*6a54128fSAndroid Build Coastguard Worker
1965*6a54128fSAndroid Build Coastguard Worker /* as we don't have ordered writes, we have to sync the recovery
1966*6a54128fSAndroid Build Coastguard Worker data before we update the magic to indicate that the recovery
1967*6a54128fSAndroid Build Coastguard Worker data is present */
1968*6a54128fSAndroid Build Coastguard Worker if (transaction_sync(tdb, recovery_offset, sizeof(*rec) + recovery_size) == -1) {
1969*6a54128fSAndroid Build Coastguard Worker free(data);
1970*6a54128fSAndroid Build Coastguard Worker return -1;
1971*6a54128fSAndroid Build Coastguard Worker }
1972*6a54128fSAndroid Build Coastguard Worker
1973*6a54128fSAndroid Build Coastguard Worker free(data);
1974*6a54128fSAndroid Build Coastguard Worker
1975*6a54128fSAndroid Build Coastguard Worker magic = TDB_RECOVERY_MAGIC;
1976*6a54128fSAndroid Build Coastguard Worker CONVERT(magic);
1977*6a54128fSAndroid Build Coastguard Worker
1978*6a54128fSAndroid Build Coastguard Worker *magic_offset = recovery_offset + offsetof(struct list_struct, magic);
1979*6a54128fSAndroid Build Coastguard Worker
1980*6a54128fSAndroid Build Coastguard Worker if (methods->tdb_write(tdb, *magic_offset, &magic, sizeof(magic)) == -1) {
1981*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_setup_recovery: failed to write recovery magic\n"));
1982*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
1983*6a54128fSAndroid Build Coastguard Worker return -1;
1984*6a54128fSAndroid Build Coastguard Worker }
1985*6a54128fSAndroid Build Coastguard Worker
1986*6a54128fSAndroid Build Coastguard Worker /* ensure the recovery magic marker is on disk */
1987*6a54128fSAndroid Build Coastguard Worker if (transaction_sync(tdb, *magic_offset, sizeof(magic)) == -1) {
1988*6a54128fSAndroid Build Coastguard Worker return -1;
1989*6a54128fSAndroid Build Coastguard Worker }
1990*6a54128fSAndroid Build Coastguard Worker
1991*6a54128fSAndroid Build Coastguard Worker return 0;
1992*6a54128fSAndroid Build Coastguard Worker }
1993*6a54128fSAndroid Build Coastguard Worker
1994*6a54128fSAndroid Build Coastguard Worker /*
1995*6a54128fSAndroid Build Coastguard Worker commit the current transaction
1996*6a54128fSAndroid Build Coastguard Worker */
tdb_transaction_commit(struct tdb_context * tdb)1997*6a54128fSAndroid Build Coastguard Worker int tdb_transaction_commit(struct tdb_context *tdb)
1998*6a54128fSAndroid Build Coastguard Worker {
1999*6a54128fSAndroid Build Coastguard Worker const struct tdb_methods *methods;
2000*6a54128fSAndroid Build Coastguard Worker tdb_off_t magic_offset = 0;
2001*6a54128fSAndroid Build Coastguard Worker u32 zero = 0;
2002*6a54128fSAndroid Build Coastguard Worker
2003*6a54128fSAndroid Build Coastguard Worker if (tdb->transaction == NULL) {
2004*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_commit: no transaction\n"));
2005*6a54128fSAndroid Build Coastguard Worker return -1;
2006*6a54128fSAndroid Build Coastguard Worker }
2007*6a54128fSAndroid Build Coastguard Worker
2008*6a54128fSAndroid Build Coastguard Worker if (tdb->transaction->transaction_error) {
2009*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
2010*6a54128fSAndroid Build Coastguard Worker tdb_transaction_cancel(tdb);
2011*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_commit: transaction error pending\n"));
2012*6a54128fSAndroid Build Coastguard Worker return -1;
2013*6a54128fSAndroid Build Coastguard Worker }
2014*6a54128fSAndroid Build Coastguard Worker
2015*6a54128fSAndroid Build Coastguard Worker if (tdb->transaction->nesting != 0) {
2016*6a54128fSAndroid Build Coastguard Worker tdb->transaction->nesting--;
2017*6a54128fSAndroid Build Coastguard Worker return 0;
2018*6a54128fSAndroid Build Coastguard Worker }
2019*6a54128fSAndroid Build Coastguard Worker
2020*6a54128fSAndroid Build Coastguard Worker /* check for a null transaction */
2021*6a54128fSAndroid Build Coastguard Worker if (tdb->transaction->elements == NULL) {
2022*6a54128fSAndroid Build Coastguard Worker tdb_transaction_cancel(tdb);
2023*6a54128fSAndroid Build Coastguard Worker return 0;
2024*6a54128fSAndroid Build Coastguard Worker }
2025*6a54128fSAndroid Build Coastguard Worker
2026*6a54128fSAndroid Build Coastguard Worker methods = tdb->transaction->io_methods;
2027*6a54128fSAndroid Build Coastguard Worker
2028*6a54128fSAndroid Build Coastguard Worker /* if there are any locks pending then the caller has not
2029*6a54128fSAndroid Build Coastguard Worker nested their locks properly, so fail the transaction */
2030*6a54128fSAndroid Build Coastguard Worker if (tdb->num_locks || tdb->global_lock.count) {
2031*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_LOCK;
2032*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_commit: locks pending on commit\n"));
2033*6a54128fSAndroid Build Coastguard Worker tdb_transaction_cancel(tdb);
2034*6a54128fSAndroid Build Coastguard Worker return -1;
2035*6a54128fSAndroid Build Coastguard Worker }
2036*6a54128fSAndroid Build Coastguard Worker
2037*6a54128fSAndroid Build Coastguard Worker /* upgrade the main transaction lock region to a write lock */
2038*6a54128fSAndroid Build Coastguard Worker if (tdb_brlock_upgrade(tdb, FREELIST_TOP, 0) == -1) {
2039*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: failed to upgrade hash locks\n"));
2040*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_LOCK;
2041*6a54128fSAndroid Build Coastguard Worker tdb_transaction_cancel(tdb);
2042*6a54128fSAndroid Build Coastguard Worker return -1;
2043*6a54128fSAndroid Build Coastguard Worker }
2044*6a54128fSAndroid Build Coastguard Worker
2045*6a54128fSAndroid Build Coastguard Worker /* get the global lock - this prevents new users attaching to the database
2046*6a54128fSAndroid Build Coastguard Worker during the commit */
2047*6a54128fSAndroid Build Coastguard Worker if (tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0, 1) == -1) {
2048*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_commit: failed to get global lock\n"));
2049*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_LOCK;
2050*6a54128fSAndroid Build Coastguard Worker tdb_transaction_cancel(tdb);
2051*6a54128fSAndroid Build Coastguard Worker return -1;
2052*6a54128fSAndroid Build Coastguard Worker }
2053*6a54128fSAndroid Build Coastguard Worker
2054*6a54128fSAndroid Build Coastguard Worker if (!(tdb->flags & TDB_NOSYNC)) {
2055*6a54128fSAndroid Build Coastguard Worker /* write the recovery data to the end of the file */
2056*6a54128fSAndroid Build Coastguard Worker if (transaction_setup_recovery(tdb, &magic_offset) == -1) {
2057*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_commit: failed to setup recovery data\n"));
2058*6a54128fSAndroid Build Coastguard Worker tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
2059*6a54128fSAndroid Build Coastguard Worker tdb_transaction_cancel(tdb);
2060*6a54128fSAndroid Build Coastguard Worker return -1;
2061*6a54128fSAndroid Build Coastguard Worker }
2062*6a54128fSAndroid Build Coastguard Worker }
2063*6a54128fSAndroid Build Coastguard Worker
2064*6a54128fSAndroid Build Coastguard Worker /* expand the file to the new size if needed */
2065*6a54128fSAndroid Build Coastguard Worker if (tdb->map_size != tdb->transaction->old_map_size) {
2066*6a54128fSAndroid Build Coastguard Worker if (methods->tdb_expand_file(tdb, tdb->transaction->old_map_size,
2067*6a54128fSAndroid Build Coastguard Worker tdb->map_size -
2068*6a54128fSAndroid Build Coastguard Worker tdb->transaction->old_map_size) == -1) {
2069*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
2070*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_commit: expansion failed\n"));
2071*6a54128fSAndroid Build Coastguard Worker tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
2072*6a54128fSAndroid Build Coastguard Worker tdb_transaction_cancel(tdb);
2073*6a54128fSAndroid Build Coastguard Worker return -1;
2074*6a54128fSAndroid Build Coastguard Worker }
2075*6a54128fSAndroid Build Coastguard Worker tdb->map_size = tdb->transaction->old_map_size;
2076*6a54128fSAndroid Build Coastguard Worker methods->tdb_oob(tdb, tdb->map_size + 1, 1);
2077*6a54128fSAndroid Build Coastguard Worker }
2078*6a54128fSAndroid Build Coastguard Worker
2079*6a54128fSAndroid Build Coastguard Worker /* perform all the writes */
2080*6a54128fSAndroid Build Coastguard Worker while (tdb->transaction->elements) {
2081*6a54128fSAndroid Build Coastguard Worker struct tdb_transaction_el *el = tdb->transaction->elements;
2082*6a54128fSAndroid Build Coastguard Worker
2083*6a54128fSAndroid Build Coastguard Worker if (methods->tdb_write(tdb, el->offset, el->data, el->length) == -1) {
2084*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_commit: write failed during commit\n"));
2085*6a54128fSAndroid Build Coastguard Worker
2086*6a54128fSAndroid Build Coastguard Worker /* we've overwritten part of the data and
2087*6a54128fSAndroid Build Coastguard Worker possibly expanded the file, so we need to
2088*6a54128fSAndroid Build Coastguard Worker run the crash recovery code */
2089*6a54128fSAndroid Build Coastguard Worker tdb->methods = methods;
2090*6a54128fSAndroid Build Coastguard Worker tdb_transaction_recover(tdb);
2091*6a54128fSAndroid Build Coastguard Worker
2092*6a54128fSAndroid Build Coastguard Worker tdb_transaction_cancel(tdb);
2093*6a54128fSAndroid Build Coastguard Worker tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
2094*6a54128fSAndroid Build Coastguard Worker
2095*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_commit: write failed\n"));
2096*6a54128fSAndroid Build Coastguard Worker return -1;
2097*6a54128fSAndroid Build Coastguard Worker }
2098*6a54128fSAndroid Build Coastguard Worker tdb->transaction->elements = el->next;
2099*6a54128fSAndroid Build Coastguard Worker free(el->data);
2100*6a54128fSAndroid Build Coastguard Worker free(el);
2101*6a54128fSAndroid Build Coastguard Worker }
2102*6a54128fSAndroid Build Coastguard Worker
2103*6a54128fSAndroid Build Coastguard Worker if (!(tdb->flags & TDB_NOSYNC)) {
2104*6a54128fSAndroid Build Coastguard Worker /* ensure the new data is on disk */
2105*6a54128fSAndroid Build Coastguard Worker if (transaction_sync(tdb, 0, tdb->map_size) == -1) {
2106*6a54128fSAndroid Build Coastguard Worker return -1;
2107*6a54128fSAndroid Build Coastguard Worker }
2108*6a54128fSAndroid Build Coastguard Worker
2109*6a54128fSAndroid Build Coastguard Worker /* remove the recovery marker */
2110*6a54128fSAndroid Build Coastguard Worker if (methods->tdb_write(tdb, magic_offset, &zero, 4) == -1) {
2111*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_commit: failed to remove recovery magic\n"));
2112*6a54128fSAndroid Build Coastguard Worker return -1;
2113*6a54128fSAndroid Build Coastguard Worker }
2114*6a54128fSAndroid Build Coastguard Worker
2115*6a54128fSAndroid Build Coastguard Worker /* ensure the recovery marker has been removed on disk */
2116*6a54128fSAndroid Build Coastguard Worker if (transaction_sync(tdb, magic_offset, 4) == -1) {
2117*6a54128fSAndroid Build Coastguard Worker return -1;
2118*6a54128fSAndroid Build Coastguard Worker }
2119*6a54128fSAndroid Build Coastguard Worker }
2120*6a54128fSAndroid Build Coastguard Worker
2121*6a54128fSAndroid Build Coastguard Worker tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
2122*6a54128fSAndroid Build Coastguard Worker
2123*6a54128fSAndroid Build Coastguard Worker /*
2124*6a54128fSAndroid Build Coastguard Worker TODO: maybe write to some dummy hdr field, or write to magic
2125*6a54128fSAndroid Build Coastguard Worker offset without mmap, before the last sync, instead of the
2126*6a54128fSAndroid Build Coastguard Worker utime() call
2127*6a54128fSAndroid Build Coastguard Worker */
2128*6a54128fSAndroid Build Coastguard Worker
2129*6a54128fSAndroid Build Coastguard Worker /* on some systems (like Linux 2.6.x) changes via mmap/msync
2130*6a54128fSAndroid Build Coastguard Worker don't change the mtime of the file, this means the file may
2131*6a54128fSAndroid Build Coastguard Worker not be backed up (as tdb rounding to block sizes means that
2132*6a54128fSAndroid Build Coastguard Worker file size changes are quite rare too). The following forces
2133*6a54128fSAndroid Build Coastguard Worker mtime changes when a transaction completes */
2134*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_UTIME
2135*6a54128fSAndroid Build Coastguard Worker utime(tdb->name, NULL);
2136*6a54128fSAndroid Build Coastguard Worker #endif
2137*6a54128fSAndroid Build Coastguard Worker
2138*6a54128fSAndroid Build Coastguard Worker /* use a transaction cancel to free memory and remove the
2139*6a54128fSAndroid Build Coastguard Worker transaction locks */
2140*6a54128fSAndroid Build Coastguard Worker tdb_transaction_cancel(tdb);
2141*6a54128fSAndroid Build Coastguard Worker return 0;
2142*6a54128fSAndroid Build Coastguard Worker }
2143*6a54128fSAndroid Build Coastguard Worker
2144*6a54128fSAndroid Build Coastguard Worker
2145*6a54128fSAndroid Build Coastguard Worker /*
2146*6a54128fSAndroid Build Coastguard Worker recover from an aborted transaction. Must be called with exclusive
2147*6a54128fSAndroid Build Coastguard Worker database write access already established (including the global
2148*6a54128fSAndroid Build Coastguard Worker lock to prevent new processes attaching)
2149*6a54128fSAndroid Build Coastguard Worker */
tdb_transaction_recover(struct tdb_context * tdb)2150*6a54128fSAndroid Build Coastguard Worker int tdb_transaction_recover(struct tdb_context *tdb)
2151*6a54128fSAndroid Build Coastguard Worker {
2152*6a54128fSAndroid Build Coastguard Worker tdb_off_t recovery_head, recovery_eof;
2153*6a54128fSAndroid Build Coastguard Worker unsigned char *data, *p;
2154*6a54128fSAndroid Build Coastguard Worker u32 zero = 0;
2155*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
2156*6a54128fSAndroid Build Coastguard Worker
2157*6a54128fSAndroid Build Coastguard Worker /* find the recovery area */
2158*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
2159*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to read recovery head\n"));
2160*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
2161*6a54128fSAndroid Build Coastguard Worker return -1;
2162*6a54128fSAndroid Build Coastguard Worker }
2163*6a54128fSAndroid Build Coastguard Worker
2164*6a54128fSAndroid Build Coastguard Worker if (recovery_head == 0) {
2165*6a54128fSAndroid Build Coastguard Worker /* we have never allocated a recovery record */
2166*6a54128fSAndroid Build Coastguard Worker return 0;
2167*6a54128fSAndroid Build Coastguard Worker }
2168*6a54128fSAndroid Build Coastguard Worker
2169*6a54128fSAndroid Build Coastguard Worker /* read the recovery record */
2170*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_read(tdb, recovery_head, &rec,
2171*6a54128fSAndroid Build Coastguard Worker sizeof(rec), DOCONV()) == -1) {
2172*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to read recovery record\n"));
2173*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
2174*6a54128fSAndroid Build Coastguard Worker return -1;
2175*6a54128fSAndroid Build Coastguard Worker }
2176*6a54128fSAndroid Build Coastguard Worker
2177*6a54128fSAndroid Build Coastguard Worker if (rec.magic != TDB_RECOVERY_MAGIC) {
2178*6a54128fSAndroid Build Coastguard Worker /* there is no valid recovery data */
2179*6a54128fSAndroid Build Coastguard Worker return 0;
2180*6a54128fSAndroid Build Coastguard Worker }
2181*6a54128fSAndroid Build Coastguard Worker
2182*6a54128fSAndroid Build Coastguard Worker if (tdb->read_only) {
2183*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: attempt to recover read only database\n"));
2184*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_CORRUPT;
2185*6a54128fSAndroid Build Coastguard Worker return -1;
2186*6a54128fSAndroid Build Coastguard Worker }
2187*6a54128fSAndroid Build Coastguard Worker
2188*6a54128fSAndroid Build Coastguard Worker recovery_eof = rec.key_len;
2189*6a54128fSAndroid Build Coastguard Worker
2190*6a54128fSAndroid Build Coastguard Worker data = (unsigned char *)malloc(rec.data_len);
2191*6a54128fSAndroid Build Coastguard Worker if (data == NULL) {
2192*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to allocate recovery data\n"));
2193*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_OOM;
2194*6a54128fSAndroid Build Coastguard Worker return -1;
2195*6a54128fSAndroid Build Coastguard Worker }
2196*6a54128fSAndroid Build Coastguard Worker
2197*6a54128fSAndroid Build Coastguard Worker /* read the full recovery data */
2198*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_read(tdb, recovery_head + sizeof(rec), data,
2199*6a54128fSAndroid Build Coastguard Worker rec.data_len, 0) == -1) {
2200*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to read recovery data\n"));
2201*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
2202*6a54128fSAndroid Build Coastguard Worker free(data);
2203*6a54128fSAndroid Build Coastguard Worker return -1;
2204*6a54128fSAndroid Build Coastguard Worker }
2205*6a54128fSAndroid Build Coastguard Worker
2206*6a54128fSAndroid Build Coastguard Worker /* recover the file data */
2207*6a54128fSAndroid Build Coastguard Worker p = data;
2208*6a54128fSAndroid Build Coastguard Worker while (p+8 < data + rec.data_len) {
2209*6a54128fSAndroid Build Coastguard Worker u32 ofs, len;
2210*6a54128fSAndroid Build Coastguard Worker if (DOCONV()) {
2211*6a54128fSAndroid Build Coastguard Worker tdb_convert(p, 8);
2212*6a54128fSAndroid Build Coastguard Worker }
2213*6a54128fSAndroid Build Coastguard Worker memcpy(&ofs, p, 4);
2214*6a54128fSAndroid Build Coastguard Worker memcpy(&len, p+4, 4);
2215*6a54128fSAndroid Build Coastguard Worker
2216*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_write(tdb, ofs, p+8, len) == -1) {
2217*6a54128fSAndroid Build Coastguard Worker free(data);
2218*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to recover %d bytes at offset %d\n", len, ofs));
2219*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
2220*6a54128fSAndroid Build Coastguard Worker return -1;
2221*6a54128fSAndroid Build Coastguard Worker }
2222*6a54128fSAndroid Build Coastguard Worker p += 8 + len;
2223*6a54128fSAndroid Build Coastguard Worker }
2224*6a54128fSAndroid Build Coastguard Worker
2225*6a54128fSAndroid Build Coastguard Worker free(data);
2226*6a54128fSAndroid Build Coastguard Worker
2227*6a54128fSAndroid Build Coastguard Worker if (transaction_sync(tdb, 0, tdb->map_size) == -1) {
2228*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to sync recovery\n"));
2229*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
2230*6a54128fSAndroid Build Coastguard Worker return -1;
2231*6a54128fSAndroid Build Coastguard Worker }
2232*6a54128fSAndroid Build Coastguard Worker
2233*6a54128fSAndroid Build Coastguard Worker /* if the recovery area is after the recovered eof then remove it */
2234*6a54128fSAndroid Build Coastguard Worker if (recovery_eof <= recovery_head) {
2235*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_write(tdb, TDB_RECOVERY_HEAD, &zero) == -1) {
2236*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to remove recovery head\n"));
2237*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
2238*6a54128fSAndroid Build Coastguard Worker return -1;
2239*6a54128fSAndroid Build Coastguard Worker }
2240*6a54128fSAndroid Build Coastguard Worker }
2241*6a54128fSAndroid Build Coastguard Worker
2242*6a54128fSAndroid Build Coastguard Worker /* remove the recovery magic */
2243*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_write(tdb, recovery_head + offsetof(struct list_struct, magic),
2244*6a54128fSAndroid Build Coastguard Worker &zero) == -1) {
2245*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to remove recovery magic\n"));
2246*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
2247*6a54128fSAndroid Build Coastguard Worker return -1;
2248*6a54128fSAndroid Build Coastguard Worker }
2249*6a54128fSAndroid Build Coastguard Worker
2250*6a54128fSAndroid Build Coastguard Worker /* reduce the file size to the old size */
2251*6a54128fSAndroid Build Coastguard Worker tdb_munmap(tdb);
2252*6a54128fSAndroid Build Coastguard Worker if (ftruncate(tdb->fd, recovery_eof) != 0) {
2253*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to reduce to recovery size\n"));
2254*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
2255*6a54128fSAndroid Build Coastguard Worker return -1;
2256*6a54128fSAndroid Build Coastguard Worker }
2257*6a54128fSAndroid Build Coastguard Worker tdb->map_size = recovery_eof;
2258*6a54128fSAndroid Build Coastguard Worker tdb_mmap(tdb);
2259*6a54128fSAndroid Build Coastguard Worker
2260*6a54128fSAndroid Build Coastguard Worker if (transaction_sync(tdb, 0, recovery_eof) == -1) {
2261*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to sync2 recovery\n"));
2262*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_IO;
2263*6a54128fSAndroid Build Coastguard Worker return -1;
2264*6a54128fSAndroid Build Coastguard Worker }
2265*6a54128fSAndroid Build Coastguard Worker
2266*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_TRACE, "tdb_transaction_recover: recovered %d byte database\n",
2267*6a54128fSAndroid Build Coastguard Worker recovery_eof));
2268*6a54128fSAndroid Build Coastguard Worker
2269*6a54128fSAndroid Build Coastguard Worker /* all done */
2270*6a54128fSAndroid Build Coastguard Worker return 0;
2271*6a54128fSAndroid Build Coastguard Worker }
2272*6a54128fSAndroid Build Coastguard Worker
2273*6a54128fSAndroid Build Coastguard Worker /* file: freelist.c */
2274*6a54128fSAndroid Build Coastguard Worker
2275*6a54128fSAndroid Build Coastguard Worker /* read a freelist record and check for simple errors */
tdb_rec_free_read(struct tdb_context * tdb,tdb_off_t off,struct list_struct * rec)2276*6a54128fSAndroid Build Coastguard Worker static int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off, struct list_struct *rec)
2277*6a54128fSAndroid Build Coastguard Worker {
2278*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_read(tdb, off, rec, sizeof(*rec),DOCONV()) == -1)
2279*6a54128fSAndroid Build Coastguard Worker return -1;
2280*6a54128fSAndroid Build Coastguard Worker
2281*6a54128fSAndroid Build Coastguard Worker if (rec->magic == TDB_MAGIC) {
2282*6a54128fSAndroid Build Coastguard Worker /* this happens when a app is showdown while deleting a record - we should
2283*6a54128fSAndroid Build Coastguard Worker not completely fail when this happens */
2284*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_rec_free_read non-free magic 0x%x at offset=%d - fixing\n",
2285*6a54128fSAndroid Build Coastguard Worker rec->magic, off));
2286*6a54128fSAndroid Build Coastguard Worker rec->magic = TDB_FREE_MAGIC;
2287*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_write(tdb, off, rec, sizeof(*rec)) == -1)
2288*6a54128fSAndroid Build Coastguard Worker return -1;
2289*6a54128fSAndroid Build Coastguard Worker }
2290*6a54128fSAndroid Build Coastguard Worker
2291*6a54128fSAndroid Build Coastguard Worker if (rec->magic != TDB_FREE_MAGIC) {
2292*6a54128fSAndroid Build Coastguard Worker /* Ensure ecode is set for log fn. */
2293*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_CORRUPT;
2294*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_rec_free_read bad magic 0x%x at offset=%d\n",
2295*6a54128fSAndroid Build Coastguard Worker rec->magic, off));
2296*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
2297*6a54128fSAndroid Build Coastguard Worker }
2298*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0) != 0)
2299*6a54128fSAndroid Build Coastguard Worker return -1;
2300*6a54128fSAndroid Build Coastguard Worker return 0;
2301*6a54128fSAndroid Build Coastguard Worker }
2302*6a54128fSAndroid Build Coastguard Worker
2303*6a54128fSAndroid Build Coastguard Worker
2304*6a54128fSAndroid Build Coastguard Worker
2305*6a54128fSAndroid Build Coastguard Worker /* Remove an element from the freelist. Must have alloc lock. */
remove_from_freelist(struct tdb_context * tdb,tdb_off_t off,tdb_off_t next)2306*6a54128fSAndroid Build Coastguard Worker static int remove_from_freelist(struct tdb_context *tdb, tdb_off_t off, tdb_off_t next)
2307*6a54128fSAndroid Build Coastguard Worker {
2308*6a54128fSAndroid Build Coastguard Worker tdb_off_t last_ptr, i;
2309*6a54128fSAndroid Build Coastguard Worker
2310*6a54128fSAndroid Build Coastguard Worker /* read in the freelist top */
2311*6a54128fSAndroid Build Coastguard Worker last_ptr = FREELIST_TOP;
2312*6a54128fSAndroid Build Coastguard Worker while (tdb_ofs_read(tdb, last_ptr, &i) != -1 && i != 0) {
2313*6a54128fSAndroid Build Coastguard Worker if (i == off) {
2314*6a54128fSAndroid Build Coastguard Worker /* We've found it! */
2315*6a54128fSAndroid Build Coastguard Worker return tdb_ofs_write(tdb, last_ptr, &next);
2316*6a54128fSAndroid Build Coastguard Worker }
2317*6a54128fSAndroid Build Coastguard Worker /* Follow chain (next offset is at start of record) */
2318*6a54128fSAndroid Build Coastguard Worker last_ptr = i;
2319*6a54128fSAndroid Build Coastguard Worker }
2320*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL,"remove_from_freelist: not on list at off=%d\n", off));
2321*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
2322*6a54128fSAndroid Build Coastguard Worker }
2323*6a54128fSAndroid Build Coastguard Worker
2324*6a54128fSAndroid Build Coastguard Worker
2325*6a54128fSAndroid Build Coastguard Worker /* update a record tailer (must hold allocation lock) */
update_tailer(struct tdb_context * tdb,tdb_off_t offset,const struct list_struct * rec)2326*6a54128fSAndroid Build Coastguard Worker static int update_tailer(struct tdb_context *tdb, tdb_off_t offset,
2327*6a54128fSAndroid Build Coastguard Worker const struct list_struct *rec)
2328*6a54128fSAndroid Build Coastguard Worker {
2329*6a54128fSAndroid Build Coastguard Worker tdb_off_t totalsize;
2330*6a54128fSAndroid Build Coastguard Worker
2331*6a54128fSAndroid Build Coastguard Worker /* Offset of tailer from record header */
2332*6a54128fSAndroid Build Coastguard Worker totalsize = sizeof(*rec) + rec->rec_len;
2333*6a54128fSAndroid Build Coastguard Worker return tdb_ofs_write(tdb, offset + totalsize - sizeof(tdb_off_t),
2334*6a54128fSAndroid Build Coastguard Worker &totalsize);
2335*6a54128fSAndroid Build Coastguard Worker }
2336*6a54128fSAndroid Build Coastguard Worker
2337*6a54128fSAndroid Build Coastguard Worker /* Add an element into the freelist. Merge adjacent records if
2338*6a54128fSAndroid Build Coastguard Worker necessary. */
tdb_free(struct tdb_context * tdb,tdb_off_t offset,struct list_struct * rec)2339*6a54128fSAndroid Build Coastguard Worker int tdb_free(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
2340*6a54128fSAndroid Build Coastguard Worker {
2341*6a54128fSAndroid Build Coastguard Worker tdb_off_t right, left;
2342*6a54128fSAndroid Build Coastguard Worker
2343*6a54128fSAndroid Build Coastguard Worker /* Allocation and tailer lock */
2344*6a54128fSAndroid Build Coastguard Worker if (tdb_lock(tdb, -1, F_WRLCK) != 0)
2345*6a54128fSAndroid Build Coastguard Worker return -1;
2346*6a54128fSAndroid Build Coastguard Worker
2347*6a54128fSAndroid Build Coastguard Worker /* set an initial tailer, so if we fail we don't leave a bogus record */
2348*6a54128fSAndroid Build Coastguard Worker if (update_tailer(tdb, offset, rec) != 0) {
2349*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: update_tailer failed!\n"));
2350*6a54128fSAndroid Build Coastguard Worker goto fail;
2351*6a54128fSAndroid Build Coastguard Worker }
2352*6a54128fSAndroid Build Coastguard Worker
2353*6a54128fSAndroid Build Coastguard Worker /* Look right first (I'm an Australian, dammit) */
2354*6a54128fSAndroid Build Coastguard Worker right = offset + sizeof(*rec) + rec->rec_len;
2355*6a54128fSAndroid Build Coastguard Worker if (right + sizeof(*rec) <= tdb->map_size) {
2356*6a54128fSAndroid Build Coastguard Worker struct list_struct r;
2357*6a54128fSAndroid Build Coastguard Worker
2358*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_read(tdb, right, &r, sizeof(r), DOCONV()) == -1) {
2359*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: right read failed at %u\n", right));
2360*6a54128fSAndroid Build Coastguard Worker goto left;
2361*6a54128fSAndroid Build Coastguard Worker }
2362*6a54128fSAndroid Build Coastguard Worker
2363*6a54128fSAndroid Build Coastguard Worker /* If it's free, expand to include it. */
2364*6a54128fSAndroid Build Coastguard Worker if (r.magic == TDB_FREE_MAGIC) {
2365*6a54128fSAndroid Build Coastguard Worker if (remove_from_freelist(tdb, right, r.next) == -1) {
2366*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: right free failed at %u\n", right));
2367*6a54128fSAndroid Build Coastguard Worker goto left;
2368*6a54128fSAndroid Build Coastguard Worker }
2369*6a54128fSAndroid Build Coastguard Worker rec->rec_len += sizeof(r) + r.rec_len;
2370*6a54128fSAndroid Build Coastguard Worker }
2371*6a54128fSAndroid Build Coastguard Worker }
2372*6a54128fSAndroid Build Coastguard Worker
2373*6a54128fSAndroid Build Coastguard Worker left:
2374*6a54128fSAndroid Build Coastguard Worker /* Look left */
2375*6a54128fSAndroid Build Coastguard Worker left = offset - sizeof(tdb_off_t);
2376*6a54128fSAndroid Build Coastguard Worker if (left > TDB_DATA_START(tdb->header.hash_size)) {
2377*6a54128fSAndroid Build Coastguard Worker struct list_struct l;
2378*6a54128fSAndroid Build Coastguard Worker tdb_off_t leftsize;
2379*6a54128fSAndroid Build Coastguard Worker
2380*6a54128fSAndroid Build Coastguard Worker /* Read in tailer and jump back to header */
2381*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, left, &leftsize) == -1) {
2382*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: left offset read failed at %u\n", left));
2383*6a54128fSAndroid Build Coastguard Worker goto update;
2384*6a54128fSAndroid Build Coastguard Worker }
2385*6a54128fSAndroid Build Coastguard Worker
2386*6a54128fSAndroid Build Coastguard Worker /* it could be uninitialised data */
2387*6a54128fSAndroid Build Coastguard Worker if (leftsize == 0 || leftsize == TDB_PAD_U32) {
2388*6a54128fSAndroid Build Coastguard Worker goto update;
2389*6a54128fSAndroid Build Coastguard Worker }
2390*6a54128fSAndroid Build Coastguard Worker
2391*6a54128fSAndroid Build Coastguard Worker left = offset - leftsize;
2392*6a54128fSAndroid Build Coastguard Worker
2393*6a54128fSAndroid Build Coastguard Worker /* Now read in record */
2394*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_read(tdb, left, &l, sizeof(l), DOCONV()) == -1) {
2395*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: left read failed at %u (%u)\n", left, leftsize));
2396*6a54128fSAndroid Build Coastguard Worker goto update;
2397*6a54128fSAndroid Build Coastguard Worker }
2398*6a54128fSAndroid Build Coastguard Worker
2399*6a54128fSAndroid Build Coastguard Worker /* If it's free, expand to include it. */
2400*6a54128fSAndroid Build Coastguard Worker if (l.magic == TDB_FREE_MAGIC) {
2401*6a54128fSAndroid Build Coastguard Worker if (remove_from_freelist(tdb, left, l.next) == -1) {
2402*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: left free failed at %u\n", left));
2403*6a54128fSAndroid Build Coastguard Worker goto update;
2404*6a54128fSAndroid Build Coastguard Worker } else {
2405*6a54128fSAndroid Build Coastguard Worker offset = left;
2406*6a54128fSAndroid Build Coastguard Worker rec->rec_len += leftsize;
2407*6a54128fSAndroid Build Coastguard Worker }
2408*6a54128fSAndroid Build Coastguard Worker }
2409*6a54128fSAndroid Build Coastguard Worker }
2410*6a54128fSAndroid Build Coastguard Worker
2411*6a54128fSAndroid Build Coastguard Worker update:
2412*6a54128fSAndroid Build Coastguard Worker if (update_tailer(tdb, offset, rec) == -1) {
2413*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: update_tailer failed at %u\n", offset));
2414*6a54128fSAndroid Build Coastguard Worker goto fail;
2415*6a54128fSAndroid Build Coastguard Worker }
2416*6a54128fSAndroid Build Coastguard Worker
2417*6a54128fSAndroid Build Coastguard Worker /* Now, prepend to free list */
2418*6a54128fSAndroid Build Coastguard Worker rec->magic = TDB_FREE_MAGIC;
2419*6a54128fSAndroid Build Coastguard Worker
2420*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, FREELIST_TOP, &rec->next) == -1 ||
2421*6a54128fSAndroid Build Coastguard Worker tdb_rec_write(tdb, offset, rec) == -1 ||
2422*6a54128fSAndroid Build Coastguard Worker tdb_ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
2423*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free record write failed at offset=%d\n", offset));
2424*6a54128fSAndroid Build Coastguard Worker goto fail;
2425*6a54128fSAndroid Build Coastguard Worker }
2426*6a54128fSAndroid Build Coastguard Worker
2427*6a54128fSAndroid Build Coastguard Worker /* And we're done. */
2428*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, -1, F_WRLCK);
2429*6a54128fSAndroid Build Coastguard Worker return 0;
2430*6a54128fSAndroid Build Coastguard Worker
2431*6a54128fSAndroid Build Coastguard Worker fail:
2432*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, -1, F_WRLCK);
2433*6a54128fSAndroid Build Coastguard Worker return -1;
2434*6a54128fSAndroid Build Coastguard Worker }
2435*6a54128fSAndroid Build Coastguard Worker
2436*6a54128fSAndroid Build Coastguard Worker
2437*6a54128fSAndroid Build Coastguard Worker /*
2438*6a54128fSAndroid Build Coastguard Worker the core of tdb_allocate - called when we have decided which
2439*6a54128fSAndroid Build Coastguard Worker free list entry to use
2440*6a54128fSAndroid Build Coastguard Worker */
tdb_allocate_ofs(struct tdb_context * tdb,tdb_len_t length,tdb_off_t rec_ptr,struct list_struct * rec,tdb_off_t last_ptr)2441*6a54128fSAndroid Build Coastguard Worker static tdb_off_t tdb_allocate_ofs(struct tdb_context *tdb, tdb_len_t length, tdb_off_t rec_ptr,
2442*6a54128fSAndroid Build Coastguard Worker struct list_struct *rec, tdb_off_t last_ptr)
2443*6a54128fSAndroid Build Coastguard Worker {
2444*6a54128fSAndroid Build Coastguard Worker struct list_struct newrec;
2445*6a54128fSAndroid Build Coastguard Worker tdb_off_t newrec_ptr;
2446*6a54128fSAndroid Build Coastguard Worker
2447*6a54128fSAndroid Build Coastguard Worker memset(&newrec, '\0', sizeof(newrec));
2448*6a54128fSAndroid Build Coastguard Worker
2449*6a54128fSAndroid Build Coastguard Worker /* found it - now possibly split it up */
2450*6a54128fSAndroid Build Coastguard Worker if (rec->rec_len > length + MIN_REC_SIZE) {
2451*6a54128fSAndroid Build Coastguard Worker /* Length of left piece */
2452*6a54128fSAndroid Build Coastguard Worker length = TDB_ALIGN(length, TDB_ALIGNMENT);
2453*6a54128fSAndroid Build Coastguard Worker
2454*6a54128fSAndroid Build Coastguard Worker /* Right piece to go on free list */
2455*6a54128fSAndroid Build Coastguard Worker newrec.rec_len = rec->rec_len - (sizeof(*rec) + length);
2456*6a54128fSAndroid Build Coastguard Worker newrec_ptr = rec_ptr + sizeof(*rec) + length;
2457*6a54128fSAndroid Build Coastguard Worker
2458*6a54128fSAndroid Build Coastguard Worker /* And left record is shortened */
2459*6a54128fSAndroid Build Coastguard Worker rec->rec_len = length;
2460*6a54128fSAndroid Build Coastguard Worker } else {
2461*6a54128fSAndroid Build Coastguard Worker newrec_ptr = 0;
2462*6a54128fSAndroid Build Coastguard Worker }
2463*6a54128fSAndroid Build Coastguard Worker
2464*6a54128fSAndroid Build Coastguard Worker /* Remove allocated record from the free list */
2465*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_write(tdb, last_ptr, &rec->next) == -1) {
2466*6a54128fSAndroid Build Coastguard Worker return 0;
2467*6a54128fSAndroid Build Coastguard Worker }
2468*6a54128fSAndroid Build Coastguard Worker
2469*6a54128fSAndroid Build Coastguard Worker /* Update header: do this before we drop alloc
2470*6a54128fSAndroid Build Coastguard Worker lock, otherwise tdb_free() might try to
2471*6a54128fSAndroid Build Coastguard Worker merge with us, thinking we're free.
2472*6a54128fSAndroid Build Coastguard Worker (Thanks Jeremy Allison). */
2473*6a54128fSAndroid Build Coastguard Worker rec->magic = TDB_MAGIC;
2474*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_write(tdb, rec_ptr, rec) == -1) {
2475*6a54128fSAndroid Build Coastguard Worker return 0;
2476*6a54128fSAndroid Build Coastguard Worker }
2477*6a54128fSAndroid Build Coastguard Worker
2478*6a54128fSAndroid Build Coastguard Worker /* Did we create new block? */
2479*6a54128fSAndroid Build Coastguard Worker if (newrec_ptr) {
2480*6a54128fSAndroid Build Coastguard Worker /* Update allocated record tailer (we
2481*6a54128fSAndroid Build Coastguard Worker shortened it). */
2482*6a54128fSAndroid Build Coastguard Worker if (update_tailer(tdb, rec_ptr, rec) == -1) {
2483*6a54128fSAndroid Build Coastguard Worker return 0;
2484*6a54128fSAndroid Build Coastguard Worker }
2485*6a54128fSAndroid Build Coastguard Worker
2486*6a54128fSAndroid Build Coastguard Worker /* Free new record */
2487*6a54128fSAndroid Build Coastguard Worker if (tdb_free(tdb, newrec_ptr, &newrec) == -1) {
2488*6a54128fSAndroid Build Coastguard Worker return 0;
2489*6a54128fSAndroid Build Coastguard Worker }
2490*6a54128fSAndroid Build Coastguard Worker }
2491*6a54128fSAndroid Build Coastguard Worker
2492*6a54128fSAndroid Build Coastguard Worker /* all done - return the new record offset */
2493*6a54128fSAndroid Build Coastguard Worker return rec_ptr;
2494*6a54128fSAndroid Build Coastguard Worker }
2495*6a54128fSAndroid Build Coastguard Worker
2496*6a54128fSAndroid Build Coastguard Worker /* allocate some space from the free list. The offset returned points
2497*6a54128fSAndroid Build Coastguard Worker to a unconnected list_struct within the database with room for at
2498*6a54128fSAndroid Build Coastguard Worker least length bytes of total data
2499*6a54128fSAndroid Build Coastguard Worker
2500*6a54128fSAndroid Build Coastguard Worker 0 is returned if the space could not be allocated
2501*6a54128fSAndroid Build Coastguard Worker */
tdb_allocate(struct tdb_context * tdb,tdb_len_t length,struct list_struct * rec)2502*6a54128fSAndroid Build Coastguard Worker tdb_off_t tdb_allocate(struct tdb_context *tdb, tdb_len_t length, struct list_struct *rec)
2503*6a54128fSAndroid Build Coastguard Worker {
2504*6a54128fSAndroid Build Coastguard Worker tdb_off_t rec_ptr, last_ptr, newrec_ptr;
2505*6a54128fSAndroid Build Coastguard Worker struct {
2506*6a54128fSAndroid Build Coastguard Worker tdb_off_t rec_ptr, last_ptr;
2507*6a54128fSAndroid Build Coastguard Worker tdb_len_t rec_len;
2508*6a54128fSAndroid Build Coastguard Worker } bestfit;
2509*6a54128fSAndroid Build Coastguard Worker
2510*6a54128fSAndroid Build Coastguard Worker if (tdb_lock(tdb, -1, F_WRLCK) == -1)
2511*6a54128fSAndroid Build Coastguard Worker return 0;
2512*6a54128fSAndroid Build Coastguard Worker
2513*6a54128fSAndroid Build Coastguard Worker /* Extra bytes required for tailer */
2514*6a54128fSAndroid Build Coastguard Worker length += sizeof(tdb_off_t);
2515*6a54128fSAndroid Build Coastguard Worker
2516*6a54128fSAndroid Build Coastguard Worker again:
2517*6a54128fSAndroid Build Coastguard Worker last_ptr = FREELIST_TOP;
2518*6a54128fSAndroid Build Coastguard Worker
2519*6a54128fSAndroid Build Coastguard Worker /* read in the freelist top */
2520*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, FREELIST_TOP, &rec_ptr) == -1)
2521*6a54128fSAndroid Build Coastguard Worker goto fail;
2522*6a54128fSAndroid Build Coastguard Worker
2523*6a54128fSAndroid Build Coastguard Worker bestfit.rec_ptr = 0;
2524*6a54128fSAndroid Build Coastguard Worker bestfit.last_ptr = 0;
2525*6a54128fSAndroid Build Coastguard Worker bestfit.rec_len = 0;
2526*6a54128fSAndroid Build Coastguard Worker
2527*6a54128fSAndroid Build Coastguard Worker /*
2528*6a54128fSAndroid Build Coastguard Worker this is a best fit allocation strategy. Originally we used
2529*6a54128fSAndroid Build Coastguard Worker a first fit strategy, but it suffered from massive fragmentation
2530*6a54128fSAndroid Build Coastguard Worker issues when faced with a slowly increasing record size.
2531*6a54128fSAndroid Build Coastguard Worker */
2532*6a54128fSAndroid Build Coastguard Worker while (rec_ptr) {
2533*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_free_read(tdb, rec_ptr, rec) == -1) {
2534*6a54128fSAndroid Build Coastguard Worker goto fail;
2535*6a54128fSAndroid Build Coastguard Worker }
2536*6a54128fSAndroid Build Coastguard Worker
2537*6a54128fSAndroid Build Coastguard Worker if (rec->rec_len >= length) {
2538*6a54128fSAndroid Build Coastguard Worker if (bestfit.rec_ptr == 0 ||
2539*6a54128fSAndroid Build Coastguard Worker rec->rec_len < bestfit.rec_len) {
2540*6a54128fSAndroid Build Coastguard Worker bestfit.rec_len = rec->rec_len;
2541*6a54128fSAndroid Build Coastguard Worker bestfit.rec_ptr = rec_ptr;
2542*6a54128fSAndroid Build Coastguard Worker bestfit.last_ptr = last_ptr;
2543*6a54128fSAndroid Build Coastguard Worker /* consider a fit to be good enough if
2544*6a54128fSAndroid Build Coastguard Worker we aren't wasting more than half
2545*6a54128fSAndroid Build Coastguard Worker the space */
2546*6a54128fSAndroid Build Coastguard Worker if (bestfit.rec_len < 2*length) {
2547*6a54128fSAndroid Build Coastguard Worker break;
2548*6a54128fSAndroid Build Coastguard Worker }
2549*6a54128fSAndroid Build Coastguard Worker }
2550*6a54128fSAndroid Build Coastguard Worker }
2551*6a54128fSAndroid Build Coastguard Worker
2552*6a54128fSAndroid Build Coastguard Worker /* move to the next record */
2553*6a54128fSAndroid Build Coastguard Worker last_ptr = rec_ptr;
2554*6a54128fSAndroid Build Coastguard Worker rec_ptr = rec->next;
2555*6a54128fSAndroid Build Coastguard Worker }
2556*6a54128fSAndroid Build Coastguard Worker
2557*6a54128fSAndroid Build Coastguard Worker if (bestfit.rec_ptr != 0) {
2558*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_free_read(tdb, bestfit.rec_ptr, rec) == -1) {
2559*6a54128fSAndroid Build Coastguard Worker goto fail;
2560*6a54128fSAndroid Build Coastguard Worker }
2561*6a54128fSAndroid Build Coastguard Worker
2562*6a54128fSAndroid Build Coastguard Worker newrec_ptr = tdb_allocate_ofs(tdb, length, bestfit.rec_ptr, rec, bestfit.last_ptr);
2563*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, -1, F_WRLCK);
2564*6a54128fSAndroid Build Coastguard Worker return newrec_ptr;
2565*6a54128fSAndroid Build Coastguard Worker }
2566*6a54128fSAndroid Build Coastguard Worker
2567*6a54128fSAndroid Build Coastguard Worker /* we didn't find enough space. See if we can expand the
2568*6a54128fSAndroid Build Coastguard Worker database and if we can then try again */
2569*6a54128fSAndroid Build Coastguard Worker if (tdb_expand(tdb, length + sizeof(*rec)) == 0)
2570*6a54128fSAndroid Build Coastguard Worker goto again;
2571*6a54128fSAndroid Build Coastguard Worker fail:
2572*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, -1, F_WRLCK);
2573*6a54128fSAndroid Build Coastguard Worker return 0;
2574*6a54128fSAndroid Build Coastguard Worker }
2575*6a54128fSAndroid Build Coastguard Worker
2576*6a54128fSAndroid Build Coastguard Worker /* file: freelistcheck.c */
2577*6a54128fSAndroid Build Coastguard Worker
2578*6a54128fSAndroid Build Coastguard Worker /* Check the freelist is good and contains no loops.
2579*6a54128fSAndroid Build Coastguard Worker Very memory intensive - only do this as a consistency
2580*6a54128fSAndroid Build Coastguard Worker checker. Heh heh - uses an in memory tdb as the storage
2581*6a54128fSAndroid Build Coastguard Worker for the "seen" record list. For some reason this strikes
2582*6a54128fSAndroid Build Coastguard Worker me as extremely clever as I don't have to write another tree
2583*6a54128fSAndroid Build Coastguard Worker data structure implementation :-).
2584*6a54128fSAndroid Build Coastguard Worker */
2585*6a54128fSAndroid Build Coastguard Worker
seen_insert(struct tdb_context * mem_tdb,tdb_off_t rec_ptr)2586*6a54128fSAndroid Build Coastguard Worker static int seen_insert(struct tdb_context *mem_tdb, tdb_off_t rec_ptr)
2587*6a54128fSAndroid Build Coastguard Worker {
2588*6a54128fSAndroid Build Coastguard Worker TDB_DATA key, data;
2589*6a54128fSAndroid Build Coastguard Worker
2590*6a54128fSAndroid Build Coastguard Worker memset(&data, '\0', sizeof(data));
2591*6a54128fSAndroid Build Coastguard Worker key.dptr = (unsigned char *)&rec_ptr;
2592*6a54128fSAndroid Build Coastguard Worker key.dsize = sizeof(rec_ptr);
2593*6a54128fSAndroid Build Coastguard Worker return tdb_store(mem_tdb, key, data, TDB_INSERT);
2594*6a54128fSAndroid Build Coastguard Worker }
2595*6a54128fSAndroid Build Coastguard Worker
tdb_validate_freelist(struct tdb_context * tdb,int * pnum_entries)2596*6a54128fSAndroid Build Coastguard Worker int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries)
2597*6a54128fSAndroid Build Coastguard Worker {
2598*6a54128fSAndroid Build Coastguard Worker struct tdb_context *mem_tdb = NULL;
2599*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
2600*6a54128fSAndroid Build Coastguard Worker tdb_off_t rec_ptr, last_ptr;
2601*6a54128fSAndroid Build Coastguard Worker int ret = -1;
2602*6a54128fSAndroid Build Coastguard Worker
2603*6a54128fSAndroid Build Coastguard Worker *pnum_entries = 0;
2604*6a54128fSAndroid Build Coastguard Worker
2605*6a54128fSAndroid Build Coastguard Worker mem_tdb = tdb_open("flval", tdb->header.hash_size,
2606*6a54128fSAndroid Build Coastguard Worker TDB_INTERNAL, O_RDWR, 0600);
2607*6a54128fSAndroid Build Coastguard Worker if (!mem_tdb) {
2608*6a54128fSAndroid Build Coastguard Worker return -1;
2609*6a54128fSAndroid Build Coastguard Worker }
2610*6a54128fSAndroid Build Coastguard Worker
2611*6a54128fSAndroid Build Coastguard Worker if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
2612*6a54128fSAndroid Build Coastguard Worker tdb_close(mem_tdb);
2613*6a54128fSAndroid Build Coastguard Worker return 0;
2614*6a54128fSAndroid Build Coastguard Worker }
2615*6a54128fSAndroid Build Coastguard Worker
2616*6a54128fSAndroid Build Coastguard Worker last_ptr = FREELIST_TOP;
2617*6a54128fSAndroid Build Coastguard Worker
2618*6a54128fSAndroid Build Coastguard Worker /* Store the FREELIST_TOP record. */
2619*6a54128fSAndroid Build Coastguard Worker if (seen_insert(mem_tdb, last_ptr) == -1) {
2620*6a54128fSAndroid Build Coastguard Worker ret = TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
2621*6a54128fSAndroid Build Coastguard Worker goto fail;
2622*6a54128fSAndroid Build Coastguard Worker }
2623*6a54128fSAndroid Build Coastguard Worker
2624*6a54128fSAndroid Build Coastguard Worker /* read in the freelist top */
2625*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, FREELIST_TOP, &rec_ptr) == -1) {
2626*6a54128fSAndroid Build Coastguard Worker goto fail;
2627*6a54128fSAndroid Build Coastguard Worker }
2628*6a54128fSAndroid Build Coastguard Worker
2629*6a54128fSAndroid Build Coastguard Worker while (rec_ptr) {
2630*6a54128fSAndroid Build Coastguard Worker
2631*6a54128fSAndroid Build Coastguard Worker /* If we can't store this record (we've seen it
2632*6a54128fSAndroid Build Coastguard Worker before) then the free list has a loop and must
2633*6a54128fSAndroid Build Coastguard Worker be corrupt. */
2634*6a54128fSAndroid Build Coastguard Worker
2635*6a54128fSAndroid Build Coastguard Worker if (seen_insert(mem_tdb, rec_ptr)) {
2636*6a54128fSAndroid Build Coastguard Worker ret = TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
2637*6a54128fSAndroid Build Coastguard Worker goto fail;
2638*6a54128fSAndroid Build Coastguard Worker }
2639*6a54128fSAndroid Build Coastguard Worker
2640*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_free_read(tdb, rec_ptr, &rec) == -1) {
2641*6a54128fSAndroid Build Coastguard Worker goto fail;
2642*6a54128fSAndroid Build Coastguard Worker }
2643*6a54128fSAndroid Build Coastguard Worker
2644*6a54128fSAndroid Build Coastguard Worker /* move to the next record */
2645*6a54128fSAndroid Build Coastguard Worker last_ptr = rec_ptr;
2646*6a54128fSAndroid Build Coastguard Worker rec_ptr = rec.next;
2647*6a54128fSAndroid Build Coastguard Worker *pnum_entries += 1;
2648*6a54128fSAndroid Build Coastguard Worker }
2649*6a54128fSAndroid Build Coastguard Worker
2650*6a54128fSAndroid Build Coastguard Worker ret = 0;
2651*6a54128fSAndroid Build Coastguard Worker
2652*6a54128fSAndroid Build Coastguard Worker fail:
2653*6a54128fSAndroid Build Coastguard Worker
2654*6a54128fSAndroid Build Coastguard Worker tdb_close(mem_tdb);
2655*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, -1, F_WRLCK);
2656*6a54128fSAndroid Build Coastguard Worker return ret;
2657*6a54128fSAndroid Build Coastguard Worker }
2658*6a54128fSAndroid Build Coastguard Worker
2659*6a54128fSAndroid Build Coastguard Worker /* file: traverse.c */
2660*6a54128fSAndroid Build Coastguard Worker
2661*6a54128fSAndroid Build Coastguard Worker /* Uses traverse lock: 0 = finish, -1 = error, other = record offset */
tdb_next_lock(struct tdb_context * tdb,struct tdb_traverse_lock * tlock,struct list_struct * rec)2662*6a54128fSAndroid Build Coastguard Worker static int tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock *tlock,
2663*6a54128fSAndroid Build Coastguard Worker struct list_struct *rec)
2664*6a54128fSAndroid Build Coastguard Worker {
2665*6a54128fSAndroid Build Coastguard Worker int want_next = (tlock->off != 0);
2666*6a54128fSAndroid Build Coastguard Worker
2667*6a54128fSAndroid Build Coastguard Worker /* Lock each chain from the start one. */
2668*6a54128fSAndroid Build Coastguard Worker for (; tlock->hash < tdb->header.hash_size; tlock->hash++) {
2669*6a54128fSAndroid Build Coastguard Worker if (!tlock->off && tlock->hash != 0) {
2670*6a54128fSAndroid Build Coastguard Worker /* this is an optimisation for the common case where
2671*6a54128fSAndroid Build Coastguard Worker the hash chain is empty, which is particularly
2672*6a54128fSAndroid Build Coastguard Worker common for the use of tdb with ldb, where large
2673*6a54128fSAndroid Build Coastguard Worker hashes are used. In that case we spend most of our
2674*6a54128fSAndroid Build Coastguard Worker time in tdb_brlock(), locking empty hash chains.
2675*6a54128fSAndroid Build Coastguard Worker
2676*6a54128fSAndroid Build Coastguard Worker To avoid this, we do an unlocked pre-check to see
2677*6a54128fSAndroid Build Coastguard Worker if the hash chain is empty before starting to look
2678*6a54128fSAndroid Build Coastguard Worker inside it. If it is empty then we can avoid that
2679*6a54128fSAndroid Build Coastguard Worker hash chain. If it isn't empty then we can't believe
2680*6a54128fSAndroid Build Coastguard Worker the value we get back, as we read it without a
2681*6a54128fSAndroid Build Coastguard Worker lock, so instead we get the lock and re-fetch the
2682*6a54128fSAndroid Build Coastguard Worker value below.
2683*6a54128fSAndroid Build Coastguard Worker
2684*6a54128fSAndroid Build Coastguard Worker Notice that not doing this optimisation on the
2685*6a54128fSAndroid Build Coastguard Worker first hash chain is critical. We must guarantee
2686*6a54128fSAndroid Build Coastguard Worker that we have done at least one fcntl lock at the
2687*6a54128fSAndroid Build Coastguard Worker start of a search to guarantee that memory is
2688*6a54128fSAndroid Build Coastguard Worker coherent on SMP systems. If records are added by
2689*6a54128fSAndroid Build Coastguard Worker others during the search then that's OK, and we
2690*6a54128fSAndroid Build Coastguard Worker could possibly miss those with this trick, but we
2691*6a54128fSAndroid Build Coastguard Worker could miss them anyway without this trick, so the
2692*6a54128fSAndroid Build Coastguard Worker semantics don't change.
2693*6a54128fSAndroid Build Coastguard Worker
2694*6a54128fSAndroid Build Coastguard Worker With a non-indexed ldb search this trick gains us a
2695*6a54128fSAndroid Build Coastguard Worker factor of around 80 in speed on a linux 2.6.x
2696*6a54128fSAndroid Build Coastguard Worker system (testing using ldbtest).
2697*6a54128fSAndroid Build Coastguard Worker */
2698*6a54128fSAndroid Build Coastguard Worker tdb->methods->next_hash_chain(tdb, &tlock->hash);
2699*6a54128fSAndroid Build Coastguard Worker if (tlock->hash == tdb->header.hash_size) {
2700*6a54128fSAndroid Build Coastguard Worker continue;
2701*6a54128fSAndroid Build Coastguard Worker }
2702*6a54128fSAndroid Build Coastguard Worker }
2703*6a54128fSAndroid Build Coastguard Worker
2704*6a54128fSAndroid Build Coastguard Worker if (tdb_lock(tdb, tlock->hash, tlock->lock_rw) == -1)
2705*6a54128fSAndroid Build Coastguard Worker return -1;
2706*6a54128fSAndroid Build Coastguard Worker
2707*6a54128fSAndroid Build Coastguard Worker /* No previous record? Start at top of chain. */
2708*6a54128fSAndroid Build Coastguard Worker if (!tlock->off) {
2709*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, TDB_HASH_TOP(tlock->hash),
2710*6a54128fSAndroid Build Coastguard Worker &tlock->off) == -1)
2711*6a54128fSAndroid Build Coastguard Worker goto fail;
2712*6a54128fSAndroid Build Coastguard Worker } else {
2713*6a54128fSAndroid Build Coastguard Worker /* Otherwise unlock the previous record. */
2714*6a54128fSAndroid Build Coastguard Worker if (tdb_unlock_record(tdb, tlock->off) != 0)
2715*6a54128fSAndroid Build Coastguard Worker goto fail;
2716*6a54128fSAndroid Build Coastguard Worker }
2717*6a54128fSAndroid Build Coastguard Worker
2718*6a54128fSAndroid Build Coastguard Worker if (want_next) {
2719*6a54128fSAndroid Build Coastguard Worker /* We have offset of old record: grab next */
2720*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_read(tdb, tlock->off, rec) == -1)
2721*6a54128fSAndroid Build Coastguard Worker goto fail;
2722*6a54128fSAndroid Build Coastguard Worker tlock->off = rec->next;
2723*6a54128fSAndroid Build Coastguard Worker }
2724*6a54128fSAndroid Build Coastguard Worker
2725*6a54128fSAndroid Build Coastguard Worker /* Iterate through chain */
2726*6a54128fSAndroid Build Coastguard Worker while( tlock->off) {
2727*6a54128fSAndroid Build Coastguard Worker tdb_off_t current;
2728*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_read(tdb, tlock->off, rec) == -1)
2729*6a54128fSAndroid Build Coastguard Worker goto fail;
2730*6a54128fSAndroid Build Coastguard Worker
2731*6a54128fSAndroid Build Coastguard Worker /* Detect infinite loops. From "Shlomi Yaakobovich" <[email protected]>. */
2732*6a54128fSAndroid Build Coastguard Worker if (tlock->off == rec->next) {
2733*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_next_lock: loop detected.\n"));
2734*6a54128fSAndroid Build Coastguard Worker goto fail;
2735*6a54128fSAndroid Build Coastguard Worker }
2736*6a54128fSAndroid Build Coastguard Worker
2737*6a54128fSAndroid Build Coastguard Worker if (!TDB_DEAD(rec)) {
2738*6a54128fSAndroid Build Coastguard Worker /* Woohoo: we found one! */
2739*6a54128fSAndroid Build Coastguard Worker if (tdb_lock_record(tdb, tlock->off) != 0)
2740*6a54128fSAndroid Build Coastguard Worker goto fail;
2741*6a54128fSAndroid Build Coastguard Worker return tlock->off;
2742*6a54128fSAndroid Build Coastguard Worker }
2743*6a54128fSAndroid Build Coastguard Worker
2744*6a54128fSAndroid Build Coastguard Worker /* Try to clean dead ones from old traverses */
2745*6a54128fSAndroid Build Coastguard Worker current = tlock->off;
2746*6a54128fSAndroid Build Coastguard Worker tlock->off = rec->next;
2747*6a54128fSAndroid Build Coastguard Worker if (!(tdb->read_only || tdb->traverse_read) &&
2748*6a54128fSAndroid Build Coastguard Worker tdb_do_delete(tdb, current, rec) != 0)
2749*6a54128fSAndroid Build Coastguard Worker goto fail;
2750*6a54128fSAndroid Build Coastguard Worker }
2751*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, tlock->hash, tlock->lock_rw);
2752*6a54128fSAndroid Build Coastguard Worker want_next = 0;
2753*6a54128fSAndroid Build Coastguard Worker }
2754*6a54128fSAndroid Build Coastguard Worker /* We finished iteration without finding anything */
2755*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_SUCCESS, 0);
2756*6a54128fSAndroid Build Coastguard Worker
2757*6a54128fSAndroid Build Coastguard Worker fail:
2758*6a54128fSAndroid Build Coastguard Worker tlock->off = 0;
2759*6a54128fSAndroid Build Coastguard Worker if (tdb_unlock(tdb, tlock->hash, tlock->lock_rw) != 0)
2760*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_next_lock: On error unlock failed!\n"));
2761*6a54128fSAndroid Build Coastguard Worker return -1;
2762*6a54128fSAndroid Build Coastguard Worker }
2763*6a54128fSAndroid Build Coastguard Worker
2764*6a54128fSAndroid Build Coastguard Worker /* traverse the entire database - calling fn(tdb, key, data) on each element.
2765*6a54128fSAndroid Build Coastguard Worker return -1 on error or the record count traversed
2766*6a54128fSAndroid Build Coastguard Worker if fn is NULL then it is not called
2767*6a54128fSAndroid Build Coastguard Worker a non-zero return value from fn() indicates that the traversal should stop
2768*6a54128fSAndroid Build Coastguard Worker */
tdb_traverse_internal(struct tdb_context * tdb,tdb_traverse_func fn,void * private_data,struct tdb_traverse_lock * tl)2769*6a54128fSAndroid Build Coastguard Worker static int tdb_traverse_internal(struct tdb_context *tdb,
2770*6a54128fSAndroid Build Coastguard Worker tdb_traverse_func fn, void *private_data,
2771*6a54128fSAndroid Build Coastguard Worker struct tdb_traverse_lock *tl)
2772*6a54128fSAndroid Build Coastguard Worker {
2773*6a54128fSAndroid Build Coastguard Worker TDB_DATA key, dbuf;
2774*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
2775*6a54128fSAndroid Build Coastguard Worker int ret, count = 0;
2776*6a54128fSAndroid Build Coastguard Worker
2777*6a54128fSAndroid Build Coastguard Worker /* This was in the initialization, above, but the IRIX compiler
2778*6a54128fSAndroid Build Coastguard Worker * did not like it. crh
2779*6a54128fSAndroid Build Coastguard Worker */
2780*6a54128fSAndroid Build Coastguard Worker tl->next = tdb->travlocks.next;
2781*6a54128fSAndroid Build Coastguard Worker
2782*6a54128fSAndroid Build Coastguard Worker /* fcntl locks don't stack: beware traverse inside traverse */
2783*6a54128fSAndroid Build Coastguard Worker tdb->travlocks.next = tl;
2784*6a54128fSAndroid Build Coastguard Worker
2785*6a54128fSAndroid Build Coastguard Worker /* tdb_next_lock places locks on the record returned, and its chain */
2786*6a54128fSAndroid Build Coastguard Worker while ((ret = tdb_next_lock(tdb, tl, &rec)) > 0) {
2787*6a54128fSAndroid Build Coastguard Worker count++;
2788*6a54128fSAndroid Build Coastguard Worker /* now read the full record */
2789*6a54128fSAndroid Build Coastguard Worker key.dptr = tdb_alloc_read(tdb, tl->off + sizeof(rec),
2790*6a54128fSAndroid Build Coastguard Worker rec.key_len + rec.data_len);
2791*6a54128fSAndroid Build Coastguard Worker if (!key.dptr) {
2792*6a54128fSAndroid Build Coastguard Worker ret = -1;
2793*6a54128fSAndroid Build Coastguard Worker if (tdb_unlock(tdb, tl->hash, tl->lock_rw) != 0)
2794*6a54128fSAndroid Build Coastguard Worker goto out;
2795*6a54128fSAndroid Build Coastguard Worker if (tdb_unlock_record(tdb, tl->off) != 0)
2796*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_traverse: key.dptr == NULL and unlock_record failed!\n"));
2797*6a54128fSAndroid Build Coastguard Worker goto out;
2798*6a54128fSAndroid Build Coastguard Worker }
2799*6a54128fSAndroid Build Coastguard Worker key.dsize = rec.key_len;
2800*6a54128fSAndroid Build Coastguard Worker dbuf.dptr = key.dptr + rec.key_len;
2801*6a54128fSAndroid Build Coastguard Worker dbuf.dsize = rec.data_len;
2802*6a54128fSAndroid Build Coastguard Worker
2803*6a54128fSAndroid Build Coastguard Worker /* Drop chain lock, call out */
2804*6a54128fSAndroid Build Coastguard Worker if (tdb_unlock(tdb, tl->hash, tl->lock_rw) != 0) {
2805*6a54128fSAndroid Build Coastguard Worker ret = -1;
2806*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(key.dptr);
2807*6a54128fSAndroid Build Coastguard Worker goto out;
2808*6a54128fSAndroid Build Coastguard Worker }
2809*6a54128fSAndroid Build Coastguard Worker if (fn && fn(tdb, key, dbuf, private_data)) {
2810*6a54128fSAndroid Build Coastguard Worker /* They want us to terminate traversal */
2811*6a54128fSAndroid Build Coastguard Worker ret = count;
2812*6a54128fSAndroid Build Coastguard Worker if (tdb_unlock_record(tdb, tl->off) != 0) {
2813*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_traverse: unlock_record failed!\n"));;
2814*6a54128fSAndroid Build Coastguard Worker ret = -1;
2815*6a54128fSAndroid Build Coastguard Worker }
2816*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(key.dptr);
2817*6a54128fSAndroid Build Coastguard Worker goto out;
2818*6a54128fSAndroid Build Coastguard Worker }
2819*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(key.dptr);
2820*6a54128fSAndroid Build Coastguard Worker }
2821*6a54128fSAndroid Build Coastguard Worker out:
2822*6a54128fSAndroid Build Coastguard Worker tdb->travlocks.next = tl->next;
2823*6a54128fSAndroid Build Coastguard Worker if (ret < 0)
2824*6a54128fSAndroid Build Coastguard Worker return -1;
2825*6a54128fSAndroid Build Coastguard Worker else
2826*6a54128fSAndroid Build Coastguard Worker return count;
2827*6a54128fSAndroid Build Coastguard Worker }
2828*6a54128fSAndroid Build Coastguard Worker
2829*6a54128fSAndroid Build Coastguard Worker
2830*6a54128fSAndroid Build Coastguard Worker /*
2831*6a54128fSAndroid Build Coastguard Worker a write style traverse - temporarily marks the db read only
2832*6a54128fSAndroid Build Coastguard Worker */
tdb_traverse_read(struct tdb_context * tdb,tdb_traverse_func fn,void * private_data)2833*6a54128fSAndroid Build Coastguard Worker int tdb_traverse_read(struct tdb_context *tdb,
2834*6a54128fSAndroid Build Coastguard Worker tdb_traverse_func fn, void *private_data)
2835*6a54128fSAndroid Build Coastguard Worker {
2836*6a54128fSAndroid Build Coastguard Worker struct tdb_traverse_lock tl = { NULL, 0, 0, F_RDLCK };
2837*6a54128fSAndroid Build Coastguard Worker int ret;
2838*6a54128fSAndroid Build Coastguard Worker
2839*6a54128fSAndroid Build Coastguard Worker /* we need to get a read lock on the transaction lock here to
2840*6a54128fSAndroid Build Coastguard Worker cope with the lock ordering semantics of solaris10 */
2841*6a54128fSAndroid Build Coastguard Worker if (tdb_transaction_lock(tdb, F_RDLCK)) {
2842*6a54128fSAndroid Build Coastguard Worker return -1;
2843*6a54128fSAndroid Build Coastguard Worker }
2844*6a54128fSAndroid Build Coastguard Worker
2845*6a54128fSAndroid Build Coastguard Worker tdb->traverse_read++;
2846*6a54128fSAndroid Build Coastguard Worker ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
2847*6a54128fSAndroid Build Coastguard Worker tdb->traverse_read--;
2848*6a54128fSAndroid Build Coastguard Worker
2849*6a54128fSAndroid Build Coastguard Worker tdb_transaction_unlock(tdb);
2850*6a54128fSAndroid Build Coastguard Worker
2851*6a54128fSAndroid Build Coastguard Worker return ret;
2852*6a54128fSAndroid Build Coastguard Worker }
2853*6a54128fSAndroid Build Coastguard Worker
2854*6a54128fSAndroid Build Coastguard Worker /*
2855*6a54128fSAndroid Build Coastguard Worker a write style traverse - needs to get the transaction lock to
2856*6a54128fSAndroid Build Coastguard Worker prevent deadlocks
2857*6a54128fSAndroid Build Coastguard Worker */
tdb_traverse(struct tdb_context * tdb,tdb_traverse_func fn,void * private_data)2858*6a54128fSAndroid Build Coastguard Worker int tdb_traverse(struct tdb_context *tdb,
2859*6a54128fSAndroid Build Coastguard Worker tdb_traverse_func fn, void *private_data)
2860*6a54128fSAndroid Build Coastguard Worker {
2861*6a54128fSAndroid Build Coastguard Worker struct tdb_traverse_lock tl = { NULL, 0, 0, F_WRLCK };
2862*6a54128fSAndroid Build Coastguard Worker int ret;
2863*6a54128fSAndroid Build Coastguard Worker
2864*6a54128fSAndroid Build Coastguard Worker if (tdb->read_only || tdb->traverse_read) {
2865*6a54128fSAndroid Build Coastguard Worker return tdb_traverse_read(tdb, fn, private_data);
2866*6a54128fSAndroid Build Coastguard Worker }
2867*6a54128fSAndroid Build Coastguard Worker
2868*6a54128fSAndroid Build Coastguard Worker if (tdb_transaction_lock(tdb, F_WRLCK)) {
2869*6a54128fSAndroid Build Coastguard Worker return -1;
2870*6a54128fSAndroid Build Coastguard Worker }
2871*6a54128fSAndroid Build Coastguard Worker
2872*6a54128fSAndroid Build Coastguard Worker ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
2873*6a54128fSAndroid Build Coastguard Worker
2874*6a54128fSAndroid Build Coastguard Worker tdb_transaction_unlock(tdb);
2875*6a54128fSAndroid Build Coastguard Worker
2876*6a54128fSAndroid Build Coastguard Worker return ret;
2877*6a54128fSAndroid Build Coastguard Worker }
2878*6a54128fSAndroid Build Coastguard Worker
2879*6a54128fSAndroid Build Coastguard Worker
2880*6a54128fSAndroid Build Coastguard Worker /* find the first entry in the database and return its key */
tdb_firstkey(struct tdb_context * tdb)2881*6a54128fSAndroid Build Coastguard Worker TDB_DATA tdb_firstkey(struct tdb_context *tdb)
2882*6a54128fSAndroid Build Coastguard Worker {
2883*6a54128fSAndroid Build Coastguard Worker TDB_DATA key;
2884*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
2885*6a54128fSAndroid Build Coastguard Worker
2886*6a54128fSAndroid Build Coastguard Worker /* release any old lock */
2887*6a54128fSAndroid Build Coastguard Worker if (tdb_unlock_record(tdb, tdb->travlocks.off) != 0)
2888*6a54128fSAndroid Build Coastguard Worker return tdb_null;
2889*6a54128fSAndroid Build Coastguard Worker tdb->travlocks.off = tdb->travlocks.hash = 0;
2890*6a54128fSAndroid Build Coastguard Worker tdb->travlocks.lock_rw = F_RDLCK;
2891*6a54128fSAndroid Build Coastguard Worker
2892*6a54128fSAndroid Build Coastguard Worker /* Grab first record: locks chain and returned record. */
2893*6a54128fSAndroid Build Coastguard Worker if (tdb_next_lock(tdb, &tdb->travlocks, &rec) <= 0)
2894*6a54128fSAndroid Build Coastguard Worker return tdb_null;
2895*6a54128fSAndroid Build Coastguard Worker /* now read the key */
2896*6a54128fSAndroid Build Coastguard Worker key.dsize = rec.key_len;
2897*6a54128fSAndroid Build Coastguard Worker key.dptr =tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),key.dsize);
2898*6a54128fSAndroid Build Coastguard Worker
2899*6a54128fSAndroid Build Coastguard Worker /* Unlock the hash chain of the record we just read. */
2900*6a54128fSAndroid Build Coastguard Worker if (tdb_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0)
2901*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_firstkey: error occurred while tdb_unlocking!\n"));
2902*6a54128fSAndroid Build Coastguard Worker return key;
2903*6a54128fSAndroid Build Coastguard Worker }
2904*6a54128fSAndroid Build Coastguard Worker
2905*6a54128fSAndroid Build Coastguard Worker /* find the next entry in the database, returning its key */
tdb_nextkey(struct tdb_context * tdb,TDB_DATA oldkey)2906*6a54128fSAndroid Build Coastguard Worker TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA oldkey)
2907*6a54128fSAndroid Build Coastguard Worker {
2908*6a54128fSAndroid Build Coastguard Worker u32 oldhash;
2909*6a54128fSAndroid Build Coastguard Worker TDB_DATA key = tdb_null;
2910*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
2911*6a54128fSAndroid Build Coastguard Worker unsigned char *k = NULL;
2912*6a54128fSAndroid Build Coastguard Worker
2913*6a54128fSAndroid Build Coastguard Worker /* Is locked key the old key? If so, traverse will be reliable. */
2914*6a54128fSAndroid Build Coastguard Worker if (tdb->travlocks.off) {
2915*6a54128fSAndroid Build Coastguard Worker if (tdb_lock(tdb,tdb->travlocks.hash,tdb->travlocks.lock_rw))
2916*6a54128fSAndroid Build Coastguard Worker return tdb_null;
2917*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_read(tdb, tdb->travlocks.off, &rec) == -1
2918*6a54128fSAndroid Build Coastguard Worker || !(k = tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),
2919*6a54128fSAndroid Build Coastguard Worker rec.key_len))
2920*6a54128fSAndroid Build Coastguard Worker || memcmp(k, oldkey.dptr, oldkey.dsize) != 0) {
2921*6a54128fSAndroid Build Coastguard Worker /* No, it wasn't: unlock it and start from scratch */
2922*6a54128fSAndroid Build Coastguard Worker if (tdb_unlock_record(tdb, tdb->travlocks.off) != 0) {
2923*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(k);
2924*6a54128fSAndroid Build Coastguard Worker return tdb_null;
2925*6a54128fSAndroid Build Coastguard Worker }
2926*6a54128fSAndroid Build Coastguard Worker if (tdb_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0) {
2927*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(k);
2928*6a54128fSAndroid Build Coastguard Worker return tdb_null;
2929*6a54128fSAndroid Build Coastguard Worker }
2930*6a54128fSAndroid Build Coastguard Worker tdb->travlocks.off = 0;
2931*6a54128fSAndroid Build Coastguard Worker }
2932*6a54128fSAndroid Build Coastguard Worker
2933*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(k);
2934*6a54128fSAndroid Build Coastguard Worker }
2935*6a54128fSAndroid Build Coastguard Worker
2936*6a54128fSAndroid Build Coastguard Worker if (!tdb->travlocks.off) {
2937*6a54128fSAndroid Build Coastguard Worker /* No previous element: do normal find, and lock record */
2938*6a54128fSAndroid Build Coastguard Worker tdb->travlocks.off = tdb_find_lock_hash(tdb, oldkey, tdb->hash_fn(&oldkey), tdb->travlocks.lock_rw, &rec);
2939*6a54128fSAndroid Build Coastguard Worker if (!tdb->travlocks.off)
2940*6a54128fSAndroid Build Coastguard Worker return tdb_null;
2941*6a54128fSAndroid Build Coastguard Worker tdb->travlocks.hash = BUCKET(rec.full_hash);
2942*6a54128fSAndroid Build Coastguard Worker if (tdb_lock_record(tdb, tdb->travlocks.off) != 0) {
2943*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: lock_record failed (%s)!\n", strerror(errno)));
2944*6a54128fSAndroid Build Coastguard Worker return tdb_null;
2945*6a54128fSAndroid Build Coastguard Worker }
2946*6a54128fSAndroid Build Coastguard Worker }
2947*6a54128fSAndroid Build Coastguard Worker oldhash = tdb->travlocks.hash;
2948*6a54128fSAndroid Build Coastguard Worker
2949*6a54128fSAndroid Build Coastguard Worker /* Grab next record: locks chain and returned record,
2950*6a54128fSAndroid Build Coastguard Worker unlocks old record */
2951*6a54128fSAndroid Build Coastguard Worker if (tdb_next_lock(tdb, &tdb->travlocks, &rec) > 0) {
2952*6a54128fSAndroid Build Coastguard Worker key.dsize = rec.key_len;
2953*6a54128fSAndroid Build Coastguard Worker key.dptr = tdb_alloc_read(tdb, tdb->travlocks.off+sizeof(rec),
2954*6a54128fSAndroid Build Coastguard Worker key.dsize);
2955*6a54128fSAndroid Build Coastguard Worker /* Unlock the chain of this new record */
2956*6a54128fSAndroid Build Coastguard Worker if (tdb_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0)
2957*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
2958*6a54128fSAndroid Build Coastguard Worker }
2959*6a54128fSAndroid Build Coastguard Worker /* Unlock the chain of old record */
2960*6a54128fSAndroid Build Coastguard Worker if (tdb_unlock(tdb, BUCKET(oldhash), tdb->travlocks.lock_rw) != 0)
2961*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
2962*6a54128fSAndroid Build Coastguard Worker return key;
2963*6a54128fSAndroid Build Coastguard Worker }
2964*6a54128fSAndroid Build Coastguard Worker
2965*6a54128fSAndroid Build Coastguard Worker /* file: dump.c */
2966*6a54128fSAndroid Build Coastguard Worker
tdb_dump_record(struct tdb_context * tdb,int hash,tdb_off_t offset)2967*6a54128fSAndroid Build Coastguard Worker static tdb_off_t tdb_dump_record(struct tdb_context *tdb, int hash,
2968*6a54128fSAndroid Build Coastguard Worker tdb_off_t offset)
2969*6a54128fSAndroid Build Coastguard Worker {
2970*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
2971*6a54128fSAndroid Build Coastguard Worker tdb_off_t tailer_ofs, tailer;
2972*6a54128fSAndroid Build Coastguard Worker
2973*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_read(tdb, offset, (char *)&rec,
2974*6a54128fSAndroid Build Coastguard Worker sizeof(rec), DOCONV()) == -1) {
2975*6a54128fSAndroid Build Coastguard Worker printf("ERROR: failed to read record at %u\n", offset);
2976*6a54128fSAndroid Build Coastguard Worker return 0;
2977*6a54128fSAndroid Build Coastguard Worker }
2978*6a54128fSAndroid Build Coastguard Worker
2979*6a54128fSAndroid Build Coastguard Worker printf(" rec: hash=%d offset=0x%08x next=0x%08x rec_len=%d "
2980*6a54128fSAndroid Build Coastguard Worker "key_len=%d data_len=%d full_hash=0x%x magic=0x%x\n",
2981*6a54128fSAndroid Build Coastguard Worker hash, offset, rec.next, rec.rec_len, rec.key_len, rec.data_len,
2982*6a54128fSAndroid Build Coastguard Worker rec.full_hash, rec.magic);
2983*6a54128fSAndroid Build Coastguard Worker
2984*6a54128fSAndroid Build Coastguard Worker tailer_ofs = offset + sizeof(rec) + rec.rec_len - sizeof(tdb_off_t);
2985*6a54128fSAndroid Build Coastguard Worker
2986*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, tailer_ofs, &tailer) == -1) {
2987*6a54128fSAndroid Build Coastguard Worker printf("ERROR: failed to read tailer at %u\n", tailer_ofs);
2988*6a54128fSAndroid Build Coastguard Worker return rec.next;
2989*6a54128fSAndroid Build Coastguard Worker }
2990*6a54128fSAndroid Build Coastguard Worker
2991*6a54128fSAndroid Build Coastguard Worker if (tailer != rec.rec_len + sizeof(rec)) {
2992*6a54128fSAndroid Build Coastguard Worker printf("ERROR: tailer does not match record! tailer=%u totalsize=%u\n",
2993*6a54128fSAndroid Build Coastguard Worker (unsigned int)tailer, (unsigned int)(rec.rec_len + sizeof(rec)));
2994*6a54128fSAndroid Build Coastguard Worker }
2995*6a54128fSAndroid Build Coastguard Worker return rec.next;
2996*6a54128fSAndroid Build Coastguard Worker }
2997*6a54128fSAndroid Build Coastguard Worker
tdb_dump_chain(struct tdb_context * tdb,int i)2998*6a54128fSAndroid Build Coastguard Worker static int tdb_dump_chain(struct tdb_context *tdb, int i)
2999*6a54128fSAndroid Build Coastguard Worker {
3000*6a54128fSAndroid Build Coastguard Worker tdb_off_t rec_ptr, top;
3001*6a54128fSAndroid Build Coastguard Worker
3002*6a54128fSAndroid Build Coastguard Worker top = TDB_HASH_TOP(i);
3003*6a54128fSAndroid Build Coastguard Worker
3004*6a54128fSAndroid Build Coastguard Worker if (tdb_lock(tdb, i, F_WRLCK) != 0)
3005*6a54128fSAndroid Build Coastguard Worker return -1;
3006*6a54128fSAndroid Build Coastguard Worker
3007*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, top, &rec_ptr) == -1)
3008*6a54128fSAndroid Build Coastguard Worker return tdb_unlock(tdb, i, F_WRLCK);
3009*6a54128fSAndroid Build Coastguard Worker
3010*6a54128fSAndroid Build Coastguard Worker if (rec_ptr)
3011*6a54128fSAndroid Build Coastguard Worker printf("hash=%d\n", i);
3012*6a54128fSAndroid Build Coastguard Worker
3013*6a54128fSAndroid Build Coastguard Worker while (rec_ptr) {
3014*6a54128fSAndroid Build Coastguard Worker rec_ptr = tdb_dump_record(tdb, i, rec_ptr);
3015*6a54128fSAndroid Build Coastguard Worker }
3016*6a54128fSAndroid Build Coastguard Worker
3017*6a54128fSAndroid Build Coastguard Worker return tdb_unlock(tdb, i, F_WRLCK);
3018*6a54128fSAndroid Build Coastguard Worker }
3019*6a54128fSAndroid Build Coastguard Worker
tdb_dump_all(struct tdb_context * tdb)3020*6a54128fSAndroid Build Coastguard Worker void tdb_dump_all(struct tdb_context *tdb)
3021*6a54128fSAndroid Build Coastguard Worker {
3022*6a54128fSAndroid Build Coastguard Worker int i;
3023*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < (int)tdb->header.hash_size; i++) {
3024*6a54128fSAndroid Build Coastguard Worker tdb_dump_chain(tdb, i);
3025*6a54128fSAndroid Build Coastguard Worker }
3026*6a54128fSAndroid Build Coastguard Worker printf("freelist:\n");
3027*6a54128fSAndroid Build Coastguard Worker tdb_dump_chain(tdb, -1);
3028*6a54128fSAndroid Build Coastguard Worker }
3029*6a54128fSAndroid Build Coastguard Worker
tdb_printfreelist(struct tdb_context * tdb)3030*6a54128fSAndroid Build Coastguard Worker int tdb_printfreelist(struct tdb_context *tdb)
3031*6a54128fSAndroid Build Coastguard Worker {
3032*6a54128fSAndroid Build Coastguard Worker int ret;
3033*6a54128fSAndroid Build Coastguard Worker long total_free = 0;
3034*6a54128fSAndroid Build Coastguard Worker tdb_off_t offset, rec_ptr;
3035*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
3036*6a54128fSAndroid Build Coastguard Worker
3037*6a54128fSAndroid Build Coastguard Worker if ((ret = tdb_lock(tdb, -1, F_WRLCK)) != 0)
3038*6a54128fSAndroid Build Coastguard Worker return ret;
3039*6a54128fSAndroid Build Coastguard Worker
3040*6a54128fSAndroid Build Coastguard Worker offset = FREELIST_TOP;
3041*6a54128fSAndroid Build Coastguard Worker
3042*6a54128fSAndroid Build Coastguard Worker /* read in the freelist top */
3043*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, offset, &rec_ptr) == -1) {
3044*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, -1, F_WRLCK);
3045*6a54128fSAndroid Build Coastguard Worker return 0;
3046*6a54128fSAndroid Build Coastguard Worker }
3047*6a54128fSAndroid Build Coastguard Worker
3048*6a54128fSAndroid Build Coastguard Worker printf("freelist top=[0x%08x]\n", rec_ptr );
3049*6a54128fSAndroid Build Coastguard Worker while (rec_ptr) {
3050*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_read(tdb, rec_ptr, (char *)&rec,
3051*6a54128fSAndroid Build Coastguard Worker sizeof(rec), DOCONV()) == -1) {
3052*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, -1, F_WRLCK);
3053*6a54128fSAndroid Build Coastguard Worker return -1;
3054*6a54128fSAndroid Build Coastguard Worker }
3055*6a54128fSAndroid Build Coastguard Worker
3056*6a54128fSAndroid Build Coastguard Worker if (rec.magic != TDB_FREE_MAGIC) {
3057*6a54128fSAndroid Build Coastguard Worker printf("bad magic 0x%08x in free list\n", rec.magic);
3058*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, -1, F_WRLCK);
3059*6a54128fSAndroid Build Coastguard Worker return -1;
3060*6a54128fSAndroid Build Coastguard Worker }
3061*6a54128fSAndroid Build Coastguard Worker
3062*6a54128fSAndroid Build Coastguard Worker printf("entry offset=[0x%08x], rec.rec_len = [0x%08x (%d)] (end = 0x%08x)\n",
3063*6a54128fSAndroid Build Coastguard Worker rec_ptr, rec.rec_len, rec.rec_len, rec_ptr + rec.rec_len);
3064*6a54128fSAndroid Build Coastguard Worker total_free += rec.rec_len;
3065*6a54128fSAndroid Build Coastguard Worker
3066*6a54128fSAndroid Build Coastguard Worker /* move to the next record */
3067*6a54128fSAndroid Build Coastguard Worker rec_ptr = rec.next;
3068*6a54128fSAndroid Build Coastguard Worker }
3069*6a54128fSAndroid Build Coastguard Worker printf("total rec_len = [0x%08x (%d)]\n", (int)total_free,
3070*6a54128fSAndroid Build Coastguard Worker (int)total_free);
3071*6a54128fSAndroid Build Coastguard Worker
3072*6a54128fSAndroid Build Coastguard Worker return tdb_unlock(tdb, -1, F_WRLCK);
3073*6a54128fSAndroid Build Coastguard Worker }
3074*6a54128fSAndroid Build Coastguard Worker
3075*6a54128fSAndroid Build Coastguard Worker /* file: tdb.c */
3076*6a54128fSAndroid Build Coastguard Worker
3077*6a54128fSAndroid Build Coastguard Worker /*
3078*6a54128fSAndroid Build Coastguard Worker non-blocking increment of the tdb sequence number if the tdb has been opened using
3079*6a54128fSAndroid Build Coastguard Worker the TDB_SEQNUM flag
3080*6a54128fSAndroid Build Coastguard Worker */
tdb_increment_seqnum_nonblock(struct tdb_context * tdb)3081*6a54128fSAndroid Build Coastguard Worker void tdb_increment_seqnum_nonblock(struct tdb_context *tdb)
3082*6a54128fSAndroid Build Coastguard Worker {
3083*6a54128fSAndroid Build Coastguard Worker tdb_off_t seqnum=0;
3084*6a54128fSAndroid Build Coastguard Worker
3085*6a54128fSAndroid Build Coastguard Worker if (!(tdb->flags & TDB_SEQNUM)) {
3086*6a54128fSAndroid Build Coastguard Worker return;
3087*6a54128fSAndroid Build Coastguard Worker }
3088*6a54128fSAndroid Build Coastguard Worker
3089*6a54128fSAndroid Build Coastguard Worker /* we ignore errors from this, as we have no sane way of
3090*6a54128fSAndroid Build Coastguard Worker dealing with them.
3091*6a54128fSAndroid Build Coastguard Worker */
3092*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum) == -1)
3093*6a54128fSAndroid Build Coastguard Worker return;
3094*6a54128fSAndroid Build Coastguard Worker seqnum++;
3095*6a54128fSAndroid Build Coastguard Worker (void) tdb_ofs_write(tdb, TDB_SEQNUM_OFS, &seqnum);
3096*6a54128fSAndroid Build Coastguard Worker }
3097*6a54128fSAndroid Build Coastguard Worker
3098*6a54128fSAndroid Build Coastguard Worker /*
3099*6a54128fSAndroid Build Coastguard Worker increment the tdb sequence number if the tdb has been opened using
3100*6a54128fSAndroid Build Coastguard Worker the TDB_SEQNUM flag
3101*6a54128fSAndroid Build Coastguard Worker */
tdb_increment_seqnum(struct tdb_context * tdb)3102*6a54128fSAndroid Build Coastguard Worker static void tdb_increment_seqnum(struct tdb_context *tdb)
3103*6a54128fSAndroid Build Coastguard Worker {
3104*6a54128fSAndroid Build Coastguard Worker if (!(tdb->flags & TDB_SEQNUM)) {
3105*6a54128fSAndroid Build Coastguard Worker return;
3106*6a54128fSAndroid Build Coastguard Worker }
3107*6a54128fSAndroid Build Coastguard Worker
3108*6a54128fSAndroid Build Coastguard Worker if (tdb_brlock(tdb, TDB_SEQNUM_OFS, F_WRLCK, F_SETLKW, 1, 1) != 0) {
3109*6a54128fSAndroid Build Coastguard Worker return;
3110*6a54128fSAndroid Build Coastguard Worker }
3111*6a54128fSAndroid Build Coastguard Worker
3112*6a54128fSAndroid Build Coastguard Worker tdb_increment_seqnum_nonblock(tdb);
3113*6a54128fSAndroid Build Coastguard Worker
3114*6a54128fSAndroid Build Coastguard Worker tdb_brlock(tdb, TDB_SEQNUM_OFS, F_UNLCK, F_SETLKW, 1, 1);
3115*6a54128fSAndroid Build Coastguard Worker }
3116*6a54128fSAndroid Build Coastguard Worker
tdb_key_compare(TDB_DATA key,TDB_DATA data,void * private_data EXT2FS_ATTR ((unused)))3117*6a54128fSAndroid Build Coastguard Worker static int tdb_key_compare(TDB_DATA key, TDB_DATA data,
3118*6a54128fSAndroid Build Coastguard Worker void *private_data EXT2FS_ATTR((unused)))
3119*6a54128fSAndroid Build Coastguard Worker {
3120*6a54128fSAndroid Build Coastguard Worker return memcmp(data.dptr, key.dptr, data.dsize);
3121*6a54128fSAndroid Build Coastguard Worker }
3122*6a54128fSAndroid Build Coastguard Worker
3123*6a54128fSAndroid Build Coastguard Worker /* Returns 0 on fail. On success, return offset of record, and fills
3124*6a54128fSAndroid Build Coastguard Worker in rec */
tdb_find(struct tdb_context * tdb,TDB_DATA key,u32 hash,struct list_struct * r)3125*6a54128fSAndroid Build Coastguard Worker static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, u32 hash,
3126*6a54128fSAndroid Build Coastguard Worker struct list_struct *r)
3127*6a54128fSAndroid Build Coastguard Worker {
3128*6a54128fSAndroid Build Coastguard Worker tdb_off_t rec_ptr;
3129*6a54128fSAndroid Build Coastguard Worker
3130*6a54128fSAndroid Build Coastguard Worker /* read in the hash top */
3131*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
3132*6a54128fSAndroid Build Coastguard Worker return 0;
3133*6a54128fSAndroid Build Coastguard Worker
3134*6a54128fSAndroid Build Coastguard Worker /* keep looking until we find the right record */
3135*6a54128fSAndroid Build Coastguard Worker while (rec_ptr) {
3136*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_read(tdb, rec_ptr, r) == -1)
3137*6a54128fSAndroid Build Coastguard Worker return 0;
3138*6a54128fSAndroid Build Coastguard Worker
3139*6a54128fSAndroid Build Coastguard Worker if (!TDB_DEAD(r) && hash==r->full_hash
3140*6a54128fSAndroid Build Coastguard Worker && key.dsize==r->key_len
3141*6a54128fSAndroid Build Coastguard Worker && tdb_parse_data(tdb, key, rec_ptr + sizeof(*r),
3142*6a54128fSAndroid Build Coastguard Worker r->key_len, tdb_key_compare,
3143*6a54128fSAndroid Build Coastguard Worker NULL) == 0) {
3144*6a54128fSAndroid Build Coastguard Worker return rec_ptr;
3145*6a54128fSAndroid Build Coastguard Worker }
3146*6a54128fSAndroid Build Coastguard Worker rec_ptr = r->next;
3147*6a54128fSAndroid Build Coastguard Worker }
3148*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
3149*6a54128fSAndroid Build Coastguard Worker }
3150*6a54128fSAndroid Build Coastguard Worker
3151*6a54128fSAndroid Build Coastguard Worker /* As tdb_find, but if you succeed, keep the lock */
tdb_find_lock_hash(struct tdb_context * tdb,TDB_DATA key,u32 hash,int locktype,struct list_struct * rec)3152*6a54128fSAndroid Build Coastguard Worker tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, u32 hash, int locktype,
3153*6a54128fSAndroid Build Coastguard Worker struct list_struct *rec)
3154*6a54128fSAndroid Build Coastguard Worker {
3155*6a54128fSAndroid Build Coastguard Worker u32 rec_ptr;
3156*6a54128fSAndroid Build Coastguard Worker
3157*6a54128fSAndroid Build Coastguard Worker if (tdb_lock(tdb, BUCKET(hash), locktype) == -1)
3158*6a54128fSAndroid Build Coastguard Worker return 0;
3159*6a54128fSAndroid Build Coastguard Worker if (!(rec_ptr = tdb_find(tdb, key, hash, rec)))
3160*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, BUCKET(hash), locktype);
3161*6a54128fSAndroid Build Coastguard Worker return rec_ptr;
3162*6a54128fSAndroid Build Coastguard Worker }
3163*6a54128fSAndroid Build Coastguard Worker
3164*6a54128fSAndroid Build Coastguard Worker
3165*6a54128fSAndroid Build Coastguard Worker /* update an entry in place - this only works if the new data size
3166*6a54128fSAndroid Build Coastguard Worker is <= the old data size and the key exists.
3167*6a54128fSAndroid Build Coastguard Worker on failure return -1.
3168*6a54128fSAndroid Build Coastguard Worker */
tdb_update_hash(struct tdb_context * tdb,TDB_DATA key,u32 hash,TDB_DATA dbuf)3169*6a54128fSAndroid Build Coastguard Worker static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, u32 hash, TDB_DATA dbuf)
3170*6a54128fSAndroid Build Coastguard Worker {
3171*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
3172*6a54128fSAndroid Build Coastguard Worker tdb_off_t rec_ptr;
3173*6a54128fSAndroid Build Coastguard Worker
3174*6a54128fSAndroid Build Coastguard Worker /* find entry */
3175*6a54128fSAndroid Build Coastguard Worker if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
3176*6a54128fSAndroid Build Coastguard Worker return -1;
3177*6a54128fSAndroid Build Coastguard Worker
3178*6a54128fSAndroid Build Coastguard Worker /* must be long enough key, data and tailer */
3179*6a54128fSAndroid Build Coastguard Worker if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off_t)) {
3180*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_SUCCESS; /* Not really an error */
3181*6a54128fSAndroid Build Coastguard Worker return -1;
3182*6a54128fSAndroid Build Coastguard Worker }
3183*6a54128fSAndroid Build Coastguard Worker
3184*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
3185*6a54128fSAndroid Build Coastguard Worker dbuf.dptr, dbuf.dsize) == -1)
3186*6a54128fSAndroid Build Coastguard Worker return -1;
3187*6a54128fSAndroid Build Coastguard Worker
3188*6a54128fSAndroid Build Coastguard Worker if (dbuf.dsize != rec.data_len) {
3189*6a54128fSAndroid Build Coastguard Worker /* update size */
3190*6a54128fSAndroid Build Coastguard Worker rec.data_len = dbuf.dsize;
3191*6a54128fSAndroid Build Coastguard Worker return tdb_rec_write(tdb, rec_ptr, &rec);
3192*6a54128fSAndroid Build Coastguard Worker }
3193*6a54128fSAndroid Build Coastguard Worker
3194*6a54128fSAndroid Build Coastguard Worker return 0;
3195*6a54128fSAndroid Build Coastguard Worker }
3196*6a54128fSAndroid Build Coastguard Worker
3197*6a54128fSAndroid Build Coastguard Worker /* find an entry in the database given a key */
3198*6a54128fSAndroid Build Coastguard Worker /* If an entry doesn't exist tdb_err will be set to
3199*6a54128fSAndroid Build Coastguard Worker * TDB_ERR_NOEXIST. If a key has no data attached
3200*6a54128fSAndroid Build Coastguard Worker * then the TDB_DATA will have zero length but
3201*6a54128fSAndroid Build Coastguard Worker * a non-zero pointer
3202*6a54128fSAndroid Build Coastguard Worker */
tdb_fetch(struct tdb_context * tdb,TDB_DATA key)3203*6a54128fSAndroid Build Coastguard Worker TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
3204*6a54128fSAndroid Build Coastguard Worker {
3205*6a54128fSAndroid Build Coastguard Worker tdb_off_t rec_ptr;
3206*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
3207*6a54128fSAndroid Build Coastguard Worker TDB_DATA ret;
3208*6a54128fSAndroid Build Coastguard Worker u32 hash;
3209*6a54128fSAndroid Build Coastguard Worker
3210*6a54128fSAndroid Build Coastguard Worker /* find which hash bucket it is in */
3211*6a54128fSAndroid Build Coastguard Worker hash = tdb->hash_fn(&key);
3212*6a54128fSAndroid Build Coastguard Worker if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec)))
3213*6a54128fSAndroid Build Coastguard Worker return tdb_null;
3214*6a54128fSAndroid Build Coastguard Worker
3215*6a54128fSAndroid Build Coastguard Worker ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
3216*6a54128fSAndroid Build Coastguard Worker rec.data_len);
3217*6a54128fSAndroid Build Coastguard Worker ret.dsize = rec.data_len;
3218*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
3219*6a54128fSAndroid Build Coastguard Worker return ret;
3220*6a54128fSAndroid Build Coastguard Worker }
3221*6a54128fSAndroid Build Coastguard Worker
3222*6a54128fSAndroid Build Coastguard Worker /*
3223*6a54128fSAndroid Build Coastguard Worker * Find an entry in the database and hand the record's data to a parsing
3224*6a54128fSAndroid Build Coastguard Worker * function. The parsing function is executed under the chain read lock, so it
3225*6a54128fSAndroid Build Coastguard Worker * should be fast and should not block on other syscalls.
3226*6a54128fSAndroid Build Coastguard Worker *
3227*6a54128fSAndroid Build Coastguard Worker * DONT CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
3228*6a54128fSAndroid Build Coastguard Worker *
3229*6a54128fSAndroid Build Coastguard Worker * For mmapped tdb's that do not have a transaction open it points the parsing
3230*6a54128fSAndroid Build Coastguard Worker * function directly at the mmap area, it avoids the malloc/memcpy in this
3231*6a54128fSAndroid Build Coastguard Worker * case. If a transaction is open or no mmap is available, it has to do
3232*6a54128fSAndroid Build Coastguard Worker * malloc/read/parse/free.
3233*6a54128fSAndroid Build Coastguard Worker *
3234*6a54128fSAndroid Build Coastguard Worker * This is interesting for all readers of potentially large data structures in
3235*6a54128fSAndroid Build Coastguard Worker * the tdb records, ldb indexes being one example.
3236*6a54128fSAndroid Build Coastguard Worker */
3237*6a54128fSAndroid Build Coastguard Worker
tdb_parse_record(struct tdb_context * tdb,TDB_DATA key,int (* parser)(TDB_DATA key,TDB_DATA data,void * private_data),void * private_data)3238*6a54128fSAndroid Build Coastguard Worker int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
3239*6a54128fSAndroid Build Coastguard Worker int (*parser)(TDB_DATA key, TDB_DATA data,
3240*6a54128fSAndroid Build Coastguard Worker void *private_data),
3241*6a54128fSAndroid Build Coastguard Worker void *private_data)
3242*6a54128fSAndroid Build Coastguard Worker {
3243*6a54128fSAndroid Build Coastguard Worker tdb_off_t rec_ptr;
3244*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
3245*6a54128fSAndroid Build Coastguard Worker int ret;
3246*6a54128fSAndroid Build Coastguard Worker u32 hash;
3247*6a54128fSAndroid Build Coastguard Worker
3248*6a54128fSAndroid Build Coastguard Worker /* find which hash bucket it is in */
3249*6a54128fSAndroid Build Coastguard Worker hash = tdb->hash_fn(&key);
3250*6a54128fSAndroid Build Coastguard Worker
3251*6a54128fSAndroid Build Coastguard Worker if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
3252*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
3253*6a54128fSAndroid Build Coastguard Worker }
3254*6a54128fSAndroid Build Coastguard Worker
3255*6a54128fSAndroid Build Coastguard Worker ret = tdb_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len,
3256*6a54128fSAndroid Build Coastguard Worker rec.data_len, parser, private_data);
3257*6a54128fSAndroid Build Coastguard Worker
3258*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
3259*6a54128fSAndroid Build Coastguard Worker
3260*6a54128fSAndroid Build Coastguard Worker return ret;
3261*6a54128fSAndroid Build Coastguard Worker }
3262*6a54128fSAndroid Build Coastguard Worker
3263*6a54128fSAndroid Build Coastguard Worker /* check if an entry in the database exists
3264*6a54128fSAndroid Build Coastguard Worker
3265*6a54128fSAndroid Build Coastguard Worker note that 1 is returned if the key is found and 0 is returned if not found
3266*6a54128fSAndroid Build Coastguard Worker this doesn't match the conventions in the rest of this module, but is
3267*6a54128fSAndroid Build Coastguard Worker compatible with gdbm
3268*6a54128fSAndroid Build Coastguard Worker */
tdb_exists_hash(struct tdb_context * tdb,TDB_DATA key,u32 hash)3269*6a54128fSAndroid Build Coastguard Worker static int tdb_exists_hash(struct tdb_context *tdb, TDB_DATA key, u32 hash)
3270*6a54128fSAndroid Build Coastguard Worker {
3271*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
3272*6a54128fSAndroid Build Coastguard Worker
3273*6a54128fSAndroid Build Coastguard Worker if (tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) == 0)
3274*6a54128fSAndroid Build Coastguard Worker return 0;
3275*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
3276*6a54128fSAndroid Build Coastguard Worker return 1;
3277*6a54128fSAndroid Build Coastguard Worker }
3278*6a54128fSAndroid Build Coastguard Worker
tdb_exists(struct tdb_context * tdb,TDB_DATA key)3279*6a54128fSAndroid Build Coastguard Worker int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
3280*6a54128fSAndroid Build Coastguard Worker {
3281*6a54128fSAndroid Build Coastguard Worker u32 hash = tdb->hash_fn(&key);
3282*6a54128fSAndroid Build Coastguard Worker return tdb_exists_hash(tdb, key, hash);
3283*6a54128fSAndroid Build Coastguard Worker }
3284*6a54128fSAndroid Build Coastguard Worker
3285*6a54128fSAndroid Build Coastguard Worker /* actually delete an entry in the database given the offset */
tdb_do_delete(struct tdb_context * tdb,tdb_off_t rec_ptr,struct list_struct * rec)3286*6a54128fSAndroid Build Coastguard Worker int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct*rec)
3287*6a54128fSAndroid Build Coastguard Worker {
3288*6a54128fSAndroid Build Coastguard Worker tdb_off_t last_ptr, i;
3289*6a54128fSAndroid Build Coastguard Worker struct list_struct lastrec;
3290*6a54128fSAndroid Build Coastguard Worker
3291*6a54128fSAndroid Build Coastguard Worker if (tdb->read_only || tdb->traverse_read) return -1;
3292*6a54128fSAndroid Build Coastguard Worker
3293*6a54128fSAndroid Build Coastguard Worker if (tdb_write_lock_record(tdb, rec_ptr) == -1) {
3294*6a54128fSAndroid Build Coastguard Worker /* Someone traversing here: mark it as dead */
3295*6a54128fSAndroid Build Coastguard Worker rec->magic = TDB_DEAD_MAGIC;
3296*6a54128fSAndroid Build Coastguard Worker return tdb_rec_write(tdb, rec_ptr, rec);
3297*6a54128fSAndroid Build Coastguard Worker }
3298*6a54128fSAndroid Build Coastguard Worker if (tdb_write_unlock_record(tdb, rec_ptr) != 0)
3299*6a54128fSAndroid Build Coastguard Worker return -1;
3300*6a54128fSAndroid Build Coastguard Worker
3301*6a54128fSAndroid Build Coastguard Worker /* find previous record in hash chain */
3302*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) == -1)
3303*6a54128fSAndroid Build Coastguard Worker return -1;
3304*6a54128fSAndroid Build Coastguard Worker for (last_ptr = 0; i != rec_ptr; last_ptr = i, i = lastrec.next)
3305*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_read(tdb, i, &lastrec) == -1)
3306*6a54128fSAndroid Build Coastguard Worker return -1;
3307*6a54128fSAndroid Build Coastguard Worker
3308*6a54128fSAndroid Build Coastguard Worker /* unlink it: next ptr is at start of record. */
3309*6a54128fSAndroid Build Coastguard Worker if (last_ptr == 0)
3310*6a54128fSAndroid Build Coastguard Worker last_ptr = TDB_HASH_TOP(rec->full_hash);
3311*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_write(tdb, last_ptr, &rec->next) == -1)
3312*6a54128fSAndroid Build Coastguard Worker return -1;
3313*6a54128fSAndroid Build Coastguard Worker
3314*6a54128fSAndroid Build Coastguard Worker /* recover the space */
3315*6a54128fSAndroid Build Coastguard Worker if (tdb_free(tdb, rec_ptr, rec) == -1)
3316*6a54128fSAndroid Build Coastguard Worker return -1;
3317*6a54128fSAndroid Build Coastguard Worker return 0;
3318*6a54128fSAndroid Build Coastguard Worker }
3319*6a54128fSAndroid Build Coastguard Worker
tdb_count_dead(struct tdb_context * tdb,u32 hash)3320*6a54128fSAndroid Build Coastguard Worker static int tdb_count_dead(struct tdb_context *tdb, u32 hash)
3321*6a54128fSAndroid Build Coastguard Worker {
3322*6a54128fSAndroid Build Coastguard Worker int res = 0;
3323*6a54128fSAndroid Build Coastguard Worker tdb_off_t rec_ptr;
3324*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
3325*6a54128fSAndroid Build Coastguard Worker
3326*6a54128fSAndroid Build Coastguard Worker /* read in the hash top */
3327*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
3328*6a54128fSAndroid Build Coastguard Worker return 0;
3329*6a54128fSAndroid Build Coastguard Worker
3330*6a54128fSAndroid Build Coastguard Worker while (rec_ptr) {
3331*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_read(tdb, rec_ptr, &rec) == -1)
3332*6a54128fSAndroid Build Coastguard Worker return 0;
3333*6a54128fSAndroid Build Coastguard Worker
3334*6a54128fSAndroid Build Coastguard Worker if (rec.magic == TDB_DEAD_MAGIC) {
3335*6a54128fSAndroid Build Coastguard Worker res += 1;
3336*6a54128fSAndroid Build Coastguard Worker }
3337*6a54128fSAndroid Build Coastguard Worker rec_ptr = rec.next;
3338*6a54128fSAndroid Build Coastguard Worker }
3339*6a54128fSAndroid Build Coastguard Worker return res;
3340*6a54128fSAndroid Build Coastguard Worker }
3341*6a54128fSAndroid Build Coastguard Worker
3342*6a54128fSAndroid Build Coastguard Worker /*
3343*6a54128fSAndroid Build Coastguard Worker * Purge all DEAD records from a hash chain
3344*6a54128fSAndroid Build Coastguard Worker */
tdb_purge_dead(struct tdb_context * tdb,u32 hash)3345*6a54128fSAndroid Build Coastguard Worker static int tdb_purge_dead(struct tdb_context *tdb, u32 hash)
3346*6a54128fSAndroid Build Coastguard Worker {
3347*6a54128fSAndroid Build Coastguard Worker int res = -1;
3348*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
3349*6a54128fSAndroid Build Coastguard Worker tdb_off_t rec_ptr;
3350*6a54128fSAndroid Build Coastguard Worker
3351*6a54128fSAndroid Build Coastguard Worker if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
3352*6a54128fSAndroid Build Coastguard Worker return -1;
3353*6a54128fSAndroid Build Coastguard Worker }
3354*6a54128fSAndroid Build Coastguard Worker
3355*6a54128fSAndroid Build Coastguard Worker /* read in the hash top */
3356*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
3357*6a54128fSAndroid Build Coastguard Worker goto fail;
3358*6a54128fSAndroid Build Coastguard Worker
3359*6a54128fSAndroid Build Coastguard Worker while (rec_ptr) {
3360*6a54128fSAndroid Build Coastguard Worker tdb_off_t next;
3361*6a54128fSAndroid Build Coastguard Worker
3362*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_read(tdb, rec_ptr, &rec) == -1) {
3363*6a54128fSAndroid Build Coastguard Worker goto fail;
3364*6a54128fSAndroid Build Coastguard Worker }
3365*6a54128fSAndroid Build Coastguard Worker
3366*6a54128fSAndroid Build Coastguard Worker next = rec.next;
3367*6a54128fSAndroid Build Coastguard Worker
3368*6a54128fSAndroid Build Coastguard Worker if (rec.magic == TDB_DEAD_MAGIC
3369*6a54128fSAndroid Build Coastguard Worker && tdb_do_delete(tdb, rec_ptr, &rec) == -1) {
3370*6a54128fSAndroid Build Coastguard Worker goto fail;
3371*6a54128fSAndroid Build Coastguard Worker }
3372*6a54128fSAndroid Build Coastguard Worker rec_ptr = next;
3373*6a54128fSAndroid Build Coastguard Worker }
3374*6a54128fSAndroid Build Coastguard Worker res = 0;
3375*6a54128fSAndroid Build Coastguard Worker fail:
3376*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, -1, F_WRLCK);
3377*6a54128fSAndroid Build Coastguard Worker return res;
3378*6a54128fSAndroid Build Coastguard Worker }
3379*6a54128fSAndroid Build Coastguard Worker
3380*6a54128fSAndroid Build Coastguard Worker /* delete an entry in the database given a key */
tdb_delete_hash(struct tdb_context * tdb,TDB_DATA key,u32 hash)3381*6a54128fSAndroid Build Coastguard Worker static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, u32 hash)
3382*6a54128fSAndroid Build Coastguard Worker {
3383*6a54128fSAndroid Build Coastguard Worker tdb_off_t rec_ptr;
3384*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
3385*6a54128fSAndroid Build Coastguard Worker int ret;
3386*6a54128fSAndroid Build Coastguard Worker
3387*6a54128fSAndroid Build Coastguard Worker if (tdb->max_dead_records != 0) {
3388*6a54128fSAndroid Build Coastguard Worker
3389*6a54128fSAndroid Build Coastguard Worker /*
3390*6a54128fSAndroid Build Coastguard Worker * Allow for some dead records per hash chain, mainly for
3391*6a54128fSAndroid Build Coastguard Worker * tdb's with a very high create/delete rate like locking.tdb.
3392*6a54128fSAndroid Build Coastguard Worker */
3393*6a54128fSAndroid Build Coastguard Worker
3394*6a54128fSAndroid Build Coastguard Worker if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
3395*6a54128fSAndroid Build Coastguard Worker return -1;
3396*6a54128fSAndroid Build Coastguard Worker
3397*6a54128fSAndroid Build Coastguard Worker if (tdb_count_dead(tdb, hash) >= tdb->max_dead_records) {
3398*6a54128fSAndroid Build Coastguard Worker /*
3399*6a54128fSAndroid Build Coastguard Worker * Don't let the per-chain freelist grow too large,
3400*6a54128fSAndroid Build Coastguard Worker * delete all existing dead records
3401*6a54128fSAndroid Build Coastguard Worker */
3402*6a54128fSAndroid Build Coastguard Worker tdb_purge_dead(tdb, hash);
3403*6a54128fSAndroid Build Coastguard Worker }
3404*6a54128fSAndroid Build Coastguard Worker
3405*6a54128fSAndroid Build Coastguard Worker if (!(rec_ptr = tdb_find(tdb, key, hash, &rec))) {
3406*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
3407*6a54128fSAndroid Build Coastguard Worker return -1;
3408*6a54128fSAndroid Build Coastguard Worker }
3409*6a54128fSAndroid Build Coastguard Worker
3410*6a54128fSAndroid Build Coastguard Worker /*
3411*6a54128fSAndroid Build Coastguard Worker * Just mark the record as dead.
3412*6a54128fSAndroid Build Coastguard Worker */
3413*6a54128fSAndroid Build Coastguard Worker rec.magic = TDB_DEAD_MAGIC;
3414*6a54128fSAndroid Build Coastguard Worker ret = tdb_rec_write(tdb, rec_ptr, &rec);
3415*6a54128fSAndroid Build Coastguard Worker }
3416*6a54128fSAndroid Build Coastguard Worker else {
3417*6a54128fSAndroid Build Coastguard Worker if (!(rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK,
3418*6a54128fSAndroid Build Coastguard Worker &rec)))
3419*6a54128fSAndroid Build Coastguard Worker return -1;
3420*6a54128fSAndroid Build Coastguard Worker
3421*6a54128fSAndroid Build Coastguard Worker ret = tdb_do_delete(tdb, rec_ptr, &rec);
3422*6a54128fSAndroid Build Coastguard Worker }
3423*6a54128fSAndroid Build Coastguard Worker
3424*6a54128fSAndroid Build Coastguard Worker if (ret == 0) {
3425*6a54128fSAndroid Build Coastguard Worker tdb_increment_seqnum(tdb);
3426*6a54128fSAndroid Build Coastguard Worker }
3427*6a54128fSAndroid Build Coastguard Worker
3428*6a54128fSAndroid Build Coastguard Worker if (tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK) != 0)
3429*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_delete: WARNING tdb_unlock failed!\n"));
3430*6a54128fSAndroid Build Coastguard Worker return ret;
3431*6a54128fSAndroid Build Coastguard Worker }
3432*6a54128fSAndroid Build Coastguard Worker
tdb_delete(struct tdb_context * tdb,TDB_DATA key)3433*6a54128fSAndroid Build Coastguard Worker int tdb_delete(struct tdb_context *tdb, TDB_DATA key)
3434*6a54128fSAndroid Build Coastguard Worker {
3435*6a54128fSAndroid Build Coastguard Worker u32 hash = tdb->hash_fn(&key);
3436*6a54128fSAndroid Build Coastguard Worker return tdb_delete_hash(tdb, key, hash);
3437*6a54128fSAndroid Build Coastguard Worker }
3438*6a54128fSAndroid Build Coastguard Worker
3439*6a54128fSAndroid Build Coastguard Worker /*
3440*6a54128fSAndroid Build Coastguard Worker * See if we have a dead record around with enough space
3441*6a54128fSAndroid Build Coastguard Worker */
tdb_find_dead(struct tdb_context * tdb,u32 hash,struct list_struct * r,tdb_len_t length)3442*6a54128fSAndroid Build Coastguard Worker static tdb_off_t tdb_find_dead(struct tdb_context *tdb, u32 hash,
3443*6a54128fSAndroid Build Coastguard Worker struct list_struct *r, tdb_len_t length)
3444*6a54128fSAndroid Build Coastguard Worker {
3445*6a54128fSAndroid Build Coastguard Worker tdb_off_t rec_ptr;
3446*6a54128fSAndroid Build Coastguard Worker
3447*6a54128fSAndroid Build Coastguard Worker /* read in the hash top */
3448*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
3449*6a54128fSAndroid Build Coastguard Worker return 0;
3450*6a54128fSAndroid Build Coastguard Worker
3451*6a54128fSAndroid Build Coastguard Worker /* keep looking until we find the right record */
3452*6a54128fSAndroid Build Coastguard Worker while (rec_ptr) {
3453*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_read(tdb, rec_ptr, r) == -1)
3454*6a54128fSAndroid Build Coastguard Worker return 0;
3455*6a54128fSAndroid Build Coastguard Worker
3456*6a54128fSAndroid Build Coastguard Worker if (TDB_DEAD(r) && r->rec_len >= length) {
3457*6a54128fSAndroid Build Coastguard Worker /*
3458*6a54128fSAndroid Build Coastguard Worker * First fit for simple coding, TODO: change to best
3459*6a54128fSAndroid Build Coastguard Worker * fit
3460*6a54128fSAndroid Build Coastguard Worker */
3461*6a54128fSAndroid Build Coastguard Worker return rec_ptr;
3462*6a54128fSAndroid Build Coastguard Worker }
3463*6a54128fSAndroid Build Coastguard Worker rec_ptr = r->next;
3464*6a54128fSAndroid Build Coastguard Worker }
3465*6a54128fSAndroid Build Coastguard Worker return 0;
3466*6a54128fSAndroid Build Coastguard Worker }
3467*6a54128fSAndroid Build Coastguard Worker
3468*6a54128fSAndroid Build Coastguard Worker /* store an element in the database, replacing any existing element
3469*6a54128fSAndroid Build Coastguard Worker with the same key
3470*6a54128fSAndroid Build Coastguard Worker
3471*6a54128fSAndroid Build Coastguard Worker return 0 on success, -1 on failure
3472*6a54128fSAndroid Build Coastguard Worker */
tdb_store(struct tdb_context * tdb,TDB_DATA key,TDB_DATA dbuf,int flag)3473*6a54128fSAndroid Build Coastguard Worker int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
3474*6a54128fSAndroid Build Coastguard Worker {
3475*6a54128fSAndroid Build Coastguard Worker struct list_struct rec;
3476*6a54128fSAndroid Build Coastguard Worker u32 hash;
3477*6a54128fSAndroid Build Coastguard Worker tdb_off_t rec_ptr;
3478*6a54128fSAndroid Build Coastguard Worker char *p = NULL;
3479*6a54128fSAndroid Build Coastguard Worker int ret = -1;
3480*6a54128fSAndroid Build Coastguard Worker
3481*6a54128fSAndroid Build Coastguard Worker if (tdb->read_only || tdb->traverse_read) {
3482*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_RDONLY;
3483*6a54128fSAndroid Build Coastguard Worker return -1;
3484*6a54128fSAndroid Build Coastguard Worker }
3485*6a54128fSAndroid Build Coastguard Worker
3486*6a54128fSAndroid Build Coastguard Worker /* find which hash bucket it is in */
3487*6a54128fSAndroid Build Coastguard Worker hash = tdb->hash_fn(&key);
3488*6a54128fSAndroid Build Coastguard Worker if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
3489*6a54128fSAndroid Build Coastguard Worker return -1;
3490*6a54128fSAndroid Build Coastguard Worker
3491*6a54128fSAndroid Build Coastguard Worker /* check for it existing, on insert. */
3492*6a54128fSAndroid Build Coastguard Worker if (flag == TDB_INSERT) {
3493*6a54128fSAndroid Build Coastguard Worker if (tdb_exists_hash(tdb, key, hash)) {
3494*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_EXISTS;
3495*6a54128fSAndroid Build Coastguard Worker goto fail;
3496*6a54128fSAndroid Build Coastguard Worker }
3497*6a54128fSAndroid Build Coastguard Worker } else {
3498*6a54128fSAndroid Build Coastguard Worker /* first try in-place update, on modify or replace. */
3499*6a54128fSAndroid Build Coastguard Worker if (tdb_update_hash(tdb, key, hash, dbuf) == 0) {
3500*6a54128fSAndroid Build Coastguard Worker goto done;
3501*6a54128fSAndroid Build Coastguard Worker }
3502*6a54128fSAndroid Build Coastguard Worker if (tdb->ecode == TDB_ERR_NOEXIST &&
3503*6a54128fSAndroid Build Coastguard Worker flag == TDB_MODIFY) {
3504*6a54128fSAndroid Build Coastguard Worker /* if the record doesn't exist and we are in TDB_MODIFY mode then
3505*6a54128fSAndroid Build Coastguard Worker we should fail the store */
3506*6a54128fSAndroid Build Coastguard Worker goto fail;
3507*6a54128fSAndroid Build Coastguard Worker }
3508*6a54128fSAndroid Build Coastguard Worker }
3509*6a54128fSAndroid Build Coastguard Worker /* reset the error code potentially set by the tdb_update() */
3510*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_SUCCESS;
3511*6a54128fSAndroid Build Coastguard Worker
3512*6a54128fSAndroid Build Coastguard Worker /* delete any existing record - if it doesn't exist we don't
3513*6a54128fSAndroid Build Coastguard Worker care. Doing this first reduces fragmentation, and avoids
3514*6a54128fSAndroid Build Coastguard Worker coalescing with `allocated' block before it's updated. */
3515*6a54128fSAndroid Build Coastguard Worker if (flag != TDB_INSERT)
3516*6a54128fSAndroid Build Coastguard Worker tdb_delete_hash(tdb, key, hash);
3517*6a54128fSAndroid Build Coastguard Worker
3518*6a54128fSAndroid Build Coastguard Worker /* Copy key+value *before* allocating free space in case malloc
3519*6a54128fSAndroid Build Coastguard Worker fails and we are left with a dead spot in the tdb. */
3520*6a54128fSAndroid Build Coastguard Worker
3521*6a54128fSAndroid Build Coastguard Worker if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
3522*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_OOM;
3523*6a54128fSAndroid Build Coastguard Worker goto fail;
3524*6a54128fSAndroid Build Coastguard Worker }
3525*6a54128fSAndroid Build Coastguard Worker
3526*6a54128fSAndroid Build Coastguard Worker memcpy(p, key.dptr, key.dsize);
3527*6a54128fSAndroid Build Coastguard Worker if (dbuf.dsize)
3528*6a54128fSAndroid Build Coastguard Worker memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
3529*6a54128fSAndroid Build Coastguard Worker
3530*6a54128fSAndroid Build Coastguard Worker if (tdb->max_dead_records != 0) {
3531*6a54128fSAndroid Build Coastguard Worker /*
3532*6a54128fSAndroid Build Coastguard Worker * Allow for some dead records per hash chain, look if we can
3533*6a54128fSAndroid Build Coastguard Worker * find one that can hold the new record. We need enough space
3534*6a54128fSAndroid Build Coastguard Worker * for key, data and tailer. If we find one, we don't have to
3535*6a54128fSAndroid Build Coastguard Worker * consult the central freelist.
3536*6a54128fSAndroid Build Coastguard Worker */
3537*6a54128fSAndroid Build Coastguard Worker rec_ptr = tdb_find_dead(
3538*6a54128fSAndroid Build Coastguard Worker tdb, hash, &rec,
3539*6a54128fSAndroid Build Coastguard Worker key.dsize + dbuf.dsize + sizeof(tdb_off_t));
3540*6a54128fSAndroid Build Coastguard Worker
3541*6a54128fSAndroid Build Coastguard Worker if (rec_ptr != 0) {
3542*6a54128fSAndroid Build Coastguard Worker rec.key_len = key.dsize;
3543*6a54128fSAndroid Build Coastguard Worker rec.data_len = dbuf.dsize;
3544*6a54128fSAndroid Build Coastguard Worker rec.full_hash = hash;
3545*6a54128fSAndroid Build Coastguard Worker rec.magic = TDB_MAGIC;
3546*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
3547*6a54128fSAndroid Build Coastguard Worker || tdb->methods->tdb_write(
3548*6a54128fSAndroid Build Coastguard Worker tdb, rec_ptr + sizeof(rec),
3549*6a54128fSAndroid Build Coastguard Worker p, key.dsize + dbuf.dsize) == -1) {
3550*6a54128fSAndroid Build Coastguard Worker goto fail;
3551*6a54128fSAndroid Build Coastguard Worker }
3552*6a54128fSAndroid Build Coastguard Worker goto done;
3553*6a54128fSAndroid Build Coastguard Worker }
3554*6a54128fSAndroid Build Coastguard Worker }
3555*6a54128fSAndroid Build Coastguard Worker
3556*6a54128fSAndroid Build Coastguard Worker /*
3557*6a54128fSAndroid Build Coastguard Worker * We have to allocate some space from the freelist, so this means we
3558*6a54128fSAndroid Build Coastguard Worker * have to lock it. Use the chance to purge all the DEAD records from
3559*6a54128fSAndroid Build Coastguard Worker * the hash chain under the freelist lock.
3560*6a54128fSAndroid Build Coastguard Worker */
3561*6a54128fSAndroid Build Coastguard Worker
3562*6a54128fSAndroid Build Coastguard Worker if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
3563*6a54128fSAndroid Build Coastguard Worker goto fail;
3564*6a54128fSAndroid Build Coastguard Worker }
3565*6a54128fSAndroid Build Coastguard Worker
3566*6a54128fSAndroid Build Coastguard Worker if ((tdb->max_dead_records != 0)
3567*6a54128fSAndroid Build Coastguard Worker && (tdb_purge_dead(tdb, hash) == -1)) {
3568*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, -1, F_WRLCK);
3569*6a54128fSAndroid Build Coastguard Worker goto fail;
3570*6a54128fSAndroid Build Coastguard Worker }
3571*6a54128fSAndroid Build Coastguard Worker
3572*6a54128fSAndroid Build Coastguard Worker /* we have to allocate some space */
3573*6a54128fSAndroid Build Coastguard Worker rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec);
3574*6a54128fSAndroid Build Coastguard Worker
3575*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, -1, F_WRLCK);
3576*6a54128fSAndroid Build Coastguard Worker
3577*6a54128fSAndroid Build Coastguard Worker if (rec_ptr == 0) {
3578*6a54128fSAndroid Build Coastguard Worker goto fail;
3579*6a54128fSAndroid Build Coastguard Worker }
3580*6a54128fSAndroid Build Coastguard Worker
3581*6a54128fSAndroid Build Coastguard Worker /* Read hash top into next ptr */
3582*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
3583*6a54128fSAndroid Build Coastguard Worker goto fail;
3584*6a54128fSAndroid Build Coastguard Worker
3585*6a54128fSAndroid Build Coastguard Worker rec.key_len = key.dsize;
3586*6a54128fSAndroid Build Coastguard Worker rec.data_len = dbuf.dsize;
3587*6a54128fSAndroid Build Coastguard Worker rec.full_hash = hash;
3588*6a54128fSAndroid Build Coastguard Worker rec.magic = TDB_MAGIC;
3589*6a54128fSAndroid Build Coastguard Worker
3590*6a54128fSAndroid Build Coastguard Worker /* write out and point the top of the hash chain at it */
3591*6a54128fSAndroid Build Coastguard Worker if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
3592*6a54128fSAndroid Build Coastguard Worker || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
3593*6a54128fSAndroid Build Coastguard Worker || tdb_ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
3594*6a54128fSAndroid Build Coastguard Worker /* Need to tdb_unallocate() here */
3595*6a54128fSAndroid Build Coastguard Worker goto fail;
3596*6a54128fSAndroid Build Coastguard Worker }
3597*6a54128fSAndroid Build Coastguard Worker
3598*6a54128fSAndroid Build Coastguard Worker done:
3599*6a54128fSAndroid Build Coastguard Worker ret = 0;
3600*6a54128fSAndroid Build Coastguard Worker fail:
3601*6a54128fSAndroid Build Coastguard Worker if (ret == 0) {
3602*6a54128fSAndroid Build Coastguard Worker tdb_increment_seqnum(tdb);
3603*6a54128fSAndroid Build Coastguard Worker }
3604*6a54128fSAndroid Build Coastguard Worker
3605*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(p);
3606*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
3607*6a54128fSAndroid Build Coastguard Worker return ret;
3608*6a54128fSAndroid Build Coastguard Worker }
3609*6a54128fSAndroid Build Coastguard Worker
3610*6a54128fSAndroid Build Coastguard Worker
3611*6a54128fSAndroid Build Coastguard Worker /* Append to an entry. Create if not exist. */
tdb_append(struct tdb_context * tdb,TDB_DATA key,TDB_DATA new_dbuf)3612*6a54128fSAndroid Build Coastguard Worker int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
3613*6a54128fSAndroid Build Coastguard Worker {
3614*6a54128fSAndroid Build Coastguard Worker u32 hash;
3615*6a54128fSAndroid Build Coastguard Worker TDB_DATA dbuf;
3616*6a54128fSAndroid Build Coastguard Worker int ret = -1;
3617*6a54128fSAndroid Build Coastguard Worker
3618*6a54128fSAndroid Build Coastguard Worker /* find which hash bucket it is in */
3619*6a54128fSAndroid Build Coastguard Worker hash = tdb->hash_fn(&key);
3620*6a54128fSAndroid Build Coastguard Worker if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
3621*6a54128fSAndroid Build Coastguard Worker return -1;
3622*6a54128fSAndroid Build Coastguard Worker
3623*6a54128fSAndroid Build Coastguard Worker dbuf = tdb_fetch(tdb, key);
3624*6a54128fSAndroid Build Coastguard Worker
3625*6a54128fSAndroid Build Coastguard Worker if (dbuf.dptr == NULL) {
3626*6a54128fSAndroid Build Coastguard Worker dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize);
3627*6a54128fSAndroid Build Coastguard Worker } else {
3628*6a54128fSAndroid Build Coastguard Worker unsigned char *new_dptr = (unsigned char *)realloc(dbuf.dptr,
3629*6a54128fSAndroid Build Coastguard Worker dbuf.dsize + new_dbuf.dsize);
3630*6a54128fSAndroid Build Coastguard Worker if (new_dptr == NULL) {
3631*6a54128fSAndroid Build Coastguard Worker free(dbuf.dptr);
3632*6a54128fSAndroid Build Coastguard Worker }
3633*6a54128fSAndroid Build Coastguard Worker dbuf.dptr = new_dptr;
3634*6a54128fSAndroid Build Coastguard Worker }
3635*6a54128fSAndroid Build Coastguard Worker
3636*6a54128fSAndroid Build Coastguard Worker if (dbuf.dptr == NULL) {
3637*6a54128fSAndroid Build Coastguard Worker tdb->ecode = TDB_ERR_OOM;
3638*6a54128fSAndroid Build Coastguard Worker goto failed;
3639*6a54128fSAndroid Build Coastguard Worker }
3640*6a54128fSAndroid Build Coastguard Worker
3641*6a54128fSAndroid Build Coastguard Worker memcpy(dbuf.dptr + dbuf.dsize, new_dbuf.dptr, new_dbuf.dsize);
3642*6a54128fSAndroid Build Coastguard Worker dbuf.dsize += new_dbuf.dsize;
3643*6a54128fSAndroid Build Coastguard Worker
3644*6a54128fSAndroid Build Coastguard Worker ret = tdb_store(tdb, key, dbuf, 0);
3645*6a54128fSAndroid Build Coastguard Worker
3646*6a54128fSAndroid Build Coastguard Worker failed:
3647*6a54128fSAndroid Build Coastguard Worker tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
3648*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(dbuf.dptr);
3649*6a54128fSAndroid Build Coastguard Worker return ret;
3650*6a54128fSAndroid Build Coastguard Worker }
3651*6a54128fSAndroid Build Coastguard Worker
3652*6a54128fSAndroid Build Coastguard Worker
3653*6a54128fSAndroid Build Coastguard Worker /*
3654*6a54128fSAndroid Build Coastguard Worker return the name of the current tdb file
3655*6a54128fSAndroid Build Coastguard Worker useful for external logging functions
3656*6a54128fSAndroid Build Coastguard Worker */
tdb_name(struct tdb_context * tdb)3657*6a54128fSAndroid Build Coastguard Worker const char *tdb_name(struct tdb_context *tdb)
3658*6a54128fSAndroid Build Coastguard Worker {
3659*6a54128fSAndroid Build Coastguard Worker return tdb->name;
3660*6a54128fSAndroid Build Coastguard Worker }
3661*6a54128fSAndroid Build Coastguard Worker
3662*6a54128fSAndroid Build Coastguard Worker /*
3663*6a54128fSAndroid Build Coastguard Worker return the underlying file descriptor being used by tdb, or -1
3664*6a54128fSAndroid Build Coastguard Worker useful for external routines that want to check the device/inode
3665*6a54128fSAndroid Build Coastguard Worker of the fd
3666*6a54128fSAndroid Build Coastguard Worker */
tdb_fd(struct tdb_context * tdb)3667*6a54128fSAndroid Build Coastguard Worker int tdb_fd(struct tdb_context *tdb)
3668*6a54128fSAndroid Build Coastguard Worker {
3669*6a54128fSAndroid Build Coastguard Worker return tdb->fd;
3670*6a54128fSAndroid Build Coastguard Worker }
3671*6a54128fSAndroid Build Coastguard Worker
3672*6a54128fSAndroid Build Coastguard Worker /*
3673*6a54128fSAndroid Build Coastguard Worker return the current logging function
3674*6a54128fSAndroid Build Coastguard Worker useful for external tdb routines that wish to log tdb errors
3675*6a54128fSAndroid Build Coastguard Worker */
tdb_log_fn(struct tdb_context * tdb)3676*6a54128fSAndroid Build Coastguard Worker tdb_log_func tdb_log_fn(struct tdb_context *tdb)
3677*6a54128fSAndroid Build Coastguard Worker {
3678*6a54128fSAndroid Build Coastguard Worker return tdb->log.log_fn;
3679*6a54128fSAndroid Build Coastguard Worker }
3680*6a54128fSAndroid Build Coastguard Worker
3681*6a54128fSAndroid Build Coastguard Worker
3682*6a54128fSAndroid Build Coastguard Worker /*
3683*6a54128fSAndroid Build Coastguard Worker get the tdb sequence number. Only makes sense if the writers opened
3684*6a54128fSAndroid Build Coastguard Worker with TDB_SEQNUM set. Note that this sequence number will wrap quite
3685*6a54128fSAndroid Build Coastguard Worker quickly, so it should only be used for a 'has something changed'
3686*6a54128fSAndroid Build Coastguard Worker test, not for code that relies on the count of the number of changes
3687*6a54128fSAndroid Build Coastguard Worker made. If you want a counter then use a tdb record.
3688*6a54128fSAndroid Build Coastguard Worker
3689*6a54128fSAndroid Build Coastguard Worker The aim of this sequence number is to allow for a very lightweight
3690*6a54128fSAndroid Build Coastguard Worker test of a possible tdb change.
3691*6a54128fSAndroid Build Coastguard Worker */
tdb_get_seqnum(struct tdb_context * tdb)3692*6a54128fSAndroid Build Coastguard Worker int tdb_get_seqnum(struct tdb_context *tdb)
3693*6a54128fSAndroid Build Coastguard Worker {
3694*6a54128fSAndroid Build Coastguard Worker tdb_off_t seqnum=0;
3695*6a54128fSAndroid Build Coastguard Worker
3696*6a54128fSAndroid Build Coastguard Worker if (tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum) == -1)
3697*6a54128fSAndroid Build Coastguard Worker return 0;
3698*6a54128fSAndroid Build Coastguard Worker return seqnum;
3699*6a54128fSAndroid Build Coastguard Worker }
3700*6a54128fSAndroid Build Coastguard Worker
tdb_hash_size(struct tdb_context * tdb)3701*6a54128fSAndroid Build Coastguard Worker int tdb_hash_size(struct tdb_context *tdb)
3702*6a54128fSAndroid Build Coastguard Worker {
3703*6a54128fSAndroid Build Coastguard Worker return tdb->header.hash_size;
3704*6a54128fSAndroid Build Coastguard Worker }
3705*6a54128fSAndroid Build Coastguard Worker
tdb_map_size(struct tdb_context * tdb)3706*6a54128fSAndroid Build Coastguard Worker size_t tdb_map_size(struct tdb_context *tdb)
3707*6a54128fSAndroid Build Coastguard Worker {
3708*6a54128fSAndroid Build Coastguard Worker return tdb->map_size;
3709*6a54128fSAndroid Build Coastguard Worker }
3710*6a54128fSAndroid Build Coastguard Worker
tdb_get_flags(struct tdb_context * tdb)3711*6a54128fSAndroid Build Coastguard Worker int tdb_get_flags(struct tdb_context *tdb)
3712*6a54128fSAndroid Build Coastguard Worker {
3713*6a54128fSAndroid Build Coastguard Worker return tdb->flags;
3714*6a54128fSAndroid Build Coastguard Worker }
3715*6a54128fSAndroid Build Coastguard Worker
3716*6a54128fSAndroid Build Coastguard Worker
3717*6a54128fSAndroid Build Coastguard Worker /*
3718*6a54128fSAndroid Build Coastguard Worker enable sequence number handling on an open tdb
3719*6a54128fSAndroid Build Coastguard Worker */
tdb_enable_seqnum(struct tdb_context * tdb)3720*6a54128fSAndroid Build Coastguard Worker void tdb_enable_seqnum(struct tdb_context *tdb)
3721*6a54128fSAndroid Build Coastguard Worker {
3722*6a54128fSAndroid Build Coastguard Worker tdb->flags |= TDB_SEQNUM;
3723*6a54128fSAndroid Build Coastguard Worker }
3724*6a54128fSAndroid Build Coastguard Worker
3725*6a54128fSAndroid Build Coastguard Worker /* file: open.c */
3726*6a54128fSAndroid Build Coastguard Worker
3727*6a54128fSAndroid Build Coastguard Worker /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
3728*6a54128fSAndroid Build Coastguard Worker static struct tdb_context *tdbs = NULL;
3729*6a54128fSAndroid Build Coastguard Worker
3730*6a54128fSAndroid Build Coastguard Worker
3731*6a54128fSAndroid Build Coastguard Worker /* This is from a hash algorithm suggested by Rogier Wolff */
default_tdb_hash(TDB_DATA * key)3732*6a54128fSAndroid Build Coastguard Worker static unsigned int default_tdb_hash(TDB_DATA *key)
3733*6a54128fSAndroid Build Coastguard Worker {
3734*6a54128fSAndroid Build Coastguard Worker u32 value; /* Used to compute the hash value. */
3735*6a54128fSAndroid Build Coastguard Worker u32 i; /* Used to cycle through random values. */
3736*6a54128fSAndroid Build Coastguard Worker
3737*6a54128fSAndroid Build Coastguard Worker /* Set the initial value from the key size. */
3738*6a54128fSAndroid Build Coastguard Worker for (value = 0, i=0; i < key->dsize; i++)
3739*6a54128fSAndroid Build Coastguard Worker value = value * 256 + key->dptr[i] + (value >> 24) * 241;
3740*6a54128fSAndroid Build Coastguard Worker
3741*6a54128fSAndroid Build Coastguard Worker return value;
3742*6a54128fSAndroid Build Coastguard Worker }
3743*6a54128fSAndroid Build Coastguard Worker
3744*6a54128fSAndroid Build Coastguard Worker
3745*6a54128fSAndroid Build Coastguard Worker /* initialise a new database with a specified hash size */
tdb_new_database(struct tdb_context * tdb,int hash_size)3746*6a54128fSAndroid Build Coastguard Worker static int tdb_new_database(struct tdb_context *tdb, int hash_size)
3747*6a54128fSAndroid Build Coastguard Worker {
3748*6a54128fSAndroid Build Coastguard Worker struct tdb_header *newdb;
3749*6a54128fSAndroid Build Coastguard Worker int size, ret = -1;
3750*6a54128fSAndroid Build Coastguard Worker
3751*6a54128fSAndroid Build Coastguard Worker /* We make it up in memory, then write it out if not internal */
3752*6a54128fSAndroid Build Coastguard Worker size = sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off_t);
3753*6a54128fSAndroid Build Coastguard Worker if (!(newdb = (struct tdb_header *)calloc(size, 1)))
3754*6a54128fSAndroid Build Coastguard Worker return TDB_ERRCODE(TDB_ERR_OOM, -1);
3755*6a54128fSAndroid Build Coastguard Worker
3756*6a54128fSAndroid Build Coastguard Worker /* Fill in the header */
3757*6a54128fSAndroid Build Coastguard Worker newdb->version = TDB_VERSION;
3758*6a54128fSAndroid Build Coastguard Worker newdb->hash_size = hash_size;
3759*6a54128fSAndroid Build Coastguard Worker if (tdb->flags & TDB_INTERNAL) {
3760*6a54128fSAndroid Build Coastguard Worker tdb->map_size = size;
3761*6a54128fSAndroid Build Coastguard Worker tdb->map_ptr = (char *)newdb;
3762*6a54128fSAndroid Build Coastguard Worker memcpy(&tdb->header, newdb, sizeof(tdb->header));
3763*6a54128fSAndroid Build Coastguard Worker /* Convert the `ondisk' version if asked. */
3764*6a54128fSAndroid Build Coastguard Worker CONVERT(*newdb);
3765*6a54128fSAndroid Build Coastguard Worker return 0;
3766*6a54128fSAndroid Build Coastguard Worker }
3767*6a54128fSAndroid Build Coastguard Worker if (lseek(tdb->fd, 0, SEEK_SET) == -1)
3768*6a54128fSAndroid Build Coastguard Worker goto fail;
3769*6a54128fSAndroid Build Coastguard Worker
3770*6a54128fSAndroid Build Coastguard Worker if (ftruncate(tdb->fd, 0) == -1)
3771*6a54128fSAndroid Build Coastguard Worker goto fail;
3772*6a54128fSAndroid Build Coastguard Worker
3773*6a54128fSAndroid Build Coastguard Worker /* This creates an endian-converted header, as if read from disk */
3774*6a54128fSAndroid Build Coastguard Worker CONVERT(*newdb);
3775*6a54128fSAndroid Build Coastguard Worker memcpy(&tdb->header, newdb, sizeof(tdb->header));
3776*6a54128fSAndroid Build Coastguard Worker /* Don't endian-convert the magic food! */
3777*6a54128fSAndroid Build Coastguard Worker memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
3778*6a54128fSAndroid Build Coastguard Worker if (write(tdb->fd, newdb, size) != size) {
3779*6a54128fSAndroid Build Coastguard Worker ret = -1;
3780*6a54128fSAndroid Build Coastguard Worker } else {
3781*6a54128fSAndroid Build Coastguard Worker ret = 0;
3782*6a54128fSAndroid Build Coastguard Worker }
3783*6a54128fSAndroid Build Coastguard Worker
3784*6a54128fSAndroid Build Coastguard Worker fail:
3785*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(newdb);
3786*6a54128fSAndroid Build Coastguard Worker return ret;
3787*6a54128fSAndroid Build Coastguard Worker }
3788*6a54128fSAndroid Build Coastguard Worker
3789*6a54128fSAndroid Build Coastguard Worker
3790*6a54128fSAndroid Build Coastguard Worker
tdb_already_open(dev_t device,ino_t ino)3791*6a54128fSAndroid Build Coastguard Worker static int tdb_already_open(dev_t device,
3792*6a54128fSAndroid Build Coastguard Worker ino_t ino)
3793*6a54128fSAndroid Build Coastguard Worker {
3794*6a54128fSAndroid Build Coastguard Worker struct tdb_context *i;
3795*6a54128fSAndroid Build Coastguard Worker
3796*6a54128fSAndroid Build Coastguard Worker for (i = tdbs; i; i = i->next) {
3797*6a54128fSAndroid Build Coastguard Worker if (i->device == device && i->inode == ino) {
3798*6a54128fSAndroid Build Coastguard Worker return 1;
3799*6a54128fSAndroid Build Coastguard Worker }
3800*6a54128fSAndroid Build Coastguard Worker }
3801*6a54128fSAndroid Build Coastguard Worker
3802*6a54128fSAndroid Build Coastguard Worker return 0;
3803*6a54128fSAndroid Build Coastguard Worker }
3804*6a54128fSAndroid Build Coastguard Worker
3805*6a54128fSAndroid Build Coastguard Worker /* open the database, creating it if necessary
3806*6a54128fSAndroid Build Coastguard Worker
3807*6a54128fSAndroid Build Coastguard Worker The open_flags and mode are passed straight to the open call on the
3808*6a54128fSAndroid Build Coastguard Worker database file. A flags value of O_WRONLY is invalid. The hash size
3809*6a54128fSAndroid Build Coastguard Worker is advisory, use zero for a default value.
3810*6a54128fSAndroid Build Coastguard Worker
3811*6a54128fSAndroid Build Coastguard Worker Return is NULL on error, in which case errno is also set. Don't
3812*6a54128fSAndroid Build Coastguard Worker try to call tdb_error or tdb_errname, just do strerror(errno).
3813*6a54128fSAndroid Build Coastguard Worker
3814*6a54128fSAndroid Build Coastguard Worker @param name may be NULL for internal databases. */
tdb_open(const char * name,int hash_size,int tdb_flags,int open_flags,mode_t mode)3815*6a54128fSAndroid Build Coastguard Worker struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
3816*6a54128fSAndroid Build Coastguard Worker int open_flags, mode_t mode)
3817*6a54128fSAndroid Build Coastguard Worker {
3818*6a54128fSAndroid Build Coastguard Worker return tdb_open_ex(name, hash_size, tdb_flags, open_flags, mode, NULL, NULL);
3819*6a54128fSAndroid Build Coastguard Worker }
3820*6a54128fSAndroid Build Coastguard Worker
3821*6a54128fSAndroid Build Coastguard Worker /* a default logging function */
3822*6a54128fSAndroid Build Coastguard Worker static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
null_log_fn(struct tdb_context * tdb EXT2FS_ATTR ((unused)),enum tdb_debug_level level EXT2FS_ATTR ((unused)),const char * fmt EXT2FS_ATTR ((unused)),...)3823*6a54128fSAndroid Build Coastguard Worker static void null_log_fn(struct tdb_context *tdb EXT2FS_ATTR((unused)),
3824*6a54128fSAndroid Build Coastguard Worker enum tdb_debug_level level EXT2FS_ATTR((unused)),
3825*6a54128fSAndroid Build Coastguard Worker const char *fmt EXT2FS_ATTR((unused)), ...)
3826*6a54128fSAndroid Build Coastguard Worker {
3827*6a54128fSAndroid Build Coastguard Worker }
3828*6a54128fSAndroid Build Coastguard Worker
3829*6a54128fSAndroid Build Coastguard Worker
tdb_open_ex(const char * name,int hash_size,int tdb_flags,int open_flags,mode_t mode,const struct tdb_logging_context * log_ctx,tdb_hash_func hash_fn)3830*6a54128fSAndroid Build Coastguard Worker struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
3831*6a54128fSAndroid Build Coastguard Worker int open_flags, mode_t mode,
3832*6a54128fSAndroid Build Coastguard Worker const struct tdb_logging_context *log_ctx,
3833*6a54128fSAndroid Build Coastguard Worker tdb_hash_func hash_fn)
3834*6a54128fSAndroid Build Coastguard Worker {
3835*6a54128fSAndroid Build Coastguard Worker struct tdb_context *tdb;
3836*6a54128fSAndroid Build Coastguard Worker struct stat st;
3837*6a54128fSAndroid Build Coastguard Worker int rev = 0, locked = 0;
3838*6a54128fSAndroid Build Coastguard Worker unsigned char *vp;
3839*6a54128fSAndroid Build Coastguard Worker u32 vertest;
3840*6a54128fSAndroid Build Coastguard Worker
3841*6a54128fSAndroid Build Coastguard Worker if (!(tdb = (struct tdb_context *)calloc(1, sizeof *tdb))) {
3842*6a54128fSAndroid Build Coastguard Worker /* Can't log this */
3843*6a54128fSAndroid Build Coastguard Worker errno = ENOMEM;
3844*6a54128fSAndroid Build Coastguard Worker goto fail;
3845*6a54128fSAndroid Build Coastguard Worker }
3846*6a54128fSAndroid Build Coastguard Worker tdb_io_init(tdb);
3847*6a54128fSAndroid Build Coastguard Worker tdb->fd = -1;
3848*6a54128fSAndroid Build Coastguard Worker tdb->name = NULL;
3849*6a54128fSAndroid Build Coastguard Worker tdb->map_ptr = NULL;
3850*6a54128fSAndroid Build Coastguard Worker tdb->flags = tdb_flags;
3851*6a54128fSAndroid Build Coastguard Worker tdb->open_flags = open_flags;
3852*6a54128fSAndroid Build Coastguard Worker if (log_ctx) {
3853*6a54128fSAndroid Build Coastguard Worker tdb->log = *log_ctx;
3854*6a54128fSAndroid Build Coastguard Worker } else {
3855*6a54128fSAndroid Build Coastguard Worker tdb->log.log_fn = null_log_fn;
3856*6a54128fSAndroid Build Coastguard Worker tdb->log.log_private = NULL;
3857*6a54128fSAndroid Build Coastguard Worker }
3858*6a54128fSAndroid Build Coastguard Worker tdb->hash_fn = hash_fn ? hash_fn : default_tdb_hash;
3859*6a54128fSAndroid Build Coastguard Worker
3860*6a54128fSAndroid Build Coastguard Worker /* cache the page size */
3861*6a54128fSAndroid Build Coastguard Worker tdb->page_size = sysconf(_SC_PAGESIZE);
3862*6a54128fSAndroid Build Coastguard Worker if (tdb->page_size <= 0) {
3863*6a54128fSAndroid Build Coastguard Worker tdb->page_size = 0x2000;
3864*6a54128fSAndroid Build Coastguard Worker }
3865*6a54128fSAndroid Build Coastguard Worker
3866*6a54128fSAndroid Build Coastguard Worker if ((open_flags & O_ACCMODE) == O_WRONLY) {
3867*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: can't open tdb %s write-only\n",
3868*6a54128fSAndroid Build Coastguard Worker name));
3869*6a54128fSAndroid Build Coastguard Worker errno = EINVAL;
3870*6a54128fSAndroid Build Coastguard Worker goto fail;
3871*6a54128fSAndroid Build Coastguard Worker }
3872*6a54128fSAndroid Build Coastguard Worker
3873*6a54128fSAndroid Build Coastguard Worker if (hash_size == 0)
3874*6a54128fSAndroid Build Coastguard Worker hash_size = DEFAULT_HASH_SIZE;
3875*6a54128fSAndroid Build Coastguard Worker if ((open_flags & O_ACCMODE) == O_RDONLY) {
3876*6a54128fSAndroid Build Coastguard Worker tdb->read_only = 1;
3877*6a54128fSAndroid Build Coastguard Worker /* read only databases don't do locking or clear if first */
3878*6a54128fSAndroid Build Coastguard Worker tdb->flags |= TDB_NOLOCK;
3879*6a54128fSAndroid Build Coastguard Worker tdb->flags &= ~TDB_CLEAR_IF_FIRST;
3880*6a54128fSAndroid Build Coastguard Worker }
3881*6a54128fSAndroid Build Coastguard Worker
3882*6a54128fSAndroid Build Coastguard Worker /* internal databases don't mmap or lock, and start off cleared */
3883*6a54128fSAndroid Build Coastguard Worker if (tdb->flags & TDB_INTERNAL) {
3884*6a54128fSAndroid Build Coastguard Worker tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
3885*6a54128fSAndroid Build Coastguard Worker tdb->flags &= ~TDB_CLEAR_IF_FIRST;
3886*6a54128fSAndroid Build Coastguard Worker if (tdb_new_database(tdb, hash_size) != 0) {
3887*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: tdb_new_database failed!"));
3888*6a54128fSAndroid Build Coastguard Worker goto fail;
3889*6a54128fSAndroid Build Coastguard Worker }
3890*6a54128fSAndroid Build Coastguard Worker goto internal;
3891*6a54128fSAndroid Build Coastguard Worker }
3892*6a54128fSAndroid Build Coastguard Worker
3893*6a54128fSAndroid Build Coastguard Worker if ((tdb->fd = open(name, open_flags, mode)) == -1) {
3894*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_open_ex: could not open file %s: %s\n",
3895*6a54128fSAndroid Build Coastguard Worker name, strerror(errno)));
3896*6a54128fSAndroid Build Coastguard Worker goto fail; /* errno set by open(2) */
3897*6a54128fSAndroid Build Coastguard Worker }
3898*6a54128fSAndroid Build Coastguard Worker
3899*6a54128fSAndroid Build Coastguard Worker /* ensure there is only one process initialising at once */
3900*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0, 1) == -1) {
3901*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to get global lock on %s: %s\n",
3902*6a54128fSAndroid Build Coastguard Worker name, strerror(errno)));
3903*6a54128fSAndroid Build Coastguard Worker goto fail; /* errno set by tdb_brlock */
3904*6a54128fSAndroid Build Coastguard Worker }
3905*6a54128fSAndroid Build Coastguard Worker
3906*6a54128fSAndroid Build Coastguard Worker /* we need to zero database if we are the only one with it open */
3907*6a54128fSAndroid Build Coastguard Worker if ((tdb_flags & TDB_CLEAR_IF_FIRST) &&
3908*6a54128fSAndroid Build Coastguard Worker (locked = (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_WRLCK, F_SETLK, 0, 1) == 0))) {
3909*6a54128fSAndroid Build Coastguard Worker open_flags |= O_CREAT;
3910*6a54128fSAndroid Build Coastguard Worker if (ftruncate(tdb->fd, 0) == -1) {
3911*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_open_ex: "
3912*6a54128fSAndroid Build Coastguard Worker "failed to truncate %s: %s\n",
3913*6a54128fSAndroid Build Coastguard Worker name, strerror(errno)));
3914*6a54128fSAndroid Build Coastguard Worker goto fail; /* errno set by ftruncate */
3915*6a54128fSAndroid Build Coastguard Worker }
3916*6a54128fSAndroid Build Coastguard Worker }
3917*6a54128fSAndroid Build Coastguard Worker
3918*6a54128fSAndroid Build Coastguard Worker if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
3919*6a54128fSAndroid Build Coastguard Worker || memcmp(tdb->header.magic_food, TDB_MAGIC_FOOD,
3920*6a54128fSAndroid Build Coastguard Worker sizeof(TDB_MAGIC_FOOD)) != 0
3921*6a54128fSAndroid Build Coastguard Worker || (tdb->header.version != TDB_VERSION
3922*6a54128fSAndroid Build Coastguard Worker && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION))))) {
3923*6a54128fSAndroid Build Coastguard Worker /* its not a valid database - possibly initialise it */
3924*6a54128fSAndroid Build Coastguard Worker if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) {
3925*6a54128fSAndroid Build Coastguard Worker errno = EIO; /* ie bad format or something */
3926*6a54128fSAndroid Build Coastguard Worker goto fail;
3927*6a54128fSAndroid Build Coastguard Worker }
3928*6a54128fSAndroid Build Coastguard Worker rev = (tdb->flags & TDB_CONVERT);
3929*6a54128fSAndroid Build Coastguard Worker }
3930*6a54128fSAndroid Build Coastguard Worker vp = (unsigned char *)&tdb->header.version;
3931*6a54128fSAndroid Build Coastguard Worker vertest = (((u32)vp[0]) << 24) | (((u32)vp[1]) << 16) |
3932*6a54128fSAndroid Build Coastguard Worker (((u32)vp[2]) << 8) | (u32)vp[3];
3933*6a54128fSAndroid Build Coastguard Worker tdb->flags |= (vertest==TDB_VERSION) ? TDB_BIGENDIAN : 0;
3934*6a54128fSAndroid Build Coastguard Worker if (!rev)
3935*6a54128fSAndroid Build Coastguard Worker tdb->flags &= ~TDB_CONVERT;
3936*6a54128fSAndroid Build Coastguard Worker else {
3937*6a54128fSAndroid Build Coastguard Worker tdb->flags |= TDB_CONVERT;
3938*6a54128fSAndroid Build Coastguard Worker tdb_convert(&tdb->header, sizeof(tdb->header));
3939*6a54128fSAndroid Build Coastguard Worker }
3940*6a54128fSAndroid Build Coastguard Worker if (fstat(tdb->fd, &st) == -1)
3941*6a54128fSAndroid Build Coastguard Worker goto fail;
3942*6a54128fSAndroid Build Coastguard Worker
3943*6a54128fSAndroid Build Coastguard Worker if (tdb->header.rwlocks != 0) {
3944*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: spinlocks no longer supported\n"));
3945*6a54128fSAndroid Build Coastguard Worker goto fail;
3946*6a54128fSAndroid Build Coastguard Worker }
3947*6a54128fSAndroid Build Coastguard Worker
3948*6a54128fSAndroid Build Coastguard Worker /* Is it already in the open list? If so, fail. */
3949*6a54128fSAndroid Build Coastguard Worker if (tdb_already_open(st.st_dev, st.st_ino)) {
3950*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: "
3951*6a54128fSAndroid Build Coastguard Worker "%s (%d,%d) is already open in this process\n",
3952*6a54128fSAndroid Build Coastguard Worker name, (int)st.st_dev, (int)st.st_ino));
3953*6a54128fSAndroid Build Coastguard Worker errno = EBUSY;
3954*6a54128fSAndroid Build Coastguard Worker goto fail;
3955*6a54128fSAndroid Build Coastguard Worker }
3956*6a54128fSAndroid Build Coastguard Worker
3957*6a54128fSAndroid Build Coastguard Worker if (!(tdb->name = (char *)strdup(name))) {
3958*6a54128fSAndroid Build Coastguard Worker errno = ENOMEM;
3959*6a54128fSAndroid Build Coastguard Worker goto fail;
3960*6a54128fSAndroid Build Coastguard Worker }
3961*6a54128fSAndroid Build Coastguard Worker
3962*6a54128fSAndroid Build Coastguard Worker tdb->map_size = st.st_size;
3963*6a54128fSAndroid Build Coastguard Worker tdb->device = st.st_dev;
3964*6a54128fSAndroid Build Coastguard Worker tdb->inode = st.st_ino;
3965*6a54128fSAndroid Build Coastguard Worker tdb->max_dead_records = 0;
3966*6a54128fSAndroid Build Coastguard Worker tdb_mmap(tdb);
3967*6a54128fSAndroid Build Coastguard Worker if (locked) {
3968*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_UNLCK, F_SETLK, 0, 1) == -1) {
3969*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: "
3970*6a54128fSAndroid Build Coastguard Worker "failed to take ACTIVE_LOCK on %s: %s\n",
3971*6a54128fSAndroid Build Coastguard Worker name, strerror(errno)));
3972*6a54128fSAndroid Build Coastguard Worker goto fail;
3973*6a54128fSAndroid Build Coastguard Worker }
3974*6a54128fSAndroid Build Coastguard Worker
3975*6a54128fSAndroid Build Coastguard Worker }
3976*6a54128fSAndroid Build Coastguard Worker
3977*6a54128fSAndroid Build Coastguard Worker /* We always need to do this if the CLEAR_IF_FIRST flag is set, even if
3978*6a54128fSAndroid Build Coastguard Worker we didn't get the initial exclusive lock as we need to let all other
3979*6a54128fSAndroid Build Coastguard Worker users know we're using it. */
3980*6a54128fSAndroid Build Coastguard Worker
3981*6a54128fSAndroid Build Coastguard Worker if (tdb_flags & TDB_CLEAR_IF_FIRST) {
3982*6a54128fSAndroid Build Coastguard Worker /* leave this lock in place to indicate it's in use */
3983*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0, 1) == -1)
3984*6a54128fSAndroid Build Coastguard Worker goto fail;
3985*6a54128fSAndroid Build Coastguard Worker }
3986*6a54128fSAndroid Build Coastguard Worker
3987*6a54128fSAndroid Build Coastguard Worker /* if needed, run recovery */
3988*6a54128fSAndroid Build Coastguard Worker if (tdb_transaction_recover(tdb) == -1) {
3989*6a54128fSAndroid Build Coastguard Worker goto fail;
3990*6a54128fSAndroid Build Coastguard Worker }
3991*6a54128fSAndroid Build Coastguard Worker
3992*6a54128fSAndroid Build Coastguard Worker internal:
3993*6a54128fSAndroid Build Coastguard Worker /* Internal (memory-only) databases skip all the code above to
3994*6a54128fSAndroid Build Coastguard Worker * do with disk files, and resume here by releasing their
3995*6a54128fSAndroid Build Coastguard Worker * global lock and hooking into the active list. */
3996*6a54128fSAndroid Build Coastguard Worker if (tdb->methods->tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1) == -1)
3997*6a54128fSAndroid Build Coastguard Worker goto fail;
3998*6a54128fSAndroid Build Coastguard Worker tdb->next = tdbs;
3999*6a54128fSAndroid Build Coastguard Worker tdbs = tdb;
4000*6a54128fSAndroid Build Coastguard Worker return tdb;
4001*6a54128fSAndroid Build Coastguard Worker
4002*6a54128fSAndroid Build Coastguard Worker fail:
4003*6a54128fSAndroid Build Coastguard Worker { int save_errno = errno;
4004*6a54128fSAndroid Build Coastguard Worker
4005*6a54128fSAndroid Build Coastguard Worker if (!tdb)
4006*6a54128fSAndroid Build Coastguard Worker return NULL;
4007*6a54128fSAndroid Build Coastguard Worker
4008*6a54128fSAndroid Build Coastguard Worker if (tdb->map_ptr) {
4009*6a54128fSAndroid Build Coastguard Worker if (tdb->flags & TDB_INTERNAL)
4010*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb->map_ptr);
4011*6a54128fSAndroid Build Coastguard Worker else
4012*6a54128fSAndroid Build Coastguard Worker tdb_munmap(tdb);
4013*6a54128fSAndroid Build Coastguard Worker }
4014*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb->name);
4015*6a54128fSAndroid Build Coastguard Worker if (tdb->fd != -1)
4016*6a54128fSAndroid Build Coastguard Worker if (close(tdb->fd) != 0)
4017*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to close tdb->fd on error!\n"));
4018*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb);
4019*6a54128fSAndroid Build Coastguard Worker errno = save_errno;
4020*6a54128fSAndroid Build Coastguard Worker return NULL;
4021*6a54128fSAndroid Build Coastguard Worker }
4022*6a54128fSAndroid Build Coastguard Worker }
4023*6a54128fSAndroid Build Coastguard Worker
4024*6a54128fSAndroid Build Coastguard Worker /*
4025*6a54128fSAndroid Build Coastguard Worker * Set the maximum number of dead records per hash chain
4026*6a54128fSAndroid Build Coastguard Worker */
4027*6a54128fSAndroid Build Coastguard Worker
tdb_set_max_dead(struct tdb_context * tdb,int max_dead)4028*6a54128fSAndroid Build Coastguard Worker void tdb_set_max_dead(struct tdb_context *tdb, int max_dead)
4029*6a54128fSAndroid Build Coastguard Worker {
4030*6a54128fSAndroid Build Coastguard Worker tdb->max_dead_records = max_dead;
4031*6a54128fSAndroid Build Coastguard Worker }
4032*6a54128fSAndroid Build Coastguard Worker
4033*6a54128fSAndroid Build Coastguard Worker /**
4034*6a54128fSAndroid Build Coastguard Worker * Close a database.
4035*6a54128fSAndroid Build Coastguard Worker *
4036*6a54128fSAndroid Build Coastguard Worker * @returns -1 for error; 0 for success.
4037*6a54128fSAndroid Build Coastguard Worker **/
tdb_close(struct tdb_context * tdb)4038*6a54128fSAndroid Build Coastguard Worker int tdb_close(struct tdb_context *tdb)
4039*6a54128fSAndroid Build Coastguard Worker {
4040*6a54128fSAndroid Build Coastguard Worker struct tdb_context **i;
4041*6a54128fSAndroid Build Coastguard Worker int ret = 0;
4042*6a54128fSAndroid Build Coastguard Worker
4043*6a54128fSAndroid Build Coastguard Worker if (tdb->transaction) {
4044*6a54128fSAndroid Build Coastguard Worker tdb_transaction_cancel(tdb);
4045*6a54128fSAndroid Build Coastguard Worker }
4046*6a54128fSAndroid Build Coastguard Worker
4047*6a54128fSAndroid Build Coastguard Worker if (tdb->map_ptr) {
4048*6a54128fSAndroid Build Coastguard Worker if (tdb->flags & TDB_INTERNAL)
4049*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb->map_ptr);
4050*6a54128fSAndroid Build Coastguard Worker else
4051*6a54128fSAndroid Build Coastguard Worker tdb_munmap(tdb);
4052*6a54128fSAndroid Build Coastguard Worker }
4053*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb->name);
4054*6a54128fSAndroid Build Coastguard Worker if (tdb->fd != -1)
4055*6a54128fSAndroid Build Coastguard Worker ret = close(tdb->fd);
4056*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb->lockrecs);
4057*6a54128fSAndroid Build Coastguard Worker
4058*6a54128fSAndroid Build Coastguard Worker /* Remove from contexts list */
4059*6a54128fSAndroid Build Coastguard Worker for (i = &tdbs; *i; i = &(*i)->next) {
4060*6a54128fSAndroid Build Coastguard Worker if (*i == tdb) {
4061*6a54128fSAndroid Build Coastguard Worker *i = tdb->next;
4062*6a54128fSAndroid Build Coastguard Worker break;
4063*6a54128fSAndroid Build Coastguard Worker }
4064*6a54128fSAndroid Build Coastguard Worker }
4065*6a54128fSAndroid Build Coastguard Worker
4066*6a54128fSAndroid Build Coastguard Worker memset(tdb, 0, sizeof(*tdb));
4067*6a54128fSAndroid Build Coastguard Worker SAFE_FREE(tdb);
4068*6a54128fSAndroid Build Coastguard Worker
4069*6a54128fSAndroid Build Coastguard Worker return ret;
4070*6a54128fSAndroid Build Coastguard Worker }
4071*6a54128fSAndroid Build Coastguard Worker
4072*6a54128fSAndroid Build Coastguard Worker /* register a logging function */
tdb_set_logging_function(struct tdb_context * tdb,const struct tdb_logging_context * log_ctx)4073*6a54128fSAndroid Build Coastguard Worker void tdb_set_logging_function(struct tdb_context *tdb,
4074*6a54128fSAndroid Build Coastguard Worker const struct tdb_logging_context *log_ctx)
4075*6a54128fSAndroid Build Coastguard Worker {
4076*6a54128fSAndroid Build Coastguard Worker tdb->log = *log_ctx;
4077*6a54128fSAndroid Build Coastguard Worker }
4078*6a54128fSAndroid Build Coastguard Worker
tdb_get_logging_private(struct tdb_context * tdb)4079*6a54128fSAndroid Build Coastguard Worker void *tdb_get_logging_private(struct tdb_context *tdb)
4080*6a54128fSAndroid Build Coastguard Worker {
4081*6a54128fSAndroid Build Coastguard Worker return tdb->log.log_private;
4082*6a54128fSAndroid Build Coastguard Worker }
4083*6a54128fSAndroid Build Coastguard Worker
4084*6a54128fSAndroid Build Coastguard Worker /* reopen a tdb - this can be used after a fork to ensure that we have an independent
4085*6a54128fSAndroid Build Coastguard Worker seek pointer from our parent and to re-establish locks */
tdb_reopen(struct tdb_context * tdb)4086*6a54128fSAndroid Build Coastguard Worker int tdb_reopen(struct tdb_context *tdb)
4087*6a54128fSAndroid Build Coastguard Worker {
4088*6a54128fSAndroid Build Coastguard Worker struct stat st;
4089*6a54128fSAndroid Build Coastguard Worker
4090*6a54128fSAndroid Build Coastguard Worker if (tdb->flags & TDB_INTERNAL) {
4091*6a54128fSAndroid Build Coastguard Worker return 0; /* Nothing to do. */
4092*6a54128fSAndroid Build Coastguard Worker }
4093*6a54128fSAndroid Build Coastguard Worker
4094*6a54128fSAndroid Build Coastguard Worker if (tdb->num_locks != 0 || tdb->global_lock.count) {
4095*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed with locks held\n"));
4096*6a54128fSAndroid Build Coastguard Worker goto fail;
4097*6a54128fSAndroid Build Coastguard Worker }
4098*6a54128fSAndroid Build Coastguard Worker
4099*6a54128fSAndroid Build Coastguard Worker if (tdb->transaction != 0) {
4100*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed inside a transaction\n"));
4101*6a54128fSAndroid Build Coastguard Worker goto fail;
4102*6a54128fSAndroid Build Coastguard Worker }
4103*6a54128fSAndroid Build Coastguard Worker
4104*6a54128fSAndroid Build Coastguard Worker if (tdb_munmap(tdb) != 0) {
4105*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: munmap failed (%s)\n", strerror(errno)));
4106*6a54128fSAndroid Build Coastguard Worker goto fail;
4107*6a54128fSAndroid Build Coastguard Worker }
4108*6a54128fSAndroid Build Coastguard Worker if (close(tdb->fd) != 0)
4109*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: WARNING closing tdb->fd failed!\n"));
4110*6a54128fSAndroid Build Coastguard Worker tdb->fd = open(tdb->name, tdb->open_flags & ~(O_CREAT|O_TRUNC), 0);
4111*6a54128fSAndroid Build Coastguard Worker if (tdb->fd == -1) {
4112*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: open failed (%s)\n", strerror(errno)));
4113*6a54128fSAndroid Build Coastguard Worker goto fail;
4114*6a54128fSAndroid Build Coastguard Worker }
4115*6a54128fSAndroid Build Coastguard Worker if ((tdb->flags & TDB_CLEAR_IF_FIRST) &&
4116*6a54128fSAndroid Build Coastguard Worker (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0, 1) == -1)) {
4117*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: failed to obtain active lock\n"));
4118*6a54128fSAndroid Build Coastguard Worker goto fail;
4119*6a54128fSAndroid Build Coastguard Worker }
4120*6a54128fSAndroid Build Coastguard Worker if (fstat(tdb->fd, &st) != 0) {
4121*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: fstat failed (%s)\n", strerror(errno)));
4122*6a54128fSAndroid Build Coastguard Worker goto fail;
4123*6a54128fSAndroid Build Coastguard Worker }
4124*6a54128fSAndroid Build Coastguard Worker if (st.st_ino != tdb->inode || st.st_dev != tdb->device) {
4125*6a54128fSAndroid Build Coastguard Worker TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: file dev/inode has changed!\n"));
4126*6a54128fSAndroid Build Coastguard Worker goto fail;
4127*6a54128fSAndroid Build Coastguard Worker }
4128*6a54128fSAndroid Build Coastguard Worker tdb_mmap(tdb);
4129*6a54128fSAndroid Build Coastguard Worker
4130*6a54128fSAndroid Build Coastguard Worker return 0;
4131*6a54128fSAndroid Build Coastguard Worker
4132*6a54128fSAndroid Build Coastguard Worker fail:
4133*6a54128fSAndroid Build Coastguard Worker tdb_close(tdb);
4134*6a54128fSAndroid Build Coastguard Worker return -1;
4135*6a54128fSAndroid Build Coastguard Worker }
4136*6a54128fSAndroid Build Coastguard Worker
4137*6a54128fSAndroid Build Coastguard Worker /* reopen all tdb's */
tdb_reopen_all(int parent_longlived)4138*6a54128fSAndroid Build Coastguard Worker int tdb_reopen_all(int parent_longlived)
4139*6a54128fSAndroid Build Coastguard Worker {
4140*6a54128fSAndroid Build Coastguard Worker struct tdb_context *tdb;
4141*6a54128fSAndroid Build Coastguard Worker
4142*6a54128fSAndroid Build Coastguard Worker for (tdb=tdbs; tdb; tdb = tdb->next) {
4143*6a54128fSAndroid Build Coastguard Worker /*
4144*6a54128fSAndroid Build Coastguard Worker * If the parent is longlived (ie. a
4145*6a54128fSAndroid Build Coastguard Worker * parent daemon architecture), we know
4146*6a54128fSAndroid Build Coastguard Worker * it will keep it's active lock on a
4147*6a54128fSAndroid Build Coastguard Worker * tdb opened with CLEAR_IF_FIRST. Thus
4148*6a54128fSAndroid Build Coastguard Worker * for child processes we don't have to
4149*6a54128fSAndroid Build Coastguard Worker * add an active lock. This is essential
4150*6a54128fSAndroid Build Coastguard Worker * to improve performance on systems that
4151*6a54128fSAndroid Build Coastguard Worker * keep POSIX locks as a non-scalable data
4152*6a54128fSAndroid Build Coastguard Worker * structure in the kernel.
4153*6a54128fSAndroid Build Coastguard Worker */
4154*6a54128fSAndroid Build Coastguard Worker if (parent_longlived) {
4155*6a54128fSAndroid Build Coastguard Worker /* Ensure no clear-if-first. */
4156*6a54128fSAndroid Build Coastguard Worker tdb->flags &= ~TDB_CLEAR_IF_FIRST;
4157*6a54128fSAndroid Build Coastguard Worker }
4158*6a54128fSAndroid Build Coastguard Worker
4159*6a54128fSAndroid Build Coastguard Worker if (tdb_reopen(tdb) != 0)
4160*6a54128fSAndroid Build Coastguard Worker return -1;
4161*6a54128fSAndroid Build Coastguard Worker }
4162*6a54128fSAndroid Build Coastguard Worker
4163*6a54128fSAndroid Build Coastguard Worker return 0;
4164*6a54128fSAndroid Build Coastguard Worker }
4165*6a54128fSAndroid Build Coastguard Worker
4166*6a54128fSAndroid Build Coastguard Worker /**
4167*6a54128fSAndroid Build Coastguard Worker * Flush a database file from the page cache.
4168*6a54128fSAndroid Build Coastguard Worker **/
tdb_flush(struct tdb_context * tdb)4169*6a54128fSAndroid Build Coastguard Worker int tdb_flush(struct tdb_context *tdb)
4170*6a54128fSAndroid Build Coastguard Worker {
4171*6a54128fSAndroid Build Coastguard Worker if (tdb->fd != -1)
4172*6a54128fSAndroid Build Coastguard Worker return fsync(tdb->fd);
4173*6a54128fSAndroid Build Coastguard Worker return 0;
4174*6a54128fSAndroid Build Coastguard Worker }
4175