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 |
|
---|
29 | #undef DBGC_CLASS
|
---|
30 | #define DBGC_CLASS DBGC_LOCKING
|
---|
31 |
|
---|
32 | #define ZERO_ZERO 0
|
---|
33 |
|
---|
34 | /* The open brlock.tdb database. */
|
---|
35 |
|
---|
36 | static struct db_context *brlock_db;
|
---|
37 |
|
---|
38 | /****************************************************************************
|
---|
39 | Debug info at level 10 for lock struct.
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | static void print_lock_struct(unsigned int i, struct lock_struct *pls)
|
---|
43 | {
|
---|
44 | DEBUG(10,("[%u]: smbpid = %u, tid = %u, pid = %s, ",
|
---|
45 | i,
|
---|
46 | (unsigned int)pls->context.smbpid,
|
---|
47 | (unsigned int)pls->context.tid,
|
---|
48 | procid_str(debug_ctx(), &pls->context.pid) ));
|
---|
49 |
|
---|
50 | DEBUG(10,("start = %.0f, size = %.0f, fnum = %d, %s %s\n",
|
---|
51 | (double)pls->start,
|
---|
52 | (double)pls->size,
|
---|
53 | pls->fnum,
|
---|
54 | lock_type_name(pls->lock_type),
|
---|
55 | lock_flav_name(pls->lock_flav) ));
|
---|
56 | }
|
---|
57 |
|
---|
58 | /****************************************************************************
|
---|
59 | See if two locking contexts are equal.
|
---|
60 | ****************************************************************************/
|
---|
61 |
|
---|
62 | bool brl_same_context(const struct lock_context *ctx1,
|
---|
63 | const struct lock_context *ctx2)
|
---|
64 | {
|
---|
65 | return (procid_equal(&ctx1->pid, &ctx2->pid) &&
|
---|
66 | (ctx1->smbpid == ctx2->smbpid) &&
|
---|
67 | (ctx1->tid == ctx2->tid));
|
---|
68 | }
|
---|
69 |
|
---|
70 | /****************************************************************************
|
---|
71 | See if lck1 and lck2 overlap.
|
---|
72 | ****************************************************************************/
|
---|
73 |
|
---|
74 | static bool brl_overlap(const struct lock_struct *lck1,
|
---|
75 | const struct lock_struct *lck2)
|
---|
76 | {
|
---|
77 | /* this extra check is not redundent - it copes with locks
|
---|
78 | that go beyond the end of 64 bit file space */
|
---|
79 | if (lck1->size != 0 &&
|
---|
80 | lck1->start == lck2->start &&
|
---|
81 | lck1->size == lck2->size) {
|
---|
82 | return True;
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (lck1->start >= (lck2->start+lck2->size) ||
|
---|
86 | lck2->start >= (lck1->start+lck1->size)) {
|
---|
87 | return False;
|
---|
88 | }
|
---|
89 | return True;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /****************************************************************************
|
---|
93 | See if lock2 can be added when lock1 is in place.
|
---|
94 | ****************************************************************************/
|
---|
95 |
|
---|
96 | static bool brl_conflict(const struct lock_struct *lck1,
|
---|
97 | const struct lock_struct *lck2)
|
---|
98 | {
|
---|
99 | /* Ignore PENDING locks. */
|
---|
100 | if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
|
---|
101 | return False;
|
---|
102 |
|
---|
103 | /* Read locks never conflict. */
|
---|
104 | if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
|
---|
105 | return False;
|
---|
106 | }
|
---|
107 |
|
---|
108 | if (brl_same_context(&lck1->context, &lck2->context) &&
|
---|
109 | lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
|
---|
110 | return False;
|
---|
111 | }
|
---|
112 |
|
---|
113 | return brl_overlap(lck1, lck2);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /****************************************************************************
|
---|
117 | See if lock2 can be added when lock1 is in place - when both locks are POSIX
|
---|
118 | flavour. POSIX locks ignore fnum - they only care about dev/ino which we
|
---|
119 | know already match.
|
---|
120 | ****************************************************************************/
|
---|
121 |
|
---|
122 | static bool brl_conflict_posix(const struct lock_struct *lck1,
|
---|
123 | const struct lock_struct *lck2)
|
---|
124 | {
|
---|
125 | #if defined(DEVELOPER)
|
---|
126 | SMB_ASSERT(lck1->lock_flav == POSIX_LOCK);
|
---|
127 | SMB_ASSERT(lck2->lock_flav == POSIX_LOCK);
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | /* Ignore PENDING locks. */
|
---|
131 | if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
|
---|
132 | return False;
|
---|
133 |
|
---|
134 | /* Read locks never conflict. */
|
---|
135 | if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
|
---|
136 | return False;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /* Locks on the same context con't conflict. Ignore fnum. */
|
---|
140 | if (brl_same_context(&lck1->context, &lck2->context)) {
|
---|
141 | return False;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /* One is read, the other write, or the context is different,
|
---|
145 | do they overlap ? */
|
---|
146 | return brl_overlap(lck1, lck2);
|
---|
147 | }
|
---|
148 |
|
---|
149 | #if ZERO_ZERO
|
---|
150 | static bool brl_conflict1(const struct lock_struct *lck1,
|
---|
151 | const struct lock_struct *lck2)
|
---|
152 | {
|
---|
153 | if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
|
---|
154 | return False;
|
---|
155 |
|
---|
156 | if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
|
---|
157 | return False;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (brl_same_context(&lck1->context, &lck2->context) &&
|
---|
161 | lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
|
---|
162 | return False;
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
|
---|
166 | return True;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (lck1->start >= (lck2->start + lck2->size) ||
|
---|
170 | lck2->start >= (lck1->start + lck1->size)) {
|
---|
171 | return False;
|
---|
172 | }
|
---|
173 |
|
---|
174 | return True;
|
---|
175 | }
|
---|
176 | #endif
|
---|
177 |
|
---|
178 | /****************************************************************************
|
---|
179 | Check to see if this lock conflicts, but ignore our own locks on the
|
---|
180 | same fnum only. This is the read/write lock check code path.
|
---|
181 | This is never used in the POSIX lock case.
|
---|
182 | ****************************************************************************/
|
---|
183 |
|
---|
184 | static bool brl_conflict_other(const struct lock_struct *lck1, const struct lock_struct *lck2)
|
---|
185 | {
|
---|
186 | if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
|
---|
187 | return False;
|
---|
188 |
|
---|
189 | if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
|
---|
190 | return False;
|
---|
191 |
|
---|
192 | /* POSIX flavour locks never conflict here - this is only called
|
---|
193 | in the read/write path. */
|
---|
194 |
|
---|
195 | if (lck1->lock_flav == POSIX_LOCK && lck2->lock_flav == POSIX_LOCK)
|
---|
196 | return False;
|
---|
197 |
|
---|
198 | /*
|
---|
199 | * Incoming WRITE locks conflict with existing READ locks even
|
---|
200 | * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
|
---|
201 | */
|
---|
202 |
|
---|
203 | if (!(lck2->lock_type == WRITE_LOCK && lck1->lock_type == READ_LOCK)) {
|
---|
204 | if (brl_same_context(&lck1->context, &lck2->context) &&
|
---|
205 | lck1->fnum == lck2->fnum)
|
---|
206 | return False;
|
---|
207 | }
|
---|
208 |
|
---|
209 | return brl_overlap(lck1, lck2);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /****************************************************************************
|
---|
213 | Check if an unlock overlaps a pending lock.
|
---|
214 | ****************************************************************************/
|
---|
215 |
|
---|
216 | static bool brl_pending_overlap(const struct lock_struct *lock, const struct lock_struct *pend_lock)
|
---|
217 | {
|
---|
218 | if ((lock->start <= pend_lock->start) && (lock->start + lock->size > pend_lock->start))
|
---|
219 | return True;
|
---|
220 | if ((lock->start >= pend_lock->start) && (lock->start <= pend_lock->start + pend_lock->size))
|
---|
221 | return True;
|
---|
222 | return False;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /****************************************************************************
|
---|
226 | Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
|
---|
227 | is the same as this one and changes its error code. I wonder if any
|
---|
228 | app depends on this ?
|
---|
229 | ****************************************************************************/
|
---|
230 |
|
---|
231 | static NTSTATUS brl_lock_failed(files_struct *fsp, const struct lock_struct *lock, bool blocking_lock)
|
---|
232 | {
|
---|
233 | if (lock->start >= 0xEF000000 && (lock->start >> 63) == 0) {
|
---|
234 | /* amazing the little things you learn with a test
|
---|
235 | suite. Locks beyond this offset (as a 64 bit
|
---|
236 | number!) always generate the conflict error code,
|
---|
237 | unless the top bit is set */
|
---|
238 | if (!blocking_lock) {
|
---|
239 | fsp->last_lock_failure = *lock;
|
---|
240 | }
|
---|
241 | return NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
242 | }
|
---|
243 |
|
---|
244 | if (procid_equal(&lock->context.pid, &fsp->last_lock_failure.context.pid) &&
|
---|
245 | lock->context.tid == fsp->last_lock_failure.context.tid &&
|
---|
246 | lock->fnum == fsp->last_lock_failure.fnum &&
|
---|
247 | lock->start == fsp->last_lock_failure.start) {
|
---|
248 | return NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
249 | }
|
---|
250 |
|
---|
251 | if (!blocking_lock) {
|
---|
252 | fsp->last_lock_failure = *lock;
|
---|
253 | }
|
---|
254 | return NT_STATUS_LOCK_NOT_GRANTED;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /****************************************************************************
|
---|
258 | Open up the brlock.tdb database.
|
---|
259 | ****************************************************************************/
|
---|
260 |
|
---|
261 | void brl_init(bool read_only)
|
---|
262 | {
|
---|
263 | if (brlock_db) {
|
---|
264 | return;
|
---|
265 | }
|
---|
266 | brlock_db = db_open(NULL, lock_path("brlock.tdb"),
|
---|
267 | lp_open_files_db_hash_size(),
|
---|
268 | TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST,
|
---|
269 | read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644 );
|
---|
270 | if (!brlock_db) {
|
---|
271 | DEBUG(0,("Failed to open byte range locking database %s\n",
|
---|
272 | lock_path("brlock.tdb")));
|
---|
273 | return;
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | /****************************************************************************
|
---|
278 | Close down the brlock.tdb database.
|
---|
279 | ****************************************************************************/
|
---|
280 |
|
---|
281 | void brl_shutdown(void)
|
---|
282 | {
|
---|
283 | TALLOC_FREE(brlock_db);
|
---|
284 | }
|
---|
285 |
|
---|
286 | #if ZERO_ZERO
|
---|
287 | /****************************************************************************
|
---|
288 | Compare two locks for sorting.
|
---|
289 | ****************************************************************************/
|
---|
290 |
|
---|
291 | static int lock_compare(const struct lock_struct *lck1,
|
---|
292 | const struct lock_struct *lck2)
|
---|
293 | {
|
---|
294 | if (lck1->start != lck2->start) {
|
---|
295 | return (lck1->start - lck2->start);
|
---|
296 | }
|
---|
297 | if (lck2->size != lck1->size) {
|
---|
298 | return ((int)lck1->size - (int)lck2->size);
|
---|
299 | }
|
---|
300 | return 0;
|
---|
301 | }
|
---|
302 | #endif
|
---|
303 |
|
---|
304 | /****************************************************************************
|
---|
305 | Lock a range of bytes - Windows lock semantics.
|
---|
306 | ****************************************************************************/
|
---|
307 |
|
---|
308 | static NTSTATUS brl_lock_windows(struct byte_range_lock *br_lck,
|
---|
309 | struct lock_struct *plock, bool blocking_lock)
|
---|
310 | {
|
---|
311 | unsigned int i;
|
---|
312 | files_struct *fsp = br_lck->fsp;
|
---|
313 | struct lock_struct *locks = br_lck->lock_data;
|
---|
314 |
|
---|
315 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
316 | /* Do any Windows or POSIX locks conflict ? */
|
---|
317 | if (brl_conflict(&locks[i], plock)) {
|
---|
318 | /* Remember who blocked us. */
|
---|
319 | plock->context.smbpid = locks[i].context.smbpid;
|
---|
320 | return brl_lock_failed(fsp,plock,blocking_lock);
|
---|
321 | }
|
---|
322 | #if ZERO_ZERO
|
---|
323 | if (plock->start == 0 && plock->size == 0 &&
|
---|
324 | locks[i].size == 0) {
|
---|
325 | break;
|
---|
326 | }
|
---|
327 | #endif
|
---|
328 | }
|
---|
329 |
|
---|
330 | /* We can get the Windows lock, now see if it needs to
|
---|
331 | be mapped into a lower level POSIX one, and if so can
|
---|
332 | we get it ? */
|
---|
333 |
|
---|
334 | if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(fsp->conn->params)) {
|
---|
335 | int errno_ret;
|
---|
336 | if (!set_posix_lock_windows_flavour(fsp,
|
---|
337 | plock->start,
|
---|
338 | plock->size,
|
---|
339 | plock->lock_type,
|
---|
340 | &plock->context,
|
---|
341 | locks,
|
---|
342 | br_lck->num_locks,
|
---|
343 | &errno_ret)) {
|
---|
344 |
|
---|
345 | /* We don't know who blocked us. */
|
---|
346 | plock->context.smbpid = 0xFFFFFFFF;
|
---|
347 |
|
---|
348 | if (errno_ret == EACCES || errno_ret == EAGAIN) {
|
---|
349 | return NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
350 | } else {
|
---|
351 | return map_nt_error_from_unix(errno);
|
---|
352 | }
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | /* no conflicts - add it to the list of locks */
|
---|
357 | locks = (struct lock_struct *)SMB_REALLOC(locks, (br_lck->num_locks + 1) * sizeof(*locks));
|
---|
358 | if (!locks) {
|
---|
359 | return NT_STATUS_NO_MEMORY;
|
---|
360 | }
|
---|
361 |
|
---|
362 | memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
|
---|
363 | br_lck->num_locks += 1;
|
---|
364 | br_lck->lock_data = locks;
|
---|
365 | br_lck->modified = True;
|
---|
366 |
|
---|
367 | return NT_STATUS_OK;
|
---|
368 | }
|
---|
369 |
|
---|
370 | /****************************************************************************
|
---|
371 | Cope with POSIX range splits and merges.
|
---|
372 | ****************************************************************************/
|
---|
373 |
|
---|
374 | static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr, /* Output array. */
|
---|
375 | struct lock_struct *ex, /* existing lock. */
|
---|
376 | struct lock_struct *plock) /* proposed lock. */
|
---|
377 | {
|
---|
378 | bool lock_types_differ = (ex->lock_type != plock->lock_type);
|
---|
379 |
|
---|
380 | /* We can't merge non-conflicting locks on different context - ignore fnum. */
|
---|
381 |
|
---|
382 | if (!brl_same_context(&ex->context, &plock->context)) {
|
---|
383 | /* Just copy. */
|
---|
384 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
385 | return 1;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /* We now know we have the same context. */
|
---|
389 |
|
---|
390 | /* Did we overlap ? */
|
---|
391 |
|
---|
392 | /*********************************************
|
---|
393 | +---------+
|
---|
394 | | ex |
|
---|
395 | +---------+
|
---|
396 | +-------+
|
---|
397 | | plock |
|
---|
398 | +-------+
|
---|
399 | OR....
|
---|
400 | +---------+
|
---|
401 | | ex |
|
---|
402 | +---------+
|
---|
403 | **********************************************/
|
---|
404 |
|
---|
405 | if ( (ex->start > (plock->start + plock->size)) ||
|
---|
406 | (plock->start > (ex->start + ex->size))) {
|
---|
407 |
|
---|
408 | /* No overlap with this lock - copy existing. */
|
---|
409 |
|
---|
410 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
411 | return 1;
|
---|
412 | }
|
---|
413 |
|
---|
414 | /*********************************************
|
---|
415 | +---------------------------+
|
---|
416 | | ex |
|
---|
417 | +---------------------------+
|
---|
418 | +---------------------------+
|
---|
419 | | plock | -> replace with plock.
|
---|
420 | +---------------------------+
|
---|
421 | OR
|
---|
422 | +---------------+
|
---|
423 | | ex |
|
---|
424 | +---------------+
|
---|
425 | +---------------------------+
|
---|
426 | | plock | -> replace with plock.
|
---|
427 | +---------------------------+
|
---|
428 |
|
---|
429 | **********************************************/
|
---|
430 |
|
---|
431 | if ( (ex->start >= plock->start) &&
|
---|
432 | (ex->start + ex->size <= plock->start + plock->size) ) {
|
---|
433 |
|
---|
434 | /* Replace - discard existing lock. */
|
---|
435 |
|
---|
436 | return 0;
|
---|
437 | }
|
---|
438 |
|
---|
439 | /*********************************************
|
---|
440 | Adjacent after.
|
---|
441 | +-------+
|
---|
442 | | ex |
|
---|
443 | +-------+
|
---|
444 | +---------------+
|
---|
445 | | plock |
|
---|
446 | +---------------+
|
---|
447 |
|
---|
448 | BECOMES....
|
---|
449 | +---------------+-------+
|
---|
450 | | plock | ex | - different lock types.
|
---|
451 | +---------------+-------+
|
---|
452 | OR.... (merge)
|
---|
453 | +-----------------------+
|
---|
454 | | plock | - same lock type.
|
---|
455 | +-----------------------+
|
---|
456 | **********************************************/
|
---|
457 |
|
---|
458 | if (plock->start + plock->size == ex->start) {
|
---|
459 |
|
---|
460 | /* If the lock types are the same, we merge, if different, we
|
---|
461 | add the remainder of the old lock. */
|
---|
462 |
|
---|
463 | if (lock_types_differ) {
|
---|
464 | /* Add existing. */
|
---|
465 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
466 | return 1;
|
---|
467 | } else {
|
---|
468 | /* Merge - adjust incoming lock as we may have more
|
---|
469 | * merging to come. */
|
---|
470 | plock->size += ex->size;
|
---|
471 | return 0;
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | /*********************************************
|
---|
476 | Adjacent before.
|
---|
477 | +-------+
|
---|
478 | | ex |
|
---|
479 | +-------+
|
---|
480 | +---------------+
|
---|
481 | | plock |
|
---|
482 | +---------------+
|
---|
483 | BECOMES....
|
---|
484 | +-------+---------------+
|
---|
485 | | ex | plock | - different lock types
|
---|
486 | +-------+---------------+
|
---|
487 |
|
---|
488 | OR.... (merge)
|
---|
489 | +-----------------------+
|
---|
490 | | plock | - same lock type.
|
---|
491 | +-----------------------+
|
---|
492 |
|
---|
493 | **********************************************/
|
---|
494 |
|
---|
495 | if (ex->start + ex->size == plock->start) {
|
---|
496 |
|
---|
497 | /* If the lock types are the same, we merge, if different, we
|
---|
498 | add the existing lock. */
|
---|
499 |
|
---|
500 | if (lock_types_differ) {
|
---|
501 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
502 | return 1;
|
---|
503 | } else {
|
---|
504 | /* Merge - adjust incoming lock as we may have more
|
---|
505 | * merging to come. */
|
---|
506 | plock->start = ex->start;
|
---|
507 | plock->size += ex->size;
|
---|
508 | return 0;
|
---|
509 | }
|
---|
510 | }
|
---|
511 |
|
---|
512 | /*********************************************
|
---|
513 | Overlap after.
|
---|
514 | +-----------------------+
|
---|
515 | | ex |
|
---|
516 | +-----------------------+
|
---|
517 | +---------------+
|
---|
518 | | plock |
|
---|
519 | +---------------+
|
---|
520 | OR
|
---|
521 | +----------------+
|
---|
522 | | ex |
|
---|
523 | +----------------+
|
---|
524 | +---------------+
|
---|
525 | | plock |
|
---|
526 | +---------------+
|
---|
527 |
|
---|
528 | BECOMES....
|
---|
529 | +---------------+-------+
|
---|
530 | | plock | ex | - different lock types.
|
---|
531 | +---------------+-------+
|
---|
532 | OR.... (merge)
|
---|
533 | +-----------------------+
|
---|
534 | | plock | - same lock type.
|
---|
535 | +-----------------------+
|
---|
536 | **********************************************/
|
---|
537 |
|
---|
538 | if ( (ex->start >= plock->start) &&
|
---|
539 | (ex->start <= plock->start + plock->size) &&
|
---|
540 | (ex->start + ex->size > plock->start + plock->size) ) {
|
---|
541 |
|
---|
542 | /* If the lock types are the same, we merge, if different, we
|
---|
543 | add the remainder of the old lock. */
|
---|
544 |
|
---|
545 | if (lock_types_differ) {
|
---|
546 | /* Add remaining existing. */
|
---|
547 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
548 | /* Adjust existing start and size. */
|
---|
549 | lck_arr[0].start = plock->start + plock->size;
|
---|
550 | lck_arr[0].size = (ex->start + ex->size) - (plock->start + plock->size);
|
---|
551 | return 1;
|
---|
552 | } else {
|
---|
553 | /* Merge - adjust incoming lock as we may have more
|
---|
554 | * merging to come. */
|
---|
555 | plock->size += (ex->start + ex->size) - (plock->start + plock->size);
|
---|
556 | return 0;
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 | /*********************************************
|
---|
561 | Overlap before.
|
---|
562 | +-----------------------+
|
---|
563 | | ex |
|
---|
564 | +-----------------------+
|
---|
565 | +---------------+
|
---|
566 | | plock |
|
---|
567 | +---------------+
|
---|
568 | OR
|
---|
569 | +-------------+
|
---|
570 | | ex |
|
---|
571 | +-------------+
|
---|
572 | +---------------+
|
---|
573 | | plock |
|
---|
574 | +---------------+
|
---|
575 |
|
---|
576 | BECOMES....
|
---|
577 | +-------+---------------+
|
---|
578 | | ex | plock | - different lock types
|
---|
579 | +-------+---------------+
|
---|
580 |
|
---|
581 | OR.... (merge)
|
---|
582 | +-----------------------+
|
---|
583 | | plock | - same lock type.
|
---|
584 | +-----------------------+
|
---|
585 |
|
---|
586 | **********************************************/
|
---|
587 |
|
---|
588 | if ( (ex->start < plock->start) &&
|
---|
589 | (ex->start + ex->size >= plock->start) &&
|
---|
590 | (ex->start + ex->size <= plock->start + plock->size) ) {
|
---|
591 |
|
---|
592 | /* If the lock types are the same, we merge, if different, we
|
---|
593 | add the truncated old lock. */
|
---|
594 |
|
---|
595 | if (lock_types_differ) {
|
---|
596 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
597 | /* Adjust existing size. */
|
---|
598 | lck_arr[0].size = plock->start - ex->start;
|
---|
599 | return 1;
|
---|
600 | } else {
|
---|
601 | /* Merge - adjust incoming lock as we may have more
|
---|
602 | * merging to come. MUST ADJUST plock SIZE FIRST ! */
|
---|
603 | plock->size += (plock->start - ex->start);
|
---|
604 | plock->start = ex->start;
|
---|
605 | return 0;
|
---|
606 | }
|
---|
607 | }
|
---|
608 |
|
---|
609 | /*********************************************
|
---|
610 | Complete overlap.
|
---|
611 | +---------------------------+
|
---|
612 | | ex |
|
---|
613 | +---------------------------+
|
---|
614 | +---------+
|
---|
615 | | plock |
|
---|
616 | +---------+
|
---|
617 | BECOMES.....
|
---|
618 | +-------+---------+---------+
|
---|
619 | | ex | plock | ex | - different lock types.
|
---|
620 | +-------+---------+---------+
|
---|
621 | OR
|
---|
622 | +---------------------------+
|
---|
623 | | plock | - same lock type.
|
---|
624 | +---------------------------+
|
---|
625 | **********************************************/
|
---|
626 |
|
---|
627 | if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
|
---|
628 |
|
---|
629 | if (lock_types_differ) {
|
---|
630 |
|
---|
631 | /* We have to split ex into two locks here. */
|
---|
632 |
|
---|
633 | memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
|
---|
634 | memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
|
---|
635 |
|
---|
636 | /* Adjust first existing size. */
|
---|
637 | lck_arr[0].size = plock->start - ex->start;
|
---|
638 |
|
---|
639 | /* Adjust second existing start and size. */
|
---|
640 | lck_arr[1].start = plock->start + plock->size;
|
---|
641 | lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
|
---|
642 | return 2;
|
---|
643 | } else {
|
---|
644 | /* Just eat the existing locks, merge them into plock. */
|
---|
645 | plock->start = ex->start;
|
---|
646 | plock->size = ex->size;
|
---|
647 | return 0;
|
---|
648 | }
|
---|
649 | }
|
---|
650 |
|
---|
651 | /* Never get here. */
|
---|
652 | smb_panic("brlock_posix_split_merge");
|
---|
653 | /* Notreached. */
|
---|
654 |
|
---|
655 | /* Keep some compilers happy. */
|
---|
656 | return 0;
|
---|
657 | }
|
---|
658 |
|
---|
659 | /****************************************************************************
|
---|
660 | Lock a range of bytes - POSIX lock semantics.
|
---|
661 | We must cope with range splits and merges.
|
---|
662 | ****************************************************************************/
|
---|
663 |
|
---|
664 | static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
|
---|
665 | struct byte_range_lock *br_lck,
|
---|
666 | struct lock_struct *plock)
|
---|
667 | {
|
---|
668 | unsigned int i, count;
|
---|
669 | struct lock_struct *locks = br_lck->lock_data;
|
---|
670 | struct lock_struct *tp;
|
---|
671 | bool signal_pending_read = False;
|
---|
672 |
|
---|
673 | /* No zero-zero locks for POSIX. */
|
---|
674 | if (plock->start == 0 && plock->size == 0) {
|
---|
675 | return NT_STATUS_INVALID_PARAMETER;
|
---|
676 | }
|
---|
677 |
|
---|
678 | /* Don't allow 64-bit lock wrap. */
|
---|
679 | if (plock->start + plock->size < plock->start ||
|
---|
680 | plock->start + plock->size < plock->size) {
|
---|
681 | return NT_STATUS_INVALID_PARAMETER;
|
---|
682 | }
|
---|
683 |
|
---|
684 | /* The worst case scenario here is we have to split an
|
---|
685 | existing POSIX lock range into two, and add our lock,
|
---|
686 | so we need at most 2 more entries. */
|
---|
687 |
|
---|
688 | tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 2));
|
---|
689 | if (!tp) {
|
---|
690 | return NT_STATUS_NO_MEMORY;
|
---|
691 | }
|
---|
692 |
|
---|
693 | count = 0;
|
---|
694 |
|
---|
695 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
696 | struct lock_struct *curr_lock = &locks[i];
|
---|
697 |
|
---|
698 | /* If we have a pending read lock, a lock downgrade should
|
---|
699 | trigger a lock re-evaluation. */
|
---|
700 | if (curr_lock->lock_type == PENDING_READ_LOCK &&
|
---|
701 | brl_pending_overlap(plock, curr_lock)) {
|
---|
702 | signal_pending_read = True;
|
---|
703 | }
|
---|
704 |
|
---|
705 | if (curr_lock->lock_flav == WINDOWS_LOCK) {
|
---|
706 | /* Do any Windows flavour locks conflict ? */
|
---|
707 | if (brl_conflict(curr_lock, plock)) {
|
---|
708 | /* No games with error messages. */
|
---|
709 | SAFE_FREE(tp);
|
---|
710 | /* Remember who blocked us. */
|
---|
711 | plock->context.smbpid = curr_lock->context.smbpid;
|
---|
712 | return NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
713 | }
|
---|
714 | /* Just copy the Windows lock into the new array. */
|
---|
715 | memcpy(&tp[count], curr_lock, sizeof(struct lock_struct));
|
---|
716 | count++;
|
---|
717 | } else {
|
---|
718 | /* POSIX conflict semantics are different. */
|
---|
719 | if (brl_conflict_posix(curr_lock, plock)) {
|
---|
720 | /* Can't block ourselves with POSIX locks. */
|
---|
721 | /* No games with error messages. */
|
---|
722 | SAFE_FREE(tp);
|
---|
723 | /* Remember who blocked us. */
|
---|
724 | plock->context.smbpid = curr_lock->context.smbpid;
|
---|
725 | return NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
726 | }
|
---|
727 |
|
---|
728 | /* Work out overlaps. */
|
---|
729 | count += brlock_posix_split_merge(&tp[count], curr_lock, plock);
|
---|
730 | }
|
---|
731 | }
|
---|
732 |
|
---|
733 | /* Try and add the lock in order, sorted by lock start. */
|
---|
734 | for (i=0; i < count; i++) {
|
---|
735 | struct lock_struct *curr_lock = &tp[i];
|
---|
736 |
|
---|
737 | if (curr_lock->start <= plock->start) {
|
---|
738 | continue;
|
---|
739 | }
|
---|
740 | }
|
---|
741 |
|
---|
742 | if (i < count) {
|
---|
743 | memmove(&tp[i+1], &tp[i],
|
---|
744 | (count - i)*sizeof(struct lock_struct));
|
---|
745 | }
|
---|
746 | memcpy(&tp[i], plock, sizeof(struct lock_struct));
|
---|
747 | count++;
|
---|
748 |
|
---|
749 | /* We can get the POSIX lock, now see if it needs to
|
---|
750 | be mapped into a lower level POSIX one, and if so can
|
---|
751 | we get it ? */
|
---|
752 |
|
---|
753 | if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(br_lck->fsp->conn->params)) {
|
---|
754 | int errno_ret;
|
---|
755 |
|
---|
756 | /* The lower layer just needs to attempt to
|
---|
757 | get the system POSIX lock. We've weeded out
|
---|
758 | any conflicts above. */
|
---|
759 |
|
---|
760 | if (!set_posix_lock_posix_flavour(br_lck->fsp,
|
---|
761 | plock->start,
|
---|
762 | plock->size,
|
---|
763 | plock->lock_type,
|
---|
764 | &errno_ret)) {
|
---|
765 |
|
---|
766 | /* We don't know who blocked us. */
|
---|
767 | plock->context.smbpid = 0xFFFFFFFF;
|
---|
768 |
|
---|
769 | if (errno_ret == EACCES || errno_ret == EAGAIN) {
|
---|
770 | SAFE_FREE(tp);
|
---|
771 | return NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
772 | } else {
|
---|
773 | SAFE_FREE(tp);
|
---|
774 | return map_nt_error_from_unix(errno);
|
---|
775 | }
|
---|
776 | }
|
---|
777 | }
|
---|
778 |
|
---|
779 | /* If we didn't use all the allocated size,
|
---|
780 | * Realloc so we don't leak entries per lock call. */
|
---|
781 | if (count < br_lck->num_locks + 2) {
|
---|
782 | tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
|
---|
783 | if (!tp) {
|
---|
784 | return NT_STATUS_NO_MEMORY;
|
---|
785 | }
|
---|
786 | }
|
---|
787 |
|
---|
788 | br_lck->num_locks = count;
|
---|
789 | SAFE_FREE(br_lck->lock_data);
|
---|
790 | br_lck->lock_data = tp;
|
---|
791 | locks = tp;
|
---|
792 | br_lck->modified = True;
|
---|
793 |
|
---|
794 | /* A successful downgrade from write to read lock can trigger a lock
|
---|
795 | re-evalutation where waiting readers can now proceed. */
|
---|
796 |
|
---|
797 | if (signal_pending_read) {
|
---|
798 | /* Send unlock messages to any pending read waiters that overlap. */
|
---|
799 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
800 | struct lock_struct *pend_lock = &locks[i];
|
---|
801 |
|
---|
802 | /* Ignore non-pending locks. */
|
---|
803 | if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
|
---|
804 | continue;
|
---|
805 | }
|
---|
806 |
|
---|
807 | if (pend_lock->lock_type == PENDING_READ_LOCK &&
|
---|
808 | brl_pending_overlap(plock, pend_lock)) {
|
---|
809 | DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
|
---|
810 | procid_str_static(&pend_lock->context.pid )));
|
---|
811 |
|
---|
812 | messaging_send(msg_ctx, pend_lock->context.pid,
|
---|
813 | MSG_SMB_UNLOCK, &data_blob_null);
|
---|
814 | }
|
---|
815 | }
|
---|
816 | }
|
---|
817 |
|
---|
818 | return NT_STATUS_OK;
|
---|
819 | }
|
---|
820 |
|
---|
821 | /****************************************************************************
|
---|
822 | Lock a range of bytes.
|
---|
823 | ****************************************************************************/
|
---|
824 |
|
---|
825 | NTSTATUS brl_lock(struct messaging_context *msg_ctx,
|
---|
826 | struct byte_range_lock *br_lck,
|
---|
827 | uint32 smbpid,
|
---|
828 | struct server_id pid,
|
---|
829 | br_off start,
|
---|
830 | br_off size,
|
---|
831 | enum brl_type lock_type,
|
---|
832 | enum brl_flavour lock_flav,
|
---|
833 | bool blocking_lock,
|
---|
834 | uint32 *psmbpid)
|
---|
835 | {
|
---|
836 | NTSTATUS ret;
|
---|
837 | struct lock_struct lock;
|
---|
838 |
|
---|
839 | #if !ZERO_ZERO
|
---|
840 | if (start == 0 && size == 0) {
|
---|
841 | DEBUG(0,("client sent 0/0 lock - please report this\n"));
|
---|
842 | }
|
---|
843 | #endif
|
---|
844 |
|
---|
845 | #ifdef DEVELOPER
|
---|
846 | /* Quieten valgrind on test. */
|
---|
847 | memset(&lock, '\0', sizeof(lock));
|
---|
848 | #endif
|
---|
849 |
|
---|
850 | lock.context.smbpid = smbpid;
|
---|
851 | lock.context.pid = pid;
|
---|
852 | lock.context.tid = br_lck->fsp->conn->cnum;
|
---|
853 | lock.start = start;
|
---|
854 | lock.size = size;
|
---|
855 | lock.fnum = br_lck->fsp->fnum;
|
---|
856 | lock.lock_type = lock_type;
|
---|
857 | lock.lock_flav = lock_flav;
|
---|
858 |
|
---|
859 | if (lock_flav == WINDOWS_LOCK) {
|
---|
860 | ret = brl_lock_windows(br_lck, &lock, blocking_lock);
|
---|
861 | } else {
|
---|
862 | ret = brl_lock_posix(msg_ctx, br_lck, &lock);
|
---|
863 | }
|
---|
864 |
|
---|
865 | #if ZERO_ZERO
|
---|
866 | /* sort the lock list */
|
---|
867 | qsort(br_lck->lock_data, (size_t)br_lck->num_locks, sizeof(lock), lock_compare);
|
---|
868 | #endif
|
---|
869 |
|
---|
870 | /* If we're returning an error, return who blocked us. */
|
---|
871 | if (!NT_STATUS_IS_OK(ret) && psmbpid) {
|
---|
872 | *psmbpid = lock.context.smbpid;
|
---|
873 | }
|
---|
874 | return ret;
|
---|
875 | }
|
---|
876 |
|
---|
877 | /****************************************************************************
|
---|
878 | Unlock a range of bytes - Windows semantics.
|
---|
879 | ****************************************************************************/
|
---|
880 |
|
---|
881 | static bool brl_unlock_windows(struct messaging_context *msg_ctx,
|
---|
882 | struct byte_range_lock *br_lck,
|
---|
883 | const struct lock_struct *plock)
|
---|
884 | {
|
---|
885 | unsigned int i, j;
|
---|
886 | struct lock_struct *locks = br_lck->lock_data;
|
---|
887 | enum brl_type deleted_lock_type = READ_LOCK; /* shut the compiler up.... */
|
---|
888 |
|
---|
889 | #if ZERO_ZERO
|
---|
890 | /* Delete write locks by preference... The lock list
|
---|
891 | is sorted in the zero zero case. */
|
---|
892 |
|
---|
893 | for (i = 0; i < br_lck->num_locks; i++) {
|
---|
894 | struct lock_struct *lock = &locks[i];
|
---|
895 |
|
---|
896 | if (lock->lock_type == WRITE_LOCK &&
|
---|
897 | brl_same_context(&lock->context, &plock->context) &&
|
---|
898 | lock->fnum == plock->fnum &&
|
---|
899 | lock->lock_flav == WINDOWS_LOCK &&
|
---|
900 | lock->start == plock->start &&
|
---|
901 | lock->size == plock->size) {
|
---|
902 |
|
---|
903 | /* found it - delete it */
|
---|
904 | deleted_lock_type = lock->lock_type;
|
---|
905 | break;
|
---|
906 | }
|
---|
907 | }
|
---|
908 |
|
---|
909 | if (i != br_lck->num_locks) {
|
---|
910 | /* We found it - don't search again. */
|
---|
911 | goto unlock_continue;
|
---|
912 | }
|
---|
913 | #endif
|
---|
914 |
|
---|
915 | for (i = 0; i < br_lck->num_locks; i++) {
|
---|
916 | struct lock_struct *lock = &locks[i];
|
---|
917 |
|
---|
918 | /* Only remove our own locks that match in start, size, and flavour. */
|
---|
919 | if (brl_same_context(&lock->context, &plock->context) &&
|
---|
920 | lock->fnum == plock->fnum &&
|
---|
921 | lock->lock_flav == WINDOWS_LOCK &&
|
---|
922 | lock->start == plock->start &&
|
---|
923 | lock->size == plock->size ) {
|
---|
924 | deleted_lock_type = lock->lock_type;
|
---|
925 | break;
|
---|
926 | }
|
---|
927 | }
|
---|
928 |
|
---|
929 | if (i == br_lck->num_locks) {
|
---|
930 | /* we didn't find it */
|
---|
931 | return False;
|
---|
932 | }
|
---|
933 |
|
---|
934 | #if ZERO_ZERO
|
---|
935 | unlock_continue:
|
---|
936 | #endif
|
---|
937 |
|
---|
938 | /* Actually delete the lock. */
|
---|
939 | if (i < br_lck->num_locks - 1) {
|
---|
940 | memmove(&locks[i], &locks[i+1],
|
---|
941 | sizeof(*locks)*((br_lck->num_locks-1) - i));
|
---|
942 | }
|
---|
943 |
|
---|
944 | br_lck->num_locks -= 1;
|
---|
945 | br_lck->modified = True;
|
---|
946 |
|
---|
947 | /* Unlock the underlying POSIX regions. */
|
---|
948 | if(lp_posix_locking(br_lck->fsp->conn->params)) {
|
---|
949 | release_posix_lock_windows_flavour(br_lck->fsp,
|
---|
950 | plock->start,
|
---|
951 | plock->size,
|
---|
952 | deleted_lock_type,
|
---|
953 | &plock->context,
|
---|
954 | locks,
|
---|
955 | br_lck->num_locks);
|
---|
956 | }
|
---|
957 |
|
---|
958 | /* Send unlock messages to any pending waiters that overlap. */
|
---|
959 | for (j=0; j < br_lck->num_locks; j++) {
|
---|
960 | struct lock_struct *pend_lock = &locks[j];
|
---|
961 |
|
---|
962 | /* Ignore non-pending locks. */
|
---|
963 | if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
|
---|
964 | continue;
|
---|
965 | }
|
---|
966 |
|
---|
967 | /* We could send specific lock info here... */
|
---|
968 | if (brl_pending_overlap(plock, pend_lock)) {
|
---|
969 | DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
|
---|
970 | procid_str_static(&pend_lock->context.pid )));
|
---|
971 |
|
---|
972 | messaging_send(msg_ctx, pend_lock->context.pid,
|
---|
973 | MSG_SMB_UNLOCK, &data_blob_null);
|
---|
974 | }
|
---|
975 | }
|
---|
976 |
|
---|
977 | return True;
|
---|
978 | }
|
---|
979 |
|
---|
980 | /****************************************************************************
|
---|
981 | Unlock a range of bytes - POSIX semantics.
|
---|
982 | ****************************************************************************/
|
---|
983 |
|
---|
984 | static bool brl_unlock_posix(struct messaging_context *msg_ctx,
|
---|
985 | struct byte_range_lock *br_lck,
|
---|
986 | struct lock_struct *plock)
|
---|
987 | {
|
---|
988 | unsigned int i, j, count;
|
---|
989 | struct lock_struct *tp;
|
---|
990 | struct lock_struct *locks = br_lck->lock_data;
|
---|
991 | bool overlap_found = False;
|
---|
992 |
|
---|
993 | /* No zero-zero locks for POSIX. */
|
---|
994 | if (plock->start == 0 && plock->size == 0) {
|
---|
995 | return False;
|
---|
996 | }
|
---|
997 |
|
---|
998 | /* Don't allow 64-bit lock wrap. */
|
---|
999 | if (plock->start + plock->size < plock->start ||
|
---|
1000 | plock->start + plock->size < plock->size) {
|
---|
1001 | DEBUG(10,("brl_unlock_posix: lock wrap\n"));
|
---|
1002 | return False;
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | /* The worst case scenario here is we have to split an
|
---|
1006 | existing POSIX lock range into two, so we need at most
|
---|
1007 | 1 more entry. */
|
---|
1008 |
|
---|
1009 | tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 1));
|
---|
1010 | if (!tp) {
|
---|
1011 | DEBUG(10,("brl_unlock_posix: malloc fail\n"));
|
---|
1012 | return False;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | count = 0;
|
---|
1016 | for (i = 0; i < br_lck->num_locks; i++) {
|
---|
1017 | struct lock_struct *lock = &locks[i];
|
---|
1018 | unsigned int tmp_count;
|
---|
1019 |
|
---|
1020 | /* Only remove our own locks - ignore fnum. */
|
---|
1021 | if (IS_PENDING_LOCK(lock->lock_type) ||
|
---|
1022 | !brl_same_context(&lock->context, &plock->context)) {
|
---|
1023 | memcpy(&tp[count], lock, sizeof(struct lock_struct));
|
---|
1024 | count++;
|
---|
1025 | continue;
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | if (lock->lock_flav == WINDOWS_LOCK) {
|
---|
1029 | /* Do any Windows flavour locks conflict ? */
|
---|
1030 | if (brl_conflict(lock, plock)) {
|
---|
1031 | SAFE_FREE(tp);
|
---|
1032 | return false;
|
---|
1033 | }
|
---|
1034 | /* Just copy the Windows lock into the new array. */
|
---|
1035 | memcpy(&tp[count], lock, sizeof(struct lock_struct));
|
---|
1036 | count++;
|
---|
1037 | continue;
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | /* Work out overlaps. */
|
---|
1041 | tmp_count = brlock_posix_split_merge(&tp[count], lock, plock);
|
---|
1042 |
|
---|
1043 | if (tmp_count == 0) {
|
---|
1044 | /* plock overlapped the existing lock completely,
|
---|
1045 | or replaced it. Don't copy the existing lock. */
|
---|
1046 | overlap_found = true;
|
---|
1047 | } else if (tmp_count == 1) {
|
---|
1048 | /* Either no overlap, (simple copy of existing lock) or
|
---|
1049 | * an overlap of an existing lock. */
|
---|
1050 | /* If the lock changed size, we had an overlap. */
|
---|
1051 | if (tp[count].size != lock->size) {
|
---|
1052 | overlap_found = true;
|
---|
1053 | }
|
---|
1054 | count += tmp_count;
|
---|
1055 | } else if (tmp_count == 2) {
|
---|
1056 | /* We split a lock range in two. */
|
---|
1057 | overlap_found = true;
|
---|
1058 | count += tmp_count;
|
---|
1059 |
|
---|
1060 | /* Optimisation... */
|
---|
1061 | /* We know we're finished here as we can't overlap any
|
---|
1062 | more POSIX locks. Copy the rest of the lock array. */
|
---|
1063 |
|
---|
1064 | if (i < br_lck->num_locks - 1) {
|
---|
1065 | memcpy(&tp[count], &locks[i+1],
|
---|
1066 | sizeof(*locks)*((br_lck->num_locks-1) - i));
|
---|
1067 | count += ((br_lck->num_locks-1) - i);
|
---|
1068 | }
|
---|
1069 | break;
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | if (!overlap_found) {
|
---|
1075 | /* Just ignore - no change. */
|
---|
1076 | SAFE_FREE(tp);
|
---|
1077 | DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
|
---|
1078 | return True;
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | /* Unlock any POSIX regions. */
|
---|
1082 | if(lp_posix_locking(br_lck->fsp->conn->params)) {
|
---|
1083 | release_posix_lock_posix_flavour(br_lck->fsp,
|
---|
1084 | plock->start,
|
---|
1085 | plock->size,
|
---|
1086 | &plock->context,
|
---|
1087 | tp,
|
---|
1088 | count);
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | /* Realloc so we don't leak entries per unlock call. */
|
---|
1092 | if (count) {
|
---|
1093 | tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
|
---|
1094 | if (!tp) {
|
---|
1095 | DEBUG(10,("brl_unlock_posix: realloc fail\n"));
|
---|
1096 | return False;
|
---|
1097 | }
|
---|
1098 | } else {
|
---|
1099 | /* We deleted the last lock. */
|
---|
1100 | SAFE_FREE(tp);
|
---|
1101 | tp = NULL;
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | br_lck->num_locks = count;
|
---|
1105 | SAFE_FREE(br_lck->lock_data);
|
---|
1106 | locks = tp;
|
---|
1107 | br_lck->lock_data = tp;
|
---|
1108 | br_lck->modified = True;
|
---|
1109 |
|
---|
1110 | /* Send unlock messages to any pending waiters that overlap. */
|
---|
1111 |
|
---|
1112 | for (j=0; j < br_lck->num_locks; j++) {
|
---|
1113 | struct lock_struct *pend_lock = &locks[j];
|
---|
1114 |
|
---|
1115 | /* Ignore non-pending locks. */
|
---|
1116 | if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
|
---|
1117 | continue;
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | /* We could send specific lock info here... */
|
---|
1121 | if (brl_pending_overlap(plock, pend_lock)) {
|
---|
1122 | DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
|
---|
1123 | procid_str_static(&pend_lock->context.pid )));
|
---|
1124 |
|
---|
1125 | messaging_send(msg_ctx, pend_lock->context.pid,
|
---|
1126 | MSG_SMB_UNLOCK, &data_blob_null);
|
---|
1127 | }
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | return True;
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | /****************************************************************************
|
---|
1134 | Unlock a range of bytes.
|
---|
1135 | ****************************************************************************/
|
---|
1136 |
|
---|
1137 | bool brl_unlock(struct messaging_context *msg_ctx,
|
---|
1138 | struct byte_range_lock *br_lck,
|
---|
1139 | uint32 smbpid,
|
---|
1140 | struct server_id pid,
|
---|
1141 | br_off start,
|
---|
1142 | br_off size,
|
---|
1143 | enum brl_flavour lock_flav)
|
---|
1144 | {
|
---|
1145 | struct lock_struct lock;
|
---|
1146 |
|
---|
1147 | lock.context.smbpid = smbpid;
|
---|
1148 | lock.context.pid = pid;
|
---|
1149 | lock.context.tid = br_lck->fsp->conn->cnum;
|
---|
1150 | lock.start = start;
|
---|
1151 | lock.size = size;
|
---|
1152 | lock.fnum = br_lck->fsp->fnum;
|
---|
1153 | lock.lock_type = UNLOCK_LOCK;
|
---|
1154 | lock.lock_flav = lock_flav;
|
---|
1155 |
|
---|
1156 | if (lock_flav == WINDOWS_LOCK) {
|
---|
1157 | return brl_unlock_windows(msg_ctx, br_lck, &lock);
|
---|
1158 | } else {
|
---|
1159 | return brl_unlock_posix(msg_ctx, br_lck, &lock);
|
---|
1160 | }
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | /****************************************************************************
|
---|
1164 | Test if we could add a lock if we wanted to.
|
---|
1165 | Returns True if the region required is currently unlocked, False if locked.
|
---|
1166 | ****************************************************************************/
|
---|
1167 |
|
---|
1168 | bool brl_locktest(struct byte_range_lock *br_lck,
|
---|
1169 | uint32 smbpid,
|
---|
1170 | struct server_id pid,
|
---|
1171 | br_off start,
|
---|
1172 | br_off size,
|
---|
1173 | enum brl_type lock_type,
|
---|
1174 | enum brl_flavour lock_flav)
|
---|
1175 | {
|
---|
1176 | bool ret = True;
|
---|
1177 | unsigned int i;
|
---|
1178 | struct lock_struct lock;
|
---|
1179 | const struct lock_struct *locks = br_lck->lock_data;
|
---|
1180 | files_struct *fsp = br_lck->fsp;
|
---|
1181 |
|
---|
1182 | lock.context.smbpid = smbpid;
|
---|
1183 | lock.context.pid = pid;
|
---|
1184 | lock.context.tid = br_lck->fsp->conn->cnum;
|
---|
1185 | lock.start = start;
|
---|
1186 | lock.size = size;
|
---|
1187 | lock.fnum = fsp->fnum;
|
---|
1188 | lock.lock_type = lock_type;
|
---|
1189 | lock.lock_flav = lock_flav;
|
---|
1190 |
|
---|
1191 | /* Make sure existing locks don't conflict */
|
---|
1192 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
1193 | /*
|
---|
1194 | * Our own locks don't conflict.
|
---|
1195 | */
|
---|
1196 | if (brl_conflict_other(&locks[i], &lock)) {
|
---|
1197 | return False;
|
---|
1198 | }
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | /*
|
---|
1202 | * There is no lock held by an SMB daemon, check to
|
---|
1203 | * see if there is a POSIX lock from a UNIX or NFS process.
|
---|
1204 | * This only conflicts with Windows locks, not POSIX locks.
|
---|
1205 | */
|
---|
1206 |
|
---|
1207 | if(lp_posix_locking(fsp->conn->params) && (lock_flav == WINDOWS_LOCK)) {
|
---|
1208 | ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
|
---|
1209 |
|
---|
1210 | DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
|
---|
1211 | (double)start, (double)size, ret ? "locked" : "unlocked",
|
---|
1212 | fsp->fnum, fsp->fsp_name ));
|
---|
1213 |
|
---|
1214 | /* We need to return the inverse of is_posix_locked. */
|
---|
1215 | ret = !ret;
|
---|
1216 | }
|
---|
1217 |
|
---|
1218 | /* no conflicts - we could have added it */
|
---|
1219 | return ret;
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | /****************************************************************************
|
---|
1223 | Query for existing locks.
|
---|
1224 | ****************************************************************************/
|
---|
1225 |
|
---|
1226 | NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
|
---|
1227 | uint32 *psmbpid,
|
---|
1228 | struct server_id pid,
|
---|
1229 | br_off *pstart,
|
---|
1230 | br_off *psize,
|
---|
1231 | enum brl_type *plock_type,
|
---|
1232 | enum brl_flavour lock_flav)
|
---|
1233 | {
|
---|
1234 | unsigned int i;
|
---|
1235 | struct lock_struct lock;
|
---|
1236 | const struct lock_struct *locks = br_lck->lock_data;
|
---|
1237 | files_struct *fsp = br_lck->fsp;
|
---|
1238 |
|
---|
1239 | lock.context.smbpid = *psmbpid;
|
---|
1240 | lock.context.pid = pid;
|
---|
1241 | lock.context.tid = br_lck->fsp->conn->cnum;
|
---|
1242 | lock.start = *pstart;
|
---|
1243 | lock.size = *psize;
|
---|
1244 | lock.fnum = fsp->fnum;
|
---|
1245 | lock.lock_type = *plock_type;
|
---|
1246 | lock.lock_flav = lock_flav;
|
---|
1247 |
|
---|
1248 | /* Make sure existing locks don't conflict */
|
---|
1249 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
1250 | const struct lock_struct *exlock = &locks[i];
|
---|
1251 | bool conflict = False;
|
---|
1252 |
|
---|
1253 | if (exlock->lock_flav == WINDOWS_LOCK) {
|
---|
1254 | conflict = brl_conflict(exlock, &lock);
|
---|
1255 | } else {
|
---|
1256 | conflict = brl_conflict_posix(exlock, &lock);
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | if (conflict) {
|
---|
1260 | *psmbpid = exlock->context.smbpid;
|
---|
1261 | *pstart = exlock->start;
|
---|
1262 | *psize = exlock->size;
|
---|
1263 | *plock_type = exlock->lock_type;
|
---|
1264 | return NT_STATUS_LOCK_NOT_GRANTED;
|
---|
1265 | }
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | /*
|
---|
1269 | * There is no lock held by an SMB daemon, check to
|
---|
1270 | * see if there is a POSIX lock from a UNIX or NFS process.
|
---|
1271 | */
|
---|
1272 |
|
---|
1273 | if(lp_posix_locking(fsp->conn->params)) {
|
---|
1274 | bool ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
|
---|
1275 |
|
---|
1276 | DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
|
---|
1277 | (double)*pstart, (double)*psize, ret ? "locked" : "unlocked",
|
---|
1278 | fsp->fnum, fsp->fsp_name ));
|
---|
1279 |
|
---|
1280 | if (ret) {
|
---|
1281 | /* Hmmm. No clue what to set smbpid to - use -1. */
|
---|
1282 | *psmbpid = 0xFFFF;
|
---|
1283 | return NT_STATUS_LOCK_NOT_GRANTED;
|
---|
1284 | }
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | return NT_STATUS_OK;
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | /****************************************************************************
|
---|
1291 | Remove a particular pending lock.
|
---|
1292 | ****************************************************************************/
|
---|
1293 |
|
---|
1294 | bool brl_lock_cancel(struct byte_range_lock *br_lck,
|
---|
1295 | uint32 smbpid,
|
---|
1296 | struct server_id pid,
|
---|
1297 | br_off start,
|
---|
1298 | br_off size,
|
---|
1299 | enum brl_flavour lock_flav)
|
---|
1300 | {
|
---|
1301 | unsigned int i;
|
---|
1302 | struct lock_struct *locks = br_lck->lock_data;
|
---|
1303 | struct lock_context context;
|
---|
1304 |
|
---|
1305 | context.smbpid = smbpid;
|
---|
1306 | context.pid = pid;
|
---|
1307 | context.tid = br_lck->fsp->conn->cnum;
|
---|
1308 |
|
---|
1309 | for (i = 0; i < br_lck->num_locks; i++) {
|
---|
1310 | struct lock_struct *lock = &locks[i];
|
---|
1311 |
|
---|
1312 | /* For pending locks we *always* care about the fnum. */
|
---|
1313 | if (brl_same_context(&lock->context, &context) &&
|
---|
1314 | lock->fnum == br_lck->fsp->fnum &&
|
---|
1315 | IS_PENDING_LOCK(lock->lock_type) &&
|
---|
1316 | lock->lock_flav == lock_flav &&
|
---|
1317 | lock->start == start &&
|
---|
1318 | lock->size == size) {
|
---|
1319 | break;
|
---|
1320 | }
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 | if (i == br_lck->num_locks) {
|
---|
1324 | /* Didn't find it. */
|
---|
1325 | return False;
|
---|
1326 | }
|
---|
1327 |
|
---|
1328 | if (i < br_lck->num_locks - 1) {
|
---|
1329 | /* Found this particular pending lock - delete it */
|
---|
1330 | memmove(&locks[i], &locks[i+1],
|
---|
1331 | sizeof(*locks)*((br_lck->num_locks-1) - i));
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 | br_lck->num_locks -= 1;
|
---|
1335 | br_lck->modified = True;
|
---|
1336 | return True;
|
---|
1337 | }
|
---|
1338 |
|
---|
1339 | /****************************************************************************
|
---|
1340 | Remove any locks associated with a open file.
|
---|
1341 | We return True if this process owns any other Windows locks on this
|
---|
1342 | fd and so we should not immediately close the fd.
|
---|
1343 | ****************************************************************************/
|
---|
1344 |
|
---|
1345 | void brl_close_fnum(struct messaging_context *msg_ctx,
|
---|
1346 | struct byte_range_lock *br_lck)
|
---|
1347 | {
|
---|
1348 | files_struct *fsp = br_lck->fsp;
|
---|
1349 | uint16 tid = fsp->conn->cnum;
|
---|
1350 | int fnum = fsp->fnum;
|
---|
1351 | unsigned int i, j, dcount=0;
|
---|
1352 | int num_deleted_windows_locks = 0;
|
---|
1353 | struct lock_struct *locks = br_lck->lock_data;
|
---|
1354 | struct server_id pid = procid_self();
|
---|
1355 | bool unlock_individually = False;
|
---|
1356 |
|
---|
1357 | if(lp_posix_locking(fsp->conn->params)) {
|
---|
1358 |
|
---|
1359 | /* Check if there are any Windows locks associated with this dev/ino
|
---|
1360 | pair that are not this fnum. If so we need to call unlock on each
|
---|
1361 | one in order to release the system POSIX locks correctly. */
|
---|
1362 |
|
---|
1363 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
1364 | struct lock_struct *lock = &locks[i];
|
---|
1365 |
|
---|
1366 | if (!procid_equal(&lock->context.pid, &pid)) {
|
---|
1367 | continue;
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | if (lock->lock_type != READ_LOCK && lock->lock_type != WRITE_LOCK) {
|
---|
1371 | continue; /* Ignore pending. */
|
---|
1372 | }
|
---|
1373 |
|
---|
1374 | if (lock->context.tid != tid || lock->fnum != fnum) {
|
---|
1375 | unlock_individually = True;
|
---|
1376 | break;
|
---|
1377 | }
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | if (unlock_individually) {
|
---|
1381 | struct lock_struct *locks_copy;
|
---|
1382 | unsigned int num_locks_copy;
|
---|
1383 |
|
---|
1384 | /* Copy the current lock array. */
|
---|
1385 | if (br_lck->num_locks) {
|
---|
1386 | locks_copy = (struct lock_struct *)TALLOC_MEMDUP(br_lck, locks, br_lck->num_locks * sizeof(struct lock_struct));
|
---|
1387 | if (!locks_copy) {
|
---|
1388 | smb_panic("brl_close_fnum: talloc failed");
|
---|
1389 | }
|
---|
1390 | } else {
|
---|
1391 | locks_copy = NULL;
|
---|
1392 | }
|
---|
1393 |
|
---|
1394 | num_locks_copy = br_lck->num_locks;
|
---|
1395 |
|
---|
1396 | for (i=0; i < num_locks_copy; i++) {
|
---|
1397 | struct lock_struct *lock = &locks_copy[i];
|
---|
1398 |
|
---|
1399 | if (lock->context.tid == tid && procid_equal(&lock->context.pid, &pid) &&
|
---|
1400 | (lock->fnum == fnum)) {
|
---|
1401 | brl_unlock(msg_ctx,
|
---|
1402 | br_lck,
|
---|
1403 | lock->context.smbpid,
|
---|
1404 | pid,
|
---|
1405 | lock->start,
|
---|
1406 | lock->size,
|
---|
1407 | lock->lock_flav);
|
---|
1408 | }
|
---|
1409 | }
|
---|
1410 | return;
|
---|
1411 | }
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 | /* We can bulk delete - any POSIX locks will be removed when the fd closes. */
|
---|
1415 |
|
---|
1416 | /* Remove any existing locks for this fnum (or any fnum if they're POSIX). */
|
---|
1417 |
|
---|
1418 | for (i=0; i < br_lck->num_locks; i++) {
|
---|
1419 | struct lock_struct *lock = &locks[i];
|
---|
1420 | bool del_this_lock = False;
|
---|
1421 |
|
---|
1422 | if (lock->context.tid == tid && procid_equal(&lock->context.pid, &pid)) {
|
---|
1423 | if ((lock->lock_flav == WINDOWS_LOCK) && (lock->fnum == fnum)) {
|
---|
1424 | del_this_lock = True;
|
---|
1425 | num_deleted_windows_locks++;
|
---|
1426 | } else if (lock->lock_flav == POSIX_LOCK) {
|
---|
1427 | del_this_lock = True;
|
---|
1428 | }
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | if (del_this_lock) {
|
---|
1432 | /* Send unlock messages to any pending waiters that overlap. */
|
---|
1433 | for (j=0; j < br_lck->num_locks; j++) {
|
---|
1434 | struct lock_struct *pend_lock = &locks[j];
|
---|
1435 |
|
---|
1436 | /* Ignore our own or non-pending locks. */
|
---|
1437 | if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
|
---|
1438 | continue;
|
---|
1439 | }
|
---|
1440 |
|
---|
1441 | /* Optimisation - don't send to this fnum as we're
|
---|
1442 | closing it. */
|
---|
1443 | if (pend_lock->context.tid == tid &&
|
---|
1444 | procid_equal(&pend_lock->context.pid, &pid) &&
|
---|
1445 | pend_lock->fnum == fnum) {
|
---|
1446 | continue;
|
---|
1447 | }
|
---|
1448 |
|
---|
1449 | /* We could send specific lock info here... */
|
---|
1450 | if (brl_pending_overlap(lock, pend_lock)) {
|
---|
1451 | messaging_send(msg_ctx, pend_lock->context.pid,
|
---|
1452 | MSG_SMB_UNLOCK, &data_blob_null);
|
---|
1453 | }
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | /* found it - delete it */
|
---|
1457 | if (br_lck->num_locks > 1 && i < br_lck->num_locks - 1) {
|
---|
1458 | memmove(&locks[i], &locks[i+1],
|
---|
1459 | sizeof(*locks)*((br_lck->num_locks-1) - i));
|
---|
1460 | }
|
---|
1461 | br_lck->num_locks--;
|
---|
1462 | br_lck->modified = True;
|
---|
1463 | i--;
|
---|
1464 | dcount++;
|
---|
1465 | }
|
---|
1466 | }
|
---|
1467 |
|
---|
1468 | if(lp_posix_locking(fsp->conn->params) && num_deleted_windows_locks) {
|
---|
1469 | /* Reduce the Windows lock POSIX reference count on this dev/ino pair. */
|
---|
1470 | reduce_windows_lock_ref_count(fsp, num_deleted_windows_locks);
|
---|
1471 | }
|
---|
1472 | }
|
---|
1473 |
|
---|
1474 | /****************************************************************************
|
---|
1475 | Ensure this set of lock entries is valid.
|
---|
1476 | ****************************************************************************/
|
---|
1477 |
|
---|
1478 | static bool validate_lock_entries(unsigned int *pnum_entries, struct lock_struct **pplocks)
|
---|
1479 | {
|
---|
1480 | unsigned int i;
|
---|
1481 | unsigned int num_valid_entries = 0;
|
---|
1482 | struct lock_struct *locks = *pplocks;
|
---|
1483 |
|
---|
1484 | for (i = 0; i < *pnum_entries; i++) {
|
---|
1485 | struct lock_struct *lock_data = &locks[i];
|
---|
1486 | if (!process_exists(lock_data->context.pid)) {
|
---|
1487 | /* This process no longer exists - mark this
|
---|
1488 | entry as invalid by zeroing it. */
|
---|
1489 | ZERO_STRUCTP(lock_data);
|
---|
1490 | } else {
|
---|
1491 | num_valid_entries++;
|
---|
1492 | }
|
---|
1493 | }
|
---|
1494 |
|
---|
1495 | if (num_valid_entries != *pnum_entries) {
|
---|
1496 | struct lock_struct *new_lock_data = NULL;
|
---|
1497 |
|
---|
1498 | if (num_valid_entries) {
|
---|
1499 | new_lock_data = SMB_MALLOC_ARRAY(struct lock_struct, num_valid_entries);
|
---|
1500 | if (!new_lock_data) {
|
---|
1501 | DEBUG(3, ("malloc fail\n"));
|
---|
1502 | return False;
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | num_valid_entries = 0;
|
---|
1506 | for (i = 0; i < *pnum_entries; i++) {
|
---|
1507 | struct lock_struct *lock_data = &locks[i];
|
---|
1508 | if (lock_data->context.smbpid &&
|
---|
1509 | lock_data->context.tid) {
|
---|
1510 | /* Valid (nonzero) entry - copy it. */
|
---|
1511 | memcpy(&new_lock_data[num_valid_entries],
|
---|
1512 | lock_data, sizeof(struct lock_struct));
|
---|
1513 | num_valid_entries++;
|
---|
1514 | }
|
---|
1515 | }
|
---|
1516 | }
|
---|
1517 |
|
---|
1518 | SAFE_FREE(*pplocks);
|
---|
1519 | *pplocks = new_lock_data;
|
---|
1520 | *pnum_entries = num_valid_entries;
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | return True;
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 | struct brl_forall_cb {
|
---|
1527 | void (*fn)(struct file_id id, struct server_id pid,
|
---|
1528 | enum brl_type lock_type,
|
---|
1529 | enum brl_flavour lock_flav,
|
---|
1530 | br_off start, br_off size,
|
---|
1531 | void *private_data);
|
---|
1532 | void *private_data;
|
---|
1533 | };
|
---|
1534 |
|
---|
1535 | /****************************************************************************
|
---|
1536 | Traverse the whole database with this function, calling traverse_callback
|
---|
1537 | on each lock.
|
---|
1538 | ****************************************************************************/
|
---|
1539 |
|
---|
1540 | static int traverse_fn(struct db_record *rec, void *state)
|
---|
1541 | {
|
---|
1542 | struct brl_forall_cb *cb = (struct brl_forall_cb *)state;
|
---|
1543 | struct lock_struct *locks;
|
---|
1544 | struct file_id *key;
|
---|
1545 | unsigned int i;
|
---|
1546 | unsigned int num_locks = 0;
|
---|
1547 | unsigned int orig_num_locks = 0;
|
---|
1548 |
|
---|
1549 | /* In a traverse function we must make a copy of
|
---|
1550 | dbuf before modifying it. */
|
---|
1551 |
|
---|
1552 | locks = (struct lock_struct *)memdup(rec->value.dptr,
|
---|
1553 | rec->value.dsize);
|
---|
1554 | if (!locks) {
|
---|
1555 | return -1; /* Terminate traversal. */
|
---|
1556 | }
|
---|
1557 |
|
---|
1558 | key = (struct file_id *)rec->key.dptr;
|
---|
1559 | orig_num_locks = num_locks = rec->value.dsize/sizeof(*locks);
|
---|
1560 |
|
---|
1561 | /* Ensure the lock db is clean of entries from invalid processes. */
|
---|
1562 |
|
---|
1563 | if (!validate_lock_entries(&num_locks, &locks)) {
|
---|
1564 | SAFE_FREE(locks);
|
---|
1565 | return -1; /* Terminate traversal */
|
---|
1566 | }
|
---|
1567 |
|
---|
1568 | if (orig_num_locks != num_locks) {
|
---|
1569 | if (num_locks) {
|
---|
1570 | TDB_DATA data;
|
---|
1571 | data.dptr = (uint8_t *)locks;
|
---|
1572 | data.dsize = num_locks*sizeof(struct lock_struct);
|
---|
1573 | rec->store(rec, data, TDB_REPLACE);
|
---|
1574 | } else {
|
---|
1575 | rec->delete_rec(rec);
|
---|
1576 | }
|
---|
1577 | }
|
---|
1578 |
|
---|
1579 | if (cb->fn) {
|
---|
1580 | for ( i=0; i<num_locks; i++) {
|
---|
1581 | cb->fn(*key,
|
---|
1582 | locks[i].context.pid,
|
---|
1583 | locks[i].lock_type,
|
---|
1584 | locks[i].lock_flav,
|
---|
1585 | locks[i].start,
|
---|
1586 | locks[i].size,
|
---|
1587 | cb->private_data);
|
---|
1588 | }
|
---|
1589 | }
|
---|
1590 |
|
---|
1591 | SAFE_FREE(locks);
|
---|
1592 | return 0;
|
---|
1593 | }
|
---|
1594 |
|
---|
1595 | /*******************************************************************
|
---|
1596 | Call the specified function on each lock in the database.
|
---|
1597 | ********************************************************************/
|
---|
1598 |
|
---|
1599 | int brl_forall(void (*fn)(struct file_id id, struct server_id pid,
|
---|
1600 | enum brl_type lock_type,
|
---|
1601 | enum brl_flavour lock_flav,
|
---|
1602 | br_off start, br_off size,
|
---|
1603 | void *private_data),
|
---|
1604 | void *private_data)
|
---|
1605 | {
|
---|
1606 | struct brl_forall_cb cb;
|
---|
1607 |
|
---|
1608 | if (!brlock_db) {
|
---|
1609 | return 0;
|
---|
1610 | }
|
---|
1611 | cb.fn = fn;
|
---|
1612 | cb.private_data = private_data;
|
---|
1613 | return brlock_db->traverse(brlock_db, traverse_fn, &cb);
|
---|
1614 | }
|
---|
1615 |
|
---|
1616 | /*******************************************************************
|
---|
1617 | Store a potentially modified set of byte range lock data back into
|
---|
1618 | the database.
|
---|
1619 | Unlock the record.
|
---|
1620 | ********************************************************************/
|
---|
1621 |
|
---|
1622 | static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
|
---|
1623 | {
|
---|
1624 | if (br_lck->read_only) {
|
---|
1625 | SMB_ASSERT(!br_lck->modified);
|
---|
1626 | }
|
---|
1627 |
|
---|
1628 | if (!br_lck->modified) {
|
---|
1629 | goto done;
|
---|
1630 | }
|
---|
1631 |
|
---|
1632 | if (br_lck->num_locks == 0) {
|
---|
1633 | /* No locks - delete this entry. */
|
---|
1634 | NTSTATUS status = br_lck->record->delete_rec(br_lck->record);
|
---|
1635 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1636 | DEBUG(0, ("delete_rec returned %s\n",
|
---|
1637 | nt_errstr(status)));
|
---|
1638 | smb_panic("Could not delete byte range lock entry");
|
---|
1639 | }
|
---|
1640 | } else {
|
---|
1641 | TDB_DATA data;
|
---|
1642 | NTSTATUS status;
|
---|
1643 |
|
---|
1644 | data.dptr = (uint8 *)br_lck->lock_data;
|
---|
1645 | data.dsize = br_lck->num_locks * sizeof(struct lock_struct);
|
---|
1646 |
|
---|
1647 | status = br_lck->record->store(br_lck->record, data,
|
---|
1648 | TDB_REPLACE);
|
---|
1649 | if (!NT_STATUS_IS_OK(status)) {
|
---|
1650 | DEBUG(0, ("store returned %s\n", nt_errstr(status)));
|
---|
1651 | smb_panic("Could not store byte range mode entry");
|
---|
1652 | }
|
---|
1653 | }
|
---|
1654 |
|
---|
1655 | done:
|
---|
1656 |
|
---|
1657 | SAFE_FREE(br_lck->lock_data);
|
---|
1658 | TALLOC_FREE(br_lck->record);
|
---|
1659 | return 0;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | /*******************************************************************
|
---|
1663 | Fetch a set of byte range lock data from the database.
|
---|
1664 | Leave the record locked.
|
---|
1665 | TALLOC_FREE(brl) will release the lock in the destructor.
|
---|
1666 | ********************************************************************/
|
---|
1667 |
|
---|
1668 | static struct byte_range_lock *brl_get_locks_internal(TALLOC_CTX *mem_ctx,
|
---|
1669 | files_struct *fsp, bool read_only)
|
---|
1670 | {
|
---|
1671 | TDB_DATA key, data;
|
---|
1672 | struct byte_range_lock *br_lck = TALLOC_P(mem_ctx, struct byte_range_lock);
|
---|
1673 |
|
---|
1674 | if (br_lck == NULL) {
|
---|
1675 | return NULL;
|
---|
1676 | }
|
---|
1677 |
|
---|
1678 | br_lck->fsp = fsp;
|
---|
1679 | br_lck->num_locks = 0;
|
---|
1680 | br_lck->modified = False;
|
---|
1681 | memset(&br_lck->key, '\0', sizeof(struct file_id));
|
---|
1682 | br_lck->key = fsp->file_id;
|
---|
1683 |
|
---|
1684 | key.dptr = (uint8 *)&br_lck->key;
|
---|
1685 | key.dsize = sizeof(struct file_id);
|
---|
1686 |
|
---|
1687 | if (!fsp->lockdb_clean) {
|
---|
1688 | /* We must be read/write to clean
|
---|
1689 | the dead entries. */
|
---|
1690 | read_only = False;
|
---|
1691 | }
|
---|
1692 |
|
---|
1693 | if (read_only) {
|
---|
1694 | if (brlock_db->fetch(brlock_db, br_lck, key, &data) == -1) {
|
---|
1695 | DEBUG(3, ("Could not fetch byte range lock record\n"));
|
---|
1696 | TALLOC_FREE(br_lck);
|
---|
1697 | return NULL;
|
---|
1698 | }
|
---|
1699 | br_lck->record = NULL;
|
---|
1700 | }
|
---|
1701 | else {
|
---|
1702 | br_lck->record = brlock_db->fetch_locked(brlock_db, br_lck, key);
|
---|
1703 |
|
---|
1704 | if (br_lck->record == NULL) {
|
---|
1705 | DEBUG(3, ("Could not lock byte range lock entry\n"));
|
---|
1706 | TALLOC_FREE(br_lck);
|
---|
1707 | return NULL;
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | data = br_lck->record->value;
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 | br_lck->read_only = read_only;
|
---|
1714 | br_lck->lock_data = NULL;
|
---|
1715 |
|
---|
1716 | talloc_set_destructor(br_lck, byte_range_lock_destructor);
|
---|
1717 |
|
---|
1718 | br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
|
---|
1719 |
|
---|
1720 | if (br_lck->num_locks != 0) {
|
---|
1721 | br_lck->lock_data = SMB_MALLOC_ARRAY(struct lock_struct,
|
---|
1722 | br_lck->num_locks);
|
---|
1723 | if (br_lck->lock_data == NULL) {
|
---|
1724 | DEBUG(0, ("malloc failed\n"));
|
---|
1725 | TALLOC_FREE(br_lck);
|
---|
1726 | return NULL;
|
---|
1727 | }
|
---|
1728 |
|
---|
1729 | memcpy(br_lck->lock_data, data.dptr, data.dsize);
|
---|
1730 | }
|
---|
1731 |
|
---|
1732 | if (!fsp->lockdb_clean) {
|
---|
1733 | int orig_num_locks = br_lck->num_locks;
|
---|
1734 |
|
---|
1735 | /* This is the first time we've accessed this. */
|
---|
1736 | /* Go through and ensure all entries exist - remove any that don't. */
|
---|
1737 | /* Makes the lockdb self cleaning at low cost. */
|
---|
1738 |
|
---|
1739 | if (!validate_lock_entries(&br_lck->num_locks,
|
---|
1740 | &br_lck->lock_data)) {
|
---|
1741 | SAFE_FREE(br_lck->lock_data);
|
---|
1742 | TALLOC_FREE(br_lck);
|
---|
1743 | return NULL;
|
---|
1744 | }
|
---|
1745 |
|
---|
1746 | /* Ensure invalid locks are cleaned up in the destructor. */
|
---|
1747 | if (orig_num_locks != br_lck->num_locks) {
|
---|
1748 | br_lck->modified = True;
|
---|
1749 | }
|
---|
1750 |
|
---|
1751 | /* Mark the lockdb as "clean" as seen from this open file. */
|
---|
1752 | fsp->lockdb_clean = True;
|
---|
1753 | }
|
---|
1754 |
|
---|
1755 | if (DEBUGLEVEL >= 10) {
|
---|
1756 | unsigned int i;
|
---|
1757 | struct lock_struct *locks = br_lck->lock_data;
|
---|
1758 | DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
|
---|
1759 | br_lck->num_locks,
|
---|
1760 | file_id_string_tos(&fsp->file_id)));
|
---|
1761 | for( i = 0; i < br_lck->num_locks; i++) {
|
---|
1762 | print_lock_struct(i, &locks[i]);
|
---|
1763 | }
|
---|
1764 | }
|
---|
1765 | return br_lck;
|
---|
1766 | }
|
---|
1767 |
|
---|
1768 | struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx,
|
---|
1769 | files_struct *fsp)
|
---|
1770 | {
|
---|
1771 | return brl_get_locks_internal(mem_ctx, fsp, False);
|
---|
1772 | }
|
---|
1773 |
|
---|
1774 | struct byte_range_lock *brl_get_locks_readonly(TALLOC_CTX *mem_ctx,
|
---|
1775 | files_struct *fsp)
|
---|
1776 | {
|
---|
1777 | return brl_get_locks_internal(mem_ctx, fsp, True);
|
---|
1778 | }
|
---|
1779 |
|
---|
1780 | struct brl_revalidate_state {
|
---|
1781 | ssize_t array_size;
|
---|
1782 | uint32 num_pids;
|
---|
1783 | struct server_id *pids;
|
---|
1784 | };
|
---|
1785 |
|
---|
1786 | /*
|
---|
1787 | * Collect PIDs of all processes with pending entries
|
---|
1788 | */
|
---|
1789 |
|
---|
1790 | static void brl_revalidate_collect(struct file_id id, struct server_id pid,
|
---|
1791 | enum brl_type lock_type,
|
---|
1792 | enum brl_flavour lock_flav,
|
---|
1793 | br_off start, br_off size,
|
---|
1794 | void *private_data)
|
---|
1795 | {
|
---|
1796 | struct brl_revalidate_state *state =
|
---|
1797 | (struct brl_revalidate_state *)private_data;
|
---|
1798 |
|
---|
1799 | if (!IS_PENDING_LOCK(lock_type)) {
|
---|
1800 | return;
|
---|
1801 | }
|
---|
1802 |
|
---|
1803 | add_to_large_array(state, sizeof(pid), (void *)&pid,
|
---|
1804 | &state->pids, &state->num_pids,
|
---|
1805 | &state->array_size);
|
---|
1806 | }
|
---|
1807 |
|
---|
1808 | /*
|
---|
1809 | * qsort callback to sort the processes
|
---|
1810 | */
|
---|
1811 |
|
---|
1812 | static int compare_procids(const void *p1, const void *p2)
|
---|
1813 | {
|
---|
1814 | const struct server_id *i1 = (struct server_id *)p1;
|
---|
1815 | const struct server_id *i2 = (struct server_id *)p2;
|
---|
1816 |
|
---|
1817 | if (i1->pid < i2->pid) return -1;
|
---|
1818 | if (i2->pid > i2->pid) return 1;
|
---|
1819 | return 0;
|
---|
1820 | }
|
---|
1821 |
|
---|
1822 | /*
|
---|
1823 | * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
|
---|
1824 | * locks so that they retry. Mainly used in the cluster code after a node has
|
---|
1825 | * died.
|
---|
1826 | *
|
---|
1827 | * Done in two steps to avoid double-sends: First we collect all entries in an
|
---|
1828 | * array, then qsort that array and only send to non-dupes.
|
---|
1829 | */
|
---|
1830 |
|
---|
1831 | static void brl_revalidate(struct messaging_context *msg_ctx,
|
---|
1832 | void *private_data,
|
---|
1833 | uint32_t msg_type,
|
---|
1834 | struct server_id server_id,
|
---|
1835 | DATA_BLOB *data)
|
---|
1836 | {
|
---|
1837 | struct brl_revalidate_state *state;
|
---|
1838 | uint32 i;
|
---|
1839 | struct server_id last_pid;
|
---|
1840 |
|
---|
1841 | if (!(state = TALLOC_ZERO_P(NULL, struct brl_revalidate_state))) {
|
---|
1842 | DEBUG(0, ("talloc failed\n"));
|
---|
1843 | return;
|
---|
1844 | }
|
---|
1845 |
|
---|
1846 | brl_forall(brl_revalidate_collect, state);
|
---|
1847 |
|
---|
1848 | if (state->array_size == -1) {
|
---|
1849 | DEBUG(0, ("talloc failed\n"));
|
---|
1850 | goto done;
|
---|
1851 | }
|
---|
1852 |
|
---|
1853 | if (state->num_pids == 0) {
|
---|
1854 | goto done;
|
---|
1855 | }
|
---|
1856 |
|
---|
1857 | qsort(state->pids, state->num_pids, sizeof(state->pids[0]),
|
---|
1858 | compare_procids);
|
---|
1859 |
|
---|
1860 | ZERO_STRUCT(last_pid);
|
---|
1861 |
|
---|
1862 | for (i=0; i<state->num_pids; i++) {
|
---|
1863 | if (procid_equal(&last_pid, &state->pids[i])) {
|
---|
1864 | /*
|
---|
1865 | * We've seen that one already
|
---|
1866 | */
|
---|
1867 | continue;
|
---|
1868 | }
|
---|
1869 |
|
---|
1870 | messaging_send(msg_ctx, state->pids[i], MSG_SMB_UNLOCK,
|
---|
1871 | &data_blob_null);
|
---|
1872 | last_pid = state->pids[i];
|
---|
1873 | }
|
---|
1874 |
|
---|
1875 | done:
|
---|
1876 | TALLOC_FREE(state);
|
---|
1877 | return;
|
---|
1878 | }
|
---|
1879 |
|
---|
1880 | void brl_register_msgs(struct messaging_context *msg_ctx)
|
---|
1881 | {
|
---|
1882 | messaging_register(msg_ctx, NULL, MSG_SMB_BRL_VALIDATE,
|
---|
1883 | brl_revalidate);
|
---|
1884 | }
|
---|