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
,
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 static void increment_current_lock_count(files_struct
*fsp
,
181 enum brl_flavour lock_flav
)
183 if (lock_flav
== WINDOWS_LOCK
&&
184 fsp
->current_lock_count
!= NO_LOCKING_COUNT
) {
185 /* blocking ie. pending, locks also count here,
186 * as this is an efficiency counter to avoid checking
187 * the lock db. on close. JRA. */
189 fsp
->current_lock_count
++;
191 /* Notice that this has had a POSIX lock request.
192 * We can't count locks after this so forget them.
194 fsp
->current_lock_count
= NO_LOCKING_COUNT
;
198 static void decrement_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 SMB_ASSERT(fsp
->current_lock_count
> 0);
204 fsp
->current_lock_count
--;
208 /****************************************************************************
209 Utility function called by locking requests.
210 ****************************************************************************/
212 struct byte_range_lock
*do_lock(struct messaging_context
*msg_ctx
,
217 enum brl_type lock_type
,
218 enum brl_flavour lock_flav
,
222 struct blocking_lock_record
*blr
)
224 struct byte_range_lock
*br_lck
= NULL
;
226 if (!fsp
->can_lock
) {
227 *perr
= fsp
->is_directory
? NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
231 if (!lp_locking(fsp
->conn
->params
)) {
232 *perr
= NT_STATUS_OK
;
236 /* NOTE! 0 byte long ranges ARE allowed and should be stored */
238 DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f "
239 "blocking_lock=%s requested for fnum %d file %s\n",
240 lock_flav_name(lock_flav
), lock_type_name(lock_type
),
241 (double)offset
, (double)count
, blocking_lock
? "true" :
242 "false", fsp
->fnum
, fsp
->fsp_name
));
244 br_lck
= brl_get_locks(talloc_tos(), fsp
);
246 *perr
= NT_STATUS_NO_MEMORY
;
250 *perr
= brl_lock(msg_ctx
,
262 DEBUG(10, ("do_lock: returning status=%s\n", nt_errstr(*perr
)));
264 increment_current_lock_count(fsp
, lock_flav
);
268 /****************************************************************************
269 Utility function called by unlocking requests.
270 ****************************************************************************/
272 NTSTATUS
do_unlock(struct messaging_context
*msg_ctx
,
277 enum brl_flavour lock_flav
)
280 struct byte_range_lock
*br_lck
= NULL
;
282 if (!fsp
->can_lock
) {
283 return fsp
->is_directory
? NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
286 if (!lp_locking(fsp
->conn
->params
)) {
290 DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
291 (double)offset
, (double)count
, fsp
->fnum
, fsp
->fsp_name
));
293 br_lck
= brl_get_locks(talloc_tos(), fsp
);
295 return NT_STATUS_NO_MEMORY
;
298 ok
= brl_unlock(msg_ctx
,
309 DEBUG(10,("do_unlock: returning ERRlock.\n" ));
310 return NT_STATUS_RANGE_NOT_LOCKED
;
313 decrement_current_lock_count(fsp
, lock_flav
);
317 /****************************************************************************
318 Cancel any pending blocked locks.
319 ****************************************************************************/
321 NTSTATUS
do_lock_cancel(files_struct
*fsp
,
325 enum brl_flavour lock_flav
,
326 struct blocking_lock_record
*blr
)
329 struct byte_range_lock
*br_lck
= NULL
;
333 if (!fsp
->can_lock
) {
334 return fsp
->is_directory
?
335 NT_STATUS_INVALID_DEVICE_REQUEST
: NT_STATUS_INVALID_HANDLE
;
338 if (!lp_locking(fsp
->conn
->params
)) {
339 return NT_STATUS_DOS(ERRDOS
, ERRcancelviolation
);
342 DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
343 (double)offset
, (double)count
, fsp
->fnum
, fsp
->fsp_name
));
345 br_lck
= brl_get_locks(talloc_tos(), fsp
);
347 return NT_STATUS_NO_MEMORY
;
350 ok
= brl_lock_cancel(br_lck
,
361 DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
362 return NT_STATUS_DOS(ERRDOS
, ERRcancelviolation
);
365 decrement_current_lock_count(fsp
, lock_flav
);
369 /****************************************************************************
370 Remove any locks on this fd. Called from file_close().
371 ****************************************************************************/
373 void locking_close_file(struct messaging_context
*msg_ctx
,
376 struct byte_range_lock
*br_lck
;
378 if (!lp_locking(fsp
->conn
->params
)) {
382 /* If we have not outstanding locks or pending
383 * locks then we don't need to look in the lock db.
386 if (fsp
->current_lock_count
== 0) {
390 br_lck
= brl_get_locks(talloc_tos(),fsp
);
393 cancel_pending_lock_requests_by_fid(fsp
, br_lck
);
394 brl_close_fnum(msg_ctx
, br_lck
);
399 /****************************************************************************
400 Initialise the locking functions.
401 ****************************************************************************/
403 static bool locking_init_internal(bool read_only
)
410 lock_db
= db_open(NULL
, lock_path("locking.tdb"),
411 lp_open_files_db_hash_size(),
412 TDB_DEFAULT
|TDB_VOLATILE
|TDB_CLEAR_IF_FIRST
,
413 read_only
?O_RDONLY
:O_RDWR
|O_CREAT
, 0644);
416 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
420 if (!posix_locking_init(read_only
))
426 bool locking_init(void)
428 return locking_init_internal(false);
431 bool locking_init_readonly(void)
433 return locking_init_internal(true);
436 /*******************************************************************
437 Deinitialize the share_mode management.
438 ******************************************************************/
440 bool locking_end(void)
443 TALLOC_FREE(lock_db
);
447 /*******************************************************************
448 Form a static locking key for a dev/inode pair.
449 ******************************************************************/
451 static TDB_DATA
locking_key(const struct file_id
*id
, struct file_id
*tmp
)
454 return make_tdb_data((const uint8_t *)tmp
, sizeof(*tmp
));
457 /*******************************************************************
458 Print out a share mode.
459 ********************************************************************/
461 char *share_mode_str(TALLOC_CTX
*ctx
, int num
, const struct share_mode_entry
*e
)
463 return talloc_asprintf(ctx
, "share_mode_entry[%d]: %s "
464 "pid = %s, share_access = 0x%x, private_options = 0x%x, "
465 "access_mask = 0x%x, mid = 0x%x, type= 0x%x, gen_id = %lu, "
466 "uid = %u, flags = %u, file_id %s",
468 e
->op_type
== UNUSED_SHARE_MODE_ENTRY
? "UNUSED" : "",
469 procid_str_static(&e
->pid
),
470 e
->share_access
, e
->private_options
,
471 e
->access_mask
, e
->op_mid
, e
->op_type
, e
->share_file_id
,
472 (unsigned int)e
->uid
, (unsigned int)e
->flags
,
473 file_id_string_tos(&e
->id
));
476 /*******************************************************************
477 Print out a share mode table.
478 ********************************************************************/
480 static void print_share_mode_table(struct locking_data
*data
)
482 int num_share_modes
= data
->u
.s
.num_share_mode_entries
;
483 struct share_mode_entry
*shares
=
484 (struct share_mode_entry
*)(data
+ 1);
487 for (i
= 0; i
< num_share_modes
; i
++) {
488 struct share_mode_entry entry
;
492 * We need to memcpy the entry here due to alignment
493 * restrictions that are not met when directly accessing
497 memcpy(&entry
, &shares
[i
], sizeof(struct share_mode_entry
));
498 str
= share_mode_str(talloc_tos(), i
, &entry
);
500 DEBUG(10,("print_share_mode_table: %s\n", str
? str
: ""));
505 /*******************************************************************
506 Get all share mode entries for a dev/inode pair.
507 ********************************************************************/
509 static bool parse_share_modes(TDB_DATA dbuf
, struct share_mode_lock
*lck
)
511 struct locking_data data
;
514 if (dbuf
.dsize
< sizeof(struct locking_data
)) {
515 smb_panic("parse_share_modes: buffer too short");
518 memcpy(&data
, dbuf
.dptr
, sizeof(data
));
520 lck
->delete_on_close
= data
.u
.s
.delete_on_close
;
521 lck
->old_write_time
= data
.u
.s
.old_write_time
;
522 lck
->changed_write_time
= data
.u
.s
.changed_write_time
;
523 lck
->num_share_modes
= data
.u
.s
.num_share_mode_entries
;
525 DEBUG(10, ("parse_share_modes: delete_on_close: %d, owrt: %s, "
526 "cwrt: %s, tok: %u, num_share_modes: %d\n",
527 lck
->delete_on_close
,
528 timestring(debug_ctx(),
529 convert_timespec_to_time_t(lck
->old_write_time
)),
530 timestring(debug_ctx(),
531 convert_timespec_to_time_t(
532 lck
->changed_write_time
)),
533 (unsigned int)data
.u
.s
.delete_token_size
,
534 lck
->num_share_modes
));
536 if ((lck
->num_share_modes
< 0) || (lck
->num_share_modes
> 1000000)) {
537 DEBUG(0, ("invalid number of share modes: %d\n",
538 lck
->num_share_modes
));
539 smb_panic("parse_share_modes: invalid number of share modes");
542 lck
->share_modes
= NULL
;
544 if (lck
->num_share_modes
!= 0) {
546 if (dbuf
.dsize
< (sizeof(struct locking_data
) +
547 (lck
->num_share_modes
*
548 sizeof(struct share_mode_entry
)))) {
549 smb_panic("parse_share_modes: buffer too short");
552 lck
->share_modes
= (struct share_mode_entry
*)
554 dbuf
.dptr
+sizeof(struct locking_data
),
555 lck
->num_share_modes
*
556 sizeof(struct share_mode_entry
));
558 if (lck
->share_modes
== NULL
) {
559 smb_panic("parse_share_modes: talloc failed");
563 /* Get any delete token. */
564 if (data
.u
.s
.delete_token_size
) {
565 uint8
*p
= dbuf
.dptr
+ sizeof(struct locking_data
) +
566 (lck
->num_share_modes
*
567 sizeof(struct share_mode_entry
));
569 if ((data
.u
.s
.delete_token_size
< sizeof(uid_t
) + sizeof(gid_t
)) ||
570 ((data
.u
.s
.delete_token_size
- sizeof(uid_t
)) % sizeof(gid_t
)) != 0) {
571 DEBUG(0, ("parse_share_modes: invalid token size %d\n",
572 data
.u
.s
.delete_token_size
));
573 smb_panic("parse_share_modes: invalid token size");
576 lck
->delete_token
= TALLOC_P(lck
, UNIX_USER_TOKEN
);
577 if (!lck
->delete_token
) {
578 smb_panic("parse_share_modes: talloc failed");
581 /* Copy out the uid and gid. */
582 memcpy(&lck
->delete_token
->uid
, p
, sizeof(uid_t
));
584 memcpy(&lck
->delete_token
->gid
, p
, sizeof(gid_t
));
587 /* Any supplementary groups ? */
588 lck
->delete_token
->ngroups
= (data
.u
.s
.delete_token_size
> (sizeof(uid_t
) + sizeof(gid_t
))) ?
589 ((data
.u
.s
.delete_token_size
-
590 (sizeof(uid_t
) + sizeof(gid_t
)))/sizeof(gid_t
)) : 0;
592 if (lck
->delete_token
->ngroups
) {
593 /* Make this a talloc child of lck->delete_token. */
594 lck
->delete_token
->groups
= TALLOC_ARRAY(lck
->delete_token
, gid_t
,
595 lck
->delete_token
->ngroups
);
596 if (!lck
->delete_token
) {
597 smb_panic("parse_share_modes: talloc failed");
600 for (i
= 0; i
< lck
->delete_token
->ngroups
; i
++) {
601 memcpy(&lck
->delete_token
->groups
[i
], p
, sizeof(gid_t
));
607 lck
->delete_token
= NULL
;
610 /* Save off the associated service path and filename. */
611 lck
->servicepath
= (const char *)dbuf
.dptr
+ sizeof(struct locking_data
) +
612 (lck
->num_share_modes
* sizeof(struct share_mode_entry
)) +
613 data
.u
.s
.delete_token_size
;
615 lck
->filename
= (const char *)dbuf
.dptr
+ sizeof(struct locking_data
) +
616 (lck
->num_share_modes
* sizeof(struct share_mode_entry
)) +
617 data
.u
.s
.delete_token_size
+
618 strlen(lck
->servicepath
) + 1;
621 * Ensure that each entry has a real process attached.
624 for (i
= 0; i
< lck
->num_share_modes
; i
++) {
625 struct share_mode_entry
*entry_p
= &lck
->share_modes
[i
];
627 if (DEBUGLEVEL
>= 10) {
628 str
= share_mode_str(NULL
, i
, entry_p
);
630 DEBUG(10,("parse_share_modes: %s\n",
632 if (!process_exists(entry_p
->pid
)) {
633 DEBUG(10,("parse_share_modes: deleted %s\n",
635 entry_p
->op_type
= UNUSED_SHARE_MODE_ENTRY
;
636 lck
->modified
= True
;
644 static TDB_DATA
unparse_share_modes(struct share_mode_lock
*lck
)
649 struct locking_data
*data
;
652 uint32 delete_token_size
;
657 for (i
=0; i
<lck
->num_share_modes
; i
++) {
658 if (!is_unused_share_mode_entry(&lck
->share_modes
[i
])) {
663 if (num_valid
== 0) {
667 sp_len
= strlen(lck
->servicepath
);
668 delete_token_size
= (lck
->delete_token
?
669 (sizeof(uid_t
) + sizeof(gid_t
) + (lck
->delete_token
->ngroups
*sizeof(gid_t
))) : 0);
671 result
.dsize
= sizeof(*data
) +
672 lck
->num_share_modes
* sizeof(struct share_mode_entry
) +
675 strlen(lck
->filename
) + 1;
676 result
.dptr
= TALLOC_ARRAY(lck
, uint8
, result
.dsize
);
678 if (result
.dptr
== NULL
) {
679 smb_panic("talloc failed");
682 data
= (struct locking_data
*)result
.dptr
;
684 data
->u
.s
.num_share_mode_entries
= lck
->num_share_modes
;
685 data
->u
.s
.delete_on_close
= lck
->delete_on_close
;
686 data
->u
.s
.old_write_time
= lck
->old_write_time
;
687 data
->u
.s
.changed_write_time
= lck
->changed_write_time
;
688 data
->u
.s
.delete_token_size
= delete_token_size
;
690 DEBUG(10,("unparse_share_modes: del: %d, owrt: %s cwrt: %s, tok: %u, "
691 "num: %d\n", data
->u
.s
.delete_on_close
,
692 timestring(debug_ctx(),
693 convert_timespec_to_time_t(lck
->old_write_time
)),
694 timestring(debug_ctx(),
695 convert_timespec_to_time_t(
696 lck
->changed_write_time
)),
697 (unsigned int)data
->u
.s
.delete_token_size
,
698 data
->u
.s
.num_share_mode_entries
));
700 memcpy(result
.dptr
+ sizeof(*data
), lck
->share_modes
,
701 sizeof(struct share_mode_entry
)*lck
->num_share_modes
);
702 offset
= sizeof(*data
) +
703 sizeof(struct share_mode_entry
)*lck
->num_share_modes
;
705 /* Store any delete on close token. */
706 if (lck
->delete_token
) {
707 uint8
*p
= result
.dptr
+ offset
;
709 memcpy(p
, &lck
->delete_token
->uid
, sizeof(uid_t
));
712 memcpy(p
, &lck
->delete_token
->gid
, sizeof(gid_t
));
715 for (i
= 0; i
< lck
->delete_token
->ngroups
; i
++) {
716 memcpy(p
, &lck
->delete_token
->groups
[i
], sizeof(gid_t
));
719 offset
= p
- result
.dptr
;
722 safe_strcpy((char *)result
.dptr
+ offset
, lck
->servicepath
,
723 result
.dsize
- offset
- 1);
724 offset
+= sp_len
+ 1;
725 safe_strcpy((char *)result
.dptr
+ offset
, lck
->filename
,
726 result
.dsize
- offset
- 1);
728 if (DEBUGLEVEL
>= 10) {
729 print_share_mode_table(data
);
735 static int share_mode_lock_destructor(struct share_mode_lock
*lck
)
740 if (!lck
->modified
) {
744 data
= unparse_share_modes(lck
);
746 if (data
.dptr
== NULL
) {
748 /* There has been an entry before, delete it */
750 status
= lck
->record
->delete_rec(lck
->record
);
751 if (!NT_STATUS_IS_OK(status
)) {
752 DEBUG(0, ("delete_rec returned %s\n",
754 smb_panic("could not delete share entry");
760 status
= lck
->record
->store(lck
->record
, data
, TDB_REPLACE
);
761 if (!NT_STATUS_IS_OK(status
)) {
762 DEBUG(0, ("store returned %s\n", nt_errstr(status
)));
763 smb_panic("could not store share mode entry");
771 static bool fill_share_mode_lock(struct share_mode_lock
*lck
,
773 const char *servicepath
,
775 TDB_DATA share_mode_data
,
776 const struct timespec
*old_write_time
)
778 /* Ensure we set every field here as the destructor must be
779 valid even if parse_share_modes fails. */
781 lck
->servicepath
= NULL
;
782 lck
->filename
= NULL
;
784 lck
->num_share_modes
= 0;
785 lck
->share_modes
= NULL
;
786 lck
->delete_token
= NULL
;
787 lck
->delete_on_close
= False
;
788 ZERO_STRUCT(lck
->old_write_time
);
789 ZERO_STRUCT(lck
->changed_write_time
);
791 lck
->modified
= False
;
793 lck
->fresh
= (share_mode_data
.dptr
== NULL
);
796 if (fname
== NULL
|| servicepath
== NULL
797 || old_write_time
== NULL
) {
800 lck
->filename
= talloc_strdup(lck
, fname
);
801 lck
->servicepath
= talloc_strdup(lck
, servicepath
);
802 if (lck
->filename
== NULL
|| lck
->servicepath
== NULL
) {
803 DEBUG(0, ("talloc failed\n"));
806 lck
->old_write_time
= *old_write_time
;
808 if (!parse_share_modes(share_mode_data
, lck
)) {
809 DEBUG(0, ("Could not parse share modes\n"));
817 struct share_mode_lock
*get_share_mode_lock(TALLOC_CTX
*mem_ctx
,
818 const struct file_id id
,
819 const char *servicepath
,
821 const struct timespec
*old_write_time
)
823 struct share_mode_lock
*lck
;
825 TDB_DATA key
= locking_key(&id
, &tmp
);
827 if (!(lck
= TALLOC_P(mem_ctx
, struct share_mode_lock
))) {
828 DEBUG(0, ("talloc failed\n"));
832 if (!(lck
->record
= lock_db
->fetch_locked(lock_db
, lck
, key
))) {
833 DEBUG(3, ("Could not lock share entry\n"));
838 if (!fill_share_mode_lock(lck
, id
, servicepath
, fname
,
839 lck
->record
->value
, old_write_time
)) {
840 DEBUG(3, ("fill_share_mode_lock failed\n"));
845 talloc_set_destructor(lck
, share_mode_lock_destructor
);
850 struct share_mode_lock
*fetch_share_mode_unlocked(TALLOC_CTX
*mem_ctx
,
851 const struct file_id id
,
852 const char *servicepath
,
855 struct share_mode_lock
*lck
;
857 TDB_DATA key
= locking_key(&id
, &tmp
);
860 if (!(lck
= TALLOC_P(mem_ctx
, struct share_mode_lock
))) {
861 DEBUG(0, ("talloc failed\n"));
865 if (lock_db
->fetch(lock_db
, lck
, key
, &data
) == -1) {
866 DEBUG(3, ("Could not fetch share entry\n"));
871 if (!fill_share_mode_lock(lck
, id
, servicepath
, fname
, data
, NULL
)) {
872 DEBUG(3, ("fill_share_mode_lock failed\n"));
880 /*******************************************************************
881 Sets the service name and filename for rename.
882 At this point we emit "file renamed" messages to all
883 process id's that have this file open.
884 Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
885 ********************************************************************/
887 bool rename_share_filename(struct messaging_context
*msg_ctx
,
888 struct share_mode_lock
*lck
,
889 const char *servicepath
,
898 DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
899 servicepath
, newname
));
902 * rename_internal_fsp() and rename_internals() add './' to
903 * head of newname if newname does not contain a '/'.
905 while (newname
[0] && newname
[1] && newname
[0] == '.' && newname
[1] == '/') {
909 lck
->servicepath
= talloc_strdup(lck
, servicepath
);
910 lck
->filename
= talloc_strdup(lck
, newname
);
911 if (lck
->filename
== NULL
|| lck
->servicepath
== NULL
) {
912 DEBUG(0, ("rename_share_filename: talloc failed\n"));
915 lck
->modified
= True
;
917 sp_len
= strlen(lck
->servicepath
);
918 fn_len
= strlen(lck
->filename
);
920 msg_len
= MSG_FILE_RENAMED_MIN_SIZE
+ sp_len
+ 1 + fn_len
+ 1;
922 /* Set up the name changed message. */
923 frm
= TALLOC_ARRAY(lck
, char, msg_len
);
928 push_file_id_16(frm
, &lck
->id
);
930 DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len
));
932 safe_strcpy(&frm
[16], lck
->servicepath
, sp_len
);
933 safe_strcpy(&frm
[16 + sp_len
+ 1], lck
->filename
, fn_len
);
935 /* Send the messages. */
936 for (i
=0; i
<lck
->num_share_modes
; i
++) {
937 struct share_mode_entry
*se
= &lck
->share_modes
[i
];
938 if (!is_valid_share_mode_entry(se
)) {
941 /* But not to ourselves... */
942 if (procid_is_me(&se
->pid
)) {
946 DEBUG(10,("rename_share_filename: sending rename message to pid %s "
947 "file_id %s sharepath %s newname %s\n",
948 procid_str_static(&se
->pid
),
949 file_id_string_tos(&lck
->id
),
950 lck
->servicepath
, lck
->filename
));
952 messaging_send_buf(msg_ctx
, se
->pid
, MSG_SMB_FILE_RENAME
,
953 (uint8
*)frm
, msg_len
);
959 void get_file_infos(struct file_id id
,
960 bool *delete_on_close
,
961 struct timespec
*write_time
)
963 struct share_mode_lock
*lck
;
965 if (delete_on_close
) {
966 *delete_on_close
= false;
970 ZERO_STRUCTP(write_time
);
973 if (!(lck
= fetch_share_mode_unlocked(talloc_tos(), id
, NULL
, NULL
))) {
977 if (delete_on_close
) {
978 *delete_on_close
= lck
->delete_on_close
;
984 wt
= lck
->changed_write_time
;
985 if (null_timespec(wt
)) {
986 wt
= lck
->old_write_time
;
995 bool is_valid_share_mode_entry(const struct share_mode_entry
*e
)
999 if (e
->op_type
== UNUSED_SHARE_MODE_ENTRY
) {
1000 /* cope with dead entries from the process not
1001 existing. These should not be considered valid,
1002 otherwise we end up doing zero timeout sharing
1007 num_props
+= ((e
->op_type
== NO_OPLOCK
) ? 1 : 0);
1008 num_props
+= (EXCLUSIVE_OPLOCK_TYPE(e
->op_type
) ? 1 : 0);
1009 num_props
+= (LEVEL_II_OPLOCK_TYPE(e
->op_type
) ? 1 : 0);
1011 SMB_ASSERT(num_props
<= 1);
1012 return (num_props
!= 0);
1015 bool is_deferred_open_entry(const struct share_mode_entry
*e
)
1017 return (e
->op_type
== DEFERRED_OPEN_ENTRY
);
1020 bool is_unused_share_mode_entry(const struct share_mode_entry
*e
)
1022 return (e
->op_type
== UNUSED_SHARE_MODE_ENTRY
);
1025 /*******************************************************************
1026 Fill a share mode entry.
1027 ********************************************************************/
1029 static void fill_share_mode_entry(struct share_mode_entry
*e
,
1031 uid_t uid
, uint16 mid
, uint16 op_type
)
1034 e
->pid
= procid_self();
1035 e
->share_access
= fsp
->share_access
;
1036 e
->private_options
= fsp
->fh
->private_options
;
1037 e
->access_mask
= fsp
->access_mask
;
1039 e
->op_type
= op_type
;
1040 e
->time
.tv_sec
= fsp
->open_time
.tv_sec
;
1041 e
->time
.tv_usec
= fsp
->open_time
.tv_usec
;
1042 e
->id
= fsp
->file_id
;
1043 e
->share_file_id
= fsp
->fh
->gen_id
;
1044 e
->uid
= (uint32
)uid
;
1045 e
->flags
= fsp
->posix_open
? SHARE_MODE_FLAG_POSIX_OPEN
: 0;
1048 static void fill_deferred_open_entry(struct share_mode_entry
*e
,
1049 const struct timeval request_time
,
1050 struct file_id id
, uint16 mid
)
1053 e
->pid
= procid_self();
1055 e
->op_type
= DEFERRED_OPEN_ENTRY
;
1056 e
->time
.tv_sec
= request_time
.tv_sec
;
1057 e
->time
.tv_usec
= request_time
.tv_usec
;
1059 e
->uid
= (uint32
)-1;
1063 static void add_share_mode_entry(struct share_mode_lock
*lck
,
1064 const struct share_mode_entry
*entry
)
1068 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1069 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
1070 if (is_unused_share_mode_entry(e
)) {
1076 if (i
== lck
->num_share_modes
) {
1077 /* No unused entry found */
1078 ADD_TO_ARRAY(lck
, struct share_mode_entry
, *entry
,
1079 &lck
->share_modes
, &lck
->num_share_modes
);
1081 lck
->modified
= True
;
1084 void set_share_mode(struct share_mode_lock
*lck
, files_struct
*fsp
,
1085 uid_t uid
, uint16 mid
, uint16 op_type
)
1087 struct share_mode_entry entry
;
1088 fill_share_mode_entry(&entry
, fsp
, uid
, mid
, op_type
);
1089 add_share_mode_entry(lck
, &entry
);
1092 void add_deferred_open(struct share_mode_lock
*lck
, uint16 mid
,
1093 struct timeval request_time
,
1096 struct share_mode_entry entry
;
1097 fill_deferred_open_entry(&entry
, request_time
, id
, mid
);
1098 add_share_mode_entry(lck
, &entry
);
1101 /*******************************************************************
1102 Check if two share mode entries are identical, ignoring oplock
1103 and mid info and desired_access. (Removed paranoia test - it's
1104 not automatically a logic error if they are identical. JRA.)
1105 ********************************************************************/
1107 static bool share_modes_identical(struct share_mode_entry
*e1
,
1108 struct share_mode_entry
*e2
)
1110 /* We used to check for e1->share_access == e2->share_access here
1111 as well as the other fields but 2 different DOS or FCB opens
1112 sharing the same share mode entry may validly differ in
1113 fsp->share_access field. */
1115 return (procid_equal(&e1
->pid
, &e2
->pid
) &&
1116 file_id_equal(&e1
->id
, &e2
->id
) &&
1117 e1
->share_file_id
== e2
->share_file_id
);
1120 static bool deferred_open_identical(struct share_mode_entry
*e1
,
1121 struct share_mode_entry
*e2
)
1123 return (procid_equal(&e1
->pid
, &e2
->pid
) &&
1124 (e1
->op_mid
== e2
->op_mid
) &&
1125 file_id_equal(&e1
->id
, &e2
->id
));
1128 static struct share_mode_entry
*find_share_mode_entry(struct share_mode_lock
*lck
,
1129 struct share_mode_entry
*entry
)
1133 for (i
=0; i
<lck
->num_share_modes
; i
++) {
1134 struct share_mode_entry
*e
= &lck
->share_modes
[i
];
1135 if (is_valid_share_mode_entry(entry
) &&
1136 is_valid_share_mode_entry(e
) &&
1137 share_modes_identical(e
, entry
)) {
1140 if (is_deferred_open_entry(entry
) &&
1141 is_deferred_open_entry(e
) &&
1142 deferred_open_identical(e
, entry
)) {
1149 /*******************************************************************
1150 Del the share mode of a file for this process. Return the number of
1152 ********************************************************************/
1154 bool del_share_mode(struct share_mode_lock
*lck
, files_struct
*fsp
)
1156 struct share_mode_entry entry
, *e
;
1158 /* Don't care about the pid owner being correct here - just a search. */
1159 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1161 e
= find_share_mode_entry(lck
, &entry
);
1166 e
->op_type
= UNUSED_SHARE_MODE_ENTRY
;
1167 lck
->modified
= True
;
1171 void del_deferred_open_entry(struct share_mode_lock
*lck
, uint16 mid
)
1173 struct share_mode_entry entry
, *e
;
1175 fill_deferred_open_entry(&entry
, timeval_zero(),
1178 e
= find_share_mode_entry(lck
, &entry
);
1183 e
->op_type
= UNUSED_SHARE_MODE_ENTRY
;
1184 lck
->modified
= True
;
1187 /*******************************************************************
1188 Remove an oplock mid and mode entry from a share mode.
1189 ********************************************************************/
1191 bool remove_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
1193 struct share_mode_entry entry
, *e
;
1195 /* Don't care about the pid owner being correct here - just a search. */
1196 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1198 e
= find_share_mode_entry(lck
, &entry
);
1204 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1206 * Going from exclusive or batch,
1207 * we always go through FAKE_LEVEL_II
1210 e
->op_type
= FAKE_LEVEL_II_OPLOCK
;
1212 e
->op_type
= NO_OPLOCK
;
1214 lck
->modified
= True
;
1218 /*******************************************************************
1219 Downgrade a oplock type from exclusive to level II.
1220 ********************************************************************/
1222 bool downgrade_share_oplock(struct share_mode_lock
*lck
, files_struct
*fsp
)
1224 struct share_mode_entry entry
, *e
;
1226 /* Don't care about the pid owner being correct here - just a search. */
1227 fill_share_mode_entry(&entry
, fsp
, (uid_t
)-1, 0, NO_OPLOCK
);
1229 e
= find_share_mode_entry(lck
, &entry
);
1234 e
->op_type
= LEVEL_II_OPLOCK
;
1235 lck
->modified
= True
;
1239 /****************************************************************************
1240 Deal with the internal needs of setting the delete on close flag. Note that
1241 as the tdb locking is recursive, it is safe to call this from within
1242 open_file_ntcreate. JRA.
1243 ****************************************************************************/
1245 NTSTATUS
can_set_delete_on_close(files_struct
*fsp
, bool delete_on_close
,
1248 if (!delete_on_close
) {
1249 return NT_STATUS_OK
;
1253 * Only allow delete on close for writable files.
1256 if ((dosmode
& aRONLY
) &&
1257 !lp_delete_readonly(SNUM(fsp
->conn
))) {
1258 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
1259 "flag set but file attribute is readonly.\n",
1261 return NT_STATUS_CANNOT_DELETE
;
1265 * Only allow delete on close for writable shares.
1268 if (!CAN_WRITE(fsp
->conn
)) {
1269 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1270 "close flag set but write access denied on share.\n",
1272 return NT_STATUS_ACCESS_DENIED
;
1276 * Only allow delete on close for files/directories opened with delete
1280 if (!(fsp
->access_mask
& DELETE_ACCESS
)) {
1281 DEBUG(10,("can_set_delete_on_close: file %s delete on "
1282 "close flag set but delete access denied.\n",
1284 return NT_STATUS_ACCESS_DENIED
;
1287 /* Don't allow delete on close for non-empty directories. */
1288 if (fsp
->is_directory
) {
1289 return can_delete_directory(fsp
->conn
, fsp
->fsp_name
);
1292 return NT_STATUS_OK
;
1295 /*************************************************************************
1296 Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
1297 (Should this be in locking.c.... ?).
1298 *************************************************************************/
1300 static UNIX_USER_TOKEN
*copy_unix_token(TALLOC_CTX
*ctx
, UNIX_USER_TOKEN
*tok
)
1302 UNIX_USER_TOKEN
*cpy
;
1308 cpy
= TALLOC_P(ctx
, UNIX_USER_TOKEN
);
1313 cpy
->uid
= tok
->uid
;
1314 cpy
->gid
= tok
->gid
;
1315 cpy
->ngroups
= tok
->ngroups
;
1317 /* Make this a talloc child of cpy. */
1318 cpy
->groups
= TALLOC_ARRAY(cpy
, gid_t
, tok
->ngroups
);
1322 memcpy(cpy
->groups
, tok
->groups
, tok
->ngroups
* sizeof(gid_t
));
1327 /****************************************************************************
1328 Replace the delete on close token.
1329 ****************************************************************************/
1331 void set_delete_on_close_token(struct share_mode_lock
*lck
, UNIX_USER_TOKEN
*tok
)
1333 TALLOC_FREE(lck
->delete_token
); /* Also deletes groups... */
1335 /* Copy the new token (can be NULL). */
1336 lck
->delete_token
= copy_unix_token(lck
, tok
);
1337 lck
->modified
= True
;
1340 /****************************************************************************
1341 Sets the delete on close flag over all share modes on this file.
1342 Modify the share mode entry for all files open
1343 on this device and inode to tell other smbds we have
1344 changed the delete on close flag. This will be noticed
1345 in the close code, the last closer will delete the file
1347 This makes a copy of any UNIX_USER_TOKEN into the
1348 lck entry. This function is used when the lock is already granted.
1349 ****************************************************************************/
1351 void set_delete_on_close_lck(struct share_mode_lock
*lck
, bool delete_on_close
, UNIX_USER_TOKEN
*tok
)
1353 if (lck
->delete_on_close
!= delete_on_close
) {
1354 set_delete_on_close_token(lck
, tok
);
1355 lck
->delete_on_close
= delete_on_close
;
1356 if (delete_on_close
) {
1357 SMB_ASSERT(lck
->delete_token
!= NULL
);
1359 lck
->modified
= True
;
1363 bool set_delete_on_close(files_struct
*fsp
, bool delete_on_close
, UNIX_USER_TOKEN
*tok
)
1365 struct share_mode_lock
*lck
;
1367 DEBUG(10,("set_delete_on_close: %s delete on close flag for "
1368 "fnum = %d, file %s\n",
1369 delete_on_close
? "Adding" : "Removing", fsp
->fnum
,
1372 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
,
1378 set_delete_on_close_lck(lck
, delete_on_close
, tok
);
1380 if (fsp
->is_directory
) {
1381 send_stat_cache_delete_message(fsp
->fsp_name
);
1388 bool set_sticky_write_time(struct file_id fileid
, struct timespec write_time
)
1390 struct share_mode_lock
*lck
;
1392 DEBUG(5,("set_sticky_write_time: %s id=%s\n",
1393 timestring(debug_ctx(),
1394 convert_timespec_to_time_t(write_time
)),
1395 file_id_string_tos(&fileid
)));
1397 lck
= get_share_mode_lock(NULL
, fileid
, NULL
, NULL
, NULL
);
1402 if (timespec_compare(&lck
->changed_write_time
, &write_time
) != 0) {
1403 lck
->modified
= True
;
1404 lck
->changed_write_time
= write_time
;
1411 bool set_write_time(struct file_id fileid
, struct timespec write_time
)
1413 struct share_mode_lock
*lck
;
1415 DEBUG(5,("set_write_time: %s id=%s\n",
1416 timestring(debug_ctx(),
1417 convert_timespec_to_time_t(write_time
)),
1418 file_id_string_tos(&fileid
)));
1420 lck
= get_share_mode_lock(NULL
, fileid
, NULL
, NULL
, NULL
);
1425 if (timespec_compare(&lck
->old_write_time
, &write_time
) != 0) {
1426 lck
->modified
= True
;
1427 lck
->old_write_time
= write_time
;
1435 struct forall_state
{
1436 void (*fn
)(const struct share_mode_entry
*entry
,
1437 const char *sharepath
,
1439 void *private_data
);
1443 static int traverse_fn(struct db_record
*rec
, void *_state
)
1445 struct forall_state
*state
= (struct forall_state
*)_state
;
1446 struct locking_data
*data
;
1447 struct share_mode_entry
*shares
;
1448 const char *sharepath
;
1452 /* Ensure this is a locking_key record. */
1453 if (rec
->key
.dsize
!= sizeof(struct file_id
))
1456 data
= (struct locking_data
*)rec
->value
.dptr
;
1457 shares
= (struct share_mode_entry
*)(rec
->value
.dptr
+ sizeof(*data
));
1458 sharepath
= (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 fname
= (const char *)rec
->value
.dptr
+ sizeof(*data
) +
1462 data
->u
.s
.num_share_mode_entries
*sizeof(*shares
) +
1463 data
->u
.s
.delete_token_size
+
1464 strlen(sharepath
) + 1;
1466 for (i
=0;i
<data
->u
.s
.num_share_mode_entries
;i
++) {
1467 state
->fn(&shares
[i
], sharepath
, fname
,
1468 state
->private_data
);
1473 /*******************************************************************
1474 Call the specified function on each entry under management by the
1476 ********************************************************************/
1478 int share_mode_forall(void (*fn
)(const struct share_mode_entry
*, const char *,
1479 const char *, void *),
1482 struct forall_state state
;
1484 if (lock_db
== NULL
)
1488 state
.private_data
= private_data
;
1490 return lock_db
->traverse_read(lock_db
, traverse_fn
, (void *)&state
);