r10492: work around a bug in solaris which cases lock upgrades to fail with
[gd/samba-autobuild/.git] / source4 / 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         }
48
49         if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
50                 tdb->ecode = TDB_ERR_RDONLY;
51                 return -1;
52         }
53
54         fl.l_type = rw_type;
55         fl.l_whence = SEEK_SET;
56         fl.l_start = offset;
57         fl.l_len = len;
58         fl.l_pid = 0;
59
60         do {
61                 ret = fcntl(tdb->fd,lck_type,&fl);
62         } while (ret == -1 && errno == EINTR);
63
64         if (ret == -1) {
65                 /* Generic lock error. errno set by fcntl.
66                  * EAGAIN is an expected return from non-blocking
67                  * locks. */
68                 if (errno != EAGAIN) {
69                         TDB_LOG((tdb, 5, "tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d: %s\n", 
70                                  tdb->fd, offset, rw_type, lck_type, len,
71                                  strerror(errno)));
72                 } else if (!probe && lck_type != F_SETLK) {
73                         /* Ensure error code is set for log fun to examine. */
74                         tdb->ecode = TDB_ERR_LOCK;
75                         TDB_LOG((tdb, 5,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d\n", 
76                                  tdb->fd, offset, rw_type, lck_type));
77                 }
78                 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
79         }
80         return 0;
81 }
82
83
84 /*
85   upgrade a read lock to a write lock. This needs to be handled in a
86   special way as some OSes (such as solaris) have too conservative
87   deadlock detection and claim a deadlock when progress can be
88   made. For those OSes we may loop for a while.  
89 */
90 int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len)
91 {
92         int count = 1000;
93         while (count--) {
94                 struct timeval tv;
95                 if (tdb_brlock_len(tdb, offset, F_WRLCK, F_SETLKW, 1, len) == 0) {
96                         return 0;
97                 }
98                 if (errno != EDEADLK) {
99                         break;
100                 }
101                 /* sleep for as short a time as we can - more portable than usleep() */
102                 tv.tv_sec = 0;
103                 tv.tv_usec = 1;
104                 select(0, NULL, NULL, NULL, &tv);
105         }
106         return -1;
107 }
108
109
110 /* a byte range locking function - return 0 on success
111    this functions locks/unlocks 1 byte at the specified offset.
112
113    On error, errno is also set so that errors are passed back properly
114    through tdb_open(). */
115 int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset, 
116                int rw_type, int lck_type, int probe)
117 {
118         return tdb_brlock_len(tdb, offset, rw_type, lck_type, probe, 1);
119 }
120
121 /* lock a list in the database. list -1 is the alloc list */
122 int tdb_lock(struct tdb_context *tdb, int list, int ltype)
123 {
124         if (list < -1 || list >= (int)tdb->header.hash_size) {
125                 TDB_LOG((tdb, 0,"tdb_lock: invalid list %d for ltype=%d\n", 
126                            list, ltype));
127                 return -1;
128         }
129         if (tdb->flags & TDB_NOLOCK)
130                 return 0;
131
132         /* Since fcntl locks don't nest, we do a lock for the first one,
133            and simply bump the count for future ones */
134         if (tdb->locked[list+1].count == 0) {
135                 if (tdb->methods->tdb_brlock(tdb,FREELIST_TOP+4*list,ltype,F_SETLKW, 0)) {
136                         TDB_LOG((tdb, 0,"tdb_lock failed on list %d ltype=%d (%s)\n", 
137                                  list, ltype, strerror(errno)));
138                         return -1;
139                 }
140                 tdb->locked[list+1].ltype = ltype;
141                 tdb->num_locks++;
142         }
143         tdb->locked[list+1].count++;
144         return 0;
145 }
146
147 /* unlock the database: returns void because it's too late for errors. */
148         /* changed to return int it may be interesting to know there
149            has been an error  --simo */
150 int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
151 {
152         int ret = -1;
153
154         if (tdb->flags & TDB_NOLOCK)
155                 return 0;
156
157         /* Sanity checks */
158         if (list < -1 || list >= (int)tdb->header.hash_size) {
159                 TDB_LOG((tdb, 0, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size));
160                 return ret;
161         }
162
163         if (tdb->locked[list+1].count==0) {
164                 TDB_LOG((tdb, 0, "tdb_unlock: count is 0\n"));
165                 return ret;
166         }
167
168         if (tdb->locked[list+1].count == 1) {
169                 /* Down to last nested lock: unlock underneath */
170                 ret = tdb->methods->tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK, F_SETLKW, 0);
171                 tdb->num_locks--;
172         } else {
173                 ret = 0;
174         }
175         tdb->locked[list+1].count--;
176
177         if (ret)
178                 TDB_LOG((tdb, 0,"tdb_unlock: An error occurred unlocking!\n")); 
179         return ret;
180 }
181
182
183
184 /* lock/unlock entire database */
185 int tdb_lockall(struct tdb_context *tdb)
186 {
187         u32 i;
188
189         /* There are no locks on read-only dbs */
190         if (tdb->read_only || tdb->traverse_read)
191                 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
192         for (i = 0; i < tdb->header.hash_size; i++) 
193                 if (tdb_lock(tdb, i, F_WRLCK))
194                         break;
195
196         /* If error, release locks we have... */
197         if (i < tdb->header.hash_size) {
198                 u32 j;
199
200                 for ( j = 0; j < i; j++)
201                         tdb_unlock(tdb, j, F_WRLCK);
202                 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
203         }
204
205         return 0;
206 }
207 void tdb_unlockall(struct tdb_context *tdb)
208 {
209         u32 i;
210         for (i=0; i < tdb->header.hash_size; i++)
211                 tdb_unlock(tdb, i, F_WRLCK);
212 }
213
214 /* lock/unlock one hash chain. This is meant to be used to reduce
215    contention - it cannot guarantee how many records will be locked */
216 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
217 {
218         return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
219 }
220
221 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
222 {
223         return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
224 }
225
226 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
227 {
228         return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
229 }
230
231 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
232 {
233         return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
234 }
235
236
237
238 /* record lock stops delete underneath */
239 int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
240 {
241         return off ? tdb->methods->tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0) : 0;
242 }
243
244 /*
245   Write locks override our own fcntl readlocks, so check it here.
246   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
247   an error to fail to get the lock here.
248 */
249 int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
250 {
251         struct tdb_traverse_lock *i;
252         for (i = &tdb->travlocks; i; i = i->next)
253                 if (i->off == off)
254                         return -1;
255         return tdb->methods->tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1);
256 }
257
258 /*
259   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
260   an error to fail to get the lock here.
261 */
262 int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
263 {
264         return tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0);
265 }
266
267 /* fcntl locks don't stack: avoid unlocking someone else's */
268 int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
269 {
270         struct tdb_traverse_lock *i;
271         u32 count = 0;
272
273         if (off == 0)
274                 return 0;
275         for (i = &tdb->travlocks; i; i = i->next)
276                 if (i->off == off)
277                         count++;
278         return (count == 1 ? tdb->methods->tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0) : 0);
279 }