]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/nfsd/nfs4state.c | |
3 | * | |
4 | * Copyright (c) 2001 The Regents of the University of Michigan. | |
5 | * All rights reserved. | |
6 | * | |
7 | * Kendrick Smith <[email protected]> | |
8 | * Andy Adamson <[email protected]> | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * | |
14 | * 1. Redistributions of source code must retain the above copyright | |
15 | * notice, this list of conditions and the following disclaimer. | |
16 | * 2. Redistributions in binary form must reproduce the above copyright | |
17 | * notice, this list of conditions and the following disclaimer in the | |
18 | * documentation and/or other materials provided with the distribution. | |
19 | * 3. Neither the name of the University nor the names of its | |
20 | * contributors may be used to endorse or promote products derived | |
21 | * from this software without specific prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
24 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
26 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | |
30 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
32 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
34 | * | |
35 | */ | |
36 | ||
37 | #include <linux/param.h> | |
38 | #include <linux/major.h> | |
39 | #include <linux/slab.h> | |
40 | ||
41 | #include <linux/sunrpc/svc.h> | |
42 | #include <linux/nfsd/nfsd.h> | |
43 | #include <linux/nfsd/cache.h> | |
44 | #include <linux/mount.h> | |
45 | #include <linux/workqueue.h> | |
46 | #include <linux/smp_lock.h> | |
47 | #include <linux/kthread.h> | |
48 | #include <linux/nfs4.h> | |
49 | #include <linux/nfsd/state.h> | |
50 | #include <linux/nfsd/xdr4.h> | |
0964a3d3 | 51 | #include <linux/namei.h> |
1da177e4 LT |
52 | |
53 | #define NFSDDBG_FACILITY NFSDDBG_PROC | |
54 | ||
55 | /* Globals */ | |
56 | static time_t lease_time = 90; /* default lease time */ | |
d99a05ad | 57 | static time_t user_lease_time = 90; |
fd39ca9a | 58 | static time_t boot_time; |
a76b4319 | 59 | static int in_grace = 1; |
1da177e4 LT |
60 | static u32 current_clientid = 1; |
61 | static u32 current_ownerid = 1; | |
62 | static u32 current_fileid = 1; | |
63 | static u32 current_delegid = 1; | |
64 | static u32 nfs4_init; | |
fd39ca9a N |
65 | static stateid_t zerostateid; /* bits all 0 */ |
66 | static stateid_t onestateid; /* bits all 1 */ | |
67 | ||
68 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t))) | |
69 | #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t))) | |
1da177e4 | 70 | |
1da177e4 | 71 | /* forward declarations */ |
fd39ca9a | 72 | static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags); |
1da177e4 LT |
73 | static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid); |
74 | static void release_stateid_lockowners(struct nfs4_stateid *open_stp); | |
0964a3d3 N |
75 | static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery"; |
76 | static void nfs4_set_recdir(char *recdir); | |
1da177e4 LT |
77 | |
78 | /* Locking: | |
79 | * | |
80 | * client_sema: | |
81 | * protects clientid_hashtbl[], clientstr_hashtbl[], | |
82 | * unconfstr_hashtbl[], uncofid_hashtbl[]. | |
83 | */ | |
84 | static DECLARE_MUTEX(client_sema); | |
85 | ||
fd39ca9a N |
86 | static kmem_cache_t *stateowner_slab = NULL; |
87 | static kmem_cache_t *file_slab = NULL; | |
88 | static kmem_cache_t *stateid_slab = NULL; | |
89 | static kmem_cache_t *deleg_slab = NULL; | |
e60d4398 | 90 | |
1da177e4 LT |
91 | void |
92 | nfs4_lock_state(void) | |
93 | { | |
94 | down(&client_sema); | |
95 | } | |
96 | ||
97 | void | |
98 | nfs4_unlock_state(void) | |
99 | { | |
100 | up(&client_sema); | |
101 | } | |
102 | ||
103 | static inline u32 | |
104 | opaque_hashval(const void *ptr, int nbytes) | |
105 | { | |
106 | unsigned char *cptr = (unsigned char *) ptr; | |
107 | ||
108 | u32 x = 0; | |
109 | while (nbytes--) { | |
110 | x *= 37; | |
111 | x += *cptr++; | |
112 | } | |
113 | return x; | |
114 | } | |
115 | ||
116 | /* forward declarations */ | |
117 | static void release_stateowner(struct nfs4_stateowner *sop); | |
118 | static void release_stateid(struct nfs4_stateid *stp, int flags); | |
1da177e4 LT |
119 | |
120 | /* | |
121 | * Delegation state | |
122 | */ | |
123 | ||
124 | /* recall_lock protects the del_recall_lru */ | |
fd39ca9a | 125 | static spinlock_t recall_lock = SPIN_LOCK_UNLOCKED; |
1da177e4 LT |
126 | static struct list_head del_recall_lru; |
127 | ||
13cd2184 N |
128 | static void |
129 | free_nfs4_file(struct kref *kref) | |
130 | { | |
131 | struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref); | |
132 | list_del(&fp->fi_hash); | |
133 | iput(fp->fi_inode); | |
134 | kmem_cache_free(file_slab, fp); | |
135 | } | |
136 | ||
137 | static inline void | |
138 | put_nfs4_file(struct nfs4_file *fi) | |
139 | { | |
140 | kref_put(&fi->fi_ref, free_nfs4_file); | |
141 | } | |
142 | ||
143 | static inline void | |
144 | get_nfs4_file(struct nfs4_file *fi) | |
145 | { | |
146 | kref_get(&fi->fi_ref); | |
147 | } | |
148 | ||
1da177e4 LT |
149 | static struct nfs4_delegation * |
150 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type) | |
151 | { | |
152 | struct nfs4_delegation *dp; | |
153 | struct nfs4_file *fp = stp->st_file; | |
154 | struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback; | |
155 | ||
156 | dprintk("NFSD alloc_init_deleg\n"); | |
5b2d21c1 N |
157 | dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL); |
158 | if (dp == NULL) | |
1da177e4 | 159 | return dp; |
ea1da636 N |
160 | INIT_LIST_HEAD(&dp->dl_perfile); |
161 | INIT_LIST_HEAD(&dp->dl_perclnt); | |
1da177e4 LT |
162 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
163 | dp->dl_client = clp; | |
13cd2184 | 164 | get_nfs4_file(fp); |
1da177e4 LT |
165 | dp->dl_file = fp; |
166 | dp->dl_flock = NULL; | |
167 | get_file(stp->st_vfs_file); | |
168 | dp->dl_vfs_file = stp->st_vfs_file; | |
169 | dp->dl_type = type; | |
170 | dp->dl_recall.cbr_dp = NULL; | |
171 | dp->dl_recall.cbr_ident = cb->cb_ident; | |
172 | dp->dl_recall.cbr_trunc = 0; | |
173 | dp->dl_stateid.si_boot = boot_time; | |
174 | dp->dl_stateid.si_stateownerid = current_delegid++; | |
175 | dp->dl_stateid.si_fileid = 0; | |
176 | dp->dl_stateid.si_generation = 0; | |
177 | dp->dl_fhlen = current_fh->fh_handle.fh_size; | |
178 | memcpy(dp->dl_fhval, ¤t_fh->fh_handle.fh_base, | |
179 | current_fh->fh_handle.fh_size); | |
180 | dp->dl_time = 0; | |
181 | atomic_set(&dp->dl_count, 1); | |
ea1da636 N |
182 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
183 | list_add(&dp->dl_perclnt, &clp->cl_delegations); | |
1da177e4 LT |
184 | return dp; |
185 | } | |
186 | ||
187 | void | |
188 | nfs4_put_delegation(struct nfs4_delegation *dp) | |
189 | { | |
190 | if (atomic_dec_and_test(&dp->dl_count)) { | |
191 | dprintk("NFSD: freeing dp %p\n",dp); | |
13cd2184 | 192 | put_nfs4_file(dp->dl_file); |
5b2d21c1 | 193 | kmem_cache_free(deleg_slab, dp); |
1da177e4 LT |
194 | } |
195 | } | |
196 | ||
197 | /* Remove the associated file_lock first, then remove the delegation. | |
198 | * lease_modify() is called to remove the FS_LEASE file_lock from | |
199 | * the i_flock list, eventually calling nfsd's lock_manager | |
200 | * fl_release_callback. | |
201 | */ | |
202 | static void | |
203 | nfs4_close_delegation(struct nfs4_delegation *dp) | |
204 | { | |
205 | struct file *filp = dp->dl_vfs_file; | |
206 | ||
207 | dprintk("NFSD: close_delegation dp %p\n",dp); | |
208 | dp->dl_vfs_file = NULL; | |
209 | /* The following nfsd_close may not actually close the file, | |
210 | * but we want to remove the lease in any case. */ | |
c907132d N |
211 | if (dp->dl_flock) |
212 | setlease(filp, F_UNLCK, &dp->dl_flock); | |
1da177e4 | 213 | nfsd_close(filp); |
1da177e4 LT |
214 | } |
215 | ||
216 | /* Called under the state lock. */ | |
217 | static void | |
218 | unhash_delegation(struct nfs4_delegation *dp) | |
219 | { | |
ea1da636 N |
220 | list_del_init(&dp->dl_perfile); |
221 | list_del_init(&dp->dl_perclnt); | |
1da177e4 LT |
222 | spin_lock(&recall_lock); |
223 | list_del_init(&dp->dl_recall_lru); | |
224 | spin_unlock(&recall_lock); | |
225 | nfs4_close_delegation(dp); | |
226 | nfs4_put_delegation(dp); | |
227 | } | |
228 | ||
229 | /* | |
230 | * SETCLIENTID state | |
231 | */ | |
232 | ||
233 | /* Hash tables for nfs4_clientid state */ | |
234 | #define CLIENT_HASH_BITS 4 | |
235 | #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS) | |
236 | #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1) | |
237 | ||
238 | #define clientid_hashval(id) \ | |
239 | ((id) & CLIENT_HASH_MASK) | |
a55370a3 N |
240 | #define clientstr_hashval(name) \ |
241 | (opaque_hashval((name), 8) & CLIENT_HASH_MASK) | |
1da177e4 LT |
242 | /* |
243 | * reclaim_str_hashtbl[] holds known client info from previous reset/reboot | |
244 | * used in reboot/reset lease grace period processing | |
245 | * | |
246 | * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed | |
247 | * setclientid_confirmed info. | |
248 | * | |
249 | * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed | |
250 | * setclientid info. | |
251 | * | |
252 | * client_lru holds client queue ordered by nfs4_client.cl_time | |
253 | * for lease renewal. | |
254 | * | |
255 | * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time | |
256 | * for last close replay. | |
257 | */ | |
258 | static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE]; | |
259 | static int reclaim_str_hashtbl_size = 0; | |
260 | static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE]; | |
261 | static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE]; | |
262 | static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE]; | |
263 | static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE]; | |
264 | static struct list_head client_lru; | |
265 | static struct list_head close_lru; | |
266 | ||
267 | static inline void | |
268 | renew_client(struct nfs4_client *clp) | |
269 | { | |
270 | /* | |
271 | * Move client to the end to the LRU list. | |
272 | */ | |
273 | dprintk("renewing client (clientid %08x/%08x)\n", | |
274 | clp->cl_clientid.cl_boot, | |
275 | clp->cl_clientid.cl_id); | |
276 | list_move_tail(&clp->cl_lru, &client_lru); | |
277 | clp->cl_time = get_seconds(); | |
278 | } | |
279 | ||
280 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ | |
281 | static int | |
282 | STALE_CLIENTID(clientid_t *clid) | |
283 | { | |
284 | if (clid->cl_boot == boot_time) | |
285 | return 0; | |
286 | dprintk("NFSD stale clientid (%08x/%08x)\n", | |
287 | clid->cl_boot, clid->cl_id); | |
288 | return 1; | |
289 | } | |
290 | ||
291 | /* | |
292 | * XXX Should we use a slab cache ? | |
293 | * This type of memory management is somewhat inefficient, but we use it | |
294 | * anyway since SETCLIENTID is not a common operation. | |
295 | */ | |
296 | static inline struct nfs4_client * | |
297 | alloc_client(struct xdr_netobj name) | |
298 | { | |
299 | struct nfs4_client *clp; | |
300 | ||
301 | if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) { | |
302 | memset(clp, 0, sizeof(*clp)); | |
303 | if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) { | |
304 | memcpy(clp->cl_name.data, name.data, name.len); | |
305 | clp->cl_name.len = name.len; | |
306 | } | |
307 | else { | |
308 | kfree(clp); | |
309 | clp = NULL; | |
310 | } | |
311 | } | |
312 | return clp; | |
313 | } | |
314 | ||
315 | static inline void | |
316 | free_client(struct nfs4_client *clp) | |
317 | { | |
318 | if (clp->cl_cred.cr_group_info) | |
319 | put_group_info(clp->cl_cred.cr_group_info); | |
320 | kfree(clp->cl_name.data); | |
321 | kfree(clp); | |
322 | } | |
323 | ||
324 | void | |
325 | put_nfs4_client(struct nfs4_client *clp) | |
326 | { | |
327 | if (atomic_dec_and_test(&clp->cl_count)) | |
328 | free_client(clp); | |
329 | } | |
330 | ||
331 | static void | |
332 | expire_client(struct nfs4_client *clp) | |
333 | { | |
334 | struct nfs4_stateowner *sop; | |
335 | struct nfs4_delegation *dp; | |
336 | struct nfs4_callback *cb = &clp->cl_callback; | |
337 | struct rpc_clnt *clnt = clp->cl_callback.cb_client; | |
338 | struct list_head reaplist; | |
339 | ||
340 | dprintk("NFSD: expire_client cl_count %d\n", | |
341 | atomic_read(&clp->cl_count)); | |
342 | ||
343 | /* shutdown rpc client, ending any outstanding recall rpcs */ | |
344 | if (atomic_read(&cb->cb_set) == 1 && clnt) { | |
345 | rpc_shutdown_client(clnt); | |
346 | clnt = clp->cl_callback.cb_client = NULL; | |
347 | } | |
348 | ||
349 | INIT_LIST_HEAD(&reaplist); | |
350 | spin_lock(&recall_lock); | |
ea1da636 N |
351 | while (!list_empty(&clp->cl_delegations)) { |
352 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); | |
1da177e4 LT |
353 | dprintk("NFSD: expire client. dp %p, fp %p\n", dp, |
354 | dp->dl_flock); | |
ea1da636 | 355 | list_del_init(&dp->dl_perclnt); |
1da177e4 LT |
356 | list_move(&dp->dl_recall_lru, &reaplist); |
357 | } | |
358 | spin_unlock(&recall_lock); | |
359 | while (!list_empty(&reaplist)) { | |
360 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); | |
361 | list_del_init(&dp->dl_recall_lru); | |
362 | unhash_delegation(dp); | |
363 | } | |
364 | list_del(&clp->cl_idhash); | |
365 | list_del(&clp->cl_strhash); | |
366 | list_del(&clp->cl_lru); | |
ea1da636 N |
367 | while (!list_empty(&clp->cl_openowners)) { |
368 | sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient); | |
1da177e4 LT |
369 | release_stateowner(sop); |
370 | } | |
371 | put_nfs4_client(clp); | |
372 | } | |
373 | ||
374 | static struct nfs4_client * | |
a55370a3 | 375 | create_client(struct xdr_netobj name, char *recdir) { |
1da177e4 LT |
376 | struct nfs4_client *clp; |
377 | ||
378 | if (!(clp = alloc_client(name))) | |
379 | goto out; | |
a55370a3 | 380 | memcpy(clp->cl_recdir, recdir, HEXDIR_LEN); |
1da177e4 LT |
381 | atomic_set(&clp->cl_count, 1); |
382 | atomic_set(&clp->cl_callback.cb_set, 0); | |
1da177e4 LT |
383 | INIT_LIST_HEAD(&clp->cl_idhash); |
384 | INIT_LIST_HEAD(&clp->cl_strhash); | |
ea1da636 N |
385 | INIT_LIST_HEAD(&clp->cl_openowners); |
386 | INIT_LIST_HEAD(&clp->cl_delegations); | |
1da177e4 LT |
387 | INIT_LIST_HEAD(&clp->cl_lru); |
388 | out: | |
389 | return clp; | |
390 | } | |
391 | ||
392 | static void | |
393 | copy_verf(struct nfs4_client *target, nfs4_verifier *source) { | |
394 | memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data)); | |
395 | } | |
396 | ||
397 | static void | |
398 | copy_clid(struct nfs4_client *target, struct nfs4_client *source) { | |
399 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; | |
400 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; | |
401 | } | |
402 | ||
403 | static void | |
404 | copy_cred(struct svc_cred *target, struct svc_cred *source) { | |
405 | ||
406 | target->cr_uid = source->cr_uid; | |
407 | target->cr_gid = source->cr_gid; | |
408 | target->cr_group_info = source->cr_group_info; | |
409 | get_group_info(target->cr_group_info); | |
410 | } | |
411 | ||
a55370a3 N |
412 | static inline int |
413 | same_name(const char *n1, const char *n2) { | |
414 | return 0 == memcmp(n1, n2, HEXDIR_LEN); | |
1da177e4 LT |
415 | } |
416 | ||
417 | static int | |
418 | cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) { | |
419 | return(!memcmp(v1->data,v2->data,sizeof(v1->data))); | |
420 | } | |
421 | ||
422 | static int | |
423 | cmp_clid(clientid_t * cl1, clientid_t * cl2) { | |
424 | return((cl1->cl_boot == cl2->cl_boot) && | |
425 | (cl1->cl_id == cl2->cl_id)); | |
426 | } | |
427 | ||
428 | /* XXX what about NGROUP */ | |
429 | static int | |
430 | cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){ | |
431 | return(cr1->cr_uid == cr2->cr_uid); | |
432 | ||
433 | } | |
434 | ||
435 | static void | |
436 | gen_clid(struct nfs4_client *clp) { | |
437 | clp->cl_clientid.cl_boot = boot_time; | |
438 | clp->cl_clientid.cl_id = current_clientid++; | |
439 | } | |
440 | ||
441 | static void | |
442 | gen_confirm(struct nfs4_client *clp) { | |
443 | struct timespec tv; | |
444 | u32 * p; | |
445 | ||
446 | tv = CURRENT_TIME; | |
447 | p = (u32 *)clp->cl_confirm.data; | |
448 | *p++ = tv.tv_sec; | |
449 | *p++ = tv.tv_nsec; | |
450 | } | |
451 | ||
452 | static int | |
453 | check_name(struct xdr_netobj name) { | |
454 | ||
455 | if (name.len == 0) | |
456 | return 0; | |
457 | if (name.len > NFS4_OPAQUE_LIMIT) { | |
458 | printk("NFSD: check_name: name too long(%d)!\n", name.len); | |
459 | return 0; | |
460 | } | |
461 | return 1; | |
462 | } | |
463 | ||
fd39ca9a | 464 | static void |
1da177e4 LT |
465 | add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval) |
466 | { | |
467 | unsigned int idhashval; | |
468 | ||
469 | list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]); | |
470 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); | |
471 | list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]); | |
472 | list_add_tail(&clp->cl_lru, &client_lru); | |
473 | clp->cl_time = get_seconds(); | |
474 | } | |
475 | ||
fd39ca9a | 476 | static void |
1da177e4 LT |
477 | move_to_confirmed(struct nfs4_client *clp) |
478 | { | |
479 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); | |
480 | unsigned int strhashval; | |
481 | ||
482 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); | |
483 | list_del_init(&clp->cl_strhash); | |
484 | list_del_init(&clp->cl_idhash); | |
485 | list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]); | |
a55370a3 | 486 | strhashval = clientstr_hashval(clp->cl_recdir); |
1da177e4 LT |
487 | list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]); |
488 | renew_client(clp); | |
489 | } | |
490 | ||
491 | static struct nfs4_client * | |
492 | find_confirmed_client(clientid_t *clid) | |
493 | { | |
494 | struct nfs4_client *clp; | |
495 | unsigned int idhashval = clientid_hashval(clid->cl_id); | |
496 | ||
497 | list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) { | |
498 | if (cmp_clid(&clp->cl_clientid, clid)) | |
499 | return clp; | |
500 | } | |
501 | return NULL; | |
502 | } | |
503 | ||
504 | static struct nfs4_client * | |
505 | find_unconfirmed_client(clientid_t *clid) | |
506 | { | |
507 | struct nfs4_client *clp; | |
508 | unsigned int idhashval = clientid_hashval(clid->cl_id); | |
509 | ||
510 | list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) { | |
511 | if (cmp_clid(&clp->cl_clientid, clid)) | |
512 | return clp; | |
513 | } | |
514 | return NULL; | |
515 | } | |
516 | ||
28ce6054 N |
517 | static struct nfs4_client * |
518 | find_confirmed_client_by_str(const char *dname, unsigned int hashval) | |
519 | { | |
520 | struct nfs4_client *clp; | |
521 | ||
522 | list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) { | |
523 | if (same_name(clp->cl_recdir, dname)) | |
524 | return clp; | |
525 | } | |
526 | return NULL; | |
527 | } | |
528 | ||
529 | static struct nfs4_client * | |
530 | find_unconfirmed_client_by_str(const char *dname, unsigned int hashval) | |
531 | { | |
532 | struct nfs4_client *clp; | |
533 | ||
534 | list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) { | |
535 | if (same_name(clp->cl_recdir, dname)) | |
536 | return clp; | |
537 | } | |
538 | return NULL; | |
539 | } | |
540 | ||
1da177e4 LT |
541 | /* a helper function for parse_callback */ |
542 | static int | |
543 | parse_octet(unsigned int *lenp, char **addrp) | |
544 | { | |
545 | unsigned int len = *lenp; | |
546 | char *p = *addrp; | |
547 | int n = -1; | |
548 | char c; | |
549 | ||
550 | for (;;) { | |
551 | if (!len) | |
552 | break; | |
553 | len--; | |
554 | c = *p++; | |
555 | if (c == '.') | |
556 | break; | |
557 | if ((c < '0') || (c > '9')) { | |
558 | n = -1; | |
559 | break; | |
560 | } | |
561 | if (n < 0) | |
562 | n = 0; | |
563 | n = (n * 10) + (c - '0'); | |
564 | if (n > 255) { | |
565 | n = -1; | |
566 | break; | |
567 | } | |
568 | } | |
569 | *lenp = len; | |
570 | *addrp = p; | |
571 | return n; | |
572 | } | |
573 | ||
574 | /* parse and set the setclientid ipv4 callback address */ | |
fd39ca9a | 575 | static int |
1da177e4 LT |
576 | parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp) |
577 | { | |
578 | int temp = 0; | |
579 | u32 cbaddr = 0; | |
580 | u16 cbport = 0; | |
581 | u32 addrlen = addr_len; | |
582 | char *addr = addr_val; | |
583 | int i, shift; | |
584 | ||
585 | /* ipaddress */ | |
586 | shift = 24; | |
587 | for(i = 4; i > 0 ; i--) { | |
588 | if ((temp = parse_octet(&addrlen, &addr)) < 0) { | |
589 | return 0; | |
590 | } | |
591 | cbaddr |= (temp << shift); | |
592 | if (shift > 0) | |
593 | shift -= 8; | |
594 | } | |
595 | *cbaddrp = cbaddr; | |
596 | ||
597 | /* port */ | |
598 | shift = 8; | |
599 | for(i = 2; i > 0 ; i--) { | |
600 | if ((temp = parse_octet(&addrlen, &addr)) < 0) { | |
601 | return 0; | |
602 | } | |
603 | cbport |= (temp << shift); | |
604 | if (shift > 0) | |
605 | shift -= 8; | |
606 | } | |
607 | *cbportp = cbport; | |
608 | return 1; | |
609 | } | |
610 | ||
fd39ca9a | 611 | static void |
1da177e4 LT |
612 | gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se) |
613 | { | |
614 | struct nfs4_callback *cb = &clp->cl_callback; | |
615 | ||
616 | /* Currently, we only support tcp for the callback channel */ | |
617 | if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3)) | |
618 | goto out_err; | |
619 | ||
620 | if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val, | |
621 | &cb->cb_addr, &cb->cb_port))) | |
622 | goto out_err; | |
623 | cb->cb_prog = se->se_callback_prog; | |
624 | cb->cb_ident = se->se_callback_ident; | |
1da177e4 LT |
625 | return; |
626 | out_err: | |
849823c5 | 627 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
1da177e4 LT |
628 | "will not receive delegations\n", |
629 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); | |
630 | ||
1da177e4 LT |
631 | return; |
632 | } | |
633 | ||
634 | /* | |
635 | * RFC 3010 has a complex implmentation description of processing a | |
636 | * SETCLIENTID request consisting of 5 bullets, labeled as | |
637 | * CASE0 - CASE4 below. | |
638 | * | |
639 | * NOTES: | |
640 | * callback information will be processed in a future patch | |
641 | * | |
642 | * an unconfirmed record is added when: | |
643 | * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record. | |
644 | * CASE 1: confirmed record found with matching name, principal, | |
645 | * verifier, and clientid. | |
646 | * CASE 2: confirmed record found with matching name, principal, | |
647 | * and there is no unconfirmed record with matching | |
648 | * name and principal | |
649 | * | |
650 | * an unconfirmed record is replaced when: | |
651 | * CASE 3: confirmed record found with matching name, principal, | |
652 | * and an unconfirmed record is found with matching | |
653 | * name, principal, and with clientid and | |
654 | * confirm that does not match the confirmed record. | |
655 | * CASE 4: there is no confirmed record with matching name and | |
656 | * principal. there is an unconfirmed record with | |
657 | * matching name, principal. | |
658 | * | |
659 | * an unconfirmed record is deleted when: | |
660 | * CASE 1: an unconfirmed record that matches input name, verifier, | |
661 | * and confirmed clientid. | |
662 | * CASE 4: any unconfirmed records with matching name and principal | |
663 | * that exist after an unconfirmed record has been replaced | |
664 | * as described above. | |
665 | * | |
666 | */ | |
667 | int | |
668 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid) | |
669 | { | |
670 | u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr; | |
671 | struct xdr_netobj clname = { | |
672 | .len = setclid->se_namelen, | |
673 | .data = setclid->se_name, | |
674 | }; | |
675 | nfs4_verifier clverifier = setclid->se_verf; | |
676 | unsigned int strhashval; | |
28ce6054 | 677 | struct nfs4_client *conf, *unconf, *new; |
1da177e4 | 678 | int status; |
a55370a3 | 679 | char dname[HEXDIR_LEN]; |
1da177e4 | 680 | |
1da177e4 | 681 | if (!check_name(clname)) |
73aea4ec | 682 | return nfserr_inval; |
1da177e4 | 683 | |
a55370a3 N |
684 | status = nfs4_make_rec_clidname(dname, &clname); |
685 | if (status) | |
73aea4ec | 686 | return status; |
a55370a3 | 687 | |
1da177e4 LT |
688 | /* |
689 | * XXX The Duplicate Request Cache (DRC) has been checked (??) | |
690 | * We get here on a DRC miss. | |
691 | */ | |
692 | ||
a55370a3 | 693 | strhashval = clientstr_hashval(dname); |
1da177e4 | 694 | |
1da177e4 | 695 | nfs4_lock_state(); |
28ce6054 N |
696 | conf = find_confirmed_client_by_str(dname, strhashval); |
697 | if (conf) { | |
1da177e4 LT |
698 | /* |
699 | * CASE 0: | |
700 | * clname match, confirmed, different principal | |
701 | * or different ip_address | |
702 | */ | |
703 | status = nfserr_clid_inuse; | |
28ce6054 N |
704 | if (!cmp_creds(&conf->cl_cred, &rqstp->rq_cred) |
705 | || conf->cl_addr != ip_addr) { | |
1da177e4 LT |
706 | printk("NFSD: setclientid: string in use by client" |
707 | "(clientid %08x/%08x)\n", | |
28ce6054 | 708 | conf->cl_clientid.cl_boot, conf->cl_clientid.cl_id); |
1da177e4 LT |
709 | goto out; |
710 | } | |
1da177e4 | 711 | } |
28ce6054 | 712 | unconf = find_unconfirmed_client_by_str(dname, strhashval); |
1da177e4 LT |
713 | status = nfserr_resource; |
714 | if (!conf) { | |
715 | /* | |
716 | * CASE 4: | |
717 | * placed first, because it is the normal case. | |
718 | */ | |
719 | if (unconf) | |
720 | expire_client(unconf); | |
a55370a3 N |
721 | new = create_client(clname, dname); |
722 | if (new == NULL) | |
1da177e4 LT |
723 | goto out; |
724 | copy_verf(new, &clverifier); | |
725 | new->cl_addr = ip_addr; | |
726 | copy_cred(&new->cl_cred,&rqstp->rq_cred); | |
727 | gen_clid(new); | |
728 | gen_confirm(new); | |
729 | gen_callback(new, setclid); | |
730 | add_to_unconfirmed(new, strhashval); | |
731 | } else if (cmp_verf(&conf->cl_verifier, &clverifier)) { | |
732 | /* | |
733 | * CASE 1: | |
734 | * cl_name match, confirmed, principal match | |
735 | * verifier match: probable callback update | |
736 | * | |
737 | * remove any unconfirmed nfs4_client with | |
738 | * matching cl_name, cl_verifier, and cl_clientid | |
739 | * | |
740 | * create and insert an unconfirmed nfs4_client with same | |
741 | * cl_name, cl_verifier, and cl_clientid as existing | |
742 | * nfs4_client, but with the new callback info and a | |
743 | * new cl_confirm | |
744 | */ | |
31f4a6c1 N |
745 | if (unconf) { |
746 | /* Note this is removing unconfirmed {*x***}, | |
747 | * which is stronger than RFC recommended {vxc**}. | |
748 | * This has the advantage that there is at most | |
749 | * one {*x***} in either list at any time. | |
750 | */ | |
751 | expire_client(unconf); | |
1da177e4 | 752 | } |
a55370a3 N |
753 | new = create_client(clname, dname); |
754 | if (new == NULL) | |
1da177e4 LT |
755 | goto out; |
756 | copy_verf(new,&conf->cl_verifier); | |
757 | new->cl_addr = ip_addr; | |
758 | copy_cred(&new->cl_cred,&rqstp->rq_cred); | |
759 | copy_clid(new, conf); | |
760 | gen_confirm(new); | |
761 | gen_callback(new, setclid); | |
762 | add_to_unconfirmed(new,strhashval); | |
763 | } else if (!unconf) { | |
764 | /* | |
765 | * CASE 2: | |
766 | * clname match, confirmed, principal match | |
767 | * verfier does not match | |
768 | * no unconfirmed. create a new unconfirmed nfs4_client | |
769 | * using input clverifier, clname, and callback info | |
770 | * and generate a new cl_clientid and cl_confirm. | |
771 | */ | |
a55370a3 N |
772 | new = create_client(clname, dname); |
773 | if (new == NULL) | |
1da177e4 LT |
774 | goto out; |
775 | copy_verf(new,&clverifier); | |
776 | new->cl_addr = ip_addr; | |
777 | copy_cred(&new->cl_cred,&rqstp->rq_cred); | |
778 | gen_clid(new); | |
779 | gen_confirm(new); | |
780 | gen_callback(new, setclid); | |
781 | add_to_unconfirmed(new, strhashval); | |
782 | } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) { | |
783 | /* | |
784 | * CASE3: | |
785 | * confirmed found (name, principal match) | |
786 | * confirmed verifier does not match input clverifier | |
787 | * | |
788 | * unconfirmed found (name match) | |
789 | * confirmed->cl_confirm != unconfirmed->cl_confirm | |
790 | * | |
791 | * remove unconfirmed. | |
792 | * | |
793 | * create an unconfirmed nfs4_client | |
794 | * with same cl_name as existing confirmed nfs4_client, | |
795 | * but with new callback info, new cl_clientid, | |
796 | * new cl_verifier and a new cl_confirm | |
797 | */ | |
798 | expire_client(unconf); | |
a55370a3 N |
799 | new = create_client(clname, dname); |
800 | if (new == NULL) | |
1da177e4 LT |
801 | goto out; |
802 | copy_verf(new,&clverifier); | |
803 | new->cl_addr = ip_addr; | |
804 | copy_cred(&new->cl_cred,&rqstp->rq_cred); | |
805 | gen_clid(new); | |
806 | gen_confirm(new); | |
807 | gen_callback(new, setclid); | |
808 | add_to_unconfirmed(new, strhashval); | |
809 | } else { | |
810 | /* No cases hit !!! */ | |
811 | status = nfserr_inval; | |
812 | goto out; | |
813 | ||
814 | } | |
815 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; | |
816 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; | |
817 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); | |
818 | status = nfs_ok; | |
819 | out: | |
820 | nfs4_unlock_state(); | |
821 | return status; | |
822 | } | |
823 | ||
824 | ||
825 | /* | |
826 | * RFC 3010 has a complex implmentation description of processing a | |
827 | * SETCLIENTID_CONFIRM request consisting of 4 bullets describing | |
828 | * processing on a DRC miss, labeled as CASE1 - CASE4 below. | |
829 | * | |
830 | * NOTE: callback information will be processed here in a future patch | |
831 | */ | |
832 | int | |
833 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm) | |
834 | { | |
835 | u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr; | |
21ab45a4 | 836 | struct nfs4_client *conf, *unconf; |
1da177e4 LT |
837 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
838 | clientid_t * clid = &setclientid_confirm->sc_clientid; | |
839 | int status; | |
840 | ||
841 | if (STALE_CLIENTID(clid)) | |
842 | return nfserr_stale_clientid; | |
843 | /* | |
844 | * XXX The Duplicate Request Cache (DRC) has been checked (??) | |
845 | * We get here on a DRC miss. | |
846 | */ | |
847 | ||
848 | nfs4_lock_state(); | |
21ab45a4 N |
849 | |
850 | conf = find_confirmed_client(clid); | |
851 | unconf = find_unconfirmed_client(clid); | |
852 | ||
853 | status = nfserr_clid_inuse; | |
854 | if (conf && conf->cl_addr != ip_addr) | |
855 | goto out; | |
856 | if (unconf && unconf->cl_addr != ip_addr) | |
857 | goto out; | |
858 | ||
1da177e4 LT |
859 | if ((conf && unconf) && |
860 | (cmp_verf(&unconf->cl_confirm, &confirm)) && | |
861 | (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) && | |
a55370a3 | 862 | (same_name(conf->cl_recdir,unconf->cl_recdir)) && |
1da177e4 | 863 | (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) { |
7c79f737 N |
864 | /* CASE 1: |
865 | * unconf record that matches input clientid and input confirm. | |
866 | * conf record that matches input clientid. | |
867 | * conf and unconf records match names, verifiers | |
868 | */ | |
1da177e4 LT |
869 | if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred)) |
870 | status = nfserr_clid_inuse; | |
871 | else { | |
1a69c179 N |
872 | /* XXX: We just turn off callbacks until we can handle |
873 | * change request correctly. */ | |
cb36d634 | 874 | atomic_set(&conf->cl_callback.cb_set, 0); |
21ab45a4 | 875 | gen_confirm(conf); |
46309029 | 876 | nfsd4_remove_clid_dir(unconf); |
1a69c179 | 877 | expire_client(unconf); |
1da177e4 | 878 | status = nfs_ok; |
1a69c179 | 879 | |
1da177e4 | 880 | } |
7c79f737 | 881 | } else if ((conf && !unconf) || |
1da177e4 LT |
882 | ((conf && unconf) && |
883 | (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) || | |
a55370a3 | 884 | !same_name(conf->cl_recdir, unconf->cl_recdir)))) { |
7c79f737 N |
885 | /* CASE 2: |
886 | * conf record that matches input clientid. | |
887 | * if unconf record matches input clientid, then | |
888 | * unconf->cl_name or unconf->cl_verifier don't match the | |
889 | * conf record. | |
890 | */ | |
21ab45a4 | 891 | if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred)) |
1da177e4 | 892 | status = nfserr_clid_inuse; |
21ab45a4 | 893 | else |
1da177e4 | 894 | status = nfs_ok; |
7c79f737 N |
895 | } else if (!conf && unconf |
896 | && cmp_verf(&unconf->cl_confirm, &confirm)) { | |
897 | /* CASE 3: | |
898 | * conf record not found. | |
899 | * unconf record found. | |
900 | * unconf->cl_confirm matches input confirm | |
901 | */ | |
1da177e4 LT |
902 | if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) { |
903 | status = nfserr_clid_inuse; | |
904 | } else { | |
1a69c179 N |
905 | unsigned int hash = |
906 | clientstr_hashval(unconf->cl_recdir); | |
907 | conf = find_confirmed_client_by_str(unconf->cl_recdir, | |
908 | hash); | |
909 | if (conf) { | |
c7b9a459 | 910 | nfsd4_remove_clid_dir(conf); |
1a69c179 N |
911 | expire_client(conf); |
912 | } | |
1da177e4 | 913 | move_to_confirmed(unconf); |
21ab45a4 | 914 | conf = unconf; |
1a69c179 | 915 | status = nfs_ok; |
1da177e4 | 916 | } |
7c79f737 N |
917 | } else if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm))) |
918 | && (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm, | |
919 | &confirm)))) { | |
920 | /* CASE 4: | |
921 | * conf record not found, or if conf, conf->cl_confirm does not | |
922 | * match input confirm. | |
923 | * unconf record not found, or if unconf, unconf->cl_confirm | |
924 | * does not match input confirm. | |
925 | */ | |
1da177e4 | 926 | status = nfserr_stale_clientid; |
7c79f737 | 927 | } else { |
08e8987c N |
928 | /* check that we have hit one of the cases...*/ |
929 | status = nfserr_clid_inuse; | |
930 | } | |
1da177e4 LT |
931 | out: |
932 | if (!status) | |
21ab45a4 | 933 | nfsd4_probe_callback(conf); |
1da177e4 LT |
934 | nfs4_unlock_state(); |
935 | return status; | |
936 | } | |
937 | ||
938 | /* | |
939 | * Open owner state (share locks) | |
940 | */ | |
941 | ||
942 | /* hash tables for nfs4_stateowner */ | |
943 | #define OWNER_HASH_BITS 8 | |
944 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) | |
945 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) | |
946 | ||
947 | #define ownerid_hashval(id) \ | |
948 | ((id) & OWNER_HASH_MASK) | |
949 | #define ownerstr_hashval(clientid, ownername) \ | |
950 | (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK) | |
951 | ||
952 | static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE]; | |
953 | static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE]; | |
954 | ||
955 | /* hash table for nfs4_file */ | |
956 | #define FILE_HASH_BITS 8 | |
957 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) | |
958 | #define FILE_HASH_MASK (FILE_HASH_SIZE - 1) | |
959 | /* hash table for (open)nfs4_stateid */ | |
960 | #define STATEID_HASH_BITS 10 | |
961 | #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS) | |
962 | #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1) | |
963 | ||
964 | #define file_hashval(x) \ | |
965 | hash_ptr(x, FILE_HASH_BITS) | |
966 | #define stateid_hashval(owner_id, file_id) \ | |
967 | (((owner_id) + (file_id)) & STATEID_HASH_MASK) | |
968 | ||
969 | static struct list_head file_hashtbl[FILE_HASH_SIZE]; | |
970 | static struct list_head stateid_hashtbl[STATEID_HASH_SIZE]; | |
971 | ||
972 | /* OPEN Share state helper functions */ | |
973 | static inline struct nfs4_file * | |
974 | alloc_init_file(struct inode *ino) | |
975 | { | |
976 | struct nfs4_file *fp; | |
977 | unsigned int hashval = file_hashval(ino); | |
978 | ||
e60d4398 N |
979 | fp = kmem_cache_alloc(file_slab, GFP_KERNEL); |
980 | if (fp) { | |
13cd2184 | 981 | kref_init(&fp->fi_ref); |
1da177e4 | 982 | INIT_LIST_HEAD(&fp->fi_hash); |
8beefa24 N |
983 | INIT_LIST_HEAD(&fp->fi_stateids); |
984 | INIT_LIST_HEAD(&fp->fi_delegations); | |
1da177e4 LT |
985 | list_add(&fp->fi_hash, &file_hashtbl[hashval]); |
986 | fp->fi_inode = igrab(ino); | |
987 | fp->fi_id = current_fileid++; | |
1da177e4 LT |
988 | return fp; |
989 | } | |
990 | return NULL; | |
991 | } | |
992 | ||
e60d4398 N |
993 | static void |
994 | nfsd4_free_slab(kmem_cache_t **slab) | |
1da177e4 | 995 | { |
e60d4398 N |
996 | int status; |
997 | ||
998 | if (*slab == NULL) | |
999 | return; | |
1000 | status = kmem_cache_destroy(*slab); | |
1001 | *slab = NULL; | |
1002 | WARN_ON(status); | |
1da177e4 LT |
1003 | } |
1004 | ||
1005 | static void | |
1006 | nfsd4_free_slabs(void) | |
1007 | { | |
e60d4398 N |
1008 | nfsd4_free_slab(&stateowner_slab); |
1009 | nfsd4_free_slab(&file_slab); | |
5ac049ac | 1010 | nfsd4_free_slab(&stateid_slab); |
5b2d21c1 | 1011 | nfsd4_free_slab(&deleg_slab); |
e60d4398 | 1012 | } |
1da177e4 | 1013 | |
e60d4398 N |
1014 | static int |
1015 | nfsd4_init_slabs(void) | |
1016 | { | |
1017 | stateowner_slab = kmem_cache_create("nfsd4_stateowners", | |
1018 | sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL); | |
1019 | if (stateowner_slab == NULL) | |
1020 | goto out_nomem; | |
1021 | file_slab = kmem_cache_create("nfsd4_files", | |
1022 | sizeof(struct nfs4_file), 0, 0, NULL, NULL); | |
1023 | if (file_slab == NULL) | |
1024 | goto out_nomem; | |
5ac049ac N |
1025 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
1026 | sizeof(struct nfs4_stateid), 0, 0, NULL, NULL); | |
1027 | if (stateid_slab == NULL) | |
1028 | goto out_nomem; | |
5b2d21c1 N |
1029 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
1030 | sizeof(struct nfs4_delegation), 0, 0, NULL, NULL); | |
1031 | if (deleg_slab == NULL) | |
1032 | goto out_nomem; | |
e60d4398 N |
1033 | return 0; |
1034 | out_nomem: | |
1035 | nfsd4_free_slabs(); | |
1036 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); | |
1037 | return -ENOMEM; | |
1da177e4 LT |
1038 | } |
1039 | ||
1040 | void | |
1041 | nfs4_free_stateowner(struct kref *kref) | |
1042 | { | |
1043 | struct nfs4_stateowner *sop = | |
1044 | container_of(kref, struct nfs4_stateowner, so_ref); | |
1045 | kfree(sop->so_owner.data); | |
1046 | kmem_cache_free(stateowner_slab, sop); | |
1047 | } | |
1048 | ||
1049 | static inline struct nfs4_stateowner * | |
1050 | alloc_stateowner(struct xdr_netobj *owner) | |
1051 | { | |
1052 | struct nfs4_stateowner *sop; | |
1053 | ||
1054 | if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) { | |
1055 | if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) { | |
1056 | memcpy(sop->so_owner.data, owner->data, owner->len); | |
1057 | sop->so_owner.len = owner->len; | |
1058 | kref_init(&sop->so_ref); | |
1059 | return sop; | |
1060 | } | |
1061 | kmem_cache_free(stateowner_slab, sop); | |
1062 | } | |
1063 | return NULL; | |
1064 | } | |
1065 | ||
1066 | static struct nfs4_stateowner * | |
1067 | alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) { | |
1068 | struct nfs4_stateowner *sop; | |
1069 | struct nfs4_replay *rp; | |
1070 | unsigned int idhashval; | |
1071 | ||
1072 | if (!(sop = alloc_stateowner(&open->op_owner))) | |
1073 | return NULL; | |
1074 | idhashval = ownerid_hashval(current_ownerid); | |
1075 | INIT_LIST_HEAD(&sop->so_idhash); | |
1076 | INIT_LIST_HEAD(&sop->so_strhash); | |
1077 | INIT_LIST_HEAD(&sop->so_perclient); | |
ea1da636 N |
1078 | INIT_LIST_HEAD(&sop->so_stateids); |
1079 | INIT_LIST_HEAD(&sop->so_perstateid); /* not used */ | |
1da177e4 LT |
1080 | INIT_LIST_HEAD(&sop->so_close_lru); |
1081 | sop->so_time = 0; | |
1082 | list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]); | |
1083 | list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]); | |
ea1da636 | 1084 | list_add(&sop->so_perclient, &clp->cl_openowners); |
1da177e4 LT |
1085 | sop->so_is_open_owner = 1; |
1086 | sop->so_id = current_ownerid++; | |
1087 | sop->so_client = clp; | |
1088 | sop->so_seqid = open->op_seqid; | |
1089 | sop->so_confirmed = 0; | |
1090 | rp = &sop->so_replay; | |
1091 | rp->rp_status = NFSERR_SERVERFAULT; | |
1092 | rp->rp_buflen = 0; | |
1093 | rp->rp_buf = rp->rp_ibuf; | |
1094 | return sop; | |
1095 | } | |
1096 | ||
1097 | static void | |
1098 | release_stateid_lockowners(struct nfs4_stateid *open_stp) | |
1099 | { | |
1100 | struct nfs4_stateowner *lock_sop; | |
1101 | ||
ea1da636 N |
1102 | while (!list_empty(&open_stp->st_lockowners)) { |
1103 | lock_sop = list_entry(open_stp->st_lockowners.next, | |
1104 | struct nfs4_stateowner, so_perstateid); | |
1105 | /* list_del(&open_stp->st_lockowners); */ | |
1da177e4 LT |
1106 | BUG_ON(lock_sop->so_is_open_owner); |
1107 | release_stateowner(lock_sop); | |
1108 | } | |
1109 | } | |
1110 | ||
1111 | static void | |
1112 | unhash_stateowner(struct nfs4_stateowner *sop) | |
1113 | { | |
1114 | struct nfs4_stateid *stp; | |
1115 | ||
1116 | list_del(&sop->so_idhash); | |
1117 | list_del(&sop->so_strhash); | |
6fa305de | 1118 | if (sop->so_is_open_owner) |
1da177e4 | 1119 | list_del(&sop->so_perclient); |
ea1da636 N |
1120 | list_del(&sop->so_perstateid); |
1121 | while (!list_empty(&sop->so_stateids)) { | |
1122 | stp = list_entry(sop->so_stateids.next, | |
1123 | struct nfs4_stateid, st_perstateowner); | |
1da177e4 LT |
1124 | if (sop->so_is_open_owner) |
1125 | release_stateid(stp, OPEN_STATE); | |
1126 | else | |
1127 | release_stateid(stp, LOCK_STATE); | |
1128 | } | |
1129 | } | |
1130 | ||
1131 | static void | |
1132 | release_stateowner(struct nfs4_stateowner *sop) | |
1133 | { | |
1134 | unhash_stateowner(sop); | |
1135 | list_del(&sop->so_close_lru); | |
1136 | nfs4_put_stateowner(sop); | |
1137 | } | |
1138 | ||
1139 | static inline void | |
1140 | init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) { | |
1141 | struct nfs4_stateowner *sop = open->op_stateowner; | |
1142 | unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id); | |
1143 | ||
1144 | INIT_LIST_HEAD(&stp->st_hash); | |
ea1da636 N |
1145 | INIT_LIST_HEAD(&stp->st_perstateowner); |
1146 | INIT_LIST_HEAD(&stp->st_lockowners); | |
1da177e4 LT |
1147 | INIT_LIST_HEAD(&stp->st_perfile); |
1148 | list_add(&stp->st_hash, &stateid_hashtbl[hashval]); | |
ea1da636 | 1149 | list_add(&stp->st_perstateowner, &sop->so_stateids); |
8beefa24 | 1150 | list_add(&stp->st_perfile, &fp->fi_stateids); |
1da177e4 | 1151 | stp->st_stateowner = sop; |
13cd2184 | 1152 | get_nfs4_file(fp); |
1da177e4 LT |
1153 | stp->st_file = fp; |
1154 | stp->st_stateid.si_boot = boot_time; | |
1155 | stp->st_stateid.si_stateownerid = sop->so_id; | |
1156 | stp->st_stateid.si_fileid = fp->fi_id; | |
1157 | stp->st_stateid.si_generation = 0; | |
1158 | stp->st_access_bmap = 0; | |
1159 | stp->st_deny_bmap = 0; | |
1160 | __set_bit(open->op_share_access, &stp->st_access_bmap); | |
1161 | __set_bit(open->op_share_deny, &stp->st_deny_bmap); | |
4c4cd222 | 1162 | stp->st_openstp = NULL; |
1da177e4 LT |
1163 | } |
1164 | ||
1165 | static void | |
1166 | release_stateid(struct nfs4_stateid *stp, int flags) | |
1167 | { | |
1168 | struct file *filp = stp->st_vfs_file; | |
1169 | ||
1170 | list_del(&stp->st_hash); | |
1da177e4 | 1171 | list_del(&stp->st_perfile); |
ea1da636 | 1172 | list_del(&stp->st_perstateowner); |
1da177e4 LT |
1173 | if (flags & OPEN_STATE) { |
1174 | release_stateid_lockowners(stp); | |
1175 | stp->st_vfs_file = NULL; | |
1176 | nfsd_close(filp); | |
1da177e4 LT |
1177 | } else if (flags & LOCK_STATE) |
1178 | locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner); | |
13cd2184 | 1179 | put_nfs4_file(stp->st_file); |
5ac049ac | 1180 | kmem_cache_free(stateid_slab, stp); |
1da177e4 LT |
1181 | stp = NULL; |
1182 | } | |
1183 | ||
fd39ca9a | 1184 | static void |
1da177e4 LT |
1185 | move_to_close_lru(struct nfs4_stateowner *sop) |
1186 | { | |
1187 | dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop); | |
1188 | ||
1189 | unhash_stateowner(sop); | |
1190 | list_add_tail(&sop->so_close_lru, &close_lru); | |
1191 | sop->so_time = get_seconds(); | |
1192 | } | |
1193 | ||
fd39ca9a | 1194 | static void |
1da177e4 LT |
1195 | release_state_owner(struct nfs4_stateid *stp, int flag) |
1196 | { | |
1197 | struct nfs4_stateowner *sop = stp->st_stateowner; | |
1da177e4 LT |
1198 | |
1199 | dprintk("NFSD: release_state_owner\n"); | |
1200 | release_stateid(stp, flag); | |
1201 | ||
1202 | /* place unused nfs4_stateowners on so_close_lru list to be | |
1203 | * released by the laundromat service after the lease period | |
1204 | * to enable us to handle CLOSE replay | |
1205 | */ | |
ea1da636 | 1206 | if (sop->so_confirmed && list_empty(&sop->so_stateids)) |
1da177e4 | 1207 | move_to_close_lru(sop); |
1da177e4 LT |
1208 | } |
1209 | ||
1210 | static int | |
1211 | cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) { | |
1212 | return ((sop->so_owner.len == owner->len) && | |
1213 | !memcmp(sop->so_owner.data, owner->data, owner->len) && | |
1214 | (sop->so_client->cl_clientid.cl_id == clid->cl_id)); | |
1215 | } | |
1216 | ||
1217 | static struct nfs4_stateowner * | |
1218 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open) | |
1219 | { | |
1220 | struct nfs4_stateowner *so = NULL; | |
1221 | ||
1222 | list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) { | |
1223 | if (cmp_owner_str(so, &open->op_owner, &open->op_clientid)) | |
1224 | return so; | |
1225 | } | |
1226 | return NULL; | |
1227 | } | |
1228 | ||
1229 | /* search file_hashtbl[] for file */ | |
1230 | static struct nfs4_file * | |
1231 | find_file(struct inode *ino) | |
1232 | { | |
1233 | unsigned int hashval = file_hashval(ino); | |
1234 | struct nfs4_file *fp; | |
1235 | ||
1236 | list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) { | |
13cd2184 N |
1237 | if (fp->fi_inode == ino) { |
1238 | get_nfs4_file(fp); | |
1da177e4 | 1239 | return fp; |
13cd2184 | 1240 | } |
1da177e4 LT |
1241 | } |
1242 | return NULL; | |
1243 | } | |
1244 | ||
1245 | #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0) | |
1246 | #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0) | |
1247 | ||
fd39ca9a | 1248 | static void |
1da177e4 LT |
1249 | set_access(unsigned int *access, unsigned long bmap) { |
1250 | int i; | |
1251 | ||
1252 | *access = 0; | |
1253 | for (i = 1; i < 4; i++) { | |
1254 | if (test_bit(i, &bmap)) | |
1255 | *access |= i; | |
1256 | } | |
1257 | } | |
1258 | ||
fd39ca9a | 1259 | static void |
1da177e4 LT |
1260 | set_deny(unsigned int *deny, unsigned long bmap) { |
1261 | int i; | |
1262 | ||
1263 | *deny = 0; | |
1264 | for (i = 0; i < 4; i++) { | |
1265 | if (test_bit(i, &bmap)) | |
1266 | *deny |= i ; | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | static int | |
1271 | test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) { | |
1272 | unsigned int access, deny; | |
1273 | ||
1274 | set_access(&access, stp->st_access_bmap); | |
1275 | set_deny(&deny, stp->st_deny_bmap); | |
1276 | if ((access & open->op_share_deny) || (deny & open->op_share_access)) | |
1277 | return 0; | |
1278 | return 1; | |
1279 | } | |
1280 | ||
1281 | /* | |
1282 | * Called to check deny when READ with all zero stateid or | |
1283 | * WRITE with all zero or all one stateid | |
1284 | */ | |
fd39ca9a | 1285 | static int |
1da177e4 LT |
1286 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
1287 | { | |
1288 | struct inode *ino = current_fh->fh_dentry->d_inode; | |
1289 | struct nfs4_file *fp; | |
1290 | struct nfs4_stateid *stp; | |
13cd2184 | 1291 | int ret; |
1da177e4 LT |
1292 | |
1293 | dprintk("NFSD: nfs4_share_conflict\n"); | |
1294 | ||
1295 | fp = find_file(ino); | |
13cd2184 N |
1296 | if (!fp) |
1297 | return nfs_ok; | |
b700949b | 1298 | ret = nfserr_locked; |
1da177e4 | 1299 | /* Search for conflicting share reservations */ |
13cd2184 N |
1300 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) { |
1301 | if (test_bit(deny_type, &stp->st_deny_bmap) || | |
1302 | test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap)) | |
1303 | goto out; | |
1da177e4 | 1304 | } |
13cd2184 N |
1305 | ret = nfs_ok; |
1306 | out: | |
1307 | put_nfs4_file(fp); | |
1308 | return ret; | |
1da177e4 LT |
1309 | } |
1310 | ||
1311 | static inline void | |
1312 | nfs4_file_downgrade(struct file *filp, unsigned int share_access) | |
1313 | { | |
1314 | if (share_access & NFS4_SHARE_ACCESS_WRITE) { | |
1315 | put_write_access(filp->f_dentry->d_inode); | |
1316 | filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE; | |
1317 | } | |
1318 | } | |
1319 | ||
1320 | /* | |
1321 | * Recall a delegation | |
1322 | */ | |
1323 | static int | |
1324 | do_recall(void *__dp) | |
1325 | { | |
1326 | struct nfs4_delegation *dp = __dp; | |
1327 | ||
1328 | daemonize("nfsv4-recall"); | |
1329 | ||
1330 | nfsd4_cb_recall(dp); | |
1331 | return 0; | |
1332 | } | |
1333 | ||
1334 | /* | |
1335 | * Spawn a thread to perform a recall on the delegation represented | |
1336 | * by the lease (file_lock) | |
1337 | * | |
1338 | * Called from break_lease() with lock_kernel() held. | |
1339 | * Note: we assume break_lease will only call this *once* for any given | |
1340 | * lease. | |
1341 | */ | |
1342 | static | |
1343 | void nfsd_break_deleg_cb(struct file_lock *fl) | |
1344 | { | |
1345 | struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner; | |
1346 | struct task_struct *t; | |
1347 | ||
1348 | dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl); | |
1349 | if (!dp) | |
1350 | return; | |
1351 | ||
1352 | /* We're assuming the state code never drops its reference | |
1353 | * without first removing the lease. Since we're in this lease | |
1354 | * callback (and since the lease code is serialized by the kernel | |
1355 | * lock) we know the server hasn't removed the lease yet, we know | |
1356 | * it's safe to take a reference: */ | |
1357 | atomic_inc(&dp->dl_count); | |
1358 | ||
1359 | spin_lock(&recall_lock); | |
1360 | list_add_tail(&dp->dl_recall_lru, &del_recall_lru); | |
1361 | spin_unlock(&recall_lock); | |
1362 | ||
1363 | /* only place dl_time is set. protected by lock_kernel*/ | |
1364 | dp->dl_time = get_seconds(); | |
1365 | ||
1366 | /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */ | |
1367 | fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ; | |
1368 | ||
1369 | t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall"); | |
1370 | if (IS_ERR(t)) { | |
1371 | struct nfs4_client *clp = dp->dl_client; | |
1372 | ||
1373 | printk(KERN_INFO "NFSD: Callback thread failed for " | |
1374 | "for client (clientid %08x/%08x)\n", | |
1375 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); | |
1376 | nfs4_put_delegation(dp); | |
1377 | } | |
1378 | } | |
1379 | ||
1380 | /* | |
1381 | * The file_lock is being reapd. | |
1382 | * | |
1383 | * Called by locks_free_lock() with lock_kernel() held. | |
1384 | */ | |
1385 | static | |
1386 | void nfsd_release_deleg_cb(struct file_lock *fl) | |
1387 | { | |
1388 | struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner; | |
1389 | ||
1390 | dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count)); | |
1391 | ||
1392 | if (!(fl->fl_flags & FL_LEASE) || !dp) | |
1393 | return; | |
1394 | dp->dl_flock = NULL; | |
1395 | } | |
1396 | ||
1397 | /* | |
1398 | * Set the delegation file_lock back pointer. | |
1399 | * | |
1400 | * Called from __setlease() with lock_kernel() held. | |
1401 | */ | |
1402 | static | |
1403 | void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl) | |
1404 | { | |
1405 | struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner; | |
1406 | ||
1407 | dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp); | |
1408 | if (!dp) | |
1409 | return; | |
1410 | dp->dl_flock = new; | |
1411 | } | |
1412 | ||
1413 | /* | |
1414 | * Called from __setlease() with lock_kernel() held | |
1415 | */ | |
1416 | static | |
1417 | int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try) | |
1418 | { | |
1419 | struct nfs4_delegation *onlistd = | |
1420 | (struct nfs4_delegation *)onlist->fl_owner; | |
1421 | struct nfs4_delegation *tryd = | |
1422 | (struct nfs4_delegation *)try->fl_owner; | |
1423 | ||
1424 | if (onlist->fl_lmops != try->fl_lmops) | |
1425 | return 0; | |
1426 | ||
1427 | return onlistd->dl_client == tryd->dl_client; | |
1428 | } | |
1429 | ||
1430 | ||
1431 | static | |
1432 | int nfsd_change_deleg_cb(struct file_lock **onlist, int arg) | |
1433 | { | |
1434 | if (arg & F_UNLCK) | |
1435 | return lease_modify(onlist, arg); | |
1436 | else | |
1437 | return -EAGAIN; | |
1438 | } | |
1439 | ||
fd39ca9a | 1440 | static struct lock_manager_operations nfsd_lease_mng_ops = { |
1da177e4 LT |
1441 | .fl_break = nfsd_break_deleg_cb, |
1442 | .fl_release_private = nfsd_release_deleg_cb, | |
1443 | .fl_copy_lock = nfsd_copy_lock_deleg_cb, | |
1444 | .fl_mylease = nfsd_same_client_deleg_cb, | |
1445 | .fl_change = nfsd_change_deleg_cb, | |
1446 | }; | |
1447 | ||
1448 | ||
1449 | /* | |
1450 | * nfsd4_process_open1() | |
1451 | * lookup stateowner. | |
1452 | * found: | |
1453 | * check confirmed | |
1454 | * confirmed: | |
1455 | * check seqid | |
1456 | * not confirmed: | |
1457 | * delete owner | |
1458 | * create new owner | |
1459 | * notfound: | |
1460 | * verify clientid | |
1461 | * create new owner | |
1462 | * | |
1463 | * called with nfs4_lock_state() held. | |
1464 | */ | |
1465 | int | |
1466 | nfsd4_process_open1(struct nfsd4_open *open) | |
1467 | { | |
1468 | int status; | |
1469 | clientid_t *clientid = &open->op_clientid; | |
1470 | struct nfs4_client *clp = NULL; | |
1471 | unsigned int strhashval; | |
1472 | struct nfs4_stateowner *sop = NULL; | |
1473 | ||
1474 | status = nfserr_inval; | |
1475 | if (!check_name(open->op_owner)) | |
1476 | goto out; | |
1477 | ||
1478 | if (STALE_CLIENTID(&open->op_clientid)) | |
1479 | return nfserr_stale_clientid; | |
1480 | ||
1481 | strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner); | |
1482 | sop = find_openstateowner_str(strhashval, open); | |
1483 | if (sop) { | |
1484 | open->op_stateowner = sop; | |
1485 | /* check for replay */ | |
bd9aac52 | 1486 | if (open->op_seqid == sop->so_seqid - 1){ |
1da177e4 LT |
1487 | if (sop->so_replay.rp_buflen) |
1488 | return NFSERR_REPLAY_ME; | |
1489 | else { | |
1490 | /* The original OPEN failed so spectacularly | |
1491 | * that we don't even have replay data saved! | |
1492 | * Therefore, we have no choice but to continue | |
1493 | * processing this OPEN; presumably, we'll | |
1494 | * fail again for the same reason. | |
1495 | */ | |
1496 | dprintk("nfsd4_process_open1:" | |
1497 | " replay with no replay cache\n"); | |
1498 | goto renew; | |
1499 | } | |
1500 | } else if (sop->so_confirmed) { | |
bd9aac52 | 1501 | if (open->op_seqid == sop->so_seqid) |
1da177e4 LT |
1502 | goto renew; |
1503 | status = nfserr_bad_seqid; | |
1504 | goto out; | |
1505 | } else { | |
1506 | /* If we get here, we received an OPEN for an | |
1507 | * unconfirmed nfs4_stateowner. Since the seqid's are | |
1508 | * different, purge the existing nfs4_stateowner, and | |
1509 | * instantiate a new one. | |
1510 | */ | |
1511 | clp = sop->so_client; | |
1512 | release_stateowner(sop); | |
1513 | } | |
1514 | } else { | |
1515 | /* nfs4_stateowner not found. | |
1516 | * Verify clientid and instantiate new nfs4_stateowner. | |
1517 | * If verify fails this is presumably the result of the | |
1518 | * client's lease expiring. | |
1519 | */ | |
1520 | status = nfserr_expired; | |
1521 | clp = find_confirmed_client(clientid); | |
1522 | if (clp == NULL) | |
1523 | goto out; | |
1524 | } | |
1525 | status = nfserr_resource; | |
1526 | sop = alloc_init_open_stateowner(strhashval, clp, open); | |
1527 | if (sop == NULL) | |
1528 | goto out; | |
1529 | open->op_stateowner = sop; | |
1530 | renew: | |
1531 | status = nfs_ok; | |
1532 | renew_client(sop->so_client); | |
1533 | out: | |
1da177e4 LT |
1534 | return status; |
1535 | } | |
1536 | ||
4a6e43e6 N |
1537 | static inline int |
1538 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) | |
1539 | { | |
1540 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) | |
1541 | return nfserr_openmode; | |
1542 | else | |
1543 | return nfs_ok; | |
1544 | } | |
1545 | ||
52f4fb43 N |
1546 | static struct nfs4_delegation * |
1547 | find_delegation_file(struct nfs4_file *fp, stateid_t *stid) | |
1548 | { | |
1549 | struct nfs4_delegation *dp; | |
1550 | ||
ea1da636 | 1551 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) { |
52f4fb43 N |
1552 | if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) |
1553 | return dp; | |
1554 | } | |
1555 | return NULL; | |
1556 | } | |
1557 | ||
c44c5eeb | 1558 | static int |
567d9829 N |
1559 | nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open, |
1560 | struct nfs4_delegation **dp) | |
1561 | { | |
1562 | int flags; | |
c44c5eeb | 1563 | int status = nfserr_bad_stateid; |
567d9829 N |
1564 | |
1565 | *dp = find_delegation_file(fp, &open->op_delegate_stateid); | |
1566 | if (*dp == NULL) | |
c44c5eeb | 1567 | goto out; |
567d9829 N |
1568 | flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ? |
1569 | RD_STATE : WR_STATE; | |
1570 | status = nfs4_check_delegmode(*dp, flags); | |
1571 | if (status) | |
1572 | *dp = NULL; | |
c44c5eeb N |
1573 | out: |
1574 | if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR) | |
1575 | return nfs_ok; | |
1576 | if (status) | |
1577 | return status; | |
1578 | open->op_stateowner->so_confirmed = 1; | |
1579 | return nfs_ok; | |
567d9829 N |
1580 | } |
1581 | ||
1da177e4 LT |
1582 | static int |
1583 | nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp) | |
1584 | { | |
1585 | struct nfs4_stateid *local; | |
1586 | int status = nfserr_share_denied; | |
1587 | struct nfs4_stateowner *sop = open->op_stateowner; | |
1588 | ||
8beefa24 | 1589 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
1da177e4 LT |
1590 | /* ignore lock owners */ |
1591 | if (local->st_stateowner->so_is_open_owner == 0) | |
1592 | continue; | |
1593 | /* remember if we have seen this open owner */ | |
1594 | if (local->st_stateowner == sop) | |
1595 | *stpp = local; | |
1596 | /* check for conflicting share reservations */ | |
1597 | if (!test_share(local, open)) | |
1598 | goto out; | |
1599 | } | |
1600 | status = 0; | |
1601 | out: | |
1602 | return status; | |
1603 | } | |
1604 | ||
5ac049ac N |
1605 | static inline struct nfs4_stateid * |
1606 | nfs4_alloc_stateid(void) | |
1607 | { | |
1608 | return kmem_cache_alloc(stateid_slab, GFP_KERNEL); | |
1609 | } | |
1610 | ||
1da177e4 LT |
1611 | static int |
1612 | nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp, | |
567d9829 | 1613 | struct nfs4_delegation *dp, |
1da177e4 LT |
1614 | struct svc_fh *cur_fh, int flags) |
1615 | { | |
1616 | struct nfs4_stateid *stp; | |
1da177e4 | 1617 | |
5ac049ac | 1618 | stp = nfs4_alloc_stateid(); |
1da177e4 LT |
1619 | if (stp == NULL) |
1620 | return nfserr_resource; | |
1621 | ||
567d9829 N |
1622 | if (dp) { |
1623 | get_file(dp->dl_vfs_file); | |
1624 | stp->st_vfs_file = dp->dl_vfs_file; | |
1625 | } else { | |
1626 | int status; | |
1627 | status = nfsd_open(rqstp, cur_fh, S_IFREG, flags, | |
1628 | &stp->st_vfs_file); | |
1629 | if (status) { | |
1630 | if (status == nfserr_dropit) | |
1631 | status = nfserr_jukebox; | |
5ac049ac | 1632 | kmem_cache_free(stateid_slab, stp); |
567d9829 N |
1633 | return status; |
1634 | } | |
1da177e4 | 1635 | } |
1da177e4 LT |
1636 | *stpp = stp; |
1637 | return 0; | |
1638 | } | |
1639 | ||
1640 | static inline int | |
1641 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, | |
1642 | struct nfsd4_open *open) | |
1643 | { | |
1644 | struct iattr iattr = { | |
1645 | .ia_valid = ATTR_SIZE, | |
1646 | .ia_size = 0, | |
1647 | }; | |
1648 | if (!open->op_truncate) | |
1649 | return 0; | |
1650 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) | |
1651 | return -EINVAL; | |
1652 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); | |
1653 | } | |
1654 | ||
1655 | static int | |
1656 | nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open) | |
1657 | { | |
1658 | struct file *filp = stp->st_vfs_file; | |
1659 | struct inode *inode = filp->f_dentry->d_inode; | |
1660 | unsigned int share_access; | |
1661 | int status; | |
1662 | ||
1663 | set_access(&share_access, stp->st_access_bmap); | |
1664 | share_access = ~share_access; | |
1665 | share_access &= open->op_share_access; | |
1666 | ||
1667 | if (!(share_access & NFS4_SHARE_ACCESS_WRITE)) | |
1668 | return nfsd4_truncate(rqstp, cur_fh, open); | |
1669 | ||
1670 | status = get_write_access(inode); | |
1671 | if (status) | |
1672 | return nfserrno(status); | |
1673 | status = nfsd4_truncate(rqstp, cur_fh, open); | |
1674 | if (status) { | |
1675 | put_write_access(inode); | |
1676 | return status; | |
1677 | } | |
1678 | /* remember the open */ | |
1679 | filp->f_mode = (filp->f_mode | FMODE_WRITE) & ~FMODE_READ; | |
1680 | set_bit(open->op_share_access, &stp->st_access_bmap); | |
1681 | set_bit(open->op_share_deny, &stp->st_deny_bmap); | |
1682 | ||
1683 | return nfs_ok; | |
1684 | } | |
1685 | ||
1686 | ||
1da177e4 | 1687 | static void |
37515177 | 1688 | nfs4_set_claim_prev(struct nfsd4_open *open) |
1da177e4 | 1689 | { |
37515177 N |
1690 | open->op_stateowner->so_confirmed = 1; |
1691 | open->op_stateowner->so_client->cl_firststate = 1; | |
1da177e4 LT |
1692 | } |
1693 | ||
1694 | /* | |
1695 | * Attempt to hand out a delegation. | |
1696 | */ | |
1697 | static void | |
1698 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp) | |
1699 | { | |
1700 | struct nfs4_delegation *dp; | |
1701 | struct nfs4_stateowner *sop = stp->st_stateowner; | |
1702 | struct nfs4_callback *cb = &sop->so_client->cl_callback; | |
1703 | struct file_lock fl, *flp = &fl; | |
1704 | int status, flag = 0; | |
1705 | ||
1706 | flag = NFS4_OPEN_DELEGATE_NONE; | |
7b190fec N |
1707 | open->op_recall = 0; |
1708 | switch (open->op_claim_type) { | |
1709 | case NFS4_OPEN_CLAIM_PREVIOUS: | |
1710 | if (!atomic_read(&cb->cb_set)) | |
1711 | open->op_recall = 1; | |
1712 | flag = open->op_delegate_type; | |
1713 | if (flag == NFS4_OPEN_DELEGATE_NONE) | |
1714 | goto out; | |
1715 | break; | |
1716 | case NFS4_OPEN_CLAIM_NULL: | |
1717 | /* Let's not give out any delegations till everyone's | |
1718 | * had the chance to reclaim theirs.... */ | |
1719 | if (nfs4_in_grace()) | |
1720 | goto out; | |
1721 | if (!atomic_read(&cb->cb_set) || !sop->so_confirmed) | |
1722 | goto out; | |
1723 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) | |
1724 | flag = NFS4_OPEN_DELEGATE_WRITE; | |
1725 | else | |
1726 | flag = NFS4_OPEN_DELEGATE_READ; | |
1727 | break; | |
1728 | default: | |
1729 | goto out; | |
1730 | } | |
1da177e4 LT |
1731 | |
1732 | dp = alloc_init_deleg(sop->so_client, stp, fh, flag); | |
1733 | if (dp == NULL) { | |
1734 | flag = NFS4_OPEN_DELEGATE_NONE; | |
1735 | goto out; | |
1736 | } | |
1737 | locks_init_lock(&fl); | |
1738 | fl.fl_lmops = &nfsd_lease_mng_ops; | |
1739 | fl.fl_flags = FL_LEASE; | |
1740 | fl.fl_end = OFFSET_MAX; | |
1741 | fl.fl_owner = (fl_owner_t)dp; | |
1742 | fl.fl_file = stp->st_vfs_file; | |
1743 | fl.fl_pid = current->tgid; | |
1744 | ||
1745 | /* setlease checks to see if delegation should be handed out. | |
1746 | * the lock_manager callbacks fl_mylease and fl_change are used | |
1747 | */ | |
1748 | if ((status = setlease(stp->st_vfs_file, | |
1749 | flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) { | |
1750 | dprintk("NFSD: setlease failed [%d], no delegation\n", status); | |
c907132d | 1751 | unhash_delegation(dp); |
1da177e4 LT |
1752 | flag = NFS4_OPEN_DELEGATE_NONE; |
1753 | goto out; | |
1754 | } | |
1755 | ||
1756 | memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid)); | |
1757 | ||
1758 | dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n", | |
1759 | dp->dl_stateid.si_boot, | |
1760 | dp->dl_stateid.si_stateownerid, | |
1761 | dp->dl_stateid.si_fileid, | |
1762 | dp->dl_stateid.si_generation); | |
1763 | out: | |
7b190fec N |
1764 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS |
1765 | && flag == NFS4_OPEN_DELEGATE_NONE | |
1766 | && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) | |
1767 | printk("NFSD: WARNING: refusing delegation reclaim\n"); | |
1da177e4 LT |
1768 | open->op_delegate_type = flag; |
1769 | } | |
1770 | ||
1771 | /* | |
1772 | * called with nfs4_lock_state() held. | |
1773 | */ | |
1774 | int | |
1775 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) | |
1776 | { | |
1777 | struct nfs4_file *fp = NULL; | |
1778 | struct inode *ino = current_fh->fh_dentry->d_inode; | |
1779 | struct nfs4_stateid *stp = NULL; | |
567d9829 | 1780 | struct nfs4_delegation *dp = NULL; |
1da177e4 LT |
1781 | int status; |
1782 | ||
b648330a N |
1783 | if (nfs4_in_grace() && open->op_claim_type != NFS4_OPEN_CLAIM_PREVIOUS) |
1784 | return nfserr_grace; | |
1785 | ||
1786 | if (!nfs4_in_grace() && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) | |
1787 | return nfserr_no_grace; | |
1788 | ||
1da177e4 LT |
1789 | status = nfserr_inval; |
1790 | if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny)) | |
1791 | goto out; | |
1792 | /* | |
1793 | * Lookup file; if found, lookup stateid and check open request, | |
1794 | * and check for delegations in the process of being recalled. | |
1795 | * If not found, create the nfs4_file struct | |
1796 | */ | |
1797 | fp = find_file(ino); | |
1798 | if (fp) { | |
1799 | if ((status = nfs4_check_open(fp, open, &stp))) | |
1800 | goto out; | |
c44c5eeb N |
1801 | status = nfs4_check_deleg(fp, open, &dp); |
1802 | if (status) | |
1803 | goto out; | |
1da177e4 | 1804 | } else { |
c44c5eeb N |
1805 | status = nfserr_bad_stateid; |
1806 | if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR) | |
1807 | goto out; | |
1da177e4 LT |
1808 | status = nfserr_resource; |
1809 | fp = alloc_init_file(ino); | |
1810 | if (fp == NULL) | |
1811 | goto out; | |
1812 | } | |
1813 | ||
1814 | /* | |
1815 | * OPEN the file, or upgrade an existing OPEN. | |
1816 | * If truncate fails, the OPEN fails. | |
1817 | */ | |
1818 | if (stp) { | |
1819 | /* Stateid was found, this is an OPEN upgrade */ | |
1820 | status = nfs4_upgrade_open(rqstp, current_fh, stp, open); | |
1821 | if (status) | |
1822 | goto out; | |
444c2c07 | 1823 | update_stateid(&stp->st_stateid); |
1da177e4 LT |
1824 | } else { |
1825 | /* Stateid was not found, this is a new OPEN */ | |
1826 | int flags = 0; | |
1827 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) | |
1828 | flags = MAY_WRITE; | |
1829 | else | |
1830 | flags = MAY_READ; | |
567d9829 N |
1831 | status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags); |
1832 | if (status) | |
1da177e4 LT |
1833 | goto out; |
1834 | init_stateid(stp, fp, open); | |
1835 | status = nfsd4_truncate(rqstp, current_fh, open); | |
1836 | if (status) { | |
1837 | release_stateid(stp, OPEN_STATE); | |
1838 | goto out; | |
1839 | } | |
1840 | } | |
1841 | memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t)); | |
1842 | ||
1843 | /* | |
1844 | * Attempt to hand out a delegation. No error return, because the | |
1845 | * OPEN succeeds even if we fail. | |
1846 | */ | |
1847 | nfs4_open_delegation(current_fh, open, stp); | |
1848 | ||
1849 | status = nfs_ok; | |
1850 | ||
1851 | dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n", | |
1852 | stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid, | |
1853 | stp->st_stateid.si_fileid, stp->st_stateid.si_generation); | |
1854 | out: | |
13cd2184 N |
1855 | if (fp) |
1856 | put_nfs4_file(fp); | |
37515177 N |
1857 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
1858 | nfs4_set_claim_prev(open); | |
1da177e4 LT |
1859 | /* |
1860 | * To finish the open response, we just need to set the rflags. | |
1861 | */ | |
1862 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; | |
1863 | if (!open->op_stateowner->so_confirmed) | |
1864 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; | |
1865 | ||
1866 | return status; | |
1867 | } | |
1868 | ||
58da282b | 1869 | static struct workqueue_struct *laundry_wq; |
1da177e4 LT |
1870 | static struct work_struct laundromat_work; |
1871 | static void laundromat_main(void *); | |
1872 | static DECLARE_WORK(laundromat_work, laundromat_main, NULL); | |
1873 | ||
1874 | int | |
1875 | nfsd4_renew(clientid_t *clid) | |
1876 | { | |
1877 | struct nfs4_client *clp; | |
1878 | int status; | |
1879 | ||
1880 | nfs4_lock_state(); | |
1881 | dprintk("process_renew(%08x/%08x): starting\n", | |
1882 | clid->cl_boot, clid->cl_id); | |
1883 | status = nfserr_stale_clientid; | |
1884 | if (STALE_CLIENTID(clid)) | |
1885 | goto out; | |
1886 | clp = find_confirmed_client(clid); | |
1887 | status = nfserr_expired; | |
1888 | if (clp == NULL) { | |
1889 | /* We assume the client took too long to RENEW. */ | |
1890 | dprintk("nfsd4_renew: clientid not found!\n"); | |
1891 | goto out; | |
1892 | } | |
1893 | renew_client(clp); | |
1894 | status = nfserr_cb_path_down; | |
ea1da636 | 1895 | if (!list_empty(&clp->cl_delegations) |
1da177e4 LT |
1896 | && !atomic_read(&clp->cl_callback.cb_set)) |
1897 | goto out; | |
1898 | status = nfs_ok; | |
1899 | out: | |
1900 | nfs4_unlock_state(); | |
1901 | return status; | |
1902 | } | |
1903 | ||
a76b4319 N |
1904 | static void |
1905 | end_grace(void) | |
1906 | { | |
1907 | dprintk("NFSD: end of grace period\n"); | |
c7b9a459 | 1908 | nfsd4_recdir_purge_old(); |
a76b4319 N |
1909 | in_grace = 0; |
1910 | } | |
1911 | ||
fd39ca9a | 1912 | static time_t |
1da177e4 LT |
1913 | nfs4_laundromat(void) |
1914 | { | |
1915 | struct nfs4_client *clp; | |
1916 | struct nfs4_stateowner *sop; | |
1917 | struct nfs4_delegation *dp; | |
1918 | struct list_head *pos, *next, reaplist; | |
1919 | time_t cutoff = get_seconds() - NFSD_LEASE_TIME; | |
1920 | time_t t, clientid_val = NFSD_LEASE_TIME; | |
1921 | time_t u, test_val = NFSD_LEASE_TIME; | |
1922 | ||
1923 | nfs4_lock_state(); | |
1924 | ||
1925 | dprintk("NFSD: laundromat service - starting\n"); | |
a76b4319 N |
1926 | if (in_grace) |
1927 | end_grace(); | |
1da177e4 LT |
1928 | list_for_each_safe(pos, next, &client_lru) { |
1929 | clp = list_entry(pos, struct nfs4_client, cl_lru); | |
1930 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { | |
1931 | t = clp->cl_time - cutoff; | |
1932 | if (clientid_val > t) | |
1933 | clientid_val = t; | |
1934 | break; | |
1935 | } | |
1936 | dprintk("NFSD: purging unused client (clientid %08x)\n", | |
1937 | clp->cl_clientid.cl_id); | |
c7b9a459 | 1938 | nfsd4_remove_clid_dir(clp); |
1da177e4 LT |
1939 | expire_client(clp); |
1940 | } | |
1941 | INIT_LIST_HEAD(&reaplist); | |
1942 | spin_lock(&recall_lock); | |
1943 | list_for_each_safe(pos, next, &del_recall_lru) { | |
1944 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); | |
1945 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { | |
1946 | u = dp->dl_time - cutoff; | |
1947 | if (test_val > u) | |
1948 | test_val = u; | |
1949 | break; | |
1950 | } | |
1951 | dprintk("NFSD: purging unused delegation dp %p, fp %p\n", | |
1952 | dp, dp->dl_flock); | |
1953 | list_move(&dp->dl_recall_lru, &reaplist); | |
1954 | } | |
1955 | spin_unlock(&recall_lock); | |
1956 | list_for_each_safe(pos, next, &reaplist) { | |
1957 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); | |
1958 | list_del_init(&dp->dl_recall_lru); | |
1959 | unhash_delegation(dp); | |
1960 | } | |
1961 | test_val = NFSD_LEASE_TIME; | |
1962 | list_for_each_safe(pos, next, &close_lru) { | |
1963 | sop = list_entry(pos, struct nfs4_stateowner, so_close_lru); | |
1964 | if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) { | |
1965 | u = sop->so_time - cutoff; | |
1966 | if (test_val > u) | |
1967 | test_val = u; | |
1968 | break; | |
1969 | } | |
1970 | dprintk("NFSD: purging unused open stateowner (so_id %d)\n", | |
1971 | sop->so_id); | |
1972 | list_del(&sop->so_close_lru); | |
1973 | nfs4_put_stateowner(sop); | |
1974 | } | |
1975 | if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT) | |
1976 | clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT; | |
1977 | nfs4_unlock_state(); | |
1978 | return clientid_val; | |
1979 | } | |
1980 | ||
1981 | void | |
1982 | laundromat_main(void *not_used) | |
1983 | { | |
1984 | time_t t; | |
1985 | ||
1986 | t = nfs4_laundromat(); | |
1987 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); | |
58da282b | 1988 | queue_delayed_work(laundry_wq, &laundromat_work, t*HZ); |
1da177e4 LT |
1989 | } |
1990 | ||
fd39ca9a | 1991 | static struct nfs4_stateowner * |
f8816512 N |
1992 | search_close_lru(u32 st_id, int flags) |
1993 | { | |
1da177e4 LT |
1994 | struct nfs4_stateowner *local = NULL; |
1995 | ||
1da177e4 LT |
1996 | if (flags & CLOSE_STATE) { |
1997 | list_for_each_entry(local, &close_lru, so_close_lru) { | |
1998 | if (local->so_id == st_id) | |
1999 | return local; | |
2000 | } | |
2001 | } | |
2002 | return NULL; | |
2003 | } | |
2004 | ||
2005 | static inline int | |
2006 | nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp) | |
2007 | { | |
2008 | return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode; | |
2009 | } | |
2010 | ||
2011 | static int | |
2012 | STALE_STATEID(stateid_t *stateid) | |
2013 | { | |
2014 | if (stateid->si_boot == boot_time) | |
2015 | return 0; | |
849823c5 | 2016 | dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n", |
1da177e4 LT |
2017 | stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid, |
2018 | stateid->si_generation); | |
2019 | return 1; | |
2020 | } | |
2021 | ||
2022 | static inline int | |
2023 | access_permit_read(unsigned long access_bmap) | |
2024 | { | |
2025 | return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) || | |
2026 | test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) || | |
2027 | test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap); | |
2028 | } | |
2029 | ||
2030 | static inline int | |
2031 | access_permit_write(unsigned long access_bmap) | |
2032 | { | |
2033 | return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) || | |
2034 | test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap); | |
2035 | } | |
2036 | ||
2037 | static | |
2038 | int nfs4_check_openmode(struct nfs4_stateid *stp, int flags) | |
2039 | { | |
2040 | int status = nfserr_openmode; | |
2041 | ||
2042 | if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap))) | |
2043 | goto out; | |
2044 | if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap))) | |
2045 | goto out; | |
2046 | status = nfs_ok; | |
2047 | out: | |
2048 | return status; | |
2049 | } | |
2050 | ||
1da177e4 LT |
2051 | static inline int |
2052 | check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags) | |
2053 | { | |
2054 | /* Trying to call delegreturn with a special stateid? Yuch: */ | |
2055 | if (!(flags & (RD_STATE | WR_STATE))) | |
2056 | return nfserr_bad_stateid; | |
2057 | else if (ONE_STATEID(stateid) && (flags & RD_STATE)) | |
2058 | return nfs_ok; | |
2059 | else if (nfs4_in_grace()) { | |
2060 | /* Answer in remaining cases depends on existance of | |
2061 | * conflicting state; so we must wait out the grace period. */ | |
2062 | return nfserr_grace; | |
2063 | } else if (flags & WR_STATE) | |
2064 | return nfs4_share_conflict(current_fh, | |
2065 | NFS4_SHARE_DENY_WRITE); | |
2066 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ | |
2067 | return nfs4_share_conflict(current_fh, | |
2068 | NFS4_SHARE_DENY_READ); | |
2069 | } | |
2070 | ||
2071 | /* | |
2072 | * Allow READ/WRITE during grace period on recovered state only for files | |
2073 | * that are not able to provide mandatory locking. | |
2074 | */ | |
2075 | static inline int | |
2076 | io_during_grace_disallowed(struct inode *inode, int flags) | |
2077 | { | |
2078 | return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE)) | |
2079 | && MANDATORY_LOCK(inode); | |
2080 | } | |
2081 | ||
2082 | /* | |
2083 | * Checks for stateid operations | |
2084 | */ | |
2085 | int | |
2086 | nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp) | |
2087 | { | |
2088 | struct nfs4_stateid *stp = NULL; | |
2089 | struct nfs4_delegation *dp = NULL; | |
2090 | stateid_t *stidp; | |
2091 | struct inode *ino = current_fh->fh_dentry->d_inode; | |
2092 | int status; | |
2093 | ||
2094 | dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n", | |
2095 | stateid->si_boot, stateid->si_stateownerid, | |
2096 | stateid->si_fileid, stateid->si_generation); | |
2097 | if (filpp) | |
2098 | *filpp = NULL; | |
2099 | ||
2100 | if (io_during_grace_disallowed(ino, flags)) | |
2101 | return nfserr_grace; | |
2102 | ||
2103 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) | |
2104 | return check_special_stateids(current_fh, stateid, flags); | |
2105 | ||
2106 | /* STALE STATEID */ | |
2107 | status = nfserr_stale_stateid; | |
2108 | if (STALE_STATEID(stateid)) | |
2109 | goto out; | |
2110 | ||
2111 | /* BAD STATEID */ | |
2112 | status = nfserr_bad_stateid; | |
2113 | if (!stateid->si_fileid) { /* delegation stateid */ | |
2114 | if(!(dp = find_delegation_stateid(ino, stateid))) { | |
2115 | dprintk("NFSD: delegation stateid not found\n"); | |
2116 | if (nfs4_in_grace()) | |
2117 | status = nfserr_grace; | |
2118 | goto out; | |
2119 | } | |
2120 | stidp = &dp->dl_stateid; | |
2121 | } else { /* open or lock stateid */ | |
2122 | if (!(stp = find_stateid(stateid, flags))) { | |
2123 | dprintk("NFSD: open or lock stateid not found\n"); | |
2124 | if (nfs4_in_grace()) | |
2125 | status = nfserr_grace; | |
2126 | goto out; | |
2127 | } | |
2128 | if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) | |
2129 | goto out; | |
2130 | if (!stp->st_stateowner->so_confirmed) | |
2131 | goto out; | |
2132 | stidp = &stp->st_stateid; | |
2133 | } | |
2134 | if (stateid->si_generation > stidp->si_generation) | |
2135 | goto out; | |
2136 | ||
2137 | /* OLD STATEID */ | |
2138 | status = nfserr_old_stateid; | |
2139 | if (stateid->si_generation < stidp->si_generation) | |
2140 | goto out; | |
2141 | if (stp) { | |
2142 | if ((status = nfs4_check_openmode(stp,flags))) | |
2143 | goto out; | |
2144 | renew_client(stp->st_stateowner->so_client); | |
2145 | if (filpp) | |
2146 | *filpp = stp->st_vfs_file; | |
2147 | } else if (dp) { | |
2148 | if ((status = nfs4_check_delegmode(dp, flags))) | |
2149 | goto out; | |
2150 | renew_client(dp->dl_client); | |
2151 | if (flags & DELEG_RET) | |
2152 | unhash_delegation(dp); | |
2153 | if (filpp) | |
2154 | *filpp = dp->dl_vfs_file; | |
2155 | } | |
2156 | status = nfs_ok; | |
2157 | out: | |
2158 | return status; | |
2159 | } | |
2160 | ||
4c4cd222 N |
2161 | static inline int |
2162 | setlkflg (int type) | |
2163 | { | |
2164 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? | |
2165 | RD_STATE : WR_STATE; | |
2166 | } | |
1da177e4 LT |
2167 | |
2168 | /* | |
2169 | * Checks for sequence id mutating operations. | |
2170 | */ | |
fd39ca9a | 2171 | static int |
4c4cd222 | 2172 | nfs4_preprocess_seqid_op(struct svc_fh *current_fh, u32 seqid, stateid_t *stateid, int flags, struct nfs4_stateowner **sopp, struct nfs4_stateid **stpp, struct nfsd4_lock *lock) |
1da177e4 | 2173 | { |
1da177e4 LT |
2174 | struct nfs4_stateid *stp; |
2175 | struct nfs4_stateowner *sop; | |
2176 | ||
2177 | dprintk("NFSD: preprocess_seqid_op: seqid=%d " | |
2178 | "stateid = (%08x/%08x/%08x/%08x)\n", seqid, | |
2179 | stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid, | |
2180 | stateid->si_generation); | |
3a4f98bb | 2181 | |
1da177e4 LT |
2182 | *stpp = NULL; |
2183 | *sopp = NULL; | |
2184 | ||
1da177e4 LT |
2185 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
2186 | printk("NFSD: preprocess_seqid_op: magic stateid!\n"); | |
3a4f98bb | 2187 | return nfserr_bad_stateid; |
1da177e4 LT |
2188 | } |
2189 | ||
1da177e4 | 2190 | if (STALE_STATEID(stateid)) |
3a4f98bb | 2191 | return nfserr_stale_stateid; |
1da177e4 LT |
2192 | /* |
2193 | * We return BAD_STATEID if filehandle doesn't match stateid, | |
2194 | * the confirmed flag is incorrecly set, or the generation | |
2195 | * number is incorrect. | |
1da177e4 | 2196 | */ |
f8816512 N |
2197 | stp = find_stateid(stateid, flags); |
2198 | if (stp == NULL) { | |
2199 | /* | |
2200 | * Also, we should make sure this isn't just the result of | |
2201 | * a replayed close: | |
2202 | */ | |
2203 | sop = search_close_lru(stateid->si_stateownerid, flags); | |
2204 | if (sop == NULL) | |
2205 | return nfserr_bad_stateid; | |
2206 | *sopp = sop; | |
2207 | goto check_replay; | |
2208 | } | |
1da177e4 | 2209 | |
4c4cd222 | 2210 | if (lock) { |
1da177e4 | 2211 | struct nfs4_stateowner *sop = stp->st_stateowner; |
4c4cd222 | 2212 | clientid_t *lockclid = &lock->v.new.clientid; |
1da177e4 | 2213 | struct nfs4_client *clp = sop->so_client; |
4c4cd222 N |
2214 | int lkflg = 0; |
2215 | int status; | |
2216 | ||
2217 | lkflg = setlkflg(lock->lk_type); | |
2218 | ||
2219 | if (lock->lk_is_new) { | |
2220 | if (!sop->so_is_open_owner) | |
2221 | return nfserr_bad_stateid; | |
2222 | if (!cmp_clid(&clp->cl_clientid, lockclid)) | |
2223 | return nfserr_bad_stateid; | |
2224 | /* stp is the open stateid */ | |
2225 | status = nfs4_check_openmode(stp, lkflg); | |
2226 | if (status) | |
2227 | return status; | |
2228 | } else { | |
2229 | /* stp is the lock stateid */ | |
2230 | status = nfs4_check_openmode(stp->st_openstp, lkflg); | |
2231 | if (status) | |
2232 | return status; | |
2233 | } | |
1da177e4 | 2234 | |
1da177e4 LT |
2235 | } |
2236 | ||
2237 | if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) { | |
2238 | printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n"); | |
3a4f98bb | 2239 | return nfserr_bad_stateid; |
1da177e4 LT |
2240 | } |
2241 | ||
2242 | *stpp = stp; | |
2243 | *sopp = sop = stp->st_stateowner; | |
2244 | ||
2245 | /* | |
2246 | * We now validate the seqid and stateid generation numbers. | |
2247 | * For the moment, we ignore the possibility of | |
2248 | * generation number wraparound. | |
2249 | */ | |
bd9aac52 | 2250 | if (seqid != sop->so_seqid) |
1da177e4 LT |
2251 | goto check_replay; |
2252 | ||
3a4f98bb N |
2253 | if (sop->so_confirmed && flags & CONFIRM) { |
2254 | printk("NFSD: preprocess_seqid_op: expected" | |
2255 | " unconfirmed stateowner!\n"); | |
2256 | return nfserr_bad_stateid; | |
1da177e4 | 2257 | } |
3a4f98bb N |
2258 | if (!sop->so_confirmed && !(flags & CONFIRM)) { |
2259 | printk("NFSD: preprocess_seqid_op: stateowner not" | |
2260 | " confirmed yet!\n"); | |
2261 | return nfserr_bad_stateid; | |
1da177e4 LT |
2262 | } |
2263 | if (stateid->si_generation > stp->st_stateid.si_generation) { | |
2264 | printk("NFSD: preprocess_seqid_op: future stateid?!\n"); | |
3a4f98bb | 2265 | return nfserr_bad_stateid; |
1da177e4 LT |
2266 | } |
2267 | ||
1da177e4 LT |
2268 | if (stateid->si_generation < stp->st_stateid.si_generation) { |
2269 | printk("NFSD: preprocess_seqid_op: old stateid!\n"); | |
3a4f98bb | 2270 | return nfserr_old_stateid; |
1da177e4 | 2271 | } |
52fd004e | 2272 | renew_client(sop->so_client); |
3a4f98bb | 2273 | return nfs_ok; |
1da177e4 | 2274 | |
1da177e4 | 2275 | check_replay: |
bd9aac52 | 2276 | if (seqid == sop->so_seqid - 1) { |
849823c5 | 2277 | dprintk("NFSD: preprocess_seqid_op: retransmission?\n"); |
1da177e4 | 2278 | /* indicate replay to calling function */ |
3a4f98bb | 2279 | return NFSERR_REPLAY_ME; |
1da177e4 | 2280 | } |
3a4f98bb N |
2281 | printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n", |
2282 | sop->so_seqid, seqid); | |
2283 | *sopp = NULL; | |
2284 | return nfserr_bad_seqid; | |
1da177e4 LT |
2285 | } |
2286 | ||
2287 | int | |
f2327d9a | 2288 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc, struct nfs4_stateowner **replay_owner) |
1da177e4 LT |
2289 | { |
2290 | int status; | |
2291 | struct nfs4_stateowner *sop; | |
2292 | struct nfs4_stateid *stp; | |
2293 | ||
2294 | dprintk("NFSD: nfsd4_open_confirm on file %.*s\n", | |
2295 | (int)current_fh->fh_dentry->d_name.len, | |
2296 | current_fh->fh_dentry->d_name.name); | |
2297 | ||
2298 | if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) | |
2299 | goto out; | |
2300 | ||
2301 | nfs4_lock_state(); | |
2302 | ||
2303 | if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid, | |
2304 | &oc->oc_req_stateid, | |
2305 | CHECK_FH | CONFIRM | OPEN_STATE, | |
2306 | &oc->oc_stateowner, &stp, NULL))) | |
2307 | goto out; | |
2308 | ||
2309 | sop = oc->oc_stateowner; | |
2310 | sop->so_confirmed = 1; | |
2311 | update_stateid(&stp->st_stateid); | |
2312 | memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t)); | |
2313 | dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d " | |
2314 | "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid, | |
2315 | stp->st_stateid.si_boot, | |
2316 | stp->st_stateid.si_stateownerid, | |
2317 | stp->st_stateid.si_fileid, | |
2318 | stp->st_stateid.si_generation); | |
c7b9a459 N |
2319 | |
2320 | nfsd4_create_clid_dir(sop->so_client); | |
1da177e4 | 2321 | out: |
f2327d9a | 2322 | if (oc->oc_stateowner) { |
1da177e4 | 2323 | nfs4_get_stateowner(oc->oc_stateowner); |
f2327d9a NB |
2324 | *replay_owner = oc->oc_stateowner; |
2325 | } | |
1da177e4 LT |
2326 | nfs4_unlock_state(); |
2327 | return status; | |
2328 | } | |
2329 | ||
2330 | ||
2331 | /* | |
2332 | * unset all bits in union bitmap (bmap) that | |
2333 | * do not exist in share (from successful OPEN_DOWNGRADE) | |
2334 | */ | |
2335 | static void | |
2336 | reset_union_bmap_access(unsigned long access, unsigned long *bmap) | |
2337 | { | |
2338 | int i; | |
2339 | for (i = 1; i < 4; i++) { | |
2340 | if ((i & access) != i) | |
2341 | __clear_bit(i, bmap); | |
2342 | } | |
2343 | } | |
2344 | ||
2345 | static void | |
2346 | reset_union_bmap_deny(unsigned long deny, unsigned long *bmap) | |
2347 | { | |
2348 | int i; | |
2349 | for (i = 0; i < 4; i++) { | |
2350 | if ((i & deny) != i) | |
2351 | __clear_bit(i, bmap); | |
2352 | } | |
2353 | } | |
2354 | ||
2355 | int | |
f2327d9a | 2356 | nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od, struct nfs4_stateowner **replay_owner) |
1da177e4 LT |
2357 | { |
2358 | int status; | |
2359 | struct nfs4_stateid *stp; | |
2360 | unsigned int share_access; | |
2361 | ||
2362 | dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n", | |
2363 | (int)current_fh->fh_dentry->d_name.len, | |
2364 | current_fh->fh_dentry->d_name.name); | |
2365 | ||
2366 | if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny)) | |
2367 | return nfserr_inval; | |
2368 | ||
2369 | nfs4_lock_state(); | |
2370 | if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid, | |
2371 | &od->od_stateid, | |
2372 | CHECK_FH | OPEN_STATE, | |
2373 | &od->od_stateowner, &stp, NULL))) | |
2374 | goto out; | |
2375 | ||
2376 | status = nfserr_inval; | |
2377 | if (!test_bit(od->od_share_access, &stp->st_access_bmap)) { | |
2378 | dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n", | |
2379 | stp->st_access_bmap, od->od_share_access); | |
2380 | goto out; | |
2381 | } | |
2382 | if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) { | |
2383 | dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n", | |
2384 | stp->st_deny_bmap, od->od_share_deny); | |
2385 | goto out; | |
2386 | } | |
2387 | set_access(&share_access, stp->st_access_bmap); | |
2388 | nfs4_file_downgrade(stp->st_vfs_file, | |
2389 | share_access & ~od->od_share_access); | |
2390 | ||
2391 | reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap); | |
2392 | reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap); | |
2393 | ||
2394 | update_stateid(&stp->st_stateid); | |
2395 | memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t)); | |
2396 | status = nfs_ok; | |
2397 | out: | |
f2327d9a | 2398 | if (od->od_stateowner) { |
1da177e4 | 2399 | nfs4_get_stateowner(od->od_stateowner); |
f2327d9a NB |
2400 | *replay_owner = od->od_stateowner; |
2401 | } | |
1da177e4 LT |
2402 | nfs4_unlock_state(); |
2403 | return status; | |
2404 | } | |
2405 | ||
2406 | /* | |
2407 | * nfs4_unlock_state() called after encode | |
2408 | */ | |
2409 | int | |
f2327d9a | 2410 | nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close, struct nfs4_stateowner **replay_owner) |
1da177e4 LT |
2411 | { |
2412 | int status; | |
2413 | struct nfs4_stateid *stp; | |
2414 | ||
2415 | dprintk("NFSD: nfsd4_close on file %.*s\n", | |
2416 | (int)current_fh->fh_dentry->d_name.len, | |
2417 | current_fh->fh_dentry->d_name.name); | |
2418 | ||
2419 | nfs4_lock_state(); | |
2420 | /* check close_lru for replay */ | |
2421 | if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid, | |
2422 | &close->cl_stateid, | |
2423 | CHECK_FH | OPEN_STATE | CLOSE_STATE, | |
2424 | &close->cl_stateowner, &stp, NULL))) | |
2425 | goto out; | |
2426 | /* | |
2427 | * Return success, but first update the stateid. | |
2428 | */ | |
2429 | status = nfs_ok; | |
2430 | update_stateid(&stp->st_stateid); | |
2431 | memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t)); | |
2432 | ||
2433 | /* release_state_owner() calls nfsd_close() if needed */ | |
2434 | release_state_owner(stp, OPEN_STATE); | |
2435 | out: | |
f2327d9a | 2436 | if (close->cl_stateowner) { |
1da177e4 | 2437 | nfs4_get_stateowner(close->cl_stateowner); |
f2327d9a NB |
2438 | *replay_owner = close->cl_stateowner; |
2439 | } | |
1da177e4 LT |
2440 | nfs4_unlock_state(); |
2441 | return status; | |
2442 | } | |
2443 | ||
2444 | int | |
2445 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr) | |
2446 | { | |
2447 | int status; | |
2448 | ||
2449 | if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) | |
2450 | goto out; | |
2451 | ||
2452 | nfs4_lock_state(); | |
2453 | status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL); | |
2454 | nfs4_unlock_state(); | |
2455 | out: | |
2456 | return status; | |
2457 | } | |
2458 | ||
2459 | ||
2460 | /* | |
2461 | * Lock owner state (byte-range locks) | |
2462 | */ | |
2463 | #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start)) | |
2464 | #define LOCK_HASH_BITS 8 | |
2465 | #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS) | |
2466 | #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1) | |
2467 | ||
2468 | #define lockownerid_hashval(id) \ | |
2469 | ((id) & LOCK_HASH_MASK) | |
2470 | ||
2471 | static inline unsigned int | |
2472 | lock_ownerstr_hashval(struct inode *inode, u32 cl_id, | |
2473 | struct xdr_netobj *ownername) | |
2474 | { | |
2475 | return (file_hashval(inode) + cl_id | |
2476 | + opaque_hashval(ownername->data, ownername->len)) | |
2477 | & LOCK_HASH_MASK; | |
2478 | } | |
2479 | ||
2480 | static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE]; | |
2481 | static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE]; | |
2482 | static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE]; | |
2483 | ||
fd39ca9a | 2484 | static struct nfs4_stateid * |
1da177e4 LT |
2485 | find_stateid(stateid_t *stid, int flags) |
2486 | { | |
2487 | struct nfs4_stateid *local = NULL; | |
2488 | u32 st_id = stid->si_stateownerid; | |
2489 | u32 f_id = stid->si_fileid; | |
2490 | unsigned int hashval; | |
2491 | ||
2492 | dprintk("NFSD: find_stateid flags 0x%x\n",flags); | |
2493 | if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) { | |
2494 | hashval = stateid_hashval(st_id, f_id); | |
2495 | list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) { | |
2496 | if ((local->st_stateid.si_stateownerid == st_id) && | |
2497 | (local->st_stateid.si_fileid == f_id)) | |
2498 | return local; | |
2499 | } | |
2500 | } | |
2501 | if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) { | |
2502 | hashval = stateid_hashval(st_id, f_id); | |
2503 | list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) { | |
2504 | if ((local->st_stateid.si_stateownerid == st_id) && | |
2505 | (local->st_stateid.si_fileid == f_id)) | |
2506 | return local; | |
2507 | } | |
849823c5 | 2508 | } |
1da177e4 LT |
2509 | return NULL; |
2510 | } | |
2511 | ||
2512 | static struct nfs4_delegation * | |
2513 | find_delegation_stateid(struct inode *ino, stateid_t *stid) | |
2514 | { | |
13cd2184 N |
2515 | struct nfs4_file *fp; |
2516 | struct nfs4_delegation *dl; | |
1da177e4 LT |
2517 | |
2518 | dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n", | |
2519 | stid->si_boot, stid->si_stateownerid, | |
2520 | stid->si_fileid, stid->si_generation); | |
2521 | ||
1da177e4 | 2522 | fp = find_file(ino); |
13cd2184 N |
2523 | if (!fp) |
2524 | return NULL; | |
2525 | dl = find_delegation_file(fp, stid); | |
2526 | put_nfs4_file(fp); | |
2527 | return dl; | |
1da177e4 LT |
2528 | } |
2529 | ||
2530 | /* | |
2531 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that | |
2532 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th | |
2533 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit | |
2534 | * locking, this prevents us from being completely protocol-compliant. The | |
2535 | * real solution to this problem is to start using unsigned file offsets in | |
2536 | * the VFS, but this is a very deep change! | |
2537 | */ | |
2538 | static inline void | |
2539 | nfs4_transform_lock_offset(struct file_lock *lock) | |
2540 | { | |
2541 | if (lock->fl_start < 0) | |
2542 | lock->fl_start = OFFSET_MAX; | |
2543 | if (lock->fl_end < 0) | |
2544 | lock->fl_end = OFFSET_MAX; | |
2545 | } | |
2546 | ||
fd39ca9a | 2547 | static int |
1da177e4 LT |
2548 | nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval) |
2549 | { | |
2550 | struct nfs4_stateowner *local = NULL; | |
2551 | int status = 0; | |
2552 | ||
2553 | if (hashval >= LOCK_HASH_SIZE) | |
2554 | goto out; | |
2555 | list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) { | |
2556 | if (local == sop) { | |
2557 | status = 1; | |
2558 | goto out; | |
2559 | } | |
2560 | } | |
2561 | out: | |
2562 | return status; | |
2563 | } | |
2564 | ||
2565 | ||
2566 | static inline void | |
2567 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) | |
2568 | { | |
2569 | struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner; | |
2570 | unsigned int hval = lockownerid_hashval(sop->so_id); | |
2571 | ||
2572 | deny->ld_sop = NULL; | |
2573 | if (nfs4_verify_lock_stateowner(sop, hval)) { | |
2574 | kref_get(&sop->so_ref); | |
2575 | deny->ld_sop = sop; | |
2576 | deny->ld_clientid = sop->so_client->cl_clientid; | |
2577 | } | |
2578 | deny->ld_start = fl->fl_start; | |
2579 | deny->ld_length = ~(u64)0; | |
2580 | if (fl->fl_end != ~(u64)0) | |
2581 | deny->ld_length = fl->fl_end - fl->fl_start + 1; | |
2582 | deny->ld_type = NFS4_READ_LT; | |
2583 | if (fl->fl_type != F_RDLCK) | |
2584 | deny->ld_type = NFS4_WRITE_LT; | |
2585 | } | |
2586 | ||
1da177e4 LT |
2587 | static struct nfs4_stateowner * |
2588 | find_lockstateowner_str(struct inode *inode, clientid_t *clid, | |
2589 | struct xdr_netobj *owner) | |
2590 | { | |
2591 | unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner); | |
2592 | struct nfs4_stateowner *op; | |
2593 | ||
2594 | list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) { | |
2595 | if (cmp_owner_str(op, owner, clid)) | |
2596 | return op; | |
2597 | } | |
2598 | return NULL; | |
2599 | } | |
2600 | ||
2601 | /* | |
2602 | * Alloc a lock owner structure. | |
2603 | * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has | |
2604 | * occured. | |
2605 | * | |
2606 | * strhashval = lock_ownerstr_hashval | |
1da177e4 LT |
2607 | */ |
2608 | ||
2609 | static struct nfs4_stateowner * | |
2610 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) { | |
2611 | struct nfs4_stateowner *sop; | |
2612 | struct nfs4_replay *rp; | |
2613 | unsigned int idhashval; | |
2614 | ||
2615 | if (!(sop = alloc_stateowner(&lock->lk_new_owner))) | |
2616 | return NULL; | |
2617 | idhashval = lockownerid_hashval(current_ownerid); | |
2618 | INIT_LIST_HEAD(&sop->so_idhash); | |
2619 | INIT_LIST_HEAD(&sop->so_strhash); | |
2620 | INIT_LIST_HEAD(&sop->so_perclient); | |
ea1da636 N |
2621 | INIT_LIST_HEAD(&sop->so_stateids); |
2622 | INIT_LIST_HEAD(&sop->so_perstateid); | |
1da177e4 LT |
2623 | INIT_LIST_HEAD(&sop->so_close_lru); /* not used */ |
2624 | sop->so_time = 0; | |
2625 | list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]); | |
2626 | list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]); | |
ea1da636 | 2627 | list_add(&sop->so_perstateid, &open_stp->st_lockowners); |
1da177e4 LT |
2628 | sop->so_is_open_owner = 0; |
2629 | sop->so_id = current_ownerid++; | |
2630 | sop->so_client = clp; | |
b59e3c0e NB |
2631 | /* It is the openowner seqid that will be incremented in encode in the |
2632 | * case of new lockowners; so increment the lock seqid manually: */ | |
2633 | sop->so_seqid = lock->lk_new_lock_seqid + 1; | |
1da177e4 LT |
2634 | sop->so_confirmed = 1; |
2635 | rp = &sop->so_replay; | |
2636 | rp->rp_status = NFSERR_SERVERFAULT; | |
2637 | rp->rp_buflen = 0; | |
2638 | rp->rp_buf = rp->rp_ibuf; | |
2639 | return sop; | |
2640 | } | |
2641 | ||
fd39ca9a | 2642 | static struct nfs4_stateid * |
1da177e4 LT |
2643 | alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp) |
2644 | { | |
2645 | struct nfs4_stateid *stp; | |
2646 | unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id); | |
2647 | ||
5ac049ac N |
2648 | stp = nfs4_alloc_stateid(); |
2649 | if (stp == NULL) | |
1da177e4 LT |
2650 | goto out; |
2651 | INIT_LIST_HEAD(&stp->st_hash); | |
2652 | INIT_LIST_HEAD(&stp->st_perfile); | |
ea1da636 N |
2653 | INIT_LIST_HEAD(&stp->st_perstateowner); |
2654 | INIT_LIST_HEAD(&stp->st_lockowners); /* not used */ | |
1da177e4 | 2655 | list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]); |
8beefa24 | 2656 | list_add(&stp->st_perfile, &fp->fi_stateids); |
ea1da636 | 2657 | list_add(&stp->st_perstateowner, &sop->so_stateids); |
1da177e4 | 2658 | stp->st_stateowner = sop; |
13cd2184 | 2659 | get_nfs4_file(fp); |
1da177e4 LT |
2660 | stp->st_file = fp; |
2661 | stp->st_stateid.si_boot = boot_time; | |
2662 | stp->st_stateid.si_stateownerid = sop->so_id; | |
2663 | stp->st_stateid.si_fileid = fp->fi_id; | |
2664 | stp->st_stateid.si_generation = 0; | |
2665 | stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */ | |
2666 | stp->st_access_bmap = open_stp->st_access_bmap; | |
2667 | stp->st_deny_bmap = open_stp->st_deny_bmap; | |
4c4cd222 | 2668 | stp->st_openstp = open_stp; |
1da177e4 LT |
2669 | |
2670 | out: | |
2671 | return stp; | |
2672 | } | |
2673 | ||
fd39ca9a | 2674 | static int |
1da177e4 LT |
2675 | check_lock_length(u64 offset, u64 length) |
2676 | { | |
2677 | return ((length == 0) || ((length != ~(u64)0) && | |
2678 | LOFF_OVERFLOW(offset, length))); | |
2679 | } | |
2680 | ||
2681 | /* | |
2682 | * LOCK operation | |
2683 | */ | |
2684 | int | |
f2327d9a | 2685 | nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock, struct nfs4_stateowner **replay_owner) |
1da177e4 | 2686 | { |
3e9e3dbe | 2687 | struct nfs4_stateowner *open_sop = NULL; |
b59e3c0e | 2688 | struct nfs4_stateowner *lock_sop = NULL; |
1da177e4 LT |
2689 | struct nfs4_stateid *lock_stp; |
2690 | struct file *filp; | |
2691 | struct file_lock file_lock; | |
2692 | struct file_lock *conflock; | |
2693 | int status = 0; | |
2694 | unsigned int strhashval; | |
2695 | ||
2696 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", | |
2697 | (long long) lock->lk_offset, | |
2698 | (long long) lock->lk_length); | |
2699 | ||
1da177e4 LT |
2700 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
2701 | return nfserr_inval; | |
2702 | ||
2703 | nfs4_lock_state(); | |
2704 | ||
2705 | if (lock->lk_is_new) { | |
893f8770 N |
2706 | /* |
2707 | * Client indicates that this is a new lockowner. | |
2708 | * Use open owner and open stateid to create lock owner and | |
2709 | * lock stateid. | |
2710 | */ | |
1da177e4 LT |
2711 | struct nfs4_stateid *open_stp = NULL; |
2712 | struct nfs4_file *fp; | |
2713 | ||
2714 | status = nfserr_stale_clientid; | |
849823c5 | 2715 | if (STALE_CLIENTID(&lock->lk_new_clientid)) |
1da177e4 | 2716 | goto out; |
1da177e4 | 2717 | |
1da177e4 LT |
2718 | /* validate and update open stateid and open seqid */ |
2719 | status = nfs4_preprocess_seqid_op(current_fh, | |
2720 | lock->lk_new_open_seqid, | |
2721 | &lock->lk_new_open_stateid, | |
2722 | CHECK_FH | OPEN_STATE, | |
b59e3c0e NB |
2723 | &lock->lk_stateowner, &open_stp, |
2724 | lock); | |
37515177 | 2725 | if (status) |
1da177e4 | 2726 | goto out; |
b59e3c0e | 2727 | open_sop = lock->lk_stateowner; |
1da177e4 LT |
2728 | /* create lockowner and lock stateid */ |
2729 | fp = open_stp->st_file; | |
2730 | strhashval = lock_ownerstr_hashval(fp->fi_inode, | |
2731 | open_sop->so_client->cl_clientid.cl_id, | |
2732 | &lock->v.new.owner); | |
3e9e3dbe N |
2733 | /* XXX: Do we need to check for duplicate stateowners on |
2734 | * the same file, or should they just be allowed (and | |
2735 | * create new stateids)? */ | |
1da177e4 | 2736 | status = nfserr_resource; |
b59e3c0e NB |
2737 | lock_sop = alloc_init_lock_stateowner(strhashval, |
2738 | open_sop->so_client, open_stp, lock); | |
2739 | if (lock_sop == NULL) | |
1da177e4 | 2740 | goto out; |
b59e3c0e NB |
2741 | lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp); |
2742 | if (lock_stp == NULL) { | |
2743 | release_stateowner(lock_sop); | |
1da177e4 LT |
2744 | goto out; |
2745 | } | |
1da177e4 LT |
2746 | } else { |
2747 | /* lock (lock owner + lock stateid) already exists */ | |
2748 | status = nfs4_preprocess_seqid_op(current_fh, | |
2749 | lock->lk_old_lock_seqid, | |
2750 | &lock->lk_old_lock_stateid, | |
2751 | CHECK_FH | LOCK_STATE, | |
4c4cd222 | 2752 | &lock->lk_stateowner, &lock_stp, lock); |
1da177e4 LT |
2753 | if (status) |
2754 | goto out; | |
b59e3c0e | 2755 | lock_sop = lock->lk_stateowner; |
1da177e4 LT |
2756 | } |
2757 | /* lock->lk_stateowner and lock_stp have been created or found */ | |
2758 | filp = lock_stp->st_vfs_file; | |
2759 | ||
2760 | if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) { | |
849823c5 | 2761 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
1da177e4 LT |
2762 | goto out; |
2763 | } | |
2764 | ||
0dd395dc N |
2765 | status = nfserr_grace; |
2766 | if (nfs4_in_grace() && !lock->lk_reclaim) | |
2767 | goto out; | |
2768 | status = nfserr_no_grace; | |
2769 | if (!nfs4_in_grace() && lock->lk_reclaim) | |
2770 | goto out; | |
2771 | ||
1da177e4 LT |
2772 | locks_init_lock(&file_lock); |
2773 | switch (lock->lk_type) { | |
2774 | case NFS4_READ_LT: | |
2775 | case NFS4_READW_LT: | |
2776 | file_lock.fl_type = F_RDLCK; | |
2777 | break; | |
2778 | case NFS4_WRITE_LT: | |
2779 | case NFS4_WRITEW_LT: | |
2780 | file_lock.fl_type = F_WRLCK; | |
2781 | break; | |
2782 | default: | |
2783 | status = nfserr_inval; | |
2784 | goto out; | |
2785 | } | |
b59e3c0e | 2786 | file_lock.fl_owner = (fl_owner_t)lock_sop; |
1da177e4 LT |
2787 | file_lock.fl_pid = current->tgid; |
2788 | file_lock.fl_file = filp; | |
2789 | file_lock.fl_flags = FL_POSIX; | |
2790 | ||
2791 | file_lock.fl_start = lock->lk_offset; | |
2792 | if ((lock->lk_length == ~(u64)0) || | |
2793 | LOFF_OVERFLOW(lock->lk_offset, lock->lk_length)) | |
2794 | file_lock.fl_end = ~(u64)0; | |
2795 | else | |
2796 | file_lock.fl_end = lock->lk_offset + lock->lk_length - 1; | |
2797 | nfs4_transform_lock_offset(&file_lock); | |
2798 | ||
2799 | /* | |
2800 | * Try to lock the file in the VFS. | |
2801 | * Note: locks.c uses the BKL to protect the inode's lock list. | |
2802 | */ | |
2803 | ||
2804 | status = posix_lock_file(filp, &file_lock); | |
2805 | if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private) | |
2806 | file_lock.fl_ops->fl_release_private(&file_lock); | |
2807 | dprintk("NFSD: nfsd4_lock: posix_lock_file status %d\n",status); | |
2808 | switch (-status) { | |
2809 | case 0: /* success! */ | |
2810 | update_stateid(&lock_stp->st_stateid); | |
2811 | memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid, | |
2812 | sizeof(stateid_t)); | |
2813 | goto out; | |
2814 | case (EAGAIN): | |
2815 | goto conflicting_lock; | |
2816 | case (EDEADLK): | |
2817 | status = nfserr_deadlock; | |
2818 | default: | |
2819 | dprintk("NFSD: nfsd4_lock: posix_lock_file() failed! status %d\n",status); | |
2820 | goto out_destroy_new_stateid; | |
2821 | } | |
2822 | ||
2823 | conflicting_lock: | |
2824 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); | |
2825 | status = nfserr_denied; | |
2826 | /* XXX There is a race here. Future patch needed to provide | |
2827 | * an atomic posix_lock_and_test_file | |
2828 | */ | |
2829 | if (!(conflock = posix_test_lock(filp, &file_lock))) { | |
2830 | status = nfserr_serverfault; | |
2831 | goto out; | |
2832 | } | |
2833 | nfs4_set_lock_denied(conflock, &lock->lk_denied); | |
2834 | ||
2835 | out_destroy_new_stateid: | |
2836 | if (lock->lk_is_new) { | |
2837 | dprintk("NFSD: nfsd4_lock: destroy new stateid!\n"); | |
893f8770 N |
2838 | /* |
2839 | * An error encountered after instantiation of the new | |
2840 | * stateid has forced us to destroy it. | |
2841 | */ | |
1da177e4 LT |
2842 | release_state_owner(lock_stp, LOCK_STATE); |
2843 | } | |
2844 | out: | |
f2327d9a | 2845 | if (lock->lk_stateowner) { |
1da177e4 | 2846 | nfs4_get_stateowner(lock->lk_stateowner); |
f2327d9a NB |
2847 | *replay_owner = lock->lk_stateowner; |
2848 | } | |
1da177e4 LT |
2849 | nfs4_unlock_state(); |
2850 | return status; | |
2851 | } | |
2852 | ||
2853 | /* | |
2854 | * LOCKT operation | |
2855 | */ | |
2856 | int | |
2857 | nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt) | |
2858 | { | |
2859 | struct inode *inode; | |
2860 | struct file file; | |
2861 | struct file_lock file_lock; | |
2862 | struct file_lock *conflicting_lock; | |
2863 | int status; | |
2864 | ||
2865 | if (nfs4_in_grace()) | |
2866 | return nfserr_grace; | |
2867 | ||
2868 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) | |
2869 | return nfserr_inval; | |
2870 | ||
2871 | lockt->lt_stateowner = NULL; | |
2872 | nfs4_lock_state(); | |
2873 | ||
2874 | status = nfserr_stale_clientid; | |
849823c5 | 2875 | if (STALE_CLIENTID(&lockt->lt_clientid)) |
1da177e4 | 2876 | goto out; |
1da177e4 LT |
2877 | |
2878 | if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) { | |
849823c5 | 2879 | dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n"); |
1da177e4 LT |
2880 | if (status == nfserr_symlink) |
2881 | status = nfserr_inval; | |
2882 | goto out; | |
2883 | } | |
2884 | ||
2885 | inode = current_fh->fh_dentry->d_inode; | |
2886 | locks_init_lock(&file_lock); | |
2887 | switch (lockt->lt_type) { | |
2888 | case NFS4_READ_LT: | |
2889 | case NFS4_READW_LT: | |
2890 | file_lock.fl_type = F_RDLCK; | |
2891 | break; | |
2892 | case NFS4_WRITE_LT: | |
2893 | case NFS4_WRITEW_LT: | |
2894 | file_lock.fl_type = F_WRLCK; | |
2895 | break; | |
2896 | default: | |
2897 | printk("NFSD: nfs4_lockt: bad lock type!\n"); | |
2898 | status = nfserr_inval; | |
2899 | goto out; | |
2900 | } | |
2901 | ||
2902 | lockt->lt_stateowner = find_lockstateowner_str(inode, | |
2903 | &lockt->lt_clientid, &lockt->lt_owner); | |
2904 | if (lockt->lt_stateowner) | |
2905 | file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner; | |
2906 | file_lock.fl_pid = current->tgid; | |
2907 | file_lock.fl_flags = FL_POSIX; | |
2908 | ||
2909 | file_lock.fl_start = lockt->lt_offset; | |
2910 | if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length)) | |
2911 | file_lock.fl_end = ~(u64)0; | |
2912 | else | |
2913 | file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1; | |
2914 | ||
2915 | nfs4_transform_lock_offset(&file_lock); | |
2916 | ||
2917 | /* posix_test_lock uses the struct file _only_ to resolve the inode. | |
2918 | * since LOCKT doesn't require an OPEN, and therefore a struct | |
2919 | * file may not exist, pass posix_test_lock a struct file with | |
2920 | * only the dentry:inode set. | |
2921 | */ | |
2922 | memset(&file, 0, sizeof (struct file)); | |
2923 | file.f_dentry = current_fh->fh_dentry; | |
2924 | ||
2925 | status = nfs_ok; | |
2926 | conflicting_lock = posix_test_lock(&file, &file_lock); | |
2927 | if (conflicting_lock) { | |
2928 | status = nfserr_denied; | |
2929 | nfs4_set_lock_denied(conflicting_lock, &lockt->lt_denied); | |
2930 | } | |
2931 | out: | |
2932 | nfs4_unlock_state(); | |
2933 | return status; | |
2934 | } | |
2935 | ||
2936 | int | |
f2327d9a | 2937 | nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku, struct nfs4_stateowner **replay_owner) |
1da177e4 LT |
2938 | { |
2939 | struct nfs4_stateid *stp; | |
2940 | struct file *filp = NULL; | |
2941 | struct file_lock file_lock; | |
2942 | int status; | |
2943 | ||
2944 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", | |
2945 | (long long) locku->lu_offset, | |
2946 | (long long) locku->lu_length); | |
2947 | ||
2948 | if (check_lock_length(locku->lu_offset, locku->lu_length)) | |
2949 | return nfserr_inval; | |
2950 | ||
2951 | nfs4_lock_state(); | |
2952 | ||
2953 | if ((status = nfs4_preprocess_seqid_op(current_fh, | |
2954 | locku->lu_seqid, | |
2955 | &locku->lu_stateid, | |
2956 | CHECK_FH | LOCK_STATE, | |
2957 | &locku->lu_stateowner, &stp, NULL))) | |
2958 | goto out; | |
2959 | ||
2960 | filp = stp->st_vfs_file; | |
2961 | BUG_ON(!filp); | |
2962 | locks_init_lock(&file_lock); | |
2963 | file_lock.fl_type = F_UNLCK; | |
2964 | file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner; | |
2965 | file_lock.fl_pid = current->tgid; | |
2966 | file_lock.fl_file = filp; | |
2967 | file_lock.fl_flags = FL_POSIX; | |
2968 | file_lock.fl_start = locku->lu_offset; | |
2969 | ||
2970 | if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length)) | |
2971 | file_lock.fl_end = ~(u64)0; | |
2972 | else | |
2973 | file_lock.fl_end = locku->lu_offset + locku->lu_length - 1; | |
2974 | nfs4_transform_lock_offset(&file_lock); | |
2975 | ||
2976 | /* | |
2977 | * Try to unlock the file in the VFS. | |
2978 | */ | |
2979 | status = posix_lock_file(filp, &file_lock); | |
2980 | if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private) | |
2981 | file_lock.fl_ops->fl_release_private(&file_lock); | |
2982 | if (status) { | |
849823c5 | 2983 | dprintk("NFSD: nfs4_locku: posix_lock_file failed!\n"); |
1da177e4 LT |
2984 | goto out_nfserr; |
2985 | } | |
2986 | /* | |
2987 | * OK, unlock succeeded; the only thing left to do is update the stateid. | |
2988 | */ | |
2989 | update_stateid(&stp->st_stateid); | |
2990 | memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t)); | |
2991 | ||
2992 | out: | |
f2327d9a | 2993 | if (locku->lu_stateowner) { |
1da177e4 | 2994 | nfs4_get_stateowner(locku->lu_stateowner); |
f2327d9a NB |
2995 | *replay_owner = locku->lu_stateowner; |
2996 | } | |
1da177e4 LT |
2997 | nfs4_unlock_state(); |
2998 | return status; | |
2999 | ||
3000 | out_nfserr: | |
3001 | status = nfserrno(status); | |
3002 | goto out; | |
3003 | } | |
3004 | ||
3005 | /* | |
3006 | * returns | |
3007 | * 1: locks held by lockowner | |
3008 | * 0: no locks held by lockowner | |
3009 | */ | |
3010 | static int | |
3011 | check_for_locks(struct file *filp, struct nfs4_stateowner *lowner) | |
3012 | { | |
3013 | struct file_lock **flpp; | |
3014 | struct inode *inode = filp->f_dentry->d_inode; | |
3015 | int status = 0; | |
3016 | ||
3017 | lock_kernel(); | |
3018 | for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) { | |
3019 | if ((*flpp)->fl_owner == (fl_owner_t)lowner) | |
3020 | status = 1; | |
3021 | goto out; | |
3022 | } | |
3023 | out: | |
3024 | unlock_kernel(); | |
3025 | return status; | |
3026 | } | |
3027 | ||
3028 | int | |
3029 | nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner) | |
3030 | { | |
3031 | clientid_t *clid = &rlockowner->rl_clientid; | |
3e9e3dbe N |
3032 | struct nfs4_stateowner *sop; |
3033 | struct nfs4_stateid *stp; | |
1da177e4 | 3034 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
3e9e3dbe N |
3035 | struct list_head matches; |
3036 | int i; | |
1da177e4 LT |
3037 | int status; |
3038 | ||
3039 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", | |
3040 | clid->cl_boot, clid->cl_id); | |
3041 | ||
3042 | /* XXX check for lease expiration */ | |
3043 | ||
3044 | status = nfserr_stale_clientid; | |
849823c5 | 3045 | if (STALE_CLIENTID(clid)) |
1da177e4 | 3046 | return status; |
1da177e4 LT |
3047 | |
3048 | nfs4_lock_state(); | |
3049 | ||
3e9e3dbe N |
3050 | status = nfserr_locks_held; |
3051 | /* XXX: we're doing a linear search through all the lockowners. | |
3052 | * Yipes! For now we'll just hope clients aren't really using | |
3053 | * release_lockowner much, but eventually we have to fix these | |
3054 | * data structures. */ | |
3055 | INIT_LIST_HEAD(&matches); | |
3056 | for (i = 0; i < LOCK_HASH_SIZE; i++) { | |
3057 | list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) { | |
3058 | if (!cmp_owner_str(sop, owner, clid)) | |
3059 | continue; | |
3060 | list_for_each_entry(stp, &sop->so_stateids, | |
3061 | st_perstateowner) { | |
3062 | if (check_for_locks(stp->st_vfs_file, sop)) | |
3063 | goto out; | |
3064 | /* Note: so_perclient unused for lockowners, | |
3065 | * so it's OK to fool with here. */ | |
3066 | list_add(&sop->so_perclient, &matches); | |
3067 | } | |
1da177e4 | 3068 | } |
3e9e3dbe N |
3069 | } |
3070 | /* Clients probably won't expect us to return with some (but not all) | |
3071 | * of the lockowner state released; so don't release any until all | |
3072 | * have been checked. */ | |
3073 | status = nfs_ok; | |
0fa822e4 N |
3074 | while (!list_empty(&matches)) { |
3075 | sop = list_entry(matches.next, struct nfs4_stateowner, | |
3076 | so_perclient); | |
3077 | /* unhash_stateowner deletes so_perclient only | |
3078 | * for openowners. */ | |
3079 | list_del(&sop->so_perclient); | |
3e9e3dbe | 3080 | release_stateowner(sop); |
1da177e4 LT |
3081 | } |
3082 | out: | |
3083 | nfs4_unlock_state(); | |
3084 | return status; | |
3085 | } | |
3086 | ||
3087 | static inline struct nfs4_client_reclaim * | |
a55370a3 | 3088 | alloc_reclaim(void) |
1da177e4 | 3089 | { |
a55370a3 | 3090 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
1da177e4 LT |
3091 | } |
3092 | ||
c7b9a459 N |
3093 | int |
3094 | nfs4_has_reclaimed_state(const char *name) | |
3095 | { | |
3096 | unsigned int strhashval = clientstr_hashval(name); | |
3097 | struct nfs4_client *clp; | |
3098 | ||
3099 | clp = find_confirmed_client_by_str(name, strhashval); | |
3100 | return clp ? 1 : 0; | |
3101 | } | |
3102 | ||
1da177e4 LT |
3103 | /* |
3104 | * failure => all reset bets are off, nfserr_no_grace... | |
3105 | */ | |
190e4fbf N |
3106 | int |
3107 | nfs4_client_to_reclaim(const char *name) | |
1da177e4 LT |
3108 | { |
3109 | unsigned int strhashval; | |
3110 | struct nfs4_client_reclaim *crp = NULL; | |
3111 | ||
a55370a3 N |
3112 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
3113 | crp = alloc_reclaim(); | |
1da177e4 LT |
3114 | if (!crp) |
3115 | return 0; | |
a55370a3 | 3116 | strhashval = clientstr_hashval(name); |
1da177e4 LT |
3117 | INIT_LIST_HEAD(&crp->cr_strhash); |
3118 | list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]); | |
a55370a3 | 3119 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
1da177e4 LT |
3120 | reclaim_str_hashtbl_size++; |
3121 | return 1; | |
3122 | } | |
3123 | ||
3124 | static void | |
3125 | nfs4_release_reclaim(void) | |
3126 | { | |
3127 | struct nfs4_client_reclaim *crp = NULL; | |
3128 | int i; | |
3129 | ||
1da177e4 LT |
3130 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
3131 | while (!list_empty(&reclaim_str_hashtbl[i])) { | |
3132 | crp = list_entry(reclaim_str_hashtbl[i].next, | |
3133 | struct nfs4_client_reclaim, cr_strhash); | |
3134 | list_del(&crp->cr_strhash); | |
1da177e4 LT |
3135 | kfree(crp); |
3136 | reclaim_str_hashtbl_size--; | |
3137 | } | |
3138 | } | |
3139 | BUG_ON(reclaim_str_hashtbl_size); | |
3140 | } | |
3141 | ||
3142 | /* | |
3143 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ | |
fd39ca9a | 3144 | static struct nfs4_client_reclaim * |
1da177e4 LT |
3145 | nfs4_find_reclaim_client(clientid_t *clid) |
3146 | { | |
3147 | unsigned int strhashval; | |
3148 | struct nfs4_client *clp; | |
3149 | struct nfs4_client_reclaim *crp = NULL; | |
3150 | ||
3151 | ||
3152 | /* find clientid in conf_id_hashtbl */ | |
3153 | clp = find_confirmed_client(clid); | |
3154 | if (clp == NULL) | |
3155 | return NULL; | |
3156 | ||
a55370a3 N |
3157 | dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n", |
3158 | clp->cl_name.len, clp->cl_name.data, | |
3159 | clp->cl_recdir); | |
1da177e4 LT |
3160 | |
3161 | /* find clp->cl_name in reclaim_str_hashtbl */ | |
a55370a3 | 3162 | strhashval = clientstr_hashval(clp->cl_recdir); |
1da177e4 | 3163 | list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) { |
a55370a3 | 3164 | if (same_name(crp->cr_recdir, clp->cl_recdir)) { |
1da177e4 LT |
3165 | return crp; |
3166 | } | |
3167 | } | |
3168 | return NULL; | |
3169 | } | |
3170 | ||
3171 | /* | |
3172 | * Called from OPEN. Look for clientid in reclaim list. | |
3173 | */ | |
3174 | int | |
3175 | nfs4_check_open_reclaim(clientid_t *clid) | |
3176 | { | |
dfc83565 | 3177 | return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad; |
1da177e4 LT |
3178 | } |
3179 | ||
ac4d8ff2 | 3180 | /* initialization to perform at module load time: */ |
1da177e4 | 3181 | |
ac4d8ff2 N |
3182 | void |
3183 | nfs4_state_init(void) | |
1da177e4 LT |
3184 | { |
3185 | int i; | |
1da177e4 | 3186 | |
1da177e4 LT |
3187 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
3188 | INIT_LIST_HEAD(&conf_id_hashtbl[i]); | |
3189 | INIT_LIST_HEAD(&conf_str_hashtbl[i]); | |
3190 | INIT_LIST_HEAD(&unconf_str_hashtbl[i]); | |
3191 | INIT_LIST_HEAD(&unconf_id_hashtbl[i]); | |
3192 | } | |
3193 | for (i = 0; i < FILE_HASH_SIZE; i++) { | |
3194 | INIT_LIST_HEAD(&file_hashtbl[i]); | |
3195 | } | |
3196 | for (i = 0; i < OWNER_HASH_SIZE; i++) { | |
3197 | INIT_LIST_HEAD(&ownerstr_hashtbl[i]); | |
3198 | INIT_LIST_HEAD(&ownerid_hashtbl[i]); | |
3199 | } | |
3200 | for (i = 0; i < STATEID_HASH_SIZE; i++) { | |
3201 | INIT_LIST_HEAD(&stateid_hashtbl[i]); | |
3202 | INIT_LIST_HEAD(&lockstateid_hashtbl[i]); | |
3203 | } | |
3204 | for (i = 0; i < LOCK_HASH_SIZE; i++) { | |
3205 | INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]); | |
3206 | INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]); | |
3207 | } | |
1da177e4 | 3208 | memset(&onestateid, ~0, sizeof(stateid_t)); |
1da177e4 LT |
3209 | INIT_LIST_HEAD(&close_lru); |
3210 | INIT_LIST_HEAD(&client_lru); | |
3211 | INIT_LIST_HEAD(&del_recall_lru); | |
ac4d8ff2 N |
3212 | for (i = 0; i < CLIENT_HASH_SIZE; i++) |
3213 | INIT_LIST_HEAD(&reclaim_str_hashtbl[i]); | |
3214 | reclaim_str_hashtbl_size = 0; | |
ac4d8ff2 N |
3215 | } |
3216 | ||
190e4fbf N |
3217 | static void |
3218 | nfsd4_load_reboot_recovery_data(void) | |
3219 | { | |
3220 | int status; | |
3221 | ||
0964a3d3 N |
3222 | nfs4_lock_state(); |
3223 | nfsd4_init_recdir(user_recovery_dirname); | |
190e4fbf | 3224 | status = nfsd4_recdir_load(); |
0964a3d3 | 3225 | nfs4_unlock_state(); |
190e4fbf N |
3226 | if (status) |
3227 | printk("NFSD: Failure reading reboot recovery data\n"); | |
3228 | } | |
3229 | ||
ac4d8ff2 N |
3230 | /* initialization to perform when the nfsd service is started: */ |
3231 | ||
3232 | static void | |
3233 | __nfs4_state_start(void) | |
3234 | { | |
3235 | time_t grace_time; | |
3236 | ||
1da177e4 | 3237 | boot_time = get_seconds(); |
d99a05ad N |
3238 | grace_time = max(user_lease_time, lease_time); |
3239 | lease_time = user_lease_time; | |
a76b4319 | 3240 | in_grace = 1; |
d99a05ad | 3241 | printk("NFSD: starting %ld-second grace period\n", grace_time); |
58da282b | 3242 | laundry_wq = create_singlethread_workqueue("nfsd4"); |
a76b4319 | 3243 | queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ); |
1da177e4 LT |
3244 | } |
3245 | ||
3246 | int | |
76a3550e | 3247 | nfs4_state_start(void) |
1da177e4 LT |
3248 | { |
3249 | int status; | |
3250 | ||
3251 | if (nfs4_init) | |
3252 | return 0; | |
3253 | status = nfsd4_init_slabs(); | |
3254 | if (status) | |
3255 | return status; | |
190e4fbf | 3256 | nfsd4_load_reboot_recovery_data(); |
76a3550e | 3257 | __nfs4_state_start(); |
1da177e4 LT |
3258 | nfs4_init = 1; |
3259 | return 0; | |
3260 | } | |
3261 | ||
3262 | int | |
3263 | nfs4_in_grace(void) | |
3264 | { | |
a76b4319 | 3265 | return in_grace; |
1da177e4 LT |
3266 | } |
3267 | ||
3268 | time_t | |
3269 | nfs4_lease_time(void) | |
3270 | { | |
3271 | return lease_time; | |
3272 | } | |
3273 | ||
3274 | static void | |
3275 | __nfs4_state_shutdown(void) | |
3276 | { | |
3277 | int i; | |
3278 | struct nfs4_client *clp = NULL; | |
3279 | struct nfs4_delegation *dp = NULL; | |
3280 | struct nfs4_stateowner *sop = NULL; | |
3281 | struct list_head *pos, *next, reaplist; | |
3282 | ||
3283 | list_for_each_safe(pos, next, &close_lru) { | |
3284 | sop = list_entry(pos, struct nfs4_stateowner, so_close_lru); | |
3285 | list_del(&sop->so_close_lru); | |
3286 | nfs4_put_stateowner(sop); | |
3287 | } | |
3288 | ||
3289 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { | |
3290 | while (!list_empty(&conf_id_hashtbl[i])) { | |
3291 | clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); | |
3292 | expire_client(clp); | |
3293 | } | |
3294 | while (!list_empty(&unconf_str_hashtbl[i])) { | |
3295 | clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash); | |
3296 | expire_client(clp); | |
3297 | } | |
3298 | } | |
3299 | INIT_LIST_HEAD(&reaplist); | |
3300 | spin_lock(&recall_lock); | |
3301 | list_for_each_safe(pos, next, &del_recall_lru) { | |
3302 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); | |
3303 | list_move(&dp->dl_recall_lru, &reaplist); | |
3304 | } | |
3305 | spin_unlock(&recall_lock); | |
3306 | list_for_each_safe(pos, next, &reaplist) { | |
3307 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); | |
3308 | list_del_init(&dp->dl_recall_lru); | |
3309 | unhash_delegation(dp); | |
3310 | } | |
3311 | ||
1da177e4 | 3312 | cancel_delayed_work(&laundromat_work); |
58da282b N |
3313 | flush_workqueue(laundry_wq); |
3314 | destroy_workqueue(laundry_wq); | |
190e4fbf | 3315 | nfsd4_shutdown_recdir(); |
1da177e4 | 3316 | nfs4_init = 0; |
1da177e4 LT |
3317 | } |
3318 | ||
3319 | void | |
3320 | nfs4_state_shutdown(void) | |
3321 | { | |
3322 | nfs4_lock_state(); | |
3323 | nfs4_release_reclaim(); | |
3324 | __nfs4_state_shutdown(); | |
3325 | nfsd4_free_slabs(); | |
3326 | nfs4_unlock_state(); | |
3327 | } | |
3328 | ||
0964a3d3 N |
3329 | static void |
3330 | nfs4_set_recdir(char *recdir) | |
3331 | { | |
3332 | nfs4_lock_state(); | |
3333 | strcpy(user_recovery_dirname, recdir); | |
3334 | nfs4_unlock_state(); | |
3335 | } | |
3336 | ||
3337 | /* | |
3338 | * Change the NFSv4 recovery directory to recdir. | |
3339 | */ | |
3340 | int | |
3341 | nfs4_reset_recoverydir(char *recdir) | |
3342 | { | |
3343 | int status; | |
3344 | struct nameidata nd; | |
3345 | ||
3346 | status = path_lookup(recdir, LOOKUP_FOLLOW, &nd); | |
3347 | if (status) | |
3348 | return status; | |
3349 | status = -ENOTDIR; | |
3350 | if (S_ISDIR(nd.dentry->d_inode->i_mode)) { | |
3351 | nfs4_set_recdir(recdir); | |
3352 | status = 0; | |
3353 | } | |
3354 | path_release(&nd); | |
3355 | return status; | |
3356 | } | |
3357 | ||
1da177e4 LT |
3358 | /* |
3359 | * Called when leasetime is changed. | |
3360 | * | |
d99a05ad N |
3361 | * The only way the protocol gives us to handle on-the-fly lease changes is to |
3362 | * simulate a reboot. Instead of doing that, we just wait till the next time | |
3363 | * we start to register any changes in lease time. If the administrator | |
3364 | * really wants to change the lease time *now*, they can go ahead and bring | |
3365 | * nfsd down and then back up again after changing the lease time. | |
1da177e4 LT |
3366 | */ |
3367 | void | |
3368 | nfs4_reset_lease(time_t leasetime) | |
3369 | { | |
d99a05ad N |
3370 | lock_kernel(); |
3371 | user_lease_time = leasetime; | |
3372 | unlock_kernel(); | |
1da177e4 | 3373 | } |