2 Unix SMB/CIFS implementation.
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 12 aug 96: Erik.Devriendt@te6.siemens.be
25 added support for shared memory implementation of share mode locking
27 May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
28 locking to deal with multiple share modes per open file.
30 September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
33 rewrtten completely to use new tdb code. Tridge, Dec '99
35 Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
36 Added Unix Extensions POSIX locking support. Jeremy Allison Mar 2006.
42 #define DBGC_CLASS DBGC_LOCKING
44 /* the locking database handle */
45 static TDB_CONTEXT *tdb;
47 /****************************************************************************
49 ****************************************************************************/
51 const char *lock_type_name(enum brl_type lock_type)
58 case PENDING_READ_LOCK:
59 return "PENDING_READ";
60 case PENDING_WRITE_LOCK:
61 return "PENDING_WRITE";
67 const char *lock_flav_name(enum brl_flavour lock_flav)
69 return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
72 /****************************************************************************
73 Utility function called to see if a file region is locked.
74 Called in the read/write codepath.
75 ****************************************************************************/
77 BOOL is_locked(files_struct *fsp,
81 enum brl_type lock_type)
83 int strict_locking = lp_strict_locking(fsp->conn->params);
84 enum brl_flavour lock_flav = lp_posix_cifsu_locktype(fsp);
91 if (!lp_locking(fsp->conn->params) || !strict_locking) {
95 if (strict_locking == Auto) {
96 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (lock_type == READ_LOCK || lock_type == WRITE_LOCK)) {
97 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp->fsp_name ));
99 } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
100 (lock_type == READ_LOCK)) {
101 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp->fsp_name ));
104 struct byte_range_lock *br_lck = brl_get_locks_readonly(NULL, fsp);
108 ret = !brl_locktest(br_lck,
118 struct byte_range_lock *br_lck = brl_get_locks_readonly(NULL, fsp);
122 ret = !brl_locktest(br_lck,
132 DEBUG(10,("is_locked: flavour = %s brl start=%.0f len=%.0f %s for fnum %d file %s\n",
133 lock_flav_name(lock_flav),
134 (double)offset, (double)count, ret ? "locked" : "unlocked",
135 fsp->fnum, fsp->fsp_name ));
140 /****************************************************************************
141 Find out if a lock could be granted - return who is blocking us if we can't.
142 ****************************************************************************/
144 NTSTATUS query_lock(files_struct *fsp,
146 SMB_BIG_UINT *pcount,
147 SMB_BIG_UINT *poffset,
148 enum brl_type *plock_type,
149 enum brl_flavour lock_flav)
151 struct byte_range_lock *br_lck = NULL;
152 NTSTATUS status = NT_STATUS_LOCK_NOT_GRANTED;
154 if (!fsp->can_lock) {
155 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
158 if (!lp_locking(fsp->conn->params)) {
162 br_lck = brl_get_locks_readonly(NULL, fsp);
164 return NT_STATUS_NO_MEMORY;
167 status = brl_lockquery(br_lck,
179 /****************************************************************************
180 Utility function called by locking requests.
181 ****************************************************************************/
183 struct byte_range_lock *do_lock(files_struct *fsp,
187 enum brl_type lock_type,
188 enum brl_flavour lock_flav,
192 struct byte_range_lock *br_lck = NULL;
194 if (!fsp->can_lock) {
195 *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
199 if (!lp_locking(fsp->conn->params)) {
200 *perr = NT_STATUS_OK;
204 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
206 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f requested for fnum %d file %s\n",
207 lock_flav_name(lock_flav), lock_type_name(lock_type),
208 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
210 br_lck = brl_get_locks(NULL, fsp);
212 *perr = NT_STATUS_NO_MEMORY;
216 *perr = brl_lock(br_lck,
228 /****************************************************************************
229 Utility function called by unlocking requests.
230 ****************************************************************************/
232 NTSTATUS do_unlock(files_struct *fsp,
236 enum brl_flavour lock_flav)
239 struct byte_range_lock *br_lck = NULL;
241 if (!fsp->can_lock) {
242 return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
245 if (!lp_locking(fsp->conn->params)) {
249 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
250 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
252 br_lck = brl_get_locks(NULL, fsp);
254 return NT_STATUS_NO_MEMORY;
257 ok = brl_unlock(br_lck,
267 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
268 return NT_STATUS_RANGE_NOT_LOCKED;
274 /****************************************************************************
275 Cancel any pending blocked locks.
276 ****************************************************************************/
278 NTSTATUS do_lock_cancel(files_struct *fsp,
282 enum brl_flavour lock_flav)
285 struct byte_range_lock *br_lck = NULL;
287 if (!fsp->can_lock) {
288 return fsp->is_directory ?
289 NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
292 if (!lp_locking(fsp->conn->params)) {
293 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
296 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
297 (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
299 br_lck = brl_get_locks(NULL, fsp);
301 return NT_STATUS_NO_MEMORY;
304 ok = brl_lock_cancel(br_lck,
314 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
315 return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
321 /****************************************************************************
322 Remove any locks on this fd. Called from file_close().
323 ****************************************************************************/
325 void locking_close_file(files_struct *fsp)
327 struct byte_range_lock *br_lck;
329 if (!lp_locking(fsp->conn->params)) {
333 br_lck = brl_get_locks(NULL,fsp);
336 cancel_pending_lock_requests_by_fid(fsp, br_lck);
337 brl_close_fnum(br_lck);
342 /****************************************************************************
343 Initialise the locking functions.
344 ****************************************************************************/
346 static int open_read_only;
348 BOOL locking_init(int read_only)
355 tdb = tdb_open_log(lock_path("locking.tdb"),
356 lp_open_files_db_hash_size(),
357 TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST),
358 read_only?O_RDONLY:O_RDWR|O_CREAT,
362 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
366 /* Activate the per-hashchain freelist */
367 tdb_set_max_dead(tdb, 5);
369 if (!posix_locking_init(read_only))
372 open_read_only = read_only;
377 /*******************************************************************
378 Deinitialize the share_mode management.
379 ******************************************************************/
381 BOOL locking_end(void)
385 brl_shutdown(open_read_only);
387 if (tdb_close(tdb) != 0)
394 /*******************************************************************
395 Form a static locking key for a dev/inode pair.
396 ******************************************************************/
398 /* key and data records in the tdb locking database */
404 /*******************************************************************
405 Form a static locking key for a dev/inode pair.
406 ******************************************************************/
408 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
410 static struct locking_key key;
413 memset(&key, '\0', sizeof(key));
416 kbuf.dptr = (char *)&key;
417 kbuf.dsize = sizeof(key);
421 /*******************************************************************
422 Print out a share mode.
423 ********************************************************************/
425 char *share_mode_str(int num, struct share_mode_entry *e)
427 static pstring share_str;
429 slprintf(share_str, sizeof(share_str)-1, "share_mode_entry[%d]: %s "
430 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
431 "access_mask = 0x%x, mid = 0x%x, type= 0x%x, file_id = %lu, "
432 "uid = %u, flags = %u, dev = 0x%x, inode = %.0f",
434 e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
435 procid_str_static(&e->pid),
436 e->share_access, e->private_options,
437 e->access_mask, e->op_mid, e->op_type, e->share_file_id,
438 (unsigned int)e->uid, (unsigned int)e->flags,
439 (unsigned int)e->dev, (double)e->inode );
444 /*******************************************************************
445 Print out a share mode table.
446 ********************************************************************/
448 static void print_share_mode_table(struct locking_data *data)
450 int num_share_modes = data->u.s.num_share_mode_entries;
451 struct share_mode_entry *shares =
452 (struct share_mode_entry *)(data + 1);
455 for (i = 0; i < num_share_modes; i++) {
456 struct share_mode_entry entry;
458 memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
459 DEBUG(10,("print_share_mode_table: %s\n",
460 share_mode_str(i, &entry)));
464 /*******************************************************************
465 Get all share mode entries for a dev/inode pair.
466 ********************************************************************/
468 static BOOL parse_share_modes(TDB_DATA dbuf, struct share_mode_lock *lck)
470 struct locking_data *data;
473 if (dbuf.dsize < sizeof(struct locking_data)) {
474 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
477 data = (struct locking_data *)dbuf.dptr;
479 lck->delete_on_close = data->u.s.delete_on_close;
480 lck->num_share_modes = data->u.s.num_share_mode_entries;
482 DEBUG(10, ("parse_share_modes: delete_on_close: %d, "
483 "num_share_modes: %d\n",
484 lck->delete_on_close,
485 lck->num_share_modes));
487 if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
488 DEBUG(0, ("invalid number of share modes: %d\n",
489 lck->num_share_modes));
490 smb_panic("PANIC: invalid number of share modes");
493 lck->share_modes = NULL;
495 if (lck->num_share_modes != 0) {
497 if (dbuf.dsize < (sizeof(struct locking_data) +
498 (lck->num_share_modes *
499 sizeof(struct share_mode_entry)))) {
500 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
503 lck->share_modes = (struct share_mode_entry *)
504 talloc_memdup(lck, dbuf.dptr+sizeof(*data),
505 lck->num_share_modes *
506 sizeof(struct share_mode_entry));
508 if (lck->share_modes == NULL) {
509 smb_panic("talloc failed\n");
513 /* Get any delete token. */
514 if (data->u.s.delete_token_size) {
515 char *p = dbuf.dptr + sizeof(*data) +
516 (lck->num_share_modes *
517 sizeof(struct share_mode_entry));
519 if ((data->u.s.delete_token_size < sizeof(uid_t) + sizeof(gid_t)) ||
520 ((data->u.s.delete_token_size - sizeof(uid_t)) % sizeof(gid_t)) != 0) {
521 DEBUG(0, ("parse_share_modes: invalid token size %d\n",
522 data->u.s.delete_token_size));
523 smb_panic("parse_share_modes: invalid token size\n");
526 lck->delete_token = TALLOC_P(lck, UNIX_USER_TOKEN);
527 if (!lck->delete_token) {
528 smb_panic("talloc failed\n");
531 /* Copy out the uid and gid. */
532 memcpy(&lck->delete_token->uid, p, sizeof(uid_t));
534 memcpy(&lck->delete_token->gid, p, sizeof(gid_t));
537 /* Any supplementary groups ? */
538 lck->delete_token->ngroups = (data->u.s.delete_token_size > (sizeof(uid_t) + sizeof(gid_t))) ?
539 ((data->u.s.delete_token_size -
540 (sizeof(uid_t) + sizeof(gid_t)))/sizeof(gid_t)) : 0;
542 if (lck->delete_token->ngroups) {
543 /* Make this a talloc child of lck->delete_token. */
544 lck->delete_token->groups = TALLOC_ARRAY(lck->delete_token, gid_t,
545 lck->delete_token->ngroups);
546 if (!lck->delete_token) {
547 smb_panic("talloc failed\n");
550 for (i = 0; i < lck->delete_token->ngroups; i++) {
551 memcpy(&lck->delete_token->groups[i], p, sizeof(gid_t));
557 lck->delete_token = NULL;
560 /* Save off the associated service path and filename. */
561 lck->servicepath = talloc_strdup(lck, dbuf.dptr + sizeof(*data) +
562 (lck->num_share_modes *
563 sizeof(struct share_mode_entry)) +
564 data->u.s.delete_token_size );
565 if (lck->servicepath == NULL) {
566 smb_panic("talloc_strdup failed\n");
569 lck->filename = talloc_strdup(lck, dbuf.dptr + sizeof(*data) +
570 (lck->num_share_modes *
571 sizeof(struct share_mode_entry)) +
572 data->u.s.delete_token_size +
573 strlen(lck->servicepath) + 1 );
574 if (lck->filename == NULL) {
575 smb_panic("talloc_strdup failed\n");
579 * Ensure that each entry has a real process attached.
582 for (i = 0; i < lck->num_share_modes; i++) {
583 struct share_mode_entry *entry_p = &lck->share_modes[i];
584 DEBUG(10,("parse_share_modes: %s\n",
585 share_mode_str(i, entry_p) ));
586 if (!process_exists(entry_p->pid)) {
587 DEBUG(10,("parse_share_modes: deleted %s\n",
588 share_mode_str(i, entry_p) ));
589 entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
590 lck->modified = True;
597 static TDB_DATA unparse_share_modes(struct share_mode_lock *lck)
602 struct locking_data *data;
605 uint32 delete_token_size;
610 for (i=0; i<lck->num_share_modes; i++) {
611 if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
616 if (num_valid == 0) {
620 sp_len = strlen(lck->servicepath);
621 delete_token_size = (lck->delete_token ?
622 (sizeof(uid_t) + sizeof(gid_t) + (lck->delete_token->ngroups*sizeof(gid_t))) : 0);
624 result.dsize = sizeof(*data) +
625 lck->num_share_modes * sizeof(struct share_mode_entry) +
628 strlen(lck->filename) + 1;
629 result.dptr = TALLOC_ARRAY(lck, char, result.dsize);
631 if (result.dptr == NULL) {
632 smb_panic("talloc failed\n");
635 data = (struct locking_data *)result.dptr;
637 data->u.s.num_share_mode_entries = lck->num_share_modes;
638 data->u.s.delete_on_close = lck->delete_on_close;
639 data->u.s.delete_token_size = delete_token_size;
640 DEBUG(10, ("unparse_share_modes: del: %d, tok = %u, num: %d\n",
641 data->u.s.delete_on_close,
642 (unsigned int)data->u.s.delete_token_size,
643 data->u.s.num_share_mode_entries));
644 memcpy(result.dptr + sizeof(*data), lck->share_modes,
645 sizeof(struct share_mode_entry)*lck->num_share_modes);
646 offset = sizeof(*data) +
647 sizeof(struct share_mode_entry)*lck->num_share_modes;
649 /* Store any delete on close token. */
650 if (lck->delete_token) {
651 char *p = result.dptr + offset;
653 memcpy(p, &lck->delete_token->uid, sizeof(uid_t));
656 memcpy(p, &lck->delete_token->gid, sizeof(gid_t));
659 for (i = 0; i < lck->delete_token->ngroups; i++) {
660 memcpy(p, &lck->delete_token->groups[i], sizeof(gid_t));
663 offset = p - result.dptr;
666 safe_strcpy(result.dptr + offset, lck->servicepath,
667 result.dsize - offset - 1);
668 offset += sp_len + 1;
669 safe_strcpy(result.dptr + offset, lck->filename,
670 result.dsize - offset - 1);
672 if (DEBUGLEVEL >= 10) {
673 print_share_mode_table(data);
679 static int share_mode_lock_destructor(struct share_mode_lock *lck)
681 TDB_DATA key = locking_key(lck->dev, lck->ino);
684 if (!lck->modified) {
688 data = unparse_share_modes(lck);
690 if (data.dptr == NULL) {
692 /* There has been an entry before, delete it */
693 if (tdb_delete(tdb, key) == -1) {
694 smb_panic("Could not delete share entry\n");
700 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1) {
701 smb_panic("Could not store share mode entry\n");
705 tdb_chainunlock(tdb, key);
710 struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
711 SMB_DEV_T dev, SMB_INO_T ino,
712 const char *servicepath,
715 struct share_mode_lock *lck;
716 TDB_DATA key = locking_key(dev, ino);
719 lck = TALLOC_P(mem_ctx, struct share_mode_lock);
721 DEBUG(0, ("talloc failed\n"));
725 /* Ensure we set every field here as the destructor must be
726 valid even if parse_share_modes fails. */
728 lck->servicepath = NULL;
729 lck->filename = NULL;
732 lck->num_share_modes = 0;
733 lck->share_modes = NULL;
734 lck->delete_token = NULL;
735 lck->delete_on_close = False;
737 lck->modified = False;
739 if (tdb_chainlock(tdb, key) != 0) {
740 DEBUG(3, ("Could not lock share entry\n"));
745 /* We must set the destructor immediately after the chainlock
746 ensure the lock is cleaned up on any of the error return
749 talloc_set_destructor(lck, share_mode_lock_destructor);
751 data = tdb_fetch(tdb, key);
752 lck->fresh = (data.dptr == NULL);
756 if (fname == NULL || servicepath == NULL) {
760 lck->filename = talloc_strdup(lck, fname);
761 lck->servicepath = talloc_strdup(lck, servicepath);
762 if (lck->filename == NULL || lck->servicepath == NULL) {
763 DEBUG(0, ("talloc failed\n"));
768 if (!parse_share_modes(data, lck)) {
769 DEBUG(0, ("Could not parse share modes\n"));
771 SAFE_FREE(data.dptr);
776 SAFE_FREE(data.dptr);
781 /*******************************************************************
782 Sets the service name and filename for rename.
783 At this point we emit "file renamed" messages to all
784 process id's that have this file open.
785 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
786 ********************************************************************/
788 BOOL rename_share_filename(struct share_mode_lock *lck,
789 const char *servicepath,
802 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
803 servicepath, newname));
806 * rename_internal_fsp() and rename_internals() add './' to
807 * head of newname if newname does not contain a '/'.
809 while (newname[0] && newname[1] && newname[0] == '.' && newname[1] == '/') {
813 lck->servicepath = talloc_strdup(lck, servicepath);
814 lck->filename = talloc_strdup(lck, newname);
815 if (lck->filename == NULL || lck->servicepath == NULL) {
816 DEBUG(0, ("rename_share_filename: talloc failed\n"));
819 lck->modified = True;
821 sp_len = strlen(lck->servicepath);
822 fn_len = strlen(lck->filename);
824 msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + fn_len + 1;
826 /* Set up the name changed message. */
827 frm = TALLOC_ARRAY(lck, char, msg_len);
832 SDEV_T_VAL(frm,0,lck->dev);
833 SINO_T_VAL(frm,8,lck->ino);
835 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
837 safe_strcpy(&frm[16], lck->servicepath, sp_len);
838 safe_strcpy(&frm[16 + sp_len + 1], lck->filename, fn_len);
840 /* Send the messages. */
841 for (i=0; i<lck->num_share_modes; i++) {
842 struct share_mode_entry *se = &lck->share_modes[i];
843 if (!is_valid_share_mode_entry(se)) {
846 /* But not to ourselves... */
847 if (procid_is_me(&se->pid)) {
851 DEBUG(10,("rename_share_filename: sending rename message to pid %s "
852 "dev %x, inode %.0f sharepath %s newname %s\n",
853 procid_str_static(&se->pid),
854 (unsigned int)lck->dev, (double)lck->ino,
855 lck->servicepath, lck->filename ));
857 message_send_pid(se->pid, MSG_SMB_FILE_RENAME,
864 static int pull_delete_on_close_flag(TDB_DATA key, TDB_DATA dbuf,
867 BOOL *result = (BOOL *)private_data;
868 struct locking_data *data;
870 if (dbuf.dsize < sizeof(struct locking_data)) {
871 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
874 data = (struct locking_data *)dbuf.dptr;
876 *result = data->u.s.delete_on_close;
880 BOOL get_delete_on_close_flag(SMB_DEV_T dev, SMB_INO_T inode)
882 TDB_DATA key = locking_key(dev, inode);
885 tdb_parse_record(tdb, key, pull_delete_on_close_flag,
890 BOOL is_valid_share_mode_entry(const struct share_mode_entry *e)
894 num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
895 num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
896 num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
898 SMB_ASSERT(num_props <= 1);
899 return (num_props != 0);
902 BOOL is_deferred_open_entry(const struct share_mode_entry *e)
904 return (e->op_type == DEFERRED_OPEN_ENTRY);
907 BOOL is_unused_share_mode_entry(const struct share_mode_entry *e)
909 return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
912 /*******************************************************************
913 Fill a share mode entry.
914 ********************************************************************/
916 static void fill_share_mode_entry(struct share_mode_entry *e,
918 uid_t uid, uint16 mid, uint16 op_type)
921 e->pid = procid_self();
922 e->share_access = fsp->share_access;
923 e->private_options = fsp->fh->private_options;
924 e->access_mask = fsp->access_mask;
926 e->op_type = op_type;
927 e->time.tv_sec = fsp->open_time.tv_sec;
928 e->time.tv_usec = fsp->open_time.tv_usec;
930 e->inode = fsp->inode;
931 e->share_file_id = fsp->fh->file_id;
932 e->uid = (uint32)uid;
933 e->flags = fsp->posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0;
936 static void fill_deferred_open_entry(struct share_mode_entry *e,
937 const struct timeval request_time,
938 SMB_DEV_T dev, SMB_INO_T ino, uint16 mid)
941 e->pid = procid_self();
943 e->op_type = DEFERRED_OPEN_ENTRY;
944 e->time.tv_sec = request_time.tv_sec;
945 e->time.tv_usec = request_time.tv_usec;
952 static void add_share_mode_entry(struct share_mode_lock *lck,
953 const struct share_mode_entry *entry)
957 for (i=0; i<lck->num_share_modes; i++) {
958 struct share_mode_entry *e = &lck->share_modes[i];
959 if (is_unused_share_mode_entry(e)) {
965 if (i == lck->num_share_modes) {
966 /* No unused entry found */
967 ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
968 &lck->share_modes, &lck->num_share_modes);
970 lck->modified = True;
973 void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
974 uid_t uid, uint16 mid, uint16 op_type)
976 struct share_mode_entry entry;
977 fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
978 add_share_mode_entry(lck, &entry);
981 void add_deferred_open(struct share_mode_lock *lck, uint16 mid,
982 struct timeval request_time,
983 SMB_DEV_T dev, SMB_INO_T ino)
985 struct share_mode_entry entry;
986 fill_deferred_open_entry(&entry, request_time, dev, ino, mid);
987 add_share_mode_entry(lck, &entry);
990 /*******************************************************************
991 Check if two share mode entries are identical, ignoring oplock
992 and mid info and desired_access. (Removed paranoia test - it's
993 not automatically a logic error if they are identical. JRA.)
994 ********************************************************************/
996 static BOOL share_modes_identical(struct share_mode_entry *e1,
997 struct share_mode_entry *e2)
999 /* We used to check for e1->share_access == e2->share_access here
1000 as well as the other fields but 2 different DOS or FCB opens
1001 sharing the same share mode entry may validly differ in
1002 fsp->share_access field. */
1004 return (procid_equal(&e1->pid, &e2->pid) &&
1005 e1->dev == e2->dev &&
1006 e1->inode == e2->inode &&
1007 e1->share_file_id == e2->share_file_id );
1010 static BOOL deferred_open_identical(struct share_mode_entry *e1,
1011 struct share_mode_entry *e2)
1013 return (procid_equal(&e1->pid, &e2->pid) &&
1014 (e1->op_mid == e2->op_mid) &&
1015 (e1->dev == e2->dev) &&
1016 (e1->inode == e2->inode));
1019 static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
1020 struct share_mode_entry *entry)
1024 for (i=0; i<lck->num_share_modes; i++) {
1025 struct share_mode_entry *e = &lck->share_modes[i];
1026 if (is_valid_share_mode_entry(entry) &&
1027 is_valid_share_mode_entry(e) &&
1028 share_modes_identical(e, entry)) {
1031 if (is_deferred_open_entry(entry) &&
1032 is_deferred_open_entry(e) &&
1033 deferred_open_identical(e, entry)) {
1040 /*******************************************************************
1041 Del the share mode of a file for this process. Return the number of
1043 ********************************************************************/
1045 BOOL del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
1047 struct share_mode_entry entry, *e;
1049 /* Don't care about the pid owner being correct here - just a search. */
1050 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1052 e = find_share_mode_entry(lck, &entry);
1057 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1058 lck->modified = True;
1062 void del_deferred_open_entry(struct share_mode_lock *lck, uint16 mid)
1064 struct share_mode_entry entry, *e;
1066 fill_deferred_open_entry(&entry, timeval_zero(),
1067 lck->dev, lck->ino, mid);
1069 e = find_share_mode_entry(lck, &entry);
1074 e->op_type = UNUSED_SHARE_MODE_ENTRY;
1075 lck->modified = True;
1078 /*******************************************************************
1079 Remove an oplock mid and mode entry from a share mode.
1080 ********************************************************************/
1082 BOOL remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1084 struct share_mode_entry entry, *e;
1086 /* Don't care about the pid owner being correct here - just a search. */
1087 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1089 e = find_share_mode_entry(lck, &entry);
1095 e->op_type = NO_OPLOCK;
1096 lck->modified = True;
1100 /*******************************************************************
1101 Downgrade a oplock type from exclusive to level II.
1102 ********************************************************************/
1104 BOOL downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
1106 struct share_mode_entry entry, *e;
1108 /* Don't care about the pid owner being correct here - just a search. */
1109 fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
1111 e = find_share_mode_entry(lck, &entry);
1116 e->op_type = LEVEL_II_OPLOCK;
1117 lck->modified = True;
1121 /****************************************************************************
1122 Deal with the internal needs of setting the delete on close flag. Note that
1123 as the tdb locking is recursive, it is safe to call this from within
1124 open_file_ntcreate. JRA.
1125 ****************************************************************************/
1127 NTSTATUS can_set_delete_on_close(files_struct *fsp, BOOL delete_on_close,
1130 if (!delete_on_close) {
1131 return NT_STATUS_OK;
1135 * Only allow delete on close for writable files.
1138 if ((dosmode & aRONLY) &&
1139 !lp_delete_readonly(SNUM(fsp->conn))) {
1140 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1141 "flag set but file attribute is readonly.\n",
1143 return NT_STATUS_CANNOT_DELETE;
1147 * Only allow delete on close for writable shares.
1150 if (!CAN_WRITE(fsp->conn)) {
1151 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1152 "close flag set but write access denied on share.\n",
1154 return NT_STATUS_ACCESS_DENIED;
1158 * Only allow delete on close for files/directories opened with delete
1162 if (!(fsp->access_mask & DELETE_ACCESS)) {
1163 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1164 "close flag set but delete access denied.\n",
1166 return NT_STATUS_ACCESS_DENIED;
1169 /* Don't allow delete on close for non-empty directories. */
1170 if (fsp->is_directory) {
1171 return can_delete_directory(fsp->conn, fsp->fsp_name);
1174 return NT_STATUS_OK;
1177 /*************************************************************************
1178 Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1179 (Should this be in locking.c.... ?).
1180 *************************************************************************/
1182 static UNIX_USER_TOKEN *copy_unix_token(TALLOC_CTX *ctx, UNIX_USER_TOKEN *tok)
1184 UNIX_USER_TOKEN *cpy;
1190 cpy = TALLOC_P(ctx, UNIX_USER_TOKEN);
1195 cpy->uid = tok->uid;
1196 cpy->gid = tok->gid;
1197 cpy->ngroups = tok->ngroups;
1199 /* Make this a talloc child of cpy. */
1200 cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
1204 memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
1209 /****************************************************************************
1210 Replace the delete on close token.
1211 ****************************************************************************/
1213 void set_delete_on_close_token(struct share_mode_lock *lck, UNIX_USER_TOKEN *tok)
1215 /* Ensure there's no token. */
1216 if (lck->delete_token) {
1217 TALLOC_FREE(lck->delete_token); /* Also deletes groups... */
1218 lck->delete_token = NULL;
1221 /* Copy the new token (can be NULL). */
1222 lck->delete_token = copy_unix_token(lck, tok);
1223 lck->modified = True;
1226 /****************************************************************************
1227 Sets the delete on close flag over all share modes on this file.
1228 Modify the share mode entry for all files open
1229 on this device and inode to tell other smbds we have
1230 changed the delete on close flag. This will be noticed
1231 in the close code, the last closer will delete the file
1233 This makes a copy of any UNIX_USER_TOKEN into the
1234 lck entry. This function is used when the lock is already granted.
1235 ****************************************************************************/
1237 void set_delete_on_close_lck(struct share_mode_lock *lck, BOOL delete_on_close, UNIX_USER_TOKEN *tok)
1239 if (lck->delete_on_close != delete_on_close) {
1240 set_delete_on_close_token(lck, tok);
1241 lck->delete_on_close = delete_on_close;
1242 if (delete_on_close) {
1243 SMB_ASSERT(lck->delete_token != NULL);
1245 lck->modified = True;
1249 BOOL set_delete_on_close(files_struct *fsp, BOOL delete_on_close, UNIX_USER_TOKEN *tok)
1251 struct share_mode_lock *lck;
1253 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1254 "fnum = %d, file %s\n",
1255 delete_on_close ? "Adding" : "Removing", fsp->fnum,
1262 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
1267 set_delete_on_close_lck(lck, delete_on_close, tok);
1269 if (fsp->is_directory) {
1270 send_stat_cache_delete_message(fsp->fsp_name);
1277 struct forall_state {
1278 void (*fn)(const struct share_mode_entry *entry,
1279 const char *sharepath,
1281 void *private_data);
1285 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1288 struct forall_state *state = (struct forall_state *)_state;
1289 struct locking_data *data;
1290 struct share_mode_entry *shares;
1291 const char *sharepath;
1295 /* Ensure this is a locking_key record. */
1296 if (kbuf.dsize != sizeof(struct locking_key))
1299 data = (struct locking_data *)dbuf.dptr;
1300 shares = (struct share_mode_entry *)(dbuf.dptr + sizeof(*data));
1301 sharepath = dbuf.dptr + sizeof(*data) +
1302 data->u.s.num_share_mode_entries*sizeof(*shares) +
1303 data->u.s.delete_token_size;
1304 fname = dbuf.dptr + sizeof(*data) +
1305 data->u.s.num_share_mode_entries*sizeof(*shares) +
1306 data->u.s.delete_token_size +
1307 strlen(sharepath) + 1;
1309 for (i=0;i<data->u.s.num_share_mode_entries;i++) {
1310 state->fn(&shares[i], sharepath, fname,
1311 state->private_data);
1316 /*******************************************************************
1317 Call the specified function on each entry under management by the
1319 ********************************************************************/
1321 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
1322 const char *, void *),
1325 struct forall_state state;
1331 state.private_data = private_data;
1333 return tdb_traverse(tdb, traverse_fn, (void *)&state);