r17346: Add optimisation vl needs for the cluster code where
[sfrench/samba-autobuild/.git] / source / locking / locking.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Locking functions
4    Copyright (C) Andrew Tridgell 1992-2000
5    Copyright (C) Jeremy Allison 1992-2006
6    Copyright (C) Volker Lendecke 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22    Revision History:
23
24    12 aug 96: Erik.Devriendt@te6.siemens.be
25    added support for shared memory implementation of share mode locking
26
27    May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
28    locking to deal with multiple share modes per open file.
29
30    September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
31    support.
32
33    rewrtten completely to use new tdb code. Tridge, Dec '99
34
35    Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
36    Added Unix Extensions POSIX locking support. Jeremy Allison Mar 2006.
37 */
38
39 #include "includes.h"
40
41 #undef DBGC_CLASS
42 #define DBGC_CLASS DBGC_LOCKING
43
44 /* the locking database handle */
45 static TDB_CONTEXT *tdb;
46
47 /****************************************************************************
48  Debugging aids :-).
49 ****************************************************************************/
50
51 const char *lock_type_name(enum brl_type lock_type)
52 {
53         switch (lock_type) {
54                 case READ_LOCK:
55                         return "READ";
56                 case WRITE_LOCK:
57                         return "WRITE";
58                 case PENDING_READ_LOCK:
59                         return "PENDING_READ";
60                 case PENDING_WRITE_LOCK:
61                         return "PENDING_WRITE";
62                 default:
63                         return "other";
64         }
65 }
66
67 const char *lock_flav_name(enum brl_flavour lock_flav)
68 {
69         return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
70 }
71
72 /****************************************************************************
73  Utility function called to see if a file region is locked.
74  Called in the read/write codepath.
75 ****************************************************************************/
76
77 BOOL is_locked(files_struct *fsp,
78                 uint32 smbpid,
79                 SMB_BIG_UINT count,
80                 SMB_BIG_UINT offset, 
81                 enum brl_type lock_type)
82 {
83         int snum = SNUM(fsp->conn);
84         int strict_locking = lp_strict_locking(snum);
85         enum brl_flavour lock_flav = lp_posix_cifsu_locktype();
86         BOOL ret = True;
87         
88         if (count == 0) {
89                 return False;
90         }
91
92         if (!lp_locking(snum) || !strict_locking) {
93                 return False;
94         }
95
96         if (strict_locking == Auto) {
97                 if  (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (lock_type == READ_LOCK || lock_type == WRITE_LOCK)) {
98                         DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp->fsp_name ));
99                         ret = False;
100                 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
101                            (lock_type == READ_LOCK)) {
102                         DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp->fsp_name ));
103                         ret = False;
104                 } else {
105                         struct byte_range_lock *br_lck = brl_get_locks_readonly(NULL, fsp);
106                         if (!br_lck) {
107                                 return False;
108                         }
109                         ret = !brl_locktest(br_lck,
110                                         smbpid,
111                                         procid_self(),
112                                         offset,
113                                         count,
114                                         lock_type,
115                                         lock_flav);
116                         TALLOC_FREE(br_lck);
117                 }
118         } else {
119                 struct byte_range_lock *br_lck = brl_get_locks_readonly(NULL, fsp);
120                 if (!br_lck) {
121                         return False;
122                 }
123                 ret = !brl_locktest(br_lck,
124                                 smbpid,
125                                 procid_self(),
126                                 offset,
127                                 count,
128                                 lock_type,
129                                 lock_flav);
130                 TALLOC_FREE(br_lck);
131         }
132
133         DEBUG(10,("is_locked: flavour = %s brl start=%.0f len=%.0f %s for fnum %d file %s\n",
134                         lock_flav_name(lock_flav),
135                         (double)offset, (double)count, ret ? "locked" : "unlocked",
136                         fsp->fnum, fsp->fsp_name ));
137
138         return ret;
139 }
140
141 /****************************************************************************
142  Find out if a lock could be granted - return who is blocking us if we can't.
143 ****************************************************************************/
144
145 NTSTATUS query_lock(files_struct *fsp,
146                         uint32 *psmbpid,
147                         SMB_BIG_UINT *pcount,
148                         SMB_BIG_UINT *poffset,
149                         enum brl_type *plock_type,
150                         enum brl_flavour lock_flav)
151 {
152         struct byte_range_lock *br_lck = NULL;
153         NTSTATUS status = NT_STATUS_LOCK_NOT_GRANTED;
154
155         if (!fsp->can_lock) {
156                 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
157         }
158
159         if (!lp_locking(SNUM(fsp->conn))) {
160                 return NT_STATUS_OK;
161         }
162
163         br_lck = brl_get_locks_readonly(NULL, fsp);
164         if (!br_lck) {
165                 return NT_STATUS_NO_MEMORY;
166         }
167
168         status = brl_lockquery(br_lck,
169                         psmbpid,
170                         procid_self(),
171                         poffset,
172                         pcount,
173                         plock_type,
174                         lock_flav);
175
176         TALLOC_FREE(br_lck);
177         return status;
178 }
179
180 /****************************************************************************
181  Utility function called by locking requests.
182 ****************************************************************************/
183
184 struct byte_range_lock *do_lock(files_struct *fsp,
185                         uint32 lock_pid,
186                         SMB_BIG_UINT count,
187                         SMB_BIG_UINT offset,
188                         enum brl_type lock_type,
189                         enum brl_flavour lock_flav,
190                         BOOL blocking_lock,
191                         NTSTATUS *perr)
192 {
193         struct byte_range_lock *br_lck = NULL;
194
195         if (!fsp->can_lock) {
196                 *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
197                 return NULL;
198         }
199
200         if (!lp_locking(SNUM(fsp->conn))) {
201                 *perr = NT_STATUS_OK;
202                 return NULL;
203         }
204
205         /* NOTE! 0 byte long ranges ARE allowed and should be stored  */
206
207         DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f requested for fnum %d file %s\n",
208                 lock_flav_name(lock_flav), lock_type_name(lock_type),
209                 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
210
211         br_lck = brl_get_locks(NULL, fsp);
212         if (!br_lck) {
213                 *perr = NT_STATUS_NO_MEMORY;
214                 return NULL;
215         }
216
217         *perr = brl_lock(br_lck,
218                         lock_pid,
219                         procid_self(),
220                         offset,
221                         count, 
222                         lock_type,
223                         lock_flav,
224                         blocking_lock);
225
226         return br_lck;
227 }
228
229 /****************************************************************************
230  Utility function called by unlocking requests.
231 ****************************************************************************/
232
233 NTSTATUS do_unlock(files_struct *fsp,
234                         uint32 lock_pid,
235                         SMB_BIG_UINT count,
236                         SMB_BIG_UINT offset,
237                         enum brl_flavour lock_flav)
238 {
239         BOOL ok = False;
240         struct byte_range_lock *br_lck = NULL;
241         
242         if (!fsp->can_lock) {
243                 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
244         }
245         
246         if (!lp_locking(SNUM(fsp->conn))) {
247                 return NT_STATUS_OK;
248         }
249         
250         DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
251                   (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
252
253         br_lck = brl_get_locks(NULL, fsp);
254         if (!br_lck) {
255                 return NT_STATUS_NO_MEMORY;
256         }
257
258         ok = brl_unlock(br_lck,
259                         lock_pid,
260                         procid_self(),
261                         offset,
262                         count,
263                         lock_flav);
264    
265         TALLOC_FREE(br_lck);
266
267         if (!ok) {
268                 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
269                 return NT_STATUS_RANGE_NOT_LOCKED;
270         }
271
272         return NT_STATUS_OK;
273 }
274
275 /****************************************************************************
276  Cancel any pending blocked locks.
277 ****************************************************************************/
278
279 NTSTATUS do_lock_cancel(files_struct *fsp,
280                         uint32 lock_pid,
281                         SMB_BIG_UINT count,
282                         SMB_BIG_UINT offset,
283                         enum brl_flavour lock_flav)
284 {
285         BOOL ok = False;
286         struct byte_range_lock *br_lck = NULL;
287         
288         if (!fsp->can_lock) {
289                 return fsp->is_directory ?
290                         NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
291         }
292         
293         if (!lp_locking(SNUM(fsp->conn))) {
294                 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
295         }
296
297         DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
298                   (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
299
300         br_lck = brl_get_locks(NULL, fsp);
301         if (!br_lck) {
302                 return NT_STATUS_NO_MEMORY;
303         }
304
305         ok = brl_lock_cancel(br_lck,
306                         lock_pid,
307                         procid_self(),
308                         offset,
309                         count,
310                         lock_flav);
311    
312         TALLOC_FREE(br_lck);
313
314         if (!ok) {
315                 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
316                 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
317         }
318
319         return NT_STATUS_OK;
320 }
321
322 /****************************************************************************
323  Remove any locks on this fd. Called from file_close().
324 ****************************************************************************/
325
326 void locking_close_file(files_struct *fsp)
327 {
328         struct byte_range_lock *br_lck;
329
330         if (!lp_locking(SNUM(fsp->conn))) {
331                 return;
332         }
333
334         br_lck = brl_get_locks(NULL,fsp);
335
336         if (br_lck) {
337                 cancel_pending_lock_requests_by_fid(fsp, br_lck);
338                 brl_close_fnum(br_lck);
339                 TALLOC_FREE(br_lck);
340         }
341 }
342
343 /****************************************************************************
344  Initialise the locking functions.
345 ****************************************************************************/
346
347 static int open_read_only;
348
349 BOOL locking_init(int read_only)
350 {
351         brl_init(read_only);
352
353         if (tdb)
354                 return True;
355
356         tdb = tdb_open_log(lock_path("locking.tdb"), 
357                         lp_open_files_db_hash_size(),
358                         TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST), 
359                         read_only?O_RDONLY:O_RDWR|O_CREAT,
360                         0644);
361
362         if (!tdb) {
363                 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
364                 return False;
365         }
366
367         if (!posix_locking_init(read_only))
368                 return False;
369
370         open_read_only = read_only;
371
372         return True;
373 }
374
375 /*******************************************************************
376  Deinitialize the share_mode management.
377 ******************************************************************/
378
379 BOOL locking_end(void)
380 {
381         BOOL ret = True;
382
383         brl_shutdown(open_read_only);
384         if (tdb) {
385                 if (tdb_close(tdb) != 0)
386                         ret = False;
387         }
388
389         return ret;
390 }
391
392 /*******************************************************************
393  Form a static locking key for a dev/inode pair.
394 ******************************************************************/
395
396 /* key and data records in the tdb locking database */
397 struct locking_key {
398         SMB_DEV_T dev;
399         SMB_INO_T ino;
400 };
401
402 /*******************************************************************
403  Form a static locking key for a dev/inode pair.
404 ******************************************************************/
405
406 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
407 {
408         static struct locking_key key;
409         TDB_DATA kbuf;
410
411         memset(&key, '\0', sizeof(key));
412         key.dev = dev;
413         key.ino = inode;
414         kbuf.dptr = (char *)&key;
415         kbuf.dsize = sizeof(key);
416         return kbuf;
417 }
418
419 /*******************************************************************
420  Print out a share mode.
421 ********************************************************************/
422
423 char *share_mode_str(int num, struct share_mode_entry *e)
424 {
425         static pstring share_str;
426
427         slprintf(share_str, sizeof(share_str)-1, "share_mode_entry[%d]: %s "
428                  "pid = %s, share_access = 0x%x, private_options = 0x%x, "
429                  "access_mask = 0x%x, mid = 0x%x, type= 0x%x, file_id = %lu, "
430                  "uid = %u, dev = 0x%x, inode = %.0f",
431                  num,
432                  e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
433                  procid_str_static(&e->pid),
434                  e->share_access, e->private_options,
435                  e->access_mask, e->op_mid, e->op_type, e->share_file_id,
436                  (unsigned int)e->uid, (unsigned int)e->dev, (double)e->inode );
437
438         return share_str;
439 }
440
441 /*******************************************************************
442  Print out a share mode table.
443 ********************************************************************/
444
445 static void print_share_mode_table(struct locking_data *data)
446 {
447         int num_share_modes = data->u.s.num_share_mode_entries;
448         struct share_mode_entry *shares =
449                 (struct share_mode_entry *)(data + 1);
450         int i;
451
452         for (i = 0; i < num_share_modes; i++) {
453                 struct share_mode_entry entry;
454
455                 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
456                 DEBUG(10,("print_share_mode_table: %s\n",
457                           share_mode_str(i, &entry)));
458         }
459 }
460
461 /*******************************************************************
462  Get all share mode entries for a dev/inode pair.
463 ********************************************************************/
464
465 static BOOL parse_share_modes(TDB_DATA dbuf, struct share_mode_lock *lck)
466 {
467         struct locking_data *data;
468         int i;
469
470         if (dbuf.dsize < sizeof(struct locking_data)) {
471                 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
472         }
473
474         data = (struct locking_data *)dbuf.dptr;
475
476         lck->delete_on_close = data->u.s.delete_on_close;
477         lck->initial_delete_on_close = data->u.s.initial_delete_on_close;
478         lck->num_share_modes = data->u.s.num_share_mode_entries;
479
480         DEBUG(10, ("parse_share_modes: delete_on_close: %d, "
481                    "initial_delete_on_close: %d, "
482                    "num_share_modes: %d\n",
483                 lck->delete_on_close,
484                 lck->initial_delete_on_close,
485                 lck->num_share_modes));
486
487         if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
488                 DEBUG(0, ("invalid number of share modes: %d\n",
489                           lck->num_share_modes));
490                 smb_panic("PANIC: invalid number of share modes");
491         }
492
493         lck->share_modes = NULL;
494         
495         if (lck->num_share_modes != 0) {
496
497                 if (dbuf.dsize < (sizeof(struct locking_data) +
498                                   (lck->num_share_modes *
499                                    sizeof(struct share_mode_entry)))) {
500                         smb_panic("PANIC: parse_share_modes: buffer too short.\n");
501                 }
502                                   
503                 lck->share_modes = (struct share_mode_entry *)
504                         talloc_memdup(lck, dbuf.dptr+sizeof(*data),
505                                       lck->num_share_modes *
506                                       sizeof(struct share_mode_entry));
507
508                 if (lck->share_modes == NULL) {
509                         smb_panic("talloc failed\n");
510                 }
511         }
512
513         /* Get any delete token. */
514         if (data->u.s.delete_token_size) {
515                 char *p = dbuf.dptr + sizeof(*data) +
516                                 (lck->num_share_modes *
517                                 sizeof(struct share_mode_entry));
518
519                 if ((data->u.s.delete_token_size < sizeof(uid_t) + sizeof(gid_t)) ||
520                                 ((data->u.s.delete_token_size - sizeof(uid_t)) % sizeof(gid_t)) != 0) {
521                         DEBUG(0, ("parse_share_modes: invalid token size %d\n",
522                                 data->u.s.delete_token_size));
523                         smb_panic("parse_share_modes: invalid token size\n");
524                 }
525
526                 lck->delete_token = TALLOC_P(lck, UNIX_USER_TOKEN);
527                 if (!lck->delete_token) {
528                         smb_panic("talloc failed\n");
529                 }
530
531                 /* Copy out the uid and gid. */
532                 memcpy(&lck->delete_token->uid, p, sizeof(uid_t));
533                 p += sizeof(uid_t);
534                 memcpy(&lck->delete_token->gid, p, sizeof(gid_t));
535                 p += sizeof(gid_t);
536
537                 /* Any supplementary groups ? */
538                 lck->delete_token->ngroups = (data->u.s.delete_token_size > (sizeof(uid_t) + sizeof(gid_t))) ?
539                                         ((data->u.s.delete_token_size -
540                                                 (sizeof(uid_t) + sizeof(gid_t)))/sizeof(gid_t)) : 0;
541
542                 if (lck->delete_token->ngroups) {
543                         /* Make this a talloc child of lck->delete_token. */
544                         lck->delete_token->groups = TALLOC_ARRAY(lck->delete_token, gid_t,
545                                                         lck->delete_token->ngroups);
546                         if (!lck->delete_token) {
547                                 smb_panic("talloc failed\n");
548                         }
549
550                         for (i = 0; i < lck->delete_token->ngroups; i++) {
551                                 memcpy(&lck->delete_token->groups[i], p, sizeof(gid_t));
552                                 p += sizeof(gid_t);
553                         }
554                 }
555
556         } else {
557                 lck->delete_token = NULL;
558         }
559
560         /* Save off the associated service path and filename. */
561         lck->servicepath = talloc_strdup(lck, dbuf.dptr + sizeof(*data) +
562                                         (lck->num_share_modes *
563                                         sizeof(struct share_mode_entry)) +
564                                         data->u.s.delete_token_size );
565
566         lck->filename = talloc_strdup(lck, dbuf.dptr + sizeof(*data) +
567                                         (lck->num_share_modes *
568                                         sizeof(struct share_mode_entry)) +
569                                         data->u.s.delete_token_size +
570                                         strlen(lck->servicepath) + 1 );
571
572         /*
573          * Ensure that each entry has a real process attached.
574          */
575
576         for (i = 0; i < lck->num_share_modes; i++) {
577                 struct share_mode_entry *entry_p = &lck->share_modes[i];
578                 DEBUG(10,("parse_share_modes: %s\n",
579                           share_mode_str(i, entry_p) ));
580                 if (!process_exists(entry_p->pid)) {
581                         DEBUG(10,("parse_share_modes: deleted %s\n",
582                                   share_mode_str(i, entry_p) ));
583                         entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
584                         lck->modified = True;
585                 }
586         }
587
588         return True;
589 }
590
591 static TDB_DATA unparse_share_modes(struct share_mode_lock *lck)
592 {
593         TDB_DATA result;
594         int num_valid = 0;
595         int i;
596         struct locking_data *data;
597         ssize_t offset;
598         ssize_t sp_len;
599         uint32 delete_token_size;
600
601         result.dptr = NULL;
602         result.dsize = 0;
603
604         for (i=0; i<lck->num_share_modes; i++) {
605                 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
606                         num_valid += 1;
607                 }
608         }
609
610         if (num_valid == 0) {
611                 return result;
612         }
613
614         sp_len = strlen(lck->servicepath);
615         delete_token_size = (lck->delete_token ?
616                         (sizeof(uid_t) + sizeof(gid_t) + (lck->delete_token->ngroups*sizeof(gid_t))) : 0);
617
618         result.dsize = sizeof(*data) +
619                 lck->num_share_modes * sizeof(struct share_mode_entry) +
620                 delete_token_size +
621                 sp_len + 1 +
622                 strlen(lck->filename) + 1;
623         result.dptr = TALLOC_ARRAY(lck, char, result.dsize);
624
625         if (result.dptr == NULL) {
626                 smb_panic("talloc failed\n");
627         }
628
629         data = (struct locking_data *)result.dptr;
630         ZERO_STRUCTP(data);
631         data->u.s.num_share_mode_entries = lck->num_share_modes;
632         data->u.s.delete_on_close = lck->delete_on_close;
633         data->u.s.initial_delete_on_close = lck->initial_delete_on_close;
634         data->u.s.delete_token_size = delete_token_size;
635         DEBUG(10, ("unparse_share_modes: del: %d, initial del %d, tok = %u, num: %d\n",
636                 data->u.s.delete_on_close,
637                 data->u.s.initial_delete_on_close,
638                 (unsigned int)data->u.s.delete_token_size,
639                 data->u.s.num_share_mode_entries));
640         memcpy(result.dptr + sizeof(*data), lck->share_modes,
641                sizeof(struct share_mode_entry)*lck->num_share_modes);
642         offset = sizeof(*data) +
643                 sizeof(struct share_mode_entry)*lck->num_share_modes;
644
645         /* Store any delete on close token. */
646         if (lck->delete_token) {
647                 char *p = result.dptr + offset;
648
649                 memcpy(p, &lck->delete_token->uid, sizeof(uid_t));
650                 p += sizeof(uid_t);
651
652                 memcpy(p, &lck->delete_token->gid, sizeof(gid_t));
653                 p += sizeof(gid_t);
654
655                 for (i = 0; i < lck->delete_token->ngroups; i++) {
656                         memcpy(p, &lck->delete_token->groups[i], sizeof(gid_t));
657                         p += sizeof(gid_t);
658                 }
659                 offset = p - result.dptr;
660         }
661
662         safe_strcpy(result.dptr + offset, lck->servicepath,
663                     result.dsize - offset - 1);
664         offset += sp_len + 1;
665         safe_strcpy(result.dptr + offset, lck->filename,
666                     result.dsize - offset - 1);
667
668         if (DEBUGLEVEL >= 10) {
669                 print_share_mode_table(data);
670         }
671
672         return result;
673 }
674
675 static int share_mode_lock_destructor(void *p)
676 {
677         struct share_mode_lock *lck =
678                 talloc_get_type_abort(p, struct share_mode_lock);
679         TDB_DATA key = locking_key(lck->dev, lck->ino);
680         TDB_DATA data;
681
682         if (!lck->modified) {
683                 goto done;
684         }
685
686         data = unparse_share_modes(lck);
687
688         if (data.dptr == NULL) {
689                 if (!lck->fresh) {
690                         /* There has been an entry before, delete it */
691                         if (tdb_delete(tdb, key) == -1) {
692                                 smb_panic("Could not delete share entry\n");
693                         }
694                 }
695                 goto done;
696         }
697
698         if (tdb_store(tdb, key, data, TDB_REPLACE) == -1) {
699                 smb_panic("Could not store share mode entry\n");
700         }
701
702  done:
703         tdb_chainunlock(tdb, key);
704
705         return 0;
706 }
707
708 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
709                                                 SMB_DEV_T dev, SMB_INO_T ino,
710                                                 const char *servicepath,
711                                                 const char *fname)
712 {
713         struct share_mode_lock *lck;
714         TDB_DATA key = locking_key(dev, ino);
715         TDB_DATA data;
716
717         lck = TALLOC_P(mem_ctx, struct share_mode_lock);
718         if (lck == NULL) {
719                 DEBUG(0, ("talloc failed\n"));
720                 return NULL;
721         }
722
723         /* Ensure we set every field here as the destructor must be
724            valid even if parse_share_modes fails. */
725
726         lck->servicepath = NULL;
727         lck->filename = NULL;
728         lck->dev = dev;
729         lck->ino = ino;
730         lck->num_share_modes = 0;
731         lck->share_modes = NULL;
732         lck->delete_token = NULL;
733         lck->delete_on_close = False;
734         lck->initial_delete_on_close = False;
735         lck->fresh = False;
736         lck->modified = False;
737
738         if (tdb_chainlock(tdb, key) != 0) {
739                 DEBUG(3, ("Could not lock share entry\n"));
740                 TALLOC_FREE(lck);
741                 return NULL;
742         }
743
744         /* We must set the destructor immediately after the chainlock
745            ensure the lock is cleaned up on any of the error return
746            paths below. */
747
748         talloc_set_destructor(lck, share_mode_lock_destructor);
749
750         data = tdb_fetch(tdb, key);
751         lck->fresh = (data.dptr == NULL);
752
753         if (lck->fresh) {
754
755                 if (fname == NULL || servicepath == NULL) {
756                         TALLOC_FREE(lck);
757                         return NULL;
758                 }
759                 lck->filename = talloc_strdup(lck, fname);
760                 lck->servicepath = talloc_strdup(lck, servicepath);
761                 if (lck->filename == NULL || lck->servicepath == NULL) {
762                         DEBUG(0, ("talloc failed\n"));
763                         TALLOC_FREE(lck);
764                         return NULL;
765                 }
766         } else {
767                 if (!parse_share_modes(data, lck)) {
768                         DEBUG(0, ("Could not parse share modes\n"));
769                         TALLOC_FREE(lck);
770                         SAFE_FREE(data.dptr);
771                         return NULL;
772                 }
773         }
774
775         SAFE_FREE(data.dptr);
776
777         return lck;
778 }
779
780 /*******************************************************************
781  Sets the service name and filename for rename.
782  At this point we emit "file renamed" messages to all
783  process id's that have this file open.
784  Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
785 ********************************************************************/
786
787 BOOL rename_share_filename(struct share_mode_lock *lck,
788                         const char *servicepath,
789                         const char *newname)
790 {
791         size_t sp_len;
792         size_t fn_len;
793         size_t msg_len;
794         char *frm = NULL;
795         int i;
796
797         if (!lck) {
798                 return False;
799         }
800
801         DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
802                 servicepath, newname));
803
804         /*
805          * rename_internal_fsp() and rename_internals() add './' to
806          * head of newname if newname does not contain a '/'.
807          */
808         while (newname[0] && newname[1] && newname[0] == '.' && newname[1] == '/') {
809                 newname += 2;
810         }
811
812         lck->servicepath = talloc_strdup(lck, servicepath);
813         lck->filename = talloc_strdup(lck, newname);
814         if (lck->filename == NULL || lck->servicepath == NULL) {
815                 DEBUG(0, ("rename_share_filename: talloc failed\n"));
816                 return False;
817         }
818         lck->modified = True;
819
820         sp_len = strlen(lck->servicepath);
821         fn_len = strlen(lck->filename);
822
823         msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + fn_len + 1;
824
825         /* Set up the name changed message. */
826         frm = TALLOC_ARRAY(lck, char, msg_len);
827         if (!frm) {
828                 return False;
829         }
830
831         SDEV_T_VAL(frm,0,lck->dev);
832         SINO_T_VAL(frm,8,lck->ino);
833
834         DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
835
836         safe_strcpy(&frm[16], lck->servicepath, sp_len);
837         safe_strcpy(&frm[16 + sp_len + 1], lck->filename, fn_len);
838
839         /* Send the messages. */
840         for (i=0; i<lck->num_share_modes; i++) {
841                 struct share_mode_entry *se = &lck->share_modes[i];
842                 if (!is_valid_share_mode_entry(se)) {
843                         continue;
844                 }
845                 /* But not to ourselves... */
846                 if (procid_is_me(&se->pid)) {
847                         continue;
848                 }
849
850                 DEBUG(10,("rename_share_filename: sending rename message to pid %s "
851                         "dev %x, inode  %.0f sharepath %s newname %s\n",
852                         procid_str_static(&se->pid),
853                         (unsigned int)lck->dev, (double)lck->ino,
854                         lck->servicepath, lck->filename ));
855
856                 message_send_pid(se->pid, MSG_SMB_FILE_RENAME,
857                                 frm, msg_len, True);
858         }
859
860         return True;
861 }
862
863 BOOL get_delete_on_close_flag(SMB_DEV_T dev, SMB_INO_T inode)
864 {
865         BOOL result;
866         struct share_mode_lock *lck = get_share_mode_lock(NULL, dev, inode, NULL, NULL);
867         if (!lck) {
868                 return False;
869         }
870         result = lck->delete_on_close;
871         TALLOC_FREE(lck);
872         return result;
873 }
874
875 BOOL is_valid_share_mode_entry(const struct share_mode_entry *e)
876 {
877         int num_props = 0;
878
879         num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
880         num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
881         num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
882
883         SMB_ASSERT(num_props <= 1);
884         return (num_props != 0);
885 }
886
887 BOOL is_deferred_open_entry(const struct share_mode_entry *e)
888 {
889         return (e->op_type == DEFERRED_OPEN_ENTRY);
890 }
891
892 BOOL is_unused_share_mode_entry(const struct share_mode_entry *e)
893 {
894         return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
895 }
896
897 /*******************************************************************
898  Fill a share mode entry.
899 ********************************************************************/
900
901 static void fill_share_mode_entry(struct share_mode_entry *e,
902                                   files_struct *fsp,
903                                   uid_t uid, uint16 mid, uint16 op_type)
904 {
905         ZERO_STRUCTP(e);
906         e->pid = procid_self();
907         e->share_access = fsp->share_access;
908         e->private_options = fsp->fh->private_options;
909         e->access_mask = fsp->access_mask;
910         e->op_mid = mid;
911         e->op_type = op_type;
912         e->time.tv_sec = fsp->open_time.tv_sec;
913         e->time.tv_usec = fsp->open_time.tv_usec;
914         e->dev = fsp->dev;
915         e->inode = fsp->inode;
916         e->share_file_id = fsp->fh->file_id;
917         e->uid = (uint32)uid;
918 }
919
920 static void fill_deferred_open_entry(struct share_mode_entry *e,
921                                      const struct timeval request_time,
922                                      SMB_DEV_T dev, SMB_INO_T ino, uint16 mid)
923 {
924         ZERO_STRUCTP(e);
925         e->pid = procid_self();
926         e->op_mid = mid;
927         e->op_type = DEFERRED_OPEN_ENTRY;
928         e->time.tv_sec = request_time.tv_sec;
929         e->time.tv_usec = request_time.tv_usec;
930         e->dev = dev;
931         e->inode = ino;
932         e->uid = (uint32)-1;
933 }
934
935 static void add_share_mode_entry(struct share_mode_lock *lck,
936                                  const struct share_mode_entry *entry)
937 {
938         int i;
939
940         for (i=0; i<lck->num_share_modes; i++) {
941                 struct share_mode_entry *e = &lck->share_modes[i];
942                 if (is_unused_share_mode_entry(e)) {
943                         *e = *entry;
944                         break;
945                 }
946         }
947
948         if (i == lck->num_share_modes) {
949                 /* No unused entry found */
950                 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
951                              &lck->share_modes, &lck->num_share_modes);
952         }
953         lck->modified = True;
954 }
955
956 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
957                         uid_t uid, uint16 mid, uint16 op_type)
958 {
959         struct share_mode_entry entry;
960         fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
961         add_share_mode_entry(lck, &entry);
962 }
963
964 void add_deferred_open(struct share_mode_lock *lck, uint16 mid,
965                        struct timeval request_time,
966                        SMB_DEV_T dev, SMB_INO_T ino)
967 {
968         struct share_mode_entry entry;
969         fill_deferred_open_entry(&entry, request_time, dev, ino, mid);
970         add_share_mode_entry(lck, &entry);
971 }
972
973 /*******************************************************************
974  Check if two share mode entries are identical, ignoring oplock 
975  and mid info and desired_access. (Removed paranoia test - it's
976  not automatically a logic error if they are identical. JRA.)
977 ********************************************************************/
978
979 static BOOL share_modes_identical(struct share_mode_entry *e1,
980                                   struct share_mode_entry *e2)
981 {
982         /* We used to check for e1->share_access == e2->share_access here
983            as well as the other fields but 2 different DOS or FCB opens
984            sharing the same share mode entry may validly differ in
985            fsp->share_access field. */
986
987         return (procid_equal(&e1->pid, &e2->pid) &&
988                 e1->dev == e2->dev &&
989                 e1->inode == e2->inode &&
990                 e1->share_file_id == e2->share_file_id );
991 }
992
993 static BOOL deferred_open_identical(struct share_mode_entry *e1,
994                                     struct share_mode_entry *e2)
995 {
996         return (procid_equal(&e1->pid, &e2->pid) &&
997                 (e1->op_mid == e2->op_mid) &&
998                 (e1->dev == e2->dev) &&
999                 (e1->inode == e2->inode));
1000 }
1001
1002 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1003                                                       struct share_mode_entry *entry)
1004 {
1005         int i;
1006
1007         for (i=0; i<lck->num_share_modes; i++) {
1008                 struct share_mode_entry *e = &lck->share_modes[i];
1009                 if (is_valid_share_mode_entry(entry) &&
1010                     is_valid_share_mode_entry(e) &&
1011                     share_modes_identical(e, entry)) {
1012                         return e;
1013                 }
1014                 if (is_deferred_open_entry(entry) &&
1015                     is_deferred_open_entry(e) &&
1016                     deferred_open_identical(e, entry)) {
1017                         return e;
1018                 }
1019         }
1020         return NULL;
1021 }
1022
1023 /*******************************************************************
1024  Del the share mode of a file for this process. Return the number of
1025  entries left.
1026 ********************************************************************/
1027
1028 BOOL del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1029 {
1030         struct share_mode_entry entry, *e;
1031
1032         /* Don't care about the pid owner being correct here - just a search. */
1033         fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1034
1035         e = find_share_mode_entry(lck, &entry);
1036         if (e == NULL) {
1037                 return False;
1038         }
1039
1040         e->op_type = UNUSED_SHARE_MODE_ENTRY;
1041         lck->modified = True;
1042         return True;
1043 }
1044
1045 void del_deferred_open_entry(struct share_mode_lock *lck, uint16 mid)
1046 {
1047         struct share_mode_entry entry, *e;
1048
1049         fill_deferred_open_entry(&entry, timeval_zero(),
1050                                  lck->dev, lck->ino, mid);
1051
1052         e = find_share_mode_entry(lck, &entry);
1053         if (e == NULL) {
1054                 return;
1055         }
1056
1057         e->op_type = UNUSED_SHARE_MODE_ENTRY;
1058         lck->modified = True;
1059 }
1060
1061 /*******************************************************************
1062  Remove an oplock mid and mode entry from a share mode.
1063 ********************************************************************/
1064
1065 BOOL remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1066 {
1067         struct share_mode_entry entry, *e;
1068
1069         /* Don't care about the pid owner being correct here - just a search. */
1070         fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1071
1072         e = find_share_mode_entry(lck, &entry);
1073         if (e == NULL) {
1074                 return False;
1075         }
1076
1077         e->op_mid = 0;
1078         e->op_type = NO_OPLOCK;
1079         lck->modified = True;
1080         return True;
1081 }
1082
1083 /*******************************************************************
1084  Downgrade a oplock type from exclusive to level II.
1085 ********************************************************************/
1086
1087 BOOL downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1088 {
1089         struct share_mode_entry entry, *e;
1090
1091         /* Don't care about the pid owner being correct here - just a search. */
1092         fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1093
1094         e = find_share_mode_entry(lck, &entry);
1095         if (e == NULL) {
1096                 return False;
1097         }
1098
1099         e->op_type = LEVEL_II_OPLOCK;
1100         lck->modified = True;
1101         return True;
1102 }
1103
1104 /****************************************************************************
1105  Deal with the internal needs of setting the delete on close flag. Note that
1106  as the tdb locking is recursive, it is safe to call this from within 
1107  open_file_ntcreate. JRA.
1108 ****************************************************************************/
1109
1110 NTSTATUS can_set_delete_on_close(files_struct *fsp, BOOL delete_on_close,
1111                                  uint32 dosmode)
1112 {
1113         if (!delete_on_close) {
1114                 return NT_STATUS_OK;
1115         }
1116
1117         /*
1118          * Only allow delete on close for writable files.
1119          */
1120
1121         if ((dosmode & aRONLY) &&
1122             !lp_delete_readonly(SNUM(fsp->conn))) {
1123                 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1124                           "flag set but file attribute is readonly.\n",
1125                           fsp->fsp_name ));
1126                 return NT_STATUS_CANNOT_DELETE;
1127         }
1128
1129         /*
1130          * Only allow delete on close for writable shares.
1131          */
1132
1133         if (!CAN_WRITE(fsp->conn)) {
1134                 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1135                           "close flag set but write access denied on share.\n",
1136                           fsp->fsp_name ));
1137                 return NT_STATUS_ACCESS_DENIED;
1138         }
1139
1140         /*
1141          * Only allow delete on close for files/directories opened with delete
1142          * intent.
1143          */
1144
1145         if (!(fsp->access_mask & DELETE_ACCESS)) {
1146                 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1147                           "close flag set but delete access denied.\n",
1148                           fsp->fsp_name ));
1149                 return NT_STATUS_ACCESS_DENIED;
1150         }
1151
1152         return NT_STATUS_OK;
1153 }
1154
1155 /*************************************************************************
1156  Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1157  (Should this be in locking.c.... ?).
1158 *************************************************************************/
1159
1160 static UNIX_USER_TOKEN *copy_unix_token(TALLOC_CTX *ctx, UNIX_USER_TOKEN *tok)
1161 {
1162         UNIX_USER_TOKEN *cpy;
1163
1164         if (tok == NULL) {
1165                 return NULL;
1166         }
1167
1168         cpy = TALLOC_P(ctx, UNIX_USER_TOKEN);
1169         if (!cpy) {
1170                 return NULL;
1171         }
1172
1173         cpy->uid = tok->uid;
1174         cpy->gid = tok->gid;
1175         cpy->ngroups = tok->ngroups;
1176         if (tok->ngroups) {
1177                 /* Make this a talloc child of cpy. */
1178                 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1179                 if (!cpy->groups) {
1180                         return NULL;
1181                 }
1182                 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1183         }
1184         return cpy;
1185 }
1186
1187 /****************************************************************************
1188  Replace the delete on close token.
1189 ****************************************************************************/
1190
1191 void set_delete_on_close_token(struct share_mode_lock *lck, UNIX_USER_TOKEN *tok)
1192 {
1193         /* Ensure there's no token. */
1194         if (lck->delete_token) {
1195                 TALLOC_FREE(lck->delete_token); /* Also deletes groups... */
1196                 lck->delete_token = NULL;
1197         }
1198
1199         /* Copy the new token (can be NULL). */
1200         lck->delete_token = copy_unix_token(lck, tok);
1201         lck->modified = True;
1202 }
1203
1204 /****************************************************************************
1205  Sets the delete on close flag over all share modes on this file.
1206  Modify the share mode entry for all files open
1207  on this device and inode to tell other smbds we have
1208  changed the delete on close flag. This will be noticed
1209  in the close code, the last closer will delete the file
1210  if flag is set.
1211  Note that setting this to any value clears the initial_delete_on_close flag.
1212  If delete_on_close is True this makes a copy of any UNIX_USER_TOKEN into the
1213  lck entry.
1214 ****************************************************************************/
1215
1216 BOOL set_delete_on_close(files_struct *fsp, BOOL delete_on_close, UNIX_USER_TOKEN *tok)
1217 {
1218         struct share_mode_lock *lck;
1219         
1220         DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1221                   "fnum = %d, file %s\n",
1222                   delete_on_close ? "Adding" : "Removing", fsp->fnum,
1223                   fsp->fsp_name ));
1224
1225         if (fsp->is_stat) {
1226                 return True;
1227         }
1228
1229         lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
1230         if (lck == NULL) {
1231                 return False;
1232         }
1233
1234         if (lck->delete_on_close != delete_on_close) {
1235                 set_delete_on_close_token(lck, tok);
1236                 lck->delete_on_close = delete_on_close;
1237                 if (delete_on_close) {
1238                         SMB_ASSERT(lck->delete_token != NULL);
1239                 }
1240                 lck->modified = True;
1241         }
1242
1243         if (lck->initial_delete_on_close) {
1244                 lck->initial_delete_on_close = False;
1245                 lck->modified = True;
1246         }
1247
1248         TALLOC_FREE(lck);
1249         return True;
1250 }
1251
1252 struct forall_state {
1253         void (*fn)(const struct share_mode_entry *entry,
1254                    const char *sharepath,
1255                    const char *fname,
1256                    void *private_data);
1257         void *private_data;
1258 };
1259
1260 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, 
1261                        void *_state)
1262 {
1263         struct forall_state *state = (struct forall_state *)_state;
1264         struct locking_data *data;
1265         struct share_mode_entry *shares;
1266         const char *sharepath;
1267         const char *fname;
1268         int i;
1269
1270         /* Ensure this is a locking_key record. */
1271         if (kbuf.dsize != sizeof(struct locking_key))
1272                 return 0;
1273
1274         data = (struct locking_data *)dbuf.dptr;
1275         shares = (struct share_mode_entry *)(dbuf.dptr + sizeof(*data));
1276         sharepath = dbuf.dptr + sizeof(*data) +
1277                 data->u.s.num_share_mode_entries*sizeof(*shares) +
1278                 data->u.s.delete_token_size;
1279         fname = dbuf.dptr + sizeof(*data) +
1280                 data->u.s.num_share_mode_entries*sizeof(*shares) +
1281                 data->u.s.delete_token_size +
1282                 strlen(sharepath) + 1;
1283
1284         for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1285                 state->fn(&shares[i], sharepath, fname,
1286                           state->private_data);
1287         }
1288         return 0;
1289 }
1290
1291 /*******************************************************************
1292  Call the specified function on each entry under management by the
1293  share mode system.
1294 ********************************************************************/
1295
1296 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1297                                  const char *, void *),
1298                       void *private_data)
1299 {
1300         struct forall_state state;
1301
1302         if (tdb == NULL)
1303                 return 0;
1304
1305         state.fn = fn;
1306         state.private_data = private_data;
1307
1308         return tdb_traverse(tdb, traverse_fn, (void *)&state);
1309 }