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