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 2 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, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
/****************************************************************************
24
This is the structure to queue to implement blocking locks.
25
notify. It consists of the requesting SMB and the expiry time.
26
*****************************************************************************/
28
typedef struct _blocking_lock_record {
29
struct _blocking_lock_record *next;
30
struct _blocking_lock_record *prev;
38
enum brl_flavour lock_flav;
39
enum brl_type lock_type;
42
} blocking_lock_record;
44
static blocking_lock_record *blocking_lock_queue;
46
/****************************************************************************
47
Destructor for the above structure.
48
****************************************************************************/
50
static void free_blocking_lock_record(blocking_lock_record *blr)
52
DLIST_REMOVE(blocking_lock_queue, blr);
53
SAFE_FREE(blr->inbuf);
57
/****************************************************************************
58
Determine if this is a secondary element of a chained SMB.
59
**************************************************************************/
61
static BOOL in_chained_smb(void)
63
return (chain_size != 0);
66
static void received_unlock_msg(int msg_type, struct process_id src,
67
void *buf, size_t len);
69
/****************************************************************************
70
Function to push a blocking lock request onto the lock queue.
71
****************************************************************************/
73
BOOL push_blocking_lock_request( char *inbuf, int length,
78
enum brl_type lock_type,
79
enum brl_flavour lock_flav,
80
SMB_BIG_UINT offset, SMB_BIG_UINT count)
82
static BOOL set_lock_msg;
83
blocking_lock_record *blr, *tmp;
84
BOOL my_lock_ctx = False;
85
struct byte_range_lock *br_lck = NULL;
88
if(in_chained_smb() ) {
89
DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
94
* Now queue an entry on the blocking lock queue. We setup
95
* the expiration time here.
98
if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
99
DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
106
if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
107
DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
112
blr->com_type = CVAL(inbuf,smb_com);
114
blr->expire_time = (lock_timeout == -1) ? (time_t)-1 : time(NULL) + (time_t)lock_timeout;
115
blr->lock_num = lock_num;
116
blr->lock_pid = lock_pid;
117
blr->lock_flav = lock_flav;
118
blr->lock_type = lock_type;
119
blr->offset = offset;
121
memcpy(blr->inbuf, inbuf, length);
122
blr->length = length;
124
br_lck = brl_get_locks(NULL, blr->fsp);
126
free_blocking_lock_record(blr);
130
/* Add a pending lock record for this. */
131
status = brl_lock(br_lck,
141
if (!NT_STATUS_IS_OK(status)) {
142
DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
143
free_blocking_lock_record(blr);
147
DLIST_ADD_END(blocking_lock_queue, blr, tmp);
149
/* Ensure we'll receive messages when this is unlocked. */
151
message_register(MSG_SMB_UNLOCK, received_unlock_msg);
155
DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with expiry time %d (+%d) \
156
for fnum = %d, name = %s\n", length, (int)blr->expire_time, lock_timeout,
157
blr->fsp->fnum, blr->fsp->fsp_name ));
159
/* Push the MID of this packet on the signing queue. */
160
srv_defer_sign_response(SVAL(inbuf,smb_mid));
165
/****************************************************************************
166
Return a smd with a given size.
167
*****************************************************************************/
169
static void send_blocking_reply(char *outbuf, int outsize)
172
smb_setlen(outbuf,outsize - 4);
174
if (!send_smb(smbd_server_fd(),outbuf))
175
exit_server("send_blocking_reply: send_smb failed.");
178
/****************************************************************************
179
Return a lockingX success SMB.
180
*****************************************************************************/
182
static void reply_lockingX_success(blocking_lock_record *blr)
184
char *outbuf = get_OutBuffer();
185
int bufsize = BUFFER_SIZE;
186
char *inbuf = blr->inbuf;
189
construct_reply_common(inbuf, outbuf);
190
set_message(outbuf,2,0,True);
193
* As this message is a lockingX call we must handle
194
* any following chained message correctly.
195
* This is normally handled in construct_reply(),
196
* but as that calls switch_message, we can't use
197
* that here and must set up the chain info manually.
200
outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
202
outsize += chain_size;
204
send_blocking_reply(outbuf,outsize);
207
/****************************************************************************
208
Return a generic lock fail error blocking call.
209
*****************************************************************************/
211
static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
213
char *outbuf = get_OutBuffer();
214
char *inbuf = blr->inbuf;
215
construct_reply_common(inbuf, outbuf);
217
/* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
218
FILE_LOCK_CONFLICT! (tridge) */
219
if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
220
status = NT_STATUS_FILE_LOCK_CONFLICT;
224
if (!send_smb(smbd_server_fd(),outbuf))
225
exit_server("generic_blocking_lock_error: send_smb failed.");
228
/****************************************************************************
229
Return a lock fail error for a lockingX call. Undo all the locks we have
231
*****************************************************************************/
233
static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
235
char *inbuf = blr->inbuf;
236
files_struct *fsp = blr->fsp;
237
uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
238
SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
240
unsigned char locktype = CVAL(inbuf,smb_vwv3);
241
BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
245
data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
248
* Data now points at the beginning of the list
249
* of smb_lkrng structs.
253
* Ensure we don't do a remove on the lock that just failed,
254
* as under POSIX rules, if we have a lock already there, we
255
* will delete it (and we shouldn't) .....
258
for(i = blr->lock_num - 1; i >= 0; i--) {
261
lock_pid = get_lock_pid( data, i, large_file_format);
262
count = get_lock_count( data, i, large_file_format);
263
offset = get_lock_offset( data, i, large_file_format, &err);
266
* We know err cannot be set as if it was the lock
267
* request would never have been queued. JRA.
277
generic_blocking_lock_error(blr, status);
280
/****************************************************************************
281
Return a lock fail error.
282
*****************************************************************************/
284
static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
286
switch(blr->com_type) {
288
/* We no longer push blocking lock requests for anything but lockingX and trans2. */
291
generic_blocking_lock_error(blr, status);
295
reply_lockingX_error(blr, status);
300
char *outbuf = get_OutBuffer();
301
char *inbuf = blr->inbuf;
302
construct_reply_common(inbuf, outbuf);
303
/* construct_reply_common has done us the favor to pre-fill the
304
* command field with SMBtranss2 which is wrong :-)
306
SCVAL(outbuf,smb_com,SMBtrans2);
308
if (!send_smb(smbd_server_fd(),outbuf)) {
309
exit_server("blocking_lock_reply_error: send_smb failed.");
314
DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
315
exit_server("PANIC - unknown type on blocking lock queue");
320
/* We no longer push blocking lock requests for anything but lockingX and trans2. */
322
/****************************************************************************
323
Attempt to finish off getting all pending blocking locks for a lockread call.
324
Returns True if we want to be removed from the list.
325
*****************************************************************************/
327
static BOOL process_lockread(blocking_lock_record *blr)
329
char *outbuf = get_OutBuffer();
330
char *inbuf = blr->inbuf;
334
SMB_BIG_UINT startpos;
337
files_struct *fsp = blr->fsp;
338
BOOL my_lock_ctx = False;
340
numtoread = SVAL(inbuf,smb_vwv1);
341
startpos = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv2);
343
numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
344
data = smb_buf(outbuf) + 3;
346
status = do_lock_spin(fsp,
347
(uint32)SVAL(inbuf,smb_pid),
348
(SMB_BIG_UINT)numtoread,
354
if (NT_STATUS_V(status)) {
355
if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
356
!NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
358
* We have other than a "can't get lock"
359
* error. Send an error.
360
* Return True so we get dequeued.
362
generic_blocking_lock_error(blr, status);
367
* Still waiting for lock....
370
DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
375
nread = read_file(fsp,data,startpos,numtoread);
378
generic_blocking_lock_error(blr,NT_STATUS_ACCESS_DENIED);
382
construct_reply_common(inbuf, outbuf);
383
outsize = set_message(outbuf,5,0,True);
386
SSVAL(outbuf,smb_vwv0,nread);
387
SSVAL(outbuf,smb_vwv5,nread+3);
390
SSVAL(p,0,nread); p += 2;
391
set_message_end(outbuf, p+nread);
393
DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%lu nread=%ld\n",
394
fsp->fsp_name, fsp->fnum, (unsigned long)numtoread, (long)nread ) );
396
send_blocking_reply(outbuf,outsize);
400
/****************************************************************************
401
Attempt to finish off getting all pending blocking locks for a lock call.
402
Returns True if we want to be removed from the list.
403
*****************************************************************************/
405
static BOOL process_lock(blocking_lock_record *blr)
407
char *outbuf = get_OutBuffer();
408
char *inbuf = blr->inbuf;
410
SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
412
files_struct *fsp = blr->fsp;
413
BOOL my_lock_ctx = False;
415
count = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
416
offset = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
419
status = do_lock_spin(fsp,
420
(uint32)SVAL(inbuf,smb_pid),
427
if (NT_STATUS_IS_ERR(status)) {
428
if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
429
!NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
431
* We have other than a "can't get lock"
432
* error. Send an error.
433
* Return True so we get dequeued.
436
blocking_lock_reply_error(blr, status);
440
* Still can't get the lock - keep waiting.
442
DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
448
* Success - we got the lock.
451
DEBUG(3,("process_lock : file=%s fnum=%d offset=%.0f count=%.0f\n",
452
fsp->fsp_name, fsp->fnum, (double)offset, (double)count));
454
construct_reply_common(inbuf, outbuf);
455
outsize = set_message(outbuf,0,0,True);
456
send_blocking_reply(outbuf,outsize);
461
/****************************************************************************
462
Attempt to finish off getting all pending blocking locks for a lockingX call.
463
Returns True if we want to be removed from the list.
464
*****************************************************************************/
466
static BOOL process_lockingX(blocking_lock_record *blr)
468
char *inbuf = blr->inbuf;
469
unsigned char locktype = CVAL(inbuf,smb_vwv3);
470
files_struct *fsp = blr->fsp;
471
uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
472
uint16 num_locks = SVAL(inbuf,smb_vwv7);
473
SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
475
BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
477
BOOL my_lock_ctx = False;
478
NTSTATUS status = NT_STATUS_OK;
480
data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
483
* Data now points at the beginning of the list
484
* of smb_lkrng structs.
487
for(; blr->lock_num < num_locks; blr->lock_num++) {
490
lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
491
count = get_lock_count( data, blr->lock_num, large_file_format);
492
offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
495
* We know err cannot be set as if it was the lock
496
* request would never have been queued. JRA.
499
status = do_lock_spin(fsp,
503
((locktype & 1) ? READ_LOCK : WRITE_LOCK),
507
if (NT_STATUS_IS_ERR(status)) {
512
if(blr->lock_num == num_locks) {
514
* Success - we got all the locks.
517
DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
518
fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
520
reply_lockingX_success(blr);
522
} else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
523
!NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
525
* We have other than a "can't get lock"
526
* error. Free any locks we had and return an error.
527
* Return True so we get dequeued.
530
blocking_lock_reply_error(blr, status);
535
* Still can't get all the locks - keep waiting.
538
DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
540
blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
545
/****************************************************************************
546
Attempt to get the posix lock request from a SMBtrans2 call.
547
Returns True if we want to be removed from the list.
548
*****************************************************************************/
550
static BOOL process_trans2(blocking_lock_record *blr)
553
char *inbuf = blr->inbuf;
555
BOOL my_lock_ctx = False;
559
status = do_lock(blr->fsp,
567
if (!NT_STATUS_IS_OK(status)) {
568
if (ERROR_WAS_LOCK_DENIED(status)) {
569
/* Still can't get the lock, just keep waiting. */
573
* We have other than a "can't get lock"
574
* error. Send an error and return True so we get dequeued.
576
blocking_lock_reply_error(blr, status);
580
/* We finally got the lock, return success. */
581
outbuf = get_OutBuffer();
582
construct_reply_common(inbuf, outbuf);
583
SCVAL(outbuf,smb_com,SMBtrans2);
585
send_trans2_replies(outbuf, max_send, params, 2, NULL, 0);
590
/****************************************************************************
591
Process a blocking lock SMB.
592
Returns True if we want to be removed from the list.
593
*****************************************************************************/
595
static BOOL blocking_lock_record_process(blocking_lock_record *blr)
597
switch(blr->com_type) {
599
/* We no longer push blocking lock requests for anything but lockingX and trans2. */
601
return process_lock(blr);
603
return process_lockread(blr);
606
return process_lockingX(blr);
609
return process_trans2(blr);
611
DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
612
exit_server("PANIC - unknown type on blocking lock queue");
614
return False; /* Keep compiler happy. */
617
/****************************************************************************
618
Delete entries by fnum from the blocking lock pending queue.
619
*****************************************************************************/
621
void remove_pending_lock_requests_by_fid(files_struct *fsp)
623
blocking_lock_record *blr, *next = NULL;
625
for(blr = blocking_lock_queue; blr; blr = next) {
627
if(blr->fsp->fnum == fsp->fnum) {
628
struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
631
DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
632
file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
634
brl_remove_pending_lock(br_lck,
644
free_blocking_lock_record(blr);
649
/****************************************************************************
650
Delete entries by mid from the blocking lock pending queue. Always send reply.
651
*****************************************************************************/
653
void remove_pending_lock_requests_by_mid(int mid)
655
blocking_lock_record *blr, *next = NULL;
657
for(blr = blocking_lock_queue; blr; blr = next) {
659
if(SVAL(blr->inbuf,smb_mid) == mid) {
660
files_struct *fsp = blr->fsp;
661
struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
664
DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
665
file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
667
brl_remove_pending_lock(br_lck,
676
blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
677
free_blocking_lock_record(blr);
682
/****************************************************************************
683
Set a flag as an unlock request affects one of our pending locks.
684
*****************************************************************************/
686
static void received_unlock_msg(int msg_type, struct process_id src,
687
void *buf, size_t len)
689
DEBUG(10,("received_unlock_msg\n"));
690
process_blocking_lock_queue(time(NULL));
693
/****************************************************************************
694
Return the number of seconds to the next blocking locks timeout, or default_timeout
695
*****************************************************************************/
697
unsigned blocking_locks_timeout(unsigned default_timeout)
699
unsigned timeout = default_timeout;
701
blocking_lock_record *blr = blocking_lock_queue;
703
/* note that we avoid the time() syscall if there are no blocking locks */
709
for (; blr; blr = blr->next) {
710
if ((blr->expire_time != (time_t)-1) &&
711
(timeout > (blr->expire_time - t))) {
712
timeout = blr->expire_time - t;
722
/****************************************************************************
723
Process the blocking lock queue. Note that this is only called as root.
724
*****************************************************************************/
726
void process_blocking_lock_queue(time_t t)
728
blocking_lock_record *blr, *next = NULL;
731
* Go through the queue and see if we can get any of the locks.
734
for (blr = blocking_lock_queue; blr; blr = next) {
735
connection_struct *conn = NULL;
737
files_struct *fsp = NULL;
742
* Ensure we don't have any old chain_fsp values
749
conn = conn_find(SVAL(blr->inbuf,smb_tid));
750
vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
751
SVAL(blr->inbuf,smb_uid);
753
DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
754
fsp->fnum, fsp->fsp_name ));
756
if((blr->expire_time != -1) && (blr->expire_time <= t)) {
757
struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
760
* Lock expired - throw away all previously
761
* obtained locks and return lock error.
765
DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
766
fsp->fnum, fsp->fsp_name ));
768
brl_remove_pending_lock(br_lck,
777
blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
778
free_blocking_lock_record(blr);
782
if(!change_to_user(conn,vuid)) {
783
struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
786
* Remove the entry and return an error to the client.
790
brl_remove_pending_lock(br_lck,
799
DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
801
blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
802
free_blocking_lock_record(blr);
806
if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
807
struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
810
* Remove the entry and return an error to the client.
814
brl_remove_pending_lock(br_lck,
823
DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
824
blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
825
free_blocking_lock_record(blr);
826
change_to_root_user();
831
* Go through the remaining locks and try and obtain them.
832
* The call returns True if all locks were obtained successfully
833
* and False if we still need to wait.
836
if(blocking_lock_record_process(blr)) {
837
struct byte_range_lock *br_lck = brl_get_locks(NULL, fsp);
840
brl_remove_pending_lock(br_lck,
849
free_blocking_lock_record(blr);
851
change_to_root_user();