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