1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | byte range locking code
|
---|
4 | Updated to handle range splits/merges.
|
---|
5 |
|
---|
6 | Copyright (C) Andrew Tridgell 1992-2000
|
---|
7 | Copyright (C) Jeremy Allison 1992-2000
|
---|
8 |
|
---|
9 | This program is free software; you can redistribute it and/or modify
|
---|
10 | it under the terms of the GNU General Public License as published by
|
---|
11 | the Free Software Foundation; either version 3 of the License, or
|
---|
12 | (at your option) any later version.
|
---|
13 |
|
---|
14 | This program is distributed in the hope that it will be useful,
|
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | GNU General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU General Public License
|
---|
20 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /* This module implements a tdb based byte range locking service,
|
---|
24 | replacing the fcntl() based byte range locking previously
|
---|
25 | used. This allows us to provide the same semantics as NT */
|
---|
26 |
|
---|
27 | #include "includes.h"
|
---|
28 | #include "system/filesys.h"
|
---|
29 | #include "locking/proto.h"
|
---|
30 | #include "smbd/globals.h"
|
---|
31 | #include "dbwrap/dbwrap.h"
|
---|
32 | #include "dbwrap/dbwrap_open.h"
|
---|
33 | #include "serverid.h"
|
---|
34 | #include "messages.h"
|
---|
35 | #include "util_tdb.h"
|
---|
36 |
|
---|
37 | #undef DBGC_CLASS
|
---|
38 | #define DBGC_CLASS DBGC_LOCKING
|
---|
39 |
|
---|
40 | #define ZERO_ZERO 0
|
---|
41 |
|
---|
42 | /* The open brlock.tdb database. */
|
---|
43 |
|
---|
44 | static struct db_context *brlock_db;
|
---|
45 |
|
---|
46 | struct byte_range_lock {
|
---|
47 | struct files_struct *fsp;
|
---|
48 | unsigned int num_locks;
|
---|
49 | bool modified;
|
---|
50 | uint32_t num_read_oplocks;
|
---|
51 | struct lock_struct *lock_data;
|
---|
52 | struct db_record *record;
|
---|
53 | };
|
---|
54 |
|
---|
55 | /****************************************************************************
|
---|
56 | Debug info at level 10 for lock struct.
|
---|
57 | ****************************************************************************/
|
---|
58 |
|
---|
59 | static void print_lock_struct(unsigned int i, const struct lock_struct *pls)
|
---|
60 | {
|
---|
61 | struct server_id_buf tmp;
|
---|
62 |
|
---|
63 | DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
|
---|
64 | i,
|
---|
65 | (unsigned long long)pls->context.smblctx,
|
---|
66 | (unsigned int)pls->context.tid,
|
---|
67 | server_id_str_buf(pls->context.pid, &tmp) ));
|
---|
68 |
|
---|
69 | DEBUG(10, ("start = %ju, size = %ju, fnum = %ju, %s %s\n",
|
---|
70 | (uintmax_t)pls->start,
|
---|
71 | (uintmax_t)pls->size,
|
---|
72 | (uintmax_t)pls->fnum,
|
---|
73 | lock_type_name(pls->lock_type),
|
---|
74 | lock_flav_name(pls->lock_flav)));
|
---|
75 | }
|
---|
76 |
|
---|
77 | unsigned int brl_num_locks(const struct byte_range_lock *brl)
|
---|
78 | {
|
---|
79 | return brl->num_locks;
|
---|
80 | }
|
---|
81 |
|
---|
82 | struct files_struct *brl_fsp(struct byte_range_lock *brl)
|
---|
83 | {
|
---|
84 | return brl->fsp;
|
---|
85 | }
|
---|
86 |
|
---|
87 | uint32_t brl_num_read_oplocks(const struct byte_range_lock *brl)
|
---|
88 | {
|
---|
89 | return brl->num_read_oplocks;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void brl_set_num_read_oplocks(struct byte_range_lock *brl,
|
---|
93 | uint32_t num_read_oplocks)
|
---|
94 | {
|
---|
95 | DEBUG(10, ("Setting num_read_oplocks to %"PRIu32"\n",
|
---|
96 | num_read_oplocks));
|
---|
97 | SMB_ASSERT(brl->record != NULL); /* otherwise we're readonly */
|
---|
98 | brl->num_read_oplocks = num_read_oplocks;
|
---|
99 | brl->modified = true;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /****************************************************************************
|
---|
103 | See if two locking contexts are equal.
|
---|
104 | ****************************************************************************/
|
---|
105 |
|
---|
106 | static bool brl_same_context(const struct lock_context *ctx1,
|
---|
107 | const struct lock_context *ctx2)
|
---|
108 | {
|
---|
109 | return (serverid_equal(&ctx1->pid, &ctx2->pid) &&
|
---|
110 | (ctx1->smblctx == ctx2->smblctx) &&
|
---|
111 | (ctx1->tid == ctx2->tid));
|
---|
112 | }
|
---|
113 |
|
---|
114 | /****************************************************************************
|
---|
115 | See if lck1 and lck2 overlap.
|
---|
116 | ****************************************************************************/
|
---|
117 |
|
---|
118 | static bool brl_overlap(const struct lock_struct *lck1,
|
---|
119 | const struct lock_struct *lck2)
|
---|
120 | {
|
---|
121 | /* XXX Remove for Win7 compatibility. */
|
---|
122 | /* this extra check is not redundant - it copes with locks
|
---|
123 | that go beyond the end of 64 bit file space */
|
---|
124 | if (lck1->size != 0 &&
|
---|
125 | lck1->start == lck2->start &&
|
---|
126 | lck1->size == lck2->size) {
|
---|
127 | return True;
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (lck1->start >= (lck2->start+lck2->size) ||
|
---|
131 | lck2->start >= (lck1->start+lck1->size)) {
|
---|
132 | return False;
|
---|
133 | }
|
---|
134 | return True;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /****************************************************************************
|
---|
138 | See if lock2 can be added when lock1 is in place.
|
---|
139 | ****************************************************************************/
|
---|
140 |
|
---|
141 | static bool brl_conflict(const struct lock_struct *lck1,
|
---|
142 | const struct lock_struct *lck2)
|
---|
143 | {
|
---|
144 | /* Ignore PENDING locks. */
|
---|
145 | if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
|
---|
146 | return False;
|
---|
147 |
|
---|
148 | /* Read locks never conflict. */
|
---|
149 | if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
|
---|
150 | return False;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /* A READ lock can stack on top of a WRITE lock if they have the same
|
---|
154 | * context & fnum. */
|
---|
155 | if (lck1->lock_type == WRITE_LOCK && lck2->lock_type == READ_LOCK &&
|
---|
156 | brl_same_context(&lck1->context, &lck2->context) &&
|
---|
157 | lck1->fnum == lck2->fnum) {
|
---|
158 | return False;
|
---|
159 | }
|
---|
160 |
|
---|
161 | return brl_overlap(lck1, lck2);
|
---|
162 | }
|
---|
163 |
|
---|
164 | /****************************************************************************
|
---|
165 | See if lock2 can be added when lock1 is in place - when both locks are POSIX
|
---|
166 | flavour. POSIX locks ignore fnum - they only care about dev/ino which we
|
---|
167 | know already match.
|
---|
168 | ****************************************************************************/
|
---|
169 |
|
---|
170 | static bool brl_conflict_posix(const struct lock_struct *lck1,
|
---|
171 | const struct lock_struct *lck2)
|
---|
172 | {
|
---|
173 | #if defined(DEVELOPER)
|
---|
174 | SMB_ASSERT(lck1->lock_flav == POSIX_LOCK);
|
---|
175 | SMB_ASSERT(lck2->lock_flav == POSIX_LOCK);
|
---|
176 | #endif
|
---|
177 |
|
---|
178 | /* Ignore PENDING locks. */
|
---|
179 | if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
|
---|
180 | return False;
|
---|
181 |
|
---|
182 | /* Read locks never conflict. */
|
---|
183 | if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
|
---|
184 | return False;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /* Locks on the same context don't conflict. Ignore fnum. */
|
---|
188 | if (brl_same_context(&lck1->context, &lck2->context)) {
|
---|
189 | return False;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /* One is read, the other write, or the context is different,
|
---|
193 | do they overlap ? */
|
---|
194 | return brl_overlap(lck1, lck2);
|
---|
195 | }
|
---|
196 |
|
---|
197 | #if ZERO_ZERO
|
---|
198 | static bool brl_conflict1(const struct lock_struct *lck1,
|
---|
199 | const struct lock_struct *lck2)
|
---|
200 | {
|
---|
201 | if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
|
---|
202 | return False;
|
---|
203 |
|
---|
204 | if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
|
---|
205 | return False;
|
---|
206 | }
|
---|
207 |
|
---|
208 | if (brl_same_context(&lck1->context, &lck2->context) &&
|
---|
209 | lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
|
---|
210 | return False;
|
---|
211 | }
|
---|
212 |
|
---|
213 | if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
|
---|
214 | return True;
|
---|
215 | }
|
---|
216 |
|
---|
217 | if (lck1->start >= (lck2->start + lck2->size) ||
|
---|
218 | lck2->start >= (lck1->start + lck1->size)) {
|
---|
219 | return False;
|
---|
220 | }
|
---|
221 |
|
---|
222 | return True;
|
---|
223 | }
|
---|
224 | #endif
|
---|
225 |
|
---|
226 | /****************************************************************************
|
---|
227 | Check to see if this lock conflicts, but ignore our own locks on the
|
---|
228 | same fnum only. This is the read/write lock check code path.
|
---|
229 | This is never used in the POSIX lock case.
|
---|
230 | ****************************************************************************/
|
---|
231 |
|
---|
232 | static bool brl_conflict_other(const struct lock_struct *lock,
|
---|
233 | const struct lock_struct *rw_probe)
|
---|
234 | {
|
---|
235 | if (IS_PENDING_LOCK(lock->lock_type) ||
|
---|
236 | IS_PENDING_LOCK(rw_probe->lock_type)) {
|
---|
237 | return False;
|
---|
238 | }
|
---|
239 |
|
---|
240 | if (lock->lock_type == READ_LOCK && rw_probe->lock_type == READ_LOCK) {
|
---|
241 | return False;
|
---|
242 | }
|
---|
243 |
|
---|
244 | if (lock->lock_flav == POSIX_LOCK &&
|
---|
245 | rw_probe->lock_flav == POSIX_LOCK) {
|
---|
246 | /*
|
---|
247 | * POSIX flavour locks never conflict here - this is only called
|
---|
248 | * in the read/write path.
|
---|
249 | */
|
---|
250 | return False;
|
---|
251 | }
|
---|
252 |
|
---|
253 | if (!brl_overlap(lock, rw_probe)) {
|
---|
254 | /*
|
---|
255 | * I/O can only conflict when overlapping a lock, thus let it
|
---|
256 | * pass
|
---|
257 | */
|
---|
258 | return false;
|
---|
259 | }
|
---|
260 |
|
---|
261 | if (!brl_same_context(&lock->context, &rw_probe->context)) {
|
---|
262 | /*
|
---|
263 | * Different process, conflict
|
---|
264 | */
|
---|
265 | return true;
|
---|
266 | }
|
---|
267 |
|
---|
268 | if (lock->fnum != rw_probe->fnum) {
|
---|
269 | /*
|
---|
270 | * Different file handle, conflict
|
---|
271 | */
|
---|
272 | return true;
|
---|
273 | }
|
---|
274 |
|
---|
275 | if ((lock->lock_type == READ_LOCK) &&
|
---|
276 | (rw_probe->lock_type == WRITE_LOCK)) {
|
---|
277 | /*
|
---|
278 | * Incoming WRITE locks conflict with existing READ locks even
|
---|
279 | * if the context is the same. JRA. See LOCKTEST7 in
|
---|
280 | * smbtorture.
|
---|
281 | */
|
---|
282 | return true;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * I/O request compatible with existing lock, let it pass without
|
---|
287 | * conflict
|
---|
288 | */
|
---|
289 |
|
---|
290 | return false;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /****************************************************************************
|
---|
294 | Check if an unlock overlaps a pending lock.
|
---|
295 | ****************************************************************************/
|
---|
296 |
|
---|
297 | static bool brl_pending_overlap(const struct lock_struct *lock, const struct lock_struct *pend_lock)
|
---|
298 | {
|
---|
299 | if ((lock->start <= pend_lock->start) && (lock->start + lock->size > pend_lock->start))
|
---|
300 | return True;
|
---|
301 | if ((lock->start >= pend_lock->start) && (lock->start < pend_lock->start + pend_lock->size))
|
---|
302 | return True;
|
---|
303 | return False;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /****************************************************************************
|
---|
307 | Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
|
---|
308 | is the same as this one and changes its error code. I wonder if any
|
---|
309 | app depends on this ?
|
---|
310 | ****************************************************************************/
|
---|
311 |
|
---|
312 | static NTSTATUS brl_lock_failed(files_struct *fsp,
|
---|
313 | const struct lock_struct *lock,
|
---|
314 | bool blocking_lock)
|
---|
315 | {
|
---|
316 | if (lock->start >= 0xEF000000 && (lock->start >> 63) == 0) {
|
---|
317 | /* amazing the little things you learn with a test
|
---|
318 | suite. Locks beyond this offset (as a 64 bit
|
---|
319 | number!) always generate the conflict error code,
|
---|
320 | unless the top bit is set */
|
---|
321 | if (!blocking_lock) {
|
---|
322 | fsp->last_lock_failure = *lock;
|
---|
323 | }
|
---|
324 | return NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
325 | }
|
---|
326 |
|
---|
327 | if (serverid_equal(&lock->context.pid, &fsp->last_lock_failure.context.pid) &&
|
---|
328 | lock->context.tid == fsp->last_lock_failure.context.tid &&
|
---|
329 | lock->fnum == fsp->last_lock_failure.fnum &&
|
---|
330 | lock->start == fsp->last_lock_failure.start) {
|
---|
331 | return NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
332 | }
|
---|
333 |
|
---|
334 | if (!blocking_lock) {
|
---|
335 | fsp->last_lock_failure = *lock;
|
---|
336 | }
|
---|
337 | return NT_STATUS_LOCK_NOT_GRANTED;
|
---|
338 | }
|
---|
339 |
|
---|
340 | /****************************************************************************
|
---|
341 | Open up the brlock.tdb database.
|
---|
342 | ****************************************************************************/
|
---|
343 |
|
---|
344 | void brl_init(bool read_only)
|
---|
345 | {
|
---|
346 | int tdb_flags;
|
---|
347 | char *db_path;
|
---|
348 |
|
---|
349 | if (brlock_db) {
|
---|
350 | return;
|
---|
351 | }
|
---|
352 |
|
---|
353 | tdb_flags = TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
|
---|
354 |
|
---|
355 | if (!lp_clustering()) {
|
---|
356 | /*
|
---|
357 | * We can't use the SEQNUM trick to cache brlock
|
---|
358 | * entries in the clustering case because ctdb seqnum
|
---|
359 | * propagation has a delay.
|
---|
360 | */
|
---|
361 | tdb_flags |= TDB_SEQNUM;
|
---|
362 | }
|
---|
363 |
|
---|
364 | db_path = lock_path("brlock.tdb");
|
---|
365 | if (db_path == NULL) {
|
---|
366 | DEBUG(0, ("out of memory!\n"));
|
---|
367 | return;
|
---|
368 | }
|
---|
369 |
|
---|
370 | brlock_db = db_open(NULL, db_path,
|
---|
371 | SMB_OPEN_DATABASE_TDB_HASH_SIZE, tdb_flags,
|
---|
372 | read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644,
|
---|
373 | DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
|
---|
374 | if (!brlock_db) {
|
---|
375 | DEBUG(0,("Failed to open byte range locking database %s\n",
|
---|
376 | db_path));
|
---|
377 | TALLOC_FREE(db_path);
|
---|
378 | return;
|
---|
379 | }
|
---|
380 | TALLOC_FREE(db_path);
|
---|
381 | }
|
---|
382 |
|
---|
383 | /****************************************************************************
|
---|
384 | Close down the brlock.tdb database.
|
---|
385 | ****************************************************************************/
|
---|
386 |
|
---|
387 | void brl_shutdown(void)
|
---|
388 | {
|
---|
389 | TALLOC_FREE(brlock_db);
|
---|
390 | }
|
---|
391 |
|
---|
392 | #if ZERO_ZERO
|
---|
393 | /****************************************************************************
|
---|
394 | Compare two locks for sorting.
|
---|
395 | ****************************************************************************/
|
---|
396 |
|
---|
397 | static int lock_compare(const struct lock_struct *lck1,
|
---|
398 | const struct lock_struct *lck2)
|
---|
399 | {
|
---|
400 | if (lck1->start != lck2->start) {
|
---|
401 | return (lck1->start - lck2->start);
|
---|
402 | }
|
---|
403 | if (lck2->size != lck1->size) {
|
---|
404 | return ((int)lck1->size - (int)lck2->size);
|
---|
405 | }
|
---|
406 | return 0;
|
---|
407 | }
|
---|
408 | #endif
|
---|
409 |
|
---|
410 | /****************************************************************************
|
---|
411 | Lock a range of bytes - Windows lock semantics.
|
---|
412 | ****************************************************************************/
|
---|
413 |
|
---|
414 | NTSTATUS brl_lock_windows_default(struct byte_range_lock *br_lck,
|
---|
415 | struct lock_struct *plock, bool blocking_lock)
|
---|
416 | {
|
---|
417 | unsigned int i;
|
---|
418 | files_struct *fsp = br_lck->fsp;
|
---|
419 | struct lock_struct *locks = br_lck->lock_data;
|
---|
420 | NTSTATUS status;
|
---|
421 |
|
---|
422 | SMB_ASSERT(plock->lock_type != UNLOCK_LOCK);
|
---|
423 |
|
---|
424 | if ((plock->start + plock->size - 1 < plock->start) &&
|
---|
425 | plock->size != 0) {
|
---|
426 | return NT_STATUS_INVALID_LOCK_RANGE;
|
---|
427 | }
|
---|
428 |
|
---|
429 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
430 | /* Do any Windows or POSIX locks conflict ? */
|
---|
431 | if (brl_conflict(&locks[i], plock)) {
|
---|
432 | if (!serverid_exists(&locks[i].context.pid)) {
|
---|
433 | locks[i].context.pid.pid = 0;
|
---|
434 | br_lck->modified = true;
|
---|
435 | continue;
|
---|
436 | }
|
---|
437 | /* Remember who blocked us. */
|
---|
438 | plock->context.smblctx = locks[i].context.smblctx;
|
---|
439 | return brl_lock_failed(fsp,plock,blocking_lock);
|
---|
440 | }
|
---|
441 | #if ZERO_ZERO
|
---|
442 | if (plock->start == 0 && plock->size == 0 &&
|
---|
443 | locks[i].size == 0) {
|
---|
444 | break;
|
---|
445 | }
|
---|
446 | #endif
|
---|
447 | }
|
---|
448 |
|
---|
449 | if (!IS_PENDING_LOCK(plock->lock_type)) {
|
---|
450 | contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
|
---|
451 | }
|
---|
452 |
|
---|
453 | /* We can get the Windows lock, now see if it needs to
|
---|
454 | be mapped into a lower level POSIX one, and if so can
|
---|
455 | we get it ? */
|
---|
456 |
|
---|
457 | if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(fsp->conn->params)) {
|
---|
458 | int errno_ret;
|
---|
459 | if (!set_posix_lock_windows_flavour(fsp,
|
---|
460 | plock->start,
|
---|
461 | plock->size,
|
---|
462 | plock->lock_type,
|
---|
463 | &plock->context,
|
---|
464 | locks,
|
---|
465 | br_lck->num_locks,
|
---|
466 | &errno_ret)) {
|
---|
467 |
|
---|
468 | /* We don't know who blocked us. */
|
---|
469 | plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
|
---|
470 |
|
---|
471 | if (errno_ret == EACCES || errno_ret == EAGAIN) {
|
---|
472 | status = NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
473 | goto fail;
|
---|
474 | } else {
|
---|
475 | status = map_nt_error_from_unix(errno);
|
---|
476 | goto fail;
|
---|
477 | }
|
---|
478 | }
|
---|
479 | }
|
---|
480 |
|
---|
481 | /* no conflicts - add it to the list of locks */
|
---|
482 | locks = talloc_realloc(br_lck, locks, struct lock_struct,
|
---|
483 | (br_lck->num_locks + 1));
|
---|
484 | if (!locks) {
|
---|
485 | status = NT_STATUS_NO_MEMORY;
|
---|
486 | goto fail;
|
---|
487 | }
|
---|
488 |
|
---|
489 | memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
|
---|
490 | br_lck->num_locks += 1;
|
---|
491 | br_lck->lock_data = locks;
|
---|
492 | br_lck->modified = True;
|
---|
493 |
|
---|
494 | return NT_STATUS_OK;
|
---|
495 | fail:
|
---|
496 | if (!IS_PENDING_LOCK(plock->lock_type)) {
|
---|
497 | contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
|
---|
498 | }
|
---|
499 | return status;
|
---|
500 | }
|
---|
501 |
|
---|
502 | /****************************************************************************
|
---|
503 | Cope with POSIX range splits and merges.
|
---|
504 | ****************************************************************************/
|
---|
505 |
|
---|
506 | static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr, /* Output array. */
|
---|
507 | struct lock_struct *ex, /* existing lock. */
|
---|
508 | struct lock_struct *plock) /* proposed lock. */
|
---|
509 | {
|
---|
510 | bool lock_types_differ = (ex->lock_type != plock->lock_type);
|
---|
511 |
|
---|
512 | /* We can't merge non-conflicting locks on different context - ignore fnum. */
|
---|
513 |
|
---|
514 | if (!brl_same_context(&ex->context, &plock->context)) {
|
---|
515 | /* Just copy. */
|
---|
516 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
517 | return 1;
|
---|
518 | }
|
---|
519 |
|
---|
520 | /* We now know we have the same context. */
|
---|
521 |
|
---|
522 | /* Did we overlap ? */
|
---|
523 |
|
---|
524 | /*********************************************
|
---|
525 | +---------+
|
---|
526 | | ex |
|
---|
527 | +---------+
|
---|
528 | +-------+
|
---|
529 | | plock |
|
---|
530 | +-------+
|
---|
531 | OR....
|
---|
532 | +---------+
|
---|
533 | | ex |
|
---|
534 | +---------+
|
---|
535 | **********************************************/
|
---|
536 |
|
---|
537 | if ( (ex->start > (plock->start + plock->size)) ||
|
---|
538 | (plock->start > (ex->start + ex->size))) {
|
---|
539 |
|
---|
540 | /* No overlap with this lock - copy existing. */
|
---|
541 |
|
---|
542 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
543 | return 1;
|
---|
544 | }
|
---|
545 |
|
---|
546 | /*********************************************
|
---|
547 | +---------------------------+
|
---|
548 | | ex |
|
---|
549 | +---------------------------+
|
---|
550 | +---------------------------+
|
---|
551 | | plock | -> replace with plock.
|
---|
552 | +---------------------------+
|
---|
553 | OR
|
---|
554 | +---------------+
|
---|
555 | | ex |
|
---|
556 | +---------------+
|
---|
557 | +---------------------------+
|
---|
558 | | plock | -> replace with plock.
|
---|
559 | +---------------------------+
|
---|
560 |
|
---|
561 | **********************************************/
|
---|
562 |
|
---|
563 | if ( (ex->start >= plock->start) &&
|
---|
564 | (ex->start + ex->size <= plock->start + plock->size) ) {
|
---|
565 |
|
---|
566 | /* Replace - discard existing lock. */
|
---|
567 |
|
---|
568 | return 0;
|
---|
569 | }
|
---|
570 |
|
---|
571 | /*********************************************
|
---|
572 | Adjacent after.
|
---|
573 | +-------+
|
---|
574 | | ex |
|
---|
575 | +-------+
|
---|
576 | +---------------+
|
---|
577 | | plock |
|
---|
578 | +---------------+
|
---|
579 |
|
---|
580 | BECOMES....
|
---|
581 | +---------------+-------+
|
---|
582 | | plock | ex | - different lock types.
|
---|
583 | +---------------+-------+
|
---|
584 | OR.... (merge)
|
---|
585 | +-----------------------+
|
---|
586 | | plock | - same lock type.
|
---|
587 | +-----------------------+
|
---|
588 | **********************************************/
|
---|
589 |
|
---|
590 | if (plock->start + plock->size == ex->start) {
|
---|
591 |
|
---|
592 | /* If the lock types are the same, we merge, if different, we
|
---|
593 | add the remainder of the old lock. */
|
---|
594 |
|
---|
595 | if (lock_types_differ) {
|
---|
596 | /* Add existing. */
|
---|
597 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
598 | return 1;
|
---|
599 | } else {
|
---|
600 | /* Merge - adjust incoming lock as we may have more
|
---|
601 | * merging to come. */
|
---|
602 | plock->size += ex->size;
|
---|
603 | return 0;
|
---|
604 | }
|
---|
605 | }
|
---|
606 |
|
---|
607 | /*********************************************
|
---|
608 | Adjacent before.
|
---|
609 | +-------+
|
---|
610 | | ex |
|
---|
611 | +-------+
|
---|
612 | +---------------+
|
---|
613 | | plock |
|
---|
614 | +---------------+
|
---|
615 | BECOMES....
|
---|
616 | +-------+---------------+
|
---|
617 | | ex | plock | - different lock types
|
---|
618 | +-------+---------------+
|
---|
619 |
|
---|
620 | OR.... (merge)
|
---|
621 | +-----------------------+
|
---|
622 | | plock | - same lock type.
|
---|
623 | +-----------------------+
|
---|
624 |
|
---|
625 | **********************************************/
|
---|
626 |
|
---|
627 | if (ex->start + ex->size == plock->start) {
|
---|
628 |
|
---|
629 | /* If the lock types are the same, we merge, if different, we
|
---|
630 | add the existing lock. */
|
---|
631 |
|
---|
632 | if (lock_types_differ) {
|
---|
633 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
634 | return 1;
|
---|
635 | } else {
|
---|
636 | /* Merge - adjust incoming lock as we may have more
|
---|
637 | * merging to come. */
|
---|
638 | plock->start = ex->start;
|
---|
639 | plock->size += ex->size;
|
---|
640 | return 0;
|
---|
641 | }
|
---|
642 | }
|
---|
643 |
|
---|
644 | /*********************************************
|
---|
645 | Overlap after.
|
---|
646 | +-----------------------+
|
---|
647 | | ex |
|
---|
648 | +-----------------------+
|
---|
649 | +---------------+
|
---|
650 | | plock |
|
---|
651 | +---------------+
|
---|
652 | OR
|
---|
653 | +----------------+
|
---|
654 | | ex |
|
---|
655 | +----------------+
|
---|
656 | +---------------+
|
---|
657 | | plock |
|
---|
658 | +---------------+
|
---|
659 |
|
---|
660 | BECOMES....
|
---|
661 | +---------------+-------+
|
---|
662 | | plock | ex | - different lock types.
|
---|
663 | +---------------+-------+
|
---|
664 | OR.... (merge)
|
---|
665 | +-----------------------+
|
---|
666 | | plock | - same lock type.
|
---|
667 | +-----------------------+
|
---|
668 | **********************************************/
|
---|
669 |
|
---|
670 | if ( (ex->start >= plock->start) &&
|
---|
671 | (ex->start <= plock->start + plock->size) &&
|
---|
672 | (ex->start + ex->size > plock->start + plock->size) ) {
|
---|
673 |
|
---|
674 | /* If the lock types are the same, we merge, if different, we
|
---|
675 | add the remainder of the old lock. */
|
---|
676 |
|
---|
677 | if (lock_types_differ) {
|
---|
678 | /* Add remaining existing. */
|
---|
679 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
680 | /* Adjust existing start and size. */
|
---|
681 | lck_arr[0].start = plock->start + plock->size;
|
---|
682 | lck_arr[0].size = (ex->start + ex->size) - (plock->start + plock->size);
|
---|
683 | return 1;
|
---|
684 | } else {
|
---|
685 | /* Merge - adjust incoming lock as we may have more
|
---|
686 | * merging to come. */
|
---|
687 | plock->size += (ex->start + ex->size) - (plock->start + plock->size);
|
---|
688 | return 0;
|
---|
689 | }
|
---|
690 | }
|
---|
691 |
|
---|
692 | /*********************************************
|
---|
693 | Overlap before.
|
---|
694 | +-----------------------+
|
---|
695 | | ex |
|
---|
696 | +-----------------------+
|
---|
697 | +---------------+
|
---|
698 | | plock |
|
---|
699 | +---------------+
|
---|
700 | OR
|
---|
701 | +-------------+
|
---|
702 | | ex |
|
---|
703 | +-------------+
|
---|
704 | +---------------+
|
---|
705 | | plock |
|
---|
706 | +---------------+
|
---|
707 |
|
---|
708 | BECOMES....
|
---|
709 | +-------+---------------+
|
---|
710 | | ex | plock | - different lock types
|
---|
711 | +-------+---------------+
|
---|
712 |
|
---|
713 | OR.... (merge)
|
---|
714 | +-----------------------+
|
---|
715 | | plock | - same lock type.
|
---|
716 | +-----------------------+
|
---|
717 |
|
---|
718 | **********************************************/
|
---|
719 |
|
---|
720 | if ( (ex->start < plock->start) &&
|
---|
721 | (ex->start + ex->size >= plock->start) &&
|
---|
722 | (ex->start + ex->size <= plock->start + plock->size) ) {
|
---|
723 |
|
---|
724 | /* If the lock types are the same, we merge, if different, we
|
---|
725 | add the truncated old lock. */
|
---|
726 |
|
---|
727 | if (lock_types_differ) {
|
---|
728 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
729 | /* Adjust existing size. */
|
---|
730 | lck_arr[0].size = plock->start - ex->start;
|
---|
731 | return 1;
|
---|
732 | } else {
|
---|
733 | /* Merge - adjust incoming lock as we may have more
|
---|
734 | * merging to come. MUST ADJUST plock SIZE FIRST ! */
|
---|
735 | plock->size += (plock->start - ex->start);
|
---|
736 | plock->start = ex->start;
|
---|
737 | return 0;
|
---|
738 | }
|
---|
739 | }
|
---|
740 |
|
---|
741 | /*********************************************
|
---|
742 | Complete overlap.
|
---|
743 | +---------------------------+
|
---|
744 | | ex |
|
---|
745 | +---------------------------+
|
---|
746 | +---------+
|
---|
747 | | plock |
|
---|
748 | +---------+
|
---|
749 | BECOMES.....
|
---|
750 | +-------+---------+---------+
|
---|
751 | | ex | plock | ex | - different lock types.
|
---|
752 | +-------+---------+---------+
|
---|
753 | OR
|
---|
754 | +---------------------------+
|
---|
755 | | plock | - same lock type.
|
---|
756 | +---------------------------+
|
---|
757 | **********************************************/
|
---|
758 |
|
---|
759 | if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
|
---|
760 |
|
---|
761 | if (lock_types_differ) {
|
---|
762 |
|
---|
763 | /* We have to split ex into two locks here. */
|
---|
764 |
|
---|
765 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
766 | memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
|
---|
767 |
|
---|
768 | /* Adjust first existing size. */
|
---|
769 | lck_arr[0].size = plock->start - ex->start;
|
---|
770 |
|
---|
771 | /* Adjust second existing start and size. */
|
---|
772 | lck_arr[1].start = plock->start + plock->size;
|
---|
773 | lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
|
---|
774 | return 2;
|
---|
775 | } else {
|
---|
776 | /* Just eat the existing locks, merge them into plock. */
|
---|
777 | plock->start = ex->start;
|
---|
778 | plock->size = ex->size;
|
---|
779 | return 0;
|
---|
780 | }
|
---|
781 | }
|
---|
782 |
|
---|
783 | /* Never get here. */
|
---|
784 | smb_panic("brlock_posix_split_merge");
|
---|
785 | /* Notreached. */
|
---|
786 |
|
---|
787 | /* Keep some compilers happy. */
|
---|
788 | return 0;
|
---|
789 | }
|
---|
790 |
|
---|
791 | /****************************************************************************
|
---|
792 | Lock a range of bytes - POSIX lock semantics.
|
---|
793 | We must cope with range splits and merges.
|
---|
794 | ****************************************************************************/
|
---|
795 |
|
---|
796 | static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
|
---|
797 | struct byte_range_lock *br_lck,
|
---|
798 | struct lock_struct *plock)
|
---|
799 | {
|
---|
800 | unsigned int i, count, posix_count;
|
---|
801 | struct lock_struct *locks = br_lck->lock_data;
|
---|
802 | struct lock_struct *tp;
|
---|
803 | bool signal_pending_read = False;
|
---|
804 | bool break_oplocks = false;
|
---|
805 | NTSTATUS status;
|
---|
806 |
|
---|
807 | /* No zero-zero locks for POSIX. */
|
---|
808 | if (plock->start == 0 && plock->size == 0) {
|
---|
809 | return NT_STATUS_INVALID_PARAMETER;
|
---|
810 | }
|
---|
811 |
|
---|
812 | /* Don't allow 64-bit lock wrap. */
|
---|
813 | if (plock->start + plock->size - 1 < plock->start) {
|
---|
814 | return NT_STATUS_INVALID_PARAMETER;
|
---|
815 | }
|
---|
816 |
|
---|
817 | /* The worst case scenario here is we have to split an
|
---|
818 | existing POSIX lock range into two, and add our lock,
|
---|
819 | so we need at most 2 more entries. */
|
---|
820 |
|
---|
821 | tp = talloc_array(br_lck, struct lock_struct, br_lck->num_locks + 2);
|
---|
822 | if (!tp) {
|
---|
823 | return NT_STATUS_NO_MEMORY;
|
---|
824 | }
|
---|
825 |
|
---|
826 | count = posix_count = 0;
|
---|
827 |
|
---|
828 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
829 | struct lock_struct *curr_lock = &locks[i];
|
---|
830 |
|
---|
831 | /* If we have a pending read lock, a lock downgrade should
|
---|
832 | trigger a lock re-evaluation. */
|
---|
833 | if (curr_lock->lock_type == PENDING_READ_LOCK &&
|
---|
834 | brl_pending_overlap(plock, curr_lock)) {
|
---|
835 | signal_pending_read = True;
|
---|
836 | }
|
---|
837 |
|
---|
838 | if (curr_lock->lock_flav == WINDOWS_LOCK) {
|
---|
839 | /* Do any Windows flavour locks conflict ? */
|
---|
840 | if (brl_conflict(curr_lock, plock)) {
|
---|
841 | if (!serverid_exists(&curr_lock->context.pid)) {
|
---|
842 | curr_lock->context.pid.pid = 0;
|
---|
843 | br_lck->modified = true;
|
---|
844 | continue;
|
---|
845 | }
|
---|
846 | /* No games with error messages. */
|
---|
847 | TALLOC_FREE(tp);
|
---|
848 | /* Remember who blocked us. */
|
---|
849 | plock->context.smblctx = curr_lock->context.smblctx;
|
---|
850 | return NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
851 | }
|
---|
852 | /* Just copy the Windows lock into the new array. */
|
---|
853 | memcpy(&tp[count], curr_lock, sizeof(struct lock_struct));
|
---|
854 | count++;
|
---|
855 | } else {
|
---|
856 | unsigned int tmp_count = 0;
|
---|
857 |
|
---|
858 | /* POSIX conflict semantics are different. */
|
---|
859 | if (brl_conflict_posix(curr_lock, plock)) {
|
---|
860 | if (!serverid_exists(&curr_lock->context.pid)) {
|
---|
861 | curr_lock->context.pid.pid = 0;
|
---|
862 | br_lck->modified = true;
|
---|
863 | continue;
|
---|
864 | }
|
---|
865 | /* Can't block ourselves with POSIX locks. */
|
---|
866 | /* No games with error messages. */
|
---|
867 | TALLOC_FREE(tp);
|
---|
868 | /* Remember who blocked us. */
|
---|
869 | plock->context.smblctx = curr_lock->context.smblctx;
|
---|
870 | return NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
871 | }
|
---|
872 |
|
---|
873 | /* Work out overlaps. */
|
---|
874 | tmp_count += brlock_posix_split_merge(&tp[count], curr_lock, plock);
|
---|
875 | posix_count += tmp_count;
|
---|
876 | count += tmp_count;
|
---|
877 | }
|
---|
878 | }
|
---|
879 |
|
---|
880 | /*
|
---|
881 | * Break oplocks while we hold a brl. Since lock() and unlock() calls
|
---|
882 | * are not symetric with POSIX semantics, we cannot guarantee our
|
---|
883 | * contend_level2_oplocks_begin/end calls will be acquired and
|
---|
884 | * released one-for-one as with Windows semantics. Therefore we only
|
---|
885 | * call contend_level2_oplocks_begin if this is the first POSIX brl on
|
---|
886 | * the file.
|
---|
887 | */
|
---|
888 | break_oplocks = (!IS_PENDING_LOCK(plock->lock_type) &&
|
---|
889 | posix_count == 0);
|
---|
890 | if (break_oplocks) {
|
---|
891 | contend_level2_oplocks_begin(br_lck->fsp,
|
---|
892 | LEVEL2_CONTEND_POSIX_BRL);
|
---|
893 | }
|
---|
894 |
|
---|
895 | /* Try and add the lock in order, sorted by lock start. */
|
---|
896 | for (i=0; i < count; i++) {
|
---|
897 | struct lock_struct *curr_lock = &tp[i];
|
---|
898 |
|
---|
899 | if (curr_lock->start <= plock->start) {
|
---|
900 | continue;
|
---|
901 | }
|
---|
902 | }
|
---|
903 |
|
---|
904 | if (i < count) {
|
---|
905 | memmove(&tp[i+1], &tp[i],
|
---|
906 | (count - i)*sizeof(struct lock_struct));
|
---|
907 | }
|
---|
908 | memcpy(&tp[i], plock, sizeof(struct lock_struct));
|
---|
909 | count++;
|
---|
910 |
|
---|
911 | /* We can get the POSIX lock, now see if it needs to
|
---|
912 | be mapped into a lower level POSIX one, and if so can
|
---|
913 | we get it ? */
|
---|
914 |
|
---|
915 | if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(br_lck->fsp->conn->params)) {
|
---|
916 | int errno_ret;
|
---|
917 |
|
---|
918 | /* The lower layer just needs to attempt to
|
---|
919 | get the system POSIX lock. We've weeded out
|
---|
920 | any conflicts above. */
|
---|
921 |
|
---|
922 | if (!set_posix_lock_posix_flavour(br_lck->fsp,
|
---|
923 | plock->start,
|
---|
924 | plock->size,
|
---|
925 | plock->lock_type,
|
---|
926 | &errno_ret)) {
|
---|
927 |
|
---|
928 | /* We don't know who blocked us. */
|
---|
929 | plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
|
---|
930 |
|
---|
931 | if (errno_ret == EACCES || errno_ret == EAGAIN) {
|
---|
932 | TALLOC_FREE(tp);
|
---|
933 | status = NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
934 | goto fail;
|
---|
935 | } else {
|
---|
936 | TALLOC_FREE(tp);
|
---|
937 | status = map_nt_error_from_unix(errno);
|
---|
938 | goto fail;
|
---|
939 | }
|
---|
940 | }
|
---|
941 | }
|
---|
942 |
|
---|
943 | /* If we didn't use all the allocated size,
|
---|
944 | * Realloc so we don't leak entries per lock call. */
|
---|
945 | if (count < br_lck->num_locks + 2) {
|
---|
946 | tp = talloc_realloc(br_lck, tp, struct lock_struct, count);
|
---|
947 | if (!tp) {
|
---|
948 | status = NT_STATUS_NO_MEMORY;
|
---|
949 | goto fail;
|
---|
950 | }
|
---|
951 | }
|
---|
952 |
|
---|
953 | br_lck->num_locks = count;
|
---|
954 | TALLOC_FREE(br_lck->lock_data);
|
---|
955 | br_lck->lock_data = tp;
|
---|
956 | locks = tp;
|
---|
957 | br_lck->modified = True;
|
---|
958 |
|
---|
959 | /* A successful downgrade from write to read lock can trigger a lock
|
---|
960 | re-evalutation where waiting readers can now proceed. */
|
---|
961 |
|
---|
962 | if (signal_pending_read) {
|
---|
963 | /* Send unlock messages to any pending read waiters that overlap. */
|
---|
964 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
965 | struct lock_struct *pend_lock = &locks[i];
|
---|
966 |
|
---|
967 | /* Ignore non-pending locks. */
|
---|
968 | if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
|
---|
969 | continue;
|
---|
970 | }
|
---|
971 |
|
---|
972 | if (pend_lock->lock_type == PENDING_READ_LOCK &&
|
---|
973 | brl_pending_overlap(plock, pend_lock)) {
|
---|
974 | struct server_id_buf tmp;
|
---|
975 |
|
---|
976 | DEBUG(10, ("brl_lock_posix: sending unlock "
|
---|
977 | "message to pid %s\n",
|
---|
978 | server_id_str_buf(pend_lock->context.pid,
|
---|
979 | &tmp)));
|
---|
980 |
|
---|
981 | messaging_send(msg_ctx, pend_lock->context.pid,
|
---|
982 | MSG_SMB_UNLOCK, &data_blob_null);
|
---|
983 | }
|
---|
984 | }
|
---|
985 | }
|
---|
986 |
|
---|
987 | return NT_STATUS_OK;
|
---|
988 | fail:
|
---|
989 | if (break_oplocks) {
|
---|
990 | contend_level2_oplocks_end(br_lck->fsp,
|
---|
991 | LEVEL2_CONTEND_POSIX_BRL);
|
---|
992 | }
|
---|
993 | return status;
|
---|
994 | }
|
---|
995 |
|
---|
996 | NTSTATUS smb_vfs_call_brl_lock_windows(struct vfs_handle_struct *handle,
|
---|
997 | struct byte_range_lock *br_lck,
|
---|
998 | struct lock_struct *plock,
|
---|
999 | bool blocking_lock)
|
---|
1000 | {
|
---|
1001 | VFS_FIND(brl_lock_windows);
|
---|
1002 | return handle->fns->brl_lock_windows_fn(handle, br_lck, plock,
|
---|
1003 | blocking_lock);
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | /****************************************************************************
|
---|
1007 | Lock a range of bytes.
|
---|
1008 | ****************************************************************************/
|
---|
1009 |
|
---|
1010 | NTSTATUS brl_lock(struct messaging_context *msg_ctx,
|
---|
1011 | struct byte_range_lock *br_lck,
|
---|
1012 | uint64_t smblctx,
|
---|
1013 | struct server_id pid,
|
---|
1014 | br_off start,
|
---|
1015 | br_off size,
|
---|
1016 | enum brl_type lock_type,
|
---|
1017 | enum brl_flavour lock_flav,
|
---|
1018 | bool blocking_lock,
|
---|
1019 | uint64_t *psmblctx)
|
---|
1020 | {
|
---|
1021 | NTSTATUS ret;
|
---|
1022 | struct lock_struct lock;
|
---|
1023 |
|
---|
1024 | ZERO_STRUCT(lock);
|
---|
1025 |
|
---|
1026 | #if !ZERO_ZERO
|
---|
1027 | if (start == 0 && size == 0) {
|
---|
1028 | DEBUG(0,("client sent 0/0 lock - please report this\n"));
|
---|
1029 | }
|
---|
1030 | #endif
|
---|
1031 |
|
---|
1032 | lock = (struct lock_struct) {
|
---|
1033 | .context.smblctx = smblctx,
|
---|
1034 | .context.pid = pid,
|
---|
1035 | .context.tid = br_lck->fsp->conn->cnum,
|
---|
1036 | .start = start,
|
---|
1037 | .size = size,
|
---|
1038 | .fnum = br_lck->fsp->fnum,
|
---|
1039 | .lock_type = lock_type,
|
---|
1040 | .lock_flav = lock_flav
|
---|
1041 | };
|
---|
1042 |
|
---|
1043 | if (lock_flav == WINDOWS_LOCK) {
|
---|
1044 | ret = SMB_VFS_BRL_LOCK_WINDOWS(br_lck->fsp->conn, br_lck,
|
---|
1045 | &lock, blocking_lock);
|
---|
1046 | } else {
|
---|
1047 | ret = brl_lock_posix(msg_ctx, br_lck, &lock);
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | #if ZERO_ZERO
|
---|
1051 | /* sort the lock list */
|
---|
1052 | TYPESAFE_QSORT(br_lck->lock_data, (size_t)br_lck->num_locks, lock_compare);
|
---|
1053 | #endif
|
---|
1054 |
|
---|
1055 | /* If we're returning an error, return who blocked us. */
|
---|
1056 | if (!NT_STATUS_IS_OK(ret) && psmblctx) {
|
---|
1057 | *psmblctx = lock.context.smblctx;
|
---|
1058 | }
|
---|
1059 | return ret;
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 | static void brl_delete_lock_struct(struct lock_struct *locks,
|
---|
1063 | unsigned num_locks,
|
---|
1064 | unsigned del_idx)
|
---|
1065 | {
|
---|
1066 | if (del_idx >= num_locks) {
|
---|
1067 | return;
|
---|
1068 | }
|
---|
1069 | memmove(&locks[del_idx], &locks[del_idx+1],
|
---|
1070 | sizeof(*locks) * (num_locks - del_idx - 1));
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | /****************************************************************************
|
---|
1074 | Unlock a range of bytes - Windows semantics.
|
---|
1075 | ****************************************************************************/
|
---|
1076 |
|
---|
1077 | bool brl_unlock_windows_default(struct messaging_context *msg_ctx,
|
---|
1078 | struct byte_range_lock *br_lck,
|
---|
1079 | const struct lock_struct *plock)
|
---|
1080 | {
|
---|
1081 | unsigned int i, j;
|
---|
1082 | struct lock_struct *locks = br_lck->lock_data;
|
---|
1083 | enum brl_type deleted_lock_type = READ_LOCK; /* shut the compiler up.... */
|
---|
1084 |
|
---|
1085 | SMB_ASSERT(plock->lock_type == UNLOCK_LOCK);
|
---|
1086 |
|
---|
1087 | #if ZERO_ZERO
|
---|
1088 | /* Delete write locks by preference... The lock list
|
---|
1089 | is sorted in the zero zero case. */
|
---|
1090 |
|
---|
1091 | for (i = 0; i < br_lck->num_locks; i++) {
|
---|
1092 | struct lock_struct *lock = &locks[i];
|
---|
1093 |
|
---|
1094 | if (lock->lock_type == WRITE_LOCK &&
|
---|
1095 | brl_same_context(&lock->context, &plock->context) &&
|
---|
1096 | lock->fnum == plock->fnum &&
|
---|
1097 | lock->lock_flav == WINDOWS_LOCK &&
|
---|
1098 | lock->start == plock->start &&
|
---|
1099 | lock->size == plock->size) {
|
---|
1100 |
|
---|
1101 | /* found it - delete it */
|
---|
1102 | deleted_lock_type = lock->lock_type;
|
---|
1103 | break;
|
---|
1104 | }
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | if (i != br_lck->num_locks) {
|
---|
1108 | /* We found it - don't search again. */
|
---|
1109 | goto unlock_continue;
|
---|
1110 | }
|
---|
1111 | #endif
|
---|
1112 |
|
---|
1113 | for (i = 0; i < br_lck->num_locks; i++) {
|
---|
1114 | struct lock_struct *lock = &locks[i];
|
---|
1115 |
|
---|
1116 | if (IS_PENDING_LOCK(lock->lock_type)) {
|
---|
1117 | continue;
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | /* Only remove our own locks that match in start, size, and flavour. */
|
---|
1121 | if (brl_same_context(&lock->context, &plock->context) &&
|
---|
1122 | lock->fnum == plock->fnum &&
|
---|
1123 | lock->lock_flav == WINDOWS_LOCK &&
|
---|
1124 | lock->start == plock->start &&
|
---|
1125 | lock->size == plock->size ) {
|
---|
1126 | deleted_lock_type = lock->lock_type;
|
---|
1127 | break;
|
---|
1128 | }
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | if (i == br_lck->num_locks) {
|
---|
1132 | /* we didn't find it */
|
---|
1133 | return False;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | #if ZERO_ZERO
|
---|
1137 | unlock_continue:
|
---|
1138 | #endif
|
---|
1139 |
|
---|
1140 | brl_delete_lock_struct(locks, br_lck->num_locks, i);
|
---|
1141 | br_lck->num_locks -= 1;
|
---|
1142 | br_lck->modified = True;
|
---|
1143 |
|
---|
1144 | /* Unlock the underlying POSIX regions. */
|
---|
1145 | if(lp_posix_locking(br_lck->fsp->conn->params)) {
|
---|
1146 | release_posix_lock_windows_flavour(br_lck->fsp,
|
---|
1147 | plock->start,
|
---|
1148 | plock->size,
|
---|
1149 | deleted_lock_type,
|
---|
1150 | &plock->context,
|
---|
1151 | locks,
|
---|
1152 | br_lck->num_locks);
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | /* Send unlock messages to any pending waiters that overlap. */
|
---|
1156 | for (j=0; j < br_lck->num_locks; j++) {
|
---|
1157 | struct lock_struct *pend_lock = &locks[j];
|
---|
1158 |
|
---|
1159 | /* Ignore non-pending locks. */
|
---|
1160 | if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
|
---|
1161 | continue;
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | /* We could send specific lock info here... */
|
---|
1165 | if (brl_pending_overlap(plock, pend_lock)) {
|
---|
1166 | struct server_id_buf tmp;
|
---|
1167 |
|
---|
1168 | DEBUG(10, ("brl_unlock: sending unlock message to "
|
---|
1169 | "pid %s\n",
|
---|
1170 | server_id_str_buf(pend_lock->context.pid,
|
---|
1171 | &tmp)));
|
---|
1172 |
|
---|
1173 | messaging_send(msg_ctx, pend_lock->context.pid,
|
---|
1174 | MSG_SMB_UNLOCK, &data_blob_null);
|
---|
1175 | }
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | contend_level2_oplocks_end(br_lck->fsp, LEVEL2_CONTEND_WINDOWS_BRL);
|
---|
1179 | return True;
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | /****************************************************************************
|
---|
1183 | Unlock a range of bytes - POSIX semantics.
|
---|
1184 | ****************************************************************************/
|
---|
1185 |
|
---|
1186 | static bool brl_unlock_posix(struct messaging_context *msg_ctx,
|
---|
1187 | struct byte_range_lock *br_lck,
|
---|
1188 | struct lock_struct *plock)
|
---|
1189 | {
|
---|
1190 | unsigned int i, j, count;
|
---|
1191 | struct lock_struct *tp;
|
---|
1192 | struct lock_struct *locks = br_lck->lock_data;
|
---|
1193 | bool overlap_found = False;
|
---|
1194 |
|
---|
1195 | /* No zero-zero locks for POSIX. */
|
---|
1196 | if (plock->start == 0 && plock->size == 0) {
|
---|
1197 | return False;
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | /* Don't allow 64-bit lock wrap. */
|
---|
1201 | if (plock->start + plock->size < plock->start ||
|
---|
1202 | plock->start + plock->size < plock->size) {
|
---|
1203 | DEBUG(10,("brl_unlock_posix: lock wrap\n"));
|
---|
1204 | return False;
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | /* The worst case scenario here is we have to split an
|
---|
1208 | existing POSIX lock range into two, so we need at most
|
---|
1209 | 1 more entry. */
|
---|
1210 |
|
---|
1211 | tp = talloc_array(br_lck, struct lock_struct, br_lck->num_locks + 1);
|
---|
1212 | if (!tp) {
|
---|
1213 | DEBUG(10,("brl_unlock_posix: malloc fail\n"));
|
---|
1214 | return False;
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | count = 0;
|
---|
1218 | for (i = 0; i < br_lck->num_locks; i++) {
|
---|
1219 | struct lock_struct *lock = &locks[i];
|
---|
1220 | unsigned int tmp_count;
|
---|
1221 |
|
---|
1222 | /* Only remove our own locks - ignore fnum. */
|
---|
1223 | if (IS_PENDING_LOCK(lock->lock_type) ||
|
---|
1224 | !brl_same_context(&lock->context, &plock->context)) {
|
---|
1225 | memcpy(&tp[count], lock, sizeof(struct lock_struct));
|
---|
1226 | count++;
|
---|
1227 | continue;
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | if (lock->lock_flav == WINDOWS_LOCK) {
|
---|
1231 | /* Do any Windows flavour locks conflict ? */
|
---|
1232 | if (brl_conflict(lock, plock)) {
|
---|
1233 | TALLOC_FREE(tp);
|
---|
1234 | return false;
|
---|
1235 | }
|
---|
1236 | /* Just copy the Windows lock into the new array. */
|
---|
1237 | memcpy(&tp[count], lock, sizeof(struct lock_struct));
|
---|
1238 | count++;
|
---|
1239 | continue;
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | /* Work out overlaps. */
|
---|
1243 | tmp_count = brlock_posix_split_merge(&tp[count], lock, plock);
|
---|
1244 |
|
---|
1245 | if (tmp_count == 0) {
|
---|
1246 | /* plock overlapped the existing lock completely,
|
---|
1247 | or replaced it. Don't copy the existing lock. */
|
---|
1248 | overlap_found = true;
|
---|
1249 | } else if (tmp_count == 1) {
|
---|
1250 | /* Either no overlap, (simple copy of existing lock) or
|
---|
1251 | * an overlap of an existing lock. */
|
---|
1252 | /* If the lock changed size, we had an overlap. */
|
---|
1253 | if (tp[count].size != lock->size) {
|
---|
1254 | overlap_found = true;
|
---|
1255 | }
|
---|
1256 | count += tmp_count;
|
---|
1257 | } else if (tmp_count == 2) {
|
---|
1258 | /* We split a lock range in two. */
|
---|
1259 | overlap_found = true;
|
---|
1260 | count += tmp_count;
|
---|
1261 |
|
---|
1262 | /* Optimisation... */
|
---|
1263 | /* We know we're finished here as we can't overlap any
|
---|
1264 | more POSIX locks. Copy the rest of the lock array. */
|
---|
1265 |
|
---|
1266 | if (i < br_lck->num_locks - 1) {
|
---|
1267 | memcpy(&tp[count], &locks[i+1],
|
---|
1268 | sizeof(*locks)*((br_lck->num_locks-1) - i));
|
---|
1269 | count += ((br_lck->num_locks-1) - i);
|
---|
1270 | }
|
---|
1271 | break;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | if (!overlap_found) {
|
---|
1277 | /* Just ignore - no change. */
|
---|
1278 | TALLOC_FREE(tp);
|
---|
1279 | DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
|
---|
1280 | return True;
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | /* Unlock any POSIX regions. */
|
---|
1284 | if(lp_posix_locking(br_lck->fsp->conn->params)) {
|
---|
1285 | release_posix_lock_posix_flavour(br_lck->fsp,
|
---|
1286 | plock->start,
|
---|
1287 | plock->size,
|
---|
1288 | &plock->context,
|
---|
1289 | tp,
|
---|
1290 | count);
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | /* Realloc so we don't leak entries per unlock call. */
|
---|
1294 | if (count) {
|
---|
1295 | tp = talloc_realloc(br_lck, tp, struct lock_struct, count);
|
---|
1296 | if (!tp) {
|
---|
1297 | DEBUG(10,("brl_unlock_posix: realloc fail\n"));
|
---|
1298 | return False;
|
---|
1299 | }
|
---|
1300 | } else {
|
---|
1301 | /* We deleted the last lock. */
|
---|
1302 | TALLOC_FREE(tp);
|
---|
1303 | tp = NULL;
|
---|
1304 | }
|
---|
1305 |
|
---|
1306 | contend_level2_oplocks_end(br_lck->fsp,
|
---|
1307 | LEVEL2_CONTEND_POSIX_BRL);
|
---|
1308 |
|
---|
1309 | br_lck->num_locks = count;
|
---|
1310 | TALLOC_FREE(br_lck->lock_data);
|
---|
1311 | locks = tp;
|
---|
1312 | br_lck->lock_data = tp;
|
---|
1313 | br_lck->modified = True;
|
---|
1314 |
|
---|
1315 | /* Send unlock messages to any pending waiters that overlap. */
|
---|
1316 |
|
---|
1317 | for (j=0; j < br_lck->num_locks; j++) {
|
---|
1318 | struct lock_struct *pend_lock = &locks[j];
|
---|
1319 |
|
---|
1320 | /* Ignore non-pending locks. */
|
---|
1321 | if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
|
---|
1322 | continue;
|
---|
1323 | }
|
---|
1324 |
|
---|
1325 | /* We could send specific lock info here... */
|
---|
1326 | if (brl_pending_overlap(plock, pend_lock)) {
|
---|
1327 | struct server_id_buf tmp;
|
---|
1328 |
|
---|
1329 | DEBUG(10, ("brl_unlock: sending unlock message to "
|
---|
1330 | "pid %s\n",
|
---|
1331 | server_id_str_buf(pend_lock->context.pid,
|
---|
1332 | &tmp)));
|
---|
1333 |
|
---|
1334 | messaging_send(msg_ctx, pend_lock->context.pid,
|
---|
1335 | MSG_SMB_UNLOCK, &data_blob_null);
|
---|
1336 | }
|
---|
1337 | }
|
---|
1338 |
|
---|
1339 | return True;
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct *handle,
|
---|
1343 | struct messaging_context *msg_ctx,
|
---|
1344 | struct byte_range_lock *br_lck,
|
---|
1345 | const struct lock_struct *plock)
|
---|
1346 | {
|
---|
1347 | VFS_FIND(brl_unlock_windows);
|
---|
1348 | return handle->fns->brl_unlock_windows_fn(handle, msg_ctx, br_lck,
|
---|
1349 | plock);
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | /****************************************************************************
|
---|
1353 | Unlock a range of bytes.
|
---|
1354 | ****************************************************************************/
|
---|
1355 |
|
---|
1356 | bool brl_unlock(struct messaging_context *msg_ctx,
|
---|
1357 | struct byte_range_lock *br_lck,
|
---|
1358 | uint64_t smblctx,
|
---|
1359 | struct server_id pid,
|
---|
1360 | br_off start,
|
---|
1361 | br_off size,
|
---|
1362 | enum brl_flavour lock_flav)
|
---|
1363 | {
|
---|
1364 | struct lock_struct lock;
|
---|
1365 |
|
---|
1366 | lock.context.smblctx = smblctx;
|
---|
1367 | lock.context.pid = pid;
|
---|
1368 | lock.context.tid = br_lck->fsp->conn->cnum;
|
---|
1369 | lock.start = start;
|
---|
1370 | lock.size = size;
|
---|
1371 | lock.fnum = br_lck->fsp->fnum;
|
---|
1372 | lock.lock_type = UNLOCK_LOCK;
|
---|
1373 | lock.lock_flav = lock_flav;
|
---|
1374 |
|
---|
1375 | if (lock_flav == WINDOWS_LOCK) {
|
---|
1376 | return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck->fsp->conn, msg_ctx,
|
---|
1377 | br_lck, &lock);
|
---|
1378 | } else {
|
---|
1379 | return brl_unlock_posix(msg_ctx, br_lck, &lock);
|
---|
1380 | }
|
---|
1381 | }
|
---|
1382 |
|
---|
1383 | /****************************************************************************
|
---|
1384 | Test if we could add a lock if we wanted to.
|
---|
1385 | Returns True if the region required is currently unlocked, False if locked.
|
---|
1386 | ****************************************************************************/
|
---|
1387 |
|
---|
1388 | bool brl_locktest(struct byte_range_lock *br_lck,
|
---|
1389 | const struct lock_struct *rw_probe)
|
---|
1390 | {
|
---|
1391 | bool ret = True;
|
---|
1392 | unsigned int i;
|
---|
1393 | struct lock_struct *locks = br_lck->lock_data;
|
---|
1394 | files_struct *fsp = br_lck->fsp;
|
---|
1395 |
|
---|
1396 | /* Make sure existing locks don't conflict */
|
---|
1397 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
1398 | /*
|
---|
1399 | * Our own locks don't conflict.
|
---|
1400 | */
|
---|
1401 | if (brl_conflict_other(&locks[i], rw_probe)) {
|
---|
1402 | if (br_lck->record == NULL) {
|
---|
1403 | /* readonly */
|
---|
1404 | return false;
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | if (!serverid_exists(&locks[i].context.pid)) {
|
---|
1408 | locks[i].context.pid.pid = 0;
|
---|
1409 | br_lck->modified = true;
|
---|
1410 | continue;
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | return False;
|
---|
1414 | }
|
---|
1415 | }
|
---|
1416 |
|
---|
1417 | /*
|
---|
1418 | * There is no lock held by an SMB daemon, check to
|
---|
1419 | * see if there is a POSIX lock from a UNIX or NFS process.
|
---|
1420 | * This only conflicts with Windows locks, not POSIX locks.
|
---|
1421 | */
|
---|
1422 |
|
---|
1423 | if(lp_posix_locking(fsp->conn->params) &&
|
---|
1424 | (rw_probe->lock_flav == WINDOWS_LOCK)) {
|
---|
1425 | /*
|
---|
1426 | * Make copies -- is_posix_locked might modify the values
|
---|
1427 | */
|
---|
1428 |
|
---|
1429 | br_off start = rw_probe->start;
|
---|
1430 | br_off size = rw_probe->size;
|
---|
1431 | enum brl_type lock_type = rw_probe->lock_type;
|
---|
1432 |
|
---|
1433 | ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
|
---|
1434 |
|
---|
1435 | DEBUG(10, ("brl_locktest: posix start=%ju len=%ju %s for %s "
|
---|
1436 | "file %s\n", (uintmax_t)start, (uintmax_t)size,
|
---|
1437 | ret ? "locked" : "unlocked",
|
---|
1438 | fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
|
---|
1439 |
|
---|
1440 | /* We need to return the inverse of is_posix_locked. */
|
---|
1441 | ret = !ret;
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 | /* no conflicts - we could have added it */
|
---|
1445 | return ret;
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | /****************************************************************************
|
---|
1449 | Query for existing locks.
|
---|
1450 | ****************************************************************************/
|
---|
1451 |
|
---|
1452 | NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
|
---|
1453 | uint64_t *psmblctx,
|
---|
1454 | struct server_id pid,
|
---|
1455 | br_off *pstart,
|
---|
1456 | br_off *psize,
|
---|
1457 | enum brl_type *plock_type,
|
---|
1458 | enum brl_flavour lock_flav)
|
---|
1459 | {
|
---|
1460 | unsigned int i;
|
---|
1461 | struct lock_struct lock;
|
---|
1462 | const struct lock_struct *locks = br_lck->lock_data;
|
---|
1463 | files_struct *fsp = br_lck->fsp;
|
---|
1464 |
|
---|
1465 | lock.context.smblctx = *psmblctx;
|
---|
1466 | lock.context.pid = pid;
|
---|
1467 | lock.context.tid = br_lck->fsp->conn->cnum;
|
---|
1468 | lock.start = *pstart;
|
---|
1469 | lock.size = *psize;
|
---|
1470 | lock.fnum = fsp->fnum;
|
---|
1471 | lock.lock_type = *plock_type;
|
---|
1472 | lock.lock_flav = lock_flav;
|
---|
1473 |
|
---|
1474 | /* Make sure existing locks don't conflict */
|
---|
1475 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
1476 | const struct lock_struct *exlock = &locks[i];
|
---|
1477 | bool conflict = False;
|
---|
1478 |
|
---|
1479 | if (exlock->lock_flav == WINDOWS_LOCK) {
|
---|
1480 | conflict = brl_conflict(exlock, &lock);
|
---|
1481 | } else {
|
---|
1482 | conflict = brl_conflict_posix(exlock, &lock);
|
---|
1483 | }
|
---|
1484 |
|
---|
1485 | if (conflict) {
|
---|
1486 | *psmblctx = exlock->context.smblctx;
|
---|
1487 | *pstart = exlock->start;
|
---|
1488 | *psize = exlock->size;
|
---|
1489 | *plock_type = exlock->lock_type;
|
---|
1490 | return NT_STATUS_LOCK_NOT_GRANTED;
|
---|
1491 | }
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 | /*
|
---|
1495 | * There is no lock held by an SMB daemon, check to
|
---|
1496 | * see if there is a POSIX lock from a UNIX or NFS process.
|
---|
1497 | */
|
---|
1498 |
|
---|
1499 | if(lp_posix_locking(fsp->conn->params)) {
|
---|
1500 | bool ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
|
---|
1501 |
|
---|
1502 | DEBUG(10, ("brl_lockquery: posix start=%ju len=%ju %s for %s "
|
---|
1503 | "file %s\n", (uintmax_t)*pstart,
|
---|
1504 | (uintmax_t)*psize, ret ? "locked" : "unlocked",
|
---|
1505 | fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
|
---|
1506 |
|
---|
1507 | if (ret) {
|
---|
1508 | /* Hmmm. No clue what to set smblctx to - use -1. */
|
---|
1509 | *psmblctx = 0xFFFFFFFFFFFFFFFFLL;
|
---|
1510 | return NT_STATUS_LOCK_NOT_GRANTED;
|
---|
1511 | }
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 | return NT_STATUS_OK;
|
---|
1515 | }
|
---|
1516 |
|
---|
1517 |
|
---|
1518 | bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct *handle,
|
---|
1519 | struct byte_range_lock *br_lck,
|
---|
1520 | struct lock_struct *plock)
|
---|
1521 | {
|
---|
1522 | VFS_FIND(brl_cancel_windows);
|
---|
1523 | return handle->fns->brl_cancel_windows_fn(handle, br_lck, plock);
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 | /****************************************************************************
|
---|
1527 | Remove a particular pending lock.
|
---|
1528 | ****************************************************************************/
|
---|
1529 | bool brl_lock_cancel(struct byte_range_lock *br_lck,
|
---|
1530 | uint64_t smblctx,
|
---|
1531 | struct server_id pid,
|
---|
1532 | br_off start,
|
---|
1533 | br_off size,
|
---|
1534 | enum brl_flavour lock_flav)
|
---|
1535 | {
|
---|
1536 | bool ret;
|
---|
1537 | struct lock_struct lock;
|
---|
1538 |
|
---|
1539 | lock.context.smblctx = smblctx;
|
---|
1540 | lock.context.pid = pid;
|
---|
1541 | lock.context.tid = br_lck->fsp->conn->cnum;
|
---|
1542 | lock.start = start;
|
---|
1543 | lock.size = size;
|
---|
1544 | lock.fnum = br_lck->fsp->fnum;
|
---|
1545 | lock.lock_flav = lock_flav;
|
---|
1546 | /* lock.lock_type doesn't matter */
|
---|
1547 |
|
---|
1548 | if (lock_flav == WINDOWS_LOCK) {
|
---|
1549 | ret = SMB_VFS_BRL_CANCEL_WINDOWS(br_lck->fsp->conn, br_lck,
|
---|
1550 | &lock);
|
---|
1551 | } else {
|
---|
1552 | ret = brl_lock_cancel_default(br_lck, &lock);
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | return ret;
|
---|
1556 | }
|
---|
1557 |
|
---|
1558 | bool brl_lock_cancel_default(struct byte_range_lock *br_lck,
|
---|
1559 | struct lock_struct *plock)
|
---|
1560 | {
|
---|
1561 | unsigned int i;
|
---|
1562 | struct lock_struct *locks = br_lck->lock_data;
|
---|
1563 |
|
---|
1564 | SMB_ASSERT(plock);
|
---|
1565 |
|
---|
1566 | for (i = 0; i < br_lck->num_locks; i++) {
|
---|
1567 | struct lock_struct *lock = &locks[i];
|
---|
1568 |
|
---|
1569 | /* For pending locks we *always* care about the fnum. */
|
---|
1570 | if (brl_same_context(&lock->context, &plock->context) &&
|
---|
1571 | lock->fnum == plock->fnum &&
|
---|
1572 | IS_PENDING_LOCK(lock->lock_type) &&
|
---|
1573 | lock->lock_flav == plock->lock_flav &&
|
---|
1574 | lock->start == plock->start &&
|
---|
1575 | lock->size == plock->size) {
|
---|
1576 | break;
|
---|
1577 | }
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | if (i == br_lck->num_locks) {
|
---|
1581 | /* Didn't find it. */
|
---|
1582 | return False;
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 | brl_delete_lock_struct(locks, br_lck->num_locks, i);
|
---|
1586 | br_lck->num_locks -= 1;
|
---|
1587 | br_lck->modified = True;
|
---|
1588 | return True;
|
---|
1589 | }
|
---|
1590 |
|
---|
1591 | /****************************************************************************
|
---|
1592 | Remove any locks associated with a open file.
|
---|
1593 | We return True if this process owns any other Windows locks on this
|
---|
1594 | fd and so we should not immediately close the fd.
|
---|
1595 | ****************************************************************************/
|
---|
1596 |
|
---|
1597 | void brl_close_fnum(struct messaging_context *msg_ctx,
|
---|
1598 | struct byte_range_lock *br_lck)
|
---|
1599 | {
|
---|
1600 | files_struct *fsp = br_lck->fsp;
|
---|
1601 | uint32_t tid = fsp->conn->cnum;
|
---|
1602 | uint64_t fnum = fsp->fnum;
|
---|
1603 | unsigned int i;
|
---|
1604 | struct lock_struct *locks = br_lck->lock_data;
|
---|
1605 | struct server_id pid = messaging_server_id(fsp->conn->sconn->msg_ctx);
|
---|
1606 | struct lock_struct *locks_copy;
|
---|
1607 | unsigned int num_locks_copy;
|
---|
1608 |
|
---|
1609 | /* Copy the current lock array. */
|
---|
1610 | if (br_lck->num_locks) {
|
---|
1611 | locks_copy = (struct lock_struct *)talloc_memdup(br_lck, locks, br_lck->num_locks * sizeof(struct lock_struct));
|
---|
1612 | if (!locks_copy) {
|
---|
1613 | smb_panic("brl_close_fnum: talloc failed");
|
---|
1614 | }
|
---|
1615 | } else {
|
---|
1616 | locks_copy = NULL;
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 | num_locks_copy = br_lck->num_locks;
|
---|
1620 |
|
---|
1621 | for (i=0; i < num_locks_copy; i++) {
|
---|
1622 | struct lock_struct *lock = &locks_copy[i];
|
---|
1623 |
|
---|
1624 | if (lock->context.tid == tid && serverid_equal(&lock->context.pid, &pid) &&
|
---|
1625 | (lock->fnum == fnum)) {
|
---|
1626 | brl_unlock(msg_ctx,
|
---|
1627 | br_lck,
|
---|
1628 | lock->context.smblctx,
|
---|
1629 | pid,
|
---|
1630 | lock->start,
|
---|
1631 | lock->size,
|
---|
1632 | lock->lock_flav);
|
---|
1633 | }
|
---|
1634 | }
|
---|
1635 | }
|
---|
1636 |
|
---|
1637 | bool brl_mark_disconnected(struct files_struct *fsp)
|
---|
1638 | {
|
---|
1639 | uint32_t tid = fsp->conn->cnum;
|
---|
1640 | uint64_t smblctx;
|
---|
1641 | uint64_t fnum = fsp->fnum;
|
---|
1642 | unsigned int i;
|
---|
1643 | struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
|
---|
1644 | struct byte_range_lock *br_lck = NULL;
|
---|
1645 |
|
---|
1646 | if (fsp->op == NULL) {
|
---|
1647 | return false;
|
---|
1648 | }
|
---|
1649 |
|
---|
1650 | smblctx = fsp->op->global->open_persistent_id;
|
---|
1651 |
|
---|
1652 | if (!fsp->op->global->durable) {
|
---|
1653 | return false;
|
---|
1654 | }
|
---|
1655 |
|
---|
1656 | if (fsp->current_lock_count == 0) {
|
---|
1657 | return true;
|
---|
1658 | }
|
---|
1659 |
|
---|
1660 | br_lck = brl_get_locks(talloc_tos(), fsp);
|
---|
1661 | if (br_lck == NULL) {
|
---|
1662 | return false;
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
1666 | struct lock_struct *lock = &br_lck->lock_data[i];
|
---|
1667 |
|
---|
1668 | /*
|
---|
1669 | * as this is a durable handle, we only expect locks
|
---|
1670 | * of the current file handle!
|
---|
1671 | */
|
---|
1672 |
|
---|
1673 | if (lock->context.smblctx != smblctx) {
|
---|
1674 | TALLOC_FREE(br_lck);
|
---|
1675 | return false;
|
---|
1676 | }
|
---|
1677 |
|
---|
1678 | if (lock->context.tid != tid) {
|
---|
1679 | TALLOC_FREE(br_lck);
|
---|
1680 | return false;
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | if (!serverid_equal(&lock->context.pid, &self)) {
|
---|
1684 | TALLOC_FREE(br_lck);
|
---|
1685 | return false;
|
---|
1686 | }
|
---|
1687 |
|
---|
1688 | if (lock->fnum != fnum) {
|
---|
1689 | TALLOC_FREE(br_lck);
|
---|
1690 | return false;
|
---|
1691 | }
|
---|
1692 |
|
---|
1693 | server_id_set_disconnected(&lock->context.pid);
|
---|
1694 | lock->context.tid = TID_FIELD_INVALID;
|
---|
1695 | lock->fnum = FNUM_FIELD_INVALID;
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 | br_lck->modified = true;
|
---|
1699 | TALLOC_FREE(br_lck);
|
---|
1700 | return true;
|
---|
1701 | }
|
---|
1702 |
|
---|
1703 | bool brl_reconnect_disconnected(struct files_struct *fsp)
|
---|
1704 | {
|
---|
1705 | uint32_t tid = fsp->conn->cnum;
|
---|
1706 | uint64_t smblctx;
|
---|
1707 | uint64_t fnum = fsp->fnum;
|
---|
1708 | unsigned int i;
|
---|
1709 | struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
|
---|
1710 | struct byte_range_lock *br_lck = NULL;
|
---|
1711 |
|
---|
1712 | if (fsp->op == NULL) {
|
---|
1713 | return false;
|
---|
1714 | }
|
---|
1715 |
|
---|
1716 | smblctx = fsp->op->global->open_persistent_id;
|
---|
1717 |
|
---|
1718 | if (!fsp->op->global->durable) {
|
---|
1719 | return false;
|
---|
1720 | }
|
---|
1721 |
|
---|
1722 | /*
|
---|
1723 | * When reconnecting, we do not want to validate the brlock entries
|
---|
1724 | * and thereby remove our own (disconnected) entries but reactivate
|
---|
1725 | * them instead.
|
---|
1726 | */
|
---|
1727 |
|
---|
1728 | br_lck = brl_get_locks(talloc_tos(), fsp);
|
---|
1729 | if (br_lck == NULL) {
|
---|
1730 | return false;
|
---|
1731 | }
|
---|
1732 |
|
---|
1733 | if (br_lck->num_locks == 0) {
|
---|
1734 | TALLOC_FREE(br_lck);
|
---|
1735 | return true;
|
---|
1736 | }
|
---|
1737 |
|
---|
1738 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
1739 | struct lock_struct *lock = &br_lck->lock_data[i];
|
---|
1740 |
|
---|
1741 | /*
|
---|
1742 | * as this is a durable handle we only expect locks
|
---|
1743 | * of the current file handle!
|
---|
1744 | */
|
---|
1745 |
|
---|
1746 | if (lock->context.smblctx != smblctx) {
|
---|
1747 | TALLOC_FREE(br_lck);
|
---|
1748 | return false;
|
---|
1749 | }
|
---|
1750 |
|
---|
1751 | if (lock->context.tid != TID_FIELD_INVALID) {
|
---|
1752 | TALLOC_FREE(br_lck);
|
---|
1753 | return false;
|
---|
1754 | }
|
---|
1755 |
|
---|
1756 | if (!server_id_is_disconnected(&lock->context.pid)) {
|
---|
1757 | TALLOC_FREE(br_lck);
|
---|
1758 | return false;
|
---|
1759 | }
|
---|
1760 |
|
---|
1761 | if (lock->fnum != FNUM_FIELD_INVALID) {
|
---|
1762 | TALLOC_FREE(br_lck);
|
---|
1763 | return false;
|
---|
1764 | }
|
---|
1765 |
|
---|
1766 | lock->context.pid = self;
|
---|
1767 | lock->context.tid = tid;
|
---|
1768 | lock->fnum = fnum;
|
---|
1769 | }
|
---|
1770 |
|
---|
1771 | fsp->current_lock_count = br_lck->num_locks;
|
---|
1772 | br_lck->modified = true;
|
---|
1773 | TALLOC_FREE(br_lck);
|
---|
1774 | return true;
|
---|
1775 | }
|
---|
1776 |
|
---|
1777 | struct brl_forall_cb {
|
---|
1778 | void (*fn)(struct file_id id, struct server_id pid,
|
---|
1779 | enum brl_type lock_type,
|
---|
1780 | enum brl_flavour lock_flav,
|
---|
1781 | br_off start, br_off size,
|
---|
1782 | void *private_data);
|
---|
1783 | void *private_data;
|
---|
1784 | };
|
---|
1785 |
|
---|
1786 | /****************************************************************************
|
---|
1787 | Traverse the whole database with this function, calling traverse_callback
|
---|
1788 | on each lock.
|
---|
1789 | ****************************************************************************/
|
---|
1790 |
|
---|
1791 | static int brl_traverse_fn(struct db_record *rec, void *state)
|
---|
1792 | {
|
---|
1793 | struct brl_forall_cb *cb = (struct brl_forall_cb *)state;
|
---|
1794 | struct lock_struct *locks;
|
---|
1795 | struct file_id *key;
|
---|
1796 | unsigned int i;
|
---|
1797 | unsigned int num_locks = 0;
|
---|
1798 | TDB_DATA dbkey;
|
---|
1799 | TDB_DATA value;
|
---|
1800 |
|
---|
1801 | dbkey = dbwrap_record_get_key(rec);
|
---|
1802 | value = dbwrap_record_get_value(rec);
|
---|
1803 |
|
---|
1804 | /* In a traverse function we must make a copy of
|
---|
1805 | dbuf before modifying it. */
|
---|
1806 |
|
---|
1807 | locks = (struct lock_struct *)talloc_memdup(
|
---|
1808 | talloc_tos(), value.dptr, value.dsize);
|
---|
1809 | if (!locks) {
|
---|
1810 | return -1; /* Terminate traversal. */
|
---|
1811 | }
|
---|
1812 |
|
---|
1813 | key = (struct file_id *)dbkey.dptr;
|
---|
1814 | num_locks = value.dsize/sizeof(*locks);
|
---|
1815 |
|
---|
1816 | if (cb->fn) {
|
---|
1817 | for ( i=0; i<num_locks; i++) {
|
---|
1818 | cb->fn(*key,
|
---|
1819 | locks[i].context.pid,
|
---|
1820 | locks[i].lock_type,
|
---|
1821 | locks[i].lock_flav,
|
---|
1822 | locks[i].start,
|
---|
1823 | locks[i].size,
|
---|
1824 | cb->private_data);
|
---|
1825 | }
|
---|
1826 | }
|
---|
1827 |
|
---|
1828 | TALLOC_FREE(locks);
|
---|
1829 | return 0;
|
---|
1830 | }
|
---|
1831 |
|
---|
1832 | /*******************************************************************
|
---|
1833 | Call the specified function on each lock in the database.
|
---|
1834 | ********************************************************************/
|
---|
1835 |
|
---|
1836 | int brl_forall(void (*fn)(struct file_id id, struct server_id pid,
|
---|
1837 | enum brl_type lock_type,
|
---|
1838 | enum brl_flavour lock_flav,
|
---|
1839 | br_off start, br_off size,
|
---|
1840 | void *private_data),
|
---|
1841 | void *private_data)
|
---|
1842 | {
|
---|
1843 | struct brl_forall_cb cb;
|
---|
1844 | NTSTATUS status;
|
---|
1845 | int count = 0;
|
---|
1846 |
|
---|
1847 | if (!brlock_db) {
|
---|
1848 | return 0;
|
---|
1849 | }
|
---|
1850 | cb.fn = fn;
|
---|
1851 | cb.private_data = private_data;
|
---|
1852 | status = dbwrap_traverse(brlock_db, brl_traverse_fn, &cb, &count);
|
---|
1853 |
|
---|
1854 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1855 | return -1;
|
---|
1856 | } else {
|
---|
1857 | return count;
|
---|
1858 | }
|
---|
1859 | }
|
---|
1860 |
|
---|
1861 | /*******************************************************************
|
---|
1862 | Store a potentially modified set of byte range lock data back into
|
---|
1863 | the database.
|
---|
1864 | Unlock the record.
|
---|
1865 | ********************************************************************/
|
---|
1866 |
|
---|
1867 | static void byte_range_lock_flush(struct byte_range_lock *br_lck)
|
---|
1868 | {
|
---|
1869 | unsigned i;
|
---|
1870 | struct lock_struct *locks = br_lck->lock_data;
|
---|
1871 |
|
---|
1872 | if (!br_lck->modified) {
|
---|
1873 | DEBUG(10, ("br_lck not modified\n"));
|
---|
1874 | goto done;
|
---|
1875 | }
|
---|
1876 |
|
---|
1877 | i = 0;
|
---|
1878 |
|
---|
1879 | while (i < br_lck->num_locks) {
|
---|
1880 | if (locks[i].context.pid.pid == 0) {
|
---|
1881 | /*
|
---|
1882 | * Autocleanup, the process conflicted and does not
|
---|
1883 | * exist anymore.
|
---|
1884 | */
|
---|
1885 | locks[i] = locks[br_lck->num_locks-1];
|
---|
1886 | br_lck->num_locks -= 1;
|
---|
1887 | } else {
|
---|
1888 | i += 1;
|
---|
1889 | }
|
---|
1890 | }
|
---|
1891 |
|
---|
1892 | if ((br_lck->num_locks == 0) && (br_lck->num_read_oplocks == 0)) {
|
---|
1893 | /* No locks - delete this entry. */
|
---|
1894 | NTSTATUS status = dbwrap_record_delete(br_lck->record);
|
---|
1895 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1896 | DEBUG(0, ("delete_rec returned %s\n",
|
---|
1897 | nt_errstr(status)));
|
---|
1898 | smb_panic("Could not delete byte range lock entry");
|
---|
1899 | }
|
---|
1900 | } else {
|
---|
1901 | size_t lock_len, data_len;
|
---|
1902 | TDB_DATA data;
|
---|
1903 | NTSTATUS status;
|
---|
1904 |
|
---|
1905 | lock_len = br_lck->num_locks * sizeof(struct lock_struct);
|
---|
1906 | data_len = lock_len + sizeof(br_lck->num_read_oplocks);
|
---|
1907 |
|
---|
1908 | data.dsize = data_len;
|
---|
1909 | data.dptr = talloc_array(talloc_tos(), uint8_t, data_len);
|
---|
1910 | SMB_ASSERT(data.dptr != NULL);
|
---|
1911 |
|
---|
1912 | memcpy(data.dptr, br_lck->lock_data, lock_len);
|
---|
1913 | memcpy(data.dptr + lock_len, &br_lck->num_read_oplocks,
|
---|
1914 | sizeof(br_lck->num_read_oplocks));
|
---|
1915 |
|
---|
1916 | status = dbwrap_record_store(br_lck->record, data, TDB_REPLACE);
|
---|
1917 | TALLOC_FREE(data.dptr);
|
---|
1918 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1919 | DEBUG(0, ("store returned %s\n", nt_errstr(status)));
|
---|
1920 | smb_panic("Could not store byte range mode entry");
|
---|
1921 | }
|
---|
1922 | }
|
---|
1923 |
|
---|
1924 | DEBUG(10, ("seqnum=%d\n", dbwrap_get_seqnum(brlock_db)));
|
---|
1925 |
|
---|
1926 | done:
|
---|
1927 | br_lck->modified = false;
|
---|
1928 | TALLOC_FREE(br_lck->record);
|
---|
1929 | }
|
---|
1930 |
|
---|
1931 | static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
|
---|
1932 | {
|
---|
1933 | byte_range_lock_flush(br_lck);
|
---|
1934 | return 0;
|
---|
1935 | }
|
---|
1936 |
|
---|
1937 | static bool brl_parse_data(struct byte_range_lock *br_lck, TDB_DATA data)
|
---|
1938 | {
|
---|
1939 | size_t data_len;
|
---|
1940 |
|
---|
1941 | if (data.dsize == 0) {
|
---|
1942 | return true;
|
---|
1943 | }
|
---|
1944 | if (data.dsize % sizeof(struct lock_struct) !=
|
---|
1945 | sizeof(br_lck->num_read_oplocks)) {
|
---|
1946 | DEBUG(1, ("Invalid data size: %u\n", (unsigned)data.dsize));
|
---|
1947 | return false;
|
---|
1948 | }
|
---|
1949 |
|
---|
1950 | br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
|
---|
1951 | data_len = br_lck->num_locks * sizeof(struct lock_struct);
|
---|
1952 |
|
---|
1953 | br_lck->lock_data = talloc_memdup(br_lck, data.dptr, data_len);
|
---|
1954 | if (br_lck->lock_data == NULL) {
|
---|
1955 | DEBUG(1, ("talloc_memdup failed\n"));
|
---|
1956 | return false;
|
---|
1957 | }
|
---|
1958 | memcpy(&br_lck->num_read_oplocks, data.dptr + data_len,
|
---|
1959 | sizeof(br_lck->num_read_oplocks));
|
---|
1960 | return true;
|
---|
1961 | }
|
---|
1962 |
|
---|
1963 | /*******************************************************************
|
---|
1964 | Fetch a set of byte range lock data from the database.
|
---|
1965 | Leave the record locked.
|
---|
1966 | TALLOC_FREE(brl) will release the lock in the destructor.
|
---|
1967 | ********************************************************************/
|
---|
1968 |
|
---|
1969 | struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx, files_struct *fsp)
|
---|
1970 | {
|
---|
1971 | TDB_DATA key, data;
|
---|
1972 | struct byte_range_lock *br_lck;
|
---|
1973 |
|
---|
1974 | br_lck = talloc_zero(mem_ctx, struct byte_range_lock);
|
---|
1975 | if (br_lck == NULL) {
|
---|
1976 | return NULL;
|
---|
1977 | }
|
---|
1978 |
|
---|
1979 | br_lck->fsp = fsp;
|
---|
1980 |
|
---|
1981 | key.dptr = (uint8_t *)&fsp->file_id;
|
---|
1982 | key.dsize = sizeof(struct file_id);
|
---|
1983 |
|
---|
1984 | br_lck->record = dbwrap_fetch_locked(brlock_db, br_lck, key);
|
---|
1985 |
|
---|
1986 | if (br_lck->record == NULL) {
|
---|
1987 | DEBUG(3, ("Could not lock byte range lock entry\n"));
|
---|
1988 | TALLOC_FREE(br_lck);
|
---|
1989 | return NULL;
|
---|
1990 | }
|
---|
1991 |
|
---|
1992 | data = dbwrap_record_get_value(br_lck->record);
|
---|
1993 |
|
---|
1994 | if (!brl_parse_data(br_lck, data)) {
|
---|
1995 | TALLOC_FREE(br_lck);
|
---|
1996 | return NULL;
|
---|
1997 | }
|
---|
1998 |
|
---|
1999 | talloc_set_destructor(br_lck, byte_range_lock_destructor);
|
---|
2000 |
|
---|
2001 | if (DEBUGLEVEL >= 10) {
|
---|
2002 | unsigned int i;
|
---|
2003 | struct lock_struct *locks = br_lck->lock_data;
|
---|
2004 | DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
|
---|
2005 | br_lck->num_locks,
|
---|
2006 | file_id_string_tos(&fsp->file_id)));
|
---|
2007 | for( i = 0; i < br_lck->num_locks; i++) {
|
---|
2008 | print_lock_struct(i, &locks[i]);
|
---|
2009 | }
|
---|
2010 | }
|
---|
2011 |
|
---|
2012 | return br_lck;
|
---|
2013 | }
|
---|
2014 |
|
---|
2015 | struct brl_get_locks_readonly_state {
|
---|
2016 | TALLOC_CTX *mem_ctx;
|
---|
2017 | struct byte_range_lock **br_lock;
|
---|
2018 | };
|
---|
2019 |
|
---|
2020 | static void brl_get_locks_readonly_parser(TDB_DATA key, TDB_DATA data,
|
---|
2021 | void *private_data)
|
---|
2022 | {
|
---|
2023 | struct brl_get_locks_readonly_state *state =
|
---|
2024 | (struct brl_get_locks_readonly_state *)private_data;
|
---|
2025 | struct byte_range_lock *br_lck;
|
---|
2026 |
|
---|
2027 | br_lck = talloc_pooled_object(
|
---|
2028 | state->mem_ctx, struct byte_range_lock, 1, data.dsize);
|
---|
2029 | if (br_lck == NULL) {
|
---|
2030 | *state->br_lock = NULL;
|
---|
2031 | return;
|
---|
2032 | }
|
---|
2033 | *br_lck = (struct byte_range_lock) { 0 };
|
---|
2034 | if (!brl_parse_data(br_lck, data)) {
|
---|
2035 | *state->br_lock = NULL;
|
---|
2036 | return;
|
---|
2037 | }
|
---|
2038 | *state->br_lock = br_lck;
|
---|
2039 | }
|
---|
2040 |
|
---|
2041 | struct byte_range_lock *brl_get_locks_readonly(files_struct *fsp)
|
---|
2042 | {
|
---|
2043 | struct byte_range_lock *br_lock = NULL;
|
---|
2044 | struct brl_get_locks_readonly_state state;
|
---|
2045 | NTSTATUS status;
|
---|
2046 |
|
---|
2047 | DEBUG(10, ("seqnum=%d, fsp->brlock_seqnum=%d\n",
|
---|
2048 | dbwrap_get_seqnum(brlock_db), fsp->brlock_seqnum));
|
---|
2049 |
|
---|
2050 | if ((fsp->brlock_rec != NULL)
|
---|
2051 | && (dbwrap_get_seqnum(brlock_db) == fsp->brlock_seqnum)) {
|
---|
2052 | /*
|
---|
2053 | * We have cached the brlock_rec and the database did not
|
---|
2054 | * change.
|
---|
2055 | */
|
---|
2056 | return fsp->brlock_rec;
|
---|
2057 | }
|
---|
2058 |
|
---|
2059 | /*
|
---|
2060 | * Parse the record fresh from the database
|
---|
2061 | */
|
---|
2062 |
|
---|
2063 | state.mem_ctx = fsp;
|
---|
2064 | state.br_lock = &br_lock;
|
---|
2065 |
|
---|
2066 | status = dbwrap_parse_record(
|
---|
2067 | brlock_db,
|
---|
2068 | make_tdb_data((uint8_t *)&fsp->file_id,
|
---|
2069 | sizeof(fsp->file_id)),
|
---|
2070 | brl_get_locks_readonly_parser, &state);
|
---|
2071 |
|
---|
2072 | if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_FOUND)) {
|
---|
2073 | /*
|
---|
2074 | * No locks on this file. Return an empty br_lock.
|
---|
2075 | */
|
---|
2076 | br_lock = talloc(fsp, struct byte_range_lock);
|
---|
2077 | if (br_lock == NULL) {
|
---|
2078 | return NULL;
|
---|
2079 | }
|
---|
2080 |
|
---|
2081 | br_lock->num_read_oplocks = 0;
|
---|
2082 | br_lock->num_locks = 0;
|
---|
2083 | br_lock->lock_data = NULL;
|
---|
2084 |
|
---|
2085 | } else if (!NT_STATUS_IS_OK(status)) {
|
---|
2086 | DEBUG(3, ("Could not parse byte range lock record: "
|
---|
2087 | "%s\n", nt_errstr(status)));
|
---|
2088 | return NULL;
|
---|
2089 | }
|
---|
2090 | if (br_lock == NULL) {
|
---|
2091 | return NULL;
|
---|
2092 | }
|
---|
2093 |
|
---|
2094 | br_lock->fsp = fsp;
|
---|
2095 | br_lock->modified = false;
|
---|
2096 | br_lock->record = NULL;
|
---|
2097 |
|
---|
2098 | if (lp_clustering()) {
|
---|
2099 | /*
|
---|
2100 | * In the cluster case we can't cache the brlock struct
|
---|
2101 | * because dbwrap_get_seqnum does not work reliably over
|
---|
2102 | * ctdb. Thus we have to throw away the brlock struct soon.
|
---|
2103 | */
|
---|
2104 | talloc_steal(talloc_tos(), br_lock);
|
---|
2105 | } else {
|
---|
2106 | /*
|
---|
2107 | * Cache the brlock struct, invalidated when the dbwrap_seqnum
|
---|
2108 | * changes. See beginning of this routine.
|
---|
2109 | */
|
---|
2110 | TALLOC_FREE(fsp->brlock_rec);
|
---|
2111 | fsp->brlock_rec = br_lock;
|
---|
2112 | fsp->brlock_seqnum = dbwrap_get_seqnum(brlock_db);
|
---|
2113 | }
|
---|
2114 |
|
---|
2115 | return br_lock;
|
---|
2116 | }
|
---|
2117 |
|
---|
2118 | struct brl_revalidate_state {
|
---|
2119 | ssize_t array_size;
|
---|
2120 | uint32_t num_pids;
|
---|
2121 | struct server_id *pids;
|
---|
2122 | };
|
---|
2123 |
|
---|
2124 | /*
|
---|
2125 | * Collect PIDs of all processes with pending entries
|
---|
2126 | */
|
---|
2127 |
|
---|
2128 | static void brl_revalidate_collect(struct file_id id, struct server_id pid,
|
---|
2129 | enum brl_type lock_type,
|
---|
2130 | enum brl_flavour lock_flav,
|
---|
2131 | br_off start, br_off size,
|
---|
2132 | void *private_data)
|
---|
2133 | {
|
---|
2134 | struct brl_revalidate_state *state =
|
---|
2135 | (struct brl_revalidate_state *)private_data;
|
---|
2136 |
|
---|
2137 | if (!IS_PENDING_LOCK(lock_type)) {
|
---|
2138 | return;
|
---|
2139 | }
|
---|
2140 |
|
---|
2141 | add_to_large_array(state, sizeof(pid), (void *)&pid,
|
---|
2142 | &state->pids, &state->num_pids,
|
---|
2143 | &state->array_size);
|
---|
2144 | }
|
---|
2145 |
|
---|
2146 | /*
|
---|
2147 | * qsort callback to sort the processes
|
---|
2148 | */
|
---|
2149 |
|
---|
2150 | static int compare_procids(const void *p1, const void *p2)
|
---|
2151 | {
|
---|
2152 | const struct server_id *i1 = (const struct server_id *)p1;
|
---|
2153 | const struct server_id *i2 = (const struct server_id *)p2;
|
---|
2154 |
|
---|
2155 | if (i1->pid < i2->pid) return -1;
|
---|
2156 | if (i1->pid > i2->pid) return 1;
|
---|
2157 | return 0;
|
---|
2158 | }
|
---|
2159 |
|
---|
2160 | /*
|
---|
2161 | * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
|
---|
2162 | * locks so that they retry. Mainly used in the cluster code after a node has
|
---|
2163 | * died.
|
---|
2164 | *
|
---|
2165 | * Done in two steps to avoid double-sends: First we collect all entries in an
|
---|
2166 | * array, then qsort that array and only send to non-dupes.
|
---|
2167 | */
|
---|
2168 |
|
---|
2169 | void brl_revalidate(struct messaging_context *msg_ctx,
|
---|
2170 | void *private_data,
|
---|
2171 | uint32_t msg_type,
|
---|
2172 | struct server_id server_id,
|
---|
2173 | DATA_BLOB *data)
|
---|
2174 | {
|
---|
2175 | struct brl_revalidate_state *state;
|
---|
2176 | uint32_t i;
|
---|
2177 | struct server_id last_pid;
|
---|
2178 |
|
---|
2179 | if (!(state = talloc_zero(NULL, struct brl_revalidate_state))) {
|
---|
2180 | DEBUG(0, ("talloc failed\n"));
|
---|
2181 | return;
|
---|
2182 | }
|
---|
2183 |
|
---|
2184 | brl_forall(brl_revalidate_collect, state);
|
---|
2185 |
|
---|
2186 | if (state->array_size == -1) {
|
---|
2187 | DEBUG(0, ("talloc failed\n"));
|
---|
2188 | goto done;
|
---|
2189 | }
|
---|
2190 |
|
---|
2191 | if (state->num_pids == 0) {
|
---|
2192 | goto done;
|
---|
2193 | }
|
---|
2194 |
|
---|
2195 | TYPESAFE_QSORT(state->pids, state->num_pids, compare_procids);
|
---|
2196 |
|
---|
2197 | ZERO_STRUCT(last_pid);
|
---|
2198 |
|
---|
2199 | for (i=0; i<state->num_pids; i++) {
|
---|
2200 | if (serverid_equal(&last_pid, &state->pids[i])) {
|
---|
2201 | /*
|
---|
2202 | * We've seen that one already
|
---|
2203 | */
|
---|
2204 | continue;
|
---|
2205 | }
|
---|
2206 |
|
---|
2207 | messaging_send(msg_ctx, state->pids[i], MSG_SMB_UNLOCK,
|
---|
2208 | &data_blob_null);
|
---|
2209 | last_pid = state->pids[i];
|
---|
2210 | }
|
---|
2211 |
|
---|
2212 | done:
|
---|
2213 | TALLOC_FREE(state);
|
---|
2214 | return;
|
---|
2215 | }
|
---|
2216 |
|
---|
2217 | bool brl_cleanup_disconnected(struct file_id fid, uint64_t open_persistent_id)
|
---|
2218 | {
|
---|
2219 | bool ret = false;
|
---|
2220 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
2221 | TDB_DATA key, val;
|
---|
2222 | struct db_record *rec;
|
---|
2223 | struct lock_struct *lock;
|
---|
2224 | unsigned n, num;
|
---|
2225 | NTSTATUS status;
|
---|
2226 |
|
---|
2227 | key = make_tdb_data((void*)&fid, sizeof(fid));
|
---|
2228 |
|
---|
2229 | rec = dbwrap_fetch_locked(brlock_db, frame, key);
|
---|
2230 | if (rec == NULL) {
|
---|
2231 | DEBUG(5, ("brl_cleanup_disconnected: failed to fetch record "
|
---|
2232 | "for file %s\n", file_id_string(frame, &fid)));
|
---|
2233 | goto done;
|
---|
2234 | }
|
---|
2235 |
|
---|
2236 | val = dbwrap_record_get_value(rec);
|
---|
2237 | lock = (struct lock_struct*)val.dptr;
|
---|
2238 | num = val.dsize / sizeof(struct lock_struct);
|
---|
2239 | if (lock == NULL) {
|
---|
2240 | DEBUG(10, ("brl_cleanup_disconnected: no byte range locks for "
|
---|
2241 | "file %s\n", file_id_string(frame, &fid)));
|
---|
2242 | ret = true;
|
---|
2243 | goto done;
|
---|
2244 | }
|
---|
2245 |
|
---|
2246 | for (n=0; n<num; n++) {
|
---|
2247 | struct lock_context *ctx = &lock[n].context;
|
---|
2248 |
|
---|
2249 | if (!server_id_is_disconnected(&ctx->pid)) {
|
---|
2250 | struct server_id_buf tmp;
|
---|
2251 | DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
|
---|
2252 | "%s used by server %s, do not cleanup\n",
|
---|
2253 | file_id_string(frame, &fid),
|
---|
2254 | server_id_str_buf(ctx->pid, &tmp)));
|
---|
2255 | goto done;
|
---|
2256 | }
|
---|
2257 |
|
---|
2258 | if (ctx->smblctx != open_persistent_id) {
|
---|
2259 | DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
|
---|
2260 | "%s expected smblctx %llu but found %llu"
|
---|
2261 | ", do not cleanup\n",
|
---|
2262 | file_id_string(frame, &fid),
|
---|
2263 | (unsigned long long)open_persistent_id,
|
---|
2264 | (unsigned long long)ctx->smblctx));
|
---|
2265 | goto done;
|
---|
2266 | }
|
---|
2267 | }
|
---|
2268 |
|
---|
2269 | status = dbwrap_record_delete(rec);
|
---|
2270 | if (!NT_STATUS_IS_OK(status)) {
|
---|
2271 | DEBUG(5, ("brl_cleanup_disconnected: failed to delete record "
|
---|
2272 | "for file %s from %s, open %llu: %s\n",
|
---|
2273 | file_id_string(frame, &fid), dbwrap_name(brlock_db),
|
---|
2274 | (unsigned long long)open_persistent_id,
|
---|
2275 | nt_errstr(status)));
|
---|
2276 | goto done;
|
---|
2277 | }
|
---|
2278 |
|
---|
2279 | DEBUG(10, ("brl_cleanup_disconnected: "
|
---|
2280 | "file %s cleaned up %u entries from open %llu\n",
|
---|
2281 | file_id_string(frame, &fid), num,
|
---|
2282 | (unsigned long long)open_persistent_id));
|
---|
2283 |
|
---|
2284 | ret = true;
|
---|
2285 | done:
|
---|
2286 | talloc_free(frame);
|
---|
2287 | return ret;
|
---|
2288 | }
|
---|