dns: Use new DNS debugclass in DNS server
[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, const 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 (serverid_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 (serverid_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 %s file %s\n",
1319                         (double)start, (double)size, ret ? "locked" : "unlocked",
1320                         fsp_fnum_dbg(fsp), 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 %s file %s\n",
1385                         (double)*pstart, (double)*psize, ret ? "locked" : "unlocked",
1386                         fsp_fnum_dbg(fsp), 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         uint64_t 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 && serverid_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 bool brl_mark_disconnected(struct files_struct *fsp)
1526 {
1527         uint32_t tid = fsp->conn->cnum;
1528         uint64_t smblctx = fsp->op->global->open_persistent_id;
1529         uint64_t fnum = fsp->fnum;
1530         unsigned int i;
1531         struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1532         struct byte_range_lock *br_lck = NULL;
1533
1534         if (!fsp->op->global->durable) {
1535                 return false;
1536         }
1537
1538         if (fsp->current_lock_count == 0) {
1539                 return true;
1540         }
1541
1542         br_lck = brl_get_locks(talloc_tos(), fsp);
1543         if (br_lck == NULL) {
1544                 return false;
1545         }
1546
1547         for (i=0; i < br_lck->num_locks; i++) {
1548                 struct lock_struct *lock = &br_lck->lock_data[i];
1549
1550                 /*
1551                  * as this is a durable handle, we only expect locks
1552                  * of the current file handle!
1553                  */
1554
1555                 if (lock->context.smblctx != smblctx) {
1556                         TALLOC_FREE(br_lck);
1557                         return false;
1558                 }
1559
1560                 if (lock->context.tid != tid) {
1561                         TALLOC_FREE(br_lck);
1562                         return false;
1563                 }
1564
1565                 if (!serverid_equal(&lock->context.pid, &self)) {
1566                         TALLOC_FREE(br_lck);
1567                         return false;
1568                 }
1569
1570                 if (lock->fnum != fnum) {
1571                         TALLOC_FREE(br_lck);
1572                         return false;
1573                 }
1574
1575                 server_id_set_disconnected(&lock->context.pid);
1576                 lock->context.tid = TID_FIELD_INVALID;
1577                 lock->fnum = FNUM_FIELD_INVALID;
1578         }
1579
1580         br_lck->modified = true;
1581         TALLOC_FREE(br_lck);
1582         return true;
1583 }
1584
1585 bool brl_reconnect_disconnected(struct files_struct *fsp)
1586 {
1587         uint32_t tid = fsp->conn->cnum;
1588         uint64_t smblctx = fsp->op->global->open_persistent_id;
1589         uint64_t fnum = fsp->fnum;
1590         unsigned int i;
1591         struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1592         struct byte_range_lock *br_lck = NULL;
1593
1594         if (!fsp->op->global->durable) {
1595                 return false;
1596         }
1597
1598         /* we want to validate ourself */
1599         fsp->lockdb_clean = true;
1600
1601         br_lck = brl_get_locks(talloc_tos(), fsp);
1602         if (br_lck == NULL) {
1603                 return false;
1604         }
1605
1606         if (br_lck->num_locks == 0) {
1607                 TALLOC_FREE(br_lck);
1608                 return true;
1609         }
1610
1611         for (i=0; i < br_lck->num_locks; i++) {
1612                 struct lock_struct *lock = &br_lck->lock_data[i];
1613
1614                 /*
1615                  * as this is a durable handle we only expect locks
1616                  * of the current file handle!
1617                  */
1618
1619                 if (lock->context.smblctx != smblctx) {
1620                         TALLOC_FREE(br_lck);
1621                         return false;
1622                 }
1623
1624                 if (lock->context.tid != TID_FIELD_INVALID) {
1625                         TALLOC_FREE(br_lck);
1626                         return false;
1627                 }
1628
1629                 if (!server_id_is_disconnected(&lock->context.pid)) {
1630                         TALLOC_FREE(br_lck);
1631                         return false;
1632                 }
1633
1634                 if (lock->fnum != FNUM_FIELD_INVALID) {
1635                         TALLOC_FREE(br_lck);
1636                         return false;
1637                 }
1638
1639                 lock->context.pid = self;
1640                 lock->context.tid = tid;
1641                 lock->fnum = fnum;
1642         }
1643
1644         fsp->current_lock_count = br_lck->num_locks;
1645         br_lck->modified = true;
1646         TALLOC_FREE(br_lck);
1647         return true;
1648 }
1649
1650 /****************************************************************************
1651  Ensure this set of lock entries is valid.
1652 ****************************************************************************/
1653 static bool validate_lock_entries(unsigned int *pnum_entries, struct lock_struct **pplocks)
1654 {
1655         unsigned int i;
1656         unsigned int num_valid_entries = 0;
1657         struct lock_struct *locks = *pplocks;
1658
1659         for (i = 0; i < *pnum_entries; i++) {
1660                 struct lock_struct *lock_data = &locks[i];
1661                 if (!serverid_exists(&lock_data->context.pid)) {
1662                         /* This process no longer exists - mark this
1663                            entry as invalid by zeroing it. */
1664                         ZERO_STRUCTP(lock_data);
1665                 } else {
1666                         num_valid_entries++;
1667                 }
1668         }
1669
1670         if (num_valid_entries != *pnum_entries) {
1671                 struct lock_struct *new_lock_data = NULL;
1672
1673                 if (num_valid_entries) {
1674                         new_lock_data = SMB_MALLOC_ARRAY(struct lock_struct, num_valid_entries);
1675                         if (!new_lock_data) {
1676                                 DEBUG(3, ("malloc fail\n"));
1677                                 return False;
1678                         }
1679
1680                         num_valid_entries = 0;
1681                         for (i = 0; i < *pnum_entries; i++) {
1682                                 struct lock_struct *lock_data = &locks[i];
1683                                 if (lock_data->context.smblctx &&
1684                                                 lock_data->context.tid) {
1685                                         /* Valid (nonzero) entry - copy it. */
1686                                         memcpy(&new_lock_data[num_valid_entries],
1687                                                 lock_data, sizeof(struct lock_struct));
1688                                         num_valid_entries++;
1689                                 }
1690                         }
1691                 }
1692
1693                 SAFE_FREE(*pplocks);
1694                 *pplocks = new_lock_data;
1695                 *pnum_entries = num_valid_entries;
1696         }
1697
1698         return True;
1699 }
1700
1701 struct brl_forall_cb {
1702         void (*fn)(struct file_id id, struct server_id pid,
1703                    enum brl_type lock_type,
1704                    enum brl_flavour lock_flav,
1705                    br_off start, br_off size,
1706                    void *private_data);
1707         void *private_data;
1708 };
1709
1710 /****************************************************************************
1711  Traverse the whole database with this function, calling traverse_callback
1712  on each lock.
1713 ****************************************************************************/
1714
1715 static int brl_traverse_fn(struct db_record *rec, void *state)
1716 {
1717         struct brl_forall_cb *cb = (struct brl_forall_cb *)state;
1718         struct lock_struct *locks;
1719         struct file_id *key;
1720         unsigned int i;
1721         unsigned int num_locks = 0;
1722         unsigned int orig_num_locks = 0;
1723         TDB_DATA dbkey;
1724         TDB_DATA value;
1725
1726         dbkey = dbwrap_record_get_key(rec);
1727         value = dbwrap_record_get_value(rec);
1728
1729         /* In a traverse function we must make a copy of
1730            dbuf before modifying it. */
1731
1732         locks = (struct lock_struct *)memdup(value.dptr, value.dsize);
1733         if (!locks) {
1734                 return -1; /* Terminate traversal. */
1735         }
1736
1737         key = (struct file_id *)dbkey.dptr;
1738         orig_num_locks = num_locks = value.dsize/sizeof(*locks);
1739
1740         /* Ensure the lock db is clean of entries from invalid processes. */
1741
1742         if (!validate_lock_entries(&num_locks, &locks)) {
1743                 SAFE_FREE(locks);
1744                 return -1; /* Terminate traversal */
1745         }
1746
1747         if (orig_num_locks != num_locks) {
1748                 if (num_locks) {
1749                         TDB_DATA data;
1750                         data.dptr = (uint8_t *)locks;
1751                         data.dsize = num_locks*sizeof(struct lock_struct);
1752                         dbwrap_record_store(rec, data, TDB_REPLACE);
1753                 } else {
1754                         dbwrap_record_delete(rec);
1755                 }
1756         }
1757
1758         if (cb->fn) {
1759                 for ( i=0; i<num_locks; i++) {
1760                         cb->fn(*key,
1761                                 locks[i].context.pid,
1762                                 locks[i].lock_type,
1763                                 locks[i].lock_flav,
1764                                 locks[i].start,
1765                                 locks[i].size,
1766                                 cb->private_data);
1767                 }
1768         }
1769
1770         SAFE_FREE(locks);
1771         return 0;
1772 }
1773
1774 /*******************************************************************
1775  Call the specified function on each lock in the database.
1776 ********************************************************************/
1777
1778 int brl_forall(void (*fn)(struct file_id id, struct server_id pid,
1779                           enum brl_type lock_type,
1780                           enum brl_flavour lock_flav,
1781                           br_off start, br_off size,
1782                           void *private_data),
1783                void *private_data)
1784 {
1785         struct brl_forall_cb cb;
1786         NTSTATUS status;
1787         int count = 0;
1788
1789         if (!brlock_db) {
1790                 return 0;
1791         }
1792         cb.fn = fn;
1793         cb.private_data = private_data;
1794         status = dbwrap_traverse(brlock_db, brl_traverse_fn, &cb, &count);
1795
1796         if (!NT_STATUS_IS_OK(status)) {
1797                 return -1;
1798         } else {
1799                 return count;
1800         }
1801 }
1802
1803 /*******************************************************************
1804  Store a potentially modified set of byte range lock data back into
1805  the database.
1806  Unlock the record.
1807 ********************************************************************/
1808
1809 static void byte_range_lock_flush(struct byte_range_lock *br_lck)
1810 {
1811         if (br_lck->read_only) {
1812                 SMB_ASSERT(!br_lck->modified);
1813         }
1814
1815         if (!br_lck->modified) {
1816                 goto done;
1817         }
1818
1819         if (br_lck->num_locks == 0) {
1820                 /* No locks - delete this entry. */
1821                 NTSTATUS status = dbwrap_record_delete(br_lck->record);
1822                 if (!NT_STATUS_IS_OK(status)) {
1823                         DEBUG(0, ("delete_rec returned %s\n",
1824                                   nt_errstr(status)));
1825                         smb_panic("Could not delete byte range lock entry");
1826                 }
1827         } else {
1828                 TDB_DATA data;
1829                 NTSTATUS status;
1830
1831                 data.dptr = (uint8 *)br_lck->lock_data;
1832                 data.dsize = br_lck->num_locks * sizeof(struct lock_struct);
1833
1834                 status = dbwrap_record_store(br_lck->record, data, TDB_REPLACE);
1835                 if (!NT_STATUS_IS_OK(status)) {
1836                         DEBUG(0, ("store returned %s\n", nt_errstr(status)));
1837                         smb_panic("Could not store byte range mode entry");
1838                 }
1839         }
1840
1841  done:
1842
1843         br_lck->read_only = true;
1844         br_lck->modified = false;
1845
1846         TALLOC_FREE(br_lck->record);
1847 }
1848
1849 static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
1850 {
1851         byte_range_lock_flush(br_lck);
1852         SAFE_FREE(br_lck->lock_data);
1853         return 0;
1854 }
1855
1856 /*******************************************************************
1857  Fetch a set of byte range lock data from the database.
1858  Leave the record locked.
1859  TALLOC_FREE(brl) will release the lock in the destructor.
1860 ********************************************************************/
1861
1862 static struct byte_range_lock *brl_get_locks_internal(TALLOC_CTX *mem_ctx,
1863                                         files_struct *fsp, bool read_only)
1864 {
1865         TDB_DATA key, data;
1866         struct byte_range_lock *br_lck = talloc(mem_ctx, struct byte_range_lock);
1867         bool do_read_only = read_only;
1868
1869         if (br_lck == NULL) {
1870                 return NULL;
1871         }
1872
1873         br_lck->fsp = fsp;
1874         br_lck->num_locks = 0;
1875         br_lck->modified = False;
1876         br_lck->key = fsp->file_id;
1877
1878         key.dptr = (uint8 *)&br_lck->key;
1879         key.dsize = sizeof(struct file_id);
1880
1881         if (!fsp->lockdb_clean) {
1882                 /* We must be read/write to clean
1883                    the dead entries. */
1884                 do_read_only = false;
1885         }
1886
1887         if (do_read_only) {
1888                 NTSTATUS status;
1889                 status = dbwrap_fetch(brlock_db, br_lck, key, &data);
1890                 if (!NT_STATUS_IS_OK(status)) {
1891                         DEBUG(3, ("Could not fetch byte range lock record\n"));
1892                         TALLOC_FREE(br_lck);
1893                         return NULL;
1894                 }
1895                 br_lck->record = NULL;
1896         } else {
1897                 br_lck->record = dbwrap_fetch_locked(brlock_db, br_lck, key);
1898
1899                 if (br_lck->record == NULL) {
1900                         DEBUG(3, ("Could not lock byte range lock entry\n"));
1901                         TALLOC_FREE(br_lck);
1902                         return NULL;
1903                 }
1904
1905                 data = dbwrap_record_get_value(br_lck->record);
1906         }
1907
1908         br_lck->read_only = do_read_only;
1909         br_lck->lock_data = NULL;
1910
1911         talloc_set_destructor(br_lck, byte_range_lock_destructor);
1912
1913         br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
1914
1915         if (br_lck->num_locks != 0) {
1916                 br_lck->lock_data = SMB_MALLOC_ARRAY(struct lock_struct,
1917                                                      br_lck->num_locks);
1918                 if (br_lck->lock_data == NULL) {
1919                         DEBUG(0, ("malloc failed\n"));
1920                         TALLOC_FREE(br_lck);
1921                         return NULL;
1922                 }
1923
1924                 memcpy(br_lck->lock_data, data.dptr, data.dsize);
1925         }
1926
1927         if (!fsp->lockdb_clean) {
1928                 int orig_num_locks = br_lck->num_locks;
1929
1930                 /* This is the first time we've accessed this. */
1931                 /* Go through and ensure all entries exist - remove any that don't. */
1932                 /* Makes the lockdb self cleaning at low cost. */
1933
1934                 if (!validate_lock_entries(&br_lck->num_locks,
1935                                            &br_lck->lock_data)) {
1936                         SAFE_FREE(br_lck->lock_data);
1937                         TALLOC_FREE(br_lck);
1938                         return NULL;
1939                 }
1940
1941                 /* Ensure invalid locks are cleaned up in the destructor. */
1942                 if (orig_num_locks != br_lck->num_locks) {
1943                         br_lck->modified = True;
1944                 }
1945
1946                 /* Mark the lockdb as "clean" as seen from this open file. */
1947                 fsp->lockdb_clean = True;
1948         }
1949
1950         if (DEBUGLEVEL >= 10) {
1951                 unsigned int i;
1952                 struct lock_struct *locks = br_lck->lock_data;
1953                 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
1954                         br_lck->num_locks,
1955                           file_id_string_tos(&fsp->file_id)));
1956                 for( i = 0; i < br_lck->num_locks; i++) {
1957                         print_lock_struct(i, &locks[i]);
1958                 }
1959         }
1960
1961         if (do_read_only != read_only) {
1962                 /*
1963                  * this stores the record and gets rid of
1964                  * the write lock that is needed for a cleanup
1965                  */
1966                 byte_range_lock_flush(br_lck);
1967         }
1968
1969         return br_lck;
1970 }
1971
1972 struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx,
1973                                         files_struct *fsp)
1974 {
1975         return brl_get_locks_internal(mem_ctx, fsp, False);
1976 }
1977
1978 struct byte_range_lock *brl_get_locks_readonly(files_struct *fsp)
1979 {
1980         struct byte_range_lock *br_lock;
1981
1982         if (lp_clustering()) {
1983                 return brl_get_locks_internal(talloc_tos(), fsp, true);
1984         }
1985
1986         if ((fsp->brlock_rec != NULL)
1987             && (dbwrap_get_seqnum(brlock_db) == fsp->brlock_seqnum)) {
1988                 return fsp->brlock_rec;
1989         }
1990
1991         TALLOC_FREE(fsp->brlock_rec);
1992
1993         br_lock = brl_get_locks_internal(talloc_tos(), fsp, true);
1994         if (br_lock == NULL) {
1995                 return NULL;
1996         }
1997         fsp->brlock_seqnum = dbwrap_get_seqnum(brlock_db);
1998
1999         fsp->brlock_rec = talloc_move(fsp, &br_lock);
2000
2001         return fsp->brlock_rec;
2002 }
2003
2004 struct brl_revalidate_state {
2005         ssize_t array_size;
2006         uint32 num_pids;
2007         struct server_id *pids;
2008 };
2009
2010 /*
2011  * Collect PIDs of all processes with pending entries
2012  */
2013
2014 static void brl_revalidate_collect(struct file_id id, struct server_id pid,
2015                                    enum brl_type lock_type,
2016                                    enum brl_flavour lock_flav,
2017                                    br_off start, br_off size,
2018                                    void *private_data)
2019 {
2020         struct brl_revalidate_state *state =
2021                 (struct brl_revalidate_state *)private_data;
2022
2023         if (!IS_PENDING_LOCK(lock_type)) {
2024                 return;
2025         }
2026
2027         add_to_large_array(state, sizeof(pid), (void *)&pid,
2028                            &state->pids, &state->num_pids,
2029                            &state->array_size);
2030 }
2031
2032 /*
2033  * qsort callback to sort the processes
2034  */
2035
2036 static int compare_procids(const void *p1, const void *p2)
2037 {
2038         const struct server_id *i1 = (const struct server_id *)p1;
2039         const struct server_id *i2 = (const struct server_id *)p2;
2040
2041         if (i1->pid < i2->pid) return -1;
2042         if (i2->pid > i2->pid) return 1;
2043         return 0;
2044 }
2045
2046 /*
2047  * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
2048  * locks so that they retry. Mainly used in the cluster code after a node has
2049  * died.
2050  *
2051  * Done in two steps to avoid double-sends: First we collect all entries in an
2052  * array, then qsort that array and only send to non-dupes.
2053  */
2054
2055 void brl_revalidate(struct messaging_context *msg_ctx,
2056                     void *private_data,
2057                     uint32_t msg_type,
2058                     struct server_id server_id,
2059                     DATA_BLOB *data)
2060 {
2061         struct brl_revalidate_state *state;
2062         uint32 i;
2063         struct server_id last_pid;
2064
2065         if (!(state = talloc_zero(NULL, struct brl_revalidate_state))) {
2066                 DEBUG(0, ("talloc failed\n"));
2067                 return;
2068         }
2069
2070         brl_forall(brl_revalidate_collect, state);
2071
2072         if (state->array_size == -1) {
2073                 DEBUG(0, ("talloc failed\n"));
2074                 goto done;
2075         }
2076
2077         if (state->num_pids == 0) {
2078                 goto done;
2079         }
2080
2081         TYPESAFE_QSORT(state->pids, state->num_pids, compare_procids);
2082
2083         ZERO_STRUCT(last_pid);
2084
2085         for (i=0; i<state->num_pids; i++) {
2086                 if (serverid_equal(&last_pid, &state->pids[i])) {
2087                         /*
2088                          * We've seen that one already
2089                          */
2090                         continue;
2091                 }
2092
2093                 messaging_send(msg_ctx, state->pids[i], MSG_SMB_UNLOCK,
2094                                &data_blob_null);
2095                 last_pid = state->pids[i];
2096         }
2097
2098  done:
2099         TALLOC_FREE(state);
2100         return;
2101 }