smbd: move printfile_offset() within write_file()
[Samba/ekacnet.git] / source3 / locking / locking.c
blob3975a78673c6521e38333c7ab1706bc72bc6bb57
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
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.
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.
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/>.
21 Revision History:
23 12 aug 96: Erik.Devriendt@te6.siemens.be
24 added support for shared memory implementation of share mode locking
26 May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
27 locking to deal with multiple share modes per open file.
29 September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
30 support.
32 rewrtten completely to use new tdb code. Tridge, Dec '99
34 Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
35 Added Unix Extensions POSIX locking support. Jeremy Allison Mar 2006.
38 #include "includes.h"
40 #undef DBGC_CLASS
41 #define DBGC_CLASS DBGC_LOCKING
43 #define NO_LOCKING_COUNT (-1)
45 /* the locking database handle */
46 static struct db_context *lock_db;
48 /****************************************************************************
49 Debugging aids :-).
50 ****************************************************************************/
52 const char *lock_type_name(enum brl_type lock_type)
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";
68 const char *lock_flav_name(enum brl_flavour lock_flav)
70 return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
73 /****************************************************************************
74 Utility function called to see if a file region is locked.
75 Called in the read/write codepath.
76 ****************************************************************************/
78 void init_strict_lock_struct(files_struct *fsp,
79 uint32 smbpid,
80 br_off start,
81 br_off size,
82 enum brl_type lock_type,
83 struct lock_struct *plock)
85 SMB_ASSERT(lock_type == READ_LOCK || lock_type == WRITE_LOCK);
87 plock->context.smbpid = smbpid;
88 plock->context.tid = fsp->conn->cnum;
89 plock->context.pid = procid_self();
90 plock->start = start;
91 plock->size = size;
92 plock->fnum = fsp->fnum;
93 plock->lock_type = lock_type;
94 plock->lock_flav = lp_posix_cifsu_locktype(fsp);
97 bool strict_lock_default(files_struct *fsp, struct lock_struct *plock)
99 int strict_locking = lp_strict_locking(fsp->conn->params);
100 bool ret = False;
102 if (plock->size == 0) {
103 return True;
106 if (!lp_locking(fsp->conn->params) || !strict_locking) {
107 return True;
110 if (strict_locking == Auto) {
111 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (plock->lock_type == READ_LOCK || plock->lock_type == WRITE_LOCK)) {
112 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp_str_dbg(fsp)));
113 ret = True;
114 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
115 (plock->lock_type == READ_LOCK)) {
116 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp_str_dbg(fsp)));
117 ret = True;
118 } else {
119 struct byte_range_lock *br_lck;
121 br_lck = brl_get_locks_readonly(fsp);
122 if (!br_lck) {
123 return True;
125 ret = brl_locktest(br_lck,
126 plock->context.smbpid,
127 plock->context.pid,
128 plock->start,
129 plock->size,
130 plock->lock_type,
131 plock->lock_flav);
133 } else {
134 struct byte_range_lock *br_lck;
136 br_lck = brl_get_locks_readonly(fsp);
137 if (!br_lck) {
138 return True;
140 ret = brl_locktest(br_lck,
141 plock->context.smbpid,
142 plock->context.pid,
143 plock->start,
144 plock->size,
145 plock->lock_type,
146 plock->lock_flav);
149 DEBUG(10,("strict_lock_default: flavour = %s brl start=%.0f "
150 "len=%.0f %s for fnum %d file %s\n",
151 lock_flav_name(plock->lock_flav),
152 (double)plock->start, (double)plock->size,
153 ret ? "unlocked" : "locked",
154 plock->fnum, fsp_str_dbg(fsp)));
156 return ret;
159 void strict_unlock_default(files_struct *fsp, struct lock_struct *plock)
163 /****************************************************************************
164 Find out if a lock could be granted - return who is blocking us if we can't.
165 ****************************************************************************/
167 NTSTATUS query_lock(files_struct *fsp,
168 uint32 *psmbpid,
169 uint64_t *pcount,
170 uint64_t *poffset,
171 enum brl_type *plock_type,
172 enum brl_flavour lock_flav)
174 struct byte_range_lock *br_lck = NULL;
176 if (!fsp->can_lock) {
177 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
180 if (!lp_locking(fsp->conn->params)) {
181 return NT_STATUS_OK;
184 br_lck = brl_get_locks_readonly(fsp);
185 if (!br_lck) {
186 return NT_STATUS_NO_MEMORY;
189 return brl_lockquery(br_lck,
190 psmbpid,
191 procid_self(),
192 poffset,
193 pcount,
194 plock_type,
195 lock_flav);
198 static void increment_current_lock_count(files_struct *fsp,
199 enum brl_flavour lock_flav)
201 if (lock_flav == WINDOWS_LOCK &&
202 fsp->current_lock_count != NO_LOCKING_COUNT) {
203 /* blocking ie. pending, locks also count here,
204 * as this is an efficiency counter to avoid checking
205 * the lock db. on close. JRA. */
207 fsp->current_lock_count++;
208 } else {
209 /* Notice that this has had a POSIX lock request.
210 * We can't count locks after this so forget them.
212 fsp->current_lock_count = NO_LOCKING_COUNT;
216 static void decrement_current_lock_count(files_struct *fsp,
217 enum brl_flavour lock_flav)
219 if (lock_flav == WINDOWS_LOCK &&
220 fsp->current_lock_count != NO_LOCKING_COUNT) {
221 SMB_ASSERT(fsp->current_lock_count > 0);
222 fsp->current_lock_count--;
226 /****************************************************************************
227 Utility function called by locking requests.
228 ****************************************************************************/
230 struct byte_range_lock *do_lock(struct messaging_context *msg_ctx,
231 files_struct *fsp,
232 uint32 lock_pid,
233 uint64_t count,
234 uint64_t offset,
235 enum brl_type lock_type,
236 enum brl_flavour lock_flav,
237 bool blocking_lock,
238 NTSTATUS *perr,
239 uint32 *plock_pid,
240 struct blocking_lock_record *blr)
242 struct byte_range_lock *br_lck = NULL;
244 /* silently return ok on print files as we don't do locking there */
245 if (fsp->print_file) {
246 *perr = NT_STATUS_OK;
247 return NULL;
250 if (!fsp->can_lock) {
251 *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
252 return NULL;
255 if (!lp_locking(fsp->conn->params)) {
256 *perr = NT_STATUS_OK;
257 return NULL;
260 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
262 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f "
263 "blocking_lock=%s requested for fnum %d file %s\n",
264 lock_flav_name(lock_flav), lock_type_name(lock_type),
265 (double)offset, (double)count, blocking_lock ? "true" :
266 "false", fsp->fnum, fsp_str_dbg(fsp)));
268 br_lck = brl_get_locks(talloc_tos(), fsp);
269 if (!br_lck) {
270 *perr = NT_STATUS_NO_MEMORY;
271 return NULL;
274 *perr = brl_lock(msg_ctx,
275 br_lck,
276 lock_pid,
277 procid_self(),
278 offset,
279 count,
280 lock_type,
281 lock_flav,
282 blocking_lock,
283 plock_pid,
284 blr);
286 DEBUG(10, ("do_lock: returning status=%s\n", nt_errstr(*perr)));
288 increment_current_lock_count(fsp, lock_flav);
289 return br_lck;
292 /****************************************************************************
293 Utility function called by unlocking requests.
294 ****************************************************************************/
296 NTSTATUS do_unlock(struct messaging_context *msg_ctx,
297 files_struct *fsp,
298 uint32 lock_pid,
299 uint64_t count,
300 uint64_t offset,
301 enum brl_flavour lock_flav)
303 bool ok = False;
304 struct byte_range_lock *br_lck = NULL;
306 if (!fsp->can_lock) {
307 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
310 if (!lp_locking(fsp->conn->params)) {
311 return NT_STATUS_OK;
314 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
315 (double)offset, (double)count, fsp->fnum,
316 fsp_str_dbg(fsp)));
318 br_lck = brl_get_locks(talloc_tos(), fsp);
319 if (!br_lck) {
320 return NT_STATUS_NO_MEMORY;
323 ok = brl_unlock(msg_ctx,
324 br_lck,
325 lock_pid,
326 procid_self(),
327 offset,
328 count,
329 lock_flav);
331 TALLOC_FREE(br_lck);
333 if (!ok) {
334 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
335 return NT_STATUS_RANGE_NOT_LOCKED;
338 decrement_current_lock_count(fsp, lock_flav);
339 return NT_STATUS_OK;
342 /****************************************************************************
343 Cancel any pending blocked locks.
344 ****************************************************************************/
346 NTSTATUS do_lock_cancel(files_struct *fsp,
347 uint32 lock_pid,
348 uint64_t count,
349 uint64_t offset,
350 enum brl_flavour lock_flav,
351 struct blocking_lock_record *blr)
353 bool ok = False;
354 struct byte_range_lock *br_lck = NULL;
356 if (!fsp->can_lock) {
357 return fsp->is_directory ?
358 NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
361 if (!lp_locking(fsp->conn->params)) {
362 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
365 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
366 (double)offset, (double)count, fsp->fnum,
367 fsp_str_dbg(fsp)));
369 br_lck = brl_get_locks(talloc_tos(), fsp);
370 if (!br_lck) {
371 return NT_STATUS_NO_MEMORY;
374 ok = brl_lock_cancel(br_lck,
375 lock_pid,
376 procid_self(),
377 offset,
378 count,
379 lock_flav,
380 blr);
382 TALLOC_FREE(br_lck);
384 if (!ok) {
385 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
386 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
389 decrement_current_lock_count(fsp, lock_flav);
390 return NT_STATUS_OK;
393 /****************************************************************************
394 Remove any locks on this fd. Called from file_close().
395 ****************************************************************************/
397 void locking_close_file(struct messaging_context *msg_ctx,
398 files_struct *fsp)
400 struct byte_range_lock *br_lck;
402 if (!lp_locking(fsp->conn->params)) {
403 return;
406 /* If we have not outstanding locks or pending
407 * locks then we don't need to look in the lock db.
410 if (fsp->current_lock_count == 0) {
411 return;
414 br_lck = brl_get_locks(talloc_tos(),fsp);
416 if (br_lck) {
417 cancel_pending_lock_requests_by_fid(fsp, br_lck);
418 brl_close_fnum(msg_ctx, br_lck);
419 TALLOC_FREE(br_lck);
423 /****************************************************************************
424 Initialise the locking functions.
425 ****************************************************************************/
427 static bool locking_init_internal(bool read_only)
429 brl_init(read_only);
431 if (lock_db)
432 return True;
434 lock_db = db_open(NULL, lock_path("locking.tdb"),
435 lp_open_files_db_hash_size(),
436 TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST,
437 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644);
439 if (!lock_db) {
440 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
441 return False;
444 if (!posix_locking_init(read_only))
445 return False;
447 return True;
450 bool locking_init(void)
452 return locking_init_internal(false);
455 bool locking_init_readonly(void)
457 return locking_init_internal(true);
460 /*******************************************************************
461 Deinitialize the share_mode management.
462 ******************************************************************/
464 bool locking_end(void)
466 brl_shutdown();
467 TALLOC_FREE(lock_db);
468 return true;
471 /*******************************************************************
472 Form a static locking key for a dev/inode pair.
473 ******************************************************************/
475 static TDB_DATA locking_key(const struct file_id *id, struct file_id *tmp)
477 *tmp = *id;
478 return make_tdb_data((const uint8_t *)tmp, sizeof(*tmp));
481 /*******************************************************************
482 Print out a share mode.
483 ********************************************************************/
485 char *share_mode_str(TALLOC_CTX *ctx, int num, const struct share_mode_entry *e)
487 return talloc_asprintf(ctx, "share_mode_entry[%d]: %s "
488 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
489 "access_mask = 0x%x, mid = 0x%llx, type= 0x%x, gen_id = %lu, "
490 "uid = %u, flags = %u, file_id %s",
491 num,
492 e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
493 procid_str_static(&e->pid),
494 e->share_access, e->private_options,
495 e->access_mask, (unsigned long long)e->op_mid,
496 e->op_type, e->share_file_id,
497 (unsigned int)e->uid, (unsigned int)e->flags,
498 file_id_string_tos(&e->id));
501 /*******************************************************************
502 Print out a share mode table.
503 ********************************************************************/
505 static void print_share_mode_table(struct locking_data *data)
507 int num_share_modes = data->u.s.num_share_mode_entries;
508 struct share_mode_entry *shares =
509 (struct share_mode_entry *)(data + 1);
510 int i;
512 for (i = 0; i < num_share_modes; i++) {
513 struct share_mode_entry entry;
514 char *str;
517 * We need to memcpy the entry here due to alignment
518 * restrictions that are not met when directly accessing
519 * shares[i]
522 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
523 str = share_mode_str(talloc_tos(), i, &entry);
525 DEBUG(10,("print_share_mode_table: %s\n", str ? str : ""));
526 TALLOC_FREE(str);
530 /*******************************************************************
531 Get all share mode entries for a dev/inode pair.
532 ********************************************************************/
534 static bool parse_share_modes(const TDB_DATA dbuf, struct share_mode_lock *lck)
536 struct locking_data data;
537 int i;
539 if (dbuf.dsize < sizeof(struct locking_data)) {
540 smb_panic("parse_share_modes: buffer too short");
543 memcpy(&data, dbuf.dptr, sizeof(data));
545 lck->delete_on_close = data.u.s.delete_on_close;
546 lck->old_write_time = data.u.s.old_write_time;
547 lck->changed_write_time = data.u.s.changed_write_time;
548 lck->num_share_modes = data.u.s.num_share_mode_entries;
550 DEBUG(10, ("parse_share_modes: delete_on_close: %d, owrt: %s, "
551 "cwrt: %s, tok: %u, num_share_modes: %d\n",
552 lck->delete_on_close,
553 timestring(talloc_tos(),
554 convert_timespec_to_time_t(lck->old_write_time)),
555 timestring(talloc_tos(),
556 convert_timespec_to_time_t(
557 lck->changed_write_time)),
558 (unsigned int)data.u.s.delete_token_size,
559 lck->num_share_modes));
561 if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
562 DEBUG(0, ("invalid number of share modes: %d\n",
563 lck->num_share_modes));
564 smb_panic("parse_share_modes: invalid number of share modes");
567 lck->share_modes = NULL;
569 if (lck->num_share_modes != 0) {
571 if (dbuf.dsize < (sizeof(struct locking_data) +
572 (lck->num_share_modes *
573 sizeof(struct share_mode_entry)))) {
574 smb_panic("parse_share_modes: buffer too short");
577 lck->share_modes = (struct share_mode_entry *)
578 TALLOC_MEMDUP(lck,
579 dbuf.dptr+sizeof(struct locking_data),
580 lck->num_share_modes *
581 sizeof(struct share_mode_entry));
583 if (lck->share_modes == NULL) {
584 smb_panic("parse_share_modes: talloc failed");
588 /* Get any delete token. */
589 if (data.u.s.delete_token_size) {
590 uint8 *p = dbuf.dptr + sizeof(struct locking_data) +
591 (lck->num_share_modes *
592 sizeof(struct share_mode_entry));
594 if ((data.u.s.delete_token_size < sizeof(uid_t) + sizeof(gid_t)) ||
595 ((data.u.s.delete_token_size - sizeof(uid_t)) % sizeof(gid_t)) != 0) {
596 DEBUG(0, ("parse_share_modes: invalid token size %d\n",
597 data.u.s.delete_token_size));
598 smb_panic("parse_share_modes: invalid token size");
601 lck->delete_token = TALLOC_P(lck, UNIX_USER_TOKEN);
602 if (!lck->delete_token) {
603 smb_panic("parse_share_modes: talloc failed");
606 /* Copy out the uid and gid. */
607 memcpy(&lck->delete_token->uid, p, sizeof(uid_t));
608 p += sizeof(uid_t);
609 memcpy(&lck->delete_token->gid, p, sizeof(gid_t));
610 p += sizeof(gid_t);
612 /* Any supplementary groups ? */
613 lck->delete_token->ngroups = (data.u.s.delete_token_size > (sizeof(uid_t) + sizeof(gid_t))) ?
614 ((data.u.s.delete_token_size -
615 (sizeof(uid_t) + sizeof(gid_t)))/sizeof(gid_t)) : 0;
617 if (lck->delete_token->ngroups) {
618 /* Make this a talloc child of lck->delete_token. */
619 lck->delete_token->groups = TALLOC_ARRAY(lck->delete_token, gid_t,
620 lck->delete_token->ngroups);
621 if (!lck->delete_token) {
622 smb_panic("parse_share_modes: talloc failed");
625 for (i = 0; i < lck->delete_token->ngroups; i++) {
626 memcpy(&lck->delete_token->groups[i], p, sizeof(gid_t));
627 p += sizeof(gid_t);
631 } else {
632 lck->delete_token = NULL;
635 /* Save off the associated service path and filename. */
636 lck->servicepath = (const char *)dbuf.dptr + sizeof(struct locking_data) +
637 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
638 data.u.s.delete_token_size;
640 lck->base_name = (const char *)dbuf.dptr + sizeof(struct locking_data) +
641 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
642 data.u.s.delete_token_size +
643 strlen(lck->servicepath) + 1;
645 lck->stream_name = (const char *)dbuf.dptr + sizeof(struct locking_data) +
646 (lck->num_share_modes * sizeof(struct share_mode_entry)) +
647 data.u.s.delete_token_size +
648 strlen(lck->servicepath) + 1 +
649 strlen(lck->base_name) + 1;
652 * Ensure that each entry has a real process attached.
655 for (i = 0; i < lck->num_share_modes; i++) {
656 struct share_mode_entry *entry_p = &lck->share_modes[i];
657 char *str = NULL;
658 if (DEBUGLEVEL >= 10) {
659 str = share_mode_str(NULL, i, entry_p);
661 DEBUG(10,("parse_share_modes: %s\n",
662 str ? str : ""));
663 if (!serverid_exists(&entry_p->pid)) {
664 DEBUG(10,("parse_share_modes: deleted %s\n",
665 str ? str : ""));
666 entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
667 lck->modified = True;
669 TALLOC_FREE(str);
672 return True;
675 static TDB_DATA unparse_share_modes(const struct share_mode_lock *lck)
677 TDB_DATA result;
678 int num_valid = 0;
679 int i;
680 struct locking_data *data;
681 ssize_t offset;
682 ssize_t sp_len, bn_len, sn_len;
683 uint32 delete_token_size;
685 result.dptr = NULL;
686 result.dsize = 0;
688 for (i=0; i<lck->num_share_modes; i++) {
689 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
690 num_valid += 1;
694 if (num_valid == 0) {
695 return result;
698 sp_len = strlen(lck->servicepath);
699 bn_len = strlen(lck->base_name);
700 sn_len = lck->stream_name != NULL ? strlen(lck->stream_name) : 0;
702 delete_token_size = (lck->delete_token ?
703 (sizeof(uid_t) + sizeof(gid_t) + (lck->delete_token->ngroups*sizeof(gid_t))) : 0);
705 result.dsize = sizeof(*data) +
706 lck->num_share_modes * sizeof(struct share_mode_entry) +
707 delete_token_size +
708 sp_len + 1 +
709 bn_len + 1 +
710 sn_len + 1;
711 result.dptr = TALLOC_ARRAY(lck, uint8, result.dsize);
713 if (result.dptr == NULL) {
714 smb_panic("talloc failed");
717 data = (struct locking_data *)result.dptr;
718 ZERO_STRUCTP(data);
719 data->u.s.num_share_mode_entries = lck->num_share_modes;
720 data->u.s.delete_on_close = lck->delete_on_close;
721 data->u.s.old_write_time = lck->old_write_time;
722 data->u.s.changed_write_time = lck->changed_write_time;
723 data->u.s.delete_token_size = delete_token_size;
725 DEBUG(10,("unparse_share_modes: del: %d, owrt: %s cwrt: %s, tok: %u, "
726 "num: %d\n", data->u.s.delete_on_close,
727 timestring(talloc_tos(),
728 convert_timespec_to_time_t(lck->old_write_time)),
729 timestring(talloc_tos(),
730 convert_timespec_to_time_t(
731 lck->changed_write_time)),
732 (unsigned int)data->u.s.delete_token_size,
733 data->u.s.num_share_mode_entries));
735 memcpy(result.dptr + sizeof(*data), lck->share_modes,
736 sizeof(struct share_mode_entry)*lck->num_share_modes);
737 offset = sizeof(*data) +
738 sizeof(struct share_mode_entry)*lck->num_share_modes;
740 /* Store any delete on close token. */
741 if (lck->delete_token) {
742 uint8 *p = result.dptr + offset;
744 memcpy(p, &lck->delete_token->uid, sizeof(uid_t));
745 p += sizeof(uid_t);
747 memcpy(p, &lck->delete_token->gid, sizeof(gid_t));
748 p += sizeof(gid_t);
750 for (i = 0; i < lck->delete_token->ngroups; i++) {
751 memcpy(p, &lck->delete_token->groups[i], sizeof(gid_t));
752 p += sizeof(gid_t);
754 offset = p - result.dptr;
757 safe_strcpy((char *)result.dptr + offset, lck->servicepath,
758 result.dsize - offset - 1);
759 offset += sp_len + 1;
760 safe_strcpy((char *)result.dptr + offset, lck->base_name,
761 result.dsize - offset - 1);
762 offset += bn_len + 1;
763 safe_strcpy((char *)result.dptr + offset, lck->stream_name,
764 result.dsize - offset - 1);
766 if (DEBUGLEVEL >= 10) {
767 print_share_mode_table(data);
770 return result;
773 static int share_mode_lock_destructor(struct share_mode_lock *lck)
775 NTSTATUS status;
776 TDB_DATA data;
778 if (!lck->modified) {
779 return 0;
782 data = unparse_share_modes(lck);
784 if (data.dptr == NULL) {
785 if (!lck->fresh) {
786 /* There has been an entry before, delete it */
788 status = lck->record->delete_rec(lck->record);
789 if (!NT_STATUS_IS_OK(status)) {
790 char *errmsg;
792 DEBUG(0, ("delete_rec returned %s\n",
793 nt_errstr(status)));
795 if (asprintf(&errmsg, "could not delete share "
796 "entry: %s\n",
797 nt_errstr(status)) == -1) {
798 smb_panic("could not delete share"
799 "entry");
801 smb_panic(errmsg);
804 goto done;
807 status = lck->record->store(lck->record, data, TDB_REPLACE);
808 if (!NT_STATUS_IS_OK(status)) {
809 char *errmsg;
811 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
813 if (asprintf(&errmsg, "could not store share mode entry: %s",
814 nt_errstr(status)) == -1) {
815 smb_panic("could not store share mode entry");
817 smb_panic(errmsg);
820 done:
822 return 0;
825 static bool fill_share_mode_lock(struct share_mode_lock *lck,
826 struct file_id id,
827 const char *servicepath,
828 const struct smb_filename *smb_fname,
829 TDB_DATA share_mode_data,
830 const struct timespec *old_write_time)
832 /* Ensure we set every field here as the destructor must be
833 valid even if parse_share_modes fails. */
835 lck->servicepath = NULL;
836 lck->base_name = NULL;
837 lck->stream_name = NULL;
838 lck->id = id;
839 lck->num_share_modes = 0;
840 lck->share_modes = NULL;
841 lck->delete_token = NULL;
842 lck->delete_on_close = False;
843 ZERO_STRUCT(lck->old_write_time);
844 ZERO_STRUCT(lck->changed_write_time);
845 lck->fresh = False;
846 lck->modified = False;
848 lck->fresh = (share_mode_data.dptr == NULL);
850 if (lck->fresh) {
851 bool has_stream;
852 if (smb_fname == NULL || servicepath == NULL
853 || old_write_time == NULL) {
854 return False;
857 has_stream = smb_fname->stream_name != NULL;
859 lck->base_name = talloc_strdup(lck, smb_fname->base_name);
860 lck->stream_name = talloc_strdup(lck, smb_fname->stream_name);
861 lck->servicepath = talloc_strdup(lck, servicepath);
862 if (lck->base_name == NULL ||
863 (has_stream && lck->stream_name == NULL) ||
864 lck->servicepath == NULL) {
865 DEBUG(0, ("talloc failed\n"));
866 return False;
868 lck->old_write_time = *old_write_time;
869 } else {
870 if (!parse_share_modes(share_mode_data, lck)) {
871 DEBUG(0, ("Could not parse share modes\n"));
872 return False;
876 return True;
879 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
880 const struct file_id id,
881 const char *servicepath,
882 const struct smb_filename *smb_fname,
883 const struct timespec *old_write_time)
885 struct share_mode_lock *lck;
886 struct file_id tmp;
887 TDB_DATA key = locking_key(&id, &tmp);
889 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
890 DEBUG(0, ("talloc failed\n"));
891 return NULL;
894 if (!(lck->record = lock_db->fetch_locked(lock_db, lck, key))) {
895 DEBUG(3, ("Could not lock share entry\n"));
896 TALLOC_FREE(lck);
897 return NULL;
900 if (!fill_share_mode_lock(lck, id, servicepath, smb_fname,
901 lck->record->value, old_write_time)) {
902 DEBUG(3, ("fill_share_mode_lock failed\n"));
903 TALLOC_FREE(lck);
904 return NULL;
907 talloc_set_destructor(lck, share_mode_lock_destructor);
909 return lck;
912 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
913 const struct file_id id)
915 struct share_mode_lock *lck;
916 struct file_id tmp;
917 TDB_DATA key = locking_key(&id, &tmp);
918 TDB_DATA data;
920 if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
921 DEBUG(0, ("talloc failed\n"));
922 return NULL;
925 if (lock_db->fetch(lock_db, lck, key, &data) == -1) {
926 DEBUG(3, ("Could not fetch share entry\n"));
927 TALLOC_FREE(lck);
928 return NULL;
931 if (!fill_share_mode_lock(lck, id, NULL, NULL, data, NULL)) {
932 DEBUG(10, ("fetch_share_mode_unlocked: no share_mode record "
933 "around (file not open)\n"));
934 TALLOC_FREE(lck);
935 return NULL;
938 return lck;
941 /*******************************************************************
942 Sets the service name and filename for rename.
943 At this point we emit "file renamed" messages to all
944 process id's that have this file open.
945 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
946 ********************************************************************/
948 bool rename_share_filename(struct messaging_context *msg_ctx,
949 struct share_mode_lock *lck,
950 const char *servicepath,
951 const struct smb_filename *smb_fname_dst)
953 size_t sp_len;
954 size_t bn_len;
955 size_t sn_len;
956 size_t msg_len;
957 char *frm = NULL;
958 int i;
959 bool strip_two_chars = false;
960 bool has_stream = smb_fname_dst->stream_name != NULL;
962 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
963 servicepath, smb_fname_dst->base_name));
966 * rename_internal_fsp() and rename_internals() add './' to
967 * head of newname if newname does not contain a '/'.
969 if (smb_fname_dst->base_name[0] &&
970 smb_fname_dst->base_name[1] &&
971 smb_fname_dst->base_name[0] == '.' &&
972 smb_fname_dst->base_name[1] == '/') {
973 strip_two_chars = true;
976 lck->servicepath = talloc_strdup(lck, servicepath);
977 lck->base_name = talloc_strdup(lck, smb_fname_dst->base_name +
978 (strip_two_chars ? 2 : 0));
979 lck->stream_name = talloc_strdup(lck, smb_fname_dst->stream_name);
980 if (lck->base_name == NULL ||
981 (has_stream && lck->stream_name == NULL) ||
982 lck->servicepath == NULL) {
983 DEBUG(0, ("rename_share_filename: talloc failed\n"));
984 return False;
986 lck->modified = True;
988 sp_len = strlen(lck->servicepath);
989 bn_len = strlen(lck->base_name);
990 sn_len = has_stream ? strlen(lck->stream_name) : 0;
992 msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + bn_len + 1 +
993 sn_len + 1;
995 /* Set up the name changed message. */
996 frm = TALLOC_ARRAY(lck, char, msg_len);
997 if (!frm) {
998 return False;
1001 push_file_id_24(frm, &lck->id);
1003 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
1005 safe_strcpy(&frm[24], lck->servicepath, sp_len);
1006 safe_strcpy(&frm[24 + sp_len + 1], lck->base_name, bn_len);
1007 safe_strcpy(&frm[24 + sp_len + 1 + bn_len + 1], lck->stream_name,
1008 sn_len);
1010 /* Send the messages. */
1011 for (i=0; i<lck->num_share_modes; i++) {
1012 struct share_mode_entry *se = &lck->share_modes[i];
1013 if (!is_valid_share_mode_entry(se)) {
1014 continue;
1016 /* But not to ourselves... */
1017 if (procid_is_me(&se->pid)) {
1018 continue;
1021 DEBUG(10,("rename_share_filename: sending rename message to "
1022 "pid %s file_id %s sharepath %s base_name %s "
1023 "stream_name %s\n",
1024 procid_str_static(&se->pid),
1025 file_id_string_tos(&lck->id),
1026 lck->servicepath, lck->base_name,
1027 has_stream ? lck->stream_name : ""));
1029 messaging_send_buf(msg_ctx, se->pid, MSG_SMB_FILE_RENAME,
1030 (uint8 *)frm, msg_len);
1033 return True;
1036 void get_file_infos(struct file_id id,
1037 bool *delete_on_close,
1038 struct timespec *write_time)
1040 struct share_mode_lock *lck;
1042 if (delete_on_close) {
1043 *delete_on_close = false;
1046 if (write_time) {
1047 ZERO_STRUCTP(write_time);
1050 if (!(lck = fetch_share_mode_unlocked(talloc_tos(), id))) {
1051 return;
1054 if (delete_on_close) {
1055 *delete_on_close = lck->delete_on_close;
1058 if (write_time) {
1059 struct timespec wt;
1061 wt = lck->changed_write_time;
1062 if (null_timespec(wt)) {
1063 wt = lck->old_write_time;
1066 *write_time = wt;
1069 TALLOC_FREE(lck);
1072 bool is_valid_share_mode_entry(const struct share_mode_entry *e)
1074 int num_props = 0;
1076 if (e->op_type == UNUSED_SHARE_MODE_ENTRY) {
1077 /* cope with dead entries from the process not
1078 existing. These should not be considered valid,
1079 otherwise we end up doing zero timeout sharing
1080 violation */
1081 return False;
1084 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
1085 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1086 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
1088 SMB_ASSERT(num_props <= 1);
1089 return (num_props != 0);
1092 bool is_deferred_open_entry(const struct share_mode_entry *e)
1094 return (e->op_type == DEFERRED_OPEN_ENTRY);
1097 bool is_unused_share_mode_entry(const struct share_mode_entry *e)
1099 return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
1102 /*******************************************************************
1103 Fill a share mode entry.
1104 ********************************************************************/
1106 static void fill_share_mode_entry(struct share_mode_entry *e,
1107 files_struct *fsp,
1108 uid_t uid, uint64_t mid, uint16 op_type)
1110 ZERO_STRUCTP(e);
1111 e->pid = procid_self();
1112 e->share_access = fsp->share_access;
1113 e->private_options = fsp->fh->private_options;
1114 e->access_mask = fsp->access_mask;
1115 e->op_mid = mid;
1116 e->op_type = op_type;
1117 e->time.tv_sec = fsp->open_time.tv_sec;
1118 e->time.tv_usec = fsp->open_time.tv_usec;
1119 e->id = fsp->file_id;
1120 e->share_file_id = fsp->fh->gen_id;
1121 e->uid = (uint32)uid;
1122 e->flags = fsp->posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0;
1125 static void fill_deferred_open_entry(struct share_mode_entry *e,
1126 const struct timeval request_time,
1127 struct file_id id, uint64_t mid)
1129 ZERO_STRUCTP(e);
1130 e->pid = procid_self();
1131 e->op_mid = mid;
1132 e->op_type = DEFERRED_OPEN_ENTRY;
1133 e->time.tv_sec = request_time.tv_sec;
1134 e->time.tv_usec = request_time.tv_usec;
1135 e->id = id;
1136 e->uid = (uint32)-1;
1137 e->flags = 0;
1140 static void add_share_mode_entry(struct share_mode_lock *lck,
1141 const struct share_mode_entry *entry)
1143 int i;
1145 for (i=0; i<lck->num_share_modes; i++) {
1146 struct share_mode_entry *e = &lck->share_modes[i];
1147 if (is_unused_share_mode_entry(e)) {
1148 *e = *entry;
1149 break;
1153 if (i == lck->num_share_modes) {
1154 /* No unused entry found */
1155 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
1156 &lck->share_modes, &lck->num_share_modes);
1158 lck->modified = True;
1161 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
1162 uid_t uid, uint64_t mid, uint16 op_type)
1164 struct share_mode_entry entry;
1165 fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
1166 add_share_mode_entry(lck, &entry);
1169 void add_deferred_open(struct share_mode_lock *lck, uint64_t mid,
1170 struct timeval request_time,
1171 struct file_id id)
1173 struct share_mode_entry entry;
1174 fill_deferred_open_entry(&entry, request_time, id, mid);
1175 add_share_mode_entry(lck, &entry);
1178 /*******************************************************************
1179 Check if two share mode entries are identical, ignoring oplock
1180 and mid info and desired_access. (Removed paranoia test - it's
1181 not automatically a logic error if they are identical. JRA.)
1182 ********************************************************************/
1184 static bool share_modes_identical(struct share_mode_entry *e1,
1185 struct share_mode_entry *e2)
1187 /* We used to check for e1->share_access == e2->share_access here
1188 as well as the other fields but 2 different DOS or FCB opens
1189 sharing the same share mode entry may validly differ in
1190 fsp->share_access field. */
1192 return (procid_equal(&e1->pid, &e2->pid) &&
1193 file_id_equal(&e1->id, &e2->id) &&
1194 e1->share_file_id == e2->share_file_id );
1197 static bool deferred_open_identical(struct share_mode_entry *e1,
1198 struct share_mode_entry *e2)
1200 return (procid_equal(&e1->pid, &e2->pid) &&
1201 (e1->op_mid == e2->op_mid) &&
1202 file_id_equal(&e1->id, &e2->id));
1205 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1206 struct share_mode_entry *entry)
1208 int i;
1210 for (i=0; i<lck->num_share_modes; i++) {
1211 struct share_mode_entry *e = &lck->share_modes[i];
1212 if (is_valid_share_mode_entry(entry) &&
1213 is_valid_share_mode_entry(e) &&
1214 share_modes_identical(e, entry)) {
1215 return e;
1217 if (is_deferred_open_entry(entry) &&
1218 is_deferred_open_entry(e) &&
1219 deferred_open_identical(e, entry)) {
1220 return e;
1223 return NULL;
1226 /*******************************************************************
1227 Del the share mode of a file for this process. Return the number of
1228 entries left.
1229 ********************************************************************/
1231 bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1233 struct share_mode_entry entry, *e;
1235 /* Don't care about the pid owner being correct here - just a search. */
1236 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1238 e = find_share_mode_entry(lck, &entry);
1239 if (e == NULL) {
1240 return False;
1243 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1244 lck->modified = True;
1245 return True;
1248 void del_deferred_open_entry(struct share_mode_lock *lck, uint64_t mid)
1250 struct share_mode_entry entry, *e;
1252 fill_deferred_open_entry(&entry, timeval_zero(),
1253 lck->id, mid);
1255 e = find_share_mode_entry(lck, &entry);
1256 if (e == NULL) {
1257 return;
1260 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1261 lck->modified = True;
1264 /*******************************************************************
1265 Remove an oplock mid and mode entry from a share mode.
1266 ********************************************************************/
1268 bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1270 struct share_mode_entry entry, *e;
1272 /* Don't care about the pid owner being correct here - just a search. */
1273 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1275 e = find_share_mode_entry(lck, &entry);
1276 if (e == NULL) {
1277 return False;
1280 e->op_mid = 0;
1281 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1283 * Going from exclusive or batch,
1284 * we always go through FAKE_LEVEL_II
1285 * first.
1287 e->op_type = FAKE_LEVEL_II_OPLOCK;
1288 } else {
1289 e->op_type = NO_OPLOCK;
1291 lck->modified = True;
1292 return True;
1295 /*******************************************************************
1296 Downgrade a oplock type from exclusive to level II.
1297 ********************************************************************/
1299 bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1301 struct share_mode_entry entry, *e;
1303 /* Don't care about the pid owner being correct here - just a search. */
1304 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1306 e = find_share_mode_entry(lck, &entry);
1307 if (e == NULL) {
1308 return False;
1311 e->op_type = LEVEL_II_OPLOCK;
1312 lck->modified = True;
1313 return True;
1316 /****************************************************************************
1317 Check if setting delete on close is allowed on this fsp.
1318 ****************************************************************************/
1320 NTSTATUS can_set_delete_on_close(files_struct *fsp, uint32 dosmode)
1323 * Only allow delete on close for writable files.
1326 if ((dosmode & aRONLY) &&
1327 !lp_delete_readonly(SNUM(fsp->conn))) {
1328 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1329 "flag set but file attribute is readonly.\n",
1330 fsp_str_dbg(fsp)));
1331 return NT_STATUS_CANNOT_DELETE;
1335 * Only allow delete on close for writable shares.
1338 if (!CAN_WRITE(fsp->conn)) {
1339 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1340 "close flag set but write access denied on share.\n",
1341 fsp_str_dbg(fsp)));
1342 return NT_STATUS_ACCESS_DENIED;
1346 * Only allow delete on close for files/directories opened with delete
1347 * intent.
1350 if (!(fsp->access_mask & DELETE_ACCESS)) {
1351 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1352 "close flag set but delete access denied.\n",
1353 fsp_str_dbg(fsp)));
1354 return NT_STATUS_ACCESS_DENIED;
1357 /* Don't allow delete on close for non-empty directories. */
1358 if (fsp->is_directory) {
1359 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1360 return can_delete_directory(fsp->conn,
1361 fsp->fsp_name->base_name);
1364 return NT_STATUS_OK;
1367 /*************************************************************************
1368 Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1369 (Should this be in locking.c.... ?).
1370 *************************************************************************/
1372 static UNIX_USER_TOKEN *copy_unix_token(TALLOC_CTX *ctx, const UNIX_USER_TOKEN *tok)
1374 UNIX_USER_TOKEN *cpy;
1376 if (tok == NULL) {
1377 return NULL;
1380 cpy = TALLOC_P(ctx, UNIX_USER_TOKEN);
1381 if (!cpy) {
1382 return NULL;
1385 cpy->uid = tok->uid;
1386 cpy->gid = tok->gid;
1387 cpy->ngroups = tok->ngroups;
1388 if (tok->ngroups) {
1389 /* Make this a talloc child of cpy. */
1390 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1391 if (!cpy->groups) {
1392 return NULL;
1394 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1396 return cpy;
1399 /****************************************************************************
1400 Replace the delete on close token.
1401 ****************************************************************************/
1403 void set_delete_on_close_token(struct share_mode_lock *lck, const UNIX_USER_TOKEN *tok)
1405 TALLOC_FREE(lck->delete_token); /* Also deletes groups... */
1407 /* Copy the new token (can be NULL). */
1408 lck->delete_token = copy_unix_token(lck, tok);
1409 lck->modified = True;
1412 /****************************************************************************
1413 Sets the delete on close flag over all share modes on this file.
1414 Modify the share mode entry for all files open
1415 on this device and inode to tell other smbds we have
1416 changed the delete on close flag. This will be noticed
1417 in the close code, the last closer will delete the file
1418 if flag is set.
1419 This makes a copy of any UNIX_USER_TOKEN into the
1420 lck entry. This function is used when the lock is already granted.
1421 ****************************************************************************/
1423 void set_delete_on_close_lck(struct share_mode_lock *lck, bool delete_on_close, const UNIX_USER_TOKEN *tok)
1425 if (lck->delete_on_close != delete_on_close) {
1426 set_delete_on_close_token(lck, tok);
1427 lck->delete_on_close = delete_on_close;
1428 if (delete_on_close) {
1429 SMB_ASSERT(lck->delete_token != NULL);
1431 lck->modified = True;
1435 bool set_delete_on_close(files_struct *fsp, bool delete_on_close, const UNIX_USER_TOKEN *tok)
1437 struct share_mode_lock *lck;
1439 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1440 "fnum = %d, file %s\n",
1441 delete_on_close ? "Adding" : "Removing", fsp->fnum,
1442 fsp_str_dbg(fsp)));
1444 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
1445 NULL);
1446 if (lck == NULL) {
1447 return False;
1450 set_delete_on_close_lck(lck, delete_on_close, tok);
1452 if (fsp->is_directory) {
1453 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
1454 send_stat_cache_delete_message(fsp->fsp_name->base_name);
1457 TALLOC_FREE(lck);
1459 fsp->delete_on_close = delete_on_close;
1461 return True;
1464 bool set_sticky_write_time(struct file_id fileid, struct timespec write_time)
1466 struct share_mode_lock *lck;
1468 DEBUG(5,("set_sticky_write_time: %s id=%s\n",
1469 timestring(talloc_tos(),
1470 convert_timespec_to_time_t(write_time)),
1471 file_id_string_tos(&fileid)));
1473 lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1474 if (lck == NULL) {
1475 return False;
1478 if (timespec_compare(&lck->changed_write_time, &write_time) != 0) {
1479 lck->modified = True;
1480 lck->changed_write_time = write_time;
1483 TALLOC_FREE(lck);
1484 return True;
1487 bool set_write_time(struct file_id fileid, struct timespec write_time)
1489 struct share_mode_lock *lck;
1491 DEBUG(5,("set_write_time: %s id=%s\n",
1492 timestring(talloc_tos(),
1493 convert_timespec_to_time_t(write_time)),
1494 file_id_string_tos(&fileid)));
1496 lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
1497 if (lck == NULL) {
1498 return False;
1501 if (timespec_compare(&lck->old_write_time, &write_time) != 0) {
1502 lck->modified = True;
1503 lck->old_write_time = write_time;
1506 TALLOC_FREE(lck);
1507 return True;
1511 struct forall_state {
1512 void (*fn)(const struct share_mode_entry *entry,
1513 const char *sharepath,
1514 const char *fname,
1515 void *private_data);
1516 void *private_data;
1519 static int traverse_fn(struct db_record *rec, void *_state)
1521 struct forall_state *state = (struct forall_state *)_state;
1522 struct locking_data *data;
1523 struct share_mode_entry *shares;
1524 const char *sharepath;
1525 const char *fname;
1526 int i;
1528 /* Ensure this is a locking_key record. */
1529 if (rec->key.dsize != sizeof(struct file_id))
1530 return 0;
1532 data = (struct locking_data *)rec->value.dptr;
1533 shares = (struct share_mode_entry *)(rec->value.dptr + sizeof(*data));
1534 sharepath = (const char *)rec->value.dptr + sizeof(*data) +
1535 data->u.s.num_share_mode_entries*sizeof(*shares) +
1536 data->u.s.delete_token_size;
1537 fname = (const char *)rec->value.dptr + sizeof(*data) +
1538 data->u.s.num_share_mode_entries*sizeof(*shares) +
1539 data->u.s.delete_token_size +
1540 strlen(sharepath) + 1;
1542 for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1543 state->fn(&shares[i], sharepath, fname,
1544 state->private_data);
1546 return 0;
1549 /*******************************************************************
1550 Call the specified function on each entry under management by the
1551 share mode system.
1552 ********************************************************************/
1554 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1555 const char *, void *),
1556 void *private_data)
1558 struct forall_state state;
1560 if (lock_db == NULL)
1561 return 0;
1563 state.fn = fn;
1564 state.private_data = private_data;
1566 return lock_db->traverse_read(lock_db, traverse_fn, (void *)&state);