1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
/*
Unix SMB/CIFS implementation.
POSIX NTVFS backend - locking
Copyright (C) Andrew Tridgell 2004
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "include/includes.h"
#include "vfs_posix.h"
/*
check if we can perform IO on a range that might be locked
*/
NTSTATUS pvfs_check_lock(struct pvfs_state *pvfs,
struct pvfs_file *f,
uint16_t smbpid,
uint64_t offset, uint64_t count,
enum brl_type rw)
{
if (!(pvfs->flags & PVFS_FLAG_STRICT_LOCKING)) {
return NT_STATUS_OK;
}
return brl_locktest(pvfs->brl_context,
&f->locking_key,
f->fnum,
smbpid,
offset, count, rw);
}
/* this state structure holds information about a lock we are waiting on */
struct pending_state {
struct pvfs_state *pvfs;
union smb_lock *lck;
struct pvfs_file *f;
struct smbsrv_request *req;
int pending_lock;
void *wait_handle;
time_t end_time;
};
/*
a secondary attempt to setup a lock has failed - back out
the locks we did get and send an error
*/
static void pvfs_lock_async_failed(struct pvfs_state *pvfs,
struct smbsrv_request *req,
struct pvfs_file *f,
struct smb_lock_entry *locks,
int i,
NTSTATUS status)
{
/* undo the locks we just did */
for (i=i-1;i>=0;i--) {
brl_unlock(pvfs->brl_context,
&f->locking_key,
locks[i].pid,
f->fnum,
locks[i].offset,
locks[i].count);
}
req->async.status = status;
req->async.send_fn(req);
}
/*
called when we receive a pending lock notification. It means that
either our lock timed out or somoene else has unlocked a overlapping
range, so we should try the lock again. Note that on timeout we
do retry the lock, giving it a last chance.
*/
static void pvfs_pending_lock_continue(void *private, BOOL timed_out)
{
struct pending_state *pending = private;
struct pvfs_state *pvfs = pending->pvfs;
struct pvfs_file *f = pending->f;
struct smbsrv_request *req = pending->req;
union smb_lock *lck = pending->lck;
struct smb_lock_entry *locks;
enum brl_type rw;
NTSTATUS status;
int i;
locks = lck->lockx.in.locks + lck->lockx.in.ulock_cnt;
if (lck->lockx.in.mode & LOCKING_ANDX_SHARED_LOCK) {
rw = READ_LOCK;
} else {
rw = WRITE_LOCK;
}
status = brl_lock(pvfs->brl_context,
&f->locking_key,
req->smbpid,
f->fnum,
locks[pending->pending_lock].offset,
locks[pending->pending_lock].count,
rw, NULL);
/* if we have failed and timed out, or succeeded, then we
don't need the pending lock any more */
if (NT_STATUS_IS_OK(status) || timed_out) {
NTSTATUS status2;
status2 = brl_remove_pending(pvfs->brl_context, &f->locking_key, pending);
if (!NT_STATUS_IS_OK(status2)) {
DEBUG(0,("pvfs_lock: failed to remove pending lock - %s\n", nt_errstr(status2)));
}
talloc_free(pending->wait_handle);
}
if (!NT_STATUS_IS_OK(status)) {
if (timed_out) {
/* no more chances */
pvfs_lock_async_failed(pvfs, req, f, locks, pending->pending_lock, status);
}
/* we can try again */
return;
}
/* if we haven't timed out yet, then we can do more pending locks */
if (timed_out) {
pending = NULL;
} else {
if (rw == READ_LOCK) {
rw = PENDING_READ_LOCK;
} else {
rw = PENDING_WRITE_LOCK;
}
}
/* we've now got the pending lock. try and get the rest, which might
lead to more pending locks */
for (i=pending->pending_lock;i<lck->lockx.in.lock_cnt;i++) {
if (pending) {
pending->pending_lock = i;
}
status = brl_lock(pvfs->brl_context,
&f->locking_key,
req->smbpid,
f->fnum,
locks[i].offset,
locks[i].count,
rw, pending);
if (!NT_STATUS_IS_OK(status)) {
if (pending) {
/* a timed lock failed - setup a wait message to handle
the pending lock notification or a timeout */
pending->wait_handle = pvfs_wait_message(pvfs, req, MSG_BRL_RETRY,
pending->end_time,
pvfs_pending_lock_continue,
pending);
if (pending->wait_handle == NULL) {
pvfs_lock_async_failed(pvfs, req, f, locks, i, NT_STATUS_NO_MEMORY);
}
return;
}
pvfs_lock_async_failed(pvfs, req, f, locks, i, status);
return;
}
}
brl_unlock(pvfs->brl_context,
&f->locking_key,
req->smbpid,
f->fnum,
lck->lock.in.offset,
lck->lock.in.count);
/* we've managed to get all the locks. Tell the client */
req->async.status = NT_STATUS_OK;
req->async.send_fn(req);
}
/*
lock or unlock a byte range
*/
NTSTATUS pvfs_lock(struct ntvfs_module_context *ntvfs,
struct smbsrv_request *req, union smb_lock *lck)
{
struct pvfs_state *pvfs = ntvfs->private_data;
struct pvfs_file *f;
struct smb_lock_entry *locks;
int i;
enum brl_type rw;
struct pending_state *pending = NULL;
f = pvfs_find_fd(pvfs, req, lck->generic.in.fnum);
if (!f) {
return NT_STATUS_INVALID_HANDLE;
}
switch (lck->generic.level) {
case RAW_LOCK_LOCK:
return brl_lock(pvfs->brl_context,
&f->locking_key,
req->smbpid,
f->fnum,
lck->lock.in.offset,
lck->lock.in.count,
WRITE_LOCK, NULL);
case RAW_LOCK_UNLOCK:
return brl_unlock(pvfs->brl_context,
&f->locking_key,
req->smbpid,
f->fnum,
lck->lock.in.offset,
lck->lock.in.count);
case RAW_LOCK_GENERIC:
return NT_STATUS_INVALID_LEVEL;
case RAW_LOCK_LOCKX:
/* fall through to the most complex case */
break;
}
/* now the lockingX case, most common and also most complex */
if (lck->lockx.in.timeout != 0) {
pending = talloc_p(req, struct pending_state);
if (pending == NULL) {
return NT_STATUS_NO_MEMORY;
}
pending->pvfs = pvfs;
pending->lck = lck;
pending->f = f;
pending->req = req;
/* round up to the nearest second */
pending->end_time = time(NULL) + ((lck->lockx.in.timeout+999)/1000);
}
if (lck->lockx.in.mode & LOCKING_ANDX_SHARED_LOCK) {
rw = pending? PENDING_READ_LOCK : READ_LOCK;
} else {
rw = pending? PENDING_WRITE_LOCK : WRITE_LOCK;
}
if (lck->lockx.in.mode &
(LOCKING_ANDX_OPLOCK_RELEASE |
LOCKING_ANDX_CHANGE_LOCKTYPE |
LOCKING_ANDX_CANCEL_LOCK)) {
/* todo: need to add support for these */
return NT_STATUS_NOT_IMPLEMENTED;
}
/* the unlocks happen first */
locks = lck->lockx.in.locks;
for (i=0;i<lck->lockx.in.ulock_cnt;i++) {
NTSTATUS status;
status = brl_unlock(pvfs->brl_context,
&f->locking_key,
locks[i].pid,
f->fnum,
locks[i].offset,
locks[i].count);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
}
locks += i;
for (i=0;i<lck->lockx.in.lock_cnt;i++) {
NTSTATUS status;
if (pending) {
pending->pending_lock = i;
}
status = brl_lock(pvfs->brl_context,
&f->locking_key,
locks[i].pid,
f->fnum,
locks[i].offset,
locks[i].count,
rw, pending);
if (!NT_STATUS_IS_OK(status)) {
if (pending) {
/* a timed lock failed - setup a wait message to handle
the pending lock notification or a timeout */
pending->wait_handle = pvfs_wait_message(pvfs, req, MSG_BRL_RETRY,
pending->end_time,
pvfs_pending_lock_continue,
pending);
if (pending->wait_handle == NULL) {
return NT_STATUS_NO_MEMORY;
}
return NT_STATUS_OK;
}
/* undo the locks we just did */
for (i=i-1;i>=0;i--) {
brl_unlock(pvfs->brl_context,
&f->locking_key,
locks[i].pid,
f->fnum,
locks[i].offset,
locks[i].count);
}
return status;
}
}
return NT_STATUS_OK;
}
|