r10405: added transactions into tdb, and hook them into ldb. See my
[ira/wip.git] / source / lib / tdb / common / lock.c
1  /* 
2    Unix SMB/CIFS implementation.
3
4    trivial database library
5
6    Copyright (C) Andrew Tridgell              1999-2005
7    Copyright (C) Paul `Rusty' Russell              2000
8    Copyright (C) Jeremy Allison                    2000-2003
9    
10      ** NOTE! The following LGPL license applies to the tdb
11      ** library. This does NOT imply that all of Samba is released
12      ** under the LGPL
13    
14    This library is free software; you can redistribute it and/or
15    modify it under the terms of the GNU Lesser General Public
16    License as published by the Free Software Foundation; either
17    version 2 of the License, or (at your option) any later version.
18
19    This library is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    Lesser General Public License for more details.
23
24    You should have received a copy of the GNU Lesser General Public
25    License along with this library; if not, write to the Free Software
26    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 */
28
29 #include "tdb_private.h"
30
31 /* a byte range locking function - return 0 on success
32    this functions locks/unlocks 1 byte at the specified offset.
33
34    On error, errno is also set so that errors are passed back properly
35    through tdb_open(). 
36
37    note that a len of zero means lock to end of file
38 */
39 int tdb_brlock_len(struct tdb_context *tdb, tdb_off_t offset, 
40                    int rw_type, int lck_type, int probe, size_t len)
41 {
42         struct flock fl;
43         int ret;
44
45         if (tdb->flags & TDB_NOLOCK)
46                 return 0;
47         if ((rw_type == F_WRLCK) && (tdb->read_only)) {
48                 errno = EACCES;
49                 return -1;
50         }
51
52         fl.l_type = rw_type;
53         fl.l_whence = SEEK_SET;
54         fl.l_start = offset;
55         fl.l_len = len;
56         fl.l_pid = 0;
57
58         do {
59                 ret = fcntl(tdb->fd,lck_type,&fl);
60         } while (ret == -1 && errno == EINTR);
61
62         if (ret == -1) {
63                 /* Generic lock error. errno set by fcntl.
64                  * EAGAIN is an expected return from non-blocking
65                  * locks. */
66                 if (errno != EAGAIN) {
67                         TDB_LOG((tdb, 5, "tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d: %s\n", 
68                                  tdb->fd, offset, rw_type, lck_type, 
69                                  strerror(errno)));
70                 } else if (!probe && lck_type != F_SETLK) {
71                         /* Ensure error code is set for log fun to examine. */
72                         tdb->ecode = TDB_ERR_LOCK;
73                         TDB_LOG((tdb, 5,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d\n", 
74                                  tdb->fd, offset, rw_type, lck_type));
75                 }
76                 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
77         }
78         return 0;
79 }
80
81
82 /* a byte range locking function - return 0 on success
83    this functions locks/unlocks 1 byte at the specified offset.
84
85    On error, errno is also set so that errors are passed back properly
86    through tdb_open(). */
87 int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset, 
88                int rw_type, int lck_type, int probe)
89 {
90         return tdb_brlock_len(tdb, offset, rw_type, lck_type, probe, 1);
91 }
92
93 /* lock a list in the database. list -1 is the alloc list */
94 int tdb_lock(struct tdb_context *tdb, int list, int ltype)
95 {
96         if (list < -1 || list >= (int)tdb->header.hash_size) {
97                 TDB_LOG((tdb, 0,"tdb_lock: invalid list %d for ltype=%d\n", 
98                            list, ltype));
99                 return -1;
100         }
101         if (tdb->flags & TDB_NOLOCK)
102                 return 0;
103
104         /* Since fcntl locks don't nest, we do a lock for the first one,
105            and simply bump the count for future ones */
106         if (tdb->locked[list+1].count == 0) {
107                 if (tdb->methods->tdb_brlock(tdb,FREELIST_TOP+4*list,ltype,F_SETLKW, 0)) {
108                         TDB_LOG((tdb, 0,"tdb_lock failed on list %d ltype=%d (%s)\n", 
109                                  list, ltype, strerror(errno)));
110                         return -1;
111                 }
112                 tdb->locked[list+1].ltype = ltype;
113                 tdb->num_locks++;
114         }
115         tdb->locked[list+1].count++;
116         return 0;
117 }
118
119 /* unlock the database: returns void because it's too late for errors. */
120         /* changed to return int it may be interesting to know there
121            has been an error  --simo */
122 int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
123 {
124         int ret = -1;
125
126         if (tdb->flags & TDB_NOLOCK)
127                 return 0;
128
129         /* Sanity checks */
130         if (list < -1 || list >= (int)tdb->header.hash_size) {
131                 TDB_LOG((tdb, 0, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size));
132                 return ret;
133         }
134
135         if (tdb->locked[list+1].count==0) {
136                 TDB_LOG((tdb, 0, "tdb_unlock: count is 0\n"));
137                 return ret;
138         }
139
140         if (tdb->locked[list+1].count == 1) {
141                 /* Down to last nested lock: unlock underneath */
142                 ret = tdb->methods->tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK, F_SETLKW, 0);
143                 tdb->num_locks--;
144         } else {
145                 ret = 0;
146         }
147         tdb->locked[list+1].count--;
148
149         if (ret)
150                 TDB_LOG((tdb, 0,"tdb_unlock: An error occurred unlocking!\n")); 
151         return ret;
152 }
153
154
155
156 /* lock/unlock entire database */
157 int tdb_lockall(struct tdb_context *tdb)
158 {
159         u32 i;
160
161         /* There are no locks on read-only dbs */
162         if (tdb->read_only)
163                 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
164         for (i = 0; i < tdb->header.hash_size; i++) 
165                 if (tdb_lock(tdb, i, F_WRLCK))
166                         break;
167
168         /* If error, release locks we have... */
169         if (i < tdb->header.hash_size) {
170                 u32 j;
171
172                 for ( j = 0; j < i; j++)
173                         tdb_unlock(tdb, j, F_WRLCK);
174                 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
175         }
176
177         return 0;
178 }
179 void tdb_unlockall(struct tdb_context *tdb)
180 {
181         u32 i;
182         for (i=0; i < tdb->header.hash_size; i++)
183                 tdb_unlock(tdb, i, F_WRLCK);
184 }
185
186 /* lock/unlock one hash chain. This is meant to be used to reduce
187    contention - it cannot guarantee how many records will be locked */
188 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
189 {
190         return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
191 }
192
193 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
194 {
195         return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
196 }
197
198 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
199 {
200         return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
201 }
202
203 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
204 {
205         return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
206 }
207
208
209
210 /* record lock stops delete underneath */
211 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
212 {
213         return off ? tdb->methods->tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0) : 0;
214 }
215
216 /*
217   Write locks override our own fcntl readlocks, so check it here.
218   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
219   an error to fail to get the lock here.
220 */
221 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
222 {
223         struct tdb_traverse_lock *i;
224         for (i = &tdb->travlocks; i; i = i->next)
225                 if (i->off == off)
226                         return -1;
227         return tdb->methods->tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1);
228 }
229
230 /*
231   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
232   an error to fail to get the lock here.
233 */
234 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
235 {
236         return tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0);
237 }
238
239 /* fcntl locks don't stack: avoid unlocking someone else's */
240 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
241 {
242         struct tdb_traverse_lock *i;
243         u32 count = 0;
244
245         if (off == 0)
246                 return 0;
247         for (i = &tdb->travlocks; i; i = i->next)
248                 if (i->off == off)
249                         count++;
250         return (count == 1 ? tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0) : 0);
251 }