r15083: Using talloc with destructors is nice and all, but in this
[samba.git] / source3 / locking / brlock.c
1 /* 
2    Unix SMB/CIFS implementation.
3    byte range locking code
4    Updated to handle range splits/merges.
5
6    Copyright (C) Andrew Tridgell 1992-2000
7    Copyright (C) Jeremy Allison 1992-2000
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 /* This module implements a tdb based byte range locking service,
25    replacing the fcntl() based byte range locking previously
26    used. This allows us to provide the same semantics as NT */
27
28 #include "includes.h"
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_LOCKING
32
33 #define ZERO_ZERO 0
34
35 /* This contains elements that differentiate locks. The smbpid is a
36    client supplied pid, and is essentially the locking context for
37    this client */
38
39 struct lock_context {
40         uint16 smbpid;
41         uint16 tid;
42         struct process_id pid;
43 };
44
45 /* The data in brlock records is an unsorted linear array of these
46    records.  It is unnecessary to store the count as tdb provides the
47    size of the record */
48
49 struct lock_struct {
50         struct lock_context context;
51         br_off start;
52         br_off size;
53         int fnum;
54         enum brl_type lock_type;
55         enum brl_flavour lock_flav;
56 };
57
58 /* The open brlock.tdb database. */
59
60 static TDB_CONTEXT *tdb;
61
62 /****************************************************************************
63  Debug info at level 10 for lock struct.
64 ****************************************************************************/
65
66 static void print_lock_struct(unsigned int i, struct lock_struct *pls)
67 {
68         DEBUG(10,("[%u]: smbpid = %u, tid = %u, pid = %u, ",
69                         i,
70                         (unsigned int)pls->context.smbpid,
71                         (unsigned int)pls->context.tid,
72                         (unsigned int)procid_to_pid(&pls->context.pid) ));
73         
74         DEBUG(10,("start = %.0f, size = %.0f, fnum = %d, %s %s\n",
75                 (double)pls->start,
76                 (double)pls->size,
77                 pls->fnum,
78                 lock_type_name(pls->lock_type),
79                 lock_flav_name(pls->lock_flav) ));
80 }
81
82 /****************************************************************************
83  See if two locking contexts are equal.
84 ****************************************************************************/
85
86 static BOOL brl_same_context(const struct lock_context *ctx1, 
87                              const struct lock_context *ctx2)
88 {
89         return (procid_equal(&ctx1->pid, &ctx2->pid) &&
90                 (ctx1->smbpid == ctx2->smbpid) &&
91                 (ctx1->tid == ctx2->tid));
92 }
93
94 /****************************************************************************
95  See if lck1 and lck2 overlap.
96 ****************************************************************************/
97
98 static BOOL brl_overlap(const struct lock_struct *lck1,
99                         const struct lock_struct *lck2)
100 {
101         /* this extra check is not redundent - it copes with locks
102            that go beyond the end of 64 bit file space */
103         if (lck1->size != 0 &&
104             lck1->start == lck2->start &&
105             lck1->size == lck2->size) {
106                 return True;
107         }
108
109         if (lck1->start >= (lck2->start+lck2->size) ||
110             lck2->start >= (lck1->start+lck1->size)) {
111                 return False;
112         }
113         return True;
114 }
115
116 /****************************************************************************
117  See if lock2 can be added when lock1 is in place.
118 ****************************************************************************/
119
120 static BOOL brl_conflict(const struct lock_struct *lck1, 
121                          const struct lock_struct *lck2)
122 {
123         /* Ignore PENDING locks. */
124         if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
125                 return False;
126
127         /* Read locks never conflict. */
128         if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
129                 return False;
130         }
131
132         if (brl_same_context(&lck1->context, &lck2->context) &&
133             lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
134                 return False;
135         }
136
137         return brl_overlap(lck1, lck2);
138
139
140 /****************************************************************************
141  See if lock2 can be added when lock1 is in place - when both locks are POSIX
142  flavour. POSIX locks ignore fnum - they only care about dev/ino which we
143  know already match.
144 ****************************************************************************/
145
146 static BOOL brl_conflict_posix(const struct lock_struct *lck1, 
147                                 const struct lock_struct *lck2)
148 {
149 #if defined(DEVELOPER)
150         SMB_ASSERT(lck1->lock_flav == POSIX_LOCK);
151         SMB_ASSERT(lck2->lock_flav == POSIX_LOCK);
152 #endif
153
154         /* Ignore PENDING locks. */
155         if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
156                 return False;
157
158         /* Read locks never conflict. */
159         if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
160                 return False;
161         }
162
163         /* Locks on the same context con't conflict. Ignore fnum. */
164         if (brl_same_context(&lck1->context, &lck2->context)) {
165                 return False;
166         }
167
168         /* One is read, the other write, or the context is different,
169            do they overlap ? */
170         return brl_overlap(lck1, lck2);
171
172
173 #if ZERO_ZERO
174 static BOOL brl_conflict1(const struct lock_struct *lck1, 
175                          const struct lock_struct *lck2)
176 {
177         if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
178                 return False;
179
180         if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
181                 return False;
182         }
183
184         if (brl_same_context(&lck1->context, &lck2->context) &&
185             lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
186                 return False;
187         }
188
189         if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
190                 return True;
191         }
192
193         if (lck1->start >= (lck2->start + lck2->size) ||
194             lck2->start >= (lck1->start + lck1->size)) {
195                 return False;
196         }
197             
198         return True;
199
200 #endif
201
202 /****************************************************************************
203  Check to see if this lock conflicts, but ignore our own locks on the
204  same fnum only. This is the read/write lock check code path.
205  This is never used in the POSIX lock case.
206 ****************************************************************************/
207
208 static BOOL brl_conflict_other(const struct lock_struct *lck1, const struct lock_struct *lck2)
209 {
210         if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
211                 return False;
212
213         if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) 
214                 return False;
215
216         /* POSIX flavour locks never conflict here - this is only called
217            in the read/write path. */
218
219         if (lck1->lock_flav == POSIX_LOCK && lck2->lock_flav == POSIX_LOCK)
220                 return False;
221
222         /*
223          * Incoming WRITE locks conflict with existing READ locks even
224          * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
225          */
226
227         if (!(lck2->lock_type == WRITE_LOCK && lck1->lock_type == READ_LOCK)) {
228                 if (brl_same_context(&lck1->context, &lck2->context) &&
229                                         lck1->fnum == lck2->fnum)
230                         return False;
231         }
232
233         return brl_overlap(lck1, lck2);
234
235
236 /****************************************************************************
237  Amazingly enough, w2k3 "remembers" whether the last lock failure
238  is the same as this one and changes its error code. I wonder if any
239  app depends on this ?
240 ****************************************************************************/
241
242 static NTSTATUS brl_lock_failed(const struct lock_struct *lock)
243 {
244         static struct lock_struct last_lock_failure;
245
246         if (brl_same_context(&lock->context, &last_lock_failure.context) &&
247                         lock->fnum == last_lock_failure.fnum &&
248                         lock->start == last_lock_failure.start &&
249                         lock->size == last_lock_failure.size) {
250                 return NT_STATUS_FILE_LOCK_CONFLICT;
251         }
252         last_lock_failure = *lock;
253         if (lock->start >= 0xEF000000 &&
254                         (lock->start >> 63) == 0) {
255                 /* amazing the little things you learn with a test
256                    suite. Locks beyond this offset (as a 64 bit
257                    number!) always generate the conflict error code,
258                    unless the top bit is set */
259                 return NT_STATUS_FILE_LOCK_CONFLICT;
260         }
261         return NT_STATUS_LOCK_NOT_GRANTED;
262 }
263
264 /****************************************************************************
265  Open up the brlock.tdb database.
266 ****************************************************************************/
267
268 void brl_init(int read_only)
269 {
270         if (tdb) {
271                 return;
272         }
273         tdb = tdb_open_log(lock_path("brlock.tdb"),
274                         lp_open_files_db_hash_size(),
275                         TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST),
276                         read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644 );
277         if (!tdb) {
278                 DEBUG(0,("Failed to open byte range locking database %s\n",
279                         lock_path("brlock.tdb")));
280                 return;
281         }
282 }
283
284 /****************************************************************************
285  Close down the brlock.tdb database.
286 ****************************************************************************/
287
288 void brl_shutdown(int read_only)
289 {
290         if (!tdb) {
291                 return;
292         }
293         tdb_close(tdb);
294 }
295
296 #if ZERO_ZERO
297 /****************************************************************************
298  Compare two locks for sorting.
299 ****************************************************************************/
300
301 static int lock_compare(const struct lock_struct *lck1, 
302                          const struct lock_struct *lck2)
303 {
304         if (lck1->start != lck2->start) {
305                 return (lck1->start - lck2->start);
306         }
307         if (lck2->size != lck1->size) {
308                 return ((int)lck1->size - (int)lck2->size);
309         }
310         return 0;
311 }
312 #endif
313
314 /****************************************************************************
315  Lock a range of bytes - Windows lock semantics.
316 ****************************************************************************/
317
318 static NTSTATUS brl_lock_windows(struct byte_range_lock *br_lck,
319                         const struct lock_struct *plock,
320                         BOOL *my_lock_ctx)
321 {
322         unsigned int i;
323         files_struct *fsp = br_lck->fsp;
324         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
325
326         for (i=0; i < br_lck->num_locks; i++) {
327                 /* Do any Windows or POSIX locks conflict ? */
328                 if (brl_conflict(&locks[i], plock)) {
329                         NTSTATUS status = brl_lock_failed(plock);;
330                         /* Did we block ourselves ? */
331                         if (brl_same_context(&locks[i].context, &plock->context)) {
332                                 *my_lock_ctx = True;
333                         }
334                         return status;
335                 }
336 #if ZERO_ZERO
337                 if (plock->start == 0 && plock->size == 0 && 
338                                 locks[i].size == 0) {
339                         break;
340                 }
341 #endif
342         }
343
344         /* We can get the Windows lock, now see if it needs to
345            be mapped into a lower level POSIX one, and if so can
346            we get it ? We tell the lower lock layer about the
347            lock type so it can cope with the difference between
348            Windows "stacking" locks and POSIX "flat" ones. */
349
350         if ((plock->lock_type != PENDING_LOCK) && lp_posix_locking(SNUM(fsp->conn))) {
351                 if (!set_posix_lock(fsp, plock->start, plock->size, plock->lock_type, WINDOWS_LOCK)) {
352                         if (errno == EACCES || errno == EAGAIN) {
353                                 return NT_STATUS_FILE_LOCK_CONFLICT;
354                         } else {
355                                 return map_nt_error_from_unix(errno);
356                         }
357                 }
358         }
359
360         /* no conflicts - add it to the list of locks */
361         locks = (struct lock_struct *)SMB_REALLOC(locks, (br_lck->num_locks + 1) * sizeof(*locks));
362         if (!locks) {
363                 return NT_STATUS_NO_MEMORY;
364         }
365
366         memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
367         br_lck->num_locks += 1;
368         br_lck->lock_data = (void *)locks;
369         br_lck->modified = True;
370
371         return NT_STATUS_OK;
372 }
373
374 /****************************************************************************
375  Cope with POSIX range splits and merges.
376 ****************************************************************************/
377
378 static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr,
379                                                 const struct lock_struct *ex,
380                                                 const struct lock_struct *plock,
381                                                 BOOL *lock_was_added)
382 {
383         BOOL lock_types_differ = (ex->lock_type != plock->lock_type);
384
385         /* We can't merge non-conflicting locks on different context - ignore fnum. */
386
387         if (!brl_same_context(&ex->context, &plock->context)) {
388                 /* Just copy. */
389                 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
390                 return 1;
391         }
392
393         /* We now know we have the same context. */
394
395         /* Did we overlap ? */
396
397 /*********************************************
398                                              +---------+
399                                              | ex      |
400                                              +---------+
401                                 +-------+
402                                 | plock |
403                                 +-------+
404 OR....
405              +---------+
406              |  ex     |
407              +---------+
408 **********************************************/
409
410         if ( (ex->start >= (plock->start + plock->size)) ||
411                         (plock->start >= (ex->start + ex->size))) {
412                 /* No overlap with this lock - copy existing. */
413                 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
414                 return 1;
415         }
416
417 /*********************************************
418                 +---------+
419                 |  ex     |
420                 +---------+
421         +---------------------------+
422         |       plock               | -> replace with plock.
423         +---------------------------+
424 **********************************************/
425
426         if ( (ex->start >= plock->start) &&
427                         (ex->start + ex->size <= plock->start + plock->size) ) {
428                 memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
429                 *lock_was_added = True;
430                 return 1;
431         }
432
433 /*********************************************
434                 +---------------+
435                 |  ex           |
436                 +---------------+
437         +---------------+
438         |   plock       |
439         +---------------+
440 BECOMES....
441         +---------------+-------+
442         |   plock       | ex    | - different lock types.
443         +---------------+-------+
444 OR....
445         +-----------------------+
446         |   ex                  | - same lock type.
447         +-----------------------+
448 **********************************************/
449
450         if ( (ex->start >= plock->start) &&
451                                 (ex->start < plock->start + plock->size) &&
452                                 (ex->start + ex->size > plock->start + plock->size) ) {
453
454                 *lock_was_added = True;
455
456                 /* If the lock types are the same, we merge, if different, we
457                    add the new lock before the old. */
458
459                 if (lock_types_differ) {
460                         /* Add new. */
461                         memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
462                         memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
463                         /* Adjust existing start and size. */
464                         lck_arr[1].start = plock->start + plock->size;
465                         lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
466                         return 2;
467                 } else {
468                         /* Merge. */
469                         memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
470                         /* Set new start and size. */
471                         lck_arr[0].start = plock->start;
472                         lck_arr[0].size = (ex->start + ex->size) - plock->start;
473                         return 1;
474                 }
475         }
476
477 /*********************************************
478    +---------------+
479    |  ex           |
480    +---------------+
481            +---------------+
482            |   plock       |
483            +---------------+
484 BECOMES....
485    +-------+---------------+
486    | ex    |   plock       | - different lock types
487    +-------+---------------+
488
489 OR
490    +-----------------------+
491    | ex                    | - same lock type.
492    +-----------------------+
493
494 **********************************************/
495
496         if ( (ex->start < plock->start) &&
497                         (ex->start + ex->size > plock->start) &&
498                         (ex->start + ex->size <= plock->start + plock->size) ) {
499
500                 *lock_was_added = True;
501
502                 /* If the lock types are the same, we merge, if different, we
503                    add the new lock after the old. */
504
505                 if (lock_types_differ) {
506                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
507                         memcpy(&lck_arr[1], plock, sizeof(struct lock_struct));
508                         /* Adjust existing size. */
509                         lck_arr[0].size = plock->start - ex->start;
510                         return 2;
511                 } else {
512                         /* Merge. */
513                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
514                         /* Adjust existing size. */
515                         lck_arr[0].size = (plock->start + plock->size) - ex->start;
516                         return 1;
517                 }
518         }
519
520 /*********************************************
521         +---------------------------+
522         |        ex                 |
523         +---------------------------+
524                 +---------+
525                 |  plock  |
526                 +---------+
527 BECOMES.....
528         +-------+---------+---------+
529         | ex    |  plock  | ex      | - different lock types.
530         +-------+---------+---------+
531 OR
532         +---------------------------+
533         |        ex                 | - same lock type.
534         +---------------------------+
535 **********************************************/
536
537         if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
538                 *lock_was_added = True;
539
540                 if (lock_types_differ) {
541
542                         /* We have to split ex into two locks here. */
543
544                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
545                         memcpy(&lck_arr[1], plock, sizeof(struct lock_struct));
546                         memcpy(&lck_arr[2], ex, sizeof(struct lock_struct));
547
548                         /* Adjust first existing size. */
549                         lck_arr[0].size = plock->start - ex->start;
550
551                         /* Adjust second existing start and size. */
552                         lck_arr[2].start = plock->start + plock->size;
553                         lck_arr[2].size = (ex->start + ex->size) - (plock->start + plock->size);
554                         return 3;
555                 } else {
556                         /* Just eat plock. */
557                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
558                         return 1;
559                 }
560         }
561
562         /* Never get here. */
563         smb_panic("brlock_posix_split_merge\n");
564         /* Notreached. */
565         abort();
566 }
567
568 /****************************************************************************
569  Lock a range of bytes - POSIX lock semantics.
570  We must cope with range splits and merges.
571 ****************************************************************************/
572
573 static NTSTATUS brl_lock_posix(struct byte_range_lock *br_lck,
574                         const struct lock_struct *plock,
575                         BOOL *my_lock_ctx)
576 {
577         unsigned int i, count;
578         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
579         struct lock_struct *tp;
580         BOOL lock_was_added = False;
581
582         /* No zero-zero locks for POSIX. */
583         if (plock->start == 0 && plock->size == 0) {
584                 return NT_STATUS_INVALID_PARAMETER;
585         }
586
587         /* Don't allow 64-bit lock wrap. */
588         if (plock->start + plock->size < plock->start ||
589                         plock->start + plock->size < plock->size) {
590                 return NT_STATUS_INVALID_PARAMETER;
591         }
592
593         /* The worst case scenario here is we have to split an
594            existing POSIX lock range into two, and add our lock,
595            so we need at most 2 more entries. */
596
597         tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 2));
598         if (!tp) {
599                 return NT_STATUS_NO_MEMORY;
600         }
601         
602         count = 0;
603         for (i=0; i < br_lck->num_locks; i++) {
604                 if (locks[i].lock_flav == WINDOWS_LOCK) {
605                         /* Do any Windows flavour locks conflict ? */
606                         if (brl_conflict(&locks[i], plock)) {
607                                 /* Did we block ourselves ? */
608                                 if (brl_same_context(&locks[i].context, &plock->context)) {
609                                         *my_lock_ctx = True;
610                                 }
611                                 /* No games with error messages. */
612                                 SAFE_FREE(tp);
613                                 return NT_STATUS_FILE_LOCK_CONFLICT;
614                         }
615                         /* Just copy the Windows lock into the new array. */
616                         memcpy(&tp[count], &locks[i], sizeof(struct lock_struct));
617                         count++;
618                 } else {
619                         /* POSIX conflict semantics are different. */
620                         if (brl_conflict_posix(&locks[i], plock)) {
621                                 /* Can't block ourselves with POSIX locks. */
622                                 /* No games with error messages. */
623                                 SAFE_FREE(tp);
624                                 return NT_STATUS_FILE_LOCK_CONFLICT;
625                         }
626
627                         /* Work out overlaps. */
628                         count += brlock_posix_split_merge(&tp[count], &locks[i], plock, &lock_was_added);
629                 }
630         }
631
632         /* We can get the POSIX lock, now see if it needs to
633            be mapped into a lower level POSIX one, and if so can
634            we get it ? We well the lower lock layer about the
635            lock type so it can cope with the difference between
636            Windows "stacking" locks and POSIX "flat" ones. */
637
638 #if 0
639         /* FIXME - this call doesn't work correctly yet for POSIX locks... */
640
641         if ((plock->lock_type != PENDING_LOCK) && lp_posix_locking(SNUM(fsp->conn))) {
642                 files_struct *fsp = br_lck->fsp;
643
644                 if (!set_posix_lock(fsp, plock->start, plock->size, plock->lock_type, POSIX_LOCK)) {
645                         if (errno == EACCES || errno == EAGAIN) {
646                                 SAFE_FREE(tp);
647                                 return NT_STATUS_FILE_LOCK_CONFLICT;
648                         } else {
649                                 SAFE_FREE(tp);
650                                 return map_nt_error_from_unix(errno);
651                         }
652                 }
653         }
654 #endif
655
656         if (!lock_was_added) {
657                 memcpy(&tp[count], plock, sizeof(struct lock_struct));
658                 count++;
659         }
660
661         /* Realloc so we don't leak entries per lock call. */
662         tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
663         if (!tp) {
664                 return NT_STATUS_NO_MEMORY;
665         }
666         br_lck->num_locks = count;
667         br_lck->lock_data = (void *)tp;
668         br_lck->modified = True;
669         return NT_STATUS_OK;
670 }
671
672 /****************************************************************************
673  Lock a range of bytes.
674 ****************************************************************************/
675
676 NTSTATUS brl_lock(struct byte_range_lock *br_lck,
677                 uint16 smbpid,
678                 struct process_id pid,
679                 br_off start,
680                 br_off size, 
681                 enum brl_type lock_type,
682                 enum brl_flavour lock_flav,
683                 BOOL *my_lock_ctx)
684 {
685         NTSTATUS ret;
686         struct lock_struct lock;
687
688         *my_lock_ctx = False;
689
690 #if !ZERO_ZERO
691         if (start == 0 && size == 0) {
692                 DEBUG(0,("client sent 0/0 lock - please report this\n"));
693         }
694 #endif
695
696         lock.context.smbpid = smbpid;
697         lock.context.pid = pid;
698         lock.context.tid = br_lck->fsp->conn->cnum;
699         lock.start = start;
700         lock.size = size;
701         lock.fnum = br_lck->fsp->fnum;
702         lock.lock_type = lock_type;
703         lock.lock_flav = lock_flav;
704
705         if (lock_flav == WINDOWS_LOCK) {
706                 ret = brl_lock_windows(br_lck, &lock, my_lock_ctx);
707         } else {
708                 ret = brl_lock_posix(br_lck, &lock, my_lock_ctx);
709         }
710
711 #if ZERO_ZERO
712         /* sort the lock list */
713         qsort(br_lck->lock_data, (size_t)br_lck->num_locks, sizeof(lock), lock_compare);
714 #endif
715
716         return ret;
717 }
718
719 /****************************************************************************
720  Check if an unlock overlaps a pending lock.
721 ****************************************************************************/
722
723 static BOOL brl_pending_overlap(struct lock_struct *lock, struct lock_struct *pend_lock)
724 {
725         if ((lock->start <= pend_lock->start) && (lock->start + lock->size > pend_lock->start))
726                 return True;
727         if ((lock->start >= pend_lock->start) && (lock->start <= pend_lock->start + pend_lock->size))
728                 return True;
729         return False;
730 }
731
732 /****************************************************************************
733  Unlock a range of bytes - Windows semantics.
734 ****************************************************************************/
735
736 static BOOL brl_unlock_windows(struct byte_range_lock *br_lck, const struct lock_struct *plock)
737 {
738         unsigned int i, j;
739         struct lock_struct *lock = NULL;
740         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
741
742 #if ZERO_ZERO
743         for (i = 0; i < br_lck->num_locks; i++) {
744                 lock = &locks[i];
745
746                 if (lock->lock_type == WRITE_LOCK &&
747                     brl_same_context(&lock->context, &plock->context) &&
748                     lock->fnum == plock->fnum &&
749                     lock->lock_flav == WINDOWS_LOCK &&
750                     lock->start == plock->start &&
751                     lock->size == plock->size) {
752
753                         /* found it - delete it */
754                         if (i < br_lck->num_locks - 1) {
755                                 memmove(&locks[i], &locks[i+1], 
756                                         sizeof(*locks)*((br_lck->num_locks-1) - i));
757                         }
758
759                         br_lck->num_locks -= 1;
760                         br_lck->modified = True;
761                         return True;
762                 }
763         }
764 #endif
765
766         for (i = 0; i < br_lck->num_locks; i++) {
767                 lock = &locks[i];
768
769                 /* Only remove our own locks that match in start, size, and flavour. */
770                 if (brl_same_context(&lock->context, &plock->context) &&
771                                         lock->fnum == plock->fnum &&
772                                         lock->lock_flav == WINDOWS_LOCK &&
773                                         lock->start == plock->start &&
774                                         lock->size == plock->size ) {
775                         break;
776                 }
777         }
778
779         if (i == br_lck->num_locks) {
780                 /* we didn't find it */
781                 return False;
782         }
783
784         /* Unlock any POSIX regions. */
785         if(lp_posix_locking(br_lck->fsp->conn->cnum)) {
786                 release_posix_lock(br_lck->fsp, plock->start, plock->size);
787         }
788
789         /* Send unlock messages to any pending waiters that overlap. */
790         for (j=0; j < br_lck->num_locks; j++) {
791                 struct lock_struct *pend_lock = &locks[j];
792
793                 /* Ignore non-pending locks. */
794                 if (pend_lock->lock_type != PENDING_LOCK) {
795                         continue;
796                 }
797
798                 /* We could send specific lock info here... */
799                 if (brl_pending_overlap(lock, pend_lock)) {
800                         DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
801                                 procid_str_static(&pend_lock->context.pid )));
802
803                         become_root();
804                         message_send_pid(pend_lock->context.pid,
805                                         MSG_SMB_UNLOCK,
806                                         NULL, 0, True);
807                         unbecome_root();
808                 }
809         }
810
811         /* Actually delete the lock. */
812         if (i < br_lck->num_locks - 1) {
813                 memmove(&locks[i], &locks[i+1], 
814                         sizeof(*locks)*((br_lck->num_locks-1) - i));
815         }
816
817         br_lck->num_locks -= 1;
818         br_lck->modified = True;
819         return True;
820 }
821
822 /****************************************************************************
823  Unlock a range of bytes - POSIX semantics.
824 ****************************************************************************/
825
826 static BOOL brl_unlock_posix(struct byte_range_lock *br_lck, const struct lock_struct *plock)
827 {
828         unsigned int i, j, count;
829         struct lock_struct *lock = NULL;
830         struct lock_struct *tp;
831         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
832         BOOL overlap_found = False;
833
834         /* No zero-zero locks for POSIX. */
835         if (plock->start == 0 && plock->size == 0) {
836                 return False;
837         }
838
839         /* Don't allow 64-bit lock wrap. */
840         if (plock->start + plock->size < plock->start ||
841                         plock->start + plock->size < plock->size) {
842                 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
843                 return False;
844         }
845
846         /* The worst case scenario here is we have to split an
847            existing POSIX lock range into two, so we need at most
848            1 more entry. */
849
850         tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 1));
851         if (!tp) {
852                 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
853                 return False;
854         }
855
856         count = 0;
857         for (i = 0; i < br_lck->num_locks; i++) {
858                 struct lock_struct tmp_lock[3];
859                 BOOL lock_was_added = False;
860                 unsigned int tmp_count;
861
862                 lock = &locks[i];
863
864                 /* Only remove our own locks - ignore fnum. */
865                 if (lock->lock_type == PENDING_LOCK ||
866                                 !brl_same_context(&lock->context, &plock->context)) {
867                         memcpy(&tp[count], lock, sizeof(struct lock_struct));
868                         count++;
869                         continue;
870                 }
871
872                 /* Work out overlaps. */
873                 tmp_count = brlock_posix_split_merge(&tmp_lock[0], &locks[i], plock, &lock_was_added);
874
875                 if (tmp_count == 1) {
876                         /* Ether the locks didn't overlap, or the unlock completely
877                            overlapped this lock. If it didn't overlap, then there's
878                            no change in the locks. */
879                         if (tmp_lock[0].lock_type != UNLOCK_LOCK) {
880                                 SMB_ASSERT(tmp_lock[0].lock_type == locks[i].lock_type);
881                                 /* No change in this lock. */
882                                 memcpy(&tp[count], &tmp_lock[0], sizeof(struct lock_struct));
883                                 count++;
884                         } else {
885                                 SMB_ASSERT(tmp_lock[0].lock_type == UNLOCK_LOCK);
886                                 overlap_found = True;
887                         }
888                         continue;
889                 } else if (tmp_count == 2) {
890                         /* The unlock overlapped an existing lock. Copy the truncated
891                            lock into the lock array. */
892                         if (tmp_lock[0].lock_type != UNLOCK_LOCK) {
893                                 SMB_ASSERT(tmp_lock[0].lock_type == locks[i].lock_type);
894                                 SMB_ASSERT(tmp_lock[1].lock_type == UNLOCK_LOCK);
895                                 memcpy(&tp[count], &tmp_lock[0], sizeof(struct lock_struct));
896                         } else {
897                                 SMB_ASSERT(tmp_lock[0].lock_type == UNLOCK_LOCK);
898                                 SMB_ASSERT(tmp_lock[1].lock_type == locks[i].lock_type);
899                                 memcpy(&tp[count], &tmp_lock[1], sizeof(struct lock_struct));
900                         }
901                         count++;
902                         overlap_found = True;
903                         continue;
904                 } else {
905                         /* tmp_count == 3 - (we split a lock range in two). */
906                         SMB_ASSERT(tmp_lock[0].lock_type == locks[i].lock_type);
907                         SMB_ASSERT(tmp_lock[1].lock_type == UNLOCK_LOCK);
908                         SMB_ASSERT(tmp_lock[2].lock_type != locks[i].lock_type);
909
910                         memcpy(&tp[count], &tmp_lock[0], sizeof(struct lock_struct));
911                         count++;
912                         memcpy(&tp[count], &tmp_lock[2], sizeof(struct lock_struct));
913                         count++;
914                         overlap_found = True;
915                         /* Optimisation... */
916                         /* We know we're finished here as we can't overlap any
917                            more POSIX locks. Copy the rest of the lock array. */
918                         if (i < br_lck->num_locks - 1) {
919                                 memcpy(&tp[count], &locks[i+1], 
920                                         sizeof(*locks)*((br_lck->num_locks-1) - i));
921                                 count += ((br_lck->num_locks-1) - i);
922                         }
923                         break;
924                 }
925         }
926
927         if (!overlap_found) {
928                 /* Just ignore - no change. */
929                 SAFE_FREE(tp);
930                 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
931                 return True;
932         }
933
934 #if 0
935         /* FIXME - this call doesn't work correctly yet for POSIX locks... */
936
937         /* Unlock any POSIX regions. */
938         if(lp_posix_locking(br_lck->fsp->conn->cnum)) {
939                 release_posix_lock(br_lck->fsp, plock->start, plock->size);
940         }
941 #endif
942
943         /* Realloc so we don't leak entries per unlock call. */
944         if (count) {
945                 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
946                 if (!tp) {
947                         DEBUG(10,("brl_unlock_posix: realloc fail\n"));
948                         return False;
949                 }
950         } else {
951                 /* We deleted the last lock. */
952                 SAFE_FREE(tp);
953                 tp = NULL;
954         }
955
956         br_lck->num_locks = count;
957         br_lck->lock_data = (void *)tp;
958         br_lck->modified = True;
959
960         /* Send unlock messages to any pending waiters that overlap. */
961         locks = tp;
962
963         for (j=0; j < br_lck->num_locks; j++) {
964                 struct lock_struct *pend_lock = &locks[j];
965
966                 /* Ignore non-pending locks. */
967                 if (pend_lock->lock_type != PENDING_LOCK) {
968                         continue;
969                 }
970
971                 /* We could send specific lock info here... */
972                 if (brl_pending_overlap(lock, pend_lock)) {
973                         DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
974                                 procid_str_static(&pend_lock->context.pid )));
975
976                         become_root();
977                         message_send_pid(pend_lock->context.pid,
978                                         MSG_SMB_UNLOCK,
979                                         NULL, 0, True);
980                         unbecome_root();
981                 }
982         }
983
984         return True;
985 }
986
987 /****************************************************************************
988  Unlock a range of bytes.
989 ****************************************************************************/
990
991 BOOL brl_unlock(struct byte_range_lock *br_lck,
992                 uint16 smbpid,
993                 struct process_id pid,
994                 br_off start,
995                 br_off size,
996                 enum brl_flavour lock_flav)
997 {
998         struct lock_struct lock;
999
1000         lock.context.smbpid = smbpid;
1001         lock.context.pid = pid;
1002         lock.context.tid = br_lck->fsp->conn->cnum;
1003         lock.start = start;
1004         lock.size = size;
1005         lock.fnum = br_lck->fsp->fnum;
1006         lock.lock_type = UNLOCK_LOCK;
1007         lock.lock_flav = lock_flav;
1008
1009         if (lock_flav == WINDOWS_LOCK) {
1010                 return brl_unlock_windows(br_lck, &lock);
1011         } else {
1012                 return brl_unlock_posix(br_lck, &lock);
1013         }
1014 }
1015
1016 /****************************************************************************
1017  Test if we could add a lock if we wanted to.
1018  Returns True if the region required is currently unlocked, False if locked.
1019 ****************************************************************************/
1020
1021 BOOL brl_locktest(struct byte_range_lock *br_lck,
1022                 uint16 smbpid,
1023                 struct process_id pid,
1024                 br_off start,
1025                 br_off size, 
1026                 enum brl_type lock_type,
1027                 enum brl_flavour lock_flav)
1028 {
1029         BOOL ret = True;
1030         unsigned int i;
1031         struct lock_struct lock;
1032         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1033         files_struct *fsp = br_lck->fsp;
1034
1035         lock.context.smbpid = smbpid;
1036         lock.context.pid = pid;
1037         lock.context.tid = br_lck->fsp->conn->cnum;
1038         lock.start = start;
1039         lock.size = size;
1040         lock.fnum = fsp->fnum;
1041         lock.lock_type = lock_type;
1042         lock.lock_flav = lock_flav;
1043
1044         /* Make sure existing locks don't conflict */
1045         for (i=0; i < br_lck->num_locks; i++) {
1046                 /*
1047                  * Our own locks don't conflict.
1048                  */
1049                 if (brl_conflict_other(&locks[i], &lock)) {
1050                         return False;
1051                 }
1052         }
1053
1054         /*
1055          * There is no lock held by an SMB daemon, check to
1056          * see if there is a POSIX lock from a UNIX or NFS process.
1057          * This only conflicts with Windows locks, not POSIX locks.
1058          */
1059
1060         if(lp_posix_locking(fsp->conn->cnum) && (lock_flav == WINDOWS_LOCK)) {
1061                 ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
1062
1063                 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1064                         (double)start, (double)size, ret ? "locked" : "unlocked",
1065                         fsp->fnum, fsp->fsp_name ));
1066
1067                 /* We need to return the inverse of is_posix_locked. */
1068                 ret = !ret;
1069         }
1070
1071         /* no conflicts - we could have added it */
1072         return ret;
1073 }
1074
1075 /****************************************************************************
1076  Query for existing locks.
1077 ****************************************************************************/
1078
1079 NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
1080                 uint16 *psmbpid,
1081                 struct process_id pid,
1082                 br_off *pstart,
1083                 br_off *psize, 
1084                 enum brl_type *plock_type,
1085                 enum brl_flavour lock_flav)
1086 {
1087         unsigned int i;
1088         struct lock_struct lock;
1089         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1090         files_struct *fsp = br_lck->fsp;
1091
1092         lock.context.smbpid = *psmbpid;
1093         lock.context.pid = pid;
1094         lock.context.tid = br_lck->fsp->conn->cnum;
1095         lock.start = *pstart;
1096         lock.size = *psize;
1097         lock.fnum = fsp->fnum;
1098         lock.lock_type = *plock_type;
1099         lock.lock_flav = lock_flav;
1100
1101         /* Make sure existing locks don't conflict */
1102         for (i=0; i < br_lck->num_locks; i++) {
1103                 struct lock_struct *exlock = &locks[i];
1104                 BOOL conflict = False;
1105
1106                 if (exlock->lock_flav == WINDOWS_LOCK) {
1107                         conflict = brl_conflict(exlock, &lock);
1108                 } else {        
1109                         conflict = brl_conflict_posix(exlock, &lock);
1110                 }
1111
1112                 if (conflict) {
1113                         *psmbpid = exlock->context.smbpid;
1114                         *pstart = exlock->start;
1115                         *psize = exlock->size;
1116                         *plock_type = exlock->lock_type;
1117                         return NT_STATUS_LOCK_NOT_GRANTED;
1118                 }
1119         }
1120
1121         /*
1122          * There is no lock held by an SMB daemon, check to
1123          * see if there is a POSIX lock from a UNIX or NFS process.
1124          */
1125
1126         if(lp_posix_locking(fsp->conn->cnum)) {
1127                 BOOL ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
1128
1129                 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1130                         (double)*pstart, (double)*psize, ret ? "locked" : "unlocked",
1131                         fsp->fnum, fsp->fsp_name ));
1132
1133                 if (ret) {
1134                         /* Hmmm. No clue what to set smbpid to - use -1. */
1135                         *psmbpid = 0xFFFF;
1136                         return NT_STATUS_LOCK_NOT_GRANTED;
1137                 }
1138         }
1139
1140         return NT_STATUS_OK;
1141 }
1142
1143
1144 /****************************************************************************
1145  Remove a particular pending lock.
1146 ****************************************************************************/
1147
1148 BOOL brl_remove_pending_lock(struct byte_range_lock *br_lck,
1149                 uint16 smbpid,
1150                 struct process_id pid,
1151                 br_off start,
1152                 br_off size,
1153                 enum brl_flavour lock_flav)
1154 {
1155         unsigned int i;
1156         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1157         struct lock_context context;
1158
1159         context.smbpid = smbpid;
1160         context.pid = pid;
1161         context.tid = br_lck->fsp->conn->cnum;
1162
1163         for (i = 0; i < br_lck->num_locks; i++) {
1164                 struct lock_struct *lock = &locks[i];
1165
1166                 /* For pending locks we *always* care about the fnum. */
1167                 if (brl_same_context(&lock->context, &context) &&
1168                                 lock->fnum == br_lck->fsp->fnum &&
1169                                 lock->lock_type == PENDING_LOCK &&
1170                                 lock->lock_flav == lock_flav &&
1171                                 lock->start == start &&
1172                                 lock->size == size) {
1173                         break;
1174                 }
1175         }
1176
1177         if (i == br_lck->num_locks) {
1178                 /* Didn't find it. */
1179                 return False;
1180         }
1181
1182         if (i < br_lck->num_locks - 1) {
1183                 /* Found this particular pending lock - delete it */
1184                 memmove(&locks[i], &locks[i+1], 
1185                         sizeof(*locks)*((br_lck->num_locks-1) - i));
1186         }
1187
1188         br_lck->num_locks -= 1;
1189         br_lck->modified = True;
1190         return True;
1191 }
1192
1193
1194 /****************************************************************************
1195  Remove any locks associated with a open file.
1196 ****************************************************************************/
1197
1198 void brl_close_fnum(struct byte_range_lock *br_lck, struct process_id pid)
1199 {
1200         files_struct *fsp = br_lck->fsp;
1201         uint16 tid = fsp->conn->cnum;
1202         int fnum = fsp->fnum;
1203         unsigned int i, j, dcount=0;
1204         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1205
1206         /* Remove any existing locks for this fnum (or any fnum if they're POSIX). */
1207
1208         for (i=0; i < br_lck->num_locks; i++) {
1209                 struct lock_struct *lock = &locks[i];
1210                 BOOL del_this_lock = False;
1211
1212                 if (lock->context.tid == tid && procid_equal(&lock->context.pid, &pid)) {
1213                         if ((lock->lock_flav == WINDOWS_LOCK) && (lock->fnum == fnum)) {
1214                                 del_this_lock = True;
1215                         } else if (lock->lock_flav == POSIX_LOCK) {
1216                                 del_this_lock = True;
1217                         }
1218                 }
1219
1220                 if (del_this_lock) {
1221                         /* Send unlock messages to any pending waiters that overlap. */
1222                         for (j=0; j < br_lck->num_locks; j++) {
1223                                 struct lock_struct *pend_lock = &locks[j];
1224
1225                                 /* Ignore our own or non-pending locks. */
1226                                 if (pend_lock->lock_type != PENDING_LOCK) {
1227                                         continue;
1228                                 }
1229
1230                                 /* Optimisation - don't send to this fnum as we're
1231                                    closing it. */
1232                                 if (pend_lock->context.tid == tid &&
1233                                     procid_equal(&pend_lock->context.pid, &pid) &&
1234                                     pend_lock->fnum == fnum) {
1235                                         continue;
1236                                 }
1237
1238                                 /* We could send specific lock info here... */
1239                                 if (brl_pending_overlap(lock, pend_lock)) {
1240                                         become_root();
1241                                         message_send_pid(pend_lock->context.pid,
1242                                                         MSG_SMB_UNLOCK,
1243                                                         NULL, 0, True);
1244                                         unbecome_root();
1245                                 }
1246                         }
1247
1248                         /* found it - delete it */
1249                         if (br_lck->num_locks > 1 && i < br_lck->num_locks - 1) {
1250                                 memmove(&locks[i], &locks[i+1], 
1251                                         sizeof(*locks)*((br_lck->num_locks-1) - i));
1252                         }
1253                         br_lck->num_locks--;
1254                         br_lck->modified = True;
1255                         i--;
1256                         dcount++;
1257                 }
1258         }
1259 }
1260
1261 /****************************************************************************
1262  Traverse the whole database with this function, calling traverse_callback
1263  on each lock.
1264 ****************************************************************************/
1265
1266 static int traverse_fn(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
1267 {
1268         struct lock_struct *locks;
1269         struct lock_key *key;
1270         int i;
1271
1272         BRLOCK_FN(traverse_callback) = (BRLOCK_FN_CAST())state;
1273
1274         locks = (struct lock_struct *)dbuf.dptr;
1275         key = (struct lock_key *)kbuf.dptr;
1276
1277         for (i=0;i<dbuf.dsize/sizeof(*locks);i++) {
1278                 traverse_callback(key->device,
1279                                   key->inode,
1280                                   locks[i].context.pid,
1281                                   locks[i].lock_type,
1282                                   locks[i].lock_flav,
1283                                   locks[i].start,
1284                                   locks[i].size);
1285         }
1286         return 0;
1287 }
1288
1289 /*******************************************************************
1290  Call the specified function on each lock in the database.
1291 ********************************************************************/
1292
1293 int brl_forall(BRLOCK_FN(fn))
1294 {
1295         if (!tdb) {
1296                 return 0;
1297         }
1298         return tdb_traverse(tdb, traverse_fn, (void *)fn);
1299 }
1300
1301 /*******************************************************************
1302  Store a potentially modified set of byte range lock data back into
1303  the database.
1304  Unlock the record.
1305 ********************************************************************/
1306
1307 int byte_range_lock_destructor(struct byte_range_lock *br_lck)
1308 {
1309         TDB_DATA key;
1310
1311         key.dptr = (char *)&br_lck->key;
1312         key.dsize = sizeof(struct lock_key);
1313
1314         if (!br_lck->modified) {
1315                 goto done;
1316         }
1317
1318         if (br_lck->num_locks == 0) {
1319                 /* No locks - delete this entry. */
1320                 if (tdb_delete(tdb, key) == -1) {
1321                         smb_panic("Could not delete byte range lock entry\n");
1322                 }
1323         } else {
1324                 TDB_DATA data;
1325                 data.dptr = br_lck->lock_data;
1326                 data.dsize = br_lck->num_locks * sizeof(struct lock_struct);
1327
1328                 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1) {
1329                         smb_panic("Could not store byte range mode entry\n");
1330                 }
1331         }
1332
1333  done:
1334
1335         tdb_chainunlock(tdb, key);
1336         SAFE_FREE(br_lck->lock_data);
1337         SAFE_FREE(br_lck);
1338         return 0;
1339 }
1340
1341 /*******************************************************************
1342  Fetch a set of byte range lock data from the database.
1343  Leave the record locked.
1344 ********************************************************************/
1345
1346 struct byte_range_lock *brl_get_locks(files_struct *fsp)
1347 {
1348         TDB_DATA key;
1349         TDB_DATA data;
1350         struct byte_range_lock *br_lck = SMB_MALLOC_P(struct byte_range_lock);
1351
1352         if (br_lck == NULL) {
1353                 return NULL;
1354         }
1355
1356         br_lck->fsp = fsp;
1357         br_lck->num_locks = 0;
1358         br_lck->modified = False;
1359         memset(&br_lck->key, '\0', sizeof(struct lock_key));
1360         br_lck->key.device = fsp->dev;
1361         br_lck->key.inode = fsp->inode;
1362
1363         key.dptr = (char *)&br_lck->key;
1364         key.dsize = sizeof(struct lock_key);
1365
1366         if (tdb_chainlock(tdb, key) != 0) {
1367                 DEBUG(3, ("Could not lock byte range lock entry\n"));
1368                 SAFE_FREE(br_lck);
1369                 return NULL;
1370         }
1371
1372         data = tdb_fetch(tdb, key);
1373         br_lck->lock_data = (void *)data.dptr;
1374         br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
1375
1376         if (DEBUGLEVEL >= 10) {
1377                 unsigned int i;
1378                 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1379                 DEBUG(10,("brl_get_locks: %u current locks on dev=%.0f, inode=%.0f\n",
1380                         br_lck->num_locks,
1381                         (double)fsp->dev, (double)fsp->inode ));
1382                 for( i = 0; i < br_lck->num_locks; i++) {
1383                         print_lock_struct(i, &locks[i]);
1384                 }
1385         }
1386         return br_lck;
1387 }