r15269: Fix incorrect boolean in assert to make POSIX lock tests
[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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 /* This module implements a tdb based byte range locking service,
25    replacing the fcntl() based byte range locking previously
26    used. This allows us to provide the same semantics as NT */
27
28 #include "includes.h"
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_LOCKING
32
33 #define ZERO_ZERO 0
34
35 /* This contains elements that differentiate locks. The smbpid is a
36    client supplied pid, and is essentially the locking context for
37    this client */
38
39 struct lock_context {
40         uint16 smbpid;
41         uint16 tid;
42         struct process_id pid;
43 };
44
45 /* The data in brlock records is an unsorted linear array of these
46    records.  It is unnecessary to store the count as tdb provides the
47    size of the record */
48
49 struct lock_struct {
50         struct lock_context context;
51         br_off start;
52         br_off size;
53         int fnum;
54         enum brl_type lock_type;
55         enum brl_flavour lock_flav;
56 };
57
58 /* The open brlock.tdb database. */
59
60 static TDB_CONTEXT *tdb;
61
62 /****************************************************************************
63  Debug info at level 10 for lock struct.
64 ****************************************************************************/
65
66 static void print_lock_struct(unsigned int i, struct lock_struct *pls)
67 {
68         DEBUG(10,("[%u]: smbpid = %u, tid = %u, pid = %u, ",
69                         i,
70                         (unsigned int)pls->context.smbpid,
71                         (unsigned int)pls->context.tid,
72                         (unsigned int)procid_to_pid(&pls->context.pid) ));
73         
74         DEBUG(10,("start = %.0f, size = %.0f, fnum = %d, %s %s\n",
75                 (double)pls->start,
76                 (double)pls->size,
77                 pls->fnum,
78                 lock_type_name(pls->lock_type),
79                 lock_flav_name(pls->lock_flav) ));
80 }
81
82 /****************************************************************************
83  See if two locking contexts are equal.
84 ****************************************************************************/
85
86 static BOOL brl_same_context(const struct lock_context *ctx1, 
87                              const struct lock_context *ctx2)
88 {
89         return (procid_equal(&ctx1->pid, &ctx2->pid) &&
90                 (ctx1->smbpid == ctx2->smbpid) &&
91                 (ctx1->tid == ctx2->tid));
92 }
93
94 /****************************************************************************
95  See if lck1 and lck2 overlap.
96 ****************************************************************************/
97
98 static BOOL brl_overlap(const struct lock_struct *lck1,
99                         const struct lock_struct *lck2)
100 {
101         /* this extra check is not redundent - it copes with locks
102            that go beyond the end of 64 bit file space */
103         if (lck1->size != 0 &&
104             lck1->start == lck2->start &&
105             lck1->size == lck2->size) {
106                 return True;
107         }
108
109         if (lck1->start >= (lck2->start+lck2->size) ||
110             lck2->start >= (lck1->start+lck1->size)) {
111                 return False;
112         }
113         return True;
114 }
115
116 /****************************************************************************
117  See if lock2 can be added when lock1 is in place.
118 ****************************************************************************/
119
120 static BOOL brl_conflict(const struct lock_struct *lck1, 
121                          const struct lock_struct *lck2)
122 {
123         /* Ignore PENDING locks. */
124         if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
125                 return False;
126
127         /* Read locks never conflict. */
128         if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
129                 return False;
130         }
131
132         if (brl_same_context(&lck1->context, &lck2->context) &&
133             lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
134                 return False;
135         }
136
137         return brl_overlap(lck1, lck2);
138
139
140 /****************************************************************************
141  See if lock2 can be added when lock1 is in place - when both locks are POSIX
142  flavour. POSIX locks ignore fnum - they only care about dev/ino which we
143  know already match.
144 ****************************************************************************/
145
146 static BOOL brl_conflict_posix(const struct lock_struct *lck1, 
147                                 const struct lock_struct *lck2)
148 {
149 #if defined(DEVELOPER)
150         SMB_ASSERT(lck1->lock_flav == POSIX_LOCK);
151         SMB_ASSERT(lck2->lock_flav == POSIX_LOCK);
152 #endif
153
154         /* Ignore PENDING locks. */
155         if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
156                 return False;
157
158         /* Read locks never conflict. */
159         if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
160                 return False;
161         }
162
163         /* Locks on the same context con't conflict. Ignore fnum. */
164         if (brl_same_context(&lck1->context, &lck2->context)) {
165                 return False;
166         }
167
168         /* One is read, the other write, or the context is different,
169            do they overlap ? */
170         return brl_overlap(lck1, lck2);
171
172
173 #if ZERO_ZERO
174 static BOOL brl_conflict1(const struct lock_struct *lck1, 
175                          const struct lock_struct *lck2)
176 {
177         if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
178                 return False;
179
180         if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
181                 return False;
182         }
183
184         if (brl_same_context(&lck1->context, &lck2->context) &&
185             lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
186                 return False;
187         }
188
189         if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
190                 return True;
191         }
192
193         if (lck1->start >= (lck2->start + lck2->size) ||
194             lck2->start >= (lck1->start + lck1->size)) {
195                 return False;
196         }
197             
198         return True;
199
200 #endif
201
202 /****************************************************************************
203  Check to see if this lock conflicts, but ignore our own locks on the
204  same fnum only. This is the read/write lock check code path.
205  This is never used in the POSIX lock case.
206 ****************************************************************************/
207
208 static BOOL brl_conflict_other(const struct lock_struct *lck1, const struct lock_struct *lck2)
209 {
210         if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
211                 return False;
212
213         if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) 
214                 return False;
215
216         /* POSIX flavour locks never conflict here - this is only called
217            in the read/write path. */
218
219         if (lck1->lock_flav == POSIX_LOCK && lck2->lock_flav == POSIX_LOCK)
220                 return False;
221
222         /*
223          * Incoming WRITE locks conflict with existing READ locks even
224          * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
225          */
226
227         if (!(lck2->lock_type == WRITE_LOCK && lck1->lock_type == READ_LOCK)) {
228                 if (brl_same_context(&lck1->context, &lck2->context) &&
229                                         lck1->fnum == lck2->fnum)
230                         return False;
231         }
232
233         return brl_overlap(lck1, lck2);
234
235
236 /****************************************************************************
237  Amazingly enough, w2k3 "remembers" whether the last lock failure
238  is the same as this one and changes its error code. I wonder if any
239  app depends on this ?
240 ****************************************************************************/
241
242 static NTSTATUS brl_lock_failed(const struct lock_struct *lock)
243 {
244         static struct lock_struct last_lock_failure;
245
246         if (brl_same_context(&lock->context, &last_lock_failure.context) &&
247                         lock->fnum == last_lock_failure.fnum &&
248                         lock->start == last_lock_failure.start &&
249                         lock->size == last_lock_failure.size) {
250                 return NT_STATUS_FILE_LOCK_CONFLICT;
251         }
252         last_lock_failure = *lock;
253         if (lock->start >= 0xEF000000 &&
254                         (lock->start >> 63) == 0) {
255                 /* amazing the little things you learn with a test
256                    suite. Locks beyond this offset (as a 64 bit
257                    number!) always generate the conflict error code,
258                    unless the top bit is set */
259                 return NT_STATUS_FILE_LOCK_CONFLICT;
260         }
261         return NT_STATUS_LOCK_NOT_GRANTED;
262 }
263
264 /****************************************************************************
265  Open up the brlock.tdb database.
266 ****************************************************************************/
267
268 void brl_init(int read_only)
269 {
270         if (tdb) {
271                 return;
272         }
273         tdb = tdb_open_log(lock_path("brlock.tdb"),
274                         lp_open_files_db_hash_size(),
275                         TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST),
276                         read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644 );
277         if (!tdb) {
278                 DEBUG(0,("Failed to open byte range locking database %s\n",
279                         lock_path("brlock.tdb")));
280                 return;
281         }
282 }
283
284 /****************************************************************************
285  Close down the brlock.tdb database.
286 ****************************************************************************/
287
288 void brl_shutdown(int read_only)
289 {
290         if (!tdb) {
291                 return;
292         }
293         tdb_close(tdb);
294 }
295
296 #if ZERO_ZERO
297 /****************************************************************************
298  Compare two locks for sorting.
299 ****************************************************************************/
300
301 static int lock_compare(const struct lock_struct *lck1, 
302                          const struct lock_struct *lck2)
303 {
304         if (lck1->start != lck2->start) {
305                 return (lck1->start - lck2->start);
306         }
307         if (lck2->size != lck1->size) {
308                 return ((int)lck1->size - (int)lck2->size);
309         }
310         return 0;
311 }
312 #endif
313
314 /****************************************************************************
315  Lock a range of bytes - Windows lock semantics.
316 ****************************************************************************/
317
318 static NTSTATUS brl_lock_windows(struct byte_range_lock *br_lck,
319                         const struct lock_struct *plock,
320                         BOOL *my_lock_ctx)
321 {
322         unsigned int i;
323         files_struct *fsp = br_lck->fsp;
324         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
325
326         for (i=0; i < br_lck->num_locks; i++) {
327                 /* Do any Windows or POSIX locks conflict ? */
328                 if (brl_conflict(&locks[i], plock)) {
329                         NTSTATUS status = brl_lock_failed(plock);;
330                         /* Did we block ourselves ? */
331                         if (brl_same_context(&locks[i].context, &plock->context)) {
332                                 *my_lock_ctx = True;
333                         }
334                         return status;
335                 }
336 #if ZERO_ZERO
337                 if (plock->start == 0 && plock->size == 0 && 
338                                 locks[i].size == 0) {
339                         break;
340                 }
341 #endif
342         }
343
344         /* We can get the Windows lock, now see if it needs to
345            be mapped into a lower level POSIX one, and if so can
346            we get it ? We tell the lower lock layer about the
347            lock type so it can cope with the difference between
348            Windows "stacking" locks and POSIX "flat" ones. */
349
350         if ((plock->lock_type != PENDING_LOCK) && lp_posix_locking(SNUM(fsp->conn))) {
351                 if (!set_posix_lock(fsp, plock->start, plock->size, plock->lock_type, WINDOWS_LOCK)) {
352                         if (errno == EACCES || errno == EAGAIN) {
353                                 return NT_STATUS_FILE_LOCK_CONFLICT;
354                         } else {
355                                 return map_nt_error_from_unix(errno);
356                         }
357                 }
358         }
359
360         /* no conflicts - add it to the list of locks */
361         locks = (struct lock_struct *)SMB_REALLOC(locks, (br_lck->num_locks + 1) * sizeof(*locks));
362         if (!locks) {
363                 return NT_STATUS_NO_MEMORY;
364         }
365
366         memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
367         br_lck->num_locks += 1;
368         br_lck->lock_data = (void *)locks;
369         br_lck->modified = True;
370
371         return NT_STATUS_OK;
372 }
373
374 /****************************************************************************
375  Cope with POSIX range splits and merges.
376 ****************************************************************************/
377
378 static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr,
379                                                 const struct lock_struct *ex,
380                                                 const struct lock_struct *plock,
381                                                 BOOL *lock_was_added)
382 {
383         BOOL lock_types_differ = (ex->lock_type != plock->lock_type);
384
385         /* We can't merge non-conflicting locks on different context - ignore fnum. */
386
387         if (!brl_same_context(&ex->context, &plock->context)) {
388                 /* Just copy. */
389                 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
390                 return 1;
391         }
392
393         /* We now know we have the same context. */
394
395         /* Did we overlap ? */
396
397 /*********************************************
398                                              +---------+
399                                              | ex      |
400                                              +---------+
401                                 +-------+
402                                 | plock |
403                                 +-------+
404 OR....
405              +---------+
406              |  ex     |
407              +---------+
408 **********************************************/
409
410         if ( (ex->start >= (plock->start + plock->size)) ||
411                         (plock->start >= (ex->start + ex->size))) {
412                 /* No overlap with this lock - copy existing. */
413                 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
414                 return 1;
415         }
416
417 /*********************************************
418                 +---------+
419                 |  ex     |
420                 +---------+
421         +---------------------------+
422         |       plock               | -> replace with plock.
423         +---------------------------+
424 **********************************************/
425
426         if ( (ex->start >= plock->start) &&
427                         (ex->start + ex->size <= plock->start + plock->size) ) {
428                 memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
429                 *lock_was_added = True;
430                 return 1;
431         }
432
433 /*********************************************
434                 +---------------+
435                 |  ex           |
436                 +---------------+
437         +---------------+
438         |   plock       |
439         +---------------+
440 BECOMES....
441         +---------------+-------+
442         |   plock       | ex    | - different lock types.
443         +---------------+-------+
444 OR....
445         +-----------------------+
446         |   ex                  | - same lock type.
447         +-----------------------+
448 **********************************************/
449
450         if ( (ex->start >= plock->start) &&
451                                 (ex->start < plock->start + plock->size) &&
452                                 (ex->start + ex->size > plock->start + plock->size) ) {
453
454                 *lock_was_added = True;
455
456                 /* If the lock types are the same, we merge, if different, we
457                    add the new lock before the old. */
458
459                 if (lock_types_differ) {
460                         /* Add new. */
461                         memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
462                         memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
463                         /* Adjust existing start and size. */
464                         lck_arr[1].start = plock->start + plock->size;
465                         lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
466                         return 2;
467                 } else {
468                         /* Merge. */
469                         memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
470                         /* Set new start and size. */
471                         lck_arr[0].start = plock->start;
472                         lck_arr[0].size = (ex->start + ex->size) - plock->start;
473                         return 1;
474                 }
475         }
476
477 /*********************************************
478    +---------------+
479    |  ex           |
480    +---------------+
481            +---------------+
482            |   plock       |
483            +---------------+
484 BECOMES....
485    +-------+---------------+
486    | ex    |   plock       | - different lock types
487    +-------+---------------+
488
489 OR
490    +-----------------------+
491    | ex                    | - same lock type.
492    +-----------------------+
493
494 **********************************************/
495
496         if ( (ex->start < plock->start) &&
497                         (ex->start + ex->size > plock->start) &&
498                         (ex->start + ex->size <= plock->start + plock->size) ) {
499
500                 *lock_was_added = True;
501
502                 /* If the lock types are the same, we merge, if different, we
503                    add the new lock after the old. */
504
505                 if (lock_types_differ) {
506                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
507                         memcpy(&lck_arr[1], plock, sizeof(struct lock_struct));
508                         /* Adjust existing size. */
509                         lck_arr[0].size = plock->start - ex->start;
510                         return 2;
511                 } else {
512                         /* Merge. */
513                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
514                         /* Adjust existing size. */
515                         lck_arr[0].size = (plock->start + plock->size) - ex->start;
516                         return 1;
517                 }
518         }
519
520 /*********************************************
521         +---------------------------+
522         |        ex                 |
523         +---------------------------+
524                 +---------+
525                 |  plock  |
526                 +---------+
527 BECOMES.....
528         +-------+---------+---------+
529         | ex    |  plock  | ex      | - different lock types.
530         +-------+---------+---------+
531 OR
532         +---------------------------+
533         |        ex                 | - same lock type.
534         +---------------------------+
535 **********************************************/
536
537         if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
538                 *lock_was_added = True;
539
540                 if (lock_types_differ) {
541
542                         /* We have to split ex into two locks here. */
543
544                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
545                         memcpy(&lck_arr[1], plock, sizeof(struct lock_struct));
546                         memcpy(&lck_arr[2], ex, sizeof(struct lock_struct));
547
548                         /* Adjust first existing size. */
549                         lck_arr[0].size = plock->start - ex->start;
550
551                         /* Adjust second existing start and size. */
552                         lck_arr[2].start = plock->start + plock->size;
553                         lck_arr[2].size = (ex->start + ex->size) - (plock->start + plock->size);
554                         return 3;
555                 } else {
556                         /* Just eat plock. */
557                         memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
558                         return 1;
559                 }
560         }
561
562         /* Never get here. */
563         smb_panic("brlock_posix_split_merge\n");
564         /* Notreached. */
565         abort();
566         /* Keep some compilers happy. */
567         return 0;
568 }
569
570 /****************************************************************************
571  Lock a range of bytes - POSIX lock semantics.
572  We must cope with range splits and merges.
573 ****************************************************************************/
574
575 static NTSTATUS brl_lock_posix(struct byte_range_lock *br_lck,
576                         const struct lock_struct *plock,
577                         BOOL *my_lock_ctx)
578 {
579         unsigned int i, count;
580         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
581         struct lock_struct *tp;
582         BOOL lock_was_added = False;
583
584         /* No zero-zero locks for POSIX. */
585         if (plock->start == 0 && plock->size == 0) {
586                 return NT_STATUS_INVALID_PARAMETER;
587         }
588
589         /* Don't allow 64-bit lock wrap. */
590         if (plock->start + plock->size < plock->start ||
591                         plock->start + plock->size < plock->size) {
592                 return NT_STATUS_INVALID_PARAMETER;
593         }
594
595         /* The worst case scenario here is we have to split an
596            existing POSIX lock range into two, and add our lock,
597            so we need at most 2 more entries. */
598
599         tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 2));
600         if (!tp) {
601                 return NT_STATUS_NO_MEMORY;
602         }
603         
604         count = 0;
605         for (i=0; i < br_lck->num_locks; i++) {
606                 if (locks[i].lock_flav == WINDOWS_LOCK) {
607                         /* Do any Windows flavour locks conflict ? */
608                         if (brl_conflict(&locks[i], plock)) {
609                                 /* Did we block ourselves ? */
610                                 if (brl_same_context(&locks[i].context, &plock->context)) {
611                                         *my_lock_ctx = True;
612                                 }
613                                 /* No games with error messages. */
614                                 SAFE_FREE(tp);
615                                 return NT_STATUS_FILE_LOCK_CONFLICT;
616                         }
617                         /* Just copy the Windows lock into the new array. */
618                         memcpy(&tp[count], &locks[i], sizeof(struct lock_struct));
619                         count++;
620                 } else {
621                         /* POSIX conflict semantics are different. */
622                         if (brl_conflict_posix(&locks[i], plock)) {
623                                 /* Can't block ourselves with POSIX locks. */
624                                 /* No games with error messages. */
625                                 SAFE_FREE(tp);
626                                 return NT_STATUS_FILE_LOCK_CONFLICT;
627                         }
628
629                         /* Work out overlaps. */
630                         count += brlock_posix_split_merge(&tp[count], &locks[i], plock, &lock_was_added);
631                 }
632         }
633
634         /* We can get the POSIX lock, now see if it needs to
635            be mapped into a lower level POSIX one, and if so can
636            we get it ? We well the lower lock layer about the
637            lock type so it can cope with the difference between
638            Windows "stacking" locks and POSIX "flat" ones. */
639
640 #if 0
641         /* FIXME - this call doesn't work correctly yet for POSIX locks... */
642
643         if ((plock->lock_type != PENDING_LOCK) && lp_posix_locking(SNUM(fsp->conn))) {
644                 files_struct *fsp = br_lck->fsp;
645
646                 if (!set_posix_lock(fsp, plock->start, plock->size, plock->lock_type, POSIX_LOCK)) {
647                         if (errno == EACCES || errno == EAGAIN) {
648                                 SAFE_FREE(tp);
649                                 return NT_STATUS_FILE_LOCK_CONFLICT;
650                         } else {
651                                 SAFE_FREE(tp);
652                                 return map_nt_error_from_unix(errno);
653                         }
654                 }
655         }
656 #endif
657
658         if (!lock_was_added) {
659                 memcpy(&tp[count], plock, sizeof(struct lock_struct));
660                 count++;
661         }
662
663         /* Realloc so we don't leak entries per lock call. */
664         tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
665         if (!tp) {
666                 return NT_STATUS_NO_MEMORY;
667         }
668         br_lck->num_locks = count;
669         br_lck->lock_data = (void *)tp;
670         br_lck->modified = True;
671         return NT_STATUS_OK;
672 }
673
674 /****************************************************************************
675  Lock a range of bytes.
676 ****************************************************************************/
677
678 NTSTATUS brl_lock(struct byte_range_lock *br_lck,
679                 uint16 smbpid,
680                 struct process_id pid,
681                 br_off start,
682                 br_off size, 
683                 enum brl_type lock_type,
684                 enum brl_flavour lock_flav,
685                 BOOL *my_lock_ctx)
686 {
687         NTSTATUS ret;
688         struct lock_struct lock;
689
690         *my_lock_ctx = False;
691
692 #if !ZERO_ZERO
693         if (start == 0 && size == 0) {
694                 DEBUG(0,("client sent 0/0 lock - please report this\n"));
695         }
696 #endif
697
698         lock.context.smbpid = smbpid;
699         lock.context.pid = pid;
700         lock.context.tid = br_lck->fsp->conn->cnum;
701         lock.start = start;
702         lock.size = size;
703         lock.fnum = br_lck->fsp->fnum;
704         lock.lock_type = lock_type;
705         lock.lock_flav = lock_flav;
706
707         if (lock_flav == WINDOWS_LOCK) {
708                 ret = brl_lock_windows(br_lck, &lock, my_lock_ctx);
709         } else {
710                 ret = brl_lock_posix(br_lck, &lock, my_lock_ctx);
711         }
712
713 #if ZERO_ZERO
714         /* sort the lock list */
715         qsort(br_lck->lock_data, (size_t)br_lck->num_locks, sizeof(lock), lock_compare);
716 #endif
717
718         return ret;
719 }
720
721 /****************************************************************************
722  Check if an unlock overlaps a pending lock.
723 ****************************************************************************/
724
725 static BOOL brl_pending_overlap(struct lock_struct *lock, struct lock_struct *pend_lock)
726 {
727         if ((lock->start <= pend_lock->start) && (lock->start + lock->size > pend_lock->start))
728                 return True;
729         if ((lock->start >= pend_lock->start) && (lock->start <= pend_lock->start + pend_lock->size))
730                 return True;
731         return False;
732 }
733
734 /****************************************************************************
735  Unlock a range of bytes - Windows semantics.
736 ****************************************************************************/
737
738 static BOOL brl_unlock_windows(struct byte_range_lock *br_lck, const struct lock_struct *plock)
739 {
740         unsigned int i, j;
741         struct lock_struct *lock = NULL;
742         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
743
744 #if ZERO_ZERO
745         for (i = 0; i < br_lck->num_locks; i++) {
746                 lock = &locks[i];
747
748                 if (lock->lock_type == WRITE_LOCK &&
749                     brl_same_context(&lock->context, &plock->context) &&
750                     lock->fnum == plock->fnum &&
751                     lock->lock_flav == WINDOWS_LOCK &&
752                     lock->start == plock->start &&
753                     lock->size == plock->size) {
754
755                         /* found it - delete it */
756                         if (i < br_lck->num_locks - 1) {
757                                 memmove(&locks[i], &locks[i+1], 
758                                         sizeof(*locks)*((br_lck->num_locks-1) - i));
759                         }
760
761                         br_lck->num_locks -= 1;
762                         br_lck->modified = True;
763                         return True;
764                 }
765         }
766 #endif
767
768         for (i = 0; i < br_lck->num_locks; i++) {
769                 lock = &locks[i];
770
771                 /* Only remove our own locks that match in start, size, and flavour. */
772                 if (brl_same_context(&lock->context, &plock->context) &&
773                                         lock->fnum == plock->fnum &&
774                                         lock->lock_flav == WINDOWS_LOCK &&
775                                         lock->start == plock->start &&
776                                         lock->size == plock->size ) {
777                         break;
778                 }
779         }
780
781         if (i == br_lck->num_locks) {
782                 /* we didn't find it */
783                 return False;
784         }
785
786         /* Unlock any POSIX regions. */
787         if(lp_posix_locking(br_lck->fsp->conn->cnum)) {
788                 release_posix_lock(br_lck->fsp, plock->start, plock->size);
789         }
790
791         /* Send unlock messages to any pending waiters that overlap. */
792         for (j=0; j < br_lck->num_locks; j++) {
793                 struct lock_struct *pend_lock = &locks[j];
794
795                 /* Ignore non-pending locks. */
796                 if (pend_lock->lock_type != PENDING_LOCK) {
797                         continue;
798                 }
799
800                 /* We could send specific lock info here... */
801                 if (brl_pending_overlap(lock, pend_lock)) {
802                         DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
803                                 procid_str_static(&pend_lock->context.pid )));
804
805                         become_root();
806                         message_send_pid(pend_lock->context.pid,
807                                         MSG_SMB_UNLOCK,
808                                         NULL, 0, True);
809                         unbecome_root();
810                 }
811         }
812
813         /* Actually delete the lock. */
814         if (i < br_lck->num_locks - 1) {
815                 memmove(&locks[i], &locks[i+1], 
816                         sizeof(*locks)*((br_lck->num_locks-1) - i));
817         }
818
819         br_lck->num_locks -= 1;
820         br_lck->modified = True;
821         return True;
822 }
823
824 /****************************************************************************
825  Unlock a range of bytes - POSIX semantics.
826 ****************************************************************************/
827
828 static BOOL brl_unlock_posix(struct byte_range_lock *br_lck, const struct lock_struct *plock)
829 {
830         unsigned int i, j, count;
831         struct lock_struct *lock = NULL;
832         struct lock_struct *tp;
833         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
834         BOOL overlap_found = False;
835
836         /* No zero-zero locks for POSIX. */
837         if (plock->start == 0 && plock->size == 0) {
838                 return False;
839         }
840
841         /* Don't allow 64-bit lock wrap. */
842         if (plock->start + plock->size < plock->start ||
843                         plock->start + plock->size < plock->size) {
844                 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
845                 return False;
846         }
847
848         /* The worst case scenario here is we have to split an
849            existing POSIX lock range into two, so we need at most
850            1 more entry. */
851
852         tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 1));
853         if (!tp) {
854                 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
855                 return False;
856         }
857
858         count = 0;
859         for (i = 0; i < br_lck->num_locks; i++) {
860                 struct lock_struct tmp_lock[3];
861                 BOOL lock_was_added = False;
862                 unsigned int tmp_count;
863
864                 lock = &locks[i];
865
866                 /* Only remove our own locks - ignore fnum. */
867                 if (lock->lock_type == PENDING_LOCK ||
868                                 !brl_same_context(&lock->context, &plock->context)) {
869                         memcpy(&tp[count], lock, sizeof(struct lock_struct));
870                         count++;
871                         continue;
872                 }
873
874                 /* Work out overlaps. */
875                 tmp_count = brlock_posix_split_merge(&tmp_lock[0], &locks[i], plock, &lock_was_added);
876
877                 if (tmp_count == 1) {
878                         /* Ether the locks didn't overlap, or the unlock completely
879                            overlapped this lock. If it didn't overlap, then there's
880                            no change in the locks. */
881                         if (tmp_lock[0].lock_type != UNLOCK_LOCK) {
882                                 SMB_ASSERT(tmp_lock[0].lock_type == locks[i].lock_type);
883                                 /* No change in this lock. */
884                                 memcpy(&tp[count], &tmp_lock[0], sizeof(struct lock_struct));
885                                 count++;
886                         } else {
887                                 SMB_ASSERT(tmp_lock[0].lock_type == UNLOCK_LOCK);
888                                 overlap_found = True;
889                         }
890                         continue;
891                 } else if (tmp_count == 2) {
892                         /* The unlock overlapped an existing lock. Copy the truncated
893                            lock into the lock array. */
894                         if (tmp_lock[0].lock_type != UNLOCK_LOCK) {
895                                 SMB_ASSERT(tmp_lock[0].lock_type == locks[i].lock_type);
896                                 SMB_ASSERT(tmp_lock[1].lock_type == UNLOCK_LOCK);
897                                 memcpy(&tp[count], &tmp_lock[0], sizeof(struct lock_struct));
898                         } else {
899                                 SMB_ASSERT(tmp_lock[0].lock_type == UNLOCK_LOCK);
900                                 SMB_ASSERT(tmp_lock[1].lock_type == locks[i].lock_type);
901                                 memcpy(&tp[count], &tmp_lock[1], sizeof(struct lock_struct));
902                         }
903                         count++;
904                         overlap_found = True;
905                         continue;
906                 } else {
907                         /* tmp_count == 3 - (we split a lock range in two). */
908                         SMB_ASSERT(tmp_lock[0].lock_type == locks[i].lock_type);
909                         SMB_ASSERT(tmp_lock[1].lock_type == UNLOCK_LOCK);
910                         SMB_ASSERT(tmp_lock[2].lock_type == locks[i].lock_type);
911
912                         memcpy(&tp[count], &tmp_lock[0], sizeof(struct lock_struct));
913                         count++;
914                         memcpy(&tp[count], &tmp_lock[2], sizeof(struct lock_struct));
915                         count++;
916                         overlap_found = True;
917                         /* Optimisation... */
918                         /* We know we're finished here as we can't overlap any
919                            more POSIX locks. Copy the rest of the lock array. */
920                         if (i < br_lck->num_locks - 1) {
921                                 memcpy(&tp[count], &locks[i+1], 
922                                         sizeof(*locks)*((br_lck->num_locks-1) - i));
923                                 count += ((br_lck->num_locks-1) - i);
924                         }
925                         break;
926                 }
927         }
928
929         if (!overlap_found) {
930                 /* Just ignore - no change. */
931                 SAFE_FREE(tp);
932                 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
933                 return True;
934         }
935
936 #if 0
937         /* FIXME - this call doesn't work correctly yet for POSIX locks... */
938
939         /* Unlock any POSIX regions. */
940         if(lp_posix_locking(br_lck->fsp->conn->cnum)) {
941                 release_posix_lock(br_lck->fsp, plock->start, plock->size);
942         }
943 #endif
944
945         /* Realloc so we don't leak entries per unlock call. */
946         if (count) {
947                 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
948                 if (!tp) {
949                         DEBUG(10,("brl_unlock_posix: realloc fail\n"));
950                         return False;
951                 }
952         } else {
953                 /* We deleted the last lock. */
954                 SAFE_FREE(tp);
955                 tp = NULL;
956         }
957
958         br_lck->num_locks = count;
959         br_lck->lock_data = (void *)tp;
960         br_lck->modified = True;
961
962         /* Send unlock messages to any pending waiters that overlap. */
963         locks = tp;
964
965         for (j=0; j < br_lck->num_locks; j++) {
966                 struct lock_struct *pend_lock = &locks[j];
967
968                 /* Ignore non-pending locks. */
969                 if (pend_lock->lock_type != PENDING_LOCK) {
970                         continue;
971                 }
972
973                 /* We could send specific lock info here... */
974                 if (brl_pending_overlap(lock, pend_lock)) {
975                         DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
976                                 procid_str_static(&pend_lock->context.pid )));
977
978                         become_root();
979                         message_send_pid(pend_lock->context.pid,
980                                         MSG_SMB_UNLOCK,
981                                         NULL, 0, True);
982                         unbecome_root();
983                 }
984         }
985
986         return True;
987 }
988
989 /****************************************************************************
990  Unlock a range of bytes.
991 ****************************************************************************/
992
993 BOOL brl_unlock(struct byte_range_lock *br_lck,
994                 uint16 smbpid,
995                 struct process_id pid,
996                 br_off start,
997                 br_off size,
998                 enum brl_flavour lock_flav)
999 {
1000         struct lock_struct lock;
1001
1002         lock.context.smbpid = smbpid;
1003         lock.context.pid = pid;
1004         lock.context.tid = br_lck->fsp->conn->cnum;
1005         lock.start = start;
1006         lock.size = size;
1007         lock.fnum = br_lck->fsp->fnum;
1008         lock.lock_type = UNLOCK_LOCK;
1009         lock.lock_flav = lock_flav;
1010
1011         if (lock_flav == WINDOWS_LOCK) {
1012                 return brl_unlock_windows(br_lck, &lock);
1013         } else {
1014                 return brl_unlock_posix(br_lck, &lock);
1015         }
1016 }
1017
1018 /****************************************************************************
1019  Test if we could add a lock if we wanted to.
1020  Returns True if the region required is currently unlocked, False if locked.
1021 ****************************************************************************/
1022
1023 BOOL brl_locktest(struct byte_range_lock *br_lck,
1024                 uint16 smbpid,
1025                 struct process_id pid,
1026                 br_off start,
1027                 br_off size, 
1028                 enum brl_type lock_type,
1029                 enum brl_flavour lock_flav)
1030 {
1031         BOOL ret = True;
1032         unsigned int i;
1033         struct lock_struct lock;
1034         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1035         files_struct *fsp = br_lck->fsp;
1036
1037         lock.context.smbpid = smbpid;
1038         lock.context.pid = pid;
1039         lock.context.tid = br_lck->fsp->conn->cnum;
1040         lock.start = start;
1041         lock.size = size;
1042         lock.fnum = fsp->fnum;
1043         lock.lock_type = lock_type;
1044         lock.lock_flav = lock_flav;
1045
1046         /* Make sure existing locks don't conflict */
1047         for (i=0; i < br_lck->num_locks; i++) {
1048                 /*
1049                  * Our own locks don't conflict.
1050                  */
1051                 if (brl_conflict_other(&locks[i], &lock)) {
1052                         return False;
1053                 }
1054         }
1055
1056         /*
1057          * There is no lock held by an SMB daemon, check to
1058          * see if there is a POSIX lock from a UNIX or NFS process.
1059          * This only conflicts with Windows locks, not POSIX locks.
1060          */
1061
1062         if(lp_posix_locking(fsp->conn->cnum) && (lock_flav == WINDOWS_LOCK)) {
1063                 ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
1064
1065                 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1066                         (double)start, (double)size, ret ? "locked" : "unlocked",
1067                         fsp->fnum, fsp->fsp_name ));
1068
1069                 /* We need to return the inverse of is_posix_locked. */
1070                 ret = !ret;
1071         }
1072
1073         /* no conflicts - we could have added it */
1074         return ret;
1075 }
1076
1077 /****************************************************************************
1078  Query for existing locks.
1079 ****************************************************************************/
1080
1081 NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
1082                 uint16 *psmbpid,
1083                 struct process_id pid,
1084                 br_off *pstart,
1085                 br_off *psize, 
1086                 enum brl_type *plock_type,
1087                 enum brl_flavour lock_flav)
1088 {
1089         unsigned int i;
1090         struct lock_struct lock;
1091         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1092         files_struct *fsp = br_lck->fsp;
1093
1094         lock.context.smbpid = *psmbpid;
1095         lock.context.pid = pid;
1096         lock.context.tid = br_lck->fsp->conn->cnum;
1097         lock.start = *pstart;
1098         lock.size = *psize;
1099         lock.fnum = fsp->fnum;
1100         lock.lock_type = *plock_type;
1101         lock.lock_flav = lock_flav;
1102
1103         /* Make sure existing locks don't conflict */
1104         for (i=0; i < br_lck->num_locks; i++) {
1105                 struct lock_struct *exlock = &locks[i];
1106                 BOOL conflict = False;
1107
1108                 if (exlock->lock_flav == WINDOWS_LOCK) {
1109                         conflict = brl_conflict(exlock, &lock);
1110                 } else {        
1111                         conflict = brl_conflict_posix(exlock, &lock);
1112                 }
1113
1114                 if (conflict) {
1115                         *psmbpid = exlock->context.smbpid;
1116                         *pstart = exlock->start;
1117                         *psize = exlock->size;
1118                         *plock_type = exlock->lock_type;
1119                         return NT_STATUS_LOCK_NOT_GRANTED;
1120                 }
1121         }
1122
1123         /*
1124          * There is no lock held by an SMB daemon, check to
1125          * see if there is a POSIX lock from a UNIX or NFS process.
1126          */
1127
1128         if(lp_posix_locking(fsp->conn->cnum)) {
1129                 BOOL ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
1130
1131                 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1132                         (double)*pstart, (double)*psize, ret ? "locked" : "unlocked",
1133                         fsp->fnum, fsp->fsp_name ));
1134
1135                 if (ret) {
1136                         /* Hmmm. No clue what to set smbpid to - use -1. */
1137                         *psmbpid = 0xFFFF;
1138                         return NT_STATUS_LOCK_NOT_GRANTED;
1139                 }
1140         }
1141
1142         return NT_STATUS_OK;
1143 }
1144
1145
1146 /****************************************************************************
1147  Remove a particular pending lock.
1148 ****************************************************************************/
1149
1150 BOOL brl_remove_pending_lock(struct byte_range_lock *br_lck,
1151                 uint16 smbpid,
1152                 struct process_id pid,
1153                 br_off start,
1154                 br_off size,
1155                 enum brl_flavour lock_flav)
1156 {
1157         unsigned int i;
1158         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1159         struct lock_context context;
1160
1161         context.smbpid = smbpid;
1162         context.pid = pid;
1163         context.tid = br_lck->fsp->conn->cnum;
1164
1165         for (i = 0; i < br_lck->num_locks; i++) {
1166                 struct lock_struct *lock = &locks[i];
1167
1168                 /* For pending locks we *always* care about the fnum. */
1169                 if (brl_same_context(&lock->context, &context) &&
1170                                 lock->fnum == br_lck->fsp->fnum &&
1171                                 lock->lock_type == PENDING_LOCK &&
1172                                 lock->lock_flav == lock_flav &&
1173                                 lock->start == start &&
1174                                 lock->size == size) {
1175                         break;
1176                 }
1177         }
1178
1179         if (i == br_lck->num_locks) {
1180                 /* Didn't find it. */
1181                 return False;
1182         }
1183
1184         if (i < br_lck->num_locks - 1) {
1185                 /* Found this particular pending lock - delete it */
1186                 memmove(&locks[i], &locks[i+1], 
1187                         sizeof(*locks)*((br_lck->num_locks-1) - i));
1188         }
1189
1190         br_lck->num_locks -= 1;
1191         br_lck->modified = True;
1192         return True;
1193 }
1194
1195
1196 /****************************************************************************
1197  Remove any locks associated with a open file.
1198 ****************************************************************************/
1199
1200 void brl_close_fnum(struct byte_range_lock *br_lck, struct process_id pid)
1201 {
1202         files_struct *fsp = br_lck->fsp;
1203         uint16 tid = fsp->conn->cnum;
1204         int fnum = fsp->fnum;
1205         unsigned int i, j, dcount=0;
1206         struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1207
1208         /* Remove any existing locks for this fnum (or any fnum if they're POSIX). */
1209
1210         for (i=0; i < br_lck->num_locks; i++) {
1211                 struct lock_struct *lock = &locks[i];
1212                 BOOL del_this_lock = False;
1213
1214                 if (lock->context.tid == tid && procid_equal(&lock->context.pid, &pid)) {
1215                         if ((lock->lock_flav == WINDOWS_LOCK) && (lock->fnum == fnum)) {
1216                                 del_this_lock = True;
1217                         } else if (lock->lock_flav == POSIX_LOCK) {
1218                                 del_this_lock = True;
1219                         }
1220                 }
1221
1222                 if (del_this_lock) {
1223                         /* Send unlock messages to any pending waiters that overlap. */
1224                         for (j=0; j < br_lck->num_locks; j++) {
1225                                 struct lock_struct *pend_lock = &locks[j];
1226
1227                                 /* Ignore our own or non-pending locks. */
1228                                 if (pend_lock->lock_type != PENDING_LOCK) {
1229                                         continue;
1230                                 }
1231
1232                                 /* Optimisation - don't send to this fnum as we're
1233                                    closing it. */
1234                                 if (pend_lock->context.tid == tid &&
1235                                     procid_equal(&pend_lock->context.pid, &pid) &&
1236                                     pend_lock->fnum == fnum) {
1237                                         continue;
1238                                 }
1239
1240                                 /* We could send specific lock info here... */
1241                                 if (brl_pending_overlap(lock, pend_lock)) {
1242                                         become_root();
1243                                         message_send_pid(pend_lock->context.pid,
1244                                                         MSG_SMB_UNLOCK,
1245                                                         NULL, 0, True);
1246                                         unbecome_root();
1247                                 }
1248                         }
1249
1250                         /* found it - delete it */
1251                         if (br_lck->num_locks > 1 && i < br_lck->num_locks - 1) {
1252                                 memmove(&locks[i], &locks[i+1], 
1253                                         sizeof(*locks)*((br_lck->num_locks-1) - i));
1254                         }
1255                         br_lck->num_locks--;
1256                         br_lck->modified = True;
1257                         i--;
1258                         dcount++;
1259                 }
1260         }
1261 }
1262
1263 /****************************************************************************
1264  Traverse the whole database with this function, calling traverse_callback
1265  on each lock.
1266 ****************************************************************************/
1267
1268 static int traverse_fn(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
1269 {
1270         struct lock_struct *locks;
1271         struct lock_key *key;
1272         int i;
1273
1274         BRLOCK_FN(traverse_callback) = (BRLOCK_FN_CAST())state;
1275
1276         locks = (struct lock_struct *)dbuf.dptr;
1277         key = (struct lock_key *)kbuf.dptr;
1278
1279         for (i=0;i<dbuf.dsize/sizeof(*locks);i++) {
1280                 traverse_callback(key->device,
1281                                   key->inode,
1282                                   locks[i].context.pid,
1283                                   locks[i].lock_type,
1284                                   locks[i].lock_flav,
1285                                   locks[i].start,
1286                                   locks[i].size);
1287         }
1288         return 0;
1289 }
1290
1291 /*******************************************************************
1292  Call the specified function on each lock in the database.
1293 ********************************************************************/
1294
1295 int brl_forall(BRLOCK_FN(fn))
1296 {
1297         if (!tdb) {
1298                 return 0;
1299         }
1300         return tdb_traverse(tdb, traverse_fn, (void *)fn);
1301 }
1302
1303 /*******************************************************************
1304  Store a potentially modified set of byte range lock data back into
1305  the database.
1306  Unlock the record.
1307 ********************************************************************/
1308
1309 int byte_range_lock_destructor(struct byte_range_lock *br_lck)
1310 {
1311         TDB_DATA key;
1312
1313         key.dptr = (char *)&br_lck->key;
1314         key.dsize = sizeof(struct lock_key);
1315
1316         if (!br_lck->modified) {
1317                 goto done;
1318         }
1319
1320         if (br_lck->num_locks == 0) {
1321                 /* No locks - delete this entry. */
1322                 if (tdb_delete(tdb, key) == -1) {
1323                         smb_panic("Could not delete byte range lock entry\n");
1324                 }
1325         } else {
1326                 TDB_DATA data;
1327                 data.dptr = br_lck->lock_data;
1328                 data.dsize = br_lck->num_locks * sizeof(struct lock_struct);
1329
1330                 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1) {
1331                         smb_panic("Could not store byte range mode entry\n");
1332                 }
1333         }
1334
1335  done:
1336
1337         tdb_chainunlock(tdb, key);
1338         SAFE_FREE(br_lck->lock_data);
1339         SAFE_FREE(br_lck);
1340         return 0;
1341 }
1342
1343 /*******************************************************************
1344  Fetch a set of byte range lock data from the database.
1345  Leave the record locked.
1346 ********************************************************************/
1347
1348 struct byte_range_lock *brl_get_locks(files_struct *fsp)
1349 {
1350         TDB_DATA key;
1351         TDB_DATA data;
1352         struct byte_range_lock *br_lck = SMB_MALLOC_P(struct byte_range_lock);
1353
1354         if (br_lck == NULL) {
1355                 return NULL;
1356         }
1357
1358         br_lck->fsp = fsp;
1359         br_lck->num_locks = 0;
1360         br_lck->modified = False;
1361         memset(&br_lck->key, '\0', sizeof(struct lock_key));
1362         br_lck->key.device = fsp->dev;
1363         br_lck->key.inode = fsp->inode;
1364
1365         key.dptr = (char *)&br_lck->key;
1366         key.dsize = sizeof(struct lock_key);
1367
1368         if (tdb_chainlock(tdb, key) != 0) {
1369                 DEBUG(3, ("Could not lock byte range lock entry\n"));
1370                 SAFE_FREE(br_lck);
1371                 return NULL;
1372         }
1373
1374         data = tdb_fetch(tdb, key);
1375         br_lck->lock_data = (void *)data.dptr;
1376         br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
1377
1378         if (DEBUGLEVEL >= 10) {
1379                 unsigned int i;
1380                 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1381                 DEBUG(10,("brl_get_locks: %u current locks on dev=%.0f, inode=%.0f\n",
1382                         br_lck->num_locks,
1383                         (double)fsp->dev, (double)fsp->inode ));
1384                 for( i = 0; i < br_lck->num_locks; i++) {
1385                         print_lock_struct(i, &locks[i]);
1386                 }
1387         }
1388         return br_lck;
1389 }