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