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