ReactOS 0.4.15-dev-7788-g1ad9096
lock.c
Go to the documentation of this file.
1/* NFSv4.1 client for Windows
2 * Copyright © 2012 The Regents of the University of Michigan
3 *
4 * Olga Kornievskaia <aglo@umich.edu>
5 * Casey Bodley <cbodley@umich.edu>
6 *
7 * This library is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or (at
10 * your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but
13 * without any warranty; without even the implied warranty of merchantability
14 * or fitness for a particular purpose. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 */
21
22#include <windows.h>
23#include <stdio.h>
24
25#include "daemon_debug.h"
26#include "delegation.h"
27#include "nfs41_ops.h"
28#include "upcall.h"
29#include "util.h"
30
31
32#define LKLVL 2 /* dprintf level for lock logging */
33
34
35static void lock_stateid_arg(
38{
39 arg->open = state;
40 arg->delegation = NULL;
41
43 if (state->locks.stateid.seqid) {
44 memcpy(&arg->stateid, &state->locks.stateid, sizeof(stateid4));
45 arg->type = STATEID_LOCK;
46 } else if (state->do_close) {
47 memcpy(&arg->stateid, &state->stateid, sizeof(stateid4));
48 arg->type = STATEID_OPEN;
49 } else {
50 memset(&arg->stateid, 0, sizeof(stateid4));
51 arg->type = STATEID_SPECIAL;
52 }
54}
55
56/* expects the caller to hold an exclusive lock on nfs41_open_state.lock */
59 IN const stateid4 *stateid)
60{
61 if (state->locks.stateid.seqid == 0) {
62 /* if it's a new lock stateid, copy it in */
63 memcpy(&state->locks.stateid, stateid, sizeof(stateid4));
64 } else if (stateid->seqid > state->locks.stateid.seqid) {
65 /* update the seqid if it's more recent */
66 state->locks.stateid.seqid = stateid->seqid;
67 }
68}
69
70static void open_lock_add(
72 IN const stateid_arg *stateid,
74{
76
77 if (stateid->type == STATEID_LOCK)
78 lock_stateid_update(open, &stateid->stateid);
79
80 lock->id = open->locks.counter++;
81 list_add_tail(&open->locks.list, &lock->open_entry);
82
84}
85
89{
90 bool_t delegated = FALSE;
91
93 if (open->delegation.state) {
94 nfs41_delegation_state *deleg = open->delegation.state;
96 if (deleg->state.type == OPEN_DELEGATE_WRITE
97 && deleg->status == DELEGATION_GRANTED) {
98 lock->delegated = 1;
99 lock->id = open->locks.counter++;
100 list_add_tail(&open->locks.list, &lock->open_entry);
101 delegated = TRUE;
102 }
103 ReleaseSRWLockShared(&deleg->lock);
104 }
106
107 return delegated;
108}
109
110#define lock_entry(pos) list_container(pos, nfs41_lock_state, open_entry)
111
112static int lock_range_cmp(const struct list_entry *entry, const void *value)
113{
114 const nfs41_lock_state *lhs = lock_entry(entry);
115 const nfs41_lock_state *rhs = (const nfs41_lock_state*)value;
116 if (lhs->offset != rhs->offset) return -1;
117 if (lhs->length != rhs->length) return -1;
118 return 0;
119}
120
124{
125 struct list_entry *entry;
127
129
130 /* find lock state that matches this range */
131 entry = list_search(&open->locks.list, input, lock_range_cmp);
132 if (entry) {
134 if (lock->delegated) {
135 /* if the lock was delegated, remove/free it and return success */
137 free(lock);
139 } else
141 }
142
144 return status;
145}
146
149 IN const stateid_arg *stateid,
151{
152 struct list_entry *entry;
153
155 if (stateid->type == STATEID_LOCK)
156 lock_stateid_update(open, &stateid->stateid);
157
158 /* find and remove the unlocked range */
159 entry = list_search(&open->locks.list, input, lock_range_cmp);
160 if (entry) {
163 }
165}
166
167
168/* NFS41_LOCK */
169static int parse_lock(unsigned char *buffer, uint32_t length, nfs41_upcall *upcall)
170{
171 int status;
172 lock_upcall_args *args = &upcall->args.lock;
173
174 status = safe_read(&buffer, &length, &args->offset, sizeof(LONGLONG));
175 if (status) goto out;
176 status = safe_read(&buffer, &length, &args->length, sizeof(LONGLONG));
177 if (status) goto out;
178 status = safe_read(&buffer, &length, &args->exclusive, sizeof(BOOLEAN));
179 if (status) goto out;
180 status = safe_read(&buffer, &length, &args->blocking, sizeof(BOOLEAN));
181 if (status) goto out;
182
183 dprintf(1, "parsing NFS41_LOCK: offset=0x%llx length=0x%llx exclusive=%u "
184 "blocking=%u\n", args->offset, args->length, args->exclusive,
185 args->blocking);
186out:
187 return status;
188}
189
190static __inline uint32_t get_lock_type(BOOLEAN exclusive, BOOLEAN blocking)
191{
192 return blocking == 0
193 ? ( exclusive == 0 ? READ_LT : WRITE_LT )
194 : ( exclusive == 0 ? READW_LT : WRITEW_LT );
195}
196
198{
199 stateid_arg stateid;
200 lock_upcall_args *args = &upcall->args.lock;
201 nfs41_open_state *state = upcall->state_ref;
203 const uint32_t type = get_lock_type(args->exclusive, args->blocking);
204 int status = NO_ERROR;
205
206 /* 18.10.3. Operation 12: LOCK - Create Lock
207 * "To lock the file from a specific offset through the end-of-file
208 * (no matter how long the file actually is) use a length field equal
209 * to NFS4_UINT64_MAX." */
210 if (args->length >= NFS4_UINT64_MAX - args->offset)
212
213 /* allocate the lock state */
214 lock = calloc(1, sizeof(nfs41_lock_state));
215 if (lock == NULL) {
217 goto out;
218 }
219 lock->offset = args->offset;
220 lock->length = args->length;
221 lock->exclusive = args->exclusive;
222
223 /* if we hold a write delegation, handle the lock locally */
225 dprintf(LKLVL, "delegated lock { %llu, %llu }\n",
226 lock->offset, lock->length);
227 args->acquired = TRUE; /* for cancel_lock() */
228 goto out;
229 }
230
231 /* open_to_lock_owner4 requires an open stateid; if we
232 * have a delegation, convert it to an open stateid */
234 if (status) {
236 goto out_free;
237 }
238
239 EnterCriticalSection(&state->locks.lock);
240
241 lock_stateid_arg(state, &stateid);
242
243 status = nfs41_lock(state->session, &state->file, &state->owner,
244 type, lock->offset, lock->length, FALSE, TRUE, &stateid);
245 if (status) {
246 dprintf(LKLVL, "nfs41_lock failed with %s\n",
249 LeaveCriticalSection(&state->locks.lock);
250 goto out_free;
251 }
252
253 /* save lock state with the open */
254 open_lock_add(state, &stateid, lock);
255 LeaveCriticalSection(&state->locks.lock);
256
257 args->acquired = TRUE; /* for cancel_lock() */
258out:
259 return status;
260
261out_free:
262 free(lock);
263 goto out;
264}
265
267{
268 stateid_arg stateid;
270 lock_upcall_args *args = &upcall->args.lock;
271 nfs41_open_state *state = upcall->state_ref;
272 int status = NO_ERROR;
273
274 dprintf(1, "--> cancel_lock()\n");
275
276 /* can't do 'if (upcall->status)' here, because a handle_lock() success
277 * could be overwritten by upcall_marshall() or allocation failure */
278 if (!args->acquired)
279 goto out;
280
281 input.offset = args->offset;
283
284 /* search for the range to unlock, and remove if delegated */
286 if (status != ERROR_LOCKED)
287 goto out;
288
289 EnterCriticalSection(&state->locks.lock);
290 lock_stateid_arg(state, &stateid);
291
292 status = nfs41_unlock(state->session, &state->file,
293 args->offset, args->length, &stateid);
294
295 open_unlock_remove(state, &stateid, &input);
296 LeaveCriticalSection(&state->locks.lock);
297
299out:
300 dprintf(1, "<-- cancel_lock() returning %d\n", status);
301}
302
303
304/* NFS41_UNLOCK */
306{
307 int status;
308 unlock_upcall_args *args = &upcall->args.unlock;
309
310 status = safe_read(&buffer, &length, &args->count, sizeof(ULONG));
311 if (status) goto out;
312
313 args->buf = buffer;
314 args->buf_len = length;
315
316 dprintf(1, "parsing NFS41_UNLOCK: count=%u\n", args->count);
317out:
318 return status;
319}
320
322{
324 stateid_arg stateid;
325 unlock_upcall_args *args = &upcall->args.unlock;
326 nfs41_open_state *state = upcall->state_ref;
327 unsigned char *buf = args->buf;
328 uint32_t buf_len = args->buf_len;
329 uint32_t i;
330 int status = NO_ERROR;
331
332 for (i = 0; i < args->count; i++) {
333 if (safe_read(&buf, &buf_len, &input.offset, sizeof(LONGLONG))) break;
334 if (safe_read(&buf, &buf_len, &input.length, sizeof(LONGLONG))) break;
335
336 /* do the same translation as LOCK, or the ranges won't match */
337 if (input.length >= NFS4_UINT64_MAX - input.offset)
338 input.length = NFS4_UINT64_MAX;
339
340 /* search for the range to unlock, and remove if delegated */
342 if (status != ERROR_LOCKED)
343 continue;
344
345 EnterCriticalSection(&state->locks.lock);
346 lock_stateid_arg(state, &stateid);
347
348 status = nfs41_unlock(state->session, &state->file,
349 input.offset, input.length, &stateid);
350
351 open_unlock_remove(state, &stateid, &input);
352 LeaveCriticalSection(&state->locks.lock);
353
355 }
356 return status;
357}
358
359
363 NULL,
365};
369};
unsigned char BOOLEAN
#define open
Definition: acwin.h:95
static int state
Definition: maze.c:121
static void list_remove(struct list_entry *entry)
Definition: list.h:90
static void list_add_tail(struct list_entry *head, struct list_entry *entry)
Definition: list.h:83
static struct list_entry * list_search(const struct list_entry *head, const void *value, list_compare_fn compare)
Definition: list.h:102
static void cancel_lock(IN nfs41_upcall *upcall)
Definition: lock.c:266
static int handle_unlock(nfs41_upcall *upcall)
Definition: lock.c:321
#define LKLVL
Definition: lock.c:32
static void open_lock_add(IN nfs41_open_state *open, IN const stateid_arg *stateid, IN nfs41_lock_state *lock)
Definition: lock.c:70
static int open_unlock_delegate(IN nfs41_open_state *open, IN const nfs41_lock_state *input)
Definition: lock.c:121
static int handle_lock(nfs41_upcall *upcall)
Definition: lock.c:197
static __inline uint32_t get_lock_type(BOOLEAN exclusive, BOOLEAN blocking)
Definition: lock.c:190
static void open_unlock_remove(IN nfs41_open_state *open, IN const stateid_arg *stateid, IN const nfs41_lock_state *input)
Definition: lock.c:147
static void lock_stateid_update(OUT nfs41_open_state *state, IN const stateid4 *stateid)
Definition: lock.c:57
static int parse_lock(unsigned char *buffer, uint32_t length, nfs41_upcall *upcall)
Definition: lock.c:169
const nfs41_upcall_op nfs41_op_unlock
Definition: lock.c:366
static void lock_stateid_arg(IN nfs41_open_state *state, OUT stateid_arg *arg)
Definition: lock.c:35
const nfs41_upcall_op nfs41_op_lock
Definition: lock.c:360
static int parse_unlock(unsigned char *buffer, uint32_t length, nfs41_upcall *upcall)
Definition: lock.c:305
static bool_t open_lock_delegate(IN nfs41_open_state *open, IN nfs41_lock_state *lock)
Definition: lock.c:86
#define lock_entry(pos)
Definition: lock.c:110
static int lock_range_cmp(const struct list_entry *entry, const void *value)
Definition: lock.c:112
int safe_read(unsigned char **pos, uint32_t *remaining, void *dest, uint32_t dest_len)
Definition: util.c:33
int nfs_to_windows_error(int status, int default_error)
Definition: util.c:235
const char * nfs_error_string(int status)
Definition: daemon_debug.c:370
#define NO_ERROR
Definition: dderror.h:5
#define free
Definition: debug_ros.c:5
int nfs41_delegation_to_open(IN nfs41_open_state *open, IN bool_t try_recovery)
Definition: delegation.c:539
#define NULL
Definition: types.h:112
int32_t bool_t
Definition: types.h:101
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
UINT32 uint32_t
Definition: types.h:75
VOID WINAPI ReleaseSRWLockShared(PSRWLOCK Lock)
Definition: sync.c:89
VOID WINAPI AcquireSRWLockExclusive(PSRWLOCK Lock)
Definition: sync.c:54
VOID WINAPI AcquireSRWLockShared(PSRWLOCK Lock)
Definition: sync.c:61
VOID WINAPI ReleaseSRWLockExclusive(PSRWLOCK Lock)
Definition: sync.c:82
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLuint buffer
Definition: glext.h:5915
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLuint GLsizei GLsizei * length
Definition: glext.h:6040
GLenum GLenum GLenum input
Definition: glext.h:9031
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
uint32_t entry
Definition: isohybrid.c:63
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
@ DELEGATION_GRANTED
Definition: nfs41.h:90
nfs41_updowncall_list upcall
Definition: nfs41_driver.c:273
int nfs41_lock(IN nfs41_session *session, IN nfs41_path_fh *file, IN state_owner4 *owner, IN uint32_t type, IN uint64_t offset, IN uint64_t length, IN bool_t reclaim, IN bool_t try_recovery, IN OUT stateid_arg *stateid)
Definition: nfs41_ops.c:904
int nfs41_unlock(IN nfs41_session *session, IN nfs41_path_fh *file, IN uint64_t offset, IN uint64_t length, IN OUT stateid_arg *stateid)
Definition: nfs41_ops.c:966
@ OPEN_DELEGATE_WRITE
Definition: nfs41_ops.h:588
@ STATEID_LOCK
Definition: nfs41_ops.h:277
@ STATEID_SPECIAL
Definition: nfs41_ops.h:281
@ STATEID_OPEN
Definition: nfs41_ops.h:276
@ READ_LT
Definition: nfs41_ops.h:399
@ READW_LT
Definition: nfs41_ops.h:401
@ WRITEW_LT
Definition: nfs41_ops.h:402
@ WRITE_LT
Definition: nfs41_ops.h:400
static const uint64_t NFS4_UINT64_MAX
Definition: nfs41_types.h:32
#define dprintf
Definition: regdump.c:33
static FILE * out
Definition: regtests2xml.c:44
#define calloc
Definition: rosglue.h:14
#define memset(x, y, z)
Definition: compat.h:39
enum delegation_status status
Definition: nfs41.h:103
open_delegation4 state
Definition: nfs41.h:96
uint64_t length
Definition: nfs41.h:115
uint64_t offset
Definition: nfs41.h:114
enum open_delegation_type4 type
Definition: nfs41_types.h:154
Definition: match.c:390
int length
Definition: match.c:394
Definition: list.h:27
Definition: ps.c:97
rwlock_t lock
Definition: tcpcore.h:0
int64_t LONGLONG
Definition: typedefs.h:68
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
Definition: pdh_main.c:94
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
#define ERROR_NOT_LOCKED
Definition: winerror.h:230
#define ERROR_BAD_NET_RESP
Definition: winerror.h:150
#define ERROR_LOCKED
Definition: winerror.h:268
#define ERROR_FILE_INVALID
Definition: winerror.h:585