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
,
193 struct byte_range_lock
*br_lck
= NULL
;
195 if (!fsp
->can_lock
) {
196 *perr
= fsp
->is_directory
? NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
200 if (!lp_locking(fsp
->conn
->params
)) {
201 *perr
= NT_STATUS_OK
;
205 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
207 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f requested for fnum %d file %s\n",
208 lock_flav_name(lock_flav
), lock_type_name(lock_type
),
209 (double)offset
, (double)count
, fsp
->fnum
, fsp
->fsp_name
));
211 br_lck
= brl_get_locks(NULL
, fsp
);
213 *perr
= NT_STATUS_NO_MEMORY
;
217 *perr
= brl_lock(br_lck
,
227 /* blocking ie. pending, locks also count here,
228 * as this is an efficiency counter to avoid checking
229 * the lock db. on close. JRA. */
231 fsp
->current_lock_count
++;
236 /****************************************************************************
237 Utility function called by unlocking requests.
238 ****************************************************************************/
240 NTSTATUS
do_unlock(files_struct
*fsp
,
244 enum brl_flavour lock_flav
)
247 struct byte_range_lock
*br_lck
= NULL
;
249 if (!fsp
->can_lock
) {
250 return fsp
->is_directory
? NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
253 if (!lp_locking(fsp
->conn
->params
)) {
257 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
258 (double)offset
, (double)count
, fsp
->fnum
, fsp
->fsp_name
));
260 br_lck
= brl_get_locks(NULL
, fsp
);
262 return NT_STATUS_NO_MEMORY
;
265 ok
= brl_unlock(br_lck
,
275 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
276 return NT_STATUS_RANGE_NOT_LOCKED
;
279 SMB_ASSERT(fsp
->current_lock_count
> 0);
280 fsp
->current_lock_count
--;
285 /****************************************************************************
286 Cancel any pending blocked locks.
287 ****************************************************************************/
289 NTSTATUS
do_lock_cancel(files_struct
*fsp
,
293 enum brl_flavour lock_flav
)
296 struct byte_range_lock
*br_lck
= NULL
;
298 if (!fsp
->can_lock
) {
299 return fsp
->is_directory
?
300 NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
303 if (!lp_locking(fsp
->conn
->params
)) {
304 return NT_STATUS_DOS(ERRDOS
, ERRcancelviolation
);
307 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
308 (double)offset
, (double)count
, fsp
->fnum
, fsp
->fsp_name
));
310 br_lck
= brl_get_locks(NULL
, fsp
);
312 return NT_STATUS_NO_MEMORY
;
315 ok
= brl_lock_cancel(br_lck
,
325 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
326 return NT_STATUS_DOS(ERRDOS
, ERRcancelviolation
);
329 SMB_ASSERT(fsp
->current_lock_count
> 0);
330 fsp
->current_lock_count
--;
335 /****************************************************************************
336 Remove any locks on this fd. Called from file_close().
337 ****************************************************************************/
339 void locking_close_file(files_struct
*fsp
)
341 struct byte_range_lock
*br_lck
;
343 if (!lp_locking(fsp
->conn
->params
)) {
347 /* If we have not outstanding locks or pending
348 * locks then we don't need to look in the lock db.
351 if (fsp
->current_lock_count
== 0) {
355 br_lck
= brl_get_locks(NULL
,fsp
);
358 cancel_pending_lock_requests_by_fid(fsp
, br_lck
);
359 brl_close_fnum(br_lck
);
364 /****************************************************************************
365 Initialise the locking functions.
366 ****************************************************************************/
368 static int open_read_only
;
370 BOOL
locking_init(int read_only
)
377 tdb
= tdb_open_log(lock_path("locking.tdb"),
378 lp_open_files_db_hash_size(),
379 TDB_DEFAULT
|(read_only
?0x0:TDB_CLEAR_IF_FIRST
),
380 read_only
?O_RDONLY
:O_RDWR
|O_CREAT
,
384 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
388 /* Activate the per-hashchain freelist */
389 tdb_set_max_dead(tdb
, 5);
391 if (!posix_locking_init(read_only
))
394 open_read_only
= read_only
;
399 /*******************************************************************
400 Deinitialize the share_mode management.
401 ******************************************************************/
403 BOOL
locking_end(void)
407 brl_shutdown(open_read_only
);
409 if (tdb_close(tdb
) != 0)
416 /*******************************************************************
417 Form a static locking key for a dev/inode pair.
418 ******************************************************************/
420 /* key and data records in the tdb locking database */
426 /*******************************************************************
427 Form a static locking key for a dev/inode pair.
428 ******************************************************************/
430 static TDB_DATA
locking_key(SMB_DEV_T dev
, SMB_INO_T inode
)
432 static struct locking_key key
;
435 memset(&key
, '\0', sizeof(key
));
438 kbuf
.dptr
= (char *)&key
;
439 kbuf
.dsize
= sizeof(key
);
443 /*******************************************************************
444 Print out a share mode.
445 ********************************************************************/
447 char *share_mode_str(int num
, struct share_mode_entry
*e
)
449 static pstring share_str
;
451 slprintf(share_str
, sizeof(share_str
)-1, "share_mode_entry[%d]: %s "
452 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
453 "access_mask = 0x%x, mid = 0x%x, type= 0x%x, file_id = %lu, "
454 "uid = %u, flags = %u, dev = 0x%x, inode = %.0f",
456 e
->op_type
== UNUSED_SHARE_MODE_ENTRY
? "UNUSED" : "",
457 procid_str_static(&e
->pid
),
458 e
->share_access
, e
->private_options
,
459 e
->access_mask
, e
->op_mid
, e
->op_type
, e
->share_file_id
,
460 (unsigned int)e
->uid
, (unsigned int)e
->flags
,
461 (unsigned int)e
->dev
, (double)e
->inode
);
466 /*******************************************************************
467 Print out a share mode table.
468 ********************************************************************/
470 static void print_share_mode_table(struct locking_data
*data
)
472 int num_share_modes
= data
->u
.s
.num_share_mode_entries
;
473 struct share_mode_entry
*shares
=
474 (struct share_mode_entry
*)(data
+ 1);
477 for (i
= 0; i
< num_share_modes
; i
++) {
478 struct share_mode_entry entry
;
480 memcpy(&entry
, &shares
[i
], sizeof(struct share_mode_entry
));
481 DEBUG(10,("print_share_mode_table: %s\n",
482 share_mode_str(i
, &entry
)));
486 /*******************************************************************
487 Get all share mode entries for a dev/inode pair.
488 ********************************************************************/
490 static BOOL
parse_share_modes(TDB_DATA dbuf
, struct share_mode_lock
*lck
)
492 struct locking_data
*data
;
495 if (dbuf
.dsize
< sizeof(struct locking_data
)) {
496 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
499 data
= (struct locking_data
*)dbuf
.dptr
;
501 lck
->delete_on_close
= data
->u
.s
.delete_on_close
;
502 lck
->num_share_modes
= data
->u
.s
.num_share_mode_entries
;
504 DEBUG(10, ("parse_share_modes: delete_on_close: %d, "
505 "num_share_modes: %d\n",
506 lck
->delete_on_close
,
507 lck
->num_share_modes
));
509 if ((lck
->num_share_modes
< 0) || (lck
->num_share_modes
> 1000000)) {
510 DEBUG(0, ("invalid number of share modes: %d\n",
511 lck
->num_share_modes
));
512 smb_panic("PANIC: invalid number of share modes");
515 lck
->share_modes
= NULL
;
517 if (lck
->num_share_modes
!= 0) {
519 if (dbuf
.dsize
< (sizeof(struct locking_data
) +
520 (lck
->num_share_modes
*
521 sizeof(struct share_mode_entry
)))) {
522 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
525 lck
->share_modes
= (struct share_mode_entry
*)
526 TALLOC_MEMDUP(lck
, dbuf
.dptr
+sizeof(*data
),
527 lck
->num_share_modes
*
528 sizeof(struct share_mode_entry
));
530 if (lck
->share_modes
== NULL
) {
531 smb_panic("talloc failed\n");
535 /* Get any delete token. */
536 if (data
->u
.s
.delete_token_size
) {
537 char *p
= dbuf
.dptr
+ sizeof(*data
) +
538 (lck
->num_share_modes
*
539 sizeof(struct share_mode_entry
));
541 if ((data
->u
.s
.delete_token_size
< sizeof(uid_t
) + sizeof(gid_t
)) ||
542 ((data
->u
.s
.delete_token_size
- sizeof(uid_t
)) % sizeof(gid_t
)) != 0) {
543 DEBUG(0, ("parse_share_modes: invalid token size %d\n",
544 data
->u
.s
.delete_token_size
));
545 smb_panic("parse_share_modes: invalid token size\n");
548 lck
->delete_token
= TALLOC_P(lck
, UNIX_USER_TOKEN
);
549 if (!lck
->delete_token
) {
550 smb_panic("talloc failed\n");
553 /* Copy out the uid and gid. */
554 memcpy(&lck
->delete_token
->uid
, p
, sizeof(uid_t
));
556 memcpy(&lck
->delete_token
->gid
, p
, sizeof(gid_t
));
559 /* Any supplementary groups ? */
560 lck
->delete_token
->ngroups
= (data
->u
.s
.delete_token_size
> (sizeof(uid_t
) + sizeof(gid_t
))) ?
561 ((data
->u
.s
.delete_token_size
-
562 (sizeof(uid_t
) + sizeof(gid_t
)))/sizeof(gid_t
)) : 0;
564 if (lck
->delete_token
->ngroups
) {
565 /* Make this a talloc child of lck->delete_token. */
566 lck
->delete_token
->groups
= TALLOC_ARRAY(lck
->delete_token
, gid_t
,
567 lck
->delete_token
->ngroups
);
568 if (!lck
->delete_token
) {
569 smb_panic("talloc failed\n");
572 for (i
= 0; i
< lck
->delete_token
->ngroups
; i
++) {
573 memcpy(&lck
->delete_token
->groups
[i
], p
, sizeof(gid_t
));
579 lck
->delete_token
= NULL
;
582 /* Save off the associated service path and filename. */
583 lck
->servicepath
= talloc_strdup(lck
, dbuf
.dptr
+ sizeof(*data
) +
584 (lck
->num_share_modes
*
585 sizeof(struct share_mode_entry
)) +
586 data
->u
.s
.delete_token_size
);
587 if (lck
->servicepath
== NULL
) {
588 smb_panic("talloc_strdup failed\n");
591 lck
->filename
= talloc_strdup(lck
, dbuf
.dptr
+ sizeof(*data
) +
592 (lck
->num_share_modes
*
593 sizeof(struct share_mode_entry
)) +
594 data
->u
.s
.delete_token_size
+
595 strlen(lck
->servicepath
) + 1 );
596 if (lck
->filename
== NULL
) {
597 smb_panic("talloc_strdup failed\n");
601 * Ensure that each entry has a real process attached.
604 for (i
= 0; i
< lck
->num_share_modes
; i
++) {
605 struct share_mode_entry
*entry_p
= &lck
->share_modes
[i
];
606 DEBUG(10,("parse_share_modes: %s\n",
607 share_mode_str(i
, entry_p
) ));
608 if (!process_exists(entry_p
->pid
)) {
609 DEBUG(10,("parse_share_modes: deleted %s\n",
610 share_mode_str(i
, entry_p
) ));
611 entry_p
->op_type
= UNUSED_SHARE_MODE_ENTRY
;
612 lck
->modified
= True
;
619 static TDB_DATA
unparse_share_modes(struct share_mode_lock
*lck
)
624 struct locking_data
*data
;
627 uint32 delete_token_size
;
632 for (i
=0; i
<lck
->num_share_modes
; i
++) {
633 if (!is_unused_share_mode_entry(&lck
->share_modes
[i
])) {
638 if (num_valid
== 0) {
642 sp_len
= strlen(lck
->servicepath
);
643 delete_token_size
= (lck
->delete_token
?
644 (sizeof(uid_t
) + sizeof(gid_t
) + (lck
->delete_token
->ngroups
*sizeof(gid_t
))) : 0);
646 result
.dsize
= sizeof(*data
) +
647 lck
->num_share_modes
* sizeof(struct share_mode_entry
) +
650 strlen(lck
->filename
) + 1;
651 result
.dptr
= TALLOC_ARRAY(lck
, char, result
.dsize
);
653 if (result
.dptr
== NULL
) {
654 smb_panic("talloc failed\n");
657 data
= (struct locking_data
*)result
.dptr
;
659 data
->u
.s
.num_share_mode_entries
= lck
->num_share_modes
;
660 data
->u
.s
.delete_on_close
= lck
->delete_on_close
;
661 data
->u
.s
.delete_token_size
= delete_token_size
;
662 DEBUG(10, ("unparse_share_modes: del: %d, tok = %u, num: %d\n",
663 data
->u
.s
.delete_on_close
,
664 (unsigned int)data
->u
.s
.delete_token_size
,
665 data
->u
.s
.num_share_mode_entries
));
666 memcpy(result
.dptr
+ sizeof(*data
), lck
->share_modes
,
667 sizeof(struct share_mode_entry
)*lck
->num_share_modes
);
668 offset
= sizeof(*data
) +
669 sizeof(struct share_mode_entry
)*lck
->num_share_modes
;
671 /* Store any delete on close token. */
672 if (lck
->delete_token
) {
673 char *p
= result
.dptr
+ offset
;
675 memcpy(p
, &lck
->delete_token
->uid
, sizeof(uid_t
));
678 memcpy(p
, &lck
->delete_token
->gid
, sizeof(gid_t
));
681 for (i
= 0; i
< lck
->delete_token
->ngroups
; i
++) {
682 memcpy(p
, &lck
->delete_token
->groups
[i
], sizeof(gid_t
));
685 offset
= p
- result
.dptr
;
688 safe_strcpy(result
.dptr
+ offset
, lck
->servicepath
,
689 result
.dsize
- offset
- 1);
690 offset
+= sp_len
+ 1;
691 safe_strcpy(result
.dptr
+ offset
, lck
->filename
,
692 result
.dsize
- offset
- 1);
694 if (DEBUGLEVEL
>= 10) {
695 print_share_mode_table(data
);
701 static int share_mode_lock_destructor(struct share_mode_lock
*lck
)
703 TDB_DATA key
= locking_key(lck
->dev
, lck
->ino
);
706 if (!lck
->modified
) {
710 data
= unparse_share_modes(lck
);
712 if (data
.dptr
== NULL
) {
714 /* There has been an entry before, delete it */
715 if (tdb_delete(tdb
, key
) == -1) {
716 smb_panic("Could not delete share entry\n");
722 if (tdb_store(tdb
, key
, data
, TDB_REPLACE
) == -1) {
723 smb_panic("Could not store share mode entry\n");
727 tdb_chainunlock(tdb
, key
);
732 struct share_mode_lock
*get_share_mode_lock(TALLOC_CTX
*mem_ctx
,
733 SMB_DEV_T dev
, SMB_INO_T ino
,
734 const char *servicepath
,
737 struct share_mode_lock
*lck
;
738 TDB_DATA key
= locking_key(dev
, ino
);
741 lck
= TALLOC_P(mem_ctx
, struct share_mode_lock
);
743 DEBUG(0, ("talloc failed\n"));
747 /* Ensure we set every field here as the destructor must be
748 valid even if parse_share_modes fails. */
750 lck
->servicepath
= NULL
;
751 lck
->filename
= NULL
;
754 lck
->num_share_modes
= 0;
755 lck
->share_modes
= NULL
;
756 lck
->delete_token
= NULL
;
757 lck
->delete_on_close
= False
;
759 lck
->modified
= False
;
761 if (tdb_chainlock(tdb
, key
) != 0) {
762 DEBUG(3, ("Could not lock share entry\n"));
767 /* We must set the destructor immediately after the chainlock
768 ensure the lock is cleaned up on any of the error return
771 talloc_set_destructor(lck
, share_mode_lock_destructor
);
773 data
= tdb_fetch(tdb
, key
);
774 lck
->fresh
= (data
.dptr
== NULL
);
778 if (fname
== NULL
|| servicepath
== NULL
) {
782 lck
->filename
= talloc_strdup(lck
, fname
);
783 lck
->servicepath
= talloc_strdup(lck
, servicepath
);
784 if (lck
->filename
== NULL
|| lck
->servicepath
== NULL
) {
785 DEBUG(0, ("talloc failed\n"));
790 if (!parse_share_modes(data
, lck
)) {
791 DEBUG(0, ("Could not parse share modes\n"));
793 SAFE_FREE(data
.dptr
);
798 SAFE_FREE(data
.dptr
);
803 /*******************************************************************
804 Sets the service name and filename for rename.
805 At this point we emit "file renamed" messages to all
806 process id's that have this file open.
807 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
808 ********************************************************************/
810 BOOL
rename_share_filename(struct share_mode_lock
*lck
,
811 const char *servicepath
,
824 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
825 servicepath
, newname
));
828 * rename_internal_fsp() and rename_internals() add './' to
829 * head of newname if newname does not contain a '/'.
831 while (newname
[0] && newname
[1] && newname
[0] == '.' && newname
[1] == '/') {
835 lck
->servicepath
= talloc_strdup(lck
, servicepath
);
836 lck
->filename
= talloc_strdup(lck
, newname
);
837 if (lck
->filename
== NULL
|| lck
->servicepath
== NULL
) {
838 DEBUG(0, ("rename_share_filename: talloc failed\n"));
841 lck
->modified
= True
;
843 sp_len
= strlen(lck
->servicepath
);
844 fn_len
= strlen(lck
->filename
);
846 msg_len
= MSG_FILE_RENAMED_MIN_SIZE
+ sp_len
+ 1 + fn_len
+ 1;
848 /* Set up the name changed message. */
849 frm
= TALLOC_ARRAY(lck
, char, msg_len
);
854 SDEV_T_VAL(frm
,0,lck
->dev
);
855 SINO_T_VAL(frm
,8,lck
->ino
);
857 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len
));
859 safe_strcpy(&frm
[16], lck
->servicepath
, sp_len
);
860 safe_strcpy(&frm
[16 + sp_len
+ 1], lck
->filename
, fn_len
);
862 /* Send the messages. */
863 for (i
=0; i
<lck
->num_share_modes
; i
++) {
864 struct share_mode_entry
*se
= &lck
->share_modes
[i
];
865 if (!is_valid_share_mode_entry(se
)) {
868 /* But not to ourselves... */
869 if (procid_is_me(&se
->pid
)) {
873 DEBUG(10,("rename_share_filename: sending rename message to pid %s "
874 "dev %x, inode %.0f sharepath %s newname %s\n",
875 procid_str_static(&se
->pid
),
876 (unsigned int)lck
->dev
, (double)lck
->ino
,
877 lck
->servicepath
, lck
->filename
));
879 message_send_pid(se
->pid
, MSG_SMB_FILE_RENAME
,
886 static int pull_delete_on_close_flag(TDB_DATA key
, TDB_DATA dbuf
,
889 BOOL
*result
= (BOOL
*)private_data
;
890 struct locking_data
*data
;
892 if (dbuf
.dsize
< sizeof(struct locking_data
)) {
893 smb_panic("PANIC: parse_share_modes: buffer too short.\n");
896 data
= (struct locking_data
*)dbuf
.dptr
;
898 *result
= data
->u
.s
.delete_on_close
;
902 BOOL
get_delete_on_close_flag(SMB_DEV_T dev
, SMB_INO_T inode
)
904 TDB_DATA key
= locking_key(dev
, inode
);
907 tdb_parse_record(tdb
, key
, pull_delete_on_close_flag
,
912 BOOL
is_valid_share_mode_entry(const struct share_mode_entry
*e
)
916 num_props
+= ((e
->op_type
== NO_OPLOCK
) ? 1 : 0);
917 num_props
+= (EXCLUSIVE_OPLOCK_TYPE(e
->op_type
) ? 1 : 0);
918 num_props
+= (LEVEL_II_OPLOCK_TYPE(e
->op_type
) ? 1 : 0);
920 SMB_ASSERT(num_props
<= 1);
921 return (num_props
!= 0);
924 BOOL
is_deferred_open_entry(const struct share_mode_entry
*e
)
926 return (e
->op_type
== DEFERRED_OPEN_ENTRY
);
929 BOOL
is_unused_share_mode_entry(const struct share_mode_entry
*e
)
931 return (e
->op_type
== UNUSED_SHARE_MODE_ENTRY
);
934 /*******************************************************************
935 Fill a share mode entry.
936 ********************************************************************/
938 static void fill_share_mode_entry(struct share_mode_entry
*e
,
940 uid_t uid
, uint16 mid
, uint16 op_type
)
943 e
->pid
= procid_self();
944 e
->share_access
= fsp
->share_access
;
945 e
->private_options
= fsp
->fh
->private_options
;
946 e
->access_mask
= fsp
->access_mask
;
948 e
->op_type
= op_type
;
949 e
->time
.tv_sec
= fsp
->open_time
.tv_sec
;
950 e
->time
.tv_usec
= fsp
->open_time
.tv_usec
;
952 e
->inode
= fsp
->inode
;
953 e
->share_file_id
= fsp
->fh
->file_id
;
954 e
->uid
= (uint32
)uid
;
955 e
->flags
= fsp
->posix_open
? SHARE_MODE_FLAG_POSIX_OPEN
: 0;
958 static void fill_deferred_open_entry(struct share_mode_entry
*e
,
959 const struct timeval request_time
,
960 SMB_DEV_T dev
, SMB_INO_T ino
, uint16 mid
)
963 e
->pid
= procid_self();
965 e
->op_type
= DEFERRED_OPEN_ENTRY
;
966 e
->time
.tv_sec
= request_time
.tv_sec
;
967 e
->time
.tv_usec
= request_time
.tv_usec
;
974 static void add_share_mode_entry(struct share_mode_lock
*lck
,
975 const struct share_mode_entry
*entry
)
979 for (i
=0; i
<lck
->num_share_modes
; i
++) {
980 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
981 if (is_unused_share_mode_entry(e
)) {
987 if (i
== lck
->num_share_modes
) {
988 /* No unused entry found */
989 ADD_TO_ARRAY(lck
, struct share_mode_entry
, *entry
,
990 &lck
->share_modes
, &lck
->num_share_modes
);
992 lck
->modified
= True
;
995 void set_share_mode(struct share_mode_lock
*lck
, files_struct
*fsp
,
996 uid_t uid
, uint16 mid
, uint16 op_type
, BOOL initial_delete_on_close_allowed
)
998 struct share_mode_entry entry
;
999 fill_share_mode_entry(&entry
, fsp
, uid
, mid
, op_type
);
1000 if (initial_delete_on_close_allowed
) {
1001 entry
.flags
|= SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE
;
1003 add_share_mode_entry(lck
, &entry
);
1006 void add_deferred_open(struct share_mode_lock
*lck
, uint16 mid
,
1007 struct timeval request_time
,
1008 SMB_DEV_T dev
, SMB_INO_T ino
)
1010 struct share_mode_entry entry
;
1011 fill_deferred_open_entry(&entry
, request_time
, dev
, ino
, mid
);
1012 add_share_mode_entry(lck
, &entry
);
1015 /*******************************************************************
1016 Check if two share mode entries are identical, ignoring oplock
1017 and mid info and desired_access. (Removed paranoia test - it's
1018 not automatically a logic error if they are identical. JRA.)
1019 ********************************************************************/
1021 static BOOL
share_modes_identical(struct share_mode_entry
*e1
,
1022 struct share_mode_entry
*e2
)
1024 /* We used to check for e1->share_access == e2->share_access here
1025 as well as the other fields but 2 different DOS or FCB opens
1026 sharing the same share mode entry may validly differ in
1027 fsp->share_access field. */
1029 return (procid_equal(&e1
->pid
, &e2
->pid
) &&
1030 e1
->dev
== e2
->dev
&&
1031 e1
->inode
== e2
->inode
&&
1032 e1
->share_file_id
== e2
->share_file_id
);
1035 static BOOL
deferred_open_identical(struct share_mode_entry
*e1
,
1036 struct share_mode_entry
*e2
)
1038 return (procid_equal(&e1
->pid
, &e2
->pid
) &&
1039 (e1
->op_mid
== e2
->op_mid
) &&
1040 (e1
->dev
== e2
->dev
) &&
1041 (e1
->inode
== e2
->inode
));
1044 static struct share_mode_entry
*find_share_mode_entry(struct share_mode_lock
*lck
,
1045 struct share_mode_entry
*entry
)
1049 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1050 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
1051 if (is_valid_share_mode_entry(entry
) &&
1052 is_valid_share_mode_entry(e
) &&
1053 share_modes_identical(e
, entry
)) {
1056 if (is_deferred_open_entry(entry
) &&
1057 is_deferred_open_entry(e
) &&
1058 deferred_open_identical(e
, entry
)) {
1065 /*******************************************************************
1066 Del the share mode of a file for this process. Return the number of
1068 ********************************************************************/
1070 BOOL
del_share_mode(struct share_mode_lock
*lck
, files_struct
*fsp
)
1072 struct share_mode_entry entry
, *e
;
1074 /* Don't care about the pid owner being correct here - just a search. */
1075 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1077 e
= find_share_mode_entry(lck
, &entry
);
1082 e
->op_type
= UNUSED_SHARE_MODE_ENTRY
;
1083 lck
->modified
= True
;
1087 void del_deferred_open_entry(struct share_mode_lock
*lck
, uint16 mid
)
1089 struct share_mode_entry entry
, *e
;
1091 fill_deferred_open_entry(&entry
, timeval_zero(),
1092 lck
->dev
, lck
->ino
, mid
);
1094 e
= find_share_mode_entry(lck
, &entry
);
1099 e
->op_type
= UNUSED_SHARE_MODE_ENTRY
;
1100 lck
->modified
= True
;
1103 /*******************************************************************
1104 Remove an oplock mid and mode entry from a share mode.
1105 ********************************************************************/
1107 BOOL
remove_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
1109 struct share_mode_entry entry
, *e
;
1111 /* Don't care about the pid owner being correct here - just a search. */
1112 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1114 e
= find_share_mode_entry(lck
, &entry
);
1120 e
->op_type
= NO_OPLOCK
;
1121 lck
->modified
= True
;
1125 /*******************************************************************
1126 Downgrade a oplock type from exclusive to level II.
1127 ********************************************************************/
1129 BOOL
downgrade_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
1131 struct share_mode_entry entry
, *e
;
1133 /* Don't care about the pid owner being correct here - just a search. */
1134 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1136 e
= find_share_mode_entry(lck
, &entry
);
1141 e
->op_type
= LEVEL_II_OPLOCK
;
1142 lck
->modified
= True
;
1146 /****************************************************************************
1147 Deal with the internal needs of setting the delete on close flag. Note that
1148 as the tdb locking is recursive, it is safe to call this from within
1149 open_file_ntcreate. JRA.
1150 ****************************************************************************/
1152 NTSTATUS
can_set_delete_on_close(files_struct
*fsp
, BOOL delete_on_close
,
1155 if (!delete_on_close
) {
1156 return NT_STATUS_OK
;
1160 * Only allow delete on close for writable files.
1163 if ((dosmode
& aRONLY
) &&
1164 !lp_delete_readonly(SNUM(fsp
->conn
))) {
1165 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1166 "flag set but file attribute is readonly.\n",
1168 return NT_STATUS_CANNOT_DELETE
;
1172 * Only allow delete on close for writable shares.
1175 if (!CAN_WRITE(fsp
->conn
)) {
1176 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1177 "close flag set but write access denied on share.\n",
1179 return NT_STATUS_ACCESS_DENIED
;
1183 * Only allow delete on close for files/directories opened with delete
1187 if (!(fsp
->access_mask
& DELETE_ACCESS
)) {
1188 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1189 "close flag set but delete access denied.\n",
1191 return NT_STATUS_ACCESS_DENIED
;
1194 /* Don't allow delete on close for non-empty directories. */
1195 if (fsp
->is_directory
) {
1196 return can_delete_directory(fsp
->conn
, fsp
->fsp_name
);
1199 return NT_STATUS_OK
;
1202 /****************************************************************************
1203 Do we have an open file handle that created this entry ?
1204 ****************************************************************************/
1206 BOOL
can_set_initial_delete_on_close(const struct share_mode_lock
*lck
)
1210 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1211 if (lck
->share_modes
[i
].flags
& SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE
) {
1218 /*************************************************************************
1219 Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1220 (Should this be in locking.c.... ?).
1221 *************************************************************************/
1223 static UNIX_USER_TOKEN
*copy_unix_token(TALLOC_CTX
*ctx
, UNIX_USER_TOKEN
*tok
)
1225 UNIX_USER_TOKEN
*cpy
;
1231 cpy
= TALLOC_P(ctx
, UNIX_USER_TOKEN
);
1236 cpy
->uid
= tok
->uid
;
1237 cpy
->gid
= tok
->gid
;
1238 cpy
->ngroups
= tok
->ngroups
;
1240 /* Make this a talloc child of cpy. */
1241 cpy
->groups
= TALLOC_ARRAY(cpy
, gid_t
, tok
->ngroups
);
1245 memcpy(cpy
->groups
, tok
->groups
, tok
->ngroups
* sizeof(gid_t
));
1250 /****************************************************************************
1251 Replace the delete on close token.
1252 ****************************************************************************/
1254 void set_delete_on_close_token(struct share_mode_lock
*lck
, UNIX_USER_TOKEN
*tok
)
1256 /* Ensure there's no token. */
1257 if (lck
->delete_token
) {
1258 TALLOC_FREE(lck
->delete_token
); /* Also deletes groups... */
1259 lck
->delete_token
= NULL
;
1262 /* Copy the new token (can be NULL). */
1263 lck
->delete_token
= copy_unix_token(lck
, tok
);
1264 lck
->modified
= True
;
1267 /****************************************************************************
1268 Sets the delete on close flag over all share modes on this file.
1269 Modify the share mode entry for all files open
1270 on this device and inode to tell other smbds we have
1271 changed the delete on close flag. This will be noticed
1272 in the close code, the last closer will delete the file
1274 This makes a copy of any UNIX_USER_TOKEN into the
1275 lck entry. This function is used when the lock is already granted.
1276 ****************************************************************************/
1278 void set_delete_on_close_lck(struct share_mode_lock
*lck
, BOOL delete_on_close
, UNIX_USER_TOKEN
*tok
)
1280 if (lck
->delete_on_close
!= delete_on_close
) {
1281 set_delete_on_close_token(lck
, tok
);
1282 lck
->delete_on_close
= delete_on_close
;
1283 if (delete_on_close
) {
1284 SMB_ASSERT(lck
->delete_token
!= NULL
);
1286 lck
->modified
= True
;
1290 BOOL
set_delete_on_close(files_struct
*fsp
, BOOL delete_on_close
, UNIX_USER_TOKEN
*tok
)
1292 struct share_mode_lock
*lck
;
1294 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1295 "fnum = %d, file %s\n",
1296 delete_on_close
? "Adding" : "Removing", fsp
->fnum
,
1303 lck
= get_share_mode_lock(NULL
, fsp
->dev
, fsp
->inode
, NULL
, NULL
);
1308 set_delete_on_close_lck(lck
, delete_on_close
, tok
);
1310 if (fsp
->is_directory
) {
1311 send_stat_cache_delete_message(fsp
->fsp_name
);
1318 /****************************************************************************
1319 Sets the allow initial delete on close flag for this share mode.
1320 ****************************************************************************/
1322 BOOL
set_allow_initial_delete_on_close(struct share_mode_lock
*lck
, files_struct
*fsp
, BOOL delete_on_close
)
1324 struct share_mode_entry entry
, *e
;
1326 /* Don't care about the pid owner being correct here - just a search. */
1327 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1329 e
= find_share_mode_entry(lck
, &entry
);
1334 if (delete_on_close
) {
1335 e
->flags
|= SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE
;
1337 e
->flags
&= ~SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE
;
1339 lck
->modified
= True
;
1343 struct forall_state
{
1344 void (*fn
)(const struct share_mode_entry
*entry
,
1345 const char *sharepath
,
1347 void *private_data
);
1351 static int traverse_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1354 struct forall_state
*state
= (struct forall_state
*)_state
;
1355 struct locking_data
*data
;
1356 struct share_mode_entry
*shares
;
1357 const char *sharepath
;
1361 /* Ensure this is a locking_key record. */
1362 if (kbuf
.dsize
!= sizeof(struct locking_key
))
1365 data
= (struct locking_data
*)dbuf
.dptr
;
1366 shares
= (struct share_mode_entry
*)(dbuf
.dptr
+ sizeof(*data
));
1367 sharepath
= dbuf
.dptr
+ sizeof(*data
) +
1368 data
->u
.s
.num_share_mode_entries
*sizeof(*shares
) +
1369 data
->u
.s
.delete_token_size
;
1370 fname
= dbuf
.dptr
+ sizeof(*data
) +
1371 data
->u
.s
.num_share_mode_entries
*sizeof(*shares
) +
1372 data
->u
.s
.delete_token_size
+
1373 strlen(sharepath
) + 1;
1375 for (i
=0;i
<data
->u
.s
.num_share_mode_entries
;i
++) {
1376 state
->fn(&shares
[i
], sharepath
, fname
,
1377 state
->private_data
);
1382 /*******************************************************************
1383 Call the specified function on each entry under management by the
1385 ********************************************************************/
1387 int share_mode_forall(void (*fn
)(const struct share_mode_entry
*, const char *,
1388 const char *, void *),
1391 struct forall_state state
;
1397 state
.private_data
= private_data
;
1399 return tdb_traverse(tdb
, traverse_fn
, (void *)&state
);