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