sync 3.0 into HEAD for the last time
[metze/samba/wip.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    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21    Revision History:
22
23    12 aug 96: Erik.Devriendt@te6.siemens.be
24    added support for shared memory implementation of share mode locking
25
26    May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
27    locking to deal with multiple share modes per open file.
28
29    September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
30    support.
31
32    rewrtten completely to use new tdb code. Tridge, Dec '99
33
34    Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
35 */
36
37 #include "includes.h"
38 uint16 global_smbpid;
39
40 /* the locking database handle */
41 static TDB_CONTEXT *tdb;
42
43 /****************************************************************************
44  Debugging aid :-).
45 ****************************************************************************/
46
47 static const char *lock_type_name(enum brl_type lock_type)
48 {
49         return (lock_type == READ_LOCK) ? "READ" : "WRITE";
50 }
51
52 /****************************************************************************
53  Utility function called to see if a file region is locked.
54  If check_self is True, then checks on our own fd with the same locking context
55  are still made. If check_self is False, then checks are not made on our own fd
56  with the same locking context are not made.
57 ****************************************************************************/
58
59 BOOL is_locked(files_struct *fsp,connection_struct *conn,
60                SMB_BIG_UINT count,SMB_BIG_UINT offset, 
61                enum brl_type lock_type, BOOL check_self)
62 {
63         int snum = SNUM(conn);
64         BOOL ret;
65         
66         if (count == 0)
67                 return(False);
68
69         if (!lp_locking(snum) || !lp_strict_locking(snum))
70                 return(False);
71
72         ret = !brl_locktest(fsp->dev, fsp->inode, fsp->fnum,
73                              global_smbpid, sys_getpid(), conn->cnum, 
74                              offset, count, lock_type, check_self);
75
76         DEBUG(10,("is_locked: brl start=%.0f len=%.0f %s for file %s\n",
77                         (double)offset, (double)count, ret ? "locked" : "unlocked",
78                         fsp->fsp_name ));
79
80         /*
81          * There is no lock held by an SMB daemon, check to
82          * see if there is a POSIX lock from a UNIX or NFS process.
83          */
84
85         if(!ret && lp_posix_locking(snum)) {
86                 ret = is_posix_locked(fsp, offset, count, lock_type);
87
88                 DEBUG(10,("is_locked: posix start=%.0f len=%.0f %s for file %s\n",
89                                 (double)offset, (double)count, ret ? "locked" : "unlocked",
90                                 fsp->fsp_name ));
91         }
92
93         return ret;
94 }
95
96 /****************************************************************************
97  Utility function called by locking requests.
98 ****************************************************************************/
99
100 static NTSTATUS do_lock(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
101                  SMB_BIG_UINT count,SMB_BIG_UINT offset,enum brl_type lock_type)
102 {
103         NTSTATUS status = NT_STATUS_LOCK_NOT_GRANTED;
104
105         if (!lp_locking(SNUM(conn)))
106                 return NT_STATUS_OK;
107
108         /* NOTE! 0 byte long ranges ARE allowed and should be stored  */
109
110         DEBUG(10,("do_lock: lock type %s start=%.0f len=%.0f requested for file %s\n",
111                   lock_type_name(lock_type), (double)offset, (double)count, fsp->fsp_name ));
112
113         if (OPEN_FSP(fsp) && fsp->can_lock && (fsp->conn == conn)) {
114                 status = brl_lock(fsp->dev, fsp->inode, fsp->fnum,
115                                   lock_pid, sys_getpid(), conn->cnum, 
116                                   offset, count, 
117                                   lock_type);
118
119                 if (NT_STATUS_IS_OK(status) && lp_posix_locking(SNUM(conn))) {
120
121                         /*
122                          * Try and get a POSIX lock on this range.
123                          * Note that this is ok if it is a read lock
124                          * overlapping on a different fd. JRA.
125                          */
126
127                         if (!set_posix_lock(fsp, offset, count, lock_type)) {
128                                 if (errno == EACCES || errno == EAGAIN)
129                                         status = NT_STATUS_FILE_LOCK_CONFLICT;
130                                 else
131                                         status = map_nt_error_from_unix(errno);
132
133                                 /*
134                                  * We failed to map - we must now remove the brl
135                                  * lock entry.
136                                  */
137                                 (void)brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
138                                                                 lock_pid, sys_getpid(), conn->cnum, 
139                                                                 offset, count, False,
140                                                                 NULL, NULL);
141                         }
142                 }
143         }
144
145         return status;
146 }
147
148 /****************************************************************************
149  Utility function called by locking requests. This is *DISGUSTING*. It also
150  appears to be "What Windows Does" (tm). Andrew, ever wonder why Windows 2000
151  is so slow on the locking tests...... ? This is the reason. Much though I hate
152  it, we need this. JRA.
153 ****************************************************************************/
154
155 NTSTATUS do_lock_spin(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
156                  SMB_BIG_UINT count,SMB_BIG_UINT offset,enum brl_type lock_type)
157 {
158         int j, maxj = lp_lock_spin_count();
159         int sleeptime = lp_lock_sleep_time();
160         NTSTATUS status, ret;
161
162         if (maxj <= 0)
163                 maxj = 1;
164
165         ret = NT_STATUS_OK; /* to keep dumb compilers happy */
166
167         for (j = 0; j < maxj; j++) {
168                 status = do_lock(fsp, conn, lock_pid, count, offset, lock_type);
169                 if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED) &&
170                     !NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
171                         return status;
172                 }
173                 /* if we do fail then return the first error code we got */
174                 if (j == 0) {
175                         ret = status;
176                 }
177                 if (sleeptime)
178                         sys_usleep(sleeptime);
179         }
180         return ret;
181 }
182
183 /* Struct passed to brl_unlock. */
184 struct posix_unlock_data_struct {
185         files_struct *fsp;
186         SMB_BIG_UINT offset;
187         SMB_BIG_UINT count;
188 };
189
190 /****************************************************************************
191  Function passed to brl_unlock to allow POSIX unlock to be done first.
192 ****************************************************************************/
193
194 static void posix_unlock(void *pre_data)
195 {
196         struct posix_unlock_data_struct *pdata = (struct posix_unlock_data_struct *)pre_data;
197
198         if (lp_posix_locking(SNUM(pdata->fsp->conn)))
199                 release_posix_lock(pdata->fsp, pdata->offset, pdata->count);
200 }
201
202 /****************************************************************************
203  Utility function called by unlocking requests.
204 ****************************************************************************/
205
206 NTSTATUS do_unlock(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
207                    SMB_BIG_UINT count,SMB_BIG_UINT offset)
208 {
209         BOOL ok = False;
210         struct posix_unlock_data_struct posix_data;
211         
212         if (!lp_locking(SNUM(conn)))
213                 return NT_STATUS_OK;
214         
215         if (!OPEN_FSP(fsp) || !fsp->can_lock || (fsp->conn != conn)) {
216                 return NT_STATUS_INVALID_HANDLE;
217         }
218         
219         DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for file %s\n",
220                   (double)offset, (double)count, fsp->fsp_name ));
221
222         /*
223          * Remove the existing lock record from the tdb lockdb
224          * before looking at POSIX locks. If this record doesn't
225          * match then don't bother looking to remove POSIX locks.
226          */
227
228         posix_data.fsp = fsp;
229         posix_data.offset = offset;
230         posix_data.count = count;
231
232         ok = brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
233                         lock_pid, sys_getpid(), conn->cnum, offset, count,
234                         False, posix_unlock, (void *)&posix_data);
235    
236         if (!ok) {
237                 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
238                 return NT_STATUS_RANGE_NOT_LOCKED;
239         }
240         return NT_STATUS_OK;
241 }
242
243 /****************************************************************************
244  Remove any locks on this fd. Called from file_close().
245 ****************************************************************************/
246
247 void locking_close_file(files_struct *fsp)
248 {
249         pid_t pid = sys_getpid();
250
251         if (!lp_locking(SNUM(fsp->conn)))
252                 return;
253
254         /*
255          * Just release all the brl locks, no need to release individually.
256          */
257
258         brl_close(fsp->dev, fsp->inode, pid, fsp->conn->cnum, fsp->fnum);
259
260         if(lp_posix_locking(SNUM(fsp->conn))) {
261
262                 /* 
263                  * Release all the POSIX locks.
264                  */
265                 posix_locking_close_file(fsp);
266
267         }
268 }
269
270 /****************************************************************************
271  Initialise the locking functions.
272 ****************************************************************************/
273
274 static int open_read_only;
275
276 BOOL locking_init(int read_only)
277 {
278         brl_init(read_only);
279
280         if (tdb)
281                 return True;
282
283         tdb = tdb_open_log(lock_path("locking.tdb"), 
284                        0, TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST), 
285                        read_only?O_RDONLY:O_RDWR|O_CREAT,
286                        0644);
287
288         if (!tdb) {
289                 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
290                 return False;
291         }
292         
293         if (!posix_locking_init(read_only))
294                 return False;
295
296         open_read_only = read_only;
297
298         return True;
299 }
300
301 /*******************************************************************
302  Deinitialize the share_mode management.
303 ******************************************************************/
304
305 BOOL locking_end(void)
306 {
307
308         brl_shutdown(open_read_only);
309         if (tdb) {
310
311                 if (tdb_close(tdb) != 0)
312                         return False;
313         }
314
315         return True;
316 }
317
318 /*******************************************************************
319  Form a static locking key for a dev/inode pair.
320 ******************************************************************/
321
322 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
323 {
324         static struct locking_key key;
325         TDB_DATA kbuf;
326
327         memset(&key, '\0', sizeof(key));
328         key.dev = dev;
329         key.inode = inode;
330         kbuf.dptr = (char *)&key;
331         kbuf.dsize = sizeof(key);
332         return kbuf;
333 }
334
335 static TDB_DATA locking_key_fsp(files_struct *fsp)
336 {
337         return locking_key(fsp->dev, fsp->inode);
338 }
339
340 /*******************************************************************
341  Lock a hash bucket entry.
342 ******************************************************************/
343
344 BOOL lock_share_entry(connection_struct *conn,
345                       SMB_DEV_T dev, SMB_INO_T inode)
346 {
347         return tdb_chainlock(tdb, locking_key(dev, inode)) == 0;
348 }
349
350 /*******************************************************************
351  Unlock a hash bucket entry.
352 ******************************************************************/
353
354 void unlock_share_entry(connection_struct *conn,
355                         SMB_DEV_T dev, SMB_INO_T inode)
356 {
357         tdb_chainunlock(tdb, locking_key(dev, inode));
358 }
359
360 /*******************************************************************
361  Lock a hash bucket entry. use a fsp for convenience
362 ******************************************************************/
363
364 BOOL lock_share_entry_fsp(files_struct *fsp)
365 {
366         return tdb_chainlock(tdb, locking_key(fsp->dev, fsp->inode)) == 0;
367 }
368
369 /*******************************************************************
370  Unlock a hash bucket entry.
371 ******************************************************************/
372
373 void unlock_share_entry_fsp(files_struct *fsp)
374 {
375         tdb_chainunlock(tdb, locking_key(fsp->dev, fsp->inode));
376 }
377
378 /*******************************************************************
379  Print out a share mode.
380 ********************************************************************/
381
382 char *share_mode_str(int num, share_mode_entry *e)
383 {
384         static pstring share_str;
385
386         slprintf(share_str, sizeof(share_str)-1, "share_mode_entry[%d]: \
387 pid = %lu, share_mode = 0x%x, desired_access = 0x%x, port = 0x%x, type= 0x%x, file_id = %lu, dev = 0x%x, inode = %.0f",
388         num, (unsigned long)e->pid, e->share_mode, (unsigned int)e->desired_access, e->op_port, e->op_type, e->share_file_id,
389         (unsigned int)e->dev, (double)e->inode );
390
391         return share_str;
392 }
393
394 /*******************************************************************
395  Print out a share mode table.
396 ********************************************************************/
397
398 static void print_share_mode_table(struct locking_data *data)
399 {
400         int num_share_modes = data->u.num_share_mode_entries;
401         share_mode_entry *shares = (share_mode_entry *)(data + 1);
402         int i;
403
404         for (i = 0; i < num_share_modes; i++) {
405                 share_mode_entry *entry_p = &shares[i];
406                 DEBUG(10,("print_share_mode_table: %s\n", share_mode_str(i, entry_p) ));
407         }
408 }
409
410 /*******************************************************************
411  Get all share mode entries for a dev/inode pair.
412 ********************************************************************/
413
414 int get_share_modes(connection_struct *conn, 
415                     SMB_DEV_T dev, SMB_INO_T inode, 
416                     share_mode_entry **pp_shares)
417 {
418         TDB_DATA dbuf;
419         struct locking_data *data;
420         int num_share_modes;
421         share_mode_entry *shares = NULL;
422         TDB_DATA key = locking_key(dev, inode);
423         *pp_shares = NULL;
424
425         dbuf = tdb_fetch(tdb, key);
426         if (!dbuf.dptr)
427                 return 0;
428
429         data = (struct locking_data *)dbuf.dptr;
430         num_share_modes = data->u.num_share_mode_entries;
431         if(num_share_modes) {
432                 int i;
433                 int del_count = 0;
434
435                 shares = (share_mode_entry *)memdup(dbuf.dptr + sizeof(*data),  
436                                                 num_share_modes * sizeof(share_mode_entry));
437
438                 if (!shares) {
439                         SAFE_FREE(dbuf.dptr);
440                         return 0;
441                 }
442
443                 /*
444                  * Ensure that each entry has a real process attached.
445                  */
446
447                 for (i = 0; i < num_share_modes; ) {
448                         share_mode_entry *entry_p = &shares[i];
449                         if (process_exists(entry_p->pid)) {
450                                 DEBUG(10,("get_share_modes: %s\n", share_mode_str(i, entry_p) ));
451                                 i++;
452                         } else {
453                                 DEBUG(10,("get_share_modes: deleted %s\n", share_mode_str(i, entry_p) ));
454                                 memcpy( &shares[i], &shares[i+1],
455                                         sizeof(share_mode_entry) * (num_share_modes - i - 1));
456                                 num_share_modes--;
457                                 del_count++;
458                         }
459                 }
460
461                 /* Did we delete any ? If so, re-store in tdb. */
462                 if (del_count) {
463                         data->u.num_share_mode_entries = num_share_modes;
464                         
465                         if (num_share_modes)
466                                 memcpy(dbuf.dptr + sizeof(*data), shares,
467                                                 num_share_modes * sizeof(share_mode_entry));
468
469                         /* The record has shrunk a bit */
470                         dbuf.dsize -= del_count * sizeof(share_mode_entry);
471
472                         if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
473                                 SAFE_FREE(shares);
474                                 SAFE_FREE(dbuf.dptr);
475                                 return 0;
476                         }
477                 }
478         }
479
480         SAFE_FREE(dbuf.dptr);
481         *pp_shares = shares;
482         return num_share_modes;
483 }
484
485 /*******************************************************************
486  Fill a share mode entry.
487 ********************************************************************/
488
489 static void fill_share_mode(char *p, files_struct *fsp, uint16 port, uint16 op_type)
490 {
491         share_mode_entry *e = (share_mode_entry *)p;
492         void *x = &e->time; /* Needed to force alignment. p may not be aligned.... */
493
494         memset(e, '\0', sizeof(share_mode_entry));
495         e->pid = sys_getpid();
496         e->share_mode = fsp->share_mode;
497         e->desired_access = fsp->desired_access;
498         e->op_port = port;
499         e->op_type = op_type;
500         memcpy(x, &fsp->open_time, sizeof(struct timeval));
501         e->share_file_id = fsp->file_id;
502         e->dev = fsp->dev;
503         e->inode = fsp->inode;
504 }
505
506 /*******************************************************************
507  Check if two share mode entries are identical, ignoring oplock 
508  and port info and desired_access.
509 ********************************************************************/
510
511 BOOL share_modes_identical( share_mode_entry *e1, share_mode_entry *e2)
512 {
513 #if 1 /* JRA PARANOIA TEST - REMOVE LATER */
514         if (e1->pid == e2->pid &&
515                 e1->share_file_id == e2->share_file_id &&
516                 e1->dev == e2->dev &&
517                 e1->inode == e2->inode &&
518                 (e1->share_mode & ~DELETE_ON_CLOSE_FLAG) != (e2->share_mode & ~DELETE_ON_CLOSE_FLAG)) {
519                         DEBUG(0,("PANIC: share_modes_identical: share_mode missmatch (e1 = %u, e2 = %u). Logic error.\n",
520                                 (unsigned int)(e1->share_mode & ~DELETE_ON_CLOSE_FLAG),
521                                 (unsigned int)(e2->share_mode & ~DELETE_ON_CLOSE_FLAG) ));
522                 smb_panic("PANIC: share_modes_identical logic error.\n");
523         }
524 #endif
525
526         return (e1->pid == e2->pid &&
527                 (e1->share_mode & ~DELETE_ON_CLOSE_FLAG) == (e2->share_mode & ~DELETE_ON_CLOSE_FLAG) &&
528                 e1->dev == e2->dev &&
529                 e1->inode == e2->inode &&
530                 e1->share_file_id == e2->share_file_id );
531 }
532
533 /*******************************************************************
534  Delete a specific share mode. Return the number
535  of entries left, and a memdup'ed copy of the entry deleted (if required).
536  Ignore if no entry deleted.
537 ********************************************************************/
538
539 ssize_t del_share_entry( SMB_DEV_T dev, SMB_INO_T inode,
540                         share_mode_entry *entry, share_mode_entry **ppse)
541 {
542         TDB_DATA dbuf;
543         struct locking_data *data;
544         int i, del_count=0;
545         share_mode_entry *shares;
546         ssize_t count = 0;
547         TDB_DATA key = locking_key(dev, inode);
548
549         if (ppse)
550                 *ppse = NULL;
551
552         /* read in the existing share modes */
553         dbuf = tdb_fetch(tdb, key);
554         if (!dbuf.dptr)
555                 return -1;
556
557         data = (struct locking_data *)dbuf.dptr;
558         shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
559
560         /*
561          * Find any with this pid and delete it
562          * by overwriting with the rest of the data 
563          * from the record.
564          */
565
566         DEBUG(10,("del_share_entry: num_share_modes = %d\n", data->u.num_share_mode_entries ));
567
568         for (i=0;i<data->u.num_share_mode_entries;) {
569                 if (share_modes_identical(&shares[i], entry)) {
570                         DEBUG(10,("del_share_entry: deleted %s\n",
571                                 share_mode_str(i, &shares[i]) ));
572                         if (ppse)
573                                 *ppse = memdup(&shares[i], sizeof(*shares));
574                         data->u.num_share_mode_entries--;
575                         memmove(&shares[i], &shares[i+1], 
576                                 dbuf.dsize - (sizeof(*data) + (i+1)*sizeof(*shares)));
577                         del_count++;
578
579                         DEBUG(10,("del_share_entry: deleting entry %d\n", i ));
580
581                 } else {
582                         i++;
583                 }
584         }
585
586         if (del_count) {
587                 /* the record may have shrunk a bit */
588                 dbuf.dsize -= del_count * sizeof(*shares);
589
590                 count = (ssize_t)data->u.num_share_mode_entries;
591
592                 /* store it back in the database */
593                 if (data->u.num_share_mode_entries == 0) {
594                         if (tdb_delete(tdb, key) == -1)
595                                 count = -1;
596                 } else {
597                         if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
598                                 count = -1;
599                 }
600         }
601         DEBUG(10,("del_share_entry: Remaining table.\n"));
602         print_share_mode_table((struct locking_data *)dbuf.dptr);
603         SAFE_FREE(dbuf.dptr);
604         return count;
605 }
606
607 /*******************************************************************
608  Del the share mode of a file for this process. Return the number
609  of entries left, and a memdup'ed copy of the entry deleted.
610 ********************************************************************/
611
612 ssize_t del_share_mode(files_struct *fsp, share_mode_entry **ppse)
613 {
614         share_mode_entry entry;
615
616         /*
617          * Fake up a share_mode_entry for comparisons.
618          */
619
620         fill_share_mode((char *)&entry, fsp, 0, 0);
621         return del_share_entry(fsp->dev, fsp->inode, &entry, ppse);
622 }
623
624 /*******************************************************************
625  Set the share mode of a file. Return False on fail, True on success.
626 ********************************************************************/
627
628 BOOL set_share_mode(files_struct *fsp, uint16 port, uint16 op_type)
629 {
630         TDB_DATA dbuf;
631         struct locking_data *data;
632         char *p=NULL;
633         int size;
634         TDB_DATA key = locking_key_fsp(fsp);
635         BOOL ret = True;
636                 
637         /* read in the existing share modes if any */
638         dbuf = tdb_fetch(tdb, key);
639         if (!dbuf.dptr) {
640                 size_t offset;
641                 /* we'll need to create a new record */
642                 pstring fname;
643
644                 pstrcpy(fname, fsp->conn->connectpath);
645                 pstrcat(fname, "/");
646                 pstrcat(fname, fsp->fsp_name);
647
648                 size = sizeof(*data) + sizeof(share_mode_entry) + strlen(fname) + 1;
649                 p = (char *)malloc(size);
650                 if (!p)
651                         return False;
652                 data = (struct locking_data *)p;
653                 data->u.num_share_mode_entries = 1;
654         
655                 DEBUG(10,("set_share_mode: creating entry for file %s. num_share_modes = 1\n",
656                         fsp->fsp_name ));
657
658                 offset = sizeof(*data) + sizeof(share_mode_entry);
659                 safe_strcpy(p + offset, fname, size - offset - 1);
660                 fill_share_mode(p + sizeof(*data), fsp, port, op_type);
661                 dbuf.dptr = p;
662                 dbuf.dsize = size;
663                 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
664                         ret = False;
665
666                 print_share_mode_table((struct locking_data *)p);
667
668                 SAFE_FREE(p);
669                 return ret;
670         }
671
672         /* we're adding to an existing entry - this is a bit fiddly */
673         data = (struct locking_data *)dbuf.dptr;
674
675         data->u.num_share_mode_entries++;
676         
677         DEBUG(10,("set_share_mode: adding entry for file %s. new num_share_modes = %d\n",
678                 fsp->fsp_name, data->u.num_share_mode_entries ));
679
680         size = dbuf.dsize + sizeof(share_mode_entry);
681         p = malloc(size);
682         if (!p) {
683                 SAFE_FREE(dbuf.dptr);
684                 return False;
685         }
686         memcpy(p, dbuf.dptr, sizeof(*data));
687         fill_share_mode(p + sizeof(*data), fsp, port, op_type);
688         memcpy(p + sizeof(*data) + sizeof(share_mode_entry), dbuf.dptr + sizeof(*data),
689                dbuf.dsize - sizeof(*data));
690         SAFE_FREE(dbuf.dptr);
691         dbuf.dptr = p;
692         dbuf.dsize = size;
693         if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
694                 ret = False;
695         print_share_mode_table((struct locking_data *)p);
696         SAFE_FREE(p);
697         return ret;
698 }
699
700 /*******************************************************************
701  A generic in-place modification call for share mode entries.
702 ********************************************************************/
703
704 static BOOL mod_share_mode( SMB_DEV_T dev, SMB_INO_T inode, share_mode_entry *entry,
705                            void (*mod_fn)(share_mode_entry *, SMB_DEV_T, SMB_INO_T, void *),
706                            void *param)
707 {
708         TDB_DATA dbuf;
709         struct locking_data *data;
710         int i;
711         share_mode_entry *shares;
712         BOOL need_store=False;
713         BOOL ret = True;
714         TDB_DATA key = locking_key(dev, inode);
715
716         /* read in the existing share modes */
717         dbuf = tdb_fetch(tdb, key);
718         if (!dbuf.dptr)
719                 return False;
720
721         data = (struct locking_data *)dbuf.dptr;
722         shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
723
724         /* find any with our pid and call the supplied function */
725         for (i=0;i<data->u.num_share_mode_entries;i++) {
726                 if (share_modes_identical(entry, &shares[i])) {
727                         mod_fn(&shares[i], dev, inode, param);
728                         need_store=True;
729                 }
730         }
731
732         /* if the mod fn was called then store it back */
733         if (need_store) {
734                 if (data->u.num_share_mode_entries == 0) {
735                         if (tdb_delete(tdb, key) == -1)
736                                 ret = False;
737                 } else {
738                         if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1)
739                                 ret = False;
740                 }
741         }
742
743         SAFE_FREE(dbuf.dptr);
744         return ret;
745 }
746
747 /*******************************************************************
748  Static function that actually does the work for the generic function
749  below.
750 ********************************************************************/
751
752 static void remove_share_oplock_fn(share_mode_entry *entry, SMB_DEV_T dev, SMB_INO_T inode, 
753                                    void *param)
754 {
755         DEBUG(10,("remove_share_oplock_fn: removing oplock info for entry dev=%x ino=%.0f\n",
756                   (unsigned int)dev, (double)inode ));
757         /* Delete the oplock info. */
758         entry->op_port = 0;
759         entry->op_type = NO_OPLOCK;
760 }
761
762 /*******************************************************************
763  Remove an oplock port and mode entry from a share mode.
764 ********************************************************************/
765
766 BOOL remove_share_oplock(files_struct *fsp)
767 {
768         share_mode_entry entry;
769         /*
770          * Fake up an entry for comparisons...
771          */
772         fill_share_mode((char *)&entry, fsp, 0, 0);
773         return mod_share_mode(fsp->dev, fsp->inode, &entry, remove_share_oplock_fn, NULL);
774 }
775
776 /*******************************************************************
777  Static function that actually does the work for the generic function
778  below.
779 ********************************************************************/
780
781 static void downgrade_share_oplock_fn(share_mode_entry *entry, SMB_DEV_T dev, SMB_INO_T inode, 
782                                    void *param)
783 {
784         DEBUG(10,("downgrade_share_oplock_fn: downgrading oplock info for entry dev=%x ino=%.0f\n",
785                   (unsigned int)dev, (double)inode ));
786         entry->op_type = LEVEL_II_OPLOCK;
787 }
788
789 /*******************************************************************
790  Downgrade a oplock type from exclusive to level II.
791 ********************************************************************/
792
793 BOOL downgrade_share_oplock(files_struct *fsp)
794 {
795         share_mode_entry entry;
796         /*
797          * Fake up an entry for comparisons...
798          */
799         fill_share_mode((char *)&entry, fsp, 0, 0);
800         return mod_share_mode(fsp->dev, fsp->inode, &entry, downgrade_share_oplock_fn, NULL);
801 }
802
803 /*******************************************************************
804  Get/Set the delete on close flag in a set of share modes.
805  Return False on fail, True on success.
806 ********************************************************************/
807
808 BOOL modify_delete_flag( SMB_DEV_T dev, SMB_INO_T inode, BOOL delete_on_close)
809 {
810         TDB_DATA dbuf;
811         struct locking_data *data;
812         int i;
813         share_mode_entry *shares;
814         TDB_DATA key = locking_key(dev, inode);
815
816         /* read in the existing share modes */
817         dbuf = tdb_fetch(tdb, key);
818         if (!dbuf.dptr)
819                 return False;
820
821         data = (struct locking_data *)dbuf.dptr;
822         shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
823
824         /* Set/Unset the delete on close element. */
825         for (i=0;i<data->u.num_share_mode_entries;i++,shares++) {
826                 shares->share_mode = (delete_on_close ?
827                             (shares->share_mode | DELETE_ON_CLOSE_FLAG) :
828                             (shares->share_mode & ~DELETE_ON_CLOSE_FLAG) );
829         }
830
831         /* store it back */
832         if (data->u.num_share_mode_entries) {
833                 if (tdb_store(tdb, key, dbuf, TDB_REPLACE)==-1) {
834                         SAFE_FREE(dbuf.dptr);
835                         return False;
836                 }
837         }
838
839         SAFE_FREE(dbuf.dptr);
840         return True;
841 }
842
843 /****************************************************************************
844  Traverse the whole database with this function, calling traverse_callback
845  on each share mode
846 ****************************************************************************/
847
848 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, 
849                        void* state)
850 {
851         struct locking_data *data;
852         share_mode_entry *shares;
853         char *name;
854         int i;
855
856         SHAREMODE_FN(traverse_callback) = (SHAREMODE_FN_CAST())state;
857
858         data = (struct locking_data *)dbuf.dptr;
859         shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
860         name = dbuf.dptr + sizeof(*data) + data->u.num_share_mode_entries*sizeof(*shares);
861
862         for (i=0;i<data->u.num_share_mode_entries;i++) {
863                 traverse_callback(&shares[i], name);
864         }
865         return 0;
866 }
867
868 /*******************************************************************
869  Call the specified function on each entry under management by the
870  share mode system.
871 ********************************************************************/
872
873 int share_mode_forall(SHAREMODE_FN(fn))
874 {
875         if (!tdb)
876                 return 0;
877         return tdb_traverse(tdb, traverse_fn, (void*)fn);
878 }