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