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 void init_strict_lock_struct(files_struct
*fsp
,
82 enum brl_type lock_type
,
83 struct lock_struct
*plock
)
85 SMB_ASSERT(lock_type
== READ_LOCK
|| lock_type
== WRITE_LOCK
);
87 plock
->context
.smbpid
= smbpid
;
88 plock
->context
.tid
= fsp
->conn
->cnum
;
89 plock
->context
.pid
= procid_self();
92 plock
->fnum
= fsp
->fnum
;
93 plock
->lock_type
= lock_type
;
94 plock
->lock_flav
= lp_posix_cifsu_locktype(fsp
);
97 bool strict_lock_default(files_struct
*fsp
, struct lock_struct
*plock
)
99 int strict_locking
= lp_strict_locking(fsp
->conn
->params
);
102 if (plock
->size
== 0) {
106 if (!lp_locking(fsp
->conn
->params
) || !strict_locking
) {
110 if (strict_locking
== Auto
) {
111 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
) && (plock
->lock_type
== READ_LOCK
|| plock
->lock_type
== WRITE_LOCK
)) {
112 DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp_str_dbg(fsp
)));
114 } else if ((fsp
->oplock_type
== LEVEL_II_OPLOCK
) &&
115 (plock
->lock_type
== READ_LOCK
)) {
116 DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp_str_dbg(fsp
)));
119 struct byte_range_lock
*br_lck
;
121 br_lck
= brl_get_locks_readonly(fsp
);
125 ret
= brl_locktest(br_lck
,
126 plock
->context
.smbpid
,
134 struct byte_range_lock
*br_lck
;
136 br_lck
= brl_get_locks_readonly(fsp
);
140 ret
= brl_locktest(br_lck
,
141 plock
->context
.smbpid
,
149 DEBUG(10,("strict_lock_default: flavour = %s brl start=%.0f "
150 "len=%.0f %s for fnum %d file %s\n",
151 lock_flav_name(plock
->lock_flav
),
152 (double)plock
->start
, (double)plock
->size
,
153 ret
? "unlocked" : "locked",
154 plock
->fnum
, fsp_str_dbg(fsp
)));
159 void strict_unlock_default(files_struct
*fsp
, struct lock_struct
*plock
)
163 /****************************************************************************
164 Find out if a lock could be granted - return who is blocking us if we can't.
165 ****************************************************************************/
167 NTSTATUS
query_lock(files_struct
*fsp
,
171 enum brl_type
*plock_type
,
172 enum brl_flavour lock_flav
)
174 struct byte_range_lock
*br_lck
= NULL
;
176 if (!fsp
->can_lock
) {
177 return fsp
->is_directory
? NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
180 if (!lp_locking(fsp
->conn
->params
)) {
184 br_lck
= brl_get_locks_readonly(fsp
);
186 return NT_STATUS_NO_MEMORY
;
189 return brl_lockquery(br_lck
,
198 static void increment_current_lock_count(files_struct
*fsp
,
199 enum brl_flavour lock_flav
)
201 if (lock_flav
== WINDOWS_LOCK
&&
202 fsp
->current_lock_count
!= NO_LOCKING_COUNT
) {
203 /* blocking ie. pending, locks also count here,
204 * as this is an efficiency counter to avoid checking
205 * the lock db. on close. JRA. */
207 fsp
->current_lock_count
++;
209 /* Notice that this has had a POSIX lock request.
210 * We can't count locks after this so forget them.
212 fsp
->current_lock_count
= NO_LOCKING_COUNT
;
216 static void decrement_current_lock_count(files_struct
*fsp
,
217 enum brl_flavour lock_flav
)
219 if (lock_flav
== WINDOWS_LOCK
&&
220 fsp
->current_lock_count
!= NO_LOCKING_COUNT
) {
221 SMB_ASSERT(fsp
->current_lock_count
> 0);
222 fsp
->current_lock_count
--;
226 /****************************************************************************
227 Utility function called by locking requests.
228 ****************************************************************************/
230 struct byte_range_lock
*do_lock(struct messaging_context
*msg_ctx
,
235 enum brl_type lock_type
,
236 enum brl_flavour lock_flav
,
240 struct blocking_lock_record
*blr
)
242 struct byte_range_lock
*br_lck
= NULL
;
244 if (!fsp
->can_lock
) {
245 *perr
= fsp
->is_directory
? NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
249 if (!lp_locking(fsp
->conn
->params
)) {
250 *perr
= NT_STATUS_OK
;
254 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
256 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f "
257 "blocking_lock=%s requested for fnum %d file %s\n",
258 lock_flav_name(lock_flav
), lock_type_name(lock_type
),
259 (double)offset
, (double)count
, blocking_lock
? "true" :
260 "false", fsp
->fnum
, fsp_str_dbg(fsp
)));
262 br_lck
= brl_get_locks(talloc_tos(), fsp
);
264 *perr
= NT_STATUS_NO_MEMORY
;
268 *perr
= brl_lock(msg_ctx
,
280 DEBUG(10, ("do_lock: returning status=%s\n", nt_errstr(*perr
)));
282 increment_current_lock_count(fsp
, lock_flav
);
286 /****************************************************************************
287 Utility function called by unlocking requests.
288 ****************************************************************************/
290 NTSTATUS
do_unlock(struct messaging_context
*msg_ctx
,
295 enum brl_flavour lock_flav
)
298 struct byte_range_lock
*br_lck
= NULL
;
300 if (!fsp
->can_lock
) {
301 return fsp
->is_directory
? NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
304 if (!lp_locking(fsp
->conn
->params
)) {
308 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
309 (double)offset
, (double)count
, fsp
->fnum
,
312 br_lck
= brl_get_locks(talloc_tos(), fsp
);
314 return NT_STATUS_NO_MEMORY
;
317 ok
= brl_unlock(msg_ctx
,
328 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
329 return NT_STATUS_RANGE_NOT_LOCKED
;
332 decrement_current_lock_count(fsp
, lock_flav
);
336 /****************************************************************************
337 Cancel any pending blocked locks.
338 ****************************************************************************/
340 NTSTATUS
do_lock_cancel(files_struct
*fsp
,
344 enum brl_flavour lock_flav
,
345 struct blocking_lock_record
*blr
)
348 struct byte_range_lock
*br_lck
= NULL
;
350 if (!fsp
->can_lock
) {
351 return fsp
->is_directory
?
352 NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
355 if (!lp_locking(fsp
->conn
->params
)) {
356 return NT_STATUS_DOS(ERRDOS
, ERRcancelviolation
);
359 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
360 (double)offset
, (double)count
, fsp
->fnum
,
363 br_lck
= brl_get_locks(talloc_tos(), fsp
);
365 return NT_STATUS_NO_MEMORY
;
368 ok
= brl_lock_cancel(br_lck
,
379 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
380 return NT_STATUS_DOS(ERRDOS
, ERRcancelviolation
);
383 decrement_current_lock_count(fsp
, lock_flav
);
387 /****************************************************************************
388 Remove any locks on this fd. Called from file_close().
389 ****************************************************************************/
391 void locking_close_file(struct messaging_context
*msg_ctx
,
394 struct byte_range_lock
*br_lck
;
396 if (!lp_locking(fsp
->conn
->params
)) {
400 /* If we have not outstanding locks or pending
401 * locks then we don't need to look in the lock db.
404 if (fsp
->current_lock_count
== 0) {
408 br_lck
= brl_get_locks(talloc_tos(),fsp
);
411 cancel_pending_lock_requests_by_fid(fsp
, br_lck
);
412 brl_close_fnum(msg_ctx
, br_lck
);
417 /****************************************************************************
418 Initialise the locking functions.
419 ****************************************************************************/
421 static bool locking_init_internal(bool read_only
)
428 lock_db
= db_open(NULL
, lock_path("locking.tdb"),
429 lp_open_files_db_hash_size(),
430 TDB_DEFAULT
|TDB_VOLATILE
|TDB_CLEAR_IF_FIRST
,
431 read_only
?O_RDONLY
:O_RDWR
|O_CREAT
, 0644);
434 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
438 if (!posix_locking_init(read_only
))
444 bool locking_init(void)
446 return locking_init_internal(false);
449 bool locking_init_readonly(void)
451 return locking_init_internal(true);
454 /*******************************************************************
455 Deinitialize the share_mode management.
456 ******************************************************************/
458 bool locking_end(void)
461 TALLOC_FREE(lock_db
);
465 /*******************************************************************
466 Form a static locking key for a dev/inode pair.
467 ******************************************************************/
469 static TDB_DATA
locking_key(const struct file_id
*id
, struct file_id
*tmp
)
472 return make_tdb_data((const uint8_t *)tmp
, sizeof(*tmp
));
475 /*******************************************************************
476 Print out a share mode.
477 ********************************************************************/
479 char *share_mode_str(TALLOC_CTX
*ctx
, int num
, const struct share_mode_entry
*e
)
481 return talloc_asprintf(ctx
, "share_mode_entry[%d]: %s "
482 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
483 "access_mask = 0x%x, mid = 0x%x, type= 0x%x, gen_id = %lu, "
484 "uid = %u, flags = %u, file_id %s",
486 e
->op_type
== UNUSED_SHARE_MODE_ENTRY
? "UNUSED" : "",
487 procid_str_static(&e
->pid
),
488 e
->share_access
, e
->private_options
,
489 e
->access_mask
, e
->op_mid
, e
->op_type
, e
->share_file_id
,
490 (unsigned int)e
->uid
, (unsigned int)e
->flags
,
491 file_id_string_tos(&e
->id
));
494 /*******************************************************************
495 Print out a share mode table.
496 ********************************************************************/
498 static void print_share_mode_table(struct locking_data
*data
)
500 int num_share_modes
= data
->u
.s
.num_share_mode_entries
;
501 struct share_mode_entry
*shares
=
502 (struct share_mode_entry
*)(data
+ 1);
505 for (i
= 0; i
< num_share_modes
; i
++) {
506 struct share_mode_entry entry
;
510 * We need to memcpy the entry here due to alignment
511 * restrictions that are not met when directly accessing
515 memcpy(&entry
, &shares
[i
], sizeof(struct share_mode_entry
));
516 str
= share_mode_str(talloc_tos(), i
, &entry
);
518 DEBUG(10,("print_share_mode_table: %s\n", str
? str
: ""));
523 /*******************************************************************
524 Get all share mode entries for a dev/inode pair.
525 ********************************************************************/
527 static bool parse_share_modes(const TDB_DATA dbuf
, struct share_mode_lock
*lck
)
529 struct locking_data data
;
532 if (dbuf
.dsize
< sizeof(struct locking_data
)) {
533 smb_panic("parse_share_modes: buffer too short");
536 memcpy(&data
, dbuf
.dptr
, sizeof(data
));
538 lck
->delete_on_close
= data
.u
.s
.delete_on_close
;
539 lck
->old_write_time
= data
.u
.s
.old_write_time
;
540 lck
->changed_write_time
= data
.u
.s
.changed_write_time
;
541 lck
->num_share_modes
= data
.u
.s
.num_share_mode_entries
;
543 DEBUG(10, ("parse_share_modes: delete_on_close: %d, owrt: %s, "
544 "cwrt: %s, tok: %u, num_share_modes: %d\n",
545 lck
->delete_on_close
,
546 timestring(talloc_tos(),
547 convert_timespec_to_time_t(lck
->old_write_time
)),
548 timestring(talloc_tos(),
549 convert_timespec_to_time_t(
550 lck
->changed_write_time
)),
551 (unsigned int)data
.u
.s
.delete_token_size
,
552 lck
->num_share_modes
));
554 if ((lck
->num_share_modes
< 0) || (lck
->num_share_modes
> 1000000)) {
555 DEBUG(0, ("invalid number of share modes: %d\n",
556 lck
->num_share_modes
));
557 smb_panic("parse_share_modes: invalid number of share modes");
560 lck
->share_modes
= NULL
;
562 if (lck
->num_share_modes
!= 0) {
564 if (dbuf
.dsize
< (sizeof(struct locking_data
) +
565 (lck
->num_share_modes
*
566 sizeof(struct share_mode_entry
)))) {
567 smb_panic("parse_share_modes: buffer too short");
570 lck
->share_modes
= (struct share_mode_entry
*)
572 dbuf
.dptr
+sizeof(struct locking_data
),
573 lck
->num_share_modes
*
574 sizeof(struct share_mode_entry
));
576 if (lck
->share_modes
== NULL
) {
577 smb_panic("parse_share_modes: talloc failed");
581 /* Get any delete token. */
582 if (data
.u
.s
.delete_token_size
) {
583 uint8
*p
= dbuf
.dptr
+ sizeof(struct locking_data
) +
584 (lck
->num_share_modes
*
585 sizeof(struct share_mode_entry
));
587 if ((data
.u
.s
.delete_token_size
< sizeof(uid_t
) + sizeof(gid_t
)) ||
588 ((data
.u
.s
.delete_token_size
- sizeof(uid_t
)) % sizeof(gid_t
)) != 0) {
589 DEBUG(0, ("parse_share_modes: invalid token size %d\n",
590 data
.u
.s
.delete_token_size
));
591 smb_panic("parse_share_modes: invalid token size");
594 lck
->delete_token
= TALLOC_P(lck
, UNIX_USER_TOKEN
);
595 if (!lck
->delete_token
) {
596 smb_panic("parse_share_modes: talloc failed");
599 /* Copy out the uid and gid. */
600 memcpy(&lck
->delete_token
->uid
, p
, sizeof(uid_t
));
602 memcpy(&lck
->delete_token
->gid
, p
, sizeof(gid_t
));
605 /* Any supplementary groups ? */
606 lck
->delete_token
->ngroups
= (data
.u
.s
.delete_token_size
> (sizeof(uid_t
) + sizeof(gid_t
))) ?
607 ((data
.u
.s
.delete_token_size
-
608 (sizeof(uid_t
) + sizeof(gid_t
)))/sizeof(gid_t
)) : 0;
610 if (lck
->delete_token
->ngroups
) {
611 /* Make this a talloc child of lck->delete_token. */
612 lck
->delete_token
->groups
= TALLOC_ARRAY(lck
->delete_token
, gid_t
,
613 lck
->delete_token
->ngroups
);
614 if (!lck
->delete_token
) {
615 smb_panic("parse_share_modes: talloc failed");
618 for (i
= 0; i
< lck
->delete_token
->ngroups
; i
++) {
619 memcpy(&lck
->delete_token
->groups
[i
], p
, sizeof(gid_t
));
625 lck
->delete_token
= NULL
;
628 /* Save off the associated service path and filename. */
629 lck
->servicepath
= (const char *)dbuf
.dptr
+ sizeof(struct locking_data
) +
630 (lck
->num_share_modes
* sizeof(struct share_mode_entry
)) +
631 data
.u
.s
.delete_token_size
;
633 lck
->base_name
= (const char *)dbuf
.dptr
+ sizeof(struct locking_data
) +
634 (lck
->num_share_modes
* sizeof(struct share_mode_entry
)) +
635 data
.u
.s
.delete_token_size
+
636 strlen(lck
->servicepath
) + 1;
638 lck
->stream_name
= (const char *)dbuf
.dptr
+ sizeof(struct locking_data
) +
639 (lck
->num_share_modes
* sizeof(struct share_mode_entry
)) +
640 data
.u
.s
.delete_token_size
+
641 strlen(lck
->servicepath
) + 1 +
642 strlen(lck
->base_name
) + 1;
645 * Ensure that each entry has a real process attached.
648 for (i
= 0; i
< lck
->num_share_modes
; i
++) {
649 struct share_mode_entry
*entry_p
= &lck
->share_modes
[i
];
651 if (DEBUGLEVEL
>= 10) {
652 str
= share_mode_str(NULL
, i
, entry_p
);
654 DEBUG(10,("parse_share_modes: %s\n",
656 if (!process_exists(entry_p
->pid
)) {
657 DEBUG(10,("parse_share_modes: deleted %s\n",
659 entry_p
->op_type
= UNUSED_SHARE_MODE_ENTRY
;
660 lck
->modified
= True
;
668 static TDB_DATA
unparse_share_modes(const struct share_mode_lock
*lck
)
673 struct locking_data
*data
;
675 ssize_t sp_len
, bn_len
, sn_len
;
676 uint32 delete_token_size
;
681 for (i
=0; i
<lck
->num_share_modes
; i
++) {
682 if (!is_unused_share_mode_entry(&lck
->share_modes
[i
])) {
687 if (num_valid
== 0) {
691 sp_len
= strlen(lck
->servicepath
);
692 bn_len
= strlen(lck
->base_name
);
693 sn_len
= lck
->stream_name
!= NULL
? strlen(lck
->stream_name
) : 0;
695 delete_token_size
= (lck
->delete_token
?
696 (sizeof(uid_t
) + sizeof(gid_t
) + (lck
->delete_token
->ngroups
*sizeof(gid_t
))) : 0);
698 result
.dsize
= sizeof(*data
) +
699 lck
->num_share_modes
* sizeof(struct share_mode_entry
) +
704 result
.dptr
= TALLOC_ARRAY(lck
, uint8
, result
.dsize
);
706 if (result
.dptr
== NULL
) {
707 smb_panic("talloc failed");
710 data
= (struct locking_data
*)result
.dptr
;
712 data
->u
.s
.num_share_mode_entries
= lck
->num_share_modes
;
713 data
->u
.s
.delete_on_close
= lck
->delete_on_close
;
714 data
->u
.s
.old_write_time
= lck
->old_write_time
;
715 data
->u
.s
.changed_write_time
= lck
->changed_write_time
;
716 data
->u
.s
.delete_token_size
= delete_token_size
;
718 DEBUG(10,("unparse_share_modes: del: %d, owrt: %s cwrt: %s, tok: %u, "
719 "num: %d\n", data
->u
.s
.delete_on_close
,
720 timestring(talloc_tos(),
721 convert_timespec_to_time_t(lck
->old_write_time
)),
722 timestring(talloc_tos(),
723 convert_timespec_to_time_t(
724 lck
->changed_write_time
)),
725 (unsigned int)data
->u
.s
.delete_token_size
,
726 data
->u
.s
.num_share_mode_entries
));
728 memcpy(result
.dptr
+ sizeof(*data
), lck
->share_modes
,
729 sizeof(struct share_mode_entry
)*lck
->num_share_modes
);
730 offset
= sizeof(*data
) +
731 sizeof(struct share_mode_entry
)*lck
->num_share_modes
;
733 /* Store any delete on close token. */
734 if (lck
->delete_token
) {
735 uint8
*p
= result
.dptr
+ offset
;
737 memcpy(p
, &lck
->delete_token
->uid
, sizeof(uid_t
));
740 memcpy(p
, &lck
->delete_token
->gid
, sizeof(gid_t
));
743 for (i
= 0; i
< lck
->delete_token
->ngroups
; i
++) {
744 memcpy(p
, &lck
->delete_token
->groups
[i
], sizeof(gid_t
));
747 offset
= p
- result
.dptr
;
750 safe_strcpy((char *)result
.dptr
+ offset
, lck
->servicepath
,
751 result
.dsize
- offset
- 1);
752 offset
+= sp_len
+ 1;
753 safe_strcpy((char *)result
.dptr
+ offset
, lck
->base_name
,
754 result
.dsize
- offset
- 1);
755 offset
+= bn_len
+ 1;
756 safe_strcpy((char *)result
.dptr
+ offset
, lck
->stream_name
,
757 result
.dsize
- offset
- 1);
759 if (DEBUGLEVEL
>= 10) {
760 print_share_mode_table(data
);
766 static int share_mode_lock_destructor(struct share_mode_lock
*lck
)
771 if (!lck
->modified
) {
775 data
= unparse_share_modes(lck
);
777 if (data
.dptr
== NULL
) {
779 /* There has been an entry before, delete it */
781 status
= lck
->record
->delete_rec(lck
->record
);
782 if (!NT_STATUS_IS_OK(status
)) {
785 DEBUG(0, ("delete_rec returned %s\n",
788 if (asprintf(&errmsg
, "could not delete share "
790 nt_errstr(status
)) == -1) {
791 smb_panic("could not delete share"
800 status
= lck
->record
->store(lck
->record
, data
, TDB_REPLACE
);
801 if (!NT_STATUS_IS_OK(status
)) {
804 DEBUG(0, ("store returned %s\n", nt_errstr(status
)));
806 if (asprintf(&errmsg
, "could not store share mode entry: %s",
807 nt_errstr(status
)) == -1) {
808 smb_panic("could not store share mode entry");
818 static bool fill_share_mode_lock(struct share_mode_lock
*lck
,
820 const char *servicepath
,
821 const struct smb_filename
*smb_fname
,
822 TDB_DATA share_mode_data
,
823 const struct timespec
*old_write_time
)
825 /* Ensure we set every field here as the destructor must be
826 valid even if parse_share_modes fails. */
828 lck
->servicepath
= NULL
;
829 lck
->base_name
= NULL
;
830 lck
->stream_name
= NULL
;
832 lck
->num_share_modes
= 0;
833 lck
->share_modes
= NULL
;
834 lck
->delete_token
= NULL
;
835 lck
->delete_on_close
= False
;
836 ZERO_STRUCT(lck
->old_write_time
);
837 ZERO_STRUCT(lck
->changed_write_time
);
839 lck
->modified
= False
;
841 lck
->fresh
= (share_mode_data
.dptr
== NULL
);
845 if (smb_fname
== NULL
|| servicepath
== NULL
846 || old_write_time
== NULL
) {
850 has_stream
= smb_fname
->stream_name
!= NULL
;
852 lck
->base_name
= talloc_strdup(lck
, smb_fname
->base_name
);
853 lck
->stream_name
= talloc_strdup(lck
, smb_fname
->stream_name
);
854 lck
->servicepath
= talloc_strdup(lck
, servicepath
);
855 if (lck
->base_name
== NULL
||
856 (has_stream
&& lck
->stream_name
== NULL
) ||
857 lck
->servicepath
== NULL
) {
858 DEBUG(0, ("talloc failed\n"));
861 lck
->old_write_time
= *old_write_time
;
863 if (!parse_share_modes(share_mode_data
, lck
)) {
864 DEBUG(0, ("Could not parse share modes\n"));
872 struct share_mode_lock
*get_share_mode_lock(TALLOC_CTX
*mem_ctx
,
873 const struct file_id id
,
874 const char *servicepath
,
875 const struct smb_filename
*smb_fname
,
876 const struct timespec
*old_write_time
)
878 struct share_mode_lock
*lck
;
880 TDB_DATA key
= locking_key(&id
, &tmp
);
882 if (!(lck
= TALLOC_P(mem_ctx
, struct share_mode_lock
))) {
883 DEBUG(0, ("talloc failed\n"));
887 if (!(lck
->record
= lock_db
->fetch_locked(lock_db
, lck
, key
))) {
888 DEBUG(3, ("Could not lock share entry\n"));
893 if (!fill_share_mode_lock(lck
, id
, servicepath
, smb_fname
,
894 lck
->record
->value
, old_write_time
)) {
895 DEBUG(3, ("fill_share_mode_lock failed\n"));
900 talloc_set_destructor(lck
, share_mode_lock_destructor
);
905 struct share_mode_lock
*fetch_share_mode_unlocked(TALLOC_CTX
*mem_ctx
,
906 const struct file_id id
)
908 struct share_mode_lock
*lck
;
910 TDB_DATA key
= locking_key(&id
, &tmp
);
913 if (!(lck
= TALLOC_P(mem_ctx
, struct share_mode_lock
))) {
914 DEBUG(0, ("talloc failed\n"));
918 if (lock_db
->fetch(lock_db
, lck
, key
, &data
) == -1) {
919 DEBUG(3, ("Could not fetch share entry\n"));
924 if (!fill_share_mode_lock(lck
, id
, NULL
, NULL
, data
, NULL
)) {
925 DEBUG(10, ("fetch_share_mode_unlocked: no share_mode record "
926 "around (file not open)\n"));
934 /*******************************************************************
935 Sets the service name and filename for rename.
936 At this point we emit "file renamed" messages to all
937 process id's that have this file open.
938 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
939 ********************************************************************/
941 bool rename_share_filename(struct messaging_context
*msg_ctx
,
942 struct share_mode_lock
*lck
,
943 const char *servicepath
,
944 const struct smb_filename
*smb_fname_dst
)
952 bool strip_two_chars
= false;
953 bool has_stream
= smb_fname_dst
->stream_name
!= NULL
;
955 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
956 servicepath
, smb_fname_dst
->base_name
));
959 * rename_internal_fsp() and rename_internals() add './' to
960 * head of newname if newname does not contain a '/'.
962 if (smb_fname_dst
->base_name
[0] &&
963 smb_fname_dst
->base_name
[1] &&
964 smb_fname_dst
->base_name
[0] == '.' &&
965 smb_fname_dst
->base_name
[1] == '/') {
966 strip_two_chars
= true;
969 lck
->servicepath
= talloc_strdup(lck
, servicepath
);
970 lck
->base_name
= talloc_strdup(lck
, smb_fname_dst
->base_name
+
971 (strip_two_chars
? 2 : 0));
972 lck
->stream_name
= talloc_strdup(lck
, smb_fname_dst
->stream_name
);
973 if (lck
->base_name
== NULL
||
974 (has_stream
&& lck
->stream_name
== NULL
) ||
975 lck
->servicepath
== NULL
) {
976 DEBUG(0, ("rename_share_filename: talloc failed\n"));
979 lck
->modified
= True
;
981 sp_len
= strlen(lck
->servicepath
);
982 bn_len
= strlen(lck
->base_name
);
983 sn_len
= has_stream
? strlen(lck
->stream_name
) : 0;
985 msg_len
= MSG_FILE_RENAMED_MIN_SIZE
+ sp_len
+ 1 + bn_len
+ 1 +
988 /* Set up the name changed message. */
989 frm
= TALLOC_ARRAY(lck
, char, msg_len
);
994 push_file_id_24(frm
, &lck
->id
);
996 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len
));
998 safe_strcpy(&frm
[24], lck
->servicepath
, sp_len
);
999 safe_strcpy(&frm
[24 + sp_len
+ 1], lck
->base_name
, bn_len
);
1000 safe_strcpy(&frm
[24 + sp_len
+ 1 + bn_len
+ 1], lck
->stream_name
,
1003 /* Send the messages. */
1004 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1005 struct share_mode_entry
*se
= &lck
->share_modes
[i
];
1006 if (!is_valid_share_mode_entry(se
)) {
1009 /* But not to ourselves... */
1010 if (procid_is_me(&se
->pid
)) {
1014 DEBUG(10,("rename_share_filename: sending rename message to "
1015 "pid %s file_id %s sharepath %s base_name %s "
1017 procid_str_static(&se
->pid
),
1018 file_id_string_tos(&lck
->id
),
1019 lck
->servicepath
, lck
->base_name
,
1020 has_stream
? lck
->stream_name
: ""));
1022 messaging_send_buf(msg_ctx
, se
->pid
, MSG_SMB_FILE_RENAME
,
1023 (uint8
*)frm
, msg_len
);
1029 void get_file_infos(struct file_id id
,
1030 bool *delete_on_close
,
1031 struct timespec
*write_time
)
1033 struct share_mode_lock
*lck
;
1035 if (delete_on_close
) {
1036 *delete_on_close
= false;
1040 ZERO_STRUCTP(write_time
);
1043 if (!(lck
= fetch_share_mode_unlocked(talloc_tos(), id
))) {
1047 if (delete_on_close
) {
1048 *delete_on_close
= lck
->delete_on_close
;
1054 wt
= lck
->changed_write_time
;
1055 if (null_timespec(wt
)) {
1056 wt
= lck
->old_write_time
;
1065 bool is_valid_share_mode_entry(const struct share_mode_entry
*e
)
1069 if (e
->op_type
== UNUSED_SHARE_MODE_ENTRY
) {
1070 /* cope with dead entries from the process not
1071 existing. These should not be considered valid,
1072 otherwise we end up doing zero timeout sharing
1077 num_props
+= ((e
->op_type
== NO_OPLOCK
) ? 1 : 0);
1078 num_props
+= (EXCLUSIVE_OPLOCK_TYPE(e
->op_type
) ? 1 : 0);
1079 num_props
+= (LEVEL_II_OPLOCK_TYPE(e
->op_type
) ? 1 : 0);
1081 SMB_ASSERT(num_props
<= 1);
1082 return (num_props
!= 0);
1085 bool is_deferred_open_entry(const struct share_mode_entry
*e
)
1087 return (e
->op_type
== DEFERRED_OPEN_ENTRY
);
1090 bool is_unused_share_mode_entry(const struct share_mode_entry
*e
)
1092 return (e
->op_type
== UNUSED_SHARE_MODE_ENTRY
);
1095 /*******************************************************************
1096 Fill a share mode entry.
1097 ********************************************************************/
1099 static void fill_share_mode_entry(struct share_mode_entry
*e
,
1101 uid_t uid
, uint16 mid
, uint16 op_type
)
1104 e
->pid
= procid_self();
1105 e
->share_access
= fsp
->share_access
;
1106 e
->private_options
= fsp
->fh
->private_options
;
1107 e
->access_mask
= fsp
->access_mask
;
1109 e
->op_type
= op_type
;
1110 e
->time
.tv_sec
= fsp
->open_time
.tv_sec
;
1111 e
->time
.tv_usec
= fsp
->open_time
.tv_usec
;
1112 e
->id
= fsp
->file_id
;
1113 e
->share_file_id
= fsp
->fh
->gen_id
;
1114 e
->uid
= (uint32
)uid
;
1115 e
->flags
= fsp
->posix_open
? SHARE_MODE_FLAG_POSIX_OPEN
: 0;
1118 static void fill_deferred_open_entry(struct share_mode_entry
*e
,
1119 const struct timeval request_time
,
1120 struct file_id id
, uint16 mid
)
1123 e
->pid
= procid_self();
1125 e
->op_type
= DEFERRED_OPEN_ENTRY
;
1126 e
->time
.tv_sec
= request_time
.tv_sec
;
1127 e
->time
.tv_usec
= request_time
.tv_usec
;
1129 e
->uid
= (uint32
)-1;
1133 static void add_share_mode_entry(struct share_mode_lock
*lck
,
1134 const struct share_mode_entry
*entry
)
1138 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1139 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
1140 if (is_unused_share_mode_entry(e
)) {
1146 if (i
== lck
->num_share_modes
) {
1147 /* No unused entry found */
1148 ADD_TO_ARRAY(lck
, struct share_mode_entry
, *entry
,
1149 &lck
->share_modes
, &lck
->num_share_modes
);
1151 lck
->modified
= True
;
1154 void set_share_mode(struct share_mode_lock
*lck
, files_struct
*fsp
,
1155 uid_t uid
, uint16 mid
, uint16 op_type
)
1157 struct share_mode_entry entry
;
1158 fill_share_mode_entry(&entry
, fsp
, uid
, mid
, op_type
);
1159 add_share_mode_entry(lck
, &entry
);
1162 void add_deferred_open(struct share_mode_lock
*lck
, uint16 mid
,
1163 struct timeval request_time
,
1166 struct share_mode_entry entry
;
1167 fill_deferred_open_entry(&entry
, request_time
, id
, mid
);
1168 add_share_mode_entry(lck
, &entry
);
1171 /*******************************************************************
1172 Check if two share mode entries are identical, ignoring oplock
1173 and mid info and desired_access. (Removed paranoia test - it's
1174 not automatically a logic error if they are identical. JRA.)
1175 ********************************************************************/
1177 static bool share_modes_identical(struct share_mode_entry
*e1
,
1178 struct share_mode_entry
*e2
)
1180 /* We used to check for e1->share_access == e2->share_access here
1181 as well as the other fields but 2 different DOS or FCB opens
1182 sharing the same share mode entry may validly differ in
1183 fsp->share_access field. */
1185 return (procid_equal(&e1
->pid
, &e2
->pid
) &&
1186 file_id_equal(&e1
->id
, &e2
->id
) &&
1187 e1
->share_file_id
== e2
->share_file_id
);
1190 static bool deferred_open_identical(struct share_mode_entry
*e1
,
1191 struct share_mode_entry
*e2
)
1193 return (procid_equal(&e1
->pid
, &e2
->pid
) &&
1194 (e1
->op_mid
== e2
->op_mid
) &&
1195 file_id_equal(&e1
->id
, &e2
->id
));
1198 static struct share_mode_entry
*find_share_mode_entry(struct share_mode_lock
*lck
,
1199 struct share_mode_entry
*entry
)
1203 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1204 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
1205 if (is_valid_share_mode_entry(entry
) &&
1206 is_valid_share_mode_entry(e
) &&
1207 share_modes_identical(e
, entry
)) {
1210 if (is_deferred_open_entry(entry
) &&
1211 is_deferred_open_entry(e
) &&
1212 deferred_open_identical(e
, entry
)) {
1219 /*******************************************************************
1220 Del the share mode of a file for this process. Return the number of
1222 ********************************************************************/
1224 bool del_share_mode(struct share_mode_lock
*lck
, files_struct
*fsp
)
1226 struct share_mode_entry entry
, *e
;
1228 /* Don't care about the pid owner being correct here - just a search. */
1229 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1231 e
= find_share_mode_entry(lck
, &entry
);
1236 e
->op_type
= UNUSED_SHARE_MODE_ENTRY
;
1237 lck
->modified
= True
;
1241 void del_deferred_open_entry(struct share_mode_lock
*lck
, uint16 mid
)
1243 struct share_mode_entry entry
, *e
;
1245 fill_deferred_open_entry(&entry
, timeval_zero(),
1248 e
= find_share_mode_entry(lck
, &entry
);
1253 e
->op_type
= UNUSED_SHARE_MODE_ENTRY
;
1254 lck
->modified
= True
;
1257 /*******************************************************************
1258 Remove an oplock mid and mode entry from a share mode.
1259 ********************************************************************/
1261 bool remove_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
1263 struct share_mode_entry entry
, *e
;
1265 /* Don't care about the pid owner being correct here - just a search. */
1266 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1268 e
= find_share_mode_entry(lck
, &entry
);
1274 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1276 * Going from exclusive or batch,
1277 * we always go through FAKE_LEVEL_II
1280 e
->op_type
= FAKE_LEVEL_II_OPLOCK
;
1282 e
->op_type
= NO_OPLOCK
;
1284 lck
->modified
= True
;
1288 /*******************************************************************
1289 Downgrade a oplock type from exclusive to level II.
1290 ********************************************************************/
1292 bool downgrade_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
1294 struct share_mode_entry entry
, *e
;
1296 /* Don't care about the pid owner being correct here - just a search. */
1297 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1299 e
= find_share_mode_entry(lck
, &entry
);
1304 e
->op_type
= LEVEL_II_OPLOCK
;
1305 lck
->modified
= True
;
1309 /****************************************************************************
1310 Check if setting delete on close is allowed on this fsp.
1311 ****************************************************************************/
1313 NTSTATUS
can_set_delete_on_close(files_struct
*fsp
, uint32 dosmode
)
1316 * Only allow delete on close for writable files.
1319 if ((dosmode
& aRONLY
) &&
1320 !lp_delete_readonly(SNUM(fsp
->conn
))) {
1321 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1322 "flag set but file attribute is readonly.\n",
1324 return NT_STATUS_CANNOT_DELETE
;
1328 * Only allow delete on close for writable shares.
1331 if (!CAN_WRITE(fsp
->conn
)) {
1332 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1333 "close flag set but write access denied on share.\n",
1335 return NT_STATUS_ACCESS_DENIED
;
1339 * Only allow delete on close for files/directories opened with delete
1343 if (!(fsp
->access_mask
& DELETE_ACCESS
)) {
1344 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1345 "close flag set but delete access denied.\n",
1347 return NT_STATUS_ACCESS_DENIED
;
1350 /* Don't allow delete on close for non-empty directories. */
1351 if (fsp
->is_directory
) {
1352 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp
->fsp_name
));
1353 return can_delete_directory(fsp
->conn
,
1354 fsp
->fsp_name
->base_name
);
1357 return NT_STATUS_OK
;
1360 /*************************************************************************
1361 Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1362 (Should this be in locking.c.... ?).
1363 *************************************************************************/
1365 static UNIX_USER_TOKEN
*copy_unix_token(TALLOC_CTX
*ctx
, const UNIX_USER_TOKEN
*tok
)
1367 UNIX_USER_TOKEN
*cpy
;
1373 cpy
= TALLOC_P(ctx
, UNIX_USER_TOKEN
);
1378 cpy
->uid
= tok
->uid
;
1379 cpy
->gid
= tok
->gid
;
1380 cpy
->ngroups
= tok
->ngroups
;
1382 /* Make this a talloc child of cpy. */
1383 cpy
->groups
= TALLOC_ARRAY(cpy
, gid_t
, tok
->ngroups
);
1387 memcpy(cpy
->groups
, tok
->groups
, tok
->ngroups
* sizeof(gid_t
));
1392 /****************************************************************************
1393 Replace the delete on close token.
1394 ****************************************************************************/
1396 void set_delete_on_close_token(struct share_mode_lock
*lck
, const UNIX_USER_TOKEN
*tok
)
1398 TALLOC_FREE(lck
->delete_token
); /* Also deletes groups... */
1400 /* Copy the new token (can be NULL). */
1401 lck
->delete_token
= copy_unix_token(lck
, tok
);
1402 lck
->modified
= True
;
1405 /****************************************************************************
1406 Sets the delete on close flag over all share modes on this file.
1407 Modify the share mode entry for all files open
1408 on this device and inode to tell other smbds we have
1409 changed the delete on close flag. This will be noticed
1410 in the close code, the last closer will delete the file
1412 This makes a copy of any UNIX_USER_TOKEN into the
1413 lck entry. This function is used when the lock is already granted.
1414 ****************************************************************************/
1416 void set_delete_on_close_lck(struct share_mode_lock
*lck
, bool delete_on_close
, const UNIX_USER_TOKEN
*tok
)
1418 if (lck
->delete_on_close
!= delete_on_close
) {
1419 set_delete_on_close_token(lck
, tok
);
1420 lck
->delete_on_close
= delete_on_close
;
1421 if (delete_on_close
) {
1422 SMB_ASSERT(lck
->delete_token
!= NULL
);
1424 lck
->modified
= True
;
1428 bool set_delete_on_close(files_struct
*fsp
, bool delete_on_close
, const UNIX_USER_TOKEN
*tok
)
1430 UNIX_USER_TOKEN
*tok_copy
= NULL
;
1431 struct share_mode_lock
*lck
;
1433 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1434 "fnum = %d, file %s\n",
1435 delete_on_close
? "Adding" : "Removing", fsp
->fnum
,
1438 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
,
1444 if (fsp
->conn
->admin_user
) {
1445 tok_copy
= copy_unix_token(lck
, tok
);
1446 if (tok_copy
== NULL
) {
1450 tok_copy
->uid
= (uid_t
)0;
1454 set_delete_on_close_lck(lck
, delete_on_close
, tok
);
1456 if (fsp
->is_directory
) {
1457 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp
->fsp_name
));
1458 send_stat_cache_delete_message(fsp
->fsp_name
->base_name
);
1465 bool set_sticky_write_time(struct file_id fileid
, struct timespec write_time
)
1467 struct share_mode_lock
*lck
;
1469 DEBUG(5,("set_sticky_write_time: %s id=%s\n",
1470 timestring(talloc_tos(),
1471 convert_timespec_to_time_t(write_time
)),
1472 file_id_string_tos(&fileid
)));
1474 lck
= get_share_mode_lock(NULL
, fileid
, NULL
, NULL
, NULL
);
1479 if (timespec_compare(&lck
->changed_write_time
, &write_time
) != 0) {
1480 lck
->modified
= True
;
1481 lck
->changed_write_time
= write_time
;
1488 bool set_write_time(struct file_id fileid
, struct timespec write_time
)
1490 struct share_mode_lock
*lck
;
1492 DEBUG(5,("set_write_time: %s id=%s\n",
1493 timestring(talloc_tos(),
1494 convert_timespec_to_time_t(write_time
)),
1495 file_id_string_tos(&fileid
)));
1497 lck
= get_share_mode_lock(NULL
, fileid
, NULL
, NULL
, NULL
);
1502 if (timespec_compare(&lck
->old_write_time
, &write_time
) != 0) {
1503 lck
->modified
= True
;
1504 lck
->old_write_time
= write_time
;
1512 struct forall_state
{
1513 void (*fn
)(const struct share_mode_entry
*entry
,
1514 const char *sharepath
,
1516 void *private_data
);
1520 static int traverse_fn(struct db_record
*rec
, void *_state
)
1522 struct forall_state
*state
= (struct forall_state
*)_state
;
1523 struct locking_data
*data
;
1524 struct share_mode_entry
*shares
;
1525 const char *sharepath
;
1529 /* Ensure this is a locking_key record. */
1530 if (rec
->key
.dsize
!= sizeof(struct file_id
))
1533 data
= (struct locking_data
*)rec
->value
.dptr
;
1534 shares
= (struct share_mode_entry
*)(rec
->value
.dptr
+ sizeof(*data
));
1535 sharepath
= (const char *)rec
->value
.dptr
+ sizeof(*data
) +
1536 data
->u
.s
.num_share_mode_entries
*sizeof(*shares
) +
1537 data
->u
.s
.delete_token_size
;
1538 fname
= (const char *)rec
->value
.dptr
+ sizeof(*data
) +
1539 data
->u
.s
.num_share_mode_entries
*sizeof(*shares
) +
1540 data
->u
.s
.delete_token_size
+
1541 strlen(sharepath
) + 1;
1543 for (i
=0;i
<data
->u
.s
.num_share_mode_entries
;i
++) {
1544 state
->fn(&shares
[i
], sharepath
, fname
,
1545 state
->private_data
);
1550 /*******************************************************************
1551 Call the specified function on each entry under management by the
1553 ********************************************************************/
1555 int share_mode_forall(void (*fn
)(const struct share_mode_entry
*, const char *,
1556 const char *, void *),
1559 struct forall_state state
;
1561 if (lock_db
== NULL
)
1565 state
.private_data
= private_data
;
1567 return lock_db
->traverse_read(lock_db
, traverse_fn
, (void *)&state
);