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