r23885: Fix make test on opi.
[nivanova/samba-autobuild/.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(int 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
269                             |TDB_VOLATILE
270                             |(read_only?0x0:TDB_CLEAR_IF_FIRST),
271                             read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644 );
272         if (!brlock_db) {
273                 DEBUG(0,("Failed to open byte range locking database %s\n",
274                         lock_path("brlock.tdb")));
275                 return;
276         }
277 }
278
279 /****************************************************************************
280  Close down the brlock.tdb database.
281 ****************************************************************************/
282
283 void brl_shutdown(int read_only)
284 {
285         if (!brlock_db) {
286                 return;
287         }
288         TALLOC_FREE(brlock_db);
289 }
290
291 #if ZERO_ZERO
292 /****************************************************************************
293  Compare two locks for sorting.
294 ****************************************************************************/
295
296 static int lock_compare(const struct lock_struct *lck1, 
297                          const struct lock_struct *lck2)
298 {
299         if (lck1->start != lck2->start) {
300                 return (lck1->start - lck2->start);
301         }
302         if (lck2->size != lck1->size) {
303                 return ((int)lck1->size - (int)lck2->size);
304         }
305         return 0;
306 }
307 #endif
308
309 /****************************************************************************
310  Lock a range of bytes - Windows lock semantics.
311 ****************************************************************************/
312
313 static NTSTATUS brl_lock_windows(struct byte_range_lock *br_lck,
314                         struct lock_struct *plock, BOOL blocking_lock)
315 {
316         unsigned int i;
317         files_struct *fsp = br_lck->fsp;
318         struct lock_struct *locks = br_lck->lock_data;
319
320         for (i=0; i < br_lck->num_locks; i++) {
321                 /* Do any Windows or POSIX locks conflict ? */
322                 if (brl_conflict(&locks[i], plock)) {
323                         /* Remember who blocked us. */
324                         plock->context.smbpid = locks[i].context.smbpid;
325                         return brl_lock_failed(fsp,plock,blocking_lock);
326                 }
327 #if ZERO_ZERO
328                 if (plock->start == 0 && plock->size == 0 && 
329                                 locks[i].size == 0) {
330                         break;
331                 }
332 #endif
333         }
334
335         /* We can get the Windows lock, now see if it needs to
336            be mapped into a lower level POSIX one, and if so can
337            we get it ? */
338
339         if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(fsp->conn->params)) {
340                 int errno_ret;
341                 if (!set_posix_lock_windows_flavour(fsp,
342                                 plock->start,
343                                 plock->size,
344                                 plock->lock_type,
345                                 &plock->context,
346                                 locks,
347                                 br_lck->num_locks,
348                                 &errno_ret)) {
349
350                         /* We don't know who blocked us. */
351                         plock->context.smbpid = 0xFFFFFFFF;
352
353                         if (errno_ret == EACCES || errno_ret == EAGAIN) {
354                                 return NT_STATUS_FILE_LOCK_CONFLICT;
355                         } else {
356                                 return map_nt_error_from_unix(errno);
357                         }
358                 }
359         }
360
361         /* no conflicts - add it to the list of locks */
362         locks = (struct lock_struct *)SMB_REALLOC(locks, (br_lck->num_locks + 1) * sizeof(*locks));
363         if (!locks) {
364                 return NT_STATUS_NO_MEMORY;
365         }
366
367         memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
368         br_lck->num_locks += 1;
369         br_lck->lock_data = locks;
370         br_lck->modified = True;
371
372         return NT_STATUS_OK;
373 }
374
375 /****************************************************************************
376  Cope with POSIX range splits and merges.
377 ****************************************************************************/
378
379 static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr,               /* Output array. */
380                                                 const struct lock_struct *ex,           /* existing lock. */
381                                                 const struct lock_struct *plock,        /* proposed lock. */
382                                                 BOOL *lock_was_added)
383 {
384         BOOL lock_types_differ = (ex->lock_type != plock->lock_type);
385
386         /* We can't merge non-conflicting locks on different context - ignore fnum. */
387
388         if (!brl_same_context(&ex->context, &plock->context)) {
389                 /* Just copy. */
390                 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
391                 return 1;
392         }
393
394         /* We now know we have the same context. */
395
396         /* Did we overlap ? */
397
398 /*********************************************
399                                              +---------+
400                                              | ex      |
401                                              +---------+
402                               +-------+
403                               | plock |
404                               +-------+
405 OR....
406              +---------+
407              |  ex     |
408              +---------+
409 **********************************************/
410
411         if ( (ex->start > (plock->start + plock->size)) ||
412                         (plock->start > (ex->start + ex->size))) {
413                 /* No overlap with this lock - copy existing. */
414                 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
415                 return 1;
416         }
417
418 /*********************************************
419         +---------------------------+
420         |          ex               |
421         +---------------------------+
422         +---------------------------+
423         |       plock               | -> replace with plock.
424         +---------------------------+
425 **********************************************/
426
427         if ( (ex->start >= plock->start) &&
428                         (ex->start + ex->size <= plock->start + plock->size) ) {
429                 memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
430                 *lock_was_added = True;
431                 return 1;
432         }
433
434 /*********************************************
435         +-----------------------+
436         |          ex           |
437         +-----------------------+
438         +---------------+
439         |   plock       |
440         +---------------+
441 OR....
442                         +-------+
443                         |  ex   |
444                         +-------+
445         +---------------+
446         |   plock       |
447         +---------------+
448
449 BECOMES....
450         +---------------+-------+
451         |   plock       | ex    | - different lock types.
452         +---------------+-------+
453 OR.... (merge)
454         +-----------------------+
455         |   ex                  | - same lock type.
456         +-----------------------+
457 **********************************************/
458
459         if ( (ex->start >= plock->start) &&
460                                 (ex->start <= plock->start + plock->size) &&
461                                 (ex->start + ex->size > plock->start + plock->size) ) {
462
463                 *lock_was_added = True;
464
465                 /* If the lock types are the same, we merge, if different, we
466                    add the new lock before the old. */
467
468                 if (lock_types_differ) {
469                         /* Add new. */
470                         memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
471                         memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
472                         /* Adjust existing start and size. */
473                         lck_arr[1].start = plock->start + plock->size;
474                         lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
475                         return 2;
476                 } else {
477                         /* Merge. */
478                         memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
479                         /* Set new start and size. */
480                         lck_arr[0].start = plock->start;
481                         lck_arr[0].size = (ex->start + ex->size) - plock->start;
482                         return 1;
483                 }
484         }
485
486 /*********************************************
487    +-----------------------+
488    |  ex                   |
489    +-----------------------+
490            +---------------+
491            |   plock       |
492            +---------------+
493 OR....
494    +-------+        
495    |  ex   |
496    +-------+
497            +---------------+
498            |   plock       |
499            +---------------+
500 BECOMES....
501    +-------+---------------+
502    | ex    |   plock       | - different lock types
503    +-------+---------------+
504
505 OR.... (merge)
506    +-----------------------+
507    | ex                    | - same lock type.
508    +-----------------------+
509
510 **********************************************/
511
512         if ( (ex->start < plock->start) &&
513                         (ex->start + ex->size >= plock->start) &&
514                         (ex->start + ex->size <= plock->start + plock->size) ) {
515
516                 *lock_was_added = True;
517
518                 /* If the lock types are the same, we merge, if different, we
519                    add the new lock after the old. */
520
521                 if (lock_types_differ) {
522                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
523                         memcpy(&lck_arr[1], plock, sizeof(struct lock_struct));
524                         /* Adjust existing size. */
525                         lck_arr[0].size = plock->start - ex->start;
526                         return 2;
527                 } else {
528                         /* Merge. */
529                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
530                         /* Adjust existing size. */
531                         lck_arr[0].size = (plock->start + plock->size) - ex->start;
532                         return 1;
533                 }
534         }
535
536 /*********************************************
537         +---------------------------+
538         |        ex                 |
539         +---------------------------+
540                 +---------+
541                 |  plock  |
542                 +---------+
543 BECOMES.....
544         +-------+---------+---------+
545         | ex    |  plock  | ex      | - different lock types.
546         +-------+---------+---------+
547 OR
548         +---------------------------+
549         |        ex                 | - same lock type.
550         +---------------------------+
551 **********************************************/
552
553         if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
554                 *lock_was_added = True;
555
556                 if (lock_types_differ) {
557
558                         /* We have to split ex into two locks here. */
559
560                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
561                         memcpy(&lck_arr[1], plock, sizeof(struct lock_struct));
562                         memcpy(&lck_arr[2], ex, sizeof(struct lock_struct));
563
564                         /* Adjust first existing size. */
565                         lck_arr[0].size = plock->start - ex->start;
566
567                         /* Adjust second existing start and size. */
568                         lck_arr[2].start = plock->start + plock->size;
569                         lck_arr[2].size = (ex->start + ex->size) - (plock->start + plock->size);
570                         return 3;
571                 } else {
572                         /* Just eat plock. */
573                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
574                         return 1;
575                 }
576         }
577
578         /* Never get here. */
579         smb_panic("brlock_posix_split_merge");
580         /* Notreached. */
581
582         /* Keep some compilers happy. */
583         return 0;
584 }
585
586 /****************************************************************************
587  Lock a range of bytes - POSIX lock semantics.
588  We must cope with range splits and merges.
589 ****************************************************************************/
590
591 static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
592                                struct byte_range_lock *br_lck,
593                                struct lock_struct *plock)
594 {
595         unsigned int i, count;
596         struct lock_struct *locks = br_lck->lock_data;
597         struct lock_struct *tp;
598         BOOL lock_was_added = False;
599         BOOL signal_pending_read = False;
600
601         /* No zero-zero locks for POSIX. */
602         if (plock->start == 0 && plock->size == 0) {
603                 return NT_STATUS_INVALID_PARAMETER;
604         }
605
606         /* Don't allow 64-bit lock wrap. */
607         if (plock->start + plock->size < plock->start ||
608                         plock->start + plock->size < plock->size) {
609                 return NT_STATUS_INVALID_PARAMETER;
610         }
611
612         /* The worst case scenario here is we have to split an
613            existing POSIX lock range into two, and add our lock,
614            so we need at most 2 more entries. */
615
616         tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 2));
617         if (!tp) {
618                 return NT_STATUS_NO_MEMORY;
619         }
620         
621         count = 0;
622         for (i=0; i < br_lck->num_locks; i++) {
623                 struct lock_struct *curr_lock = &locks[i];
624
625                 /* If we have a pending read lock, a lock downgrade should
626                    trigger a lock re-evaluation. */
627                 if (curr_lock->lock_type == PENDING_READ_LOCK &&
628                                 brl_pending_overlap(plock, curr_lock)) {
629                         signal_pending_read = True;
630                 }
631
632                 if (curr_lock->lock_flav == WINDOWS_LOCK) {
633                         /* Do any Windows flavour locks conflict ? */
634                         if (brl_conflict(curr_lock, plock)) {
635                                 /* No games with error messages. */
636                                 SAFE_FREE(tp);
637                                 /* Remember who blocked us. */
638                                 plock->context.smbpid = curr_lock->context.smbpid;
639                                 return NT_STATUS_FILE_LOCK_CONFLICT;
640                         }
641                         /* Just copy the Windows lock into the new array. */
642                         memcpy(&tp[count], curr_lock, sizeof(struct lock_struct));
643                         count++;
644                 } else {
645                         /* POSIX conflict semantics are different. */
646                         if (brl_conflict_posix(curr_lock, plock)) {
647                                 /* Can't block ourselves with POSIX locks. */
648                                 /* No games with error messages. */
649                                 SAFE_FREE(tp);
650                                 /* Remember who blocked us. */
651                                 plock->context.smbpid = curr_lock->context.smbpid;
652                                 return NT_STATUS_FILE_LOCK_CONFLICT;
653                         }
654
655                         /* Work out overlaps. */
656                         count += brlock_posix_split_merge(&tp[count], curr_lock, plock, &lock_was_added);
657                 }
658         }
659
660         if (!lock_was_added) {
661                 memcpy(&tp[count], plock, sizeof(struct lock_struct));
662                 count++;
663         }
664
665         /* We can get the POSIX lock, now see if it needs to
666            be mapped into a lower level POSIX one, and if so can
667            we get it ? */
668
669         if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(br_lck->fsp->conn->params)) {
670                 int errno_ret;
671
672                 /* The lower layer just needs to attempt to
673                    get the system POSIX lock. We've weeded out
674                    any conflicts above. */
675
676                 if (!set_posix_lock_posix_flavour(br_lck->fsp,
677                                 plock->start,
678                                 plock->size,
679                                 plock->lock_type,
680                                 &errno_ret)) {
681
682                         /* We don't know who blocked us. */
683                         plock->context.smbpid = 0xFFFFFFFF;
684
685                         if (errno_ret == EACCES || errno_ret == EAGAIN) {
686                                 SAFE_FREE(tp);
687                                 return NT_STATUS_FILE_LOCK_CONFLICT;
688                         } else {
689                                 SAFE_FREE(tp);
690                                 return map_nt_error_from_unix(errno);
691                         }
692                 }
693         }
694
695         /* Realloc so we don't leak entries per lock call. */
696         tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
697         if (!tp) {
698                 return NT_STATUS_NO_MEMORY;
699         }
700         br_lck->num_locks = count;
701         SAFE_FREE(br_lck->lock_data);
702         br_lck->lock_data = tp;
703         locks = tp;
704         br_lck->modified = True;
705
706         /* A successful downgrade from write to read lock can trigger a lock
707            re-evalutation where waiting readers can now proceed. */
708
709         if (signal_pending_read) {
710                 /* Send unlock messages to any pending read waiters that overlap. */
711                 for (i=0; i < br_lck->num_locks; i++) {
712                         struct lock_struct *pend_lock = &locks[i];
713
714                         /* Ignore non-pending locks. */
715                         if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
716                                 continue;
717                         }
718
719                         if (pend_lock->lock_type == PENDING_READ_LOCK &&
720                                         brl_pending_overlap(plock, pend_lock)) {
721                                 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
722                                         procid_str_static(&pend_lock->context.pid )));
723
724                                 messaging_send(msg_ctx, pend_lock->context.pid,
725                                                MSG_SMB_UNLOCK, &data_blob_null);
726                         }
727                 }
728         }
729
730         return NT_STATUS_OK;
731 }
732
733 /****************************************************************************
734  Lock a range of bytes.
735 ****************************************************************************/
736
737 NTSTATUS brl_lock(struct messaging_context *msg_ctx,
738                 struct byte_range_lock *br_lck,
739                 uint32 smbpid,
740                 struct server_id pid,
741                 br_off start,
742                 br_off size, 
743                 enum brl_type lock_type,
744                 enum brl_flavour lock_flav,
745                 BOOL blocking_lock,
746                 uint32 *psmbpid)
747 {
748         NTSTATUS ret;
749         struct lock_struct lock;
750
751 #if !ZERO_ZERO
752         if (start == 0 && size == 0) {
753                 DEBUG(0,("client sent 0/0 lock - please report this\n"));
754         }
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         for ( i=0; i<num_locks; i++) {
1503                 cb->fn(*key,
1504                        locks[i].context.pid,
1505                        locks[i].lock_type,
1506                        locks[i].lock_flav,
1507                        locks[i].start,
1508                        locks[i].size,
1509                        cb->private_data);
1510         }
1511
1512         SAFE_FREE(locks);
1513         return 0;
1514 }
1515
1516 /*******************************************************************
1517  Call the specified function on each lock in the database.
1518 ********************************************************************/
1519
1520 int brl_forall(void (*fn)(struct file_id id, struct server_id pid,
1521                           enum brl_type lock_type,
1522                           enum brl_flavour lock_flav,
1523                           br_off start, br_off size,
1524                           void *private_data),
1525                void *private_data)
1526 {
1527         struct brl_forall_cb cb;
1528
1529         if (!brlock_db) {
1530                 return 0;
1531         }
1532         cb.fn = fn;
1533         cb.private_data = private_data;
1534         return brlock_db->traverse(brlock_db, traverse_fn, &cb);
1535 }
1536
1537 /*******************************************************************
1538  Store a potentially modified set of byte range lock data back into
1539  the database.
1540  Unlock the record.
1541 ********************************************************************/
1542
1543 static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
1544 {
1545         TDB_DATA key;
1546
1547         key.dptr = (uint8 *)&br_lck->key;
1548         key.dsize = sizeof(struct file_id);
1549
1550         if (br_lck->read_only) {
1551                 SMB_ASSERT(!br_lck->modified);
1552         }
1553
1554         if (!br_lck->modified) {
1555                 goto done;
1556         }
1557
1558         if (br_lck->num_locks == 0) {
1559                 /* No locks - delete this entry. */
1560                 NTSTATUS status = br_lck->record->delete_rec(br_lck->record);
1561                 if (!NT_STATUS_IS_OK(status)) {
1562                         DEBUG(0, ("delete_rec returned %s\n",
1563                                   nt_errstr(status)));
1564                         smb_panic("Could not delete byte range lock entry");
1565                 }
1566         } else {
1567                 TDB_DATA data;
1568                 NTSTATUS status;
1569
1570                 data.dptr = (uint8 *)br_lck->lock_data;
1571                 data.dsize = br_lck->num_locks * sizeof(struct lock_struct);
1572
1573                 status = br_lck->record->store(br_lck->record, data,
1574                                                TDB_REPLACE);
1575                 if (!NT_STATUS_IS_OK(status)) {
1576                         DEBUG(0, ("store returned %s\n", nt_errstr(status)));
1577                         smb_panic("Could not store byte range mode entry");
1578                 }
1579         }
1580
1581  done:
1582
1583         SAFE_FREE(br_lck->lock_data);
1584         TALLOC_FREE(br_lck->record);
1585         return 0;
1586 }
1587
1588 /*******************************************************************
1589  Fetch a set of byte range lock data from the database.
1590  Leave the record locked.
1591  TALLOC_FREE(brl) will release the lock in the destructor.
1592 ********************************************************************/
1593
1594 static struct byte_range_lock *brl_get_locks_internal(TALLOC_CTX *mem_ctx,
1595                                         files_struct *fsp, BOOL read_only)
1596 {
1597         TDB_DATA key, data;
1598         struct byte_range_lock *br_lck = TALLOC_P(mem_ctx, struct byte_range_lock);
1599
1600         if (br_lck == NULL) {
1601                 return NULL;
1602         }
1603
1604         br_lck->fsp = fsp;
1605         br_lck->num_locks = 0;
1606         br_lck->modified = False;
1607         memset(&br_lck->key, '\0', sizeof(struct file_id));
1608         br_lck->key = fsp->file_id;
1609
1610         key.dptr = (uint8 *)&br_lck->key;
1611         key.dsize = sizeof(struct file_id);
1612
1613         if (!fsp->lockdb_clean) {
1614                 /* We must be read/write to clean
1615                    the dead entries. */
1616                 read_only = False;
1617         }
1618
1619         if (read_only) {
1620                 if (brlock_db->fetch(brlock_db, br_lck, key, &data) == -1) {
1621                         DEBUG(3, ("Could not fetch byte range lock record\n"));
1622                         TALLOC_FREE(br_lck);
1623                         return NULL;
1624                 }
1625                 br_lck->record = NULL;
1626         }
1627         else {
1628                 br_lck->record = brlock_db->fetch_locked(brlock_db, br_lck, key);
1629
1630                 if (br_lck->record == NULL) {
1631                         DEBUG(3, ("Could not lock byte range lock entry\n"));
1632                         TALLOC_FREE(br_lck);
1633                         return NULL;
1634                 }
1635
1636                 data = br_lck->record->value;
1637         }
1638
1639         br_lck->read_only = read_only;
1640         br_lck->lock_data = NULL;
1641
1642         talloc_set_destructor(br_lck, byte_range_lock_destructor);
1643
1644         br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
1645
1646         if (br_lck->num_locks != 0) {
1647                 br_lck->lock_data = SMB_MALLOC_ARRAY(struct lock_struct,
1648                                                      br_lck->num_locks);
1649                 if (br_lck->lock_data == NULL) {
1650                         DEBUG(0, ("malloc failed\n"));
1651                         TALLOC_FREE(br_lck);
1652                         return NULL;
1653                 }
1654
1655                 memcpy(br_lck->lock_data, data.dptr, data.dsize);
1656         }
1657         
1658         if (!fsp->lockdb_clean) {
1659                 int orig_num_locks = br_lck->num_locks;
1660
1661                 /* This is the first time we've accessed this. */
1662                 /* Go through and ensure all entries exist - remove any that don't. */
1663                 /* Makes the lockdb self cleaning at low cost. */
1664
1665                 if (!validate_lock_entries(&br_lck->num_locks,
1666                                            &br_lck->lock_data)) {
1667                         SAFE_FREE(br_lck->lock_data);
1668                         TALLOC_FREE(br_lck);
1669                         return NULL;
1670                 }
1671
1672                 /* Ensure invalid locks are cleaned up in the destructor. */
1673                 if (orig_num_locks != br_lck->num_locks) {
1674                         br_lck->modified = True;
1675                 }
1676
1677                 /* Mark the lockdb as "clean" as seen from this open file. */
1678                 fsp->lockdb_clean = True;
1679         }
1680
1681         if (DEBUGLEVEL >= 10) {
1682                 unsigned int i;
1683                 struct lock_struct *locks = br_lck->lock_data;
1684                 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
1685                         br_lck->num_locks,
1686                           file_id_static_string(&fsp->file_id)));
1687                 for( i = 0; i < br_lck->num_locks; i++) {
1688                         print_lock_struct(i, &locks[i]);
1689                 }
1690         }
1691         return br_lck;
1692 }
1693
1694 struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx,
1695                                         files_struct *fsp)
1696 {
1697         return brl_get_locks_internal(mem_ctx, fsp, False);
1698 }
1699
1700 struct byte_range_lock *brl_get_locks_readonly(TALLOC_CTX *mem_ctx,
1701                                         files_struct *fsp)
1702 {
1703         return brl_get_locks_internal(mem_ctx, fsp, True);
1704 }
1705
1706 struct brl_revalidate_state {
1707         ssize_t array_size;
1708         uint32 num_pids;
1709         struct server_id *pids;
1710 };
1711
1712 /*
1713  * Collect PIDs of all processes with pending entries
1714  */
1715
1716 static void brl_revalidate_collect(struct file_id id, struct server_id pid,
1717                                    enum brl_type lock_type,
1718                                    enum brl_flavour lock_flav,
1719                                    br_off start, br_off size,
1720                                    void *private_data)
1721 {
1722         struct brl_revalidate_state *state =
1723                 (struct brl_revalidate_state *)private_data;
1724
1725         if (!IS_PENDING_LOCK(lock_type)) {
1726                 return;
1727         }
1728
1729         add_to_large_array(state, sizeof(pid), (void *)&pid,
1730                            &state->pids, &state->num_pids,
1731                            &state->array_size);
1732 }
1733
1734 /*
1735  * qsort callback to sort the processes
1736  */
1737
1738 static int compare_procids(const void *p1, const void *p2)
1739 {
1740         const struct server_id *i1 = (struct server_id *)p1;
1741         const struct server_id *i2 = (struct server_id *)p2;
1742
1743         if (i1->pid < i2->pid) return -1;
1744         if (i2->pid > i2->pid) return 1;
1745         return 0;
1746 }
1747
1748 /*
1749  * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
1750  * locks so that they retry. Mainly used in the cluster code after a node has
1751  * died.
1752  *
1753  * Done in two steps to avoid double-sends: First we collect all entries in an
1754  * array, then qsort that array and only send to non-dupes.
1755  */
1756
1757 static void brl_revalidate(struct messaging_context *msg_ctx,
1758                            void *private_data,
1759                            uint32_t msg_type,
1760                            struct server_id server_id,
1761                            DATA_BLOB *data)
1762 {
1763         struct brl_revalidate_state *state;
1764         uint32 i;
1765         struct server_id last_pid;
1766
1767         if (!(state = TALLOC_ZERO_P(NULL, struct brl_revalidate_state))) {
1768                 DEBUG(0, ("talloc failed\n"));
1769                 return;
1770         }
1771
1772         brl_forall(brl_revalidate_collect, state);
1773
1774         if (state->array_size == -1) {
1775                 DEBUG(0, ("talloc failed\n"));
1776                 goto done;
1777         }
1778
1779         if (state->num_pids == 0) {
1780                 goto done;
1781         }
1782
1783         qsort(state->pids, state->num_pids, sizeof(state->pids[0]),
1784               compare_procids);
1785
1786         ZERO_STRUCT(last_pid);
1787
1788         for (i=0; i<state->num_pids; i++) {
1789                 if (procid_equal(&last_pid, &state->pids[i])) {
1790                         /*
1791                          * We've seen that one already
1792                          */
1793                         continue;
1794                 }
1795
1796                 messaging_send(msg_ctx, state->pids[i], MSG_SMB_UNLOCK,
1797                                &data_blob_null);
1798                 last_pid = state->pids[i];
1799         }
1800
1801  done:
1802         TALLOC_FREE(state);
1803         return;
1804 }
1805
1806 void brl_register_msgs(struct messaging_context *msg_ctx)
1807 {
1808         messaging_register(msg_ctx, NULL, MSG_SMB_BRL_VALIDATE,
1809                            brl_revalidate);
1810 }