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/smbd.h"
22 #include "smbd/globals.h"
24 #include "lib/util/tevent_ntstatus.h"
25 #include "lib/dbwrap/dbwrap_watch.h"
26 #include "librpc/gen_ndr/ndr_open_files.h"
29 #define DBGC_CLASS DBGC_LOCKING
31 NTSTATUS
smbd_do_locks_try(
32 struct files_struct
*fsp
,
33 enum brl_flavour lock_flav
,
35 struct smbd_lock_element
*locks
,
36 uint16_t *blocker_idx
,
37 struct server_id
*blocking_pid
,
38 uint64_t *blocking_smblctx
)
40 NTSTATUS status
= NT_STATUS_OK
;
43 for (i
=0; i
<num_locks
; i
++) {
44 struct smbd_lock_element
*e
= &locks
[i
];
55 if (!NT_STATUS_IS_OK(status
)) {
60 if (NT_STATUS_IS_OK(status
)) {
67 * Undo the locks we successfully got
69 for (i
= i
-1; i
!= UINT16_MAX
; i
--) {
70 struct smbd_lock_element
*e
= &locks
[i
];
81 static bool smbd_smb1_fsp_add_blocked_lock_req(
82 struct files_struct
*fsp
, struct tevent_req
*req
)
84 size_t num_reqs
= talloc_array_length(fsp
->blocked_smb1_lock_reqs
);
85 struct tevent_req
**tmp
= NULL
;
89 fsp
->blocked_smb1_lock_reqs
,
95 fsp
->blocked_smb1_lock_reqs
= tmp
;
96 fsp
->blocked_smb1_lock_reqs
[num_reqs
] = req
;
100 struct smbd_smb1_do_locks_state
{
101 struct tevent_context
*ev
;
102 struct smb_request
*smbreq
;
103 struct files_struct
*fsp
;
105 uint32_t polling_msecs
;
106 struct timeval endtime
;
107 bool large_offset
; /* required for correct cancel */
108 enum brl_flavour lock_flav
;
110 struct smbd_lock_element
*locks
;
112 NTSTATUS deny_status
;
115 static void smbd_smb1_do_locks_try(struct tevent_req
*req
);
116 static void smbd_smb1_do_locks_retry(struct tevent_req
*subreq
);
117 static void smbd_smb1_blocked_locks_cleanup(
118 struct tevent_req
*req
, enum tevent_req_state req_state
);
120 static void smbd_smb1_do_locks_update_polling_msecs(
121 struct smbd_smb1_do_locks_state
*state
)
124 * The default lp_lock_spin_time() is 200ms.
126 * v_min is in the range of 0.002 to 20 secs
127 * (0.2 secs by default)
129 * v_max is in the range of 0.02 to 200 secs
130 * (2.0 secs by default)
132 * The typical steps are:
133 * 0.2, 0.4, 0.6, 0.8, ... 2.0
135 uint32_t v_min
= MAX(2, MIN(20000, lp_lock_spin_time()));
136 uint32_t v_max
= 10 * v_min
;
138 if (state
->polling_msecs
>= v_max
) {
139 state
->polling_msecs
= v_max
;
143 state
->polling_msecs
+= v_min
;
146 struct tevent_req
*smbd_smb1_do_locks_send(
148 struct tevent_context
*ev
,
149 struct smb_request
**smbreq
, /* talloc_move()d into our state */
150 struct files_struct
*fsp
,
151 uint32_t lock_timeout
,
153 enum brl_flavour lock_flav
,
155 struct smbd_lock_element
*locks
)
157 struct tevent_req
*req
= NULL
, *subreq
= NULL
;
158 struct smbd_smb1_do_locks_state
*state
= NULL
;
159 struct share_mode_lock
*lck
= NULL
;
160 struct server_id blocking_pid
= { 0 };
161 uint64_t blocking_smblctx
= 0;
162 struct timeval endtime
;
163 NTSTATUS status
= NT_STATUS_OK
;
166 req
= tevent_req_create(
167 mem_ctx
, &state
, struct smbd_smb1_do_locks_state
);
172 state
->smbreq
= talloc_move(state
, smbreq
);
174 state
->timeout
= lock_timeout
;
175 state
->large_offset
= large_offset
;
176 state
->lock_flav
= lock_flav
;
177 state
->num_locks
= num_locks
;
178 state
->locks
= locks
;
180 if (lock_flav
== POSIX_LOCK
) {
182 * SMB1 posix locks always use
183 * NT_STATUS_FILE_LOCK_CONFLICT.
185 state
->deny_status
= NT_STATUS_FILE_LOCK_CONFLICT
;
187 state
->deny_status
= NT_STATUS_LOCK_NOT_GRANTED
;
190 DBG_DEBUG("state=%p, state->smbreq=%p\n", state
, state
->smbreq
);
192 if (num_locks
== 0) {
193 DBG_DEBUG("no locks\n");
194 tevent_req_done(req
);
195 return tevent_req_post(req
, ev
);
198 if ((state
->timeout
!= 0) && (state
->timeout
!= UINT32_MAX
)) {
200 * Windows internal resolution for blocking locks
201 * seems to be about 200ms... Don't wait for less than
204 state
->timeout
= MAX(state
->timeout
, lp_lock_spin_time());
207 lck
= get_existing_share_mode_lock(state
, state
->fsp
->file_id
);
208 if (tevent_req_nomem(lck
, req
)) {
209 DBG_DEBUG("Could not get share mode lock\n");
210 return tevent_req_post(req
, ev
);
213 status
= smbd_do_locks_try(
221 if (NT_STATUS_IS_OK(status
)) {
222 tevent_req_done(req
);
225 if (!ERROR_WAS_LOCK_DENIED(status
)) {
226 tevent_req_nterror(req
, status
);
230 if (state
->timeout
== 0) {
231 struct smbd_lock_element
*blocker
= &locks
[state
->blocker
];
233 if ((blocker
->offset
>= 0xEF000000) &&
234 ((blocker
->offset
>> 63) == 0)) {
236 * This must be an optimization of an ancient
239 state
->timeout
= lp_lock_spin_time();
242 if ((fsp
->lock_failure_seen
) &&
243 (blocker
->offset
== fsp
->lock_failure_offset
)) {
245 * Delay repeated lock attempts on the same
246 * lock. Maybe a more advanced version of the
249 DBG_DEBUG("Delaying lock request due to previous "
251 state
->timeout
= lp_lock_spin_time();
255 DBG_DEBUG("timeout=%"PRIu32
", blocking_smblctx=%"PRIu64
"\n",
260 * If the endtime is not elapsed yet,
261 * it means we'll retry after a timeout.
262 * In that case we'll have to return
263 * NT_STATUS_FILE_LOCK_CONFLICT
264 * instead of NT_STATUS_LOCK_NOT_GRANTED.
266 if (state
->timeout
== 0) {
267 status
= state
->deny_status
;
268 tevent_req_nterror(req
, status
);
271 state
->deny_status
= NT_STATUS_FILE_LOCK_CONFLICT
;
273 subreq
= dbwrap_watched_watch_send(
274 state
, state
->ev
, lck
->data
->record
, blocking_pid
);
275 if (tevent_req_nomem(subreq
, req
)) {
279 tevent_req_set_callback(subreq
, smbd_smb1_do_locks_retry
, req
);
281 state
->endtime
= timeval_current_ofs_msec(state
->timeout
);
282 endtime
= state
->endtime
;
284 if (blocking_smblctx
== UINT64_MAX
) {
287 smbd_smb1_do_locks_update_polling_msecs(state
);
289 DBG_DEBUG("Blocked on a posix lock. Retry in %"PRIu32
" msecs\n",
290 state
->polling_msecs
);
292 tmp
= timeval_current_ofs_msec(state
->polling_msecs
);
293 endtime
= timeval_min(&endtime
, &tmp
);
296 ok
= tevent_req_set_endtime(subreq
, state
->ev
, endtime
);
302 ok
= smbd_smb1_fsp_add_blocked_lock_req(fsp
, req
);
307 tevent_req_set_cleanup_fn(req
, smbd_smb1_blocked_locks_cleanup
);
311 return tevent_req_post(req
, ev
);
314 static void smbd_smb1_blocked_locks_cleanup(
315 struct tevent_req
*req
, enum tevent_req_state req_state
)
317 struct smbd_smb1_do_locks_state
*state
= tevent_req_data(
318 req
, struct smbd_smb1_do_locks_state
);
319 struct files_struct
*fsp
= state
->fsp
;
320 struct tevent_req
**blocked
= fsp
->blocked_smb1_lock_reqs
;
321 size_t num_blocked
= talloc_array_length(blocked
);
324 DBG_DEBUG("req=%p, state=%p, req_state=%d\n",
329 if (req_state
== TEVENT_REQ_RECEIVED
) {
330 DBG_DEBUG("already received\n");
334 for (i
=0; i
<num_blocked
; i
++) {
335 if (blocked
[i
] == req
) {
339 SMB_ASSERT(i
<num_blocked
);
341 num_after
= num_blocked
- (i
+1);
345 * The locks need to be kept in order, see
346 * raw.lock.multilock2
350 sizeof(*blocked
) * num_after
);
352 fsp
->blocked_smb1_lock_reqs
= talloc_realloc(
353 fsp
, blocked
, struct tevent_req
*, num_blocked
-1);
356 static void smbd_smb1_do_locks_try(struct tevent_req
*req
)
358 struct smbd_smb1_do_locks_state
*state
= tevent_req_data(
359 req
, struct smbd_smb1_do_locks_state
);
360 struct files_struct
*fsp
= state
->fsp
;
361 struct tevent_req
**blocked
= fsp
->blocked_smb1_lock_reqs
;
362 struct tevent_req
*retry_req
= blocked
[0];
363 struct smbd_smb1_do_locks_state
*retry_state
= tevent_req_data(
364 retry_req
, struct smbd_smb1_do_locks_state
);
365 struct share_mode_lock
*lck
;
366 struct timeval endtime
;
367 struct server_id blocking_pid
= { 0 };
368 uint64_t blocking_smblctx
= 0;
369 struct tevent_req
*subreq
= NULL
;
374 lck
= get_existing_share_mode_lock(state
, fsp
->file_id
);
375 if (tevent_req_nomem(lck
, req
)) {
376 DBG_DEBUG("Could not get share mode lock\n");
380 status
= smbd_do_locks_try(
382 retry_state
->lock_flav
,
383 retry_state
->num_locks
,
388 if (NT_STATUS_IS_OK(status
)) {
391 if (!ERROR_WAS_LOCK_DENIED(status
)) {
396 * The client specified timeout elapsed
397 * avoid further retries.
399 * Otherwise keep waiting either waiting
400 * for changes in locking.tdb or the polling
401 * mode timers waiting for posix locks.
403 * If the endtime is not expired yet,
404 * it means we'll retry after a timeout.
405 * In that case we'll have to return
406 * NT_STATUS_FILE_LOCK_CONFLICT
407 * instead of NT_STATUS_LOCK_NOT_GRANTED.
409 elapsed
= timeval_elapsed(&state
->endtime
);
411 status
= state
->deny_status
;
414 state
->deny_status
= NT_STATUS_FILE_LOCK_CONFLICT
;
416 subreq
= dbwrap_watched_watch_send(
417 state
, state
->ev
, lck
->data
->record
, blocking_pid
);
418 if (tevent_req_nomem(subreq
, req
)) {
422 tevent_req_set_callback(subreq
, smbd_smb1_do_locks_retry
, req
);
424 endtime
= state
->endtime
;
426 if (blocking_smblctx
== UINT64_MAX
) {
429 smbd_smb1_do_locks_update_polling_msecs(state
);
431 DBG_DEBUG("Blocked on a posix lock. Retry in %"PRIu32
" msecs\n",
432 state
->polling_msecs
);
434 tmp
= timeval_current_ofs_msec(state
->polling_msecs
);
435 endtime
= timeval_min(&endtime
, &tmp
);
438 ok
= tevent_req_set_endtime(subreq
, state
->ev
, endtime
);
440 status
= NT_STATUS_NO_MEMORY
;
446 smbd_smb1_brl_finish_by_req(req
, status
);
449 static void smbd_smb1_do_locks_retry(struct tevent_req
*subreq
)
451 struct tevent_req
*req
= tevent_req_callback_data(
452 subreq
, struct tevent_req
);
453 struct smbd_smb1_do_locks_state
*state
= tevent_req_data(
454 req
, struct smbd_smb1_do_locks_state
);
459 * Make sure we run as the user again
461 ok
= change_to_user_by_fsp(state
->fsp
);
463 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
467 status
= dbwrap_watched_watch_recv(subreq
, NULL
, NULL
);
470 DBG_DEBUG("dbwrap_watched_watch_recv returned %s\n",
474 * We ignore any errors here, it's most likely
475 * we just get NT_STATUS_OK or NT_STATUS_IO_TIMEOUT.
477 * In any case we can just give it a retry.
480 smbd_smb1_do_locks_try(req
);
483 NTSTATUS
smbd_smb1_do_locks_recv(struct tevent_req
*req
)
485 struct smbd_smb1_do_locks_state
*state
= tevent_req_data(
486 req
, struct smbd_smb1_do_locks_state
);
487 NTSTATUS status
= NT_STATUS_OK
;
490 err
= tevent_req_is_nterror(req
, &status
);
492 DBG_DEBUG("err=%d, status=%s\n", (int)err
, nt_errstr(status
));
494 if (tevent_req_is_nterror(req
, &status
)) {
495 struct files_struct
*fsp
= state
->fsp
;
496 struct smbd_lock_element
*blocker
=
497 &state
->locks
[state
->blocker
];
499 DBG_DEBUG("Setting lock_failure_offset=%"PRIu64
"\n",
502 fsp
->lock_failure_seen
= true;
503 fsp
->lock_failure_offset
= blocker
->offset
;
507 tevent_req_received(req
);
512 bool smbd_smb1_do_locks_extract_smbreq(
513 struct tevent_req
*req
,
515 struct smb_request
**psmbreq
)
517 struct smbd_smb1_do_locks_state
*state
= tevent_req_data(
518 req
, struct smbd_smb1_do_locks_state
);
520 DBG_DEBUG("req=%p, state=%p, state->smbreq=%p\n",
525 if (state
->smbreq
== NULL
) {
528 *psmbreq
= talloc_move(mem_ctx
, &state
->smbreq
);
532 void smbd_smb1_brl_finish_by_req(struct tevent_req
*req
, NTSTATUS status
)
534 DBG_DEBUG("req=%p, status=%s\n", req
, nt_errstr(status
));
536 if (NT_STATUS_IS_OK(status
)) {
537 tevent_req_done(req
);
539 tevent_req_nterror(req
, status
);
543 bool smbd_smb1_brl_finish_by_lock(
544 struct files_struct
*fsp
,
546 enum brl_flavour lock_flav
,
547 struct smbd_lock_element lock
,
548 NTSTATUS finish_status
)
550 struct tevent_req
**blocked
= fsp
->blocked_smb1_lock_reqs
;
551 size_t num_blocked
= talloc_array_length(blocked
);
554 DBG_DEBUG("num_blocked=%zu\n", num_blocked
);
556 for (i
=0; i
<num_blocked
; i
++) {
557 struct tevent_req
*req
= blocked
[i
];
558 struct smbd_smb1_do_locks_state
*state
= tevent_req_data(
559 req
, struct smbd_smb1_do_locks_state
);
562 DBG_DEBUG("i=%zu, req=%p\n", i
, req
);
564 if ((state
->large_offset
!= large_offset
) ||
565 (state
->lock_flav
!= lock_flav
)) {
569 for (j
=0; j
<state
->num_locks
; j
++) {
570 struct smbd_lock_element
*l
= &state
->locks
[j
];
572 if ((lock
.smblctx
== l
->smblctx
) &&
573 (lock
.offset
== l
->offset
) &&
574 (lock
.count
== l
->count
)) {
575 smbd_smb1_brl_finish_by_req(
584 static struct files_struct
*smbd_smb1_brl_finish_by_mid_fn(
585 struct files_struct
*fsp
, void *private_data
)
587 struct tevent_req
**blocked
= fsp
->blocked_smb1_lock_reqs
;
588 size_t num_blocked
= talloc_array_length(blocked
);
589 uint64_t mid
= *((uint64_t *)private_data
);
592 DBG_DEBUG("fsp=%p, num_blocked=%zu\n", fsp
, num_blocked
);
594 for (i
=0; i
<num_blocked
; i
++) {
595 struct tevent_req
*req
= blocked
[i
];
596 struct smbd_smb1_do_locks_state
*state
= tevent_req_data(
597 req
, struct smbd_smb1_do_locks_state
);
598 struct smb_request
*smbreq
= state
->smbreq
;
600 if (smbreq
->mid
== mid
) {
601 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
610 * This walks the list of fsps, we store the blocked reqs attached to
611 * them. It can be expensive, but this is legacy SMB1 and trying to
612 * remember looking at traces I don't reall many of those calls.
615 bool smbd_smb1_brl_finish_by_mid(
616 struct smbd_server_connection
*sconn
, uint64_t mid
)
618 struct files_struct
*found
= files_forall(
619 sconn
, smbd_smb1_brl_finish_by_mid_fn
, &mid
);
620 return (found
!= NULL
);