2
Unix SMB/CIFS implementation.
3
Blocking Locking functions
4
Copyright (C) Jeremy Allison 1998-2003
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 3 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program. If not, see <http://www.gnu.org/licenses/>.
21
#include "smbd/globals.h"
24
#define DBGC_CLASS DBGC_LOCKING
26
/****************************************************************************
27
Determine if this is a secondary element of a chained SMB.
28
**************************************************************************/
30
static void received_unlock_msg(struct messaging_context *msg,
33
struct server_id server_id,
36
static void brl_timeout_fn(struct event_context *event_ctx,
37
struct timed_event *te,
41
SMB_ASSERT(brl_timeout == te);
42
TALLOC_FREE(brl_timeout);
44
change_to_root_user(); /* TODO: Possibly run all timed events as
47
process_blocking_lock_queue();
50
/****************************************************************************
51
We need a version of timeval_min that treats zero timval as infinite.
52
****************************************************************************/
54
static struct timeval timeval_brl_min(const struct timeval *tv1,
55
const struct timeval *tv2)
57
if (timeval_is_zero(tv1)) {
60
if (timeval_is_zero(tv2)) {
63
return timeval_min(tv1, tv2);
66
/****************************************************************************
67
After a change to blocking_lock_queue, recalculate the timed_event for the
69
****************************************************************************/
71
static bool recalc_brl_timeout(void)
73
struct blocking_lock_record *blr;
74
struct timeval next_timeout;
76
TALLOC_FREE(brl_timeout);
78
next_timeout = timeval_zero();
80
for (blr = blocking_lock_queue; blr; blr = blr->next) {
81
if (timeval_is_zero(&blr->expire_time)) {
83
* If we're blocked on pid 0xFFFFFFFF this is
84
* a POSIX lock, so calculate a timeout of
85
* 10 seconds into the future.
87
if (blr->blocking_pid == 0xFFFFFFFF) {
88
struct timeval psx_to = timeval_current_ofs(10, 0);
89
next_timeout = timeval_brl_min(&next_timeout, &psx_to);
95
next_timeout = timeval_brl_min(&next_timeout, &blr->expire_time);
98
if (timeval_is_zero(&next_timeout)) {
99
DEBUG(10, ("Next timeout = Infinite.\n"));
104
struct timeval cur, from_now;
106
cur = timeval_current();
107
from_now = timeval_until(&cur, &next_timeout);
108
DEBUG(10, ("Next timeout = %d.%d seconds from now.\n",
109
(int)from_now.tv_sec, (int)from_now.tv_usec));
112
if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
114
brl_timeout_fn, NULL))) {
122
/****************************************************************************
123
Function to push a blocking lock request onto the lock queue.
124
****************************************************************************/
126
bool push_blocking_lock_request( struct byte_range_lock *br_lck,
127
struct smb_request *req,
132
enum brl_type lock_type,
133
enum brl_flavour lock_flav,
136
uint32_t blocking_pid)
138
struct blocking_lock_record *blr;
141
if(req_is_in_chain(req)) {
142
DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
147
* Now queue an entry on the blocking lock queue. We setup
148
* the expiration time here.
151
blr = talloc(NULL, struct blocking_lock_record);
153
DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
161
if (lock_timeout == -1) {
162
blr->expire_time.tv_sec = 0;
163
blr->expire_time.tv_usec = 0; /* Never expire. */
165
blr->expire_time = timeval_current_ofs(lock_timeout/1000,
166
(lock_timeout % 1000) * 1000);
168
blr->lock_num = lock_num;
169
blr->lock_pid = lock_pid;
170
blr->blocking_pid = blocking_pid;
171
blr->lock_flav = lock_flav;
172
blr->lock_type = lock_type;
173
blr->offset = offset;
176
/* Specific brl_lock() implementations can fill this in. */
177
blr->blr_private = NULL;
179
/* Add a pending lock record for this. */
180
status = brl_lock(smbd_messaging_context(),
186
lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
188
lock_timeout ? True : False, /* blocking_lock. */
192
if (!NT_STATUS_IS_OK(status)) {
193
DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
198
SMB_PERFCOUNT_DEFER_OP(&req->pcd, &req->pcd);
199
blr->req = talloc_move(blr, &req);
201
DLIST_ADD_END(blocking_lock_queue, blr, struct blocking_lock_record *);
202
recalc_brl_timeout();
204
/* Ensure we'll receive messages when this is unlocked. */
205
if (!blocking_lock_unlock_state) {
206
messaging_register(smbd_messaging_context(), NULL,
207
MSG_SMB_UNLOCK, received_unlock_msg);
208
blocking_lock_unlock_state = true;
211
DEBUG(3,("push_blocking_lock_request: lock request blocked with "
212
"expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
213
(unsigned int)blr->expire_time.tv_sec,
214
(unsigned int)blr->expire_time.tv_usec, lock_timeout,
215
blr->fsp->fnum, blr->fsp->fsp_name ));
217
/* Push the MID of this packet on the signing queue. */
218
srv_defer_sign_response(blr->req->mid);
223
/****************************************************************************
224
Return a lockingX success SMB.
225
*****************************************************************************/
227
static void reply_lockingX_success(struct blocking_lock_record *blr)
229
reply_outbuf(blr->req, 2, 0);
232
* As this message is a lockingX call we must handle
233
* any following chained message correctly.
234
* This is normally handled in construct_reply(),
235
* but as that calls switch_message, we can't use
236
* that here and must set up the chain info manually.
239
chain_reply(blr->req);
240
TALLOC_FREE(blr->req->outbuf);
243
/****************************************************************************
244
Return a generic lock fail error blocking call.
245
*****************************************************************************/
247
static void generic_blocking_lock_error(struct blocking_lock_record *blr, NTSTATUS status)
249
/* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
250
FILE_LOCK_CONFLICT! (tridge) */
251
if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
252
status = NT_STATUS_FILE_LOCK_CONFLICT;
255
if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
256
/* Store the last lock error. */
257
files_struct *fsp = blr->fsp;
260
fsp->last_lock_failure.context.smbpid = blr->lock_pid;
261
fsp->last_lock_failure.context.tid = fsp->conn->cnum;
262
fsp->last_lock_failure.context.pid = procid_self();
263
fsp->last_lock_failure.start = blr->offset;
264
fsp->last_lock_failure.size = blr->count;
265
fsp->last_lock_failure.fnum = fsp->fnum;
266
fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
267
fsp->last_lock_failure.lock_flav = blr->lock_flav;
271
reply_nterror(blr->req, status);
272
if (!srv_send_smb(smbd_server_fd(), (char *)blr->req->outbuf,
273
blr->req->encrypted, NULL)) {
274
exit_server_cleanly("generic_blocking_lock_error: srv_send_smb failed.");
276
TALLOC_FREE(blr->req->outbuf);
279
/****************************************************************************
280
Return a lock fail error for a lockingX call. Undo all the locks we have
282
*****************************************************************************/
284
static void reply_lockingX_error(struct blocking_lock_record *blr, NTSTATUS status)
286
files_struct *fsp = blr->fsp;
287
uint16 num_ulocks = SVAL(blr->req->vwv+6, 0);
288
uint64_t count = (uint64_t)0, offset = (uint64_t) 0;
290
unsigned char locktype = CVAL(blr->req->vwv+3, 0);
291
bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
295
data = (uint8_t *)blr->req->buf
296
+ ((large_file_format ? 20 : 10)*num_ulocks);
299
* Data now points at the beginning of the list
300
* of smb_lkrng structs.
304
* Ensure we don't do a remove on the lock that just failed,
305
* as under POSIX rules, if we have a lock already there, we
306
* will delete it (and we shouldn't) .....
309
for(i = blr->lock_num - 1; i >= 0; i--) {
312
lock_pid = get_lock_pid( data, i, large_file_format);
313
count = get_lock_count( data, i, large_file_format);
314
offset = get_lock_offset( data, i, large_file_format, &err);
317
* We know err cannot be set as if it was the lock
318
* request would never have been queued. JRA.
321
do_unlock(smbd_messaging_context(),
329
generic_blocking_lock_error(blr, status);
332
/****************************************************************************
333
Return a lock fail error.
334
*****************************************************************************/
336
static void blocking_lock_reply_error(struct blocking_lock_record *blr, NTSTATUS status)
338
DEBUG(10, ("Replying with error=%s. BLR = %p\n", nt_errstr(status), blr));
340
switch(blr->req->cmd) {
342
reply_lockingX_error(blr, status);
346
reply_nterror(blr->req, status);
349
* construct_reply_common has done us the favor to pre-fill
350
* the command field with SMBtranss2 which is wrong :-)
352
SCVAL(blr->req->outbuf,smb_com,SMBtrans2);
354
if (!srv_send_smb(smbd_server_fd(),
355
(char *)blr->req->outbuf,
356
IS_CONN_ENCRYPTED(blr->fsp->conn),
358
exit_server_cleanly("blocking_lock_reply_error: "
359
"srv_send_smb failed.");
361
TALLOC_FREE(blr->req->outbuf);
364
DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
365
exit_server("PANIC - unknown type on blocking lock queue");
369
/****************************************************************************
370
Attempt to finish off getting all pending blocking locks for a lockingX call.
371
Returns True if we want to be removed from the list.
372
*****************************************************************************/
374
static bool process_lockingX(struct blocking_lock_record *blr)
376
unsigned char locktype = CVAL(blr->req->vwv+3, 0);
377
files_struct *fsp = blr->fsp;
378
uint16 num_ulocks = SVAL(blr->req->vwv+6, 0);
379
uint16 num_locks = SVAL(blr->req->vwv+7, 0);
380
uint64_t count = (uint64_t)0, offset = (uint64_t)0;
382
bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
384
NTSTATUS status = NT_STATUS_OK;
386
data = (uint8_t *)blr->req->buf
387
+ ((large_file_format ? 20 : 10)*num_ulocks);
390
* Data now points at the beginning of the list
391
* of smb_lkrng structs.
394
for(; blr->lock_num < num_locks; blr->lock_num++) {
395
struct byte_range_lock *br_lck = NULL;
398
lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
399
count = get_lock_count( data, blr->lock_num, large_file_format);
400
offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
403
* We know err cannot be set as if it was the lock
404
* request would never have been queued. JRA.
407
br_lck = do_lock(smbd_messaging_context(),
412
((locktype & LOCKING_ANDX_SHARED_LOCK) ?
413
READ_LOCK : WRITE_LOCK),
422
if (NT_STATUS_IS_ERR(status)) {
427
if(blr->lock_num == num_locks) {
429
* Success - we got all the locks.
432
DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
433
fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
435
reply_lockingX_success(blr);
439
if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
440
!NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
442
* We have other than a "can't get lock"
443
* error. Free any locks we had and return an error.
444
* Return True so we get dequeued.
446
blocking_lock_reply_error(blr, status);
451
* Still can't get all the locks - keep waiting.
454
DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
456
blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
461
/****************************************************************************
462
Attempt to get the posix lock request from a SMBtrans2 call.
463
Returns True if we want to be removed from the list.
464
*****************************************************************************/
466
static bool process_trans2(struct blocking_lock_record *blr)
470
struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
483
if (!NT_STATUS_IS_OK(status)) {
484
if (ERROR_WAS_LOCK_DENIED(status)) {
485
/* Still can't get the lock, just keep waiting. */
489
* We have other than a "can't get lock"
490
* error. Send an error and return True so we get dequeued.
492
blocking_lock_reply_error(blr, status);
496
/* We finally got the lock, return success. */
499
/* Fake up max_data_bytes here - we know it fits. */
500
send_trans2_replies(blr->fsp->conn, blr->req, params, 2, NULL, 0, 0xffff);
505
/****************************************************************************
506
Process a blocking lock SMB.
507
Returns True if we want to be removed from the list.
508
*****************************************************************************/
510
static bool blocking_lock_record_process(struct blocking_lock_record *blr)
512
switch(blr->req->cmd) {
514
return process_lockingX(blr);
517
return process_trans2(blr);
519
DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
520
exit_server("PANIC - unknown type on blocking lock queue");
522
return False; /* Keep compiler happy. */
525
/****************************************************************************
526
Cancel entries by fnum from the blocking lock pending queue.
527
*****************************************************************************/
529
void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
531
struct blocking_lock_record *blr, *blr_cancelled, *next = NULL;
533
for(blr = blocking_lock_queue; blr; blr = next) {
534
unsigned char locktype = 0;
537
if (blr->fsp->fnum != fsp->fnum) {
541
if (blr->req->cmd == SMBlockingX) {
542
locktype = CVAL(blr->req->vwv+3, 0);
545
DEBUG(10, ("remove_pending_lock_requests_by_fid - removing "
546
"request type %d for file %s fnum = %d\n",
547
blr->req->cmd, fsp->fsp_name, fsp->fnum));
549
blr_cancelled = blocking_lock_cancel(fsp,
555
NT_STATUS_RANGE_NOT_LOCKED);
557
SMB_ASSERT(blr_cancelled == blr);
559
brl_lock_cancel(br_lck,
567
/* We're closing the file fsp here, so ensure
568
* we don't have a dangling pointer. */
573
/****************************************************************************
574
Delete entries by mid from the blocking lock pending queue. Always send reply.
575
*****************************************************************************/
577
void remove_pending_lock_requests_by_mid(int mid)
579
struct blocking_lock_record *blr, *next = NULL;
581
for(blr = blocking_lock_queue; blr; blr = next) {
583
struct byte_range_lock *br_lck;
587
if (blr->req->mid != mid) {
592
br_lck = brl_get_locks(talloc_tos(), fsp);
595
DEBUG(10, ("remove_pending_lock_requests_by_mid - "
596
"removing request type %d for file %s fnum "
597
"= %d\n", blr->req->cmd, fsp->fsp_name,
600
brl_lock_cancel(br_lck,
610
blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
611
DLIST_REMOVE(blocking_lock_queue, blr);
616
/****************************************************************************
617
Is this mid a blocking lock request on the queue ?
618
*****************************************************************************/
620
bool blocking_lock_was_deferred(int mid)
622
struct blocking_lock_record *blr, *next = NULL;
624
for(blr = blocking_lock_queue; blr; blr = next) {
626
if(blr->req->mid == mid) {
633
/****************************************************************************
634
Set a flag as an unlock request affects one of our pending locks.
635
*****************************************************************************/
637
static void received_unlock_msg(struct messaging_context *msg,
640
struct server_id server_id,
643
DEBUG(10,("received_unlock_msg\n"));
644
process_blocking_lock_queue();
647
/****************************************************************************
648
Process the blocking lock queue. Note that this is only called as root.
649
*****************************************************************************/
651
void process_blocking_lock_queue(void)
653
struct timeval tv_curr = timeval_current();
654
struct blocking_lock_record *blr, *next = NULL;
657
* Go through the queue and see if we can get any of the locks.
660
for (blr = blocking_lock_queue; blr; blr = next) {
665
* Go through the remaining locks and try and obtain them.
666
* The call returns True if all locks were obtained successfully
667
* and False if we still need to wait.
670
DEBUG(10, ("Processing BLR = %p\n", blr));
672
/* We use set_current_service so connections with
673
* pending locks are not marked as idle.
676
set_current_service(blr->fsp->conn,
677
SVAL(blr->req->inbuf,smb_flg),
680
if(blocking_lock_record_process(blr)) {
681
struct byte_range_lock *br_lck = brl_get_locks(
682
talloc_tos(), blr->fsp);
684
DEBUG(10, ("BLR_process returned true: cancelling and "
685
"removing lock. BLR = %p\n", blr));
688
brl_lock_cancel(br_lck,
698
DLIST_REMOVE(blocking_lock_queue, blr);
704
* We couldn't get the locks for this record on the list.
705
* If the time has expired, return a lock error.
708
if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
709
struct byte_range_lock *br_lck = brl_get_locks(
710
talloc_tos(), blr->fsp);
712
DEBUG(10, ("Lock timed out! BLR = %p\n", blr));
715
* Lock expired - throw away all previously
716
* obtained locks and return lock error.
720
DEBUG(5,("process_blocking_lock_queue: "
721
"pending lock fnum = %d for file %s "
722
"timed out.\n", blr->fsp->fnum,
723
blr->fsp->fsp_name ));
725
brl_lock_cancel(br_lck,
735
blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
736
DLIST_REMOVE(blocking_lock_queue, blr);
741
recalc_brl_timeout();
744
/****************************************************************************
745
Handle a cancel message. Lock already moved onto the cancel queue.
746
*****************************************************************************/
748
#define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(struct blocking_lock_record *) + sizeof(NTSTATUS))
750
static void process_blocking_lock_cancel_message(struct messaging_context *ctx,
753
struct server_id server_id,
757
const char *msg = (const char *)data->data;
758
struct blocking_lock_record *blr;
760
if (data->data == NULL) {
761
smb_panic("process_blocking_lock_cancel_message: null msg");
764
if (data->length != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
765
DEBUG(0, ("process_blocking_lock_cancel_message: "
766
"Got invalid msg len %d\n", (int)data->length));
767
smb_panic("process_blocking_lock_cancel_message: bad msg");
770
memcpy(&blr, msg, sizeof(blr));
771
memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
773
DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
776
blocking_lock_reply_error(blr, err);
777
DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
781
/****************************************************************************
782
Send ourselves a blocking lock cancelled message. Handled asynchronously above.
783
Returns the blocking_lock_record that is being cancelled.
784
*****************************************************************************/
786
struct blocking_lock_record *blocking_lock_cancel(files_struct *fsp,
790
enum brl_flavour lock_flav,
791
unsigned char locktype,
794
char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
795
struct blocking_lock_record *blr;
797
if (!blocking_lock_cancel_state) {
798
/* Register our message. */
799
messaging_register(smbd_messaging_context(), NULL,
800
MSG_SMB_BLOCKING_LOCK_CANCEL,
801
process_blocking_lock_cancel_message);
803
blocking_lock_cancel_state = True;
806
for (blr = blocking_lock_queue; blr; blr = blr->next) {
807
if (fsp == blr->fsp &&
808
lock_pid == blr->lock_pid &&
809
offset == blr->offset &&
810
count == blr->count &&
811
lock_flav == blr->lock_flav) {
820
/* Check the flags are right. */
821
if (blr->req->cmd == SMBlockingX &&
822
(locktype & LOCKING_ANDX_LARGE_FILES) !=
823
(CVAL(blr->req->vwv+3, 0) & LOCKING_ANDX_LARGE_FILES)) {
827
/* Move to cancelled queue. */
828
DLIST_REMOVE(blocking_lock_queue, blr);
829
DLIST_ADD(blocking_lock_cancelled_queue, blr);
831
/* Create the message. */
832
memcpy(msg, &blr, sizeof(blr));
833
memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
835
messaging_send_buf(smbd_messaging_context(), procid_self(),
836
MSG_SMB_BLOCKING_LOCK_CANCEL,
837
(uint8 *)&msg, sizeof(msg));