Removed version number from file header.
[samba.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-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 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;
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
111         DEBUG(10,("do_lock: lock type %s start=%.0f len=%.0f requested for file %s\n",
112                   lock_type_name(lock_type), (double)offset, (double)count, fsp->fsp_name ));
113
114         if (OPEN_FSP(fsp) && fsp->can_lock && (fsp->conn == conn)) {
115                 status = brl_lock(fsp->dev, fsp->inode, fsp->fnum,
116                                   lock_pid, sys_getpid(), conn->cnum, 
117                                   offset, count, 
118                                   lock_type);
119
120                 if (NT_STATUS_IS_OK(status) && lp_posix_locking(SNUM(conn))) {
121
122                         /*
123                          * Try and get a POSIX lock on this range.
124                          * Note that this is ok if it is a read lock
125                          * overlapping on a different fd. JRA.
126                          */
127
128                         if (!set_posix_lock(fsp, offset, count, lock_type)) {
129                                 status = NT_STATUS_LOCK_NOT_GRANTED;
130                                 /*
131                                  * We failed to map - we must now remove the brl
132                                  * lock entry.
133                                  */
134                                 (void)brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
135                                                                 lock_pid, sys_getpid(), conn->cnum, 
136                                                                 offset, count);
137                         }
138                 }
139         }
140
141         return status;
142 }
143
144 /****************************************************************************
145  Utility function called by unlocking requests.
146 ****************************************************************************/
147
148 NTSTATUS do_unlock(files_struct *fsp,connection_struct *conn, uint16 lock_pid,
149                    SMB_BIG_UINT count,SMB_BIG_UINT offset)
150 {
151         BOOL ok = False;
152         
153         if (!lp_locking(SNUM(conn)))
154                 return NT_STATUS_OK;
155         
156         if (!OPEN_FSP(fsp) || !fsp->can_lock || (fsp->conn != conn)) {
157                 return NT_STATUS_INVALID_HANDLE;
158         }
159         
160         DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for file %s\n",
161                   (double)offset, (double)count, fsp->fsp_name ));
162
163         /*
164          * Remove the existing lock record from the tdb lockdb
165          * before looking at POSIX locks. If this record doesn't
166          * match then don't bother looking to remove POSIX locks.
167          */
168
169         ok = brl_unlock(fsp->dev, fsp->inode, fsp->fnum,
170                         lock_pid, sys_getpid(), conn->cnum, offset, count);
171    
172         if (!ok) {
173                 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
174                 return NT_STATUS_LOCK_NOT_GRANTED;
175         }
176
177         if (!lp_posix_locking(SNUM(conn)))
178                 return NT_STATUS_OK;
179
180         (void)release_posix_lock(fsp, offset, count);
181
182         return NT_STATUS_OK;
183 }
184
185 /****************************************************************************
186  Remove any locks on this fd. Called from file_close().
187 ****************************************************************************/
188
189 void locking_close_file(files_struct *fsp)
190 {
191         pid_t pid = sys_getpid();
192
193         if (!lp_locking(SNUM(fsp->conn)))
194                 return;
195
196         /*
197          * Just release all the brl locks, no need to release individually.
198          */
199
200         brl_close(fsp->dev, fsp->inode, pid, fsp->conn->cnum, fsp->fnum);
201
202         if(lp_posix_locking(SNUM(fsp->conn))) {
203
204                 /* 
205                  * Release all the POSIX locks.
206                  */
207                 posix_locking_close_file(fsp);
208
209         }
210 }
211
212 /****************************************************************************
213  Initialise the locking functions.
214 ****************************************************************************/
215
216 static int open_read_only;
217
218 BOOL locking_init(int read_only)
219 {
220         brl_init(read_only);
221
222         if (tdb)
223                 return True;
224
225         tdb = tdb_open_log(lock_path("locking.tdb"), 
226                        0, TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST), 
227                        read_only?O_RDONLY:O_RDWR|O_CREAT,
228                        0644);
229
230         if (!tdb) {
231                 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
232                 return False;
233         }
234         
235         if (!posix_locking_init(read_only))
236                 return False;
237
238         open_read_only = read_only;
239
240         return True;
241 }
242
243 /*******************************************************************
244  Deinitialize the share_mode management.
245 ******************************************************************/
246
247 BOOL locking_end(void)
248 {
249
250         brl_shutdown(open_read_only);
251         if (tdb) {
252
253                 if (tdb_close(tdb) != 0)
254                         return False;
255         }
256
257         return True;
258 }
259
260 /*******************************************************************
261  Form a static locking key for a dev/inode pair.
262 ******************************************************************/
263
264 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
265 {
266         static struct locking_key key;
267         TDB_DATA kbuf;
268
269         memset(&key, '\0', sizeof(key));
270         key.dev = dev;
271         key.inode = inode;
272         kbuf.dptr = (char *)&key;
273         kbuf.dsize = sizeof(key);
274         return kbuf;
275 }
276
277 static TDB_DATA locking_key_fsp(files_struct *fsp)
278 {
279         return locking_key(fsp->dev, fsp->inode);
280 }
281
282 /*******************************************************************
283  Lock a hash bucket entry.
284 ******************************************************************/
285
286 BOOL lock_share_entry(connection_struct *conn,
287                       SMB_DEV_T dev, SMB_INO_T inode)
288 {
289         return tdb_chainlock(tdb, locking_key(dev, inode)) == 0;
290 }
291
292 /*******************************************************************
293  Unlock a hash bucket entry.
294 ******************************************************************/
295
296 void unlock_share_entry(connection_struct *conn,
297                         SMB_DEV_T dev, SMB_INO_T inode)
298 {
299         tdb_chainunlock(tdb, locking_key(dev, inode));
300 }
301
302 /*******************************************************************
303  Lock a hash bucket entry. use a fsp for convenience
304 ******************************************************************/
305
306 BOOL lock_share_entry_fsp(files_struct *fsp)
307 {
308         return tdb_chainlock(tdb, locking_key(fsp->dev, fsp->inode)) == 0;
309 }
310
311 /*******************************************************************
312  Unlock a hash bucket entry.
313 ******************************************************************/
314
315 void unlock_share_entry_fsp(files_struct *fsp)
316 {
317         tdb_chainunlock(tdb, locking_key(fsp->dev, fsp->inode));
318 }
319
320 /*******************************************************************
321  Print out a share mode.
322 ********************************************************************/
323
324 static char *share_mode_str(int num, share_mode_entry *e)
325 {
326         static pstring share_str;
327
328         slprintf(share_str, sizeof(share_str)-1, "share_mode_entry[%d]: \
329 pid = %u, share_mode = 0x%x, port = 0x%x, type= 0x%x, file_id = %lu, dev = 0x%x, inode = %.0f",
330         num, e->pid, e->share_mode, e->op_port, e->op_type, e->share_file_id,
331         (unsigned int)e->dev, (double)e->inode );
332
333         return share_str;
334 }
335
336 /*******************************************************************
337  Print out a share mode table.
338 ********************************************************************/
339
340 static void print_share_mode_table(struct locking_data *data)
341 {
342         int num_share_modes = data->u.num_share_mode_entries;
343         share_mode_entry *shares = (share_mode_entry *)(data + 1);
344         int i;
345
346         for (i = 0; i < num_share_modes; i++) {
347                 share_mode_entry *entry_p = &shares[i];
348                 DEBUG(10,("print_share_mode_table: %s\n", share_mode_str(i, entry_p) ));
349         }
350 }
351
352 /*******************************************************************
353  Get all share mode entries for a dev/inode pair.
354 ********************************************************************/
355
356 int get_share_modes(connection_struct *conn, 
357                     SMB_DEV_T dev, SMB_INO_T inode, 
358                     share_mode_entry **pp_shares)
359 {
360         TDB_DATA dbuf;
361         struct locking_data *data;
362         int num_share_modes;
363         share_mode_entry *shares = NULL;
364
365         *pp_shares = NULL;
366
367         dbuf = tdb_fetch(tdb, locking_key(dev, inode));
368         if (!dbuf.dptr)
369                 return 0;
370
371         data = (struct locking_data *)dbuf.dptr;
372         num_share_modes = data->u.num_share_mode_entries;
373         if(num_share_modes) {
374                 int i;
375                 int del_count = 0;
376
377                 shares = (share_mode_entry *)memdup(dbuf.dptr + sizeof(*data),  
378                                                 num_share_modes * sizeof(share_mode_entry));
379
380                 if (!shares) {
381                         SAFE_FREE(dbuf.dptr);
382                         return 0;
383                 }
384
385                 /*
386                  * Ensure that each entry has a real process attached.
387                  */
388
389                 for (i = 0; i < num_share_modes; ) {
390                         share_mode_entry *entry_p = &shares[i];
391                         if (process_exists(entry_p->pid)) {
392                                 DEBUG(10,("get_share_modes: %s\n", share_mode_str(i, entry_p) ));
393                                 i++;
394                         } else {
395                                 DEBUG(10,("get_share_modes: deleted %s\n", share_mode_str(i, entry_p) ));
396                                 memcpy( &shares[i], &shares[i+1],
397                                         sizeof(share_mode_entry) * (num_share_modes - i - 1));
398                                 num_share_modes--;
399                                 del_count++;
400                         }
401                 }
402
403                 /* Did we delete any ? If so, re-store in tdb. */
404                 if (del_count) {
405                         data->u.num_share_mode_entries = num_share_modes;
406                         
407                         if (num_share_modes)
408                                 memcpy(dbuf.dptr + sizeof(*data), shares,
409                                                 num_share_modes * sizeof(share_mode_entry));
410
411                         /* The record has shrunk a bit */
412                         dbuf.dsize -= del_count * sizeof(share_mode_entry);
413
414                         if (tdb_store(tdb, locking_key(dev, inode), dbuf, TDB_REPLACE) == -1) {
415                                 SAFE_FREE(shares);
416                                 SAFE_FREE(dbuf.dptr);
417                                 return 0;
418                         }
419                 }
420         }
421
422         SAFE_FREE(dbuf.dptr);
423         *pp_shares = shares;
424         return num_share_modes;
425 }
426
427 /*******************************************************************
428  Fill a share mode entry.
429 ********************************************************************/
430
431 static void fill_share_mode(char *p, files_struct *fsp, uint16 port, uint16 op_type)
432 {
433         share_mode_entry *e = (share_mode_entry *)p;
434         void *x = &e->time; /* Needed to force alignment. p may not be aligned.... */
435
436         memset(e, '\0', sizeof(share_mode_entry));
437         e->pid = sys_getpid();
438         e->share_mode = fsp->share_mode;
439         e->op_port = port;
440         e->op_type = op_type;
441         memcpy(x, &fsp->open_time, sizeof(struct timeval));
442         e->share_file_id = fsp->file_id;
443         e->dev = fsp->dev;
444         e->inode = fsp->inode;
445 }
446
447 /*******************************************************************
448  Check if two share mode entries are identical, ignoring oplock 
449  and port info. 
450 ********************************************************************/
451
452 BOOL share_modes_identical( share_mode_entry *e1, share_mode_entry *e2)
453 {
454 #if 1 /* JRA PARANOIA TEST - REMOVE LATER */
455         if (e1->pid == e2->pid &&
456                 e1->share_file_id == e2->share_file_id &&
457                 e1->dev == e2->dev &&
458                 e1->inode == e2->inode &&
459                 (e1->share_mode & ~DELETE_ON_CLOSE_FLAG) != (e2->share_mode & ~DELETE_ON_CLOSE_FLAG)) {
460                         DEBUG(0,("PANIC: share_modes_identical: share_mode missmatch (e1 = %u, e2 = %u). Logic error.\n",
461                                 (unsigned int)(e1->share_mode & ~DELETE_ON_CLOSE_FLAG),
462                                 (unsigned int)(e2->share_mode & ~DELETE_ON_CLOSE_FLAG) ));
463                 smb_panic("PANIC: share_modes_identical logic error.\n");
464         }
465 #endif
466
467         return (e1->pid == e2->pid &&
468                 (e1->share_mode & ~DELETE_ON_CLOSE_FLAG) == (e2->share_mode & ~DELETE_ON_CLOSE_FLAG) &&
469                 e1->dev == e2->dev &&
470                 e1->inode == e2->inode &&
471                 e1->share_file_id == e2->share_file_id );
472 }
473
474 /*******************************************************************
475  Delete a specific share mode. Return the number
476  of entries left, and a memdup'ed copy of the entry deleted (if required).
477  Ignore if no entry deleted.
478 ********************************************************************/
479
480 ssize_t del_share_entry( SMB_DEV_T dev, SMB_INO_T inode,
481                         share_mode_entry *entry, share_mode_entry **ppse)
482 {
483         TDB_DATA dbuf;
484         struct locking_data *data;
485         int i, del_count=0;
486         share_mode_entry *shares;
487         ssize_t count = 0;
488
489         if (ppse)
490                 *ppse = NULL;
491
492         /* read in the existing share modes */
493         dbuf = tdb_fetch(tdb, locking_key(dev, inode));
494         if (!dbuf.dptr)
495                 return -1;
496
497         data = (struct locking_data *)dbuf.dptr;
498         shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
499
500         /*
501          * Find any with this pid and delete it
502          * by overwriting with the rest of the data 
503          * from the record.
504          */
505
506         DEBUG(10,("del_share_entry: num_share_modes = %d\n", data->u.num_share_mode_entries ));
507
508         for (i=0;i<data->u.num_share_mode_entries;) {
509                 if (share_modes_identical(&shares[i], entry)) {
510                         DEBUG(10,("del_share_entry: deleted %s\n",
511                                 share_mode_str(i, &shares[i]) ));
512                         if (ppse)
513                                 *ppse = memdup(&shares[i], sizeof(*shares));
514                         data->u.num_share_mode_entries--;
515                         memmove(&shares[i], &shares[i+1], 
516                                 dbuf.dsize - (sizeof(*data) + (i+1)*sizeof(*shares)));
517                         del_count++;
518
519                         DEBUG(10,("del_share_entry: deleting entry %d\n", i ));
520
521                 } else {
522                         i++;
523                 }
524         }
525
526         if (del_count) {
527                 /* the record may have shrunk a bit */
528                 dbuf.dsize -= del_count * sizeof(*shares);
529
530                 count = (ssize_t)data->u.num_share_mode_entries;
531
532                 /* store it back in the database */
533                 if (data->u.num_share_mode_entries == 0) {
534                         if (tdb_delete(tdb, locking_key(dev, inode)) == -1)
535                                 count = -1;
536                 } else {
537                         if (tdb_store(tdb, locking_key(dev, inode), dbuf, TDB_REPLACE) == -1)
538                                 count = -1;
539                 }
540         }
541         DEBUG(10,("del_share_entry: Remaining table.\n"));
542         print_share_mode_table((struct locking_data *)dbuf.dptr);
543         SAFE_FREE(dbuf.dptr);
544         return count;
545 }
546
547 /*******************************************************************
548  Del the share mode of a file for this process. Return the number
549  of entries left, and a memdup'ed copy of the entry deleted.
550 ********************************************************************/
551
552 ssize_t del_share_mode(files_struct *fsp, share_mode_entry **ppse)
553 {
554         share_mode_entry entry;
555
556         /*
557          * Fake up a share_mode_entry for comparisons.
558          */
559
560         fill_share_mode((char *)&entry, fsp, 0, 0);
561         return del_share_entry(fsp->dev, fsp->inode, &entry, ppse);
562 }
563
564 /*******************************************************************
565  Set the share mode of a file. Return False on fail, True on success.
566 ********************************************************************/
567
568 BOOL set_share_mode(files_struct *fsp, uint16 port, uint16 op_type)
569 {
570         TDB_DATA dbuf;
571         struct locking_data *data;
572         char *p=NULL;
573         int size;
574         BOOL ret = True;
575                 
576         /* read in the existing share modes if any */
577         dbuf = tdb_fetch(tdb, locking_key_fsp(fsp));
578         if (!dbuf.dptr) {
579                 /* we'll need to create a new record */
580                 pstring fname;
581
582                 pstrcpy(fname, fsp->conn->connectpath);
583                 pstrcat(fname, "/");
584                 pstrcat(fname, fsp->fsp_name);
585
586                 size = sizeof(*data) + sizeof(share_mode_entry) + strlen(fname) + 1;
587                 p = (char *)malloc(size);
588                 if (!p)
589                         return False;
590                 data = (struct locking_data *)p;
591                 data->u.num_share_mode_entries = 1;
592         
593                 DEBUG(10,("set_share_mode: creating entry for file %s. num_share_modes = 1\n",
594                         fsp->fsp_name ));
595
596                 pstrcpy(p + sizeof(*data) + sizeof(share_mode_entry), fname);
597                 fill_share_mode(p + sizeof(*data), fsp, port, op_type);
598                 dbuf.dptr = p;
599                 dbuf.dsize = size;
600                 if (tdb_store(tdb, locking_key_fsp(fsp), dbuf, TDB_REPLACE) == -1)
601                         ret = False;
602
603                 print_share_mode_table((struct locking_data *)p);
604
605                 SAFE_FREE(p);
606                 return ret;
607         }
608
609         /* we're adding to an existing entry - this is a bit fiddly */
610         data = (struct locking_data *)dbuf.dptr;
611
612         data->u.num_share_mode_entries++;
613         
614         DEBUG(10,("set_share_mode: adding entry for file %s. new num_share_modes = %d\n",
615                 fsp->fsp_name, data->u.num_share_mode_entries ));
616
617         size = dbuf.dsize + sizeof(share_mode_entry);
618         p = malloc(size);
619         if (!p)
620                 return False;
621         memcpy(p, dbuf.dptr, sizeof(*data));
622         fill_share_mode(p + sizeof(*data), fsp, port, op_type);
623         memcpy(p + sizeof(*data) + sizeof(share_mode_entry), dbuf.dptr + sizeof(*data),
624                dbuf.dsize - sizeof(*data));
625         SAFE_FREE(dbuf.dptr);
626         dbuf.dptr = p;
627         dbuf.dsize = size;
628         if (tdb_store(tdb, locking_key_fsp(fsp), dbuf, TDB_REPLACE) == -1)
629                 ret = False;
630         print_share_mode_table((struct locking_data *)p);
631         SAFE_FREE(p);
632         return ret;
633 }
634
635 /*******************************************************************
636  A generic in-place modification call for share mode entries.
637 ********************************************************************/
638
639 static BOOL mod_share_mode( SMB_DEV_T dev, SMB_INO_T inode, share_mode_entry *entry,
640                            void (*mod_fn)(share_mode_entry *, SMB_DEV_T, SMB_INO_T, void *),
641                            void *param)
642 {
643         TDB_DATA dbuf;
644         struct locking_data *data;
645         int i;
646         share_mode_entry *shares;
647         BOOL need_store=False;
648
649         /* read in the existing share modes */
650         dbuf = tdb_fetch(tdb, locking_key(dev, inode));
651         if (!dbuf.dptr)
652                 return False;
653
654         data = (struct locking_data *)dbuf.dptr;
655         shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
656
657         /* find any with our pid and call the supplied function */
658         for (i=0;i<data->u.num_share_mode_entries;i++) {
659                 if (share_modes_identical(entry, &shares[i])) {
660                         mod_fn(&shares[i], dev, inode, param);
661                         need_store=True;
662                 }
663         }
664
665         /* if the mod fn was called then store it back */
666         if (need_store) {
667                 if (data->u.num_share_mode_entries == 0) {
668                         if (tdb_delete(tdb, locking_key(dev, inode)) == -1)
669                                 need_store = False;
670                 } else {
671                         if (tdb_store(tdb, locking_key(dev, inode), dbuf, TDB_REPLACE) == -1)
672                                 need_store = False;
673                 }
674         }
675
676         SAFE_FREE(dbuf.dptr);
677         return need_store;
678 }
679
680 /*******************************************************************
681  Static function that actually does the work for the generic function
682  below.
683 ********************************************************************/
684
685 static void remove_share_oplock_fn(share_mode_entry *entry, SMB_DEV_T dev, SMB_INO_T inode, 
686                                    void *param)
687 {
688         DEBUG(10,("remove_share_oplock_fn: removing oplock info for entry dev=%x ino=%.0f\n",
689                   (unsigned int)dev, (double)inode ));
690         /* Delete the oplock info. */
691         entry->op_port = 0;
692         entry->op_type = NO_OPLOCK;
693 }
694
695 /*******************************************************************
696  Remove an oplock port and mode entry from a share mode.
697 ********************************************************************/
698
699 BOOL remove_share_oplock(files_struct *fsp)
700 {
701         share_mode_entry entry;
702         /*
703          * Fake up an entry for comparisons...
704          */
705         fill_share_mode((char *)&entry, fsp, 0, 0);
706         return mod_share_mode(fsp->dev, fsp->inode, &entry, remove_share_oplock_fn, NULL);
707 }
708
709 /*******************************************************************
710  Static function that actually does the work for the generic function
711  below.
712 ********************************************************************/
713
714 static void downgrade_share_oplock_fn(share_mode_entry *entry, SMB_DEV_T dev, SMB_INO_T inode, 
715                                    void *param)
716 {
717         DEBUG(10,("downgrade_share_oplock_fn: downgrading oplock info for entry dev=%x ino=%.0f\n",
718                   (unsigned int)dev, (double)inode ));
719         entry->op_type = LEVEL_II_OPLOCK;
720 }
721
722 /*******************************************************************
723  Downgrade a oplock type from exclusive to level II.
724 ********************************************************************/
725
726 BOOL downgrade_share_oplock(files_struct *fsp)
727 {
728         share_mode_entry entry;
729         /*
730          * Fake up an entry for comparisons...
731          */
732         fill_share_mode((char *)&entry, fsp, 0, 0);
733         return mod_share_mode(fsp->dev, fsp->inode, &entry, downgrade_share_oplock_fn, NULL);
734 }
735
736 /*******************************************************************
737  Get/Set the delete on close flag in a set of share modes.
738  Return False on fail, True on success.
739 ********************************************************************/
740
741 BOOL modify_delete_flag( SMB_DEV_T dev, SMB_INO_T inode, BOOL delete_on_close)
742 {
743         TDB_DATA dbuf;
744         struct locking_data *data;
745         int i;
746         share_mode_entry *shares;
747
748         /* read in the existing share modes */
749         dbuf = tdb_fetch(tdb, locking_key(dev, inode));
750         if (!dbuf.dptr)
751                 return False;
752
753         data = (struct locking_data *)dbuf.dptr;
754         shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
755
756         /* Set/Unset the delete on close element. */
757         for (i=0;i<data->u.num_share_mode_entries;i++,shares++) {
758                 shares->share_mode = (delete_on_close ?
759                             (shares->share_mode | DELETE_ON_CLOSE_FLAG) :
760                             (shares->share_mode & ~DELETE_ON_CLOSE_FLAG) );
761         }
762
763         /* store it back */
764         if (data->u.num_share_mode_entries) {
765                 if (tdb_store(tdb, locking_key(dev,inode), dbuf, TDB_REPLACE)==-1) {
766                         SAFE_FREE(dbuf.dptr);
767                         return False;
768                 }
769         }
770
771         SAFE_FREE(dbuf.dptr);
772         return True;
773 }
774
775 /****************************************************************************
776  Traverse the whole database with this function, calling traverse_callback
777  on each share mode
778 ****************************************************************************/
779
780 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, 
781                        void* state)
782 {
783         struct locking_data *data;
784         share_mode_entry *shares;
785         char *name;
786         int i;
787
788         SHAREMODE_FN(traverse_callback) = (SHAREMODE_FN_CAST())state;
789
790         data = (struct locking_data *)dbuf.dptr;
791         shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));
792         name = dbuf.dptr + sizeof(*data) + data->u.num_share_mode_entries*sizeof(*shares);
793
794         for (i=0;i<data->u.num_share_mode_entries;i++) {
795                 traverse_callback(&shares[i], name);
796         }
797         return 0;
798 }
799
800 /*******************************************************************
801  Call the specified function on each entry under management by the
802  share mode system.
803 ********************************************************************/
804
805 int share_mode_forall(SHAREMODE_FN(fn))
806 {
807         if (!tdb)
808                 return 0;
809         return tdb_traverse(tdb, traverse_fn, (void*)fn);
810 }