s3:include: change lock_struct->fnum to uint64_t
[kai/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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 /* This module implements a tdb based byte range locking service,
24    replacing the fcntl() based byte range locking previously
25    used. This allows us to provide the same semantics as NT */
26
27 #include "includes.h"
28 #include "system/filesys.h"
29 #include "locking/proto.h"
30 #include "smbd/globals.h"
31 #include "dbwrap/dbwrap.h"
32 #include "dbwrap/dbwrap_open.h"
33 #include "serverid.h"
34 #include "messages.h"
35
36 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_LOCKING
38
39 #define ZERO_ZERO 0
40
41 /* The open brlock.tdb database. */
42
43 static struct db_context *brlock_db;
44
45 /****************************************************************************
46  Debug info at level 10 for lock struct.
47 ****************************************************************************/
48
49 static void print_lock_struct(unsigned int i, struct lock_struct *pls)
50 {
51         DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
52                         i,
53                         (unsigned long long)pls->context.smblctx,
54                         (unsigned int)pls->context.tid,
55                         server_id_str(talloc_tos(), &pls->context.pid) ));
56
57         DEBUG(10,("start = %.0f, size = %.0f, fnum = %llu, %s %s\n",
58                 (double)pls->start,
59                 (double)pls->size,
60                 (unsigned long long)pls->fnum,
61                 lock_type_name(pls->lock_type),
62                 lock_flav_name(pls->lock_flav) ));
63 }
64
65 /****************************************************************************
66  See if two locking contexts are equal.
67 ****************************************************************************/
68
69 bool brl_same_context(const struct lock_context *ctx1, 
70                              const struct lock_context *ctx2)
71 {
72         return (procid_equal(&ctx1->pid, &ctx2->pid) &&
73                 (ctx1->smblctx == ctx2->smblctx) &&
74                 (ctx1->tid == ctx2->tid));
75 }
76
77 /****************************************************************************
78  See if lck1 and lck2 overlap.
79 ****************************************************************************/
80
81 static bool brl_overlap(const struct lock_struct *lck1,
82                         const struct lock_struct *lck2)
83 {
84         /* XXX Remove for Win7 compatibility. */
85         /* this extra check is not redundent - it copes with locks
86            that go beyond the end of 64 bit file space */
87         if (lck1->size != 0 &&
88             lck1->start == lck2->start &&
89             lck1->size == lck2->size) {
90                 return True;
91         }
92
93         if (lck1->start >= (lck2->start+lck2->size) ||
94             lck2->start >= (lck1->start+lck1->size)) {
95                 return False;
96         }
97         return True;
98 }
99
100 /****************************************************************************
101  See if lock2 can be added when lock1 is in place.
102 ****************************************************************************/
103
104 static bool brl_conflict(const struct lock_struct *lck1, 
105                          const struct lock_struct *lck2)
106 {
107         /* Ignore PENDING locks. */
108         if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
109                 return False;
110
111         /* Read locks never conflict. */
112         if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
113                 return False;
114         }
115
116         /* A READ lock can stack on top of a WRITE lock if they have the same
117          * context & fnum. */
118         if (lck1->lock_type == WRITE_LOCK && lck2->lock_type == READ_LOCK &&
119             brl_same_context(&lck1->context, &lck2->context) &&
120             lck1->fnum == lck2->fnum) {
121                 return False;
122         }
123
124         return brl_overlap(lck1, lck2);
125
126
127 /****************************************************************************
128  See if lock2 can be added when lock1 is in place - when both locks are POSIX
129  flavour. POSIX locks ignore fnum - they only care about dev/ino which we
130  know already match.
131 ****************************************************************************/
132
133 static bool brl_conflict_posix(const struct lock_struct *lck1, 
134                                 const struct lock_struct *lck2)
135 {
136 #if defined(DEVELOPER)
137         SMB_ASSERT(lck1->lock_flav == POSIX_LOCK);
138         SMB_ASSERT(lck2->lock_flav == POSIX_LOCK);
139 #endif
140
141         /* Ignore PENDING locks. */
142         if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
143                 return False;
144
145         /* Read locks never conflict. */
146         if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
147                 return False;
148         }
149
150         /* Locks on the same context con't conflict. Ignore fnum. */
151         if (brl_same_context(&lck1->context, &lck2->context)) {
152                 return False;
153         }
154
155         /* One is read, the other write, or the context is different,
156            do they overlap ? */
157         return brl_overlap(lck1, lck2);
158
159
160 #if ZERO_ZERO
161 static bool brl_conflict1(const struct lock_struct *lck1, 
162                          const struct lock_struct *lck2)
163 {
164         if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
165                 return False;
166
167         if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
168                 return False;
169         }
170
171         if (brl_same_context(&lck1->context, &lck2->context) &&
172             lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
173                 return False;
174         }
175
176         if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
177                 return True;
178         }
179
180         if (lck1->start >= (lck2->start + lck2->size) ||
181             lck2->start >= (lck1->start + lck1->size)) {
182                 return False;
183         }
184
185         return True;
186
187 #endif
188
189 /****************************************************************************
190  Check to see if this lock conflicts, but ignore our own locks on the
191  same fnum only. This is the read/write lock check code path.
192  This is never used in the POSIX lock case.
193 ****************************************************************************/
194
195 static bool brl_conflict_other(const struct lock_struct *lck1, const struct lock_struct *lck2)
196 {
197         if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
198                 return False;
199
200         if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) 
201                 return False;
202
203         /* POSIX flavour locks never conflict here - this is only called
204            in the read/write path. */
205
206         if (lck1->lock_flav == POSIX_LOCK && lck2->lock_flav == POSIX_LOCK)
207                 return False;
208
209         /*
210          * Incoming WRITE locks conflict with existing READ locks even
211          * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
212          */
213
214         if (!(lck2->lock_type == WRITE_LOCK && lck1->lock_type == READ_LOCK)) {
215                 if (brl_same_context(&lck1->context, &lck2->context) &&
216                                         lck1->fnum == lck2->fnum)
217                         return False;
218         }
219
220         return brl_overlap(lck1, lck2);
221
222
223 /****************************************************************************
224  Check if an unlock overlaps a pending lock.
225 ****************************************************************************/
226
227 static bool brl_pending_overlap(const struct lock_struct *lock, const struct lock_struct *pend_lock)
228 {
229         if ((lock->start <= pend_lock->start) && (lock->start + lock->size > pend_lock->start))
230                 return True;
231         if ((lock->start >= pend_lock->start) && (lock->start <= pend_lock->start + pend_lock->size))
232                 return True;
233         return False;
234 }
235
236 /****************************************************************************
237  Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
238  is the same as this one and changes its error code. I wonder if any
239  app depends on this ?
240 ****************************************************************************/
241
242 NTSTATUS brl_lock_failed(files_struct *fsp, const struct lock_struct *lock, bool blocking_lock)
243 {
244         if (lock->start >= 0xEF000000 && (lock->start >> 63) == 0) {
245                 /* amazing the little things you learn with a test
246                    suite. Locks beyond this offset (as a 64 bit
247                    number!) always generate the conflict error code,
248                    unless the top bit is set */
249                 if (!blocking_lock) {
250                         fsp->last_lock_failure = *lock;
251                 }
252                 return NT_STATUS_FILE_LOCK_CONFLICT;
253         }
254
255         if (procid_equal(&lock->context.pid, &fsp->last_lock_failure.context.pid) &&
256                         lock->context.tid == fsp->last_lock_failure.context.tid &&
257                         lock->fnum == fsp->last_lock_failure.fnum &&
258                         lock->start == fsp->last_lock_failure.start) {
259                 return NT_STATUS_FILE_LOCK_CONFLICT;
260         }
261
262         if (!blocking_lock) {
263                 fsp->last_lock_failure = *lock;
264         }
265         return NT_STATUS_LOCK_NOT_GRANTED;
266 }
267
268 /****************************************************************************
269  Open up the brlock.tdb database.
270 ****************************************************************************/
271
272 void brl_init(bool read_only)
273 {
274         int tdb_flags;
275
276         if (brlock_db) {
277                 return;
278         }
279
280         tdb_flags = TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
281
282         if (!lp_clustering()) {
283                 /*
284                  * We can't use the SEQNUM trick to cache brlock
285                  * entries in the clustering case because ctdb seqnum
286                  * propagation has a delay.
287                  */
288                 tdb_flags |= TDB_SEQNUM;
289         }
290
291         brlock_db = db_open(NULL, lock_path("brlock.tdb"),
292                             lp_open_files_db_hash_size(), tdb_flags,
293                             read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644,
294                             DBWRAP_LOCK_ORDER_2);
295         if (!brlock_db) {
296                 DEBUG(0,("Failed to open byte range locking database %s\n",
297                         lock_path("brlock.tdb")));
298                 return;
299         }
300 }
301
302 /****************************************************************************
303  Close down the brlock.tdb database.
304 ****************************************************************************/
305
306 void brl_shutdown(void)
307 {
308         TALLOC_FREE(brlock_db);
309 }
310
311 #if ZERO_ZERO
312 /****************************************************************************
313  Compare two locks for sorting.
314 ****************************************************************************/
315
316 static int lock_compare(const struct lock_struct *lck1, 
317                          const struct lock_struct *lck2)
318 {
319         if (lck1->start != lck2->start) {
320                 return (lck1->start - lck2->start);
321         }
322         if (lck2->size != lck1->size) {
323                 return ((int)lck1->size - (int)lck2->size);
324         }
325         return 0;
326 }
327 #endif
328
329 /****************************************************************************
330  Lock a range of bytes - Windows lock semantics.
331 ****************************************************************************/
332
333 NTSTATUS brl_lock_windows_default(struct byte_range_lock *br_lck,
334     struct lock_struct *plock, bool blocking_lock)
335 {
336         unsigned int i;
337         files_struct *fsp = br_lck->fsp;
338         struct lock_struct *locks = br_lck->lock_data;
339         NTSTATUS status;
340
341         SMB_ASSERT(plock->lock_type != UNLOCK_LOCK);
342
343         if ((plock->start + plock->size - 1 < plock->start) &&
344                         plock->size != 0) {
345                 return NT_STATUS_INVALID_LOCK_RANGE;
346         }
347
348         for (i=0; i < br_lck->num_locks; i++) {
349                 /* Do any Windows or POSIX locks conflict ? */
350                 if (brl_conflict(&locks[i], plock)) {
351                         /* Remember who blocked us. */
352                         plock->context.smblctx = locks[i].context.smblctx;
353                         return brl_lock_failed(fsp,plock,blocking_lock);
354                 }
355 #if ZERO_ZERO
356                 if (plock->start == 0 && plock->size == 0 && 
357                                 locks[i].size == 0) {
358                         break;
359                 }
360 #endif
361         }
362
363         if (!IS_PENDING_LOCK(plock->lock_type)) {
364                 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
365         }
366
367         /* We can get the Windows lock, now see if it needs to
368            be mapped into a lower level POSIX one, and if so can
369            we get it ? */
370
371         if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(fsp->conn->params)) {
372                 int errno_ret;
373                 if (!set_posix_lock_windows_flavour(fsp,
374                                 plock->start,
375                                 plock->size,
376                                 plock->lock_type,
377                                 &plock->context,
378                                 locks,
379                                 br_lck->num_locks,
380                                 &errno_ret)) {
381
382                         /* We don't know who blocked us. */
383                         plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
384
385                         if (errno_ret == EACCES || errno_ret == EAGAIN) {
386                                 status = NT_STATUS_FILE_LOCK_CONFLICT;
387                                 goto fail;
388                         } else {
389                                 status = map_nt_error_from_unix(errno);
390                                 goto fail;
391                         }
392                 }
393         }
394
395         /* no conflicts - add it to the list of locks */
396         locks = (struct lock_struct *)SMB_REALLOC(locks, (br_lck->num_locks + 1) * sizeof(*locks));
397         if (!locks) {
398                 status = NT_STATUS_NO_MEMORY;
399                 goto fail;
400         }
401
402         memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
403         br_lck->num_locks += 1;
404         br_lck->lock_data = locks;
405         br_lck->modified = True;
406
407         return NT_STATUS_OK;
408  fail:
409         if (!IS_PENDING_LOCK(plock->lock_type)) {
410                 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
411         }
412         return status;
413 }
414
415 /****************************************************************************
416  Cope with POSIX range splits and merges.
417 ****************************************************************************/
418
419 static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr,       /* Output array. */
420                                                 struct lock_struct *ex,         /* existing lock. */
421                                                 struct lock_struct *plock)      /* proposed lock. */
422 {
423         bool lock_types_differ = (ex->lock_type != plock->lock_type);
424
425         /* We can't merge non-conflicting locks on different context - ignore fnum. */
426
427         if (!brl_same_context(&ex->context, &plock->context)) {
428                 /* Just copy. */
429                 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
430                 return 1;
431         }
432
433         /* We now know we have the same context. */
434
435         /* Did we overlap ? */
436
437 /*********************************************
438                                         +---------+
439                                         | ex      |
440                                         +---------+
441                          +-------+
442                          | plock |
443                          +-------+
444 OR....
445         +---------+
446         |  ex     |
447         +---------+
448 **********************************************/
449
450         if ( (ex->start > (plock->start + plock->size)) ||
451                 (plock->start > (ex->start + ex->size))) {
452
453                 /* No overlap with this lock - copy existing. */
454
455                 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
456                 return 1;
457         }
458
459 /*********************************************
460         +---------------------------+
461         |          ex               |
462         +---------------------------+
463         +---------------------------+
464         |       plock               | -> replace with plock.
465         +---------------------------+
466 OR
467              +---------------+
468              |       ex      |
469              +---------------+
470         +---------------------------+
471         |       plock               | -> replace with plock.
472         +---------------------------+
473
474 **********************************************/
475
476         if ( (ex->start >= plock->start) &&
477                 (ex->start + ex->size <= plock->start + plock->size) ) {
478
479                 /* Replace - discard existing lock. */
480
481                 return 0;
482         }
483
484 /*********************************************
485 Adjacent after.
486                         +-------+
487                         |  ex   |
488                         +-------+
489         +---------------+
490         |   plock       |
491         +---------------+
492
493 BECOMES....
494         +---------------+-------+
495         |   plock       | ex    | - different lock types.
496         +---------------+-------+
497 OR.... (merge)
498         +-----------------------+
499         |   plock               | - same lock type.
500         +-----------------------+
501 **********************************************/
502
503         if (plock->start + plock->size == ex->start) {
504
505                 /* If the lock types are the same, we merge, if different, we
506                    add the remainder of the old lock. */
507
508                 if (lock_types_differ) {
509                         /* Add existing. */
510                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
511                         return 1;
512                 } else {
513                         /* Merge - adjust incoming lock as we may have more
514                          * merging to come. */
515                         plock->size += ex->size;
516                         return 0;
517                 }
518         }
519
520 /*********************************************
521 Adjacent before.
522         +-------+
523         |  ex   |
524         +-------+
525                 +---------------+
526                 |   plock       |
527                 +---------------+
528 BECOMES....
529         +-------+---------------+
530         | ex    |   plock       | - different lock types
531         +-------+---------------+
532
533 OR.... (merge)
534         +-----------------------+
535         |      plock            | - same lock type.
536         +-----------------------+
537
538 **********************************************/
539
540         if (ex->start + ex->size == plock->start) {
541
542                 /* If the lock types are the same, we merge, if different, we
543                    add the existing lock. */
544
545                 if (lock_types_differ) {
546                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
547                         return 1;
548                 } else {
549                         /* Merge - adjust incoming lock as we may have more
550                          * merging to come. */
551                         plock->start = ex->start;
552                         plock->size += ex->size;
553                         return 0;
554                 }
555         }
556
557 /*********************************************
558 Overlap after.
559         +-----------------------+
560         |          ex           |
561         +-----------------------+
562         +---------------+
563         |   plock       |
564         +---------------+
565 OR
566                +----------------+
567                |       ex       |
568                +----------------+
569         +---------------+
570         |   plock       |
571         +---------------+
572
573 BECOMES....
574         +---------------+-------+
575         |   plock       | ex    | - different lock types.
576         +---------------+-------+
577 OR.... (merge)
578         +-----------------------+
579         |   plock               | - same lock type.
580         +-----------------------+
581 **********************************************/
582
583         if ( (ex->start >= plock->start) &&
584                 (ex->start <= plock->start + plock->size) &&
585                 (ex->start + ex->size > plock->start + plock->size) ) {
586
587                 /* If the lock types are the same, we merge, if different, we
588                    add the remainder of the old lock. */
589
590                 if (lock_types_differ) {
591                         /* Add remaining existing. */
592                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
593                         /* Adjust existing start and size. */
594                         lck_arr[0].start = plock->start + plock->size;
595                         lck_arr[0].size = (ex->start + ex->size) - (plock->start + plock->size);
596                         return 1;
597                 } else {
598                         /* Merge - adjust incoming lock as we may have more
599                          * merging to come. */
600                         plock->size += (ex->start + ex->size) - (plock->start + plock->size);
601                         return 0;
602                 }
603         }
604
605 /*********************************************
606 Overlap before.
607         +-----------------------+
608         |  ex                   |
609         +-----------------------+
610                 +---------------+
611                 |   plock       |
612                 +---------------+
613 OR
614         +-------------+
615         |  ex         |
616         +-------------+
617                 +---------------+
618                 |   plock       |
619                 +---------------+
620
621 BECOMES....
622         +-------+---------------+
623         | ex    |   plock       | - different lock types
624         +-------+---------------+
625
626 OR.... (merge)
627         +-----------------------+
628         |      plock            | - same lock type.
629         +-----------------------+
630
631 **********************************************/
632
633         if ( (ex->start < plock->start) &&
634                         (ex->start + ex->size >= plock->start) &&
635                         (ex->start + ex->size <= plock->start + plock->size) ) {
636
637                 /* If the lock types are the same, we merge, if different, we
638                    add the truncated old lock. */
639
640                 if (lock_types_differ) {
641                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
642                         /* Adjust existing size. */
643                         lck_arr[0].size = plock->start - ex->start;
644                         return 1;
645                 } else {
646                         /* Merge - adjust incoming lock as we may have more
647                          * merging to come. MUST ADJUST plock SIZE FIRST ! */
648                         plock->size += (plock->start - ex->start);
649                         plock->start = ex->start;
650                         return 0;
651                 }
652         }
653
654 /*********************************************
655 Complete overlap.
656         +---------------------------+
657         |        ex                 |
658         +---------------------------+
659                 +---------+
660                 |  plock  |
661                 +---------+
662 BECOMES.....
663         +-------+---------+---------+
664         | ex    |  plock  | ex      | - different lock types.
665         +-------+---------+---------+
666 OR
667         +---------------------------+
668         |        plock              | - same lock type.
669         +---------------------------+
670 **********************************************/
671
672         if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
673
674                 if (lock_types_differ) {
675
676                         /* We have to split ex into two locks here. */
677
678                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
679                         memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
680
681                         /* Adjust first existing size. */
682                         lck_arr[0].size = plock->start - ex->start;
683
684                         /* Adjust second existing start and size. */
685                         lck_arr[1].start = plock->start + plock->size;
686                         lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
687                         return 2;
688                 } else {
689                         /* Just eat the existing locks, merge them into plock. */
690                         plock->start = ex->start;
691                         plock->size = ex->size;
692                         return 0;
693                 }
694         }
695
696         /* Never get here. */
697         smb_panic("brlock_posix_split_merge");
698         /* Notreached. */
699
700         /* Keep some compilers happy. */
701         return 0;
702 }
703
704 /****************************************************************************
705  Lock a range of bytes - POSIX lock semantics.
706  We must cope with range splits and merges.
707 ****************************************************************************/
708
709 static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
710                                struct byte_range_lock *br_lck,
711                                struct lock_struct *plock)
712 {
713         unsigned int i, count, posix_count;
714         struct lock_struct *locks = br_lck->lock_data;
715         struct lock_struct *tp;
716         bool signal_pending_read = False;
717         bool break_oplocks = false;
718         NTSTATUS status;
719
720         /* No zero-zero locks for POSIX. */
721         if (plock->start == 0 && plock->size == 0) {
722                 return NT_STATUS_INVALID_PARAMETER;
723         }
724
725         /* Don't allow 64-bit lock wrap. */
726         if (plock->start + plock->size - 1 < plock->start) {
727                 return NT_STATUS_INVALID_PARAMETER;
728         }
729
730         /* The worst case scenario here is we have to split an
731            existing POSIX lock range into two, and add our lock,
732            so we need at most 2 more entries. */
733
734         tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 2));
735         if (!tp) {
736                 return NT_STATUS_NO_MEMORY;
737         }
738
739         count = posix_count = 0;
740
741         for (i=0; i < br_lck->num_locks; i++) {
742                 struct lock_struct *curr_lock = &locks[i];
743
744                 /* If we have a pending read lock, a lock downgrade should
745                    trigger a lock re-evaluation. */
746                 if (curr_lock->lock_type == PENDING_READ_LOCK &&
747                                 brl_pending_overlap(plock, curr_lock)) {
748                         signal_pending_read = True;
749                 }
750
751                 if (curr_lock->lock_flav == WINDOWS_LOCK) {
752                         /* Do any Windows flavour locks conflict ? */
753                         if (brl_conflict(curr_lock, plock)) {
754                                 /* No games with error messages. */
755                                 SAFE_FREE(tp);
756                                 /* Remember who blocked us. */
757                                 plock->context.smblctx = curr_lock->context.smblctx;
758                                 return NT_STATUS_FILE_LOCK_CONFLICT;
759                         }
760                         /* Just copy the Windows lock into the new array. */
761                         memcpy(&tp[count], curr_lock, sizeof(struct lock_struct));
762                         count++;
763                 } else {
764                         unsigned int tmp_count = 0;
765
766                         /* POSIX conflict semantics are different. */
767                         if (brl_conflict_posix(curr_lock, plock)) {
768                                 /* Can't block ourselves with POSIX locks. */
769                                 /* No games with error messages. */
770                                 SAFE_FREE(tp);
771                                 /* Remember who blocked us. */
772                                 plock->context.smblctx = curr_lock->context.smblctx;
773                                 return NT_STATUS_FILE_LOCK_CONFLICT;
774                         }
775
776                         /* Work out overlaps. */
777                         tmp_count += brlock_posix_split_merge(&tp[count], curr_lock, plock);
778                         posix_count += tmp_count;
779                         count += tmp_count;
780                 }
781         }
782
783         /*
784          * Break oplocks while we hold a brl. Since lock() and unlock() calls
785          * are not symetric with POSIX semantics, we cannot guarantee our
786          * contend_level2_oplocks_begin/end calls will be acquired and
787          * released one-for-one as with Windows semantics. Therefore we only
788          * call contend_level2_oplocks_begin if this is the first POSIX brl on
789          * the file.
790          */
791         break_oplocks = (!IS_PENDING_LOCK(plock->lock_type) &&
792                          posix_count == 0);
793         if (break_oplocks) {
794                 contend_level2_oplocks_begin(br_lck->fsp,
795                                              LEVEL2_CONTEND_POSIX_BRL);
796         }
797
798         /* Try and add the lock in order, sorted by lock start. */
799         for (i=0; i < count; i++) {
800                 struct lock_struct *curr_lock = &tp[i];
801
802                 if (curr_lock->start <= plock->start) {
803                         continue;
804                 }
805         }
806
807         if (i < count) {
808                 memmove(&tp[i+1], &tp[i],
809                         (count - i)*sizeof(struct lock_struct));
810         }
811         memcpy(&tp[i], plock, sizeof(struct lock_struct));
812         count++;
813
814         /* We can get the POSIX lock, now see if it needs to
815            be mapped into a lower level POSIX one, and if so can
816            we get it ? */
817
818         if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(br_lck->fsp->conn->params)) {
819                 int errno_ret;
820
821                 /* The lower layer just needs to attempt to
822                    get the system POSIX lock. We've weeded out
823                    any conflicts above. */
824
825                 if (!set_posix_lock_posix_flavour(br_lck->fsp,
826                                 plock->start,
827                                 plock->size,
828                                 plock->lock_type,
829                                 &errno_ret)) {
830
831                         /* We don't know who blocked us. */
832                         plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
833
834                         if (errno_ret == EACCES || errno_ret == EAGAIN) {
835                                 SAFE_FREE(tp);
836                                 status = NT_STATUS_FILE_LOCK_CONFLICT;
837                                 goto fail;
838                         } else {
839                                 SAFE_FREE(tp);
840                                 status = map_nt_error_from_unix(errno);
841                                 goto fail;
842                         }
843                 }
844         }
845
846         /* If we didn't use all the allocated size,
847          * Realloc so we don't leak entries per lock call. */
848         if (count < br_lck->num_locks + 2) {
849                 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
850                 if (!tp) {
851                         status = NT_STATUS_NO_MEMORY;
852                         goto fail;
853                 }
854         }
855
856         br_lck->num_locks = count;
857         SAFE_FREE(br_lck->lock_data);
858         br_lck->lock_data = tp;
859         locks = tp;
860         br_lck->modified = True;
861
862         /* A successful downgrade from write to read lock can trigger a lock
863            re-evalutation where waiting readers can now proceed. */
864
865         if (signal_pending_read) {
866                 /* Send unlock messages to any pending read waiters that overlap. */
867                 for (i=0; i < br_lck->num_locks; i++) {
868                         struct lock_struct *pend_lock = &locks[i];
869
870                         /* Ignore non-pending locks. */
871                         if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
872                                 continue;
873                         }
874
875                         if (pend_lock->lock_type == PENDING_READ_LOCK &&
876                                         brl_pending_overlap(plock, pend_lock)) {
877                                 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
878                                         procid_str_static(&pend_lock->context.pid )));
879
880                                 messaging_send(msg_ctx, pend_lock->context.pid,
881                                                MSG_SMB_UNLOCK, &data_blob_null);
882                         }
883                 }
884         }
885
886         return NT_STATUS_OK;
887  fail:
888         if (break_oplocks) {
889                 contend_level2_oplocks_end(br_lck->fsp,
890                                            LEVEL2_CONTEND_POSIX_BRL);
891         }
892         return status;
893 }
894
895 NTSTATUS smb_vfs_call_brl_lock_windows(struct vfs_handle_struct *handle,
896                                        struct byte_range_lock *br_lck,
897                                        struct lock_struct *plock,
898                                        bool blocking_lock,
899                                        struct blocking_lock_record *blr)
900 {
901         VFS_FIND(brl_lock_windows);
902         return handle->fns->brl_lock_windows_fn(handle, br_lck, plock,
903                                                 blocking_lock, blr);
904 }
905
906 /****************************************************************************
907  Lock a range of bytes.
908 ****************************************************************************/
909
910 NTSTATUS brl_lock(struct messaging_context *msg_ctx,
911                 struct byte_range_lock *br_lck,
912                 uint64_t smblctx,
913                 struct server_id pid,
914                 br_off start,
915                 br_off size, 
916                 enum brl_type lock_type,
917                 enum brl_flavour lock_flav,
918                 bool blocking_lock,
919                 uint64_t *psmblctx,
920                 struct blocking_lock_record *blr)
921 {
922         NTSTATUS ret;
923         struct lock_struct lock;
924
925 #if !ZERO_ZERO
926         if (start == 0 && size == 0) {
927                 DEBUG(0,("client sent 0/0 lock - please report this\n"));
928         }
929 #endif
930
931 #ifdef DEVELOPER
932         /* Quieten valgrind on test. */
933         memset(&lock, '\0', sizeof(lock));
934 #endif
935
936         lock.context.smblctx = smblctx;
937         lock.context.pid = pid;
938         lock.context.tid = br_lck->fsp->conn->cnum;
939         lock.start = start;
940         lock.size = size;
941         lock.fnum = br_lck->fsp->fnum;
942         lock.lock_type = lock_type;
943         lock.lock_flav = lock_flav;
944
945         if (lock_flav == WINDOWS_LOCK) {
946                 ret = SMB_VFS_BRL_LOCK_WINDOWS(br_lck->fsp->conn, br_lck,
947                     &lock, blocking_lock, blr);
948         } else {
949                 ret = brl_lock_posix(msg_ctx, br_lck, &lock);
950         }
951
952 #if ZERO_ZERO
953         /* sort the lock list */
954         TYPESAFE_QSORT(br_lck->lock_data, (size_t)br_lck->num_locks, lock_compare);
955 #endif
956
957         /* If we're returning an error, return who blocked us. */
958         if (!NT_STATUS_IS_OK(ret) && psmblctx) {
959                 *psmblctx = lock.context.smblctx;
960         }
961         return ret;
962 }
963
964 /****************************************************************************
965  Unlock a range of bytes - Windows semantics.
966 ****************************************************************************/
967
968 bool brl_unlock_windows_default(struct messaging_context *msg_ctx,
969                                struct byte_range_lock *br_lck,
970                                const struct lock_struct *plock)
971 {
972         unsigned int i, j;
973         struct lock_struct *locks = br_lck->lock_data;
974         enum brl_type deleted_lock_type = READ_LOCK; /* shut the compiler up.... */
975
976         SMB_ASSERT(plock->lock_type == UNLOCK_LOCK);
977
978 #if ZERO_ZERO
979         /* Delete write locks by preference... The lock list
980            is sorted in the zero zero case. */
981
982         for (i = 0; i < br_lck->num_locks; i++) {
983                 struct lock_struct *lock = &locks[i];
984
985                 if (lock->lock_type == WRITE_LOCK &&
986                     brl_same_context(&lock->context, &plock->context) &&
987                     lock->fnum == plock->fnum &&
988                     lock->lock_flav == WINDOWS_LOCK &&
989                     lock->start == plock->start &&
990                     lock->size == plock->size) {
991
992                         /* found it - delete it */
993                         deleted_lock_type = lock->lock_type;
994                         break;
995                 }
996         }
997
998         if (i != br_lck->num_locks) {
999                 /* We found it - don't search again. */
1000                 goto unlock_continue;
1001         }
1002 #endif
1003
1004         for (i = 0; i < br_lck->num_locks; i++) {
1005                 struct lock_struct *lock = &locks[i];
1006
1007                 if (IS_PENDING_LOCK(lock->lock_type)) {
1008                         continue;
1009                 }
1010
1011                 /* Only remove our own locks that match in start, size, and flavour. */
1012                 if (brl_same_context(&lock->context, &plock->context) &&
1013                                         lock->fnum == plock->fnum &&
1014                                         lock->lock_flav == WINDOWS_LOCK &&
1015                                         lock->start == plock->start &&
1016                                         lock->size == plock->size ) {
1017                         deleted_lock_type = lock->lock_type;
1018                         break;
1019                 }
1020         }
1021
1022         if (i == br_lck->num_locks) {
1023                 /* we didn't find it */
1024                 return False;
1025         }
1026
1027 #if ZERO_ZERO
1028   unlock_continue:
1029 #endif
1030
1031         /* Actually delete the lock. */
1032         if (i < br_lck->num_locks - 1) {
1033                 memmove(&locks[i], &locks[i+1], 
1034                         sizeof(*locks)*((br_lck->num_locks-1) - i));
1035         }
1036
1037         br_lck->num_locks -= 1;
1038         br_lck->modified = True;
1039
1040         /* Unlock the underlying POSIX regions. */
1041         if(lp_posix_locking(br_lck->fsp->conn->params)) {
1042                 release_posix_lock_windows_flavour(br_lck->fsp,
1043                                 plock->start,
1044                                 plock->size,
1045                                 deleted_lock_type,
1046                                 &plock->context,
1047                                 locks,
1048                                 br_lck->num_locks);
1049         }
1050
1051         /* Send unlock messages to any pending waiters that overlap. */
1052         for (j=0; j < br_lck->num_locks; j++) {
1053                 struct lock_struct *pend_lock = &locks[j];
1054
1055                 /* Ignore non-pending locks. */
1056                 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1057                         continue;
1058                 }
1059
1060                 /* We could send specific lock info here... */
1061                 if (brl_pending_overlap(plock, pend_lock)) {
1062                         DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1063                                 procid_str_static(&pend_lock->context.pid )));
1064
1065                         messaging_send(msg_ctx, pend_lock->context.pid,
1066                                        MSG_SMB_UNLOCK, &data_blob_null);
1067                 }
1068         }
1069
1070         contend_level2_oplocks_end(br_lck->fsp, LEVEL2_CONTEND_WINDOWS_BRL);
1071         return True;
1072 }
1073
1074 /****************************************************************************
1075  Unlock a range of bytes - POSIX semantics.
1076 ****************************************************************************/
1077
1078 static bool brl_unlock_posix(struct messaging_context *msg_ctx,
1079                              struct byte_range_lock *br_lck,
1080                              struct lock_struct *plock)
1081 {
1082         unsigned int i, j, count;
1083         struct lock_struct *tp;
1084         struct lock_struct *locks = br_lck->lock_data;
1085         bool overlap_found = False;
1086
1087         /* No zero-zero locks for POSIX. */
1088         if (plock->start == 0 && plock->size == 0) {
1089                 return False;
1090         }
1091
1092         /* Don't allow 64-bit lock wrap. */
1093         if (plock->start + plock->size < plock->start ||
1094                         plock->start + plock->size < plock->size) {
1095                 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
1096                 return False;
1097         }
1098
1099         /* The worst case scenario here is we have to split an
1100            existing POSIX lock range into two, so we need at most
1101            1 more entry. */
1102
1103         tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 1));
1104         if (!tp) {
1105                 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1106                 return False;
1107         }
1108
1109         count = 0;
1110         for (i = 0; i < br_lck->num_locks; i++) {
1111                 struct lock_struct *lock = &locks[i];
1112                 unsigned int tmp_count;
1113
1114                 /* Only remove our own locks - ignore fnum. */
1115                 if (IS_PENDING_LOCK(lock->lock_type) ||
1116                                 !brl_same_context(&lock->context, &plock->context)) {
1117                         memcpy(&tp[count], lock, sizeof(struct lock_struct));
1118                         count++;
1119                         continue;
1120                 }
1121
1122                 if (lock->lock_flav == WINDOWS_LOCK) {
1123                         /* Do any Windows flavour locks conflict ? */
1124                         if (brl_conflict(lock, plock)) {
1125                                 SAFE_FREE(tp);
1126                                 return false;
1127                         }
1128                         /* Just copy the Windows lock into the new array. */
1129                         memcpy(&tp[count], lock, sizeof(struct lock_struct));
1130                         count++;
1131                         continue;
1132                 }
1133
1134                 /* Work out overlaps. */
1135                 tmp_count = brlock_posix_split_merge(&tp[count], lock, plock);
1136
1137                 if (tmp_count == 0) {
1138                         /* plock overlapped the existing lock completely,
1139                            or replaced it. Don't copy the existing lock. */
1140                         overlap_found = true;
1141                 } else if (tmp_count == 1) {
1142                         /* Either no overlap, (simple copy of existing lock) or
1143                          * an overlap of an existing lock. */
1144                         /* If the lock changed size, we had an overlap. */
1145                         if (tp[count].size != lock->size) {
1146                                 overlap_found = true;
1147                         }
1148                         count += tmp_count;
1149                 } else if (tmp_count == 2) {
1150                         /* We split a lock range in two. */
1151                         overlap_found = true;
1152                         count += tmp_count;
1153
1154                         /* Optimisation... */
1155                         /* We know we're finished here as we can't overlap any
1156                            more POSIX locks. Copy the rest of the lock array. */
1157
1158                         if (i < br_lck->num_locks - 1) {
1159                                 memcpy(&tp[count], &locks[i+1],
1160                                         sizeof(*locks)*((br_lck->num_locks-1) - i));
1161                                 count += ((br_lck->num_locks-1) - i);
1162                         }
1163                         break;
1164                 }
1165
1166         }
1167
1168         if (!overlap_found) {
1169                 /* Just ignore - no change. */
1170                 SAFE_FREE(tp);
1171                 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1172                 return True;
1173         }
1174
1175         /* Unlock any POSIX regions. */
1176         if(lp_posix_locking(br_lck->fsp->conn->params)) {
1177                 release_posix_lock_posix_flavour(br_lck->fsp,
1178                                                 plock->start,
1179                                                 plock->size,
1180                                                 &plock->context,
1181                                                 tp,
1182                                                 count);
1183         }
1184
1185         /* Realloc so we don't leak entries per unlock call. */
1186         if (count) {
1187                 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
1188                 if (!tp) {
1189                         DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1190                         return False;
1191                 }
1192         } else {
1193                 /* We deleted the last lock. */
1194                 SAFE_FREE(tp);
1195                 tp = NULL;
1196         }
1197
1198         contend_level2_oplocks_end(br_lck->fsp,
1199                                    LEVEL2_CONTEND_POSIX_BRL);
1200
1201         br_lck->num_locks = count;
1202         SAFE_FREE(br_lck->lock_data);
1203         locks = tp;
1204         br_lck->lock_data = tp;
1205         br_lck->modified = True;
1206
1207         /* Send unlock messages to any pending waiters that overlap. */
1208
1209         for (j=0; j < br_lck->num_locks; j++) {
1210                 struct lock_struct *pend_lock = &locks[j];
1211
1212                 /* Ignore non-pending locks. */
1213                 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1214                         continue;
1215                 }
1216
1217                 /* We could send specific lock info here... */
1218                 if (brl_pending_overlap(plock, pend_lock)) {
1219                         DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1220                                 procid_str_static(&pend_lock->context.pid )));
1221
1222                         messaging_send(msg_ctx, pend_lock->context.pid,
1223                                        MSG_SMB_UNLOCK, &data_blob_null);
1224                 }
1225         }
1226
1227         return True;
1228 }
1229
1230 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct *handle,
1231                                      struct messaging_context *msg_ctx,
1232                                      struct byte_range_lock *br_lck,
1233                                      const struct lock_struct *plock)
1234 {
1235         VFS_FIND(brl_unlock_windows);
1236         return handle->fns->brl_unlock_windows_fn(handle, msg_ctx, br_lck, 
1237                                                   plock);
1238 }
1239
1240 /****************************************************************************
1241  Unlock a range of bytes.
1242 ****************************************************************************/
1243
1244 bool brl_unlock(struct messaging_context *msg_ctx,
1245                 struct byte_range_lock *br_lck,
1246                 uint64_t smblctx,
1247                 struct server_id pid,
1248                 br_off start,
1249                 br_off size,
1250                 enum brl_flavour lock_flav)
1251 {
1252         struct lock_struct lock;
1253
1254         lock.context.smblctx = smblctx;
1255         lock.context.pid = pid;
1256         lock.context.tid = br_lck->fsp->conn->cnum;
1257         lock.start = start;
1258         lock.size = size;
1259         lock.fnum = br_lck->fsp->fnum;
1260         lock.lock_type = UNLOCK_LOCK;
1261         lock.lock_flav = lock_flav;
1262
1263         if (lock_flav == WINDOWS_LOCK) {
1264                 return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck->fsp->conn, msg_ctx,
1265                     br_lck, &lock);
1266         } else {
1267                 return brl_unlock_posix(msg_ctx, br_lck, &lock);
1268         }
1269 }
1270
1271 /****************************************************************************
1272  Test if we could add a lock if we wanted to.
1273  Returns True if the region required is currently unlocked, False if locked.
1274 ****************************************************************************/
1275
1276 bool brl_locktest(struct byte_range_lock *br_lck,
1277                 uint64_t smblctx,
1278                 struct server_id pid,
1279                 br_off start,
1280                 br_off size, 
1281                 enum brl_type lock_type,
1282                 enum brl_flavour lock_flav)
1283 {
1284         bool ret = True;
1285         unsigned int i;
1286         struct lock_struct lock;
1287         const struct lock_struct *locks = br_lck->lock_data;
1288         files_struct *fsp = br_lck->fsp;
1289
1290         lock.context.smblctx = smblctx;
1291         lock.context.pid = pid;
1292         lock.context.tid = br_lck->fsp->conn->cnum;
1293         lock.start = start;
1294         lock.size = size;
1295         lock.fnum = fsp->fnum;
1296         lock.lock_type = lock_type;
1297         lock.lock_flav = lock_flav;
1298
1299         /* Make sure existing locks don't conflict */
1300         for (i=0; i < br_lck->num_locks; i++) {
1301                 /*
1302                  * Our own locks don't conflict.
1303                  */
1304                 if (brl_conflict_other(&locks[i], &lock)) {
1305                         return False;
1306                 }
1307         }
1308
1309         /*
1310          * There is no lock held by an SMB daemon, check to
1311          * see if there is a POSIX lock from a UNIX or NFS process.
1312          * This only conflicts with Windows locks, not POSIX locks.
1313          */
1314
1315         if(lp_posix_locking(fsp->conn->params) && (lock_flav == WINDOWS_LOCK)) {
1316                 ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
1317
1318                 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1319                         (double)start, (double)size, ret ? "locked" : "unlocked",
1320                         fsp->fnum, fsp_str_dbg(fsp)));
1321
1322                 /* We need to return the inverse of is_posix_locked. */
1323                 ret = !ret;
1324         }
1325
1326         /* no conflicts - we could have added it */
1327         return ret;
1328 }
1329
1330 /****************************************************************************
1331  Query for existing locks.
1332 ****************************************************************************/
1333
1334 NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
1335                 uint64_t *psmblctx,
1336                 struct server_id pid,
1337                 br_off *pstart,
1338                 br_off *psize, 
1339                 enum brl_type *plock_type,
1340                 enum brl_flavour lock_flav)
1341 {
1342         unsigned int i;
1343         struct lock_struct lock;
1344         const struct lock_struct *locks = br_lck->lock_data;
1345         files_struct *fsp = br_lck->fsp;
1346
1347         lock.context.smblctx = *psmblctx;
1348         lock.context.pid = pid;
1349         lock.context.tid = br_lck->fsp->conn->cnum;
1350         lock.start = *pstart;
1351         lock.size = *psize;
1352         lock.fnum = fsp->fnum;
1353         lock.lock_type = *plock_type;
1354         lock.lock_flav = lock_flav;
1355
1356         /* Make sure existing locks don't conflict */
1357         for (i=0; i < br_lck->num_locks; i++) {
1358                 const struct lock_struct *exlock = &locks[i];
1359                 bool conflict = False;
1360
1361                 if (exlock->lock_flav == WINDOWS_LOCK) {
1362                         conflict = brl_conflict(exlock, &lock);
1363                 } else {        
1364                         conflict = brl_conflict_posix(exlock, &lock);
1365                 }
1366
1367                 if (conflict) {
1368                         *psmblctx = exlock->context.smblctx;
1369                         *pstart = exlock->start;
1370                         *psize = exlock->size;
1371                         *plock_type = exlock->lock_type;
1372                         return NT_STATUS_LOCK_NOT_GRANTED;
1373                 }
1374         }
1375
1376         /*
1377          * There is no lock held by an SMB daemon, check to
1378          * see if there is a POSIX lock from a UNIX or NFS process.
1379          */
1380
1381         if(lp_posix_locking(fsp->conn->params)) {
1382                 bool ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
1383
1384                 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1385                         (double)*pstart, (double)*psize, ret ? "locked" : "unlocked",
1386                         fsp->fnum, fsp_str_dbg(fsp)));
1387
1388                 if (ret) {
1389                         /* Hmmm. No clue what to set smblctx to - use -1. */
1390                         *psmblctx = 0xFFFFFFFFFFFFFFFFLL;
1391                         return NT_STATUS_LOCK_NOT_GRANTED;
1392                 }
1393         }
1394
1395         return NT_STATUS_OK;
1396 }
1397
1398
1399 bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct *handle,
1400                                      struct byte_range_lock *br_lck,
1401                                      struct lock_struct *plock,
1402                                      struct blocking_lock_record *blr)
1403 {
1404         VFS_FIND(brl_cancel_windows);
1405         return handle->fns->brl_cancel_windows_fn(handle, br_lck, plock, blr);
1406 }
1407
1408 /****************************************************************************
1409  Remove a particular pending lock.
1410 ****************************************************************************/
1411 bool brl_lock_cancel(struct byte_range_lock *br_lck,
1412                 uint64_t smblctx,
1413                 struct server_id pid,
1414                 br_off start,
1415                 br_off size,
1416                 enum brl_flavour lock_flav,
1417                 struct blocking_lock_record *blr)
1418 {
1419         bool ret;
1420         struct lock_struct lock;
1421
1422         lock.context.smblctx = smblctx;
1423         lock.context.pid = pid;
1424         lock.context.tid = br_lck->fsp->conn->cnum;
1425         lock.start = start;
1426         lock.size = size;
1427         lock.fnum = br_lck->fsp->fnum;
1428         lock.lock_flav = lock_flav;
1429         /* lock.lock_type doesn't matter */
1430
1431         if (lock_flav == WINDOWS_LOCK) {
1432                 ret = SMB_VFS_BRL_CANCEL_WINDOWS(br_lck->fsp->conn, br_lck,
1433                     &lock, blr);
1434         } else {
1435                 ret = brl_lock_cancel_default(br_lck, &lock);
1436         }
1437
1438         return ret;
1439 }
1440
1441 bool brl_lock_cancel_default(struct byte_range_lock *br_lck,
1442                 struct lock_struct *plock)
1443 {
1444         unsigned int i;
1445         struct lock_struct *locks = br_lck->lock_data;
1446
1447         SMB_ASSERT(plock);
1448
1449         for (i = 0; i < br_lck->num_locks; i++) {
1450                 struct lock_struct *lock = &locks[i];
1451
1452                 /* For pending locks we *always* care about the fnum. */
1453                 if (brl_same_context(&lock->context, &plock->context) &&
1454                                 lock->fnum == plock->fnum &&
1455                                 IS_PENDING_LOCK(lock->lock_type) &&
1456                                 lock->lock_flav == plock->lock_flav &&
1457                                 lock->start == plock->start &&
1458                                 lock->size == plock->size) {
1459                         break;
1460                 }
1461         }
1462
1463         if (i == br_lck->num_locks) {
1464                 /* Didn't find it. */
1465                 return False;
1466         }
1467
1468         if (i < br_lck->num_locks - 1) {
1469                 /* Found this particular pending lock - delete it */
1470                 memmove(&locks[i], &locks[i+1], 
1471                         sizeof(*locks)*((br_lck->num_locks-1) - i));
1472         }
1473
1474         br_lck->num_locks -= 1;
1475         br_lck->modified = True;
1476         return True;
1477 }
1478
1479 /****************************************************************************
1480  Remove any locks associated with a open file.
1481  We return True if this process owns any other Windows locks on this
1482  fd and so we should not immediately close the fd.
1483 ****************************************************************************/
1484
1485 void brl_close_fnum(struct messaging_context *msg_ctx,
1486                     struct byte_range_lock *br_lck)
1487 {
1488         files_struct *fsp = br_lck->fsp;
1489         uint32_t tid = fsp->conn->cnum;
1490         int fnum = fsp->fnum;
1491         unsigned int i;
1492         struct lock_struct *locks = br_lck->lock_data;
1493         struct server_id pid = messaging_server_id(fsp->conn->sconn->msg_ctx);
1494         struct lock_struct *locks_copy;
1495         unsigned int num_locks_copy;
1496
1497         /* Copy the current lock array. */
1498         if (br_lck->num_locks) {
1499                 locks_copy = (struct lock_struct *)talloc_memdup(br_lck, locks, br_lck->num_locks * sizeof(struct lock_struct));
1500                 if (!locks_copy) {
1501                         smb_panic("brl_close_fnum: talloc failed");
1502                         }
1503         } else {        
1504                 locks_copy = NULL;
1505         }
1506
1507         num_locks_copy = br_lck->num_locks;
1508
1509         for (i=0; i < num_locks_copy; i++) {
1510                 struct lock_struct *lock = &locks_copy[i];
1511
1512                 if (lock->context.tid == tid && procid_equal(&lock->context.pid, &pid) &&
1513                                 (lock->fnum == fnum)) {
1514                         brl_unlock(msg_ctx,
1515                                 br_lck,
1516                                 lock->context.smblctx,
1517                                 pid,
1518                                 lock->start,
1519                                 lock->size,
1520                                 lock->lock_flav);
1521                 }
1522         }
1523 }
1524
1525 /****************************************************************************
1526  Ensure this set of lock entries is valid.
1527 ****************************************************************************/
1528 static bool validate_lock_entries(unsigned int *pnum_entries, struct lock_struct **pplocks)
1529 {
1530         unsigned int i;
1531         unsigned int num_valid_entries = 0;
1532         struct lock_struct *locks = *pplocks;
1533
1534         for (i = 0; i < *pnum_entries; i++) {
1535                 struct lock_struct *lock_data = &locks[i];
1536                 if (!serverid_exists(&lock_data->context.pid)) {
1537                         /* This process no longer exists - mark this
1538                            entry as invalid by zeroing it. */
1539                         ZERO_STRUCTP(lock_data);
1540                 } else {
1541                         num_valid_entries++;
1542                 }
1543         }
1544
1545         if (num_valid_entries != *pnum_entries) {
1546                 struct lock_struct *new_lock_data = NULL;
1547
1548                 if (num_valid_entries) {
1549                         new_lock_data = SMB_MALLOC_ARRAY(struct lock_struct, num_valid_entries);
1550                         if (!new_lock_data) {
1551                                 DEBUG(3, ("malloc fail\n"));
1552                                 return False;
1553                         }
1554
1555                         num_valid_entries = 0;
1556                         for (i = 0; i < *pnum_entries; i++) {
1557                                 struct lock_struct *lock_data = &locks[i];
1558                                 if (lock_data->context.smblctx &&
1559                                                 lock_data->context.tid) {
1560                                         /* Valid (nonzero) entry - copy it. */
1561                                         memcpy(&new_lock_data[num_valid_entries],
1562                                                 lock_data, sizeof(struct lock_struct));
1563                                         num_valid_entries++;
1564                                 }
1565                         }
1566                 }
1567
1568                 SAFE_FREE(*pplocks);
1569                 *pplocks = new_lock_data;
1570                 *pnum_entries = num_valid_entries;
1571         }
1572
1573         return True;
1574 }
1575
1576 struct brl_forall_cb {
1577         void (*fn)(struct file_id id, struct server_id pid,
1578                    enum brl_type lock_type,
1579                    enum brl_flavour lock_flav,
1580                    br_off start, br_off size,
1581                    void *private_data);
1582         void *private_data;
1583 };
1584
1585 /****************************************************************************
1586  Traverse the whole database with this function, calling traverse_callback
1587  on each lock.
1588 ****************************************************************************/
1589
1590 static int traverse_fn(struct db_record *rec, void *state)
1591 {
1592         struct brl_forall_cb *cb = (struct brl_forall_cb *)state;
1593         struct lock_struct *locks;
1594         struct file_id *key;
1595         unsigned int i;
1596         unsigned int num_locks = 0;
1597         unsigned int orig_num_locks = 0;
1598         TDB_DATA dbkey;
1599         TDB_DATA value;
1600
1601         dbkey = dbwrap_record_get_key(rec);
1602         value = dbwrap_record_get_value(rec);
1603
1604         /* In a traverse function we must make a copy of
1605            dbuf before modifying it. */
1606
1607         locks = (struct lock_struct *)memdup(value.dptr, value.dsize);
1608         if (!locks) {
1609                 return -1; /* Terminate traversal. */
1610         }
1611
1612         key = (struct file_id *)dbkey.dptr;
1613         orig_num_locks = num_locks = value.dsize/sizeof(*locks);
1614
1615         /* Ensure the lock db is clean of entries from invalid processes. */
1616
1617         if (!validate_lock_entries(&num_locks, &locks)) {
1618                 SAFE_FREE(locks);
1619                 return -1; /* Terminate traversal */
1620         }
1621
1622         if (orig_num_locks != num_locks) {
1623                 if (num_locks) {
1624                         TDB_DATA data;
1625                         data.dptr = (uint8_t *)locks;
1626                         data.dsize = num_locks*sizeof(struct lock_struct);
1627                         dbwrap_record_store(rec, data, TDB_REPLACE);
1628                 } else {
1629                         dbwrap_record_delete(rec);
1630                 }
1631         }
1632
1633         if (cb->fn) {
1634                 for ( i=0; i<num_locks; i++) {
1635                         cb->fn(*key,
1636                                 locks[i].context.pid,
1637                                 locks[i].lock_type,
1638                                 locks[i].lock_flav,
1639                                 locks[i].start,
1640                                 locks[i].size,
1641                                 cb->private_data);
1642                 }
1643         }
1644
1645         SAFE_FREE(locks);
1646         return 0;
1647 }
1648
1649 /*******************************************************************
1650  Call the specified function on each lock in the database.
1651 ********************************************************************/
1652
1653 int brl_forall(void (*fn)(struct file_id id, struct server_id pid,
1654                           enum brl_type lock_type,
1655                           enum brl_flavour lock_flav,
1656                           br_off start, br_off size,
1657                           void *private_data),
1658                void *private_data)
1659 {
1660         struct brl_forall_cb cb;
1661         NTSTATUS status;
1662         int count = 0;
1663
1664         if (!brlock_db) {
1665                 return 0;
1666         }
1667         cb.fn = fn;
1668         cb.private_data = private_data;
1669         status = dbwrap_traverse(brlock_db, traverse_fn, &cb, &count);
1670
1671         if (!NT_STATUS_IS_OK(status)) {
1672                 return -1;
1673         } else {
1674                 return count;
1675         }
1676 }
1677
1678 /*******************************************************************
1679  Store a potentially modified set of byte range lock data back into
1680  the database.
1681  Unlock the record.
1682 ********************************************************************/
1683
1684 static void byte_range_lock_flush(struct byte_range_lock *br_lck)
1685 {
1686         if (br_lck->read_only) {
1687                 SMB_ASSERT(!br_lck->modified);
1688         }
1689
1690         if (!br_lck->modified) {
1691                 goto done;
1692         }
1693
1694         if (br_lck->num_locks == 0) {
1695                 /* No locks - delete this entry. */
1696                 NTSTATUS status = dbwrap_record_delete(br_lck->record);
1697                 if (!NT_STATUS_IS_OK(status)) {
1698                         DEBUG(0, ("delete_rec returned %s\n",
1699                                   nt_errstr(status)));
1700                         smb_panic("Could not delete byte range lock entry");
1701                 }
1702         } else {
1703                 TDB_DATA data;
1704                 NTSTATUS status;
1705
1706                 data.dptr = (uint8 *)br_lck->lock_data;
1707                 data.dsize = br_lck->num_locks * sizeof(struct lock_struct);
1708
1709                 status = dbwrap_record_store(br_lck->record, data, TDB_REPLACE);
1710                 if (!NT_STATUS_IS_OK(status)) {
1711                         DEBUG(0, ("store returned %s\n", nt_errstr(status)));
1712                         smb_panic("Could not store byte range mode entry");
1713                 }
1714         }
1715
1716  done:
1717
1718         br_lck->read_only = true;
1719         br_lck->modified = false;
1720
1721         TALLOC_FREE(br_lck->record);
1722 }
1723
1724 static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
1725 {
1726         byte_range_lock_flush(br_lck);
1727         SAFE_FREE(br_lck->lock_data);
1728         return 0;
1729 }
1730
1731 /*******************************************************************
1732  Fetch a set of byte range lock data from the database.
1733  Leave the record locked.
1734  TALLOC_FREE(brl) will release the lock in the destructor.
1735 ********************************************************************/
1736
1737 static struct byte_range_lock *brl_get_locks_internal(TALLOC_CTX *mem_ctx,
1738                                         files_struct *fsp, bool read_only)
1739 {
1740         TDB_DATA key, data;
1741         struct byte_range_lock *br_lck = talloc(mem_ctx, struct byte_range_lock);
1742         bool do_read_only = read_only;
1743
1744         if (br_lck == NULL) {
1745                 return NULL;
1746         }
1747
1748         br_lck->fsp = fsp;
1749         br_lck->num_locks = 0;
1750         br_lck->modified = False;
1751         br_lck->key = fsp->file_id;
1752
1753         key.dptr = (uint8 *)&br_lck->key;
1754         key.dsize = sizeof(struct file_id);
1755
1756         if (!fsp->lockdb_clean) {
1757                 /* We must be read/write to clean
1758                    the dead entries. */
1759                 do_read_only = false;
1760         }
1761
1762         if (do_read_only) {
1763                 NTSTATUS status;
1764                 status = dbwrap_fetch(brlock_db, br_lck, key, &data);
1765                 if (!NT_STATUS_IS_OK(status)) {
1766                         DEBUG(3, ("Could not fetch byte range lock record\n"));
1767                         TALLOC_FREE(br_lck);
1768                         return NULL;
1769                 }
1770                 br_lck->record = NULL;
1771         } else {
1772                 br_lck->record = dbwrap_fetch_locked(brlock_db, br_lck, key);
1773
1774                 if (br_lck->record == NULL) {
1775                         DEBUG(3, ("Could not lock byte range lock entry\n"));
1776                         TALLOC_FREE(br_lck);
1777                         return NULL;
1778                 }
1779
1780                 data = dbwrap_record_get_value(br_lck->record);
1781         }
1782
1783         br_lck->read_only = do_read_only;
1784         br_lck->lock_data = NULL;
1785
1786         talloc_set_destructor(br_lck, byte_range_lock_destructor);
1787
1788         br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
1789
1790         if (br_lck->num_locks != 0) {
1791                 br_lck->lock_data = SMB_MALLOC_ARRAY(struct lock_struct,
1792                                                      br_lck->num_locks);
1793                 if (br_lck->lock_data == NULL) {
1794                         DEBUG(0, ("malloc failed\n"));
1795                         TALLOC_FREE(br_lck);
1796                         return NULL;
1797                 }
1798
1799                 memcpy(br_lck->lock_data, data.dptr, data.dsize);
1800         }
1801
1802         if (!fsp->lockdb_clean) {
1803                 int orig_num_locks = br_lck->num_locks;
1804
1805                 /* This is the first time we've accessed this. */
1806                 /* Go through and ensure all entries exist - remove any that don't. */
1807                 /* Makes the lockdb self cleaning at low cost. */
1808
1809                 if (!validate_lock_entries(&br_lck->num_locks,
1810                                            &br_lck->lock_data)) {
1811                         SAFE_FREE(br_lck->lock_data);
1812                         TALLOC_FREE(br_lck);
1813                         return NULL;
1814                 }
1815
1816                 /* Ensure invalid locks are cleaned up in the destructor. */
1817                 if (orig_num_locks != br_lck->num_locks) {
1818                         br_lck->modified = True;
1819                 }
1820
1821                 /* Mark the lockdb as "clean" as seen from this open file. */
1822                 fsp->lockdb_clean = True;
1823         }
1824
1825         if (DEBUGLEVEL >= 10) {
1826                 unsigned int i;
1827                 struct lock_struct *locks = br_lck->lock_data;
1828                 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
1829                         br_lck->num_locks,
1830                           file_id_string_tos(&fsp->file_id)));
1831                 for( i = 0; i < br_lck->num_locks; i++) {
1832                         print_lock_struct(i, &locks[i]);
1833                 }
1834         }
1835
1836         if (do_read_only != read_only) {
1837                 /*
1838                  * this stores the record and gets rid of
1839                  * the write lock that is needed for a cleanup
1840                  */
1841                 byte_range_lock_flush(br_lck);
1842         }
1843
1844         return br_lck;
1845 }
1846
1847 struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx,
1848                                         files_struct *fsp)
1849 {
1850         return brl_get_locks_internal(mem_ctx, fsp, False);
1851 }
1852
1853 struct byte_range_lock *brl_get_locks_readonly(files_struct *fsp)
1854 {
1855         struct byte_range_lock *br_lock;
1856
1857         if (lp_clustering()) {
1858                 return brl_get_locks_internal(talloc_tos(), fsp, true);
1859         }
1860
1861         if ((fsp->brlock_rec != NULL)
1862             && (dbwrap_get_seqnum(brlock_db) == fsp->brlock_seqnum)) {
1863                 return fsp->brlock_rec;
1864         }
1865
1866         TALLOC_FREE(fsp->brlock_rec);
1867
1868         br_lock = brl_get_locks_internal(talloc_tos(), fsp, true);
1869         if (br_lock == NULL) {
1870                 return NULL;
1871         }
1872         fsp->brlock_seqnum = dbwrap_get_seqnum(brlock_db);
1873
1874         fsp->brlock_rec = talloc_move(fsp, &br_lock);
1875
1876         return fsp->brlock_rec;
1877 }
1878
1879 struct brl_revalidate_state {
1880         ssize_t array_size;
1881         uint32 num_pids;
1882         struct server_id *pids;
1883 };
1884
1885 /*
1886  * Collect PIDs of all processes with pending entries
1887  */
1888
1889 static void brl_revalidate_collect(struct file_id id, struct server_id pid,
1890                                    enum brl_type lock_type,
1891                                    enum brl_flavour lock_flav,
1892                                    br_off start, br_off size,
1893                                    void *private_data)
1894 {
1895         struct brl_revalidate_state *state =
1896                 (struct brl_revalidate_state *)private_data;
1897
1898         if (!IS_PENDING_LOCK(lock_type)) {
1899                 return;
1900         }
1901
1902         add_to_large_array(state, sizeof(pid), (void *)&pid,
1903                            &state->pids, &state->num_pids,
1904                            &state->array_size);
1905 }
1906
1907 /*
1908  * qsort callback to sort the processes
1909  */
1910
1911 static int compare_procids(const void *p1, const void *p2)
1912 {
1913         const struct server_id *i1 = (const struct server_id *)p1;
1914         const struct server_id *i2 = (const struct server_id *)p2;
1915
1916         if (i1->pid < i2->pid) return -1;
1917         if (i2->pid > i2->pid) return 1;
1918         return 0;
1919 }
1920
1921 /*
1922  * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
1923  * locks so that they retry. Mainly used in the cluster code after a node has
1924  * died.
1925  *
1926  * Done in two steps to avoid double-sends: First we collect all entries in an
1927  * array, then qsort that array and only send to non-dupes.
1928  */
1929
1930 void brl_revalidate(struct messaging_context *msg_ctx,
1931                     void *private_data,
1932                     uint32_t msg_type,
1933                     struct server_id server_id,
1934                     DATA_BLOB *data)
1935 {
1936         struct brl_revalidate_state *state;
1937         uint32 i;
1938         struct server_id last_pid;
1939
1940         if (!(state = talloc_zero(NULL, struct brl_revalidate_state))) {
1941                 DEBUG(0, ("talloc failed\n"));
1942                 return;
1943         }
1944
1945         brl_forall(brl_revalidate_collect, state);
1946
1947         if (state->array_size == -1) {
1948                 DEBUG(0, ("talloc failed\n"));
1949                 goto done;
1950         }
1951
1952         if (state->num_pids == 0) {
1953                 goto done;
1954         }
1955
1956         TYPESAFE_QSORT(state->pids, state->num_pids, compare_procids);
1957
1958         ZERO_STRUCT(last_pid);
1959
1960         for (i=0; i<state->num_pids; i++) {
1961                 if (procid_equal(&last_pid, &state->pids[i])) {
1962                         /*
1963                          * We've seen that one already
1964                          */
1965                         continue;
1966                 }
1967
1968                 messaging_send(msg_ctx, state->pids[i], MSG_SMB_UNLOCK,
1969                                &data_blob_null);
1970                 last_pid = state->pids[i];
1971         }
1972
1973  done:
1974         TALLOC_FREE(state);
1975         return;
1976 }