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 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/>.
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
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.
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 /****************************************************************************
50 ****************************************************************************/
52 const char *lock_type_name(enum brl_type lock_type
)
59 case PENDING_READ_LOCK
:
60 return "PENDING_READ";
61 case PENDING_WRITE_LOCK
:
62 return "PENDING_WRITE";
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 bool is_locked(files_struct
*fsp
,
82 enum brl_type lock_type
)
84 int strict_locking
= lp_strict_locking(fsp
->conn
->params
);
85 enum brl_flavour lock_flav
= lp_posix_cifsu_locktype(fsp
);
92 if (!lp_locking(fsp
->conn
->params
) || !strict_locking
) {
96 if (strict_locking
== Auto
) {
97 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && (lock_type
== READ_LOCK
|| lock_type
== WRITE_LOCK
)) {
98 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp
->fsp_name
));
100 } else if ((fsp
->oplock_type
== LEVEL_II_OPLOCK
) &&
101 (lock_type
== READ_LOCK
)) {
102 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp
->fsp_name
));
105 struct byte_range_lock
*br_lck
= brl_get_locks_readonly(talloc_tos(), fsp
);
109 ret
= !brl_locktest(br_lck
,
119 struct byte_range_lock
*br_lck
= brl_get_locks_readonly(talloc_tos(), fsp
);
123 ret
= !brl_locktest(br_lck
,
133 DEBUG(10,("is_locked: flavour = %s brl start=%.0f len=%.0f %s for fnum %d file %s\n",
134 lock_flav_name(lock_flav
),
135 (double)offset
, (double)count
, ret
? "locked" : "unlocked",
136 fsp
->fnum
, fsp
->fsp_name
));
141 /****************************************************************************
142 Find out if a lock could be granted - return who is blocking us if we can't.
143 ****************************************************************************/
145 NTSTATUS
query_lock(files_struct
*fsp
,
147 SMB_BIG_UINT
*pcount
,
148 SMB_BIG_UINT
*poffset
,
149 enum brl_type
*plock_type
,
150 enum brl_flavour lock_flav
)
152 struct byte_range_lock
*br_lck
= NULL
;
153 NTSTATUS status
= NT_STATUS_LOCK_NOT_GRANTED
;
155 if (!fsp
->can_lock
) {
156 return fsp
->is_directory
? NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
159 if (!lp_locking(fsp
->conn
->params
)) {
163 br_lck
= brl_get_locks_readonly(talloc_tos(), fsp
);
165 return NT_STATUS_NO_MEMORY
;
168 status
= brl_lockquery(br_lck
,
180 /****************************************************************************
181 Utility function called by locking requests.
182 ****************************************************************************/
184 struct byte_range_lock
*do_lock(struct messaging_context
*msg_ctx
,
189 enum brl_type lock_type
,
190 enum brl_flavour lock_flav
,
195 struct byte_range_lock
*br_lck
= NULL
;
197 if (!fsp
->can_lock
) {
198 *perr
= fsp
->is_directory
? NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
202 if (!lp_locking(fsp
->conn
->params
)) {
203 *perr
= NT_STATUS_OK
;
207 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
209 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f requested for fnum %d file %s\n",
210 lock_flav_name(lock_flav
), lock_type_name(lock_type
),
211 (double)offset
, (double)count
, fsp
->fnum
, fsp
->fsp_name
));
213 br_lck
= brl_get_locks(talloc_tos(), fsp
);
215 *perr
= NT_STATUS_NO_MEMORY
;
219 *perr
= brl_lock(msg_ctx
,
230 if (lock_flav
== WINDOWS_LOCK
&&
231 fsp
->current_lock_count
!= NO_LOCKING_COUNT
) {
232 /* blocking ie. pending, locks also count here,
233 * as this is an efficiency counter to avoid checking
234 * the lock db. on close. JRA. */
236 fsp
->current_lock_count
++;
238 /* Notice that this has had a POSIX lock request.
239 * We can't count locks after this so forget them.
241 fsp
->current_lock_count
= NO_LOCKING_COUNT
;
247 /****************************************************************************
248 Utility function called by unlocking requests.
249 ****************************************************************************/
251 NTSTATUS
do_unlock(struct messaging_context
*msg_ctx
,
256 enum brl_flavour lock_flav
)
259 struct byte_range_lock
*br_lck
= NULL
;
261 if (!fsp
->can_lock
) {
262 return fsp
->is_directory
? NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
265 if (!lp_locking(fsp
->conn
->params
)) {
269 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
270 (double)offset
, (double)count
, fsp
->fnum
, fsp
->fsp_name
));
272 br_lck
= brl_get_locks(talloc_tos(), fsp
);
274 return NT_STATUS_NO_MEMORY
;
277 ok
= brl_unlock(msg_ctx
,
288 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
289 return NT_STATUS_RANGE_NOT_LOCKED
;
292 if (lock_flav
== WINDOWS_LOCK
&&
293 fsp
->current_lock_count
!= NO_LOCKING_COUNT
) {
294 SMB_ASSERT(fsp
->current_lock_count
> 0);
295 fsp
->current_lock_count
--;
301 /****************************************************************************
302 Cancel any pending blocked locks.
303 ****************************************************************************/
305 NTSTATUS
do_lock_cancel(files_struct
*fsp
,
309 enum brl_flavour lock_flav
)
312 struct byte_range_lock
*br_lck
= NULL
;
314 if (!fsp
->can_lock
) {
315 return fsp
->is_directory
?
316 NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
319 if (!lp_locking(fsp
->conn
->params
)) {
320 return NT_STATUS_DOS(ERRDOS
, ERRcancelviolation
);
323 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
324 (double)offset
, (double)count
, fsp
->fnum
, fsp
->fsp_name
));
326 br_lck
= brl_get_locks(talloc_tos(), fsp
);
328 return NT_STATUS_NO_MEMORY
;
331 ok
= brl_lock_cancel(br_lck
,
341 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
342 return NT_STATUS_DOS(ERRDOS
, ERRcancelviolation
);
345 if (lock_flav
== WINDOWS_LOCK
&&
346 fsp
->current_lock_count
!= NO_LOCKING_COUNT
) {
347 SMB_ASSERT(fsp
->current_lock_count
> 0);
348 fsp
->current_lock_count
--;
354 /****************************************************************************
355 Remove any locks on this fd. Called from file_close().
356 ****************************************************************************/
358 void locking_close_file(struct messaging_context
*msg_ctx
,
361 struct byte_range_lock
*br_lck
;
363 if (!lp_locking(fsp
->conn
->params
)) {
367 /* If we have not outstanding locks or pending
368 * locks then we don't need to look in the lock db.
371 if (fsp
->current_lock_count
== 0) {
375 br_lck
= brl_get_locks(talloc_tos(),fsp
);
378 cancel_pending_lock_requests_by_fid(fsp
, br_lck
);
379 brl_close_fnum(msg_ctx
, br_lck
);
384 /****************************************************************************
385 Initialise the locking functions.
386 ****************************************************************************/
388 static bool locking_init_internal(bool read_only
)
395 lock_db
= db_open(NULL
, lock_path("locking.tdb"),
396 lp_open_files_db_hash_size(),
397 TDB_DEFAULT
|TDB_VOLATILE
|TDB_CLEAR_IF_FIRST
,
398 read_only
?O_RDONLY
:O_RDWR
|O_CREAT
, 0644);
401 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
405 if (!posix_locking_init(read_only
))
411 bool locking_init(void)
413 return locking_init_internal(false);
416 bool locking_init_readonly(void)
418 return locking_init_internal(true);
421 /*******************************************************************
422 Deinitialize the share_mode management.
423 ******************************************************************/
425 bool locking_end(void)
428 TALLOC_FREE(lock_db
);
432 /*******************************************************************
433 Form a static locking key for a dev/inode pair.
434 ******************************************************************/
436 static TDB_DATA
locking_key(const struct file_id
*id
, struct file_id
*tmp
)
439 return make_tdb_data((const uint8_t *)tmp
, sizeof(*tmp
));
442 /*******************************************************************
443 Print out a share mode.
444 ********************************************************************/
446 char *share_mode_str(TALLOC_CTX
*ctx
, int num
, struct share_mode_entry
*e
)
448 return talloc_asprintf(ctx
, "share_mode_entry[%d]: %s "
449 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
450 "access_mask = 0x%x, mid = 0x%x, type= 0x%x, gen_id = %lu, "
451 "uid = %u, flags = %u, file_id %s",
453 e
->op_type
== UNUSED_SHARE_MODE_ENTRY
? "UNUSED" : "",
454 procid_str_static(&e
->pid
),
455 e
->share_access
, e
->private_options
,
456 e
->access_mask
, e
->op_mid
, e
->op_type
, e
->share_file_id
,
457 (unsigned int)e
->uid
, (unsigned int)e
->flags
,
458 file_id_string_tos(&e
->id
));
461 /*******************************************************************
462 Print out a share mode table.
463 ********************************************************************/
465 static void print_share_mode_table(struct locking_data
*data
)
467 int num_share_modes
= data
->u
.s
.num_share_mode_entries
;
468 struct share_mode_entry
*shares
=
469 (struct share_mode_entry
*)(data
+ 1);
472 for (i
= 0; i
< num_share_modes
; i
++) {
473 struct share_mode_entry entry
;
477 * We need to memcpy the entry here due to alignment
478 * restrictions that are not met when directly accessing
482 memcpy(&entry
, &shares
[i
], sizeof(struct share_mode_entry
));
483 str
= share_mode_str(talloc_tos(), i
, &entry
);
485 DEBUG(10,("print_share_mode_table: %s\n", str
? str
: ""));
490 /*******************************************************************
491 Get all share mode entries for a dev/inode pair.
492 ********************************************************************/
494 static bool parse_share_modes(TDB_DATA dbuf
, struct share_mode_lock
*lck
)
496 struct locking_data
*data
;
499 if (dbuf
.dsize
< sizeof(struct locking_data
)) {
500 smb_panic("parse_share_modes: buffer too short");
503 data
= (struct locking_data
*)dbuf
.dptr
;
505 lck
->delete_on_close
= data
->u
.s
.delete_on_close
;
506 lck
->old_write_time
= data
->u
.s
.old_write_time
;
507 lck
->changed_write_time
= data
->u
.s
.changed_write_time
;
508 lck
->num_share_modes
= data
->u
.s
.num_share_mode_entries
;
510 DEBUG(10, ("parse_share_modes: delete_on_close: %d, owrt: %s, "
511 "cwrt: %s, tok: %u, num_share_modes: %d\n",
512 lck
->delete_on_close
,
513 timestring(debug_ctx(),
514 convert_timespec_to_time_t(lck
->old_write_time
)),
515 timestring(debug_ctx(),
516 convert_timespec_to_time_t(
517 lck
->changed_write_time
)),
518 (unsigned int)data
->u
.s
.delete_token_size
,
519 lck
->num_share_modes
));
521 if ((lck
->num_share_modes
< 0) || (lck
->num_share_modes
> 1000000)) {
522 DEBUG(0, ("invalid number of share modes: %d\n",
523 lck
->num_share_modes
));
524 smb_panic("parse_share_modes: invalid number of share modes");
527 lck
->share_modes
= NULL
;
529 if (lck
->num_share_modes
!= 0) {
531 if (dbuf
.dsize
< (sizeof(struct locking_data
) +
532 (lck
->num_share_modes
*
533 sizeof(struct share_mode_entry
)))) {
534 smb_panic("parse_share_modes: buffer too short");
537 lck
->share_modes
= (struct share_mode_entry
*)
538 TALLOC_MEMDUP(lck
, dbuf
.dptr
+sizeof(*data
),
539 lck
->num_share_modes
*
540 sizeof(struct share_mode_entry
));
542 if (lck
->share_modes
== NULL
) {
543 smb_panic("parse_share_modes: talloc failed");
547 /* Get any delete token. */
548 if (data
->u
.s
.delete_token_size
) {
549 uint8
*p
= dbuf
.dptr
+ sizeof(*data
) +
550 (lck
->num_share_modes
*
551 sizeof(struct share_mode_entry
));
553 if ((data
->u
.s
.delete_token_size
< sizeof(uid_t
) + sizeof(gid_t
)) ||
554 ((data
->u
.s
.delete_token_size
- sizeof(uid_t
)) % sizeof(gid_t
)) != 0) {
555 DEBUG(0, ("parse_share_modes: invalid token size %d\n",
556 data
->u
.s
.delete_token_size
));
557 smb_panic("parse_share_modes: invalid token size");
560 lck
->delete_token
= TALLOC_P(lck
, UNIX_USER_TOKEN
);
561 if (!lck
->delete_token
) {
562 smb_panic("parse_share_modes: talloc failed");
565 /* Copy out the uid and gid. */
566 memcpy(&lck
->delete_token
->uid
, p
, sizeof(uid_t
));
568 memcpy(&lck
->delete_token
->gid
, p
, sizeof(gid_t
));
571 /* Any supplementary groups ? */
572 lck
->delete_token
->ngroups
= (data
->u
.s
.delete_token_size
> (sizeof(uid_t
) + sizeof(gid_t
))) ?
573 ((data
->u
.s
.delete_token_size
-
574 (sizeof(uid_t
) + sizeof(gid_t
)))/sizeof(gid_t
)) : 0;
576 if (lck
->delete_token
->ngroups
) {
577 /* Make this a talloc child of lck->delete_token. */
578 lck
->delete_token
->groups
= TALLOC_ARRAY(lck
->delete_token
, gid_t
,
579 lck
->delete_token
->ngroups
);
580 if (!lck
->delete_token
) {
581 smb_panic("parse_share_modes: talloc failed");
584 for (i
= 0; i
< lck
->delete_token
->ngroups
; i
++) {
585 memcpy(&lck
->delete_token
->groups
[i
], p
, sizeof(gid_t
));
591 lck
->delete_token
= NULL
;
594 /* Save off the associated service path and filename. */
595 lck
->servicepath
= (const char *)dbuf
.dptr
+ sizeof(*data
) +
596 (lck
->num_share_modes
* sizeof(struct share_mode_entry
)) +
597 data
->u
.s
.delete_token_size
;
599 lck
->filename
= (const char *)dbuf
.dptr
+ sizeof(*data
) +
600 (lck
->num_share_modes
* sizeof(struct share_mode_entry
)) +
601 data
->u
.s
.delete_token_size
+
602 strlen(lck
->servicepath
) + 1;
605 * Ensure that each entry has a real process attached.
608 for (i
= 0; i
< lck
->num_share_modes
; i
++) {
609 struct share_mode_entry
*entry_p
= &lck
->share_modes
[i
];
611 if (DEBUGLEVEL
>= 10) {
612 str
= share_mode_str(NULL
, i
, entry_p
);
614 DEBUG(10,("parse_share_modes: %s\n",
616 if (!process_exists(entry_p
->pid
)) {
617 DEBUG(10,("parse_share_modes: deleted %s\n",
619 entry_p
->op_type
= UNUSED_SHARE_MODE_ENTRY
;
620 lck
->modified
= True
;
628 static TDB_DATA
unparse_share_modes(struct share_mode_lock
*lck
)
633 struct locking_data
*data
;
636 uint32 delete_token_size
;
641 for (i
=0; i
<lck
->num_share_modes
; i
++) {
642 if (!is_unused_share_mode_entry(&lck
->share_modes
[i
])) {
647 if (num_valid
== 0) {
651 sp_len
= strlen(lck
->servicepath
);
652 delete_token_size
= (lck
->delete_token
?
653 (sizeof(uid_t
) + sizeof(gid_t
) + (lck
->delete_token
->ngroups
*sizeof(gid_t
))) : 0);
655 result
.dsize
= sizeof(*data
) +
656 lck
->num_share_modes
* sizeof(struct share_mode_entry
) +
659 strlen(lck
->filename
) + 1;
660 result
.dptr
= TALLOC_ARRAY(lck
, uint8
, result
.dsize
);
662 if (result
.dptr
== NULL
) {
663 smb_panic("talloc failed");
666 data
= (struct locking_data
*)result
.dptr
;
668 data
->u
.s
.num_share_mode_entries
= lck
->num_share_modes
;
669 data
->u
.s
.delete_on_close
= lck
->delete_on_close
;
670 data
->u
.s
.old_write_time
= lck
->old_write_time
;
671 data
->u
.s
.changed_write_time
= lck
->changed_write_time
;
672 data
->u
.s
.delete_token_size
= delete_token_size
;
674 DEBUG(10,("unparse_share_modes: del: %d, owrt: %s cwrt: %s, tok: %u, "
675 "num: %d\n", data
->u
.s
.delete_on_close
,
676 timestring(debug_ctx(),
677 convert_timespec_to_time_t(lck
->old_write_time
)),
678 timestring(debug_ctx(),
679 convert_timespec_to_time_t(
680 lck
->changed_write_time
)),
681 (unsigned int)data
->u
.s
.delete_token_size
,
682 data
->u
.s
.num_share_mode_entries
));
684 memcpy(result
.dptr
+ sizeof(*data
), lck
->share_modes
,
685 sizeof(struct share_mode_entry
)*lck
->num_share_modes
);
686 offset
= sizeof(*data
) +
687 sizeof(struct share_mode_entry
)*lck
->num_share_modes
;
689 /* Store any delete on close token. */
690 if (lck
->delete_token
) {
691 uint8
*p
= result
.dptr
+ offset
;
693 memcpy(p
, &lck
->delete_token
->uid
, sizeof(uid_t
));
696 memcpy(p
, &lck
->delete_token
->gid
, sizeof(gid_t
));
699 for (i
= 0; i
< lck
->delete_token
->ngroups
; i
++) {
700 memcpy(p
, &lck
->delete_token
->groups
[i
], sizeof(gid_t
));
703 offset
= p
- result
.dptr
;
706 safe_strcpy((char *)result
.dptr
+ offset
, lck
->servicepath
,
707 result
.dsize
- offset
- 1);
708 offset
+= sp_len
+ 1;
709 safe_strcpy((char *)result
.dptr
+ offset
, lck
->filename
,
710 result
.dsize
- offset
- 1);
712 if (DEBUGLEVEL
>= 10) {
713 print_share_mode_table(data
);
719 static int share_mode_lock_destructor(struct share_mode_lock
*lck
)
724 if (!lck
->modified
) {
728 data
= unparse_share_modes(lck
);
730 if (data
.dptr
== NULL
) {
732 /* There has been an entry before, delete it */
734 status
= lck
->record
->delete_rec(lck
->record
);
735 if (!NT_STATUS_IS_OK(status
)) {
736 DEBUG(0, ("delete_rec returned %s\n",
738 smb_panic("could not delete share entry");
744 status
= lck
->record
->store(lck
->record
, data
, TDB_REPLACE
);
745 if (!NT_STATUS_IS_OK(status
)) {
746 DEBUG(0, ("store returned %s\n", nt_errstr(status
)));
747 smb_panic("could not store share mode entry");
755 static bool fill_share_mode_lock(struct share_mode_lock
*lck
,
757 const char *servicepath
,
759 TDB_DATA share_mode_data
,
760 const struct timespec
*old_write_time
)
762 /* Ensure we set every field here as the destructor must be
763 valid even if parse_share_modes fails. */
765 lck
->servicepath
= NULL
;
766 lck
->filename
= NULL
;
768 lck
->num_share_modes
= 0;
769 lck
->share_modes
= NULL
;
770 lck
->delete_token
= NULL
;
771 lck
->delete_on_close
= False
;
772 ZERO_STRUCT(lck
->old_write_time
);
773 ZERO_STRUCT(lck
->changed_write_time
);
775 lck
->modified
= False
;
777 lck
->fresh
= (share_mode_data
.dptr
== NULL
);
780 if (fname
== NULL
|| servicepath
== NULL
781 || old_write_time
== NULL
) {
784 lck
->filename
= talloc_strdup(lck
, fname
);
785 lck
->servicepath
= talloc_strdup(lck
, servicepath
);
786 if (lck
->filename
== NULL
|| lck
->servicepath
== NULL
) {
787 DEBUG(0, ("talloc failed\n"));
790 lck
->old_write_time
= *old_write_time
;
792 if (!parse_share_modes(share_mode_data
, lck
)) {
793 DEBUG(0, ("Could not parse share modes\n"));
801 struct share_mode_lock
*get_share_mode_lock(TALLOC_CTX
*mem_ctx
,
802 const struct file_id id
,
803 const char *servicepath
,
805 const struct timespec
*old_write_time
)
807 struct share_mode_lock
*lck
;
809 TDB_DATA key
= locking_key(&id
, &tmp
);
811 if (!(lck
= TALLOC_P(mem_ctx
, struct share_mode_lock
))) {
812 DEBUG(0, ("talloc failed\n"));
816 if (!(lck
->record
= lock_db
->fetch_locked(lock_db
, lck
, key
))) {
817 DEBUG(3, ("Could not lock share entry\n"));
822 if (!fill_share_mode_lock(lck
, id
, servicepath
, fname
,
823 lck
->record
->value
, old_write_time
)) {
824 DEBUG(3, ("fill_share_mode_lock failed\n"));
829 talloc_set_destructor(lck
, share_mode_lock_destructor
);
834 struct share_mode_lock
*fetch_share_mode_unlocked(TALLOC_CTX
*mem_ctx
,
835 const struct file_id id
,
836 const char *servicepath
,
839 struct share_mode_lock
*lck
;
841 TDB_DATA key
= locking_key(&id
, &tmp
);
844 if (!(lck
= TALLOC_P(mem_ctx
, struct share_mode_lock
))) {
845 DEBUG(0, ("talloc failed\n"));
849 if (lock_db
->fetch(lock_db
, lck
, key
, &data
) == -1) {
850 DEBUG(3, ("Could not fetch share entry\n"));
855 if (!fill_share_mode_lock(lck
, id
, servicepath
, fname
, data
, NULL
)) {
856 DEBUG(3, ("fill_share_mode_lock failed\n"));
864 /*******************************************************************
865 Sets the service name and filename for rename.
866 At this point we emit "file renamed" messages to all
867 process id's that have this file open.
868 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
869 ********************************************************************/
871 bool rename_share_filename(struct messaging_context
*msg_ctx
,
872 struct share_mode_lock
*lck
,
873 const char *servicepath
,
882 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
883 servicepath
, newname
));
886 * rename_internal_fsp() and rename_internals() add './' to
887 * head of newname if newname does not contain a '/'.
889 while (newname
[0] && newname
[1] && newname
[0] == '.' && newname
[1] == '/') {
893 lck
->servicepath
= talloc_strdup(lck
, servicepath
);
894 lck
->filename
= talloc_strdup(lck
, newname
);
895 if (lck
->filename
== NULL
|| lck
->servicepath
== NULL
) {
896 DEBUG(0, ("rename_share_filename: talloc failed\n"));
899 lck
->modified
= True
;
901 sp_len
= strlen(lck
->servicepath
);
902 fn_len
= strlen(lck
->filename
);
904 msg_len
= MSG_FILE_RENAMED_MIN_SIZE
+ sp_len
+ 1 + fn_len
+ 1;
906 /* Set up the name changed message. */
907 frm
= TALLOC_ARRAY(lck
, char, msg_len
);
912 push_file_id_16(frm
, &lck
->id
);
914 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len
));
916 safe_strcpy(&frm
[16], lck
->servicepath
, sp_len
);
917 safe_strcpy(&frm
[16 + sp_len
+ 1], lck
->filename
, fn_len
);
919 /* Send the messages. */
920 for (i
=0; i
<lck
->num_share_modes
; i
++) {
921 struct share_mode_entry
*se
= &lck
->share_modes
[i
];
922 if (!is_valid_share_mode_entry(se
)) {
925 /* But not to ourselves... */
926 if (procid_is_me(&se
->pid
)) {
930 DEBUG(10,("rename_share_filename: sending rename message to pid %s "
931 "file_id %s sharepath %s newname %s\n",
932 procid_str_static(&se
->pid
),
933 file_id_string_tos(&lck
->id
),
934 lck
->servicepath
, lck
->filename
));
936 messaging_send_buf(msg_ctx
, se
->pid
, MSG_SMB_FILE_RENAME
,
937 (uint8
*)frm
, msg_len
);
943 struct timespec
get_write_time(struct file_id id
)
945 struct timespec result
;
946 struct share_mode_lock
*lck
;
950 if (!(lck
= fetch_share_mode_unlocked(talloc_tos(), id
, NULL
, NULL
))) {
953 result
= lck
->changed_write_time
;
955 if (null_timespec(result
)) {
956 result
= lck
->old_write_time
;
963 bool get_delete_on_close_flag(struct file_id id
)
966 struct share_mode_lock
*lck
;
968 if (!(lck
= fetch_share_mode_unlocked(talloc_tos(), id
, NULL
, NULL
))) {
971 result
= lck
->delete_on_close
;
976 bool is_valid_share_mode_entry(const struct share_mode_entry
*e
)
980 if (e
->op_type
== UNUSED_SHARE_MODE_ENTRY
) {
981 /* cope with dead entries from the process not
982 existing. These should not be considered valid,
983 otherwise we end up doing zero timeout sharing
988 num_props
+= ((e
->op_type
== NO_OPLOCK
) ? 1 : 0);
989 num_props
+= (EXCLUSIVE_OPLOCK_TYPE(e
->op_type
) ? 1 : 0);
990 num_props
+= (LEVEL_II_OPLOCK_TYPE(e
->op_type
) ? 1 : 0);
992 SMB_ASSERT(num_props
<= 1);
993 return (num_props
!= 0);
996 bool is_deferred_open_entry(const struct share_mode_entry
*e
)
998 return (e
->op_type
== DEFERRED_OPEN_ENTRY
);
1001 bool is_unused_share_mode_entry(const struct share_mode_entry
*e
)
1003 return (e
->op_type
== UNUSED_SHARE_MODE_ENTRY
);
1006 /*******************************************************************
1007 Fill a share mode entry.
1008 ********************************************************************/
1010 static void fill_share_mode_entry(struct share_mode_entry
*e
,
1012 uid_t uid
, uint16 mid
, uint16 op_type
)
1015 e
->pid
= procid_self();
1016 e
->share_access
= fsp
->share_access
;
1017 e
->private_options
= fsp
->fh
->private_options
;
1018 e
->access_mask
= fsp
->access_mask
;
1020 e
->op_type
= op_type
;
1021 e
->time
.tv_sec
= fsp
->open_time
.tv_sec
;
1022 e
->time
.tv_usec
= fsp
->open_time
.tv_usec
;
1023 e
->id
= fsp
->file_id
;
1024 e
->share_file_id
= fsp
->fh
->gen_id
;
1025 e
->uid
= (uint32
)uid
;
1026 e
->flags
= fsp
->posix_open
? SHARE_MODE_FLAG_POSIX_OPEN
: 0;
1029 static void fill_deferred_open_entry(struct share_mode_entry
*e
,
1030 const struct timeval request_time
,
1031 struct file_id id
, uint16 mid
)
1034 e
->pid
= procid_self();
1036 e
->op_type
= DEFERRED_OPEN_ENTRY
;
1037 e
->time
.tv_sec
= request_time
.tv_sec
;
1038 e
->time
.tv_usec
= request_time
.tv_usec
;
1040 e
->uid
= (uint32
)-1;
1044 static void add_share_mode_entry(struct share_mode_lock
*lck
,
1045 const 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_unused_share_mode_entry(e
)) {
1057 if (i
== lck
->num_share_modes
) {
1058 /* No unused entry found */
1059 ADD_TO_ARRAY(lck
, struct share_mode_entry
, *entry
,
1060 &lck
->share_modes
, &lck
->num_share_modes
);
1062 lck
->modified
= True
;
1065 void set_share_mode(struct share_mode_lock
*lck
, files_struct
*fsp
,
1066 uid_t uid
, uint16 mid
, uint16 op_type
, bool initial_delete_on_close_allowed
)
1068 struct share_mode_entry entry
;
1069 fill_share_mode_entry(&entry
, fsp
, uid
, mid
, op_type
);
1070 if (initial_delete_on_close_allowed
) {
1071 entry
.flags
|= SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE
;
1073 add_share_mode_entry(lck
, &entry
);
1076 void add_deferred_open(struct share_mode_lock
*lck
, uint16 mid
,
1077 struct timeval request_time
,
1080 struct share_mode_entry entry
;
1081 fill_deferred_open_entry(&entry
, request_time
, id
, mid
);
1082 add_share_mode_entry(lck
, &entry
);
1085 /*******************************************************************
1086 Check if two share mode entries are identical, ignoring oplock
1087 and mid info and desired_access. (Removed paranoia test - it's
1088 not automatically a logic error if they are identical. JRA.)
1089 ********************************************************************/
1091 static bool share_modes_identical(struct share_mode_entry
*e1
,
1092 struct share_mode_entry
*e2
)
1094 /* We used to check for e1->share_access == e2->share_access here
1095 as well as the other fields but 2 different DOS or FCB opens
1096 sharing the same share mode entry may validly differ in
1097 fsp->share_access field. */
1099 return (procid_equal(&e1
->pid
, &e2
->pid
) &&
1100 file_id_equal(&e1
->id
, &e2
->id
) &&
1101 e1
->share_file_id
== e2
->share_file_id
);
1104 static bool deferred_open_identical(struct share_mode_entry
*e1
,
1105 struct share_mode_entry
*e2
)
1107 return (procid_equal(&e1
->pid
, &e2
->pid
) &&
1108 (e1
->op_mid
== e2
->op_mid
) &&
1109 file_id_equal(&e1
->id
, &e2
->id
));
1112 static struct share_mode_entry
*find_share_mode_entry(struct share_mode_lock
*lck
,
1113 struct share_mode_entry
*entry
)
1117 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1118 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
1119 if (is_valid_share_mode_entry(entry
) &&
1120 is_valid_share_mode_entry(e
) &&
1121 share_modes_identical(e
, entry
)) {
1124 if (is_deferred_open_entry(entry
) &&
1125 is_deferred_open_entry(e
) &&
1126 deferred_open_identical(e
, entry
)) {
1133 /*******************************************************************
1134 Del the share mode of a file for this process. Return the number of
1136 ********************************************************************/
1138 bool del_share_mode(struct share_mode_lock
*lck
, files_struct
*fsp
)
1140 struct share_mode_entry entry
, *e
;
1142 /* Don't care about the pid owner being correct here - just a search. */
1143 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1145 e
= find_share_mode_entry(lck
, &entry
);
1150 e
->op_type
= UNUSED_SHARE_MODE_ENTRY
;
1151 lck
->modified
= True
;
1155 void del_deferred_open_entry(struct share_mode_lock
*lck
, uint16 mid
)
1157 struct share_mode_entry entry
, *e
;
1159 fill_deferred_open_entry(&entry
, timeval_zero(),
1162 e
= find_share_mode_entry(lck
, &entry
);
1167 e
->op_type
= UNUSED_SHARE_MODE_ENTRY
;
1168 lck
->modified
= True
;
1171 /*******************************************************************
1172 Remove an oplock mid and mode entry from a share mode.
1173 ********************************************************************/
1175 bool remove_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
1177 struct share_mode_entry entry
, *e
;
1179 /* Don't care about the pid owner being correct here - just a search. */
1180 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1182 e
= find_share_mode_entry(lck
, &entry
);
1188 e
->op_type
= NO_OPLOCK
;
1189 lck
->modified
= True
;
1193 /*******************************************************************
1194 Downgrade a oplock type from exclusive to level II.
1195 ********************************************************************/
1197 bool downgrade_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
1199 struct share_mode_entry entry
, *e
;
1201 /* Don't care about the pid owner being correct here - just a search. */
1202 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1204 e
= find_share_mode_entry(lck
, &entry
);
1209 e
->op_type
= LEVEL_II_OPLOCK
;
1210 lck
->modified
= True
;
1214 /****************************************************************************
1215 Deal with the internal needs of setting the delete on close flag. Note that
1216 as the tdb locking is recursive, it is safe to call this from within
1217 open_file_ntcreate. JRA.
1218 ****************************************************************************/
1220 NTSTATUS
can_set_delete_on_close(files_struct
*fsp
, bool delete_on_close
,
1223 if (!delete_on_close
) {
1224 return NT_STATUS_OK
;
1228 * Only allow delete on close for writable files.
1231 if ((dosmode
& aRONLY
) &&
1232 !lp_delete_readonly(SNUM(fsp
->conn
))) {
1233 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1234 "flag set but file attribute is readonly.\n",
1236 return NT_STATUS_CANNOT_DELETE
;
1240 * Only allow delete on close for writable shares.
1243 if (!CAN_WRITE(fsp
->conn
)) {
1244 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1245 "close flag set but write access denied on share.\n",
1247 return NT_STATUS_ACCESS_DENIED
;
1251 * Only allow delete on close for files/directories opened with delete
1255 if (!(fsp
->access_mask
& DELETE_ACCESS
)) {
1256 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1257 "close flag set but delete access denied.\n",
1259 return NT_STATUS_ACCESS_DENIED
;
1262 /* Don't allow delete on close for non-empty directories. */
1263 if (fsp
->is_directory
) {
1264 return can_delete_directory(fsp
->conn
, fsp
->fsp_name
);
1267 return NT_STATUS_OK
;
1270 /****************************************************************************
1271 Do we have an open file handle that created this entry ?
1272 ****************************************************************************/
1274 bool can_set_initial_delete_on_close(const struct share_mode_lock
*lck
)
1278 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1279 if (lck
->share_modes
[i
].flags
& SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE
) {
1286 /*************************************************************************
1287 Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1288 (Should this be in locking.c.... ?).
1289 *************************************************************************/
1291 static UNIX_USER_TOKEN
*copy_unix_token(TALLOC_CTX
*ctx
, UNIX_USER_TOKEN
*tok
)
1293 UNIX_USER_TOKEN
*cpy
;
1299 cpy
= TALLOC_P(ctx
, UNIX_USER_TOKEN
);
1304 cpy
->uid
= tok
->uid
;
1305 cpy
->gid
= tok
->gid
;
1306 cpy
->ngroups
= tok
->ngroups
;
1308 /* Make this a talloc child of cpy. */
1309 cpy
->groups
= TALLOC_ARRAY(cpy
, gid_t
, tok
->ngroups
);
1313 memcpy(cpy
->groups
, tok
->groups
, tok
->ngroups
* sizeof(gid_t
));
1318 /****************************************************************************
1319 Replace the delete on close token.
1320 ****************************************************************************/
1322 void set_delete_on_close_token(struct share_mode_lock
*lck
, UNIX_USER_TOKEN
*tok
)
1324 TALLOC_FREE(lck
->delete_token
); /* Also deletes groups... */
1326 /* Copy the new token (can be NULL). */
1327 lck
->delete_token
= copy_unix_token(lck
, tok
);
1328 lck
->modified
= True
;
1331 /****************************************************************************
1332 Sets the delete on close flag over all share modes on this file.
1333 Modify the share mode entry for all files open
1334 on this device and inode to tell other smbds we have
1335 changed the delete on close flag. This will be noticed
1336 in the close code, the last closer will delete the file
1338 This makes a copy of any UNIX_USER_TOKEN into the
1339 lck entry. This function is used when the lock is already granted.
1340 ****************************************************************************/
1342 void set_delete_on_close_lck(struct share_mode_lock
*lck
, bool delete_on_close
, UNIX_USER_TOKEN
*tok
)
1344 if (lck
->delete_on_close
!= delete_on_close
) {
1345 set_delete_on_close_token(lck
, tok
);
1346 lck
->delete_on_close
= delete_on_close
;
1347 if (delete_on_close
) {
1348 SMB_ASSERT(lck
->delete_token
!= NULL
);
1350 lck
->modified
= True
;
1354 bool set_delete_on_close(files_struct
*fsp
, bool delete_on_close
, UNIX_USER_TOKEN
*tok
)
1356 struct share_mode_lock
*lck
;
1358 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1359 "fnum = %d, file %s\n",
1360 delete_on_close
? "Adding" : "Removing", fsp
->fnum
,
1367 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
,
1373 set_delete_on_close_lck(lck
, delete_on_close
, tok
);
1375 if (fsp
->is_directory
) {
1376 send_stat_cache_delete_message(fsp
->fsp_name
);
1383 /****************************************************************************
1384 Sets the allow initial delete on close flag for this share mode.
1385 ****************************************************************************/
1387 bool set_allow_initial_delete_on_close(struct share_mode_lock
*lck
, files_struct
*fsp
, bool delete_on_close
)
1389 struct share_mode_entry entry
, *e
;
1391 /* Don't care about the pid owner being correct here - just a search. */
1392 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1394 e
= find_share_mode_entry(lck
, &entry
);
1399 if (delete_on_close
) {
1400 e
->flags
|= SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE
;
1402 e
->flags
&= ~SHARE_MODE_ALLOW_INITIAL_DELETE_ON_CLOSE
;
1404 lck
->modified
= True
;
1408 bool set_write_time(struct file_id fileid
, struct timespec write_time
,
1411 struct share_mode_lock
*lck
;
1413 DEBUG(5,("set_write_time: %s overwrite=%d id=%s\n",
1414 timestring(debug_ctx(),
1415 convert_timespec_to_time_t(write_time
)),
1416 overwrite
, file_id_string_tos(&fileid
)));
1418 lck
= get_share_mode_lock(NULL
, fileid
, NULL
, NULL
, NULL
);
1423 if (overwrite
|| null_timespec(lck
->changed_write_time
)) {
1424 lck
->modified
= True
;
1425 lck
->changed_write_time
= write_time
;
1432 struct forall_state
{
1433 void (*fn
)(const struct share_mode_entry
*entry
,
1434 const char *sharepath
,
1436 void *private_data
);
1440 static int traverse_fn(struct db_record
*rec
, void *_state
)
1442 struct forall_state
*state
= (struct forall_state
*)_state
;
1443 struct locking_data
*data
;
1444 struct share_mode_entry
*shares
;
1445 const char *sharepath
;
1449 /* Ensure this is a locking_key record. */
1450 if (rec
->key
.dsize
!= sizeof(struct file_id
))
1453 data
= (struct locking_data
*)rec
->value
.dptr
;
1454 shares
= (struct share_mode_entry
*)(rec
->value
.dptr
+ sizeof(*data
));
1455 sharepath
= (const char *)rec
->value
.dptr
+ sizeof(*data
) +
1456 data
->u
.s
.num_share_mode_entries
*sizeof(*shares
) +
1457 data
->u
.s
.delete_token_size
;
1458 fname
= (const char *)rec
->value
.dptr
+ sizeof(*data
) +
1459 data
->u
.s
.num_share_mode_entries
*sizeof(*shares
) +
1460 data
->u
.s
.delete_token_size
+
1461 strlen(sharepath
) + 1;
1463 for (i
=0;i
<data
->u
.s
.num_share_mode_entries
;i
++) {
1464 state
->fn(&shares
[i
], sharepath
, fname
,
1465 state
->private_data
);
1470 /*******************************************************************
1471 Call the specified function on each entry under management by the
1473 ********************************************************************/
1475 int share_mode_forall(void (*fn
)(const struct share_mode_entry
*, const char *,
1476 const char *, void *),
1479 struct forall_state state
;
1481 if (lock_db
== NULL
)
1485 state
.private_data
= private_data
;
1487 return lock_db
->traverse_read(lock_db
, traverse_fn
, (void *)&state
);