1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Blocking Locking functions
|
---|
4 | Copyright (C) Jeremy Allison 1998-2003
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #include "smbd/globals.h"
|
---|
22 |
|
---|
23 | #undef DBGC_CLASS
|
---|
24 | #define DBGC_CLASS DBGC_LOCKING
|
---|
25 |
|
---|
26 | /****************************************************************************
|
---|
27 | Determine if this is a secondary element of a chained SMB.
|
---|
28 | **************************************************************************/
|
---|
29 |
|
---|
30 | static void received_unlock_msg(struct messaging_context *msg,
|
---|
31 | void *private_data,
|
---|
32 | uint32_t msg_type,
|
---|
33 | struct server_id server_id,
|
---|
34 | DATA_BLOB *data);
|
---|
35 |
|
---|
36 | static void brl_timeout_fn(struct event_context *event_ctx,
|
---|
37 | struct timed_event *te,
|
---|
38 | struct timeval now,
|
---|
39 | void *private_data)
|
---|
40 | {
|
---|
41 | SMB_ASSERT(brl_timeout == te);
|
---|
42 | TALLOC_FREE(brl_timeout);
|
---|
43 |
|
---|
44 | change_to_root_user(); /* TODO: Possibly run all timed events as
|
---|
45 | * root */
|
---|
46 |
|
---|
47 | process_blocking_lock_queue();
|
---|
48 | }
|
---|
49 |
|
---|
50 | /****************************************************************************
|
---|
51 | We need a version of timeval_min that treats zero timval as infinite.
|
---|
52 | ****************************************************************************/
|
---|
53 |
|
---|
54 | static struct timeval timeval_brl_min(const struct timeval *tv1,
|
---|
55 | const struct timeval *tv2)
|
---|
56 | {
|
---|
57 | if (timeval_is_zero(tv1)) {
|
---|
58 | return *tv2;
|
---|
59 | }
|
---|
60 | if (timeval_is_zero(tv2)) {
|
---|
61 | return *tv1;
|
---|
62 | }
|
---|
63 | return timeval_min(tv1, tv2);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /****************************************************************************
|
---|
67 | After a change to blocking_lock_queue, recalculate the timed_event for the
|
---|
68 | next processing.
|
---|
69 | ****************************************************************************/
|
---|
70 |
|
---|
71 | static bool recalc_brl_timeout(void)
|
---|
72 | {
|
---|
73 | struct blocking_lock_record *blr;
|
---|
74 | struct timeval next_timeout;
|
---|
75 |
|
---|
76 | TALLOC_FREE(brl_timeout);
|
---|
77 |
|
---|
78 | next_timeout = timeval_zero();
|
---|
79 |
|
---|
80 | for (blr = blocking_lock_queue; blr; blr = blr->next) {
|
---|
81 | if (timeval_is_zero(&blr->expire_time)) {
|
---|
82 | /*
|
---|
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.
|
---|
86 | */
|
---|
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);
|
---|
90 | }
|
---|
91 |
|
---|
92 | continue;
|
---|
93 | }
|
---|
94 |
|
---|
95 | next_timeout = timeval_brl_min(&next_timeout, &blr->expire_time);
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (timeval_is_zero(&next_timeout)) {
|
---|
99 | DEBUG(10, ("Next timeout = Infinite.\n"));
|
---|
100 | return True;
|
---|
101 | }
|
---|
102 |
|
---|
103 | if (DEBUGLVL(10)) {
|
---|
104 | struct timeval cur, from_now;
|
---|
105 |
|
---|
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));
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
|
---|
113 | next_timeout,
|
---|
114 | brl_timeout_fn, NULL))) {
|
---|
115 | return False;
|
---|
116 | }
|
---|
117 |
|
---|
118 | return True;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | /****************************************************************************
|
---|
123 | Function to push a blocking lock request onto the lock queue.
|
---|
124 | ****************************************************************************/
|
---|
125 |
|
---|
126 | bool push_blocking_lock_request( struct byte_range_lock *br_lck,
|
---|
127 | struct smb_request *req,
|
---|
128 | files_struct *fsp,
|
---|
129 | int lock_timeout,
|
---|
130 | int lock_num,
|
---|
131 | uint32_t lock_pid,
|
---|
132 | enum brl_type lock_type,
|
---|
133 | enum brl_flavour lock_flav,
|
---|
134 | uint64_t offset,
|
---|
135 | uint64_t count,
|
---|
136 | uint32_t blocking_pid)
|
---|
137 | {
|
---|
138 | struct blocking_lock_record *blr;
|
---|
139 | NTSTATUS status;
|
---|
140 |
|
---|
141 | if(req_is_in_chain(req)) {
|
---|
142 | DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
|
---|
143 | return False;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * Now queue an entry on the blocking lock queue. We setup
|
---|
148 | * the expiration time here.
|
---|
149 | */
|
---|
150 |
|
---|
151 | blr = talloc(NULL, struct blocking_lock_record);
|
---|
152 | if (blr == NULL) {
|
---|
153 | DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
|
---|
154 | return False;
|
---|
155 | }
|
---|
156 |
|
---|
157 | blr->next = NULL;
|
---|
158 | blr->prev = NULL;
|
---|
159 |
|
---|
160 | blr->fsp = fsp;
|
---|
161 | if (lock_timeout == -1) {
|
---|
162 | blr->expire_time.tv_sec = 0;
|
---|
163 | blr->expire_time.tv_usec = 0; /* Never expire. */
|
---|
164 | } else {
|
---|
165 | blr->expire_time = timeval_current_ofs(lock_timeout/1000,
|
---|
166 | (lock_timeout % 1000) * 1000);
|
---|
167 | }
|
---|
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;
|
---|
174 | blr->count = count;
|
---|
175 |
|
---|
176 | /* Specific brl_lock() implementations can fill this in. */
|
---|
177 | blr->blr_private = NULL;
|
---|
178 |
|
---|
179 | /* Add a pending lock record for this. */
|
---|
180 | status = brl_lock(smbd_messaging_context(),
|
---|
181 | br_lck,
|
---|
182 | lock_pid,
|
---|
183 | procid_self(),
|
---|
184 | offset,
|
---|
185 | count,
|
---|
186 | lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
|
---|
187 | blr->lock_flav,
|
---|
188 | True,
|
---|
189 | NULL,
|
---|
190 | blr);
|
---|
191 |
|
---|
192 | if (!NT_STATUS_IS_OK(status)) {
|
---|
193 | DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
|
---|
194 | TALLOC_FREE(blr);
|
---|
195 | return False;
|
---|
196 | }
|
---|
197 |
|
---|
198 | SMB_PERFCOUNT_DEFER_OP(&req->pcd, &req->pcd);
|
---|
199 | blr->req = talloc_move(blr, &req);
|
---|
200 |
|
---|
201 | DLIST_ADD_END(blocking_lock_queue, blr, struct blocking_lock_record *);
|
---|
202 | recalc_brl_timeout();
|
---|
203 |
|
---|
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;
|
---|
209 | }
|
---|
210 |
|
---|
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, fsp_str_dbg(blr->fsp)));
|
---|
216 |
|
---|
217 | return True;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /****************************************************************************
|
---|
221 | Return a lockingX success SMB.
|
---|
222 | *****************************************************************************/
|
---|
223 |
|
---|
224 | static void reply_lockingX_success(struct blocking_lock_record *blr)
|
---|
225 | {
|
---|
226 | reply_outbuf(blr->req, 2, 0);
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * As this message is a lockingX call we must handle
|
---|
230 | * any following chained message correctly.
|
---|
231 | * This is normally handled in construct_reply(),
|
---|
232 | * but as that calls switch_message, we can't use
|
---|
233 | * that here and must set up the chain info manually.
|
---|
234 | */
|
---|
235 |
|
---|
236 | chain_reply(blr->req);
|
---|
237 | TALLOC_FREE(blr->req->outbuf);
|
---|
238 | }
|
---|
239 |
|
---|
240 | /****************************************************************************
|
---|
241 | Return a generic lock fail error blocking call.
|
---|
242 | *****************************************************************************/
|
---|
243 |
|
---|
244 | static void generic_blocking_lock_error(struct blocking_lock_record *blr, NTSTATUS status)
|
---|
245 | {
|
---|
246 | /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
|
---|
247 | FILE_LOCK_CONFLICT! (tridge) */
|
---|
248 | if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
|
---|
249 | status = NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
|
---|
253 | /* Store the last lock error. */
|
---|
254 | files_struct *fsp = blr->fsp;
|
---|
255 |
|
---|
256 | if (fsp) {
|
---|
257 | fsp->last_lock_failure.context.smbpid = blr->lock_pid;
|
---|
258 | fsp->last_lock_failure.context.tid = fsp->conn->cnum;
|
---|
259 | fsp->last_lock_failure.context.pid = procid_self();
|
---|
260 | fsp->last_lock_failure.start = blr->offset;
|
---|
261 | fsp->last_lock_failure.size = blr->count;
|
---|
262 | fsp->last_lock_failure.fnum = fsp->fnum;
|
---|
263 | fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
|
---|
264 | fsp->last_lock_failure.lock_flav = blr->lock_flav;
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | reply_nterror(blr->req, status);
|
---|
269 | if (!srv_send_smb(smbd_server_fd(), (char *)blr->req->outbuf,
|
---|
270 | true, blr->req->seqnum+1,
|
---|
271 | blr->req->encrypted, NULL)) {
|
---|
272 | exit_server_cleanly("generic_blocking_lock_error: srv_send_smb failed.");
|
---|
273 | }
|
---|
274 | TALLOC_FREE(blr->req->outbuf);
|
---|
275 | }
|
---|
276 |
|
---|
277 | /****************************************************************************
|
---|
278 | Return a lock fail error for a lockingX call. Undo all the locks we have
|
---|
279 | obtained first.
|
---|
280 | *****************************************************************************/
|
---|
281 |
|
---|
282 | static void reply_lockingX_error(struct blocking_lock_record *blr, NTSTATUS status)
|
---|
283 | {
|
---|
284 | files_struct *fsp = blr->fsp;
|
---|
285 | uint16 num_ulocks = SVAL(blr->req->vwv+6, 0);
|
---|
286 | uint64_t count = (uint64_t)0, offset = (uint64_t) 0;
|
---|
287 | uint32 lock_pid;
|
---|
288 | unsigned char locktype = CVAL(blr->req->vwv+3, 0);
|
---|
289 | bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
|
---|
290 | uint8_t *data;
|
---|
291 | int i;
|
---|
292 |
|
---|
293 | data = (uint8_t *)blr->req->buf
|
---|
294 | + ((large_file_format ? 20 : 10)*num_ulocks);
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * Data now points at the beginning of the list
|
---|
298 | * of smb_lkrng structs.
|
---|
299 | */
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * Ensure we don't do a remove on the lock that just failed,
|
---|
303 | * as under POSIX rules, if we have a lock already there, we
|
---|
304 | * will delete it (and we shouldn't) .....
|
---|
305 | */
|
---|
306 |
|
---|
307 | for(i = blr->lock_num - 1; i >= 0; i--) {
|
---|
308 | bool err;
|
---|
309 |
|
---|
310 | lock_pid = get_lock_pid( data, i, large_file_format);
|
---|
311 | count = get_lock_count( data, i, large_file_format);
|
---|
312 | offset = get_lock_offset( data, i, large_file_format, &err);
|
---|
313 |
|
---|
314 | /*
|
---|
315 | * We know err cannot be set as if it was the lock
|
---|
316 | * request would never have been queued. JRA.
|
---|
317 | */
|
---|
318 |
|
---|
319 | do_unlock(smbd_messaging_context(),
|
---|
320 | fsp,
|
---|
321 | lock_pid,
|
---|
322 | count,
|
---|
323 | offset,
|
---|
324 | WINDOWS_LOCK);
|
---|
325 | }
|
---|
326 |
|
---|
327 | generic_blocking_lock_error(blr, status);
|
---|
328 | }
|
---|
329 |
|
---|
330 | /****************************************************************************
|
---|
331 | Return a lock fail error.
|
---|
332 | *****************************************************************************/
|
---|
333 |
|
---|
334 | static void blocking_lock_reply_error(struct blocking_lock_record *blr, NTSTATUS status)
|
---|
335 | {
|
---|
336 | DEBUG(10, ("Replying with error=%s. BLR = %p\n", nt_errstr(status), blr));
|
---|
337 |
|
---|
338 | switch(blr->req->cmd) {
|
---|
339 | case SMBlockingX:
|
---|
340 | reply_lockingX_error(blr, status);
|
---|
341 | break;
|
---|
342 | case SMBtrans2:
|
---|
343 | case SMBtranss2:
|
---|
344 | reply_nterror(blr->req, status);
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * construct_reply_common has done us the favor to pre-fill
|
---|
348 | * the command field with SMBtranss2 which is wrong :-)
|
---|
349 | */
|
---|
350 | SCVAL(blr->req->outbuf,smb_com,SMBtrans2);
|
---|
351 |
|
---|
352 | if (!srv_send_smb(smbd_server_fd(),
|
---|
353 | (char *)blr->req->outbuf,
|
---|
354 | true, blr->req->seqnum+1,
|
---|
355 | IS_CONN_ENCRYPTED(blr->fsp->conn),
|
---|
356 | NULL)) {
|
---|
357 | exit_server_cleanly("blocking_lock_reply_error: "
|
---|
358 | "srv_send_smb failed.");
|
---|
359 | }
|
---|
360 | TALLOC_FREE(blr->req->outbuf);
|
---|
361 | break;
|
---|
362 | default:
|
---|
363 | DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
|
---|
364 | exit_server("PANIC - unknown type on blocking lock queue");
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | /****************************************************************************
|
---|
369 | Attempt to finish off getting all pending blocking locks for a lockingX call.
|
---|
370 | Returns True if we want to be removed from the list.
|
---|
371 | *****************************************************************************/
|
---|
372 |
|
---|
373 | static bool process_lockingX(struct blocking_lock_record *blr)
|
---|
374 | {
|
---|
375 | unsigned char locktype = CVAL(blr->req->vwv+3, 0);
|
---|
376 | files_struct *fsp = blr->fsp;
|
---|
377 | uint16 num_ulocks = SVAL(blr->req->vwv+6, 0);
|
---|
378 | uint16 num_locks = SVAL(blr->req->vwv+7, 0);
|
---|
379 | uint64_t count = (uint64_t)0, offset = (uint64_t)0;
|
---|
380 | uint32 lock_pid;
|
---|
381 | bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
|
---|
382 | uint8_t *data;
|
---|
383 | NTSTATUS status = NT_STATUS_OK;
|
---|
384 |
|
---|
385 | data = (uint8_t *)blr->req->buf
|
---|
386 | + ((large_file_format ? 20 : 10)*num_ulocks);
|
---|
387 |
|
---|
388 | /*
|
---|
389 | * Data now points at the beginning of the list
|
---|
390 | * of smb_lkrng structs.
|
---|
391 | */
|
---|
392 |
|
---|
393 | for(; blr->lock_num < num_locks; blr->lock_num++) {
|
---|
394 | struct byte_range_lock *br_lck = NULL;
|
---|
395 | bool err;
|
---|
396 |
|
---|
397 | lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
|
---|
398 | count = get_lock_count( data, blr->lock_num, large_file_format);
|
---|
399 | offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
|
---|
400 |
|
---|
401 | /*
|
---|
402 | * We know err cannot be set as if it was the lock
|
---|
403 | * request would never have been queued. JRA.
|
---|
404 | */
|
---|
405 | errno = 0;
|
---|
406 | br_lck = do_lock(smbd_messaging_context(),
|
---|
407 | fsp,
|
---|
408 | lock_pid,
|
---|
409 | count,
|
---|
410 | offset,
|
---|
411 | ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
|
---|
412 | READ_LOCK : WRITE_LOCK),
|
---|
413 | WINDOWS_LOCK,
|
---|
414 | True,
|
---|
415 | &status,
|
---|
416 | &blr->blocking_pid,
|
---|
417 | blr);
|
---|
418 |
|
---|
419 | TALLOC_FREE(br_lck);
|
---|
420 |
|
---|
421 | if (NT_STATUS_IS_ERR(status)) {
|
---|
422 | break;
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 | if(blr->lock_num == num_locks) {
|
---|
427 | /*
|
---|
428 | * Success - we got all the locks.
|
---|
429 | */
|
---|
430 |
|
---|
431 | DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d "
|
---|
432 | "num_locks=%d\n", fsp_str_dbg(fsp), fsp->fnum,
|
---|
433 | (unsigned int)locktype, num_locks));
|
---|
434 |
|
---|
435 | reply_lockingX_success(blr);
|
---|
436 | return True;
|
---|
437 | }
|
---|
438 |
|
---|
439 | if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
|
---|
440 | !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
|
---|
441 | /*
|
---|
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.
|
---|
445 | */
|
---|
446 | blocking_lock_reply_error(blr, status);
|
---|
447 | return True;
|
---|
448 | }
|
---|
449 |
|
---|
450 | /*
|
---|
451 | * Still can't get all the locks - keep waiting.
|
---|
452 | */
|
---|
453 |
|
---|
454 | DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
|
---|
455 | Waiting....\n",
|
---|
456 | blr->lock_num, num_locks, fsp_str_dbg(fsp), fsp->fnum));
|
---|
457 |
|
---|
458 | return False;
|
---|
459 | }
|
---|
460 |
|
---|
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 | *****************************************************************************/
|
---|
465 |
|
---|
466 | static bool process_trans2(struct blocking_lock_record *blr)
|
---|
467 | {
|
---|
468 | char params[2];
|
---|
469 | NTSTATUS status;
|
---|
470 | struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
|
---|
471 | blr->fsp,
|
---|
472 | blr->lock_pid,
|
---|
473 | blr->count,
|
---|
474 | blr->offset,
|
---|
475 | blr->lock_type,
|
---|
476 | blr->lock_flav,
|
---|
477 | True,
|
---|
478 | &status,
|
---|
479 | &blr->blocking_pid,
|
---|
480 | blr);
|
---|
481 | TALLOC_FREE(br_lck);
|
---|
482 |
|
---|
483 | if (!NT_STATUS_IS_OK(status)) {
|
---|
484 | if (ERROR_WAS_LOCK_DENIED(status)) {
|
---|
485 | /* Still can't get the lock, just keep waiting. */
|
---|
486 | return False;
|
---|
487 | }
|
---|
488 | /*
|
---|
489 | * We have other than a "can't get lock"
|
---|
490 | * error. Send an error and return True so we get dequeued.
|
---|
491 | */
|
---|
492 | blocking_lock_reply_error(blr, status);
|
---|
493 | return True;
|
---|
494 | }
|
---|
495 |
|
---|
496 | /* We finally got the lock, return success. */
|
---|
497 |
|
---|
498 | SSVAL(params,0,0);
|
---|
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);
|
---|
501 | return True;
|
---|
502 | }
|
---|
503 |
|
---|
504 |
|
---|
505 | /****************************************************************************
|
---|
506 | Process a blocking lock SMB.
|
---|
507 | Returns True if we want to be removed from the list.
|
---|
508 | *****************************************************************************/
|
---|
509 |
|
---|
510 | static bool blocking_lock_record_process(struct blocking_lock_record *blr)
|
---|
511 | {
|
---|
512 | switch(blr->req->cmd) {
|
---|
513 | case SMBlockingX:
|
---|
514 | return process_lockingX(blr);
|
---|
515 | case SMBtrans2:
|
---|
516 | case SMBtranss2:
|
---|
517 | return process_trans2(blr);
|
---|
518 | default:
|
---|
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");
|
---|
521 | }
|
---|
522 | return False; /* Keep compiler happy. */
|
---|
523 | }
|
---|
524 |
|
---|
525 | /****************************************************************************
|
---|
526 | Cancel entries by fnum from the blocking lock pending queue.
|
---|
527 | *****************************************************************************/
|
---|
528 |
|
---|
529 | void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
|
---|
530 | {
|
---|
531 | struct blocking_lock_record *blr, *blr_cancelled, *next = NULL;
|
---|
532 |
|
---|
533 | for(blr = blocking_lock_queue; blr; blr = next) {
|
---|
534 | unsigned char locktype = 0;
|
---|
535 |
|
---|
536 | next = blr->next;
|
---|
537 | if (blr->fsp->fnum != fsp->fnum) {
|
---|
538 | continue;
|
---|
539 | }
|
---|
540 |
|
---|
541 | if (blr->req->cmd == SMBlockingX) {
|
---|
542 | locktype = CVAL(blr->req->vwv+3, 0);
|
---|
543 | }
|
---|
544 |
|
---|
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_str_dbg(fsp), fsp->fnum));
|
---|
548 |
|
---|
549 | blr_cancelled = blocking_lock_cancel(fsp,
|
---|
550 | blr->lock_pid,
|
---|
551 | blr->offset,
|
---|
552 | blr->count,
|
---|
553 | blr->lock_flav,
|
---|
554 | locktype,
|
---|
555 | NT_STATUS_RANGE_NOT_LOCKED);
|
---|
556 |
|
---|
557 | SMB_ASSERT(blr_cancelled == blr);
|
---|
558 |
|
---|
559 | brl_lock_cancel(br_lck,
|
---|
560 | blr->lock_pid,
|
---|
561 | procid_self(),
|
---|
562 | blr->offset,
|
---|
563 | blr->count,
|
---|
564 | blr->lock_flav,
|
---|
565 | blr);
|
---|
566 |
|
---|
567 | /* We're closing the file fsp here, so ensure
|
---|
568 | * we don't have a dangling pointer. */
|
---|
569 | blr->fsp = NULL;
|
---|
570 | }
|
---|
571 | }
|
---|
572 |
|
---|
573 | /****************************************************************************
|
---|
574 | Delete entries by mid from the blocking lock pending queue. Always send reply.
|
---|
575 | *****************************************************************************/
|
---|
576 |
|
---|
577 | void remove_pending_lock_requests_by_mid(int mid)
|
---|
578 | {
|
---|
579 | struct blocking_lock_record *blr, *next = NULL;
|
---|
580 |
|
---|
581 | for(blr = blocking_lock_queue; blr; blr = next) {
|
---|
582 | files_struct *fsp;
|
---|
583 | struct byte_range_lock *br_lck;
|
---|
584 |
|
---|
585 | next = blr->next;
|
---|
586 |
|
---|
587 | if (blr->req->mid != mid) {
|
---|
588 | continue;
|
---|
589 | }
|
---|
590 |
|
---|
591 | fsp = blr->fsp;
|
---|
592 | br_lck = brl_get_locks(talloc_tos(), fsp);
|
---|
593 |
|
---|
594 | if (br_lck) {
|
---|
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_str_dbg(fsp),
|
---|
598 | fsp->fnum ));
|
---|
599 |
|
---|
600 | brl_lock_cancel(br_lck,
|
---|
601 | blr->lock_pid,
|
---|
602 | procid_self(),
|
---|
603 | blr->offset,
|
---|
604 | blr->count,
|
---|
605 | blr->lock_flav,
|
---|
606 | blr);
|
---|
607 | TALLOC_FREE(br_lck);
|
---|
608 | }
|
---|
609 |
|
---|
610 | blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
|
---|
611 | DLIST_REMOVE(blocking_lock_queue, blr);
|
---|
612 | TALLOC_FREE(blr);
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | /****************************************************************************
|
---|
617 | Is this mid a blocking lock request on the queue ?
|
---|
618 | *****************************************************************************/
|
---|
619 |
|
---|
620 | bool blocking_lock_was_deferred(int mid)
|
---|
621 | {
|
---|
622 | struct blocking_lock_record *blr, *next = NULL;
|
---|
623 |
|
---|
624 | for(blr = blocking_lock_queue; blr; blr = next) {
|
---|
625 | next = blr->next;
|
---|
626 | if(blr->req->mid == mid) {
|
---|
627 | return True;
|
---|
628 | }
|
---|
629 | }
|
---|
630 | return False;
|
---|
631 | }
|
---|
632 |
|
---|
633 | /****************************************************************************
|
---|
634 | Set a flag as an unlock request affects one of our pending locks.
|
---|
635 | *****************************************************************************/
|
---|
636 |
|
---|
637 | static void received_unlock_msg(struct messaging_context *msg,
|
---|
638 | void *private_data,
|
---|
639 | uint32_t msg_type,
|
---|
640 | struct server_id server_id,
|
---|
641 | DATA_BLOB *data)
|
---|
642 | {
|
---|
643 | DEBUG(10,("received_unlock_msg\n"));
|
---|
644 | process_blocking_lock_queue();
|
---|
645 | }
|
---|
646 |
|
---|
647 | /****************************************************************************
|
---|
648 | Process the blocking lock queue. Note that this is only called as root.
|
---|
649 | *****************************************************************************/
|
---|
650 |
|
---|
651 | void process_blocking_lock_queue(void)
|
---|
652 | {
|
---|
653 | struct timeval tv_curr = timeval_current();
|
---|
654 | struct blocking_lock_record *blr, *next = NULL;
|
---|
655 |
|
---|
656 | /*
|
---|
657 | * Go through the queue and see if we can get any of the locks.
|
---|
658 | */
|
---|
659 |
|
---|
660 | for (blr = blocking_lock_queue; blr; blr = next) {
|
---|
661 |
|
---|
662 | next = blr->next;
|
---|
663 |
|
---|
664 | /*
|
---|
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.
|
---|
668 | */
|
---|
669 |
|
---|
670 | DEBUG(10, ("Processing BLR = %p\n", blr));
|
---|
671 |
|
---|
672 | /* We use set_current_service so connections with
|
---|
673 | * pending locks are not marked as idle.
|
---|
674 | */
|
---|
675 |
|
---|
676 | set_current_service(blr->fsp->conn,
|
---|
677 | SVAL(blr->req->inbuf,smb_flg),
|
---|
678 | false);
|
---|
679 |
|
---|
680 | if(blocking_lock_record_process(blr)) {
|
---|
681 | struct byte_range_lock *br_lck = brl_get_locks(
|
---|
682 | talloc_tos(), blr->fsp);
|
---|
683 |
|
---|
684 | DEBUG(10, ("BLR_process returned true: cancelling and "
|
---|
685 | "removing lock. BLR = %p\n", blr));
|
---|
686 |
|
---|
687 | if (br_lck) {
|
---|
688 | brl_lock_cancel(br_lck,
|
---|
689 | blr->lock_pid,
|
---|
690 | procid_self(),
|
---|
691 | blr->offset,
|
---|
692 | blr->count,
|
---|
693 | blr->lock_flav,
|
---|
694 | blr);
|
---|
695 | TALLOC_FREE(br_lck);
|
---|
696 | }
|
---|
697 |
|
---|
698 | DLIST_REMOVE(blocking_lock_queue, blr);
|
---|
699 | TALLOC_FREE(blr);
|
---|
700 | continue;
|
---|
701 | }
|
---|
702 |
|
---|
703 | /*
|
---|
704 | * We couldn't get the locks for this record on the list.
|
---|
705 | * If the time has expired, return a lock error.
|
---|
706 | */
|
---|
707 |
|
---|
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);
|
---|
711 |
|
---|
712 | DEBUG(10, ("Lock timed out! BLR = %p\n", blr));
|
---|
713 |
|
---|
714 | /*
|
---|
715 | * Lock expired - throw away all previously
|
---|
716 | * obtained locks and return lock error.
|
---|
717 | */
|
---|
718 |
|
---|
719 | if (br_lck) {
|
---|
720 | DEBUG(5,("process_blocking_lock_queue: "
|
---|
721 | "pending lock fnum = %d for file %s "
|
---|
722 | "timed out.\n", blr->fsp->fnum,
|
---|
723 | fsp_str_dbg(blr->fsp)));
|
---|
724 |
|
---|
725 | brl_lock_cancel(br_lck,
|
---|
726 | blr->lock_pid,
|
---|
727 | procid_self(),
|
---|
728 | blr->offset,
|
---|
729 | blr->count,
|
---|
730 | blr->lock_flav,
|
---|
731 | blr);
|
---|
732 | TALLOC_FREE(br_lck);
|
---|
733 | }
|
---|
734 |
|
---|
735 | blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
|
---|
736 | DLIST_REMOVE(blocking_lock_queue, blr);
|
---|
737 | TALLOC_FREE(blr);
|
---|
738 | }
|
---|
739 | }
|
---|
740 |
|
---|
741 | recalc_brl_timeout();
|
---|
742 | }
|
---|
743 |
|
---|
744 | /****************************************************************************
|
---|
745 | Handle a cancel message. Lock already moved onto the cancel queue.
|
---|
746 | *****************************************************************************/
|
---|
747 |
|
---|
748 | #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(struct blocking_lock_record *) + sizeof(NTSTATUS))
|
---|
749 |
|
---|
750 | static void process_blocking_lock_cancel_message(struct messaging_context *ctx,
|
---|
751 | void *private_data,
|
---|
752 | uint32_t msg_type,
|
---|
753 | struct server_id server_id,
|
---|
754 | DATA_BLOB *data)
|
---|
755 | {
|
---|
756 | NTSTATUS err;
|
---|
757 | const char *msg = (const char *)data->data;
|
---|
758 | struct blocking_lock_record *blr;
|
---|
759 |
|
---|
760 | if (data->data == NULL) {
|
---|
761 | smb_panic("process_blocking_lock_cancel_message: null msg");
|
---|
762 | }
|
---|
763 |
|
---|
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");
|
---|
768 | }
|
---|
769 |
|
---|
770 | memcpy(&blr, msg, sizeof(blr));
|
---|
771 | memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
|
---|
772 |
|
---|
773 | DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
|
---|
774 | nt_errstr(err) ));
|
---|
775 |
|
---|
776 | blocking_lock_reply_error(blr, err);
|
---|
777 | DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
|
---|
778 | TALLOC_FREE(blr);
|
---|
779 | }
|
---|
780 |
|
---|
781 | /****************************************************************************
|
---|
782 | Send ourselves a blocking lock cancelled message. Handled asynchronously above.
|
---|
783 | Returns the blocking_lock_record that is being cancelled.
|
---|
784 | *****************************************************************************/
|
---|
785 |
|
---|
786 | struct blocking_lock_record *blocking_lock_cancel(files_struct *fsp,
|
---|
787 | uint32 lock_pid,
|
---|
788 | uint64_t offset,
|
---|
789 | uint64_t count,
|
---|
790 | enum brl_flavour lock_flav,
|
---|
791 | unsigned char locktype,
|
---|
792 | NTSTATUS err)
|
---|
793 | {
|
---|
794 | char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
|
---|
795 | struct blocking_lock_record *blr;
|
---|
796 |
|
---|
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);
|
---|
802 |
|
---|
803 | blocking_lock_cancel_state = True;
|
---|
804 | }
|
---|
805 |
|
---|
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) {
|
---|
812 | break;
|
---|
813 | }
|
---|
814 | }
|
---|
815 |
|
---|
816 | if (!blr) {
|
---|
817 | return NULL;
|
---|
818 | }
|
---|
819 |
|
---|
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)) {
|
---|
824 | return NULL;
|
---|
825 | }
|
---|
826 |
|
---|
827 | /* Move to cancelled queue. */
|
---|
828 | DLIST_REMOVE(blocking_lock_queue, blr);
|
---|
829 | DLIST_ADD(blocking_lock_cancelled_queue, blr);
|
---|
830 |
|
---|
831 | /* Create the message. */
|
---|
832 | memcpy(msg, &blr, sizeof(blr));
|
---|
833 | memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
|
---|
834 |
|
---|
835 | messaging_send_buf(smbd_messaging_context(), procid_self(),
|
---|
836 | MSG_SMB_BLOCKING_LOCK_CANCEL,
|
---|
837 | (uint8 *)&msg, sizeof(msg));
|
---|
838 |
|
---|
839 | return blr;
|
---|
840 | }
|
---|