r13274: Fix for bug #3467. Not a show stopper.
[samba.git] / source3 / 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-2000
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 */
37
38 #include "includes.h"
39 uint16 global_smbpid;
40
41 #undef DBGC_CLASS
42 #define DBGC_CLASS DBGC_LOCKING
43
44 /* the locking database handle */
45 static TDB_CONTEXT *tdb;
46
47 struct locking_data {
48         union {
49                 struct {
50                         int num_share_mode_entries;
51                         BOOL delete_on_close;
52                         BOOL initial_delete_on_close; /* Only set at NTCreateX if file was created. */
53                 } s;
54                 struct share_mode_entry dummy; /* Needed for alignment. */
55         } u;
56         /* the following two entries are implicit
57            struct share_mode_entry modes[num_share_mode_entries];
58            char file_name[];
59         */
60 };
61
62 /****************************************************************************
63  Debugging aid :-).
64 ****************************************************************************/
65
66 static const char *lock_type_name(enum brl_type lock_type)
67 {
68         return (lock_type == READ_LOCK) ? "READ" : "WRITE";
69 }
70
71 /****************************************************************************
72  Utility function called to see if a file region is locked.
73 ****************************************************************************/
74
75 BOOL is_locked(files_struct *fsp,connection_struct *conn,
76                SMB_BIG_UINT count,SMB_BIG_UINT offset, 
77                enum brl_type lock_type)
78 {
79         int snum = SNUM(conn);
80         int strict_locking = lp_strict_locking(snum);
81         BOOL ret;
82         
83         if (count == 0)
84                 return(False);
85
86         if (!lp_locking(snum) || !strict_locking)
87                 return(False);
88
89         if (strict_locking == Auto) {
90                 if  (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (lock_type == READ_LOCK || lock_type == WRITE_LOCK)) {
91                         DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp->fsp_name ));
92                         ret = 0;
93                 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
94                            (lock_type == READ_LOCK)) {
95                         DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp->fsp_name ));
96                         ret = 0;
97                 } else {
98                         ret = !brl_locktest(fsp->dev, fsp->inode, fsp->fnum,
99                                      global_smbpid, procid_self(), conn->cnum, 
100                                      offset, count, lock_type);
101                 }
102         } else {
103                 ret = !brl_locktest(fsp->dev, fsp->inode, fsp->fnum,
104                                 global_smbpid, procid_self(), conn->cnum,
105                                 offset, count, lock_type);
106         }
107
108         DEBUG(10,("is_locked: brl start=%.0f len=%.0f %s for file %s\n",
109                         (double)offset, (double)count, ret ? "locked" : "unlocked",
110                         fsp->fsp_name ));
111
112         /*
113          * There is no lock held by an SMB daemon, check to
114          * see if there is a POSIX lock from a UNIX or NFS process.
115          */
116
117         if(!ret && lp_posix_locking(snum)) {
118                 ret = is_posix_locked(fsp, offset, count, lock_type);
119
120                 DEBUG(10,("is_locked: posix start=%.0f len=%.0f %s for file %s\n",
121                                 (double)offset, (double)count, ret ? "locked" : "unlocked",
122                                 fsp->fsp_name ));
123         }
124
125         return ret;
126 }
127
128 /****************************************************************************
129  Utility function called by locking requests.
130 ****************************************************************************/
131
132 static NTSTATUS do_lock(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
133                  SMB_BIG_UINT count,SMB_BIG_UINT offset,enum brl_type lock_type, BOOL *my_lock_ctx)
134 {
135         NTSTATUS status = NT_STATUS_LOCK_NOT_GRANTED;
136
137         if (!lp_locking(SNUM(conn)))
138                 return NT_STATUS_OK;
139
140         /* NOTE! 0 byte long ranges ARE allowed and should be stored  */
141
142         DEBUG(10,("do_lock: lock type %s start=%.0f len=%.0f requested for file %s\n",
143                   lock_type_name(lock_type), (double)offset, (double)count, fsp->fsp_name ));
144
145         if (OPEN_FSP(fsp) && fsp->can_lock && (fsp->conn == conn)) {
146                 status = brl_lock(fsp->dev, fsp->inode, fsp->fnum,
147                                   lock_pid, procid_self(), conn->cnum, 
148                                   offset, count, 
149                                   lock_type, my_lock_ctx);
150
151                 if (NT_STATUS_IS_OK(status) && lp_posix_locking(SNUM(conn))) {
152
153                         /*
154                          * Try and get a POSIX lock on this range.
155                          * Note that this is ok if it is a read lock
156                          * overlapping on a different fd. JRA.
157                          */
158
159                         if (!set_posix_lock(fsp, offset, count, lock_type)) {
160                                 if (errno == EACCES || errno == EAGAIN)
161                                         status = NT_STATUS_FILE_LOCK_CONFLICT;
162                                 else
163                                         status = map_nt_error_from_unix(errno);
164
165                                 /*
166                                  * We failed to map - we must now remove the brl
167                                  * lock entry.
168                                  */
169                                 (void)brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
170                                                                 lock_pid, procid_self(), conn->cnum, 
171                                                                 offset, count, False,
172                                                                 NULL, NULL);
173                         }
174                 }
175         }
176
177         return status;
178 }
179
180 /****************************************************************************
181  Utility function called by locking requests. This is *DISGUSTING*. It also
182  appears to be "What Windows Does" (tm). Andrew, ever wonder why Windows 2000
183  is so slow on the locking tests...... ? This is the reason. Much though I hate
184  it, we need this. JRA.
185 ****************************************************************************/
186
187 NTSTATUS do_lock_spin(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
188                  SMB_BIG_UINT count,SMB_BIG_UINT offset,enum brl_type lock_type, BOOL *my_lock_ctx)
189 {
190         int j, maxj = lp_lock_spin_count();
191         int sleeptime = lp_lock_sleep_time();
192         NTSTATUS status, ret;
193
194         if (maxj <= 0)
195                 maxj = 1;
196
197         ret = NT_STATUS_OK; /* to keep dumb compilers happy */
198
199         for (j = 0; j < maxj; j++) {
200                 status = do_lock(fsp, conn, lock_pid, count, offset, lock_type, my_lock_ctx);
201                 if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED) &&
202                     !NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
203                         return status;
204                 }
205                 /* if we do fail then return the first error code we got */
206                 if (j == 0) {
207                         ret = status;
208                         /* Don't spin if we blocked ourselves. */
209                         if (*my_lock_ctx)
210                                 return ret;
211                 }
212                 if (sleeptime)
213                         sys_usleep(sleeptime);
214         }
215         return ret;
216 }
217
218 /* Struct passed to brl_unlock. */
219 struct posix_unlock_data_struct {
220         files_struct *fsp;
221         SMB_BIG_UINT offset;
222         SMB_BIG_UINT count;
223 };
224
225 /****************************************************************************
226  Function passed to brl_unlock to allow POSIX unlock to be done first.
227 ****************************************************************************/
228
229 static void posix_unlock(void *pre_data)
230 {
231         struct posix_unlock_data_struct *pdata = (struct posix_unlock_data_struct *)pre_data;
232
233         if (lp_posix_locking(SNUM(pdata->fsp->conn)))
234                 release_posix_lock(pdata->fsp, pdata->offset, pdata->count);
235 }
236
237 /****************************************************************************
238  Utility function called by unlocking requests.
239 ****************************************************************************/
240
241 NTSTATUS do_unlock(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
242                    SMB_BIG_UINT count,SMB_BIG_UINT offset)
243 {
244         BOOL ok = False;
245         struct posix_unlock_data_struct posix_data;
246         
247         if (!lp_locking(SNUM(conn)))
248                 return NT_STATUS_OK;
249         
250         if (!OPEN_FSP(fsp) || !fsp->can_lock || (fsp->conn != conn)) {
251                 return NT_STATUS_INVALID_HANDLE;
252         }
253         
254         DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for file %s\n",
255                   (double)offset, (double)count, fsp->fsp_name ));
256
257         /*
258          * Remove the existing lock record from the tdb lockdb
259          * before looking at POSIX locks. If this record doesn't
260          * match then don't bother looking to remove POSIX locks.
261          */
262
263         posix_data.fsp = fsp;
264         posix_data.offset = offset;
265         posix_data.count = count;
266
267         ok = brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
268                         lock_pid, procid_self(), conn->cnum, offset, count,
269                         False, posix_unlock, (void *)&posix_data);
270    
271         if (!ok) {
272                 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
273                 return NT_STATUS_RANGE_NOT_LOCKED;
274         }
275         return NT_STATUS_OK;
276 }
277
278 /****************************************************************************
279  Remove any locks on this fd. Called from file_close().
280 ****************************************************************************/
281
282 void locking_close_file(files_struct *fsp)
283 {
284         struct process_id pid = procid_self();
285
286         if (!lp_locking(SNUM(fsp->conn)))
287                 return;
288
289         /*
290          * Just release all the brl locks, no need to release individually.
291          */
292
293         brl_close(fsp->dev, fsp->inode, pid, fsp->conn->cnum, fsp->fnum);
294
295         if(lp_posix_locking(SNUM(fsp->conn))) {
296
297                 /* 
298                  * Release all the POSIX locks.
299                  */
300                 posix_locking_close_file(fsp);
301
302         }
303 }
304
305 /****************************************************************************
306  Initialise the locking functions.
307 ****************************************************************************/
308
309 static int open_read_only;
310
311 BOOL locking_init(int read_only)
312 {
313         brl_init(read_only);
314
315         if (tdb)
316                 return True;
317
318         tdb = tdb_open_log(lock_path("locking.tdb"), 
319                         SMB_OPEN_DATABASE_TDB_HASH_SIZE, TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST), 
320                         read_only?O_RDONLY:O_RDWR|O_CREAT,
321                         0644);
322
323         if (!tdb) {
324                 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
325                 return False;
326         }
327
328         if (!posix_locking_init(read_only))
329                 return False;
330
331         open_read_only = read_only;
332
333         return True;
334 }
335
336 /*******************************************************************
337  Deinitialize the share_mode management.
338 ******************************************************************/
339
340 BOOL locking_end(void)
341 {
342         BOOL ret = True;
343
344         brl_shutdown(open_read_only);
345         if (tdb) {
346                 if (tdb_close(tdb) != 0)
347                         ret = False;
348         }
349
350         return ret;
351 }
352
353 /*******************************************************************
354  Form a static locking key for a dev/inode pair.
355 ******************************************************************/
356
357 /* key and data records in the tdb locking database */
358 struct locking_key {
359         SMB_DEV_T dev;
360         SMB_INO_T ino;
361 };
362
363 /*******************************************************************
364  Form a static locking key for a dev/inode pair.
365 ******************************************************************/
366
367 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
368 {
369         static struct locking_key key;
370         TDB_DATA kbuf;
371
372         memset(&key, '\0', sizeof(key));
373         key.dev = dev;
374         key.ino = inode;
375         kbuf.dptr = (char *)&key;
376         kbuf.dsize = sizeof(key);
377         return kbuf;
378 }
379
380 /*******************************************************************
381  Print out a share mode.
382 ********************************************************************/
383
384 char *share_mode_str(int num, struct share_mode_entry *e)
385 {
386         static pstring share_str;
387
388         slprintf(share_str, sizeof(share_str)-1, "share_mode_entry[%d]: %s "
389                  "pid = %s, share_access = 0x%x, private_options = 0x%x, "
390                  "access_mask = 0x%x, mid = 0x%x, type= 0x%x, file_id = %lu, "
391                  "dev = 0x%x, inode = %.0f",
392                  num,
393                  e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
394                  procid_str_static(&e->pid),
395                  e->share_access, e->private_options,
396                  e->access_mask, e->op_mid, e->op_type, e->share_file_id,
397                  (unsigned int)e->dev, (double)e->inode );
398
399         return share_str;
400 }
401
402 /*******************************************************************
403  Print out a share mode table.
404 ********************************************************************/
405
406 static void print_share_mode_table(struct locking_data *data)
407 {
408         int num_share_modes = data->u.s.num_share_mode_entries;
409         struct share_mode_entry *shares =
410                 (struct share_mode_entry *)(data + 1);
411         int i;
412
413         for (i = 0; i < num_share_modes; i++) {
414                 struct share_mode_entry entry;
415
416                 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
417                 DEBUG(10,("print_share_mode_table: %s\n",
418                           share_mode_str(i, &entry)));
419         }
420 }
421
422 /*******************************************************************
423  Get all share mode entries for a dev/inode pair.
424 ********************************************************************/
425
426 static BOOL parse_share_modes(TDB_DATA dbuf, struct share_mode_lock *lck)
427 {
428         struct locking_data *data;
429         int i;
430
431         if (dbuf.dsize < sizeof(struct locking_data)) {
432                 DEBUG(0, ("parse_share_modes: buffer too short\n"));
433                 return False;
434         }
435
436         data = (struct locking_data *)dbuf.dptr;
437
438         lck->delete_on_close = data->u.s.delete_on_close;
439         lck->initial_delete_on_close = data->u.s.initial_delete_on_close;
440         lck->num_share_modes = data->u.s.num_share_mode_entries;
441
442         DEBUG(10, ("parse_share_modes: delete_on_close: %d, "
443                    "initial_delete_on_close: %d, "
444                    "num_share_modes: %d\n",
445                 lck->delete_on_close,
446                 lck->initial_delete_on_close,
447                 lck->num_share_modes));
448
449         if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
450                 DEBUG(0, ("invalid number of share modes: %d\n",
451                           lck->num_share_modes));
452                 return False;
453         }
454
455         lck->share_modes = NULL;
456
457         if (lck->num_share_modes != 0) {
458
459                 if (dbuf.dsize < (sizeof(struct locking_data) +
460                                   (lck->num_share_modes *
461                                    sizeof(struct share_mode_entry)))) {
462                         DEBUG(0, ("parse_share_modes: buffer too short\n"));
463                         return False;
464                 }
465                                   
466                 lck->share_modes = talloc_memdup(lck, dbuf.dptr+sizeof(*data),
467                                                  lck->num_share_modes *
468                                                  sizeof(struct share_mode_entry));
469
470                 if (lck->share_modes == NULL) {
471                         DEBUG(0, ("talloc failed\n"));
472                         return False;
473                 }
474         }
475
476         /* Save off the associated service path and filename. */
477         lck->servicepath = talloc_strdup(lck, dbuf.dptr + sizeof(*data) +
478                                       (lck->num_share_modes *
479                                       sizeof(struct share_mode_entry)));
480
481         lck->filename = talloc_strdup(lck, dbuf.dptr + sizeof(*data) +
482                                       (lck->num_share_modes *
483                                       sizeof(struct share_mode_entry)) +
484                                       strlen(lck->servicepath) + 1 );
485
486         /*
487          * Ensure that each entry has a real process attached.
488          */
489
490         for (i = 0; i < lck->num_share_modes; i++) {
491                 struct share_mode_entry *entry_p = &lck->share_modes[i];
492                 DEBUG(10,("parse_share_modes: %s\n",
493                           share_mode_str(i, entry_p) ));
494                 if (!process_exists(entry_p->pid)) {
495                         DEBUG(10,("parse_share_modes: deleted %s\n",
496                                   share_mode_str(i, entry_p) ));
497                         entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
498                         lck->modified = True;
499                 }
500         }
501
502         return True;
503 }
504
505 static TDB_DATA unparse_share_modes(struct share_mode_lock *lck)
506 {
507         TDB_DATA result;
508         int num_valid = 0;
509         int i;
510         struct locking_data *data;
511         ssize_t offset;
512         ssize_t sp_len;
513
514         result.dptr = NULL;
515         result.dsize = 0;
516
517         for (i=0; i<lck->num_share_modes; i++) {
518                 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
519                         num_valid += 1;
520                 }
521         }
522
523         if (num_valid == 0) {
524                 return result;
525         }
526
527         sp_len = strlen(lck->servicepath);
528
529         result.dsize = sizeof(*data) +
530                 lck->num_share_modes * sizeof(struct share_mode_entry) +
531                 sp_len + 1 +
532                 strlen(lck->filename) + 1;
533         result.dptr = talloc_size(lck, result.dsize);
534
535         if (result.dptr == NULL) {
536                 smb_panic("talloc failed\n");
537         }
538
539         data = (struct locking_data *)result.dptr;
540         ZERO_STRUCTP(data);
541         data->u.s.num_share_mode_entries = lck->num_share_modes;
542         data->u.s.delete_on_close = lck->delete_on_close;
543         data->u.s.initial_delete_on_close = lck->initial_delete_on_close;
544         DEBUG(10, ("unparse_share_modes: del: %d, initial del %d, num: %d\n",
545                    data->u.s.delete_on_close,
546                    data->u.s.initial_delete_on_close,
547                    data->u.s.num_share_mode_entries));
548         memcpy(result.dptr + sizeof(*data), lck->share_modes,
549                sizeof(struct share_mode_entry)*lck->num_share_modes);
550         offset = sizeof(*data) +
551                 sizeof(struct share_mode_entry)*lck->num_share_modes;
552         safe_strcpy(result.dptr + offset, lck->servicepath,
553                     result.dsize - offset - 1);
554         offset += sp_len + 1;
555         safe_strcpy(result.dptr + offset, lck->filename,
556                     result.dsize - offset - 1);
557
558         if (DEBUGLEVEL >= 10) {
559                 print_share_mode_table(data);
560         }
561
562         return result;
563 }
564
565 static int share_mode_lock_destructor(void *p)
566 {
567         struct share_mode_lock *lck =
568                 talloc_get_type_abort(p, struct share_mode_lock);
569         TDB_DATA key = locking_key(lck->dev, lck->ino);
570         TDB_DATA data;
571
572         if (!lck->modified) {
573                 goto done;
574         }
575
576         data = unparse_share_modes(lck);
577
578         if (data.dptr == NULL) {
579                 if (!lck->fresh) {
580                         /* There has been an entry before, delete it */
581                         if (tdb_delete(tdb, key) == -1) {
582                                 smb_panic("Could not delete share entry\n");
583                         }
584                 }
585                 goto done;
586         }
587
588         if (tdb_store(tdb, key, data, TDB_REPLACE) == -1) {
589                 smb_panic("Could not store share mode entry\n");
590         }
591
592  done:
593         tdb_chainunlock(tdb, key);
594
595         return 0;
596 }
597
598 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
599                                                 SMB_DEV_T dev, SMB_INO_T ino,
600                                                 const char *servicepath,
601                                                 const char *fname)
602 {
603         struct share_mode_lock *lck;
604         TDB_DATA key = locking_key(dev, ino);
605         TDB_DATA data;
606
607         lck = TALLOC_P(mem_ctx, struct share_mode_lock);
608         if (lck == NULL) {
609                 DEBUG(0, ("talloc failed\n"));
610                 return NULL;
611         }
612
613         /* Ensure we set every field here as the destructor must be
614            valid even if parse_share_modes fails. */
615
616         lck->servicepath = NULL;
617         lck->filename = NULL;
618         lck->dev = dev;
619         lck->ino = ino;
620         lck->num_share_modes = 0;
621         lck->share_modes = NULL;
622         lck->delete_on_close = False;
623         lck->initial_delete_on_close = False;
624         lck->fresh = False;
625         lck->modified = False;
626
627         if (tdb_chainlock(tdb, key) != 0) {
628                 DEBUG(3, ("Could not lock share entry\n"));
629                 talloc_free(lck);
630                 return NULL;
631         }
632
633         /* We must set the destructor immediately after the chainlock
634            ensure the lock is cleaned up on any of the error return
635            paths below. */
636
637         talloc_set_destructor(lck, share_mode_lock_destructor);
638
639         data = tdb_fetch(tdb, key);
640         lck->fresh = (data.dptr == NULL);
641
642         if (lck->fresh) {
643
644                 if (fname == NULL || servicepath == NULL) {
645                         talloc_free(lck);
646                         return NULL;
647                 }
648                 lck->filename = talloc_strdup(lck, fname);
649                 lck->servicepath = talloc_strdup(lck, servicepath);
650                 if (lck->filename == NULL || lck->servicepath == NULL) {
651                         DEBUG(0, ("talloc failed\n"));
652                         talloc_free(lck);
653                         return NULL;
654                 }
655         } else {
656                 if (!parse_share_modes(data, lck)) {
657                         DEBUG(0, ("Could not parse share modes\n"));
658                         talloc_free(lck);
659                         SAFE_FREE(data.dptr);
660                         return NULL;
661                 }
662         }
663
664         SAFE_FREE(data.dptr);
665
666         return lck;
667 }
668
669 /*******************************************************************
670  Sets the service name and filename for rename.
671  At this point we emit "file renamed" messages to all
672  process id's that have this file open.
673  Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
674 ********************************************************************/
675
676 BOOL rename_share_filename(struct share_mode_lock *lck,
677                         const char *servicepath,
678                         const char *newname)
679 {
680         size_t sp_len;
681         size_t fn_len;
682         size_t msg_len;
683         char *frm = NULL;
684         int i;
685
686         if (!lck) {
687                 return False;
688         }
689
690         DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
691                 servicepath, newname));
692
693         /*
694          * rename_internal_fsp() and rename_internals() add './' to
695          * head of newname if newname does not contain a '/'.
696          */
697         while (newname[0] && newname[1] && newname[0] == '.' && newname[1] == '/') {
698                 newname += 2;
699         }
700
701         lck->servicepath = talloc_strdup(lck, servicepath);
702         lck->filename = talloc_strdup(lck, newname);
703         if (lck->filename == NULL || lck->servicepath == NULL) {
704                 DEBUG(0, ("rename_share_filename: talloc failed\n"));
705                 return False;
706         }
707         lck->modified = True;
708
709         sp_len = strlen(lck->servicepath);
710         fn_len = strlen(lck->filename);
711
712         msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + fn_len + 1;
713
714         /* Set up the name changed message. */
715         frm = TALLOC(lck, msg_len);
716         if (!frm) {
717                 return False;
718         }
719
720         SDEV_T_VAL(frm,0,lck->dev);
721         SINO_T_VAL(frm,8,lck->ino);
722
723         DEBUG(10,("rename_share_filename: msg_len = %d\n", msg_len ));
724
725         safe_strcpy(&frm[16], lck->servicepath, sp_len);
726         safe_strcpy(&frm[16 + sp_len + 1], lck->filename, fn_len);
727
728         /* Send the messages. */
729         for (i=0; i<lck->num_share_modes; i++) {
730                 struct share_mode_entry *se = &lck->share_modes[i];
731                 if (!is_valid_share_mode_entry(se)) {
732                         continue;
733                 }
734                 /* But not to ourselves... */
735                 if (procid_is_me(&se->pid)) {
736                         continue;
737                 }
738
739                 DEBUG(10,("rename_share_filename: sending rename message to pid %u "
740                         "dev %x, inode  %.0f sharepath %s newname %s\n",
741                         (unsigned int)procid_to_pid(&se->pid),
742                         (unsigned int)lck->dev, (double)lck->ino,
743                         lck->servicepath, lck->filename ));
744
745                 become_root();
746                 message_send_pid(se->pid, MSG_SMB_FILE_RENAME,
747                                 frm, msg_len, True);
748                 unbecome_root();
749         }
750
751         return True;
752 }
753
754 BOOL get_delete_on_close_flag(SMB_DEV_T dev, SMB_INO_T inode)
755 {
756         BOOL result;
757         struct share_mode_lock *lck = get_share_mode_lock(NULL, dev, inode, NULL, NULL);
758         if (!lck) {
759                 return False;
760         }
761         result = lck->delete_on_close;
762         talloc_free(lck);
763         return result;
764 }
765
766 BOOL is_valid_share_mode_entry(const struct share_mode_entry *e)
767 {
768         int num_props = 0;
769
770         num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
771         num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
772         num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
773
774         SMB_ASSERT(num_props <= 1);
775         return (num_props != 0);
776 }
777
778 BOOL is_deferred_open_entry(const struct share_mode_entry *e)
779 {
780         return (e->op_type == DEFERRED_OPEN_ENTRY);
781 }
782
783 BOOL is_unused_share_mode_entry(const struct share_mode_entry *e)
784 {
785         return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
786 }
787
788 /*******************************************************************
789  Fill a share mode entry.
790 ********************************************************************/
791
792 static void fill_share_mode_entry(struct share_mode_entry *e,
793                                   files_struct *fsp,
794                                   uint16 mid, uint16 op_type)
795 {
796         ZERO_STRUCTP(e);
797         e->pid = procid_self();
798         e->share_access = fsp->share_access;
799         e->private_options = fsp->fh->private_options;
800         e->access_mask = fsp->access_mask;
801         e->op_mid = mid;
802         e->op_type = op_type;
803         e->time.tv_sec = fsp->open_time.tv_sec;
804         e->time.tv_usec = fsp->open_time.tv_usec;
805         e->share_file_id = fsp->file_id;
806         e->dev = fsp->dev;
807         e->inode = fsp->inode;
808 }
809
810 static void fill_deferred_open_entry(struct share_mode_entry *e,
811                                      const struct timeval request_time,
812                                      SMB_DEV_T dev, SMB_INO_T ino, uint16 mid)
813 {
814         ZERO_STRUCTP(e);
815         e->pid = procid_self();
816         e->op_mid = mid;
817         e->op_type = DEFERRED_OPEN_ENTRY;
818         e->time.tv_sec = request_time.tv_sec;
819         e->time.tv_usec = request_time.tv_usec;
820         e->dev = dev;
821         e->inode = ino;
822 }
823
824 static void add_share_mode_entry(struct share_mode_lock *lck,
825                                  const struct share_mode_entry *entry)
826 {
827         int i;
828
829         for (i=0; i<lck->num_share_modes; i++) {
830                 struct share_mode_entry *e = &lck->share_modes[i];
831                 if (is_unused_share_mode_entry(e)) {
832                         *e = *entry;
833                         break;
834                 }
835         }
836
837         if (i == lck->num_share_modes) {
838                 /* No unused entry found */
839                 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
840                              &lck->share_modes, &lck->num_share_modes);
841         }
842         lck->modified = True;
843 }
844
845 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
846                     uint16 mid, uint16 op_type)
847 {
848         struct share_mode_entry entry;
849         fill_share_mode_entry(&entry, fsp, mid, op_type);
850         add_share_mode_entry(lck, &entry);
851 }
852
853 void add_deferred_open(struct share_mode_lock *lck, uint16 mid,
854                        struct timeval request_time,
855                        SMB_DEV_T dev, SMB_INO_T ino)
856 {
857         struct share_mode_entry entry;
858         fill_deferred_open_entry(&entry, request_time, dev, ino, mid);
859         add_share_mode_entry(lck, &entry);
860 }
861
862 /*******************************************************************
863  Check if two share mode entries are identical, ignoring oplock 
864  and mid info and desired_access.
865 ********************************************************************/
866
867 static BOOL share_modes_identical(struct share_mode_entry *e1,
868                                   struct share_mode_entry *e2)
869 {
870 #if 1 /* JRA PARANOIA TEST - REMOVE LATER */
871         if (procid_equal(&e1->pid, &e2->pid) &&
872             e1->share_file_id == e2->share_file_id &&
873             e1->dev == e2->dev &&
874             e1->inode == e2->inode &&
875             (e1->share_access) != (e2->share_access)) {
876                 DEBUG(0,("PANIC: share_modes_identical: share_mode "
877                          "mismatch (e1 = 0x%x, e2 = 0x%x). Logic error.\n",
878                          (unsigned int)e1->share_access,
879                          (unsigned int)e2->share_access ));
880                 smb_panic("PANIC: share_modes_identical logic error.\n");
881         }
882 #endif
883
884         return (procid_equal(&e1->pid, &e2->pid) &&
885                 (e1->share_access) == (e2->share_access) &&
886                 e1->dev == e2->dev &&
887                 e1->inode == e2->inode &&
888                 e1->share_file_id == e2->share_file_id );
889 }
890
891 static BOOL deferred_open_identical(struct share_mode_entry *e1,
892                                     struct share_mode_entry *e2)
893 {
894         return (procid_equal(&e1->pid, &e2->pid) &&
895                 (e1->op_mid == e2->op_mid) &&
896                 (e1->dev == e2->dev) &&
897                 (e1->inode == e2->inode));
898 }
899
900 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
901                                                       struct share_mode_entry *entry)
902 {
903         int i;
904
905         for (i=0; i<lck->num_share_modes; i++) {
906                 struct share_mode_entry *e = &lck->share_modes[i];
907                 if (is_valid_share_mode_entry(entry) &&
908                     is_valid_share_mode_entry(e) &&
909                     share_modes_identical(e, entry)) {
910                         return e;
911                 }
912                 if (is_deferred_open_entry(entry) &&
913                     is_deferred_open_entry(e) &&
914                     deferred_open_identical(e, entry)) {
915                         return e;
916                 }
917         }
918         return NULL;
919 }
920
921 /*******************************************************************
922  Del the share mode of a file for this process. Return the number of
923  entries left.
924 ********************************************************************/
925
926 BOOL del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
927 {
928         struct share_mode_entry entry, *e;
929
930         fill_share_mode_entry(&entry, fsp, 0, 0);
931
932         e = find_share_mode_entry(lck, &entry);
933         if (e == NULL) {
934                 return False;
935         }
936
937         e->op_type = UNUSED_SHARE_MODE_ENTRY;
938         lck->modified = True;
939         return True;
940 }
941
942 void del_deferred_open_entry(struct share_mode_lock *lck, uint16 mid)
943 {
944         struct share_mode_entry entry, *e;
945
946         fill_deferred_open_entry(&entry, timeval_zero(),
947                                  lck->dev, lck->ino, mid);
948
949         e = find_share_mode_entry(lck, &entry);
950         if (e == NULL) {
951                 return;
952         }
953
954         e->op_type = UNUSED_SHARE_MODE_ENTRY;
955         lck->modified = True;
956 }
957
958 /*******************************************************************
959  Remove an oplock mid and mode entry from a share mode.
960 ********************************************************************/
961
962 BOOL remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
963 {
964         struct share_mode_entry entry, *e;
965
966         fill_share_mode_entry(&entry, fsp, 0, 0);
967
968         e = find_share_mode_entry(lck, &entry);
969         if (e == NULL) {
970                 return False;
971         }
972
973         e->op_mid = 0;
974         e->op_type = NO_OPLOCK;
975         lck->modified = True;
976         return True;
977 }
978
979 /*******************************************************************
980  Downgrade a oplock type from exclusive to level II.
981 ********************************************************************/
982
983 BOOL downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
984 {
985         struct share_mode_entry entry, *e;
986
987         fill_share_mode_entry(&entry, fsp, 0, 0);
988
989         e = find_share_mode_entry(lck, &entry);
990         if (e == NULL) {
991                 return False;
992         }
993
994         e->op_type = LEVEL_II_OPLOCK;
995         lck->modified = True;
996         return True;
997 }
998
999 /****************************************************************************
1000  Deal with the internal needs of setting the delete on close flag. Note that
1001  as the tdb locking is recursive, it is safe to call this from within 
1002  open_file_shared. JRA.
1003 ****************************************************************************/
1004
1005 NTSTATUS can_set_delete_on_close(files_struct *fsp, BOOL delete_on_close,
1006                                  uint32 dosmode)
1007 {
1008         if (!delete_on_close) {
1009                 return NT_STATUS_OK;
1010         }
1011
1012         /*
1013          * Only allow delete on close for writable files.
1014          */
1015
1016         if ((dosmode & aRONLY) &&
1017             !lp_delete_readonly(SNUM(fsp->conn))) {
1018                 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1019                           "flag set but file attribute is readonly.\n",
1020                           fsp->fsp_name ));
1021                 return NT_STATUS_CANNOT_DELETE;
1022         }
1023
1024         /*
1025          * Only allow delete on close for writable shares.
1026          */
1027
1028         if (!CAN_WRITE(fsp->conn)) {
1029                 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1030                           "close flag set but write access denied on share.\n",
1031                           fsp->fsp_name ));
1032                 return NT_STATUS_ACCESS_DENIED;
1033         }
1034
1035         /*
1036          * Only allow delete on close for files/directories opened with delete
1037          * intent.
1038          */
1039
1040         if (!(fsp->access_mask & DELETE_ACCESS)) {
1041                 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1042                           "close flag set but delete access denied.\n",
1043                           fsp->fsp_name ));
1044                 return NT_STATUS_ACCESS_DENIED;
1045         }
1046
1047         return NT_STATUS_OK;
1048 }
1049
1050 /****************************************************************************
1051  Sets the delete on close flag over all share modes on this file.
1052  Modify the share mode entry for all files open
1053  on this device and inode to tell other smbds we have
1054  changed the delete on close flag. This will be noticed
1055  in the close code, the last closer will delete the file
1056  if flag is set.
1057  Note that setting this to any value clears the initial_delete_on_close flag.
1058 ****************************************************************************/
1059
1060 BOOL set_delete_on_close(files_struct *fsp, BOOL delete_on_close)
1061 {
1062         struct share_mode_lock *lck;
1063         
1064         DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1065                   "fnum = %d, file %s\n",
1066                   delete_on_close ? "Adding" : "Removing", fsp->fnum,
1067                   fsp->fsp_name ));
1068
1069         if (fsp->is_stat) {
1070                 return True;
1071         }
1072
1073         lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
1074         if (lck == NULL) {
1075                 return False;
1076         }
1077         if (lck->delete_on_close != delete_on_close) {
1078                 lck->delete_on_close = delete_on_close;
1079                 lck->modified = True;
1080         }
1081
1082         if (lck->initial_delete_on_close) {
1083                 lck->initial_delete_on_close = False;
1084                 lck->modified = True;
1085         }
1086
1087         talloc_free(lck);
1088         return True;
1089 }
1090
1091 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, 
1092                        void *state)
1093 {
1094         struct locking_data *data;
1095         struct share_mode_entry *shares;
1096         const char *sharepath;
1097         const char *fname;
1098         int i;
1099         void (*traverse_callback)(struct share_mode_entry *, const char *, const char *) = state;
1100
1101         /* Ensure this is a locking_key record. */
1102         if (kbuf.dsize != sizeof(struct locking_key))
1103                 return 0;
1104
1105         data = (struct locking_data *)dbuf.dptr;
1106         shares = (struct share_mode_entry *)(dbuf.dptr + sizeof(*data));
1107         sharepath = dbuf.dptr + sizeof(*data) +
1108                 data->u.s.num_share_mode_entries*sizeof(*shares);
1109         fname = dbuf.dptr + sizeof(*data) +
1110                 data->u.s.num_share_mode_entries*sizeof(*shares) +
1111                 strlen(sharepath) + 1;
1112
1113         for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1114                 traverse_callback(&shares[i], sharepath, fname);
1115         }
1116         return 0;
1117 }
1118
1119 /*******************************************************************
1120  Call the specified function on each entry under management by the
1121  share mode system.
1122 ********************************************************************/
1123
1124 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *, const char *))
1125 {
1126         if (tdb == NULL)
1127                 return 0;
1128         return tdb_traverse(tdb, traverse_fn, fn);
1129 }