Merge branch 'v3-2-test' of ssh://jra@git.samba.org/data/git/samba into v3-2-test
[ira/wip.git] / source3 / locking / locking.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Locking functions
4    Copyright (C) Andrew Tridgell 1992-2000
5    Copyright (C) Jeremy Allison 1992-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 bool locking_init_internal(bool read_only)
389 {
390         brl_init(read_only);
391
392         if (lock_db)
393                 return True;
394
395         lock_db = db_open(NULL, lock_path("locking.tdb"), 0,
396                           TDB_DEFAULT
397                           |TDB_VOLATILE
398                           |(read_only?0x0:TDB_CLEAR_IF_FIRST),
399                           read_only?O_RDONLY:O_RDWR|O_CREAT, 0644);
400
401         if (!lock_db) {
402                 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
403                 return False;
404         }
405
406         if (!posix_locking_init(read_only))
407                 return False;
408
409         return True;
410 }
411
412 bool locking_init(void)
413 {
414         return locking_init_internal(false);
415 }
416
417 bool locking_init_readonly(void)
418 {
419         return locking_init_internal(true);
420 }
421
422 /*******************************************************************
423  Deinitialize the share_mode management.
424 ******************************************************************/
425
426 bool locking_end(void)
427 {
428         brl_shutdown();
429         TALLOC_FREE(lock_db);
430         return true;
431 }
432
433 /*******************************************************************
434  Form a static locking key for a dev/inode pair.
435 ******************************************************************/
436
437 static TDB_DATA locking_key(const struct file_id *id, struct file_id *tmp)
438 {
439         *tmp = *id;
440         return make_tdb_data((const uint8_t *)tmp, sizeof(*tmp));
441 }
442
443 /*******************************************************************
444  Print out a share mode.
445 ********************************************************************/
446
447 char *share_mode_str(TALLOC_CTX *ctx, int num, struct share_mode_entry *e)
448 {
449         return talloc_asprintf(ctx, "share_mode_entry[%d]: %s "
450                  "pid = %s, share_access = 0x%x, private_options = 0x%x, "
451                  "access_mask = 0x%x, mid = 0x%x, type= 0x%x, gen_id = %lu, "
452                  "uid = %u, flags = %u, file_id %s",
453                  num,
454                  e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
455                  procid_str_static(&e->pid),
456                  e->share_access, e->private_options,
457                  e->access_mask, e->op_mid, e->op_type, e->share_file_id,
458                  (unsigned int)e->uid, (unsigned int)e->flags,
459                  file_id_string_tos(&e->id));
460 }
461
462 /*******************************************************************
463  Print out a share mode table.
464 ********************************************************************/
465
466 static void print_share_mode_table(struct locking_data *data)
467 {
468         int num_share_modes = data->u.s.num_share_mode_entries;
469         struct share_mode_entry *shares =
470                 (struct share_mode_entry *)(data + 1);
471         int i;
472
473         for (i = 0; i < num_share_modes; i++) {
474                 struct share_mode_entry entry;
475                 char *str;
476
477                 /*
478                  * We need to memcpy the entry here due to alignment
479                  * restrictions that are not met when directly accessing
480                  * shares[i]
481                  */
482
483                 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
484                 str = share_mode_str(talloc_tos(), i, &entry);
485
486                 DEBUG(10,("print_share_mode_table: %s\n", str ? str : ""));
487                 TALLOC_FREE(str);
488         }
489 }
490
491 /*******************************************************************
492  Get all share mode entries for a dev/inode pair.
493 ********************************************************************/
494
495 static bool parse_share_modes(TDB_DATA dbuf, struct share_mode_lock *lck)
496 {
497         struct locking_data *data;
498         int i;
499
500         if (dbuf.dsize < sizeof(struct locking_data)) {
501                 smb_panic("parse_share_modes: buffer too short");
502         }
503
504         data = (struct locking_data *)dbuf.dptr;
505
506         lck->delete_on_close = data->u.s.delete_on_close;
507         lck->num_share_modes = data->u.s.num_share_mode_entries;
508
509         DEBUG(10, ("parse_share_modes: delete_on_close: %d, "
510                    "num_share_modes: %d\n",
511                 lck->delete_on_close,
512                 lck->num_share_modes));
513
514         if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
515                 DEBUG(0, ("invalid number of share modes: %d\n",
516                           lck->num_share_modes));
517                 smb_panic("parse_share_modes: invalid number of share modes");
518         }
519
520         lck->share_modes = NULL;
521         
522         if (lck->num_share_modes != 0) {
523
524                 if (dbuf.dsize < (sizeof(struct locking_data) +
525                                   (lck->num_share_modes *
526                                    sizeof(struct share_mode_entry)))) {
527                         smb_panic("parse_share_modes: buffer too short");
528                 }
529                                   
530                 lck->share_modes = (struct share_mode_entry *)
531                         TALLOC_MEMDUP(lck, dbuf.dptr+sizeof(*data),
532                                       lck->num_share_modes *
533                                       sizeof(struct share_mode_entry));
534
535                 if (lck->share_modes == NULL) {
536                         smb_panic("parse_share_modes: talloc failed");
537                 }
538         }
539
540         /* Get any delete token. */
541         if (data->u.s.delete_token_size) {
542                 uint8 *p = dbuf.dptr + sizeof(*data) +
543                                 (lck->num_share_modes *
544                                 sizeof(struct share_mode_entry));
545
546                 if ((data->u.s.delete_token_size < sizeof(uid_t) + sizeof(gid_t)) ||
547                                 ((data->u.s.delete_token_size - sizeof(uid_t)) % sizeof(gid_t)) != 0) {
548                         DEBUG(0, ("parse_share_modes: invalid token size %d\n",
549                                 data->u.s.delete_token_size));
550                         smb_panic("parse_share_modes: invalid token size");
551                 }
552
553                 lck->delete_token = TALLOC_P(lck, UNIX_USER_TOKEN);
554                 if (!lck->delete_token) {
555                         smb_panic("parse_share_modes: talloc failed");
556                 }
557
558                 /* Copy out the uid and gid. */
559                 memcpy(&lck->delete_token->uid, p, sizeof(uid_t));
560                 p += sizeof(uid_t);
561                 memcpy(&lck->delete_token->gid, p, sizeof(gid_t));
562                 p += sizeof(gid_t);
563
564                 /* Any supplementary groups ? */
565                 lck->delete_token->ngroups = (data->u.s.delete_token_size > (sizeof(uid_t) + sizeof(gid_t))) ?
566                                         ((data->u.s.delete_token_size -
567                                                 (sizeof(uid_t) + sizeof(gid_t)))/sizeof(gid_t)) : 0;
568
569                 if (lck->delete_token->ngroups) {
570                         /* Make this a talloc child of lck->delete_token. */
571                         lck->delete_token->groups = TALLOC_ARRAY(lck->delete_token, gid_t,
572                                                         lck->delete_token->ngroups);
573                         if (!lck->delete_token) {
574                                 smb_panic("parse_share_modes: talloc failed");
575                         }
576
577                         for (i = 0; i < lck->delete_token->ngroups; i++) {
578                                 memcpy(&lck->delete_token->groups[i], p, sizeof(gid_t));
579                                 p += sizeof(gid_t);
580                         }
581                 }
582
583         } else {
584                 lck->delete_token = NULL;
585         }
586
587         /* Save off the associated service path and filename. */
588         lck->servicepath = talloc_strdup(lck, (const char *)dbuf.dptr + sizeof(*data) +
589                                         (lck->num_share_modes *
590                                         sizeof(struct share_mode_entry)) +
591                                         data->u.s.delete_token_size );
592         if (lck->servicepath == NULL) {
593                 smb_panic("parse_share_modes: talloc_strdup failed");
594         }
595
596         lck->filename = talloc_strdup(lck, (const char *)dbuf.dptr + sizeof(*data) +
597                                         (lck->num_share_modes *
598                                         sizeof(struct share_mode_entry)) +
599                                         data->u.s.delete_token_size +
600                                         strlen(lck->servicepath) + 1 );
601         if (lck->filename == NULL) {
602                 smb_panic("parse_share_modes: talloc_strdup failed");
603         }
604
605         /*
606          * Ensure that each entry has a real process attached.
607          */
608
609         for (i = 0; i < lck->num_share_modes; i++) {
610                 struct share_mode_entry *entry_p = &lck->share_modes[i];
611                 char *str = NULL;
612                 if (DEBUGLEVEL >= 10) {
613                         str = share_mode_str(NULL, i, entry_p);
614                 }
615                 DEBUG(10,("parse_share_modes: %s\n",
616                         str ? str : ""));
617                 if (!process_exists(entry_p->pid)) {
618                         DEBUG(10,("parse_share_modes: deleted %s\n",
619                                 str ? str : ""));
620                         entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
621                         lck->modified = True;
622                 }
623                 TALLOC_FREE(str);
624         }
625
626         return True;
627 }
628
629 static TDB_DATA unparse_share_modes(struct share_mode_lock *lck)
630 {
631         TDB_DATA result;
632         int num_valid = 0;
633         int i;
634         struct locking_data *data;
635         ssize_t offset;
636         ssize_t sp_len;
637         uint32 delete_token_size;
638
639         result.dptr = NULL;
640         result.dsize = 0;
641
642         for (i=0; i<lck->num_share_modes; i++) {
643                 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
644                         num_valid += 1;
645                 }
646         }
647
648         if (num_valid == 0) {
649                 return result;
650         }
651
652         sp_len = strlen(lck->servicepath);
653         delete_token_size = (lck->delete_token ?
654                         (sizeof(uid_t) + sizeof(gid_t) + (lck->delete_token->ngroups*sizeof(gid_t))) : 0);
655
656         result.dsize = sizeof(*data) +
657                 lck->num_share_modes * sizeof(struct share_mode_entry) +
658                 delete_token_size +
659                 sp_len + 1 +
660                 strlen(lck->filename) + 1;
661         result.dptr = TALLOC_ARRAY(lck, uint8, result.dsize);
662
663         if (result.dptr == NULL) {
664                 smb_panic("talloc failed");
665         }
666
667         data = (struct locking_data *)result.dptr;
668         ZERO_STRUCTP(data);
669         data->u.s.num_share_mode_entries = lck->num_share_modes;
670         data->u.s.delete_on_close = lck->delete_on_close;
671         data->u.s.delete_token_size = delete_token_size;
672         DEBUG(10, ("unparse_share_modes: del: %d, tok = %u, num: %d\n",
673                 data->u.s.delete_on_close,
674                 (unsigned int)data->u.s.delete_token_size,
675                 data->u.s.num_share_mode_entries));
676         memcpy(result.dptr + sizeof(*data), lck->share_modes,
677                sizeof(struct share_mode_entry)*lck->num_share_modes);
678         offset = sizeof(*data) +
679                 sizeof(struct share_mode_entry)*lck->num_share_modes;
680
681         /* Store any delete on close token. */
682         if (lck->delete_token) {
683                 uint8 *p = result.dptr + offset;
684
685                 memcpy(p, &lck->delete_token->uid, sizeof(uid_t));
686                 p += sizeof(uid_t);
687
688                 memcpy(p, &lck->delete_token->gid, sizeof(gid_t));
689                 p += sizeof(gid_t);
690
691                 for (i = 0; i < lck->delete_token->ngroups; i++) {
692                         memcpy(p, &lck->delete_token->groups[i], sizeof(gid_t));
693                         p += sizeof(gid_t);
694                 }
695                 offset = p - result.dptr;
696         }
697
698         safe_strcpy((char *)result.dptr + offset, lck->servicepath,
699                     result.dsize - offset - 1);
700         offset += sp_len + 1;
701         safe_strcpy((char *)result.dptr + offset, lck->filename,
702                     result.dsize - offset - 1);
703
704         if (DEBUGLEVEL >= 10) {
705                 print_share_mode_table(data);
706         }
707
708         return result;
709 }
710
711 static int share_mode_lock_destructor(struct share_mode_lock *lck)
712 {
713         NTSTATUS status;
714         TDB_DATA data;
715
716         if (!lck->modified) {
717                 return 0;
718         }
719
720         data = unparse_share_modes(lck);
721
722         if (data.dptr == NULL) {
723                 if (!lck->fresh) {
724                         /* There has been an entry before, delete it */
725
726                         status = lck->record->delete_rec(lck->record);
727                         if (!NT_STATUS_IS_OK(status)) {
728                                 DEBUG(0, ("delete_rec returned %s\n",
729                                           nt_errstr(status)));
730                                 smb_panic("could not delete share entry");
731                         }
732                 }
733                 goto done;
734         }
735
736         status = lck->record->store(lck->record, data, TDB_REPLACE);
737         if (!NT_STATUS_IS_OK(status)) {
738                 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
739                 smb_panic("could not store share mode entry");
740         }
741
742  done:
743
744         return 0;
745 }
746
747 static bool fill_share_mode_lock(struct share_mode_lock *lck,
748                                  struct file_id id,
749                                  const char *servicepath,
750                                  const char *fname,
751                                  TDB_DATA share_mode_data)
752 {
753         /* Ensure we set every field here as the destructor must be
754            valid even if parse_share_modes fails. */
755
756         lck->servicepath = NULL;
757         lck->filename = NULL;
758         lck->id = id;
759         lck->num_share_modes = 0;
760         lck->share_modes = NULL;
761         lck->delete_token = NULL;
762         lck->delete_on_close = False;
763         lck->fresh = False;
764         lck->modified = False;
765
766         lck->fresh = (share_mode_data.dptr == NULL);
767
768         if (lck->fresh) {
769                 if (fname == NULL || servicepath == NULL) {
770                         return False;
771                 }
772                 lck->filename = talloc_strdup(lck, fname);
773                 lck->servicepath = talloc_strdup(lck, servicepath);
774                 if (lck->filename == NULL || lck->servicepath == NULL) {
775                         DEBUG(0, ("talloc failed\n"));
776                         return False;
777                 }
778         } else {
779                 if (!parse_share_modes(share_mode_data, lck)) {
780                         DEBUG(0, ("Could not parse share modes\n"));
781                         return False;
782                 }
783         }
784
785         return True;
786 }
787
788 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
789                                             const struct file_id id,
790                                             const char *servicepath,
791                                             const char *fname)
792 {
793         struct share_mode_lock *lck;
794         struct file_id tmp;
795         TDB_DATA key = locking_key(&id, &tmp);
796
797         if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
798                 DEBUG(0, ("talloc failed\n"));
799                 return NULL;
800         }
801
802         if (!(lck->record = lock_db->fetch_locked(lock_db, lck, key))) {
803                 DEBUG(3, ("Could not lock share entry\n"));
804                 TALLOC_FREE(lck);
805                 return NULL;
806         }
807
808         if (!fill_share_mode_lock(lck, id, servicepath, fname,
809                                   lck->record->value)) {
810                 DEBUG(3, ("fill_share_mode_lock failed\n"));
811                 TALLOC_FREE(lck);
812                 return NULL;
813         }
814
815         talloc_set_destructor(lck, share_mode_lock_destructor);
816
817         return lck;
818 }
819
820 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
821                                                   const struct file_id id,
822                                                   const char *servicepath,
823                                                   const char *fname)
824 {
825         struct share_mode_lock *lck;
826         struct file_id tmp;
827         TDB_DATA key = locking_key(&id, &tmp);
828         TDB_DATA data;
829
830         if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
831                 DEBUG(0, ("talloc failed\n"));
832                 return NULL;
833         }
834
835         if (lock_db->fetch(lock_db, lck, key, &data) == -1) {
836                 DEBUG(3, ("Could not fetch share entry\n"));
837                 TALLOC_FREE(lck);
838                 return NULL;
839         }
840
841         if (!fill_share_mode_lock(lck, id, servicepath, fname, data)) {
842                 DEBUG(3, ("fill_share_mode_lock failed\n"));
843                 TALLOC_FREE(lck);
844                 return NULL;
845         }
846
847         TALLOC_FREE(data.dptr);
848
849         return lck;
850 }
851
852 /*******************************************************************
853  Sets the service name and filename for rename.
854  At this point we emit "file renamed" messages to all
855  process id's that have this file open.
856  Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
857 ********************************************************************/
858
859 bool rename_share_filename(struct messaging_context *msg_ctx,
860                         struct share_mode_lock *lck,
861                         const char *servicepath,
862                         const char *newname)
863 {
864         size_t sp_len;
865         size_t fn_len;
866         size_t msg_len;
867         char *frm = NULL;
868         int i;
869
870         DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
871                 servicepath, newname));
872
873         /*
874          * rename_internal_fsp() and rename_internals() add './' to
875          * head of newname if newname does not contain a '/'.
876          */
877         while (newname[0] && newname[1] && newname[0] == '.' && newname[1] == '/') {
878                 newname += 2;
879         }
880
881         lck->servicepath = talloc_strdup(lck, servicepath);
882         lck->filename = talloc_strdup(lck, newname);
883         if (lck->filename == NULL || lck->servicepath == NULL) {
884                 DEBUG(0, ("rename_share_filename: talloc failed\n"));
885                 return False;
886         }
887         lck->modified = True;
888
889         sp_len = strlen(lck->servicepath);
890         fn_len = strlen(lck->filename);
891
892         msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + fn_len + 1;
893
894         /* Set up the name changed message. */
895         frm = TALLOC_ARRAY(lck, char, msg_len);
896         if (!frm) {
897                 return False;
898         }
899
900         push_file_id_16(frm, &lck->id);
901
902         DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
903
904         safe_strcpy(&frm[16], lck->servicepath, sp_len);
905         safe_strcpy(&frm[16 + sp_len + 1], lck->filename, fn_len);
906
907         /* Send the messages. */
908         for (i=0; i<lck->num_share_modes; i++) {
909                 struct share_mode_entry *se = &lck->share_modes[i];
910                 if (!is_valid_share_mode_entry(se)) {
911                         continue;
912                 }
913                 /* But not to ourselves... */
914                 if (procid_is_me(&se->pid)) {
915                         continue;
916                 }
917
918                 DEBUG(10,("rename_share_filename: sending rename message to pid %s "
919                           "file_id %s sharepath %s newname %s\n",
920                           procid_str_static(&se->pid),
921                           file_id_string_tos(&lck->id),
922                           lck->servicepath, lck->filename ));
923
924                 messaging_send_buf(msg_ctx, se->pid, MSG_SMB_FILE_RENAME,
925                                    (uint8 *)frm, msg_len);
926         }
927
928         return True;
929 }
930
931 bool get_delete_on_close_flag(struct file_id id)
932 {
933         bool result;
934         struct share_mode_lock *lck;
935   
936         if (!(lck = fetch_share_mode_unlocked(NULL, id, NULL, NULL))) {
937                 return False;
938         }
939         result = lck->delete_on_close;
940         TALLOC_FREE(lck);
941         return result;
942 }
943
944 bool is_valid_share_mode_entry(const struct share_mode_entry *e)
945 {
946         int num_props = 0;
947
948         if (e->op_type == UNUSED_SHARE_MODE_ENTRY) {
949                 /* cope with dead entries from the process not
950                    existing. These should not be considered valid,
951                    otherwise we end up doing zero timeout sharing
952                    violation */
953                 return False;
954         }
955
956         num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
957         num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
958         num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
959
960         SMB_ASSERT(num_props <= 1);
961         return (num_props != 0);
962 }
963
964 bool is_deferred_open_entry(const struct share_mode_entry *e)
965 {
966         return (e->op_type == DEFERRED_OPEN_ENTRY);
967 }
968
969 bool is_unused_share_mode_entry(const struct share_mode_entry *e)
970 {
971         return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
972 }
973
974 /*******************************************************************
975  Fill a share mode entry.
976 ********************************************************************/
977
978 static void fill_share_mode_entry(struct share_mode_entry *e,
979                                   files_struct *fsp,
980                                   uid_t uid, uint16 mid, uint16 op_type)
981 {
982         ZERO_STRUCTP(e);
983         e->pid = procid_self();
984         e->share_access = fsp->share_access;
985         e->private_options = fsp->fh->private_options;
986         e->access_mask = fsp->access_mask;
987         e->op_mid = mid;
988         e->op_type = op_type;
989         e->time.tv_sec = fsp->open_time.tv_sec;
990         e->time.tv_usec = fsp->open_time.tv_usec;
991         e->id = fsp->file_id;
992         e->share_file_id = fsp->fh->gen_id;
993         e->uid = (uint32)uid;
994         e->flags = fsp->posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0;
995 }
996
997 static void fill_deferred_open_entry(struct share_mode_entry *e,
998                                      const struct timeval request_time,
999                                      struct file_id id, uint16 mid)
1000 {
1001         ZERO_STRUCTP(e);
1002         e->pid = procid_self();
1003         e->op_mid = mid;
1004         e->op_type = DEFERRED_OPEN_ENTRY;
1005         e->time.tv_sec = request_time.tv_sec;
1006         e->time.tv_usec = request_time.tv_usec;
1007         e->id = id;
1008         e->uid = (uint32)-1;
1009         e->flags = 0;
1010 }
1011
1012 static void add_share_mode_entry(struct share_mode_lock *lck,
1013                                  const struct share_mode_entry *entry)
1014 {
1015         int i;
1016
1017         for (i=0; i<lck->num_share_modes; i++) {
1018                 struct share_mode_entry *e = &lck->share_modes[i];
1019                 if (is_unused_share_mode_entry(e)) {
1020                         *e = *entry;
1021                         break;
1022                 }
1023         }
1024
1025         if (i == lck->num_share_modes) {
1026                 /* No unused entry found */
1027                 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
1028                              &lck->share_modes, &lck->num_share_modes);
1029         }
1030         lck->modified = True;
1031 }
1032
1033 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
1034                         uid_t uid, uint16 mid, uint16 op_type, bool initial_delete_on_close_allowed)
1035 {
1036         struct share_mode_entry entry;
1037         fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
1038         if (initial_delete_on_close_allowed) {
1039                 entry.flags |= SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE;
1040         }
1041         add_share_mode_entry(lck, &entry);
1042 }
1043
1044 void add_deferred_open(struct share_mode_lock *lck, uint16 mid,
1045                        struct timeval request_time,
1046                        struct file_id id)
1047 {
1048         struct share_mode_entry entry;
1049         fill_deferred_open_entry(&entry, request_time, id, mid);
1050         add_share_mode_entry(lck, &entry);
1051 }
1052
1053 /*******************************************************************
1054  Check if two share mode entries are identical, ignoring oplock 
1055  and mid info and desired_access. (Removed paranoia test - it's
1056  not automatically a logic error if they are identical. JRA.)
1057 ********************************************************************/
1058
1059 static bool share_modes_identical(struct share_mode_entry *e1,
1060                                   struct share_mode_entry *e2)
1061 {
1062         /* We used to check for e1->share_access == e2->share_access here
1063            as well as the other fields but 2 different DOS or FCB opens
1064            sharing the same share mode entry may validly differ in
1065            fsp->share_access field. */
1066
1067         return (procid_equal(&e1->pid, &e2->pid) &&
1068                 file_id_equal(&e1->id, &e2->id) &&
1069                 e1->share_file_id == e2->share_file_id );
1070 }
1071
1072 static bool deferred_open_identical(struct share_mode_entry *e1,
1073                                     struct share_mode_entry *e2)
1074 {
1075         return (procid_equal(&e1->pid, &e2->pid) &&
1076                 (e1->op_mid == e2->op_mid) &&
1077                 file_id_equal(&e1->id, &e2->id));
1078 }
1079
1080 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1081                                                       struct share_mode_entry *entry)
1082 {
1083         int i;
1084
1085         for (i=0; i<lck->num_share_modes; i++) {
1086                 struct share_mode_entry *e = &lck->share_modes[i];
1087                 if (is_valid_share_mode_entry(entry) &&
1088                     is_valid_share_mode_entry(e) &&
1089                     share_modes_identical(e, entry)) {
1090                         return e;
1091                 }
1092                 if (is_deferred_open_entry(entry) &&
1093                     is_deferred_open_entry(e) &&
1094                     deferred_open_identical(e, entry)) {
1095                         return e;
1096                 }
1097         }
1098         return NULL;
1099 }
1100
1101 /*******************************************************************
1102  Del the share mode of a file for this process. Return the number of
1103  entries left.
1104 ********************************************************************/
1105
1106 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1107 {
1108         struct share_mode_entry entry, *e;
1109
1110         /* Don't care about the pid owner being correct here - just a search. */
1111         fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1112
1113         e = find_share_mode_entry(lck, &entry);
1114         if (e == NULL) {
1115                 return False;
1116         }
1117
1118         e->op_type = UNUSED_SHARE_MODE_ENTRY;
1119         lck->modified = True;
1120         return True;
1121 }
1122
1123 void del_deferred_open_entry(struct share_mode_lock *lck, uint16 mid)
1124 {
1125         struct share_mode_entry entry, *e;
1126
1127         fill_deferred_open_entry(&entry, timeval_zero(),
1128                                  lck->id, mid);
1129
1130         e = find_share_mode_entry(lck, &entry);
1131         if (e == NULL) {
1132                 return;
1133         }
1134
1135         e->op_type = UNUSED_SHARE_MODE_ENTRY;
1136         lck->modified = True;
1137 }
1138
1139 /*******************************************************************
1140  Remove an oplock mid and mode entry from a share mode.
1141 ********************************************************************/
1142
1143 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1144 {
1145         struct share_mode_entry entry, *e;
1146
1147         /* Don't care about the pid owner being correct here - just a search. */
1148         fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1149
1150         e = find_share_mode_entry(lck, &entry);
1151         if (e == NULL) {
1152                 return False;
1153         }
1154
1155         e->op_mid = 0;
1156         e->op_type = NO_OPLOCK;
1157         lck->modified = True;
1158         return True;
1159 }
1160
1161 /*******************************************************************
1162  Downgrade a oplock type from exclusive to level II.
1163 ********************************************************************/
1164
1165 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1166 {
1167         struct share_mode_entry entry, *e;
1168
1169         /* Don't care about the pid owner being correct here - just a search. */
1170         fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1171
1172         e = find_share_mode_entry(lck, &entry);
1173         if (e == NULL) {
1174                 return False;
1175         }
1176
1177         e->op_type = LEVEL_II_OPLOCK;
1178         lck->modified = True;
1179         return True;
1180 }
1181
1182 /****************************************************************************
1183  Deal with the internal needs of setting the delete on close flag. Note that
1184  as the tdb locking is recursive, it is safe to call this from within 
1185  open_file_ntcreate. JRA.
1186 ****************************************************************************/
1187
1188 NTSTATUS can_set_delete_on_close(files_struct *fsp, bool delete_on_close,
1189                                  uint32 dosmode)
1190 {
1191         if (!delete_on_close) {
1192                 return NT_STATUS_OK;
1193         }
1194
1195         /*
1196          * Only allow delete on close for writable files.
1197          */
1198
1199         if ((dosmode & aRONLY) &&
1200             !lp_delete_readonly(SNUM(fsp->conn))) {
1201                 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1202                           "flag set but file attribute is readonly.\n",
1203                           fsp->fsp_name ));
1204                 return NT_STATUS_CANNOT_DELETE;
1205         }
1206
1207         /*
1208          * Only allow delete on close for writable shares.
1209          */
1210
1211         if (!CAN_WRITE(fsp->conn)) {
1212                 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1213                           "close flag set but write access denied on share.\n",
1214                           fsp->fsp_name ));
1215                 return NT_STATUS_ACCESS_DENIED;
1216         }
1217
1218         /*
1219          * Only allow delete on close for files/directories opened with delete
1220          * intent.
1221          */
1222
1223         if (!(fsp->access_mask & DELETE_ACCESS)) {
1224                 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1225                           "close flag set but delete access denied.\n",
1226                           fsp->fsp_name ));
1227                 return NT_STATUS_ACCESS_DENIED;
1228         }
1229
1230         /* Don't allow delete on close for non-empty directories. */
1231         if (fsp->is_directory) {
1232                 return can_delete_directory(fsp->conn, fsp->fsp_name);
1233         }
1234
1235         return NT_STATUS_OK;
1236 }
1237
1238 /****************************************************************************
1239  Do we have an open file handle that created this entry ?
1240 ****************************************************************************/
1241
1242 bool can_set_initial_delete_on_close(const struct share_mode_lock *lck)
1243 {
1244         int i;
1245
1246         for (i=0; i<lck->num_share_modes; i++) {
1247                 if (lck->share_modes[i].flags & SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE) {
1248                         return True;
1249                 }
1250         }
1251         return False;
1252 }
1253
1254 /*************************************************************************
1255  Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1256  (Should this be in locking.c.... ?).
1257 *************************************************************************/
1258
1259 static UNIX_USER_TOKEN *copy_unix_token(TALLOC_CTX *ctx, UNIX_USER_TOKEN *tok)
1260 {
1261         UNIX_USER_TOKEN *cpy;
1262
1263         if (tok == NULL) {
1264                 return NULL;
1265         }
1266
1267         cpy = TALLOC_P(ctx, UNIX_USER_TOKEN);
1268         if (!cpy) {
1269                 return NULL;
1270         }
1271
1272         cpy->uid = tok->uid;
1273         cpy->gid = tok->gid;
1274         cpy->ngroups = tok->ngroups;
1275         if (tok->ngroups) {
1276                 /* Make this a talloc child of cpy. */
1277                 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1278                 if (!cpy->groups) {
1279                         return NULL;
1280                 }
1281                 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1282         }
1283         return cpy;
1284 }
1285
1286 /****************************************************************************
1287  Replace the delete on close token.
1288 ****************************************************************************/
1289
1290 void set_delete_on_close_token(struct share_mode_lock *lck, UNIX_USER_TOKEN *tok)
1291 {
1292         /* Ensure there's no token. */
1293         if (lck->delete_token) {
1294                 TALLOC_FREE(lck->delete_token); /* Also deletes groups... */
1295                 lck->delete_token = NULL;
1296         }
1297
1298         /* Copy the new token (can be NULL). */
1299         lck->delete_token = copy_unix_token(lck, tok);
1300         lck->modified = True;
1301 }
1302
1303 /****************************************************************************
1304  Sets the delete on close flag over all share modes on this file.
1305  Modify the share mode entry for all files open
1306  on this device and inode to tell other smbds we have
1307  changed the delete on close flag. This will be noticed
1308  in the close code, the last closer will delete the file
1309  if flag is set.
1310  This makes a copy of any UNIX_USER_TOKEN into the
1311  lck entry. This function is used when the lock is already granted.
1312 ****************************************************************************/
1313
1314 void set_delete_on_close_lck(struct share_mode_lock *lck, bool delete_on_close, UNIX_USER_TOKEN *tok)
1315 {
1316         if (lck->delete_on_close != delete_on_close) {
1317                 set_delete_on_close_token(lck, tok);
1318                 lck->delete_on_close = delete_on_close;
1319                 if (delete_on_close) {
1320                         SMB_ASSERT(lck->delete_token != NULL);
1321                 }
1322                 lck->modified = True;
1323         }
1324 }
1325
1326 bool set_delete_on_close(files_struct *fsp, bool delete_on_close, UNIX_USER_TOKEN *tok)
1327 {
1328         struct share_mode_lock *lck;
1329         
1330         DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1331                   "fnum = %d, file %s\n",
1332                   delete_on_close ? "Adding" : "Removing", fsp->fnum,
1333                   fsp->fsp_name ));
1334
1335         if (fsp->is_stat) {
1336                 return True;
1337         }
1338
1339         lck = get_share_mode_lock(NULL, fsp->file_id, NULL, NULL);
1340         if (lck == NULL) {
1341                 return False;
1342         }
1343
1344         set_delete_on_close_lck(lck, delete_on_close, tok);
1345
1346         if (fsp->is_directory) {
1347                 send_stat_cache_delete_message(fsp->fsp_name);
1348         }
1349
1350         TALLOC_FREE(lck);
1351         return True;
1352 }
1353
1354 /****************************************************************************
1355  Sets the allow initial delete on close flag for this share mode.
1356 ****************************************************************************/
1357
1358 bool set_allow_initial_delete_on_close(struct share_mode_lock *lck, files_struct *fsp, bool delete_on_close)
1359 {
1360         struct share_mode_entry entry, *e;
1361
1362         /* Don't care about the pid owner being correct here - just a search. */
1363         fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1364
1365         e = find_share_mode_entry(lck, &entry);
1366         if (e == NULL) {
1367                 return False;
1368         }
1369
1370         if (delete_on_close) {
1371                 e->flags |= SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE;
1372         } else {
1373                 e->flags &= ~SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE;
1374         }
1375         lck->modified = True;
1376         return True;
1377 }
1378
1379 struct forall_state {
1380         void (*fn)(const struct share_mode_entry *entry,
1381                    const char *sharepath,
1382                    const char *fname,
1383                    void *private_data);
1384         void *private_data;
1385 };
1386
1387 static int traverse_fn(struct db_record *rec, void *_state)
1388 {
1389         struct forall_state *state = (struct forall_state *)_state;
1390         struct locking_data *data;
1391         struct share_mode_entry *shares;
1392         const char *sharepath;
1393         const char *fname;
1394         int i;
1395
1396         /* Ensure this is a locking_key record. */
1397         if (rec->key.dsize != sizeof(struct file_id))
1398                 return 0;
1399
1400         data = (struct locking_data *)rec->value.dptr;
1401         shares = (struct share_mode_entry *)(rec->value.dptr + sizeof(*data));
1402         sharepath = (const char *)rec->value.dptr + sizeof(*data) +
1403                 data->u.s.num_share_mode_entries*sizeof(*shares) +
1404                 data->u.s.delete_token_size;
1405         fname = (const char *)rec->value.dptr + sizeof(*data) +
1406                 data->u.s.num_share_mode_entries*sizeof(*shares) +
1407                 data->u.s.delete_token_size +
1408                 strlen(sharepath) + 1;
1409
1410         for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1411                 state->fn(&shares[i], sharepath, fname,
1412                           state->private_data);
1413         }
1414         return 0;
1415 }
1416
1417 /*******************************************************************
1418  Call the specified function on each entry under management by the
1419  share mode system.
1420 ********************************************************************/
1421
1422 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1423                                  const char *, void *),
1424                       void *private_data)
1425 {
1426         struct forall_state state;
1427
1428         if (lock_db == NULL)
1429                 return 0;
1430
1431         state.fn = fn;
1432         state.private_data = private_data;
1433
1434         return lock_db->traverse_read(lock_db, traverse_fn, (void *)&state);
1435 }