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