1 /*
2 * Copyright (c) 2001 The Regents of the University of Michigan.
3 * All rights reserved.
4 *
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #include <linux/file.h>
36 #include <linux/fs.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/jhash.h>
45 #include "xdr4.h"
46 #include "xdr4cb.h"
47 #include "vfs.h"
48 #include "current_stateid.h"
49
50 #include "netns.h"
51 #include "pnfs.h"
52
53 #define NFSDDBG_FACILITY NFSDDBG_PROC
54
55 #define all_ones {{~0,~0},~0}
56 static const stateid_t one_stateid = {
57 .si_generation = ~0,
58 .si_opaque = all_ones,
59 };
60 static const stateid_t zero_stateid = {
61 /* all fields zero */
62 };
63 static const stateid_t currentstateid = {
64 .si_generation = 1,
65 };
66 static const stateid_t close_stateid = {
67 .si_generation = 0xffffffffU,
68 };
69
70 static u64 current_sessionid = 1;
71
72 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
73 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
74 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
75 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
76
77 /* forward declarations */
78 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
79 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
80
81 /* Locking: */
82
83 /*
84 * Currently used for the del_recall_lru and file hash table. In an
85 * effort to decrease the scope of the client_mutex, this spinlock may
86 * eventually cover more:
87 */
88 static DEFINE_SPINLOCK(state_lock);
89
90 /*
91 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
92 * the refcount on the open stateid to drop.
93 */
94 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
95
96 static struct kmem_cache *openowner_slab;
97 static struct kmem_cache *lockowner_slab;
98 static struct kmem_cache *file_slab;
99 static struct kmem_cache *stateid_slab;
100 static struct kmem_cache *deleg_slab;
101 static struct kmem_cache *odstate_slab;
102
103 static void free_session(struct nfsd4_session *);
104
105 static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
106
is_session_dead(struct nfsd4_session * ses)107 static bool is_session_dead(struct nfsd4_session *ses)
108 {
109 return ses->se_flags & NFS4_SESSION_DEAD;
110 }
111
mark_session_dead_locked(struct nfsd4_session * ses,int ref_held_by_me)112 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
113 {
114 if (atomic_read(&ses->se_ref) > ref_held_by_me)
115 return nfserr_jukebox;
116 ses->se_flags |= NFS4_SESSION_DEAD;
117 return nfs_ok;
118 }
119
is_client_expired(struct nfs4_client * clp)120 static bool is_client_expired(struct nfs4_client *clp)
121 {
122 return clp->cl_time == 0;
123 }
124
get_client_locked(struct nfs4_client * clp)125 static __be32 get_client_locked(struct nfs4_client *clp)
126 {
127 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
128
129 lockdep_assert_held(&nn->client_lock);
130
131 if (is_client_expired(clp))
132 return nfserr_expired;
133 atomic_inc(&clp->cl_refcount);
134 return nfs_ok;
135 }
136
137 /* must be called under the client_lock */
138 static inline void
renew_client_locked(struct nfs4_client * clp)139 renew_client_locked(struct nfs4_client *clp)
140 {
141 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
142
143 if (is_client_expired(clp)) {
144 WARN_ON(1);
145 printk("%s: client (clientid %08x/%08x) already expired\n",
146 __func__,
147 clp->cl_clientid.cl_boot,
148 clp->cl_clientid.cl_id);
149 return;
150 }
151
152 dprintk("renewing client (clientid %08x/%08x)\n",
153 clp->cl_clientid.cl_boot,
154 clp->cl_clientid.cl_id);
155 list_move_tail(&clp->cl_lru, &nn->client_lru);
156 clp->cl_time = get_seconds();
157 }
158
put_client_renew_locked(struct nfs4_client * clp)159 static void put_client_renew_locked(struct nfs4_client *clp)
160 {
161 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
162
163 lockdep_assert_held(&nn->client_lock);
164
165 if (!atomic_dec_and_test(&clp->cl_refcount))
166 return;
167 if (!is_client_expired(clp))
168 renew_client_locked(clp);
169 }
170
put_client_renew(struct nfs4_client * clp)171 static void put_client_renew(struct nfs4_client *clp)
172 {
173 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
174
175 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
176 return;
177 if (!is_client_expired(clp))
178 renew_client_locked(clp);
179 spin_unlock(&nn->client_lock);
180 }
181
nfsd4_get_session_locked(struct nfsd4_session * ses)182 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
183 {
184 __be32 status;
185
186 if (is_session_dead(ses))
187 return nfserr_badsession;
188 status = get_client_locked(ses->se_client);
189 if (status)
190 return status;
191 atomic_inc(&ses->se_ref);
192 return nfs_ok;
193 }
194
nfsd4_put_session_locked(struct nfsd4_session * ses)195 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
196 {
197 struct nfs4_client *clp = ses->se_client;
198 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
199
200 lockdep_assert_held(&nn->client_lock);
201
202 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
203 free_session(ses);
204 put_client_renew_locked(clp);
205 }
206
nfsd4_put_session(struct nfsd4_session * ses)207 static void nfsd4_put_session(struct nfsd4_session *ses)
208 {
209 struct nfs4_client *clp = ses->se_client;
210 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
211
212 spin_lock(&nn->client_lock);
213 nfsd4_put_session_locked(ses);
214 spin_unlock(&nn->client_lock);
215 }
216
217 static inline struct nfs4_stateowner *
nfs4_get_stateowner(struct nfs4_stateowner * sop)218 nfs4_get_stateowner(struct nfs4_stateowner *sop)
219 {
220 atomic_inc(&sop->so_count);
221 return sop;
222 }
223
224 static int
same_owner_str(struct nfs4_stateowner * sop,struct xdr_netobj * owner)225 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
226 {
227 return (sop->so_owner.len == owner->len) &&
228 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
229 }
230
231 static struct nfs4_openowner *
find_openstateowner_str_locked(unsigned int hashval,struct nfsd4_open * open,struct nfs4_client * clp)232 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
233 struct nfs4_client *clp)
234 {
235 struct nfs4_stateowner *so;
236
237 lockdep_assert_held(&clp->cl_lock);
238
239 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
240 so_strhash) {
241 if (!so->so_is_open_owner)
242 continue;
243 if (same_owner_str(so, &open->op_owner))
244 return openowner(nfs4_get_stateowner(so));
245 }
246 return NULL;
247 }
248
249 static struct nfs4_openowner *
find_openstateowner_str(unsigned int hashval,struct nfsd4_open * open,struct nfs4_client * clp)250 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
251 struct nfs4_client *clp)
252 {
253 struct nfs4_openowner *oo;
254
255 spin_lock(&clp->cl_lock);
256 oo = find_openstateowner_str_locked(hashval, open, clp);
257 spin_unlock(&clp->cl_lock);
258 return oo;
259 }
260
261 static inline u32
opaque_hashval(const void * ptr,int nbytes)262 opaque_hashval(const void *ptr, int nbytes)
263 {
264 unsigned char *cptr = (unsigned char *) ptr;
265
266 u32 x = 0;
267 while (nbytes--) {
268 x *= 37;
269 x += *cptr++;
270 }
271 return x;
272 }
273
nfsd4_free_file_rcu(struct rcu_head * rcu)274 static void nfsd4_free_file_rcu(struct rcu_head *rcu)
275 {
276 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
277
278 kmem_cache_free(file_slab, fp);
279 }
280
281 void
put_nfs4_file(struct nfs4_file * fi)282 put_nfs4_file(struct nfs4_file *fi)
283 {
284 might_lock(&state_lock);
285
286 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
287 hlist_del_rcu(&fi->fi_hash);
288 spin_unlock(&state_lock);
289 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
290 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
291 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
292 }
293 }
294
295 static struct file *
__nfs4_get_fd(struct nfs4_file * f,int oflag)296 __nfs4_get_fd(struct nfs4_file *f, int oflag)
297 {
298 if (f->fi_fds[oflag])
299 return get_file(f->fi_fds[oflag]);
300 return NULL;
301 }
302
303 static struct file *
find_writeable_file_locked(struct nfs4_file * f)304 find_writeable_file_locked(struct nfs4_file *f)
305 {
306 struct file *ret;
307
308 lockdep_assert_held(&f->fi_lock);
309
310 ret = __nfs4_get_fd(f, O_WRONLY);
311 if (!ret)
312 ret = __nfs4_get_fd(f, O_RDWR);
313 return ret;
314 }
315
316 static struct file *
find_writeable_file(struct nfs4_file * f)317 find_writeable_file(struct nfs4_file *f)
318 {
319 struct file *ret;
320
321 spin_lock(&f->fi_lock);
322 ret = find_writeable_file_locked(f);
323 spin_unlock(&f->fi_lock);
324
325 return ret;
326 }
327
find_readable_file_locked(struct nfs4_file * f)328 static struct file *find_readable_file_locked(struct nfs4_file *f)
329 {
330 struct file *ret;
331
332 lockdep_assert_held(&f->fi_lock);
333
334 ret = __nfs4_get_fd(f, O_RDONLY);
335 if (!ret)
336 ret = __nfs4_get_fd(f, O_RDWR);
337 return ret;
338 }
339
340 static struct file *
find_readable_file(struct nfs4_file * f)341 find_readable_file(struct nfs4_file *f)
342 {
343 struct file *ret;
344
345 spin_lock(&f->fi_lock);
346 ret = find_readable_file_locked(f);
347 spin_unlock(&f->fi_lock);
348
349 return ret;
350 }
351
352 struct file *
find_any_file(struct nfs4_file * f)353 find_any_file(struct nfs4_file *f)
354 {
355 struct file *ret;
356
357 spin_lock(&f->fi_lock);
358 ret = __nfs4_get_fd(f, O_RDWR);
359 if (!ret) {
360 ret = __nfs4_get_fd(f, O_WRONLY);
361 if (!ret)
362 ret = __nfs4_get_fd(f, O_RDONLY);
363 }
364 spin_unlock(&f->fi_lock);
365 return ret;
366 }
367
368 static atomic_long_t num_delegations;
369 unsigned long max_delegations;
370
371 /*
372 * Open owner state (share locks)
373 */
374
375 /* hash tables for lock and open owners */
376 #define OWNER_HASH_BITS 8
377 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
378 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
379
ownerstr_hashval(struct xdr_netobj * ownername)380 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
381 {
382 unsigned int ret;
383
384 ret = opaque_hashval(ownername->data, ownername->len);
385 return ret & OWNER_HASH_MASK;
386 }
387
388 /* hash table for nfs4_file */
389 #define FILE_HASH_BITS 8
390 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
391
nfsd_fh_hashval(struct knfsd_fh * fh)392 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
393 {
394 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
395 }
396
file_hashval(struct knfsd_fh * fh)397 static unsigned int file_hashval(struct knfsd_fh *fh)
398 {
399 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
400 }
401
402 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
403
404 static void
__nfs4_file_get_access(struct nfs4_file * fp,u32 access)405 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
406 {
407 lockdep_assert_held(&fp->fi_lock);
408
409 if (access & NFS4_SHARE_ACCESS_WRITE)
410 atomic_inc(&fp->fi_access[O_WRONLY]);
411 if (access & NFS4_SHARE_ACCESS_READ)
412 atomic_inc(&fp->fi_access[O_RDONLY]);
413 }
414
415 static __be32
nfs4_file_get_access(struct nfs4_file * fp,u32 access)416 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
417 {
418 lockdep_assert_held(&fp->fi_lock);
419
420 /* Does this access mode make sense? */
421 if (access & ~NFS4_SHARE_ACCESS_BOTH)
422 return nfserr_inval;
423
424 /* Does it conflict with a deny mode already set? */
425 if ((access & fp->fi_share_deny) != 0)
426 return nfserr_share_denied;
427
428 __nfs4_file_get_access(fp, access);
429 return nfs_ok;
430 }
431
nfs4_file_check_deny(struct nfs4_file * fp,u32 deny)432 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
433 {
434 /* Common case is that there is no deny mode. */
435 if (deny) {
436 /* Does this deny mode make sense? */
437 if (deny & ~NFS4_SHARE_DENY_BOTH)
438 return nfserr_inval;
439
440 if ((deny & NFS4_SHARE_DENY_READ) &&
441 atomic_read(&fp->fi_access[O_RDONLY]))
442 return nfserr_share_denied;
443
444 if ((deny & NFS4_SHARE_DENY_WRITE) &&
445 atomic_read(&fp->fi_access[O_WRONLY]))
446 return nfserr_share_denied;
447 }
448 return nfs_ok;
449 }
450
__nfs4_file_put_access(struct nfs4_file * fp,int oflag)451 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
452 {
453 might_lock(&fp->fi_lock);
454
455 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
456 struct file *f1 = NULL;
457 struct file *f2 = NULL;
458
459 swap(f1, fp->fi_fds[oflag]);
460 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
461 swap(f2, fp->fi_fds[O_RDWR]);
462 spin_unlock(&fp->fi_lock);
463 if (f1)
464 fput(f1);
465 if (f2)
466 fput(f2);
467 }
468 }
469
nfs4_file_put_access(struct nfs4_file * fp,u32 access)470 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
471 {
472 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
473
474 if (access & NFS4_SHARE_ACCESS_WRITE)
475 __nfs4_file_put_access(fp, O_WRONLY);
476 if (access & NFS4_SHARE_ACCESS_READ)
477 __nfs4_file_put_access(fp, O_RDONLY);
478 }
479
480 /*
481 * Allocate a new open/delegation state counter. This is needed for
482 * pNFS for proper return on close semantics.
483 *
484 * Note that we only allocate it for pNFS-enabled exports, otherwise
485 * all pointers to struct nfs4_clnt_odstate are always NULL.
486 */
487 static struct nfs4_clnt_odstate *
alloc_clnt_odstate(struct nfs4_client * clp)488 alloc_clnt_odstate(struct nfs4_client *clp)
489 {
490 struct nfs4_clnt_odstate *co;
491
492 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
493 if (co) {
494 co->co_client = clp;
495 atomic_set(&co->co_odcount, 1);
496 }
497 return co;
498 }
499
500 static void
hash_clnt_odstate_locked(struct nfs4_clnt_odstate * co)501 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
502 {
503 struct nfs4_file *fp = co->co_file;
504
505 lockdep_assert_held(&fp->fi_lock);
506 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
507 }
508
509 static inline void
get_clnt_odstate(struct nfs4_clnt_odstate * co)510 get_clnt_odstate(struct nfs4_clnt_odstate *co)
511 {
512 if (co)
513 atomic_inc(&co->co_odcount);
514 }
515
516 static void
put_clnt_odstate(struct nfs4_clnt_odstate * co)517 put_clnt_odstate(struct nfs4_clnt_odstate *co)
518 {
519 struct nfs4_file *fp;
520
521 if (!co)
522 return;
523
524 fp = co->co_file;
525 if (atomic_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
526 list_del(&co->co_perfile);
527 spin_unlock(&fp->fi_lock);
528
529 nfsd4_return_all_file_layouts(co->co_client, fp);
530 kmem_cache_free(odstate_slab, co);
531 }
532 }
533
534 static struct nfs4_clnt_odstate *
find_or_hash_clnt_odstate(struct nfs4_file * fp,struct nfs4_clnt_odstate * new)535 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
536 {
537 struct nfs4_clnt_odstate *co;
538 struct nfs4_client *cl;
539
540 if (!new)
541 return NULL;
542
543 cl = new->co_client;
544
545 spin_lock(&fp->fi_lock);
546 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
547 if (co->co_client == cl) {
548 get_clnt_odstate(co);
549 goto out;
550 }
551 }
552 co = new;
553 co->co_file = fp;
554 hash_clnt_odstate_locked(new);
555 out:
556 spin_unlock(&fp->fi_lock);
557 return co;
558 }
559
nfs4_alloc_stid(struct nfs4_client * cl,struct kmem_cache * slab,void (* sc_free)(struct nfs4_stid *))560 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
561 void (*sc_free)(struct nfs4_stid *))
562 {
563 struct nfs4_stid *stid;
564 int new_id;
565
566 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
567 if (!stid)
568 return NULL;
569
570 idr_preload(GFP_KERNEL);
571 spin_lock(&cl->cl_lock);
572 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
573 spin_unlock(&cl->cl_lock);
574 idr_preload_end();
575 if (new_id < 0)
576 goto out_free;
577
578 stid->sc_free = sc_free;
579 stid->sc_client = cl;
580 stid->sc_stateid.si_opaque.so_id = new_id;
581 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
582 /* Will be incremented before return to client: */
583 atomic_set(&stid->sc_count, 1);
584 spin_lock_init(&stid->sc_lock);
585
586 /*
587 * It shouldn't be a problem to reuse an opaque stateid value.
588 * I don't think it is for 4.1. But with 4.0 I worry that, for
589 * example, a stray write retransmission could be accepted by
590 * the server when it should have been rejected. Therefore,
591 * adopt a trick from the sctp code to attempt to maximize the
592 * amount of time until an id is reused, by ensuring they always
593 * "increase" (mod INT_MAX):
594 */
595 return stid;
596 out_free:
597 kmem_cache_free(slab, stid);
598 return NULL;
599 }
600
nfs4_alloc_open_stateid(struct nfs4_client * clp)601 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
602 {
603 struct nfs4_stid *stid;
604
605 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
606 if (!stid)
607 return NULL;
608
609 return openlockstateid(stid);
610 }
611
nfs4_free_deleg(struct nfs4_stid * stid)612 static void nfs4_free_deleg(struct nfs4_stid *stid)
613 {
614 kmem_cache_free(deleg_slab, stid);
615 atomic_long_dec(&num_delegations);
616 }
617
618 /*
619 * When we recall a delegation, we should be careful not to hand it
620 * out again straight away.
621 * To ensure this we keep a pair of bloom filters ('new' and 'old')
622 * in which the filehandles of recalled delegations are "stored".
623 * If a filehandle appear in either filter, a delegation is blocked.
624 * When a delegation is recalled, the filehandle is stored in the "new"
625 * filter.
626 * Every 30 seconds we swap the filters and clear the "new" one,
627 * unless both are empty of course.
628 *
629 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
630 * low 3 bytes as hash-table indices.
631 *
632 * 'blocked_delegations_lock', which is always taken in block_delegations(),
633 * is used to manage concurrent access. Testing does not need the lock
634 * except when swapping the two filters.
635 */
636 static DEFINE_SPINLOCK(blocked_delegations_lock);
637 static struct bloom_pair {
638 int entries, old_entries;
639 time_t swap_time;
640 int new; /* index into 'set' */
641 DECLARE_BITMAP(set[2], 256);
642 } blocked_delegations;
643
delegation_blocked(struct knfsd_fh * fh)644 static int delegation_blocked(struct knfsd_fh *fh)
645 {
646 u32 hash;
647 struct bloom_pair *bd = &blocked_delegations;
648
649 if (bd->entries == 0)
650 return 0;
651 if (seconds_since_boot() - bd->swap_time > 30) {
652 spin_lock(&blocked_delegations_lock);
653 if (seconds_since_boot() - bd->swap_time > 30) {
654 bd->entries -= bd->old_entries;
655 bd->old_entries = bd->entries;
656 memset(bd->set[bd->new], 0,
657 sizeof(bd->set[0]));
658 bd->new = 1-bd->new;
659 bd->swap_time = seconds_since_boot();
660 }
661 spin_unlock(&blocked_delegations_lock);
662 }
663 hash = jhash(&fh->fh_base, fh->fh_size, 0);
664 if (test_bit(hash&255, bd->set[0]) &&
665 test_bit((hash>>8)&255, bd->set[0]) &&
666 test_bit((hash>>16)&255, bd->set[0]))
667 return 1;
668
669 if (test_bit(hash&255, bd->set[1]) &&
670 test_bit((hash>>8)&255, bd->set[1]) &&
671 test_bit((hash>>16)&255, bd->set[1]))
672 return 1;
673
674 return 0;
675 }
676
block_delegations(struct knfsd_fh * fh)677 static void block_delegations(struct knfsd_fh *fh)
678 {
679 u32 hash;
680 struct bloom_pair *bd = &blocked_delegations;
681
682 hash = jhash(&fh->fh_base, fh->fh_size, 0);
683
684 spin_lock(&blocked_delegations_lock);
685 __set_bit(hash&255, bd->set[bd->new]);
686 __set_bit((hash>>8)&255, bd->set[bd->new]);
687 __set_bit((hash>>16)&255, bd->set[bd->new]);
688 if (bd->entries == 0)
689 bd->swap_time = seconds_since_boot();
690 bd->entries += 1;
691 spin_unlock(&blocked_delegations_lock);
692 }
693
694 static struct nfs4_delegation *
alloc_init_deleg(struct nfs4_client * clp,struct svc_fh * current_fh,struct nfs4_clnt_odstate * odstate)695 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh,
696 struct nfs4_clnt_odstate *odstate)
697 {
698 struct nfs4_delegation *dp;
699 long n;
700
701 dprintk("NFSD alloc_init_deleg\n");
702 n = atomic_long_inc_return(&num_delegations);
703 if (n < 0 || n > max_delegations)
704 goto out_dec;
705 if (delegation_blocked(¤t_fh->fh_handle))
706 goto out_dec;
707 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
708 if (dp == NULL)
709 goto out_dec;
710
711 /*
712 * delegation seqid's are never incremented. The 4.1 special
713 * meaning of seqid 0 isn't meaningful, really, but let's avoid
714 * 0 anyway just for consistency and use 1:
715 */
716 dp->dl_stid.sc_stateid.si_generation = 1;
717 INIT_LIST_HEAD(&dp->dl_perfile);
718 INIT_LIST_HEAD(&dp->dl_perclnt);
719 INIT_LIST_HEAD(&dp->dl_recall_lru);
720 dp->dl_clnt_odstate = odstate;
721 get_clnt_odstate(odstate);
722 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
723 dp->dl_retries = 1;
724 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
725 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
726 return dp;
727 out_dec:
728 atomic_long_dec(&num_delegations);
729 return NULL;
730 }
731
732 void
nfs4_put_stid(struct nfs4_stid * s)733 nfs4_put_stid(struct nfs4_stid *s)
734 {
735 struct nfs4_file *fp = s->sc_file;
736 struct nfs4_client *clp = s->sc_client;
737
738 might_lock(&clp->cl_lock);
739
740 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
741 wake_up_all(&close_wq);
742 return;
743 }
744 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
745 spin_unlock(&clp->cl_lock);
746 s->sc_free(s);
747 if (fp)
748 put_nfs4_file(fp);
749 }
750
751 void
nfs4_inc_and_copy_stateid(stateid_t * dst,struct nfs4_stid * stid)752 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
753 {
754 stateid_t *src = &stid->sc_stateid;
755
756 spin_lock(&stid->sc_lock);
757 if (unlikely(++src->si_generation == 0))
758 src->si_generation = 1;
759 memcpy(dst, src, sizeof(*dst));
760 spin_unlock(&stid->sc_lock);
761 }
762
nfs4_put_deleg_lease(struct nfs4_file * fp)763 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
764 {
765 struct file *filp = NULL;
766
767 spin_lock(&fp->fi_lock);
768 if (fp->fi_deleg_file && --fp->fi_delegees == 0)
769 swap(filp, fp->fi_deleg_file);
770 spin_unlock(&fp->fi_lock);
771
772 if (filp) {
773 vfs_setlease(filp, F_UNLCK, NULL, (void **)&fp);
774 fput(filp);
775 }
776 }
777
nfs4_unhash_stid(struct nfs4_stid * s)778 void nfs4_unhash_stid(struct nfs4_stid *s)
779 {
780 s->sc_type = 0;
781 }
782
783 /**
784 * nfs4_get_existing_delegation - Discover if this delegation already exists
785 * @clp: a pointer to the nfs4_client we're granting a delegation to
786 * @fp: a pointer to the nfs4_file we're granting a delegation on
787 *
788 * Return:
789 * On success: NULL if an existing delegation was not found.
790 *
791 * On error: -EAGAIN if one was previously granted to this nfs4_client
792 * for this nfs4_file.
793 *
794 */
795
796 static int
nfs4_get_existing_delegation(struct nfs4_client * clp,struct nfs4_file * fp)797 nfs4_get_existing_delegation(struct nfs4_client *clp, struct nfs4_file *fp)
798 {
799 struct nfs4_delegation *searchdp = NULL;
800 struct nfs4_client *searchclp = NULL;
801
802 lockdep_assert_held(&state_lock);
803 lockdep_assert_held(&fp->fi_lock);
804
805 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
806 searchclp = searchdp->dl_stid.sc_client;
807 if (clp == searchclp) {
808 return -EAGAIN;
809 }
810 }
811 return 0;
812 }
813
814 /**
815 * hash_delegation_locked - Add a delegation to the appropriate lists
816 * @dp: a pointer to the nfs4_delegation we are adding.
817 * @fp: a pointer to the nfs4_file we're granting a delegation on
818 *
819 * Return:
820 * On success: NULL if the delegation was successfully hashed.
821 *
822 * On error: -EAGAIN if one was previously granted to this
823 * nfs4_client for this nfs4_file. Delegation is not hashed.
824 *
825 */
826
827 static int
hash_delegation_locked(struct nfs4_delegation * dp,struct nfs4_file * fp)828 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
829 {
830 int status;
831 struct nfs4_client *clp = dp->dl_stid.sc_client;
832
833 lockdep_assert_held(&state_lock);
834 lockdep_assert_held(&fp->fi_lock);
835
836 status = nfs4_get_existing_delegation(clp, fp);
837 if (status)
838 return status;
839 ++fp->fi_delegees;
840 atomic_inc(&dp->dl_stid.sc_count);
841 dp->dl_stid.sc_type = NFS4_DELEG_STID;
842 list_add(&dp->dl_perfile, &fp->fi_delegations);
843 list_add(&dp->dl_perclnt, &clp->cl_delegations);
844 return 0;
845 }
846
delegation_hashed(struct nfs4_delegation * dp)847 static bool delegation_hashed(struct nfs4_delegation *dp)
848 {
849 return !(list_empty(&dp->dl_perfile));
850 }
851
852 static bool
unhash_delegation_locked(struct nfs4_delegation * dp)853 unhash_delegation_locked(struct nfs4_delegation *dp)
854 {
855 struct nfs4_file *fp = dp->dl_stid.sc_file;
856
857 lockdep_assert_held(&state_lock);
858
859 if (!delegation_hashed(dp))
860 return false;
861
862 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
863 /* Ensure that deleg break won't try to requeue it */
864 ++dp->dl_time;
865 spin_lock(&fp->fi_lock);
866 list_del_init(&dp->dl_perclnt);
867 list_del_init(&dp->dl_recall_lru);
868 list_del_init(&dp->dl_perfile);
869 spin_unlock(&fp->fi_lock);
870 return true;
871 }
872
destroy_delegation(struct nfs4_delegation * dp)873 static void destroy_delegation(struct nfs4_delegation *dp)
874 {
875 bool unhashed;
876
877 spin_lock(&state_lock);
878 unhashed = unhash_delegation_locked(dp);
879 spin_unlock(&state_lock);
880 if (unhashed) {
881 put_clnt_odstate(dp->dl_clnt_odstate);
882 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
883 nfs4_put_stid(&dp->dl_stid);
884 }
885 }
886
revoke_delegation(struct nfs4_delegation * dp)887 static void revoke_delegation(struct nfs4_delegation *dp)
888 {
889 struct nfs4_client *clp = dp->dl_stid.sc_client;
890
891 WARN_ON(!list_empty(&dp->dl_recall_lru));
892
893 put_clnt_odstate(dp->dl_clnt_odstate);
894 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
895
896 if (clp->cl_minorversion == 0)
897 nfs4_put_stid(&dp->dl_stid);
898 else {
899 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
900 spin_lock(&clp->cl_lock);
901 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
902 spin_unlock(&clp->cl_lock);
903 }
904 }
905
906 /*
907 * SETCLIENTID state
908 */
909
clientid_hashval(u32 id)910 static unsigned int clientid_hashval(u32 id)
911 {
912 return id & CLIENT_HASH_MASK;
913 }
914
clientstr_hashval(const char * name)915 static unsigned int clientstr_hashval(const char *name)
916 {
917 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
918 }
919
920 /*
921 * We store the NONE, READ, WRITE, and BOTH bits separately in the
922 * st_{access,deny}_bmap field of the stateid, in order to track not
923 * only what share bits are currently in force, but also what
924 * combinations of share bits previous opens have used. This allows us
925 * to enforce the recommendation of rfc 3530 14.2.19 that the server
926 * return an error if the client attempt to downgrade to a combination
927 * of share bits not explicable by closing some of its previous opens.
928 *
929 * XXX: This enforcement is actually incomplete, since we don't keep
930 * track of access/deny bit combinations; so, e.g., we allow:
931 *
932 * OPEN allow read, deny write
933 * OPEN allow both, deny none
934 * DOWNGRADE allow read, deny none
935 *
936 * which we should reject.
937 */
938 static unsigned int
bmap_to_share_mode(unsigned long bmap)939 bmap_to_share_mode(unsigned long bmap) {
940 int i;
941 unsigned int access = 0;
942
943 for (i = 1; i < 4; i++) {
944 if (test_bit(i, &bmap))
945 access |= i;
946 }
947 return access;
948 }
949
950 /* set share access for a given stateid */
951 static inline void
set_access(u32 access,struct nfs4_ol_stateid * stp)952 set_access(u32 access, struct nfs4_ol_stateid *stp)
953 {
954 unsigned char mask = 1 << access;
955
956 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
957 stp->st_access_bmap |= mask;
958 }
959
960 /* clear share access for a given stateid */
961 static inline void
clear_access(u32 access,struct nfs4_ol_stateid * stp)962 clear_access(u32 access, struct nfs4_ol_stateid *stp)
963 {
964 unsigned char mask = 1 << access;
965
966 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
967 stp->st_access_bmap &= ~mask;
968 }
969
970 /* test whether a given stateid has access */
971 static inline bool
test_access(u32 access,struct nfs4_ol_stateid * stp)972 test_access(u32 access, struct nfs4_ol_stateid *stp)
973 {
974 unsigned char mask = 1 << access;
975
976 return (bool)(stp->st_access_bmap & mask);
977 }
978
979 /* set share deny for a given stateid */
980 static inline void
set_deny(u32 deny,struct nfs4_ol_stateid * stp)981 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
982 {
983 unsigned char mask = 1 << deny;
984
985 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
986 stp->st_deny_bmap |= mask;
987 }
988
989 /* clear share deny for a given stateid */
990 static inline void
clear_deny(u32 deny,struct nfs4_ol_stateid * stp)991 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
992 {
993 unsigned char mask = 1 << deny;
994
995 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
996 stp->st_deny_bmap &= ~mask;
997 }
998
999 /* test whether a given stateid is denying specific access */
1000 static inline bool
test_deny(u32 deny,struct nfs4_ol_stateid * stp)1001 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1002 {
1003 unsigned char mask = 1 << deny;
1004
1005 return (bool)(stp->st_deny_bmap & mask);
1006 }
1007
nfs4_access_to_omode(u32 access)1008 static int nfs4_access_to_omode(u32 access)
1009 {
1010 switch (access & NFS4_SHARE_ACCESS_BOTH) {
1011 case NFS4_SHARE_ACCESS_READ:
1012 return O_RDONLY;
1013 case NFS4_SHARE_ACCESS_WRITE:
1014 return O_WRONLY;
1015 case NFS4_SHARE_ACCESS_BOTH:
1016 return O_RDWR;
1017 }
1018 WARN_ON_ONCE(1);
1019 return O_RDONLY;
1020 }
1021
1022 /*
1023 * A stateid that had a deny mode associated with it is being released
1024 * or downgraded. Recalculate the deny mode on the file.
1025 */
1026 static void
recalculate_deny_mode(struct nfs4_file * fp)1027 recalculate_deny_mode(struct nfs4_file *fp)
1028 {
1029 struct nfs4_ol_stateid *stp;
1030
1031 spin_lock(&fp->fi_lock);
1032 fp->fi_share_deny = 0;
1033 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1034 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1035 spin_unlock(&fp->fi_lock);
1036 }
1037
1038 static void
reset_union_bmap_deny(u32 deny,struct nfs4_ol_stateid * stp)1039 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1040 {
1041 int i;
1042 bool change = false;
1043
1044 for (i = 1; i < 4; i++) {
1045 if ((i & deny) != i) {
1046 change = true;
1047 clear_deny(i, stp);
1048 }
1049 }
1050
1051 /* Recalculate per-file deny mode if there was a change */
1052 if (change)
1053 recalculate_deny_mode(stp->st_stid.sc_file);
1054 }
1055
1056 /* release all access and file references for a given stateid */
1057 static void
release_all_access(struct nfs4_ol_stateid * stp)1058 release_all_access(struct nfs4_ol_stateid *stp)
1059 {
1060 int i;
1061 struct nfs4_file *fp = stp->st_stid.sc_file;
1062
1063 if (fp && stp->st_deny_bmap != 0)
1064 recalculate_deny_mode(fp);
1065
1066 for (i = 1; i < 4; i++) {
1067 if (test_access(i, stp))
1068 nfs4_file_put_access(stp->st_stid.sc_file, i);
1069 clear_access(i, stp);
1070 }
1071 }
1072
nfs4_free_stateowner(struct nfs4_stateowner * sop)1073 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1074 {
1075 kfree(sop->so_owner.data);
1076 sop->so_ops->so_free(sop);
1077 }
1078
nfs4_put_stateowner(struct nfs4_stateowner * sop)1079 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1080 {
1081 struct nfs4_client *clp = sop->so_client;
1082
1083 might_lock(&clp->cl_lock);
1084
1085 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1086 return;
1087 sop->so_ops->so_unhash(sop);
1088 spin_unlock(&clp->cl_lock);
1089 nfs4_free_stateowner(sop);
1090 }
1091
unhash_ol_stateid(struct nfs4_ol_stateid * stp)1092 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1093 {
1094 struct nfs4_file *fp = stp->st_stid.sc_file;
1095
1096 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1097
1098 if (list_empty(&stp->st_perfile))
1099 return false;
1100
1101 spin_lock(&fp->fi_lock);
1102 list_del_init(&stp->st_perfile);
1103 spin_unlock(&fp->fi_lock);
1104 list_del(&stp->st_perstateowner);
1105 return true;
1106 }
1107
nfs4_free_ol_stateid(struct nfs4_stid * stid)1108 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1109 {
1110 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1111
1112 put_clnt_odstate(stp->st_clnt_odstate);
1113 release_all_access(stp);
1114 if (stp->st_stateowner)
1115 nfs4_put_stateowner(stp->st_stateowner);
1116 kmem_cache_free(stateid_slab, stid);
1117 }
1118
nfs4_free_lock_stateid(struct nfs4_stid * stid)1119 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1120 {
1121 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1122 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1123 struct file *file;
1124
1125 file = find_any_file(stp->st_stid.sc_file);
1126 if (file)
1127 filp_close(file, (fl_owner_t)lo);
1128 nfs4_free_ol_stateid(stid);
1129 }
1130
1131 /*
1132 * Put the persistent reference to an already unhashed generic stateid, while
1133 * holding the cl_lock. If it's the last reference, then put it onto the
1134 * reaplist for later destruction.
1135 */
put_ol_stateid_locked(struct nfs4_ol_stateid * stp,struct list_head * reaplist)1136 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1137 struct list_head *reaplist)
1138 {
1139 struct nfs4_stid *s = &stp->st_stid;
1140 struct nfs4_client *clp = s->sc_client;
1141
1142 lockdep_assert_held(&clp->cl_lock);
1143
1144 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1145
1146 if (!atomic_dec_and_test(&s->sc_count)) {
1147 wake_up_all(&close_wq);
1148 return;
1149 }
1150
1151 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1152 list_add(&stp->st_locks, reaplist);
1153 }
1154
unhash_lock_stateid(struct nfs4_ol_stateid * stp)1155 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1156 {
1157 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1158
1159 list_del_init(&stp->st_locks);
1160 nfs4_unhash_stid(&stp->st_stid);
1161 return unhash_ol_stateid(stp);
1162 }
1163
release_lock_stateid(struct nfs4_ol_stateid * stp)1164 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1165 {
1166 struct nfs4_client *clp = stp->st_stid.sc_client;
1167 bool unhashed;
1168
1169 spin_lock(&clp->cl_lock);
1170 unhashed = unhash_lock_stateid(stp);
1171 spin_unlock(&clp->cl_lock);
1172 if (unhashed)
1173 nfs4_put_stid(&stp->st_stid);
1174 }
1175
unhash_lockowner_locked(struct nfs4_lockowner * lo)1176 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1177 {
1178 struct nfs4_client *clp = lo->lo_owner.so_client;
1179
1180 lockdep_assert_held(&clp->cl_lock);
1181
1182 list_del_init(&lo->lo_owner.so_strhash);
1183 }
1184
1185 /*
1186 * Free a list of generic stateids that were collected earlier after being
1187 * fully unhashed.
1188 */
1189 static void
free_ol_stateid_reaplist(struct list_head * reaplist)1190 free_ol_stateid_reaplist(struct list_head *reaplist)
1191 {
1192 struct nfs4_ol_stateid *stp;
1193 struct nfs4_file *fp;
1194
1195 might_sleep();
1196
1197 while (!list_empty(reaplist)) {
1198 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1199 st_locks);
1200 list_del(&stp->st_locks);
1201 fp = stp->st_stid.sc_file;
1202 stp->st_stid.sc_free(&stp->st_stid);
1203 if (fp)
1204 put_nfs4_file(fp);
1205 }
1206 }
1207
release_open_stateid_locks(struct nfs4_ol_stateid * open_stp,struct list_head * reaplist)1208 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1209 struct list_head *reaplist)
1210 {
1211 struct nfs4_ol_stateid *stp;
1212
1213 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1214
1215 while (!list_empty(&open_stp->st_locks)) {
1216 stp = list_entry(open_stp->st_locks.next,
1217 struct nfs4_ol_stateid, st_locks);
1218 WARN_ON(!unhash_lock_stateid(stp));
1219 put_ol_stateid_locked(stp, reaplist);
1220 }
1221 }
1222
unhash_open_stateid(struct nfs4_ol_stateid * stp,struct list_head * reaplist)1223 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1224 struct list_head *reaplist)
1225 {
1226 bool unhashed;
1227
1228 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1229
1230 unhashed = unhash_ol_stateid(stp);
1231 release_open_stateid_locks(stp, reaplist);
1232 return unhashed;
1233 }
1234
release_open_stateid(struct nfs4_ol_stateid * stp)1235 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1236 {
1237 LIST_HEAD(reaplist);
1238
1239 spin_lock(&stp->st_stid.sc_client->cl_lock);
1240 if (unhash_open_stateid(stp, &reaplist))
1241 put_ol_stateid_locked(stp, &reaplist);
1242 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1243 free_ol_stateid_reaplist(&reaplist);
1244 }
1245
unhash_openowner_locked(struct nfs4_openowner * oo)1246 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1247 {
1248 struct nfs4_client *clp = oo->oo_owner.so_client;
1249
1250 lockdep_assert_held(&clp->cl_lock);
1251
1252 list_del_init(&oo->oo_owner.so_strhash);
1253 list_del_init(&oo->oo_perclient);
1254 }
1255
release_last_closed_stateid(struct nfs4_openowner * oo)1256 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1257 {
1258 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1259 nfsd_net_id);
1260 struct nfs4_ol_stateid *s;
1261
1262 spin_lock(&nn->client_lock);
1263 s = oo->oo_last_closed_stid;
1264 if (s) {
1265 list_del_init(&oo->oo_close_lru);
1266 oo->oo_last_closed_stid = NULL;
1267 }
1268 spin_unlock(&nn->client_lock);
1269 if (s)
1270 nfs4_put_stid(&s->st_stid);
1271 }
1272
release_openowner(struct nfs4_openowner * oo)1273 static void release_openowner(struct nfs4_openowner *oo)
1274 {
1275 struct nfs4_ol_stateid *stp;
1276 struct nfs4_client *clp = oo->oo_owner.so_client;
1277 struct list_head reaplist;
1278
1279 INIT_LIST_HEAD(&reaplist);
1280
1281 spin_lock(&clp->cl_lock);
1282 unhash_openowner_locked(oo);
1283 while (!list_empty(&oo->oo_owner.so_stateids)) {
1284 stp = list_first_entry(&oo->oo_owner.so_stateids,
1285 struct nfs4_ol_stateid, st_perstateowner);
1286 if (unhash_open_stateid(stp, &reaplist))
1287 put_ol_stateid_locked(stp, &reaplist);
1288 }
1289 spin_unlock(&clp->cl_lock);
1290 free_ol_stateid_reaplist(&reaplist);
1291 release_last_closed_stateid(oo);
1292 nfs4_put_stateowner(&oo->oo_owner);
1293 }
1294
1295 static inline int
hash_sessionid(struct nfs4_sessionid * sessionid)1296 hash_sessionid(struct nfs4_sessionid *sessionid)
1297 {
1298 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1299
1300 return sid->sequence % SESSION_HASH_SIZE;
1301 }
1302
1303 #ifdef CONFIG_SUNRPC_DEBUG
1304 static inline void
dump_sessionid(const char * fn,struct nfs4_sessionid * sessionid)1305 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1306 {
1307 u32 *ptr = (u32 *)(&sessionid->data[0]);
1308 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1309 }
1310 #else
1311 static inline void
dump_sessionid(const char * fn,struct nfs4_sessionid * sessionid)1312 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1313 {
1314 }
1315 #endif
1316
1317 /*
1318 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1319 * won't be used for replay.
1320 */
nfsd4_bump_seqid(struct nfsd4_compound_state * cstate,__be32 nfserr)1321 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1322 {
1323 struct nfs4_stateowner *so = cstate->replay_owner;
1324
1325 if (nfserr == nfserr_replay_me)
1326 return;
1327
1328 if (!seqid_mutating_err(ntohl(nfserr))) {
1329 nfsd4_cstate_clear_replay(cstate);
1330 return;
1331 }
1332 if (!so)
1333 return;
1334 if (so->so_is_open_owner)
1335 release_last_closed_stateid(openowner(so));
1336 so->so_seqid++;
1337 return;
1338 }
1339
1340 static void
gen_sessionid(struct nfsd4_session * ses)1341 gen_sessionid(struct nfsd4_session *ses)
1342 {
1343 struct nfs4_client *clp = ses->se_client;
1344 struct nfsd4_sessionid *sid;
1345
1346 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1347 sid->clientid = clp->cl_clientid;
1348 sid->sequence = current_sessionid++;
1349 sid->reserved = 0;
1350 }
1351
1352 /*
1353 * The protocol defines ca_maxresponssize_cached to include the size of
1354 * the rpc header, but all we need to cache is the data starting after
1355 * the end of the initial SEQUENCE operation--the rest we regenerate
1356 * each time. Therefore we can advertise a ca_maxresponssize_cached
1357 * value that is the number of bytes in our cache plus a few additional
1358 * bytes. In order to stay on the safe side, and not promise more than
1359 * we can cache, those additional bytes must be the minimum possible: 24
1360 * bytes of rpc header (xid through accept state, with AUTH_NULL
1361 * verifier), 12 for the compound header (with zero-length tag), and 44
1362 * for the SEQUENCE op response:
1363 */
1364 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1365
1366 static void
free_session_slots(struct nfsd4_session * ses)1367 free_session_slots(struct nfsd4_session *ses)
1368 {
1369 int i;
1370
1371 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1372 kfree(ses->se_slots[i]);
1373 }
1374
1375 /*
1376 * We don't actually need to cache the rpc and session headers, so we
1377 * can allocate a little less for each slot:
1378 */
slot_bytes(struct nfsd4_channel_attrs * ca)1379 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1380 {
1381 u32 size;
1382
1383 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1384 size = 0;
1385 else
1386 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1387 return size + sizeof(struct nfsd4_slot);
1388 }
1389
1390 /*
1391 * XXX: If we run out of reserved DRC memory we could (up to a point)
1392 * re-negotiate active sessions and reduce their slot usage to make
1393 * room for new connections. For now we just fail the create session.
1394 */
nfsd4_get_drc_mem(struct nfsd4_channel_attrs * ca)1395 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1396 {
1397 u32 slotsize = slot_bytes(ca);
1398 u32 num = ca->maxreqs;
1399 unsigned long avail, total_avail;
1400
1401 spin_lock(&nfsd_drc_lock);
1402 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1403 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1404 /*
1405 * Never use more than a third of the remaining memory,
1406 * unless it's the only way to give this client a slot:
1407 */
1408 avail = clamp_t(unsigned long, avail, slotsize, total_avail/3);
1409 num = min_t(int, num, avail / slotsize);
1410 nfsd_drc_mem_used += num * slotsize;
1411 spin_unlock(&nfsd_drc_lock);
1412
1413 return num;
1414 }
1415
nfsd4_put_drc_mem(struct nfsd4_channel_attrs * ca)1416 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1417 {
1418 int slotsize = slot_bytes(ca);
1419
1420 spin_lock(&nfsd_drc_lock);
1421 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1422 spin_unlock(&nfsd_drc_lock);
1423 }
1424
alloc_session(struct nfsd4_channel_attrs * fattrs,struct nfsd4_channel_attrs * battrs)1425 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1426 struct nfsd4_channel_attrs *battrs)
1427 {
1428 int numslots = fattrs->maxreqs;
1429 int slotsize = slot_bytes(fattrs);
1430 struct nfsd4_session *new;
1431 int mem, i;
1432
1433 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1434 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1435 mem = numslots * sizeof(struct nfsd4_slot *);
1436
1437 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1438 if (!new)
1439 return NULL;
1440 /* allocate each struct nfsd4_slot and data cache in one piece */
1441 for (i = 0; i < numslots; i++) {
1442 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1443 if (!new->se_slots[i])
1444 goto out_free;
1445 }
1446
1447 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1448 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1449
1450 return new;
1451 out_free:
1452 while (i--)
1453 kfree(new->se_slots[i]);
1454 kfree(new);
1455 return NULL;
1456 }
1457
free_conn(struct nfsd4_conn * c)1458 static void free_conn(struct nfsd4_conn *c)
1459 {
1460 svc_xprt_put(c->cn_xprt);
1461 kfree(c);
1462 }
1463
nfsd4_conn_lost(struct svc_xpt_user * u)1464 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1465 {
1466 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1467 struct nfs4_client *clp = c->cn_session->se_client;
1468
1469 spin_lock(&clp->cl_lock);
1470 if (!list_empty(&c->cn_persession)) {
1471 list_del(&c->cn_persession);
1472 free_conn(c);
1473 }
1474 nfsd4_probe_callback(clp);
1475 spin_unlock(&clp->cl_lock);
1476 }
1477
alloc_conn(struct svc_rqst * rqstp,u32 flags)1478 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1479 {
1480 struct nfsd4_conn *conn;
1481
1482 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1483 if (!conn)
1484 return NULL;
1485 svc_xprt_get(rqstp->rq_xprt);
1486 conn->cn_xprt = rqstp->rq_xprt;
1487 conn->cn_flags = flags;
1488 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1489 return conn;
1490 }
1491
__nfsd4_hash_conn(struct nfsd4_conn * conn,struct nfsd4_session * ses)1492 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1493 {
1494 conn->cn_session = ses;
1495 list_add(&conn->cn_persession, &ses->se_conns);
1496 }
1497
nfsd4_hash_conn(struct nfsd4_conn * conn,struct nfsd4_session * ses)1498 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1499 {
1500 struct nfs4_client *clp = ses->se_client;
1501
1502 spin_lock(&clp->cl_lock);
1503 __nfsd4_hash_conn(conn, ses);
1504 spin_unlock(&clp->cl_lock);
1505 }
1506
nfsd4_register_conn(struct nfsd4_conn * conn)1507 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1508 {
1509 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1510 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1511 }
1512
nfsd4_init_conn(struct svc_rqst * rqstp,struct nfsd4_conn * conn,struct nfsd4_session * ses)1513 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1514 {
1515 int ret;
1516
1517 nfsd4_hash_conn(conn, ses);
1518 ret = nfsd4_register_conn(conn);
1519 if (ret)
1520 /* oops; xprt is already down: */
1521 nfsd4_conn_lost(&conn->cn_xpt_user);
1522 /* We may have gained or lost a callback channel: */
1523 nfsd4_probe_callback_sync(ses->se_client);
1524 }
1525
alloc_conn_from_crses(struct svc_rqst * rqstp,struct nfsd4_create_session * cses)1526 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1527 {
1528 u32 dir = NFS4_CDFC4_FORE;
1529
1530 if (cses->flags & SESSION4_BACK_CHAN)
1531 dir |= NFS4_CDFC4_BACK;
1532 return alloc_conn(rqstp, dir);
1533 }
1534
1535 /* must be called under client_lock */
nfsd4_del_conns(struct nfsd4_session * s)1536 static void nfsd4_del_conns(struct nfsd4_session *s)
1537 {
1538 struct nfs4_client *clp = s->se_client;
1539 struct nfsd4_conn *c;
1540
1541 spin_lock(&clp->cl_lock);
1542 while (!list_empty(&s->se_conns)) {
1543 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1544 list_del_init(&c->cn_persession);
1545 spin_unlock(&clp->cl_lock);
1546
1547 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1548 free_conn(c);
1549
1550 spin_lock(&clp->cl_lock);
1551 }
1552 spin_unlock(&clp->cl_lock);
1553 }
1554
__free_session(struct nfsd4_session * ses)1555 static void __free_session(struct nfsd4_session *ses)
1556 {
1557 free_session_slots(ses);
1558 kfree(ses);
1559 }
1560
free_session(struct nfsd4_session * ses)1561 static void free_session(struct nfsd4_session *ses)
1562 {
1563 nfsd4_del_conns(ses);
1564 nfsd4_put_drc_mem(&ses->se_fchannel);
1565 __free_session(ses);
1566 }
1567
init_session(struct svc_rqst * rqstp,struct nfsd4_session * new,struct nfs4_client * clp,struct nfsd4_create_session * cses)1568 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1569 {
1570 int idx;
1571 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1572
1573 new->se_client = clp;
1574 gen_sessionid(new);
1575
1576 INIT_LIST_HEAD(&new->se_conns);
1577
1578 new->se_cb_seq_nr = 1;
1579 new->se_flags = cses->flags;
1580 new->se_cb_prog = cses->callback_prog;
1581 new->se_cb_sec = cses->cb_sec;
1582 atomic_set(&new->se_ref, 0);
1583 idx = hash_sessionid(&new->se_sessionid);
1584 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1585 spin_lock(&clp->cl_lock);
1586 list_add(&new->se_perclnt, &clp->cl_sessions);
1587 spin_unlock(&clp->cl_lock);
1588
1589 {
1590 struct sockaddr *sa = svc_addr(rqstp);
1591 /*
1592 * This is a little silly; with sessions there's no real
1593 * use for the callback address. Use the peer address
1594 * as a reasonable default for now, but consider fixing
1595 * the rpc client not to require an address in the
1596 * future:
1597 */
1598 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1599 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1600 }
1601 }
1602
1603 /* caller must hold client_lock */
1604 static struct nfsd4_session *
__find_in_sessionid_hashtbl(struct nfs4_sessionid * sessionid,struct net * net)1605 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1606 {
1607 struct nfsd4_session *elem;
1608 int idx;
1609 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1610
1611 lockdep_assert_held(&nn->client_lock);
1612
1613 dump_sessionid(__func__, sessionid);
1614 idx = hash_sessionid(sessionid);
1615 /* Search in the appropriate list */
1616 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1617 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1618 NFS4_MAX_SESSIONID_LEN)) {
1619 return elem;
1620 }
1621 }
1622
1623 dprintk("%s: session not found\n", __func__);
1624 return NULL;
1625 }
1626
1627 static struct nfsd4_session *
find_in_sessionid_hashtbl(struct nfs4_sessionid * sessionid,struct net * net,__be32 * ret)1628 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1629 __be32 *ret)
1630 {
1631 struct nfsd4_session *session;
1632 __be32 status = nfserr_badsession;
1633
1634 session = __find_in_sessionid_hashtbl(sessionid, net);
1635 if (!session)
1636 goto out;
1637 status = nfsd4_get_session_locked(session);
1638 if (status)
1639 session = NULL;
1640 out:
1641 *ret = status;
1642 return session;
1643 }
1644
1645 /* caller must hold client_lock */
1646 static void
unhash_session(struct nfsd4_session * ses)1647 unhash_session(struct nfsd4_session *ses)
1648 {
1649 struct nfs4_client *clp = ses->se_client;
1650 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1651
1652 lockdep_assert_held(&nn->client_lock);
1653
1654 list_del(&ses->se_hash);
1655 spin_lock(&ses->se_client->cl_lock);
1656 list_del(&ses->se_perclnt);
1657 spin_unlock(&ses->se_client->cl_lock);
1658 }
1659
1660 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1661 static int
STALE_CLIENTID(clientid_t * clid,struct nfsd_net * nn)1662 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1663 {
1664 /*
1665 * We're assuming the clid was not given out from a boot
1666 * precisely 2^32 (about 136 years) before this one. That seems
1667 * a safe assumption:
1668 */
1669 if (clid->cl_boot == (u32)nn->boot_time)
1670 return 0;
1671 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1672 clid->cl_boot, clid->cl_id, nn->boot_time);
1673 return 1;
1674 }
1675
1676 /*
1677 * XXX Should we use a slab cache ?
1678 * This type of memory management is somewhat inefficient, but we use it
1679 * anyway since SETCLIENTID is not a common operation.
1680 */
alloc_client(struct xdr_netobj name)1681 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1682 {
1683 struct nfs4_client *clp;
1684 int i;
1685
1686 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1687 if (clp == NULL)
1688 return NULL;
1689 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1690 if (clp->cl_name.data == NULL)
1691 goto err_no_name;
1692 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1693 OWNER_HASH_SIZE, GFP_KERNEL);
1694 if (!clp->cl_ownerstr_hashtbl)
1695 goto err_no_hashtbl;
1696 for (i = 0; i < OWNER_HASH_SIZE; i++)
1697 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1698 clp->cl_name.len = name.len;
1699 INIT_LIST_HEAD(&clp->cl_sessions);
1700 idr_init(&clp->cl_stateids);
1701 atomic_set(&clp->cl_refcount, 0);
1702 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1703 INIT_LIST_HEAD(&clp->cl_idhash);
1704 INIT_LIST_HEAD(&clp->cl_openowners);
1705 INIT_LIST_HEAD(&clp->cl_delegations);
1706 INIT_LIST_HEAD(&clp->cl_lru);
1707 INIT_LIST_HEAD(&clp->cl_revoked);
1708 #ifdef CONFIG_NFSD_PNFS
1709 INIT_LIST_HEAD(&clp->cl_lo_states);
1710 #endif
1711 spin_lock_init(&clp->cl_lock);
1712 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1713 return clp;
1714 err_no_hashtbl:
1715 kfree(clp->cl_name.data);
1716 err_no_name:
1717 kfree(clp);
1718 return NULL;
1719 }
1720
1721 static void
free_client(struct nfs4_client * clp)1722 free_client(struct nfs4_client *clp)
1723 {
1724 while (!list_empty(&clp->cl_sessions)) {
1725 struct nfsd4_session *ses;
1726 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1727 se_perclnt);
1728 list_del(&ses->se_perclnt);
1729 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1730 free_session(ses);
1731 }
1732 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1733 free_svc_cred(&clp->cl_cred);
1734 kfree(clp->cl_ownerstr_hashtbl);
1735 kfree(clp->cl_name.data);
1736 idr_destroy(&clp->cl_stateids);
1737 kfree(clp);
1738 }
1739
1740 /* must be called under the client_lock */
1741 static void
unhash_client_locked(struct nfs4_client * clp)1742 unhash_client_locked(struct nfs4_client *clp)
1743 {
1744 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1745 struct nfsd4_session *ses;
1746
1747 lockdep_assert_held(&nn->client_lock);
1748
1749 /* Mark the client as expired! */
1750 clp->cl_time = 0;
1751 /* Make it invisible */
1752 if (!list_empty(&clp->cl_idhash)) {
1753 list_del_init(&clp->cl_idhash);
1754 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1755 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1756 else
1757 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1758 }
1759 list_del_init(&clp->cl_lru);
1760 spin_lock(&clp->cl_lock);
1761 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1762 list_del_init(&ses->se_hash);
1763 spin_unlock(&clp->cl_lock);
1764 }
1765
1766 static void
unhash_client(struct nfs4_client * clp)1767 unhash_client(struct nfs4_client *clp)
1768 {
1769 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1770
1771 spin_lock(&nn->client_lock);
1772 unhash_client_locked(clp);
1773 spin_unlock(&nn->client_lock);
1774 }
1775
mark_client_expired_locked(struct nfs4_client * clp)1776 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1777 {
1778 if (atomic_read(&clp->cl_refcount))
1779 return nfserr_jukebox;
1780 unhash_client_locked(clp);
1781 return nfs_ok;
1782 }
1783
1784 static void
__destroy_client(struct nfs4_client * clp)1785 __destroy_client(struct nfs4_client *clp)
1786 {
1787 struct nfs4_openowner *oo;
1788 struct nfs4_delegation *dp;
1789 struct list_head reaplist;
1790
1791 INIT_LIST_HEAD(&reaplist);
1792 spin_lock(&state_lock);
1793 while (!list_empty(&clp->cl_delegations)) {
1794 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1795 WARN_ON(!unhash_delegation_locked(dp));
1796 list_add(&dp->dl_recall_lru, &reaplist);
1797 }
1798 spin_unlock(&state_lock);
1799 while (!list_empty(&reaplist)) {
1800 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1801 list_del_init(&dp->dl_recall_lru);
1802 put_clnt_odstate(dp->dl_clnt_odstate);
1803 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1804 nfs4_put_stid(&dp->dl_stid);
1805 }
1806 while (!list_empty(&clp->cl_revoked)) {
1807 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1808 list_del_init(&dp->dl_recall_lru);
1809 nfs4_put_stid(&dp->dl_stid);
1810 }
1811 while (!list_empty(&clp->cl_openowners)) {
1812 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1813 nfs4_get_stateowner(&oo->oo_owner);
1814 release_openowner(oo);
1815 }
1816 nfsd4_return_all_client_layouts(clp);
1817 nfsd4_shutdown_callback(clp);
1818 if (clp->cl_cb_conn.cb_xprt)
1819 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1820 free_client(clp);
1821 }
1822
1823 static void
destroy_client(struct nfs4_client * clp)1824 destroy_client(struct nfs4_client *clp)
1825 {
1826 unhash_client(clp);
1827 __destroy_client(clp);
1828 }
1829
expire_client(struct nfs4_client * clp)1830 static void expire_client(struct nfs4_client *clp)
1831 {
1832 unhash_client(clp);
1833 nfsd4_client_record_remove(clp);
1834 __destroy_client(clp);
1835 }
1836
copy_verf(struct nfs4_client * target,nfs4_verifier * source)1837 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1838 {
1839 memcpy(target->cl_verifier.data, source->data,
1840 sizeof(target->cl_verifier.data));
1841 }
1842
copy_clid(struct nfs4_client * target,struct nfs4_client * source)1843 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1844 {
1845 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1846 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1847 }
1848
copy_cred(struct svc_cred * target,struct svc_cred * source)1849 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1850 {
1851 if (source->cr_principal) {
1852 target->cr_principal =
1853 kstrdup(source->cr_principal, GFP_KERNEL);
1854 if (target->cr_principal == NULL)
1855 return -ENOMEM;
1856 } else
1857 target->cr_principal = NULL;
1858 target->cr_flavor = source->cr_flavor;
1859 target->cr_uid = source->cr_uid;
1860 target->cr_gid = source->cr_gid;
1861 target->cr_group_info = source->cr_group_info;
1862 get_group_info(target->cr_group_info);
1863 target->cr_gss_mech = source->cr_gss_mech;
1864 if (source->cr_gss_mech)
1865 gss_mech_get(source->cr_gss_mech);
1866 return 0;
1867 }
1868
1869 static int
compare_blob(const struct xdr_netobj * o1,const struct xdr_netobj * o2)1870 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1871 {
1872 if (o1->len < o2->len)
1873 return -1;
1874 if (o1->len > o2->len)
1875 return 1;
1876 return memcmp(o1->data, o2->data, o1->len);
1877 }
1878
same_name(const char * n1,const char * n2)1879 static int same_name(const char *n1, const char *n2)
1880 {
1881 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1882 }
1883
1884 static int
same_verf(nfs4_verifier * v1,nfs4_verifier * v2)1885 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1886 {
1887 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1888 }
1889
1890 static int
same_clid(clientid_t * cl1,clientid_t * cl2)1891 same_clid(clientid_t *cl1, clientid_t *cl2)
1892 {
1893 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1894 }
1895
groups_equal(struct group_info * g1,struct group_info * g2)1896 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1897 {
1898 int i;
1899
1900 if (g1->ngroups != g2->ngroups)
1901 return false;
1902 for (i=0; i<g1->ngroups; i++)
1903 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1904 return false;
1905 return true;
1906 }
1907
1908 /*
1909 * RFC 3530 language requires clid_inuse be returned when the
1910 * "principal" associated with a requests differs from that previously
1911 * used. We use uid, gid's, and gss principal string as our best
1912 * approximation. We also don't want to allow non-gss use of a client
1913 * established using gss: in theory cr_principal should catch that
1914 * change, but in practice cr_principal can be null even in the gss case
1915 * since gssd doesn't always pass down a principal string.
1916 */
is_gss_cred(struct svc_cred * cr)1917 static bool is_gss_cred(struct svc_cred *cr)
1918 {
1919 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1920 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1921 }
1922
1923
1924 static bool
same_creds(struct svc_cred * cr1,struct svc_cred * cr2)1925 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1926 {
1927 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1928 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1929 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1930 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1931 return false;
1932 if (cr1->cr_principal == cr2->cr_principal)
1933 return true;
1934 if (!cr1->cr_principal || !cr2->cr_principal)
1935 return false;
1936 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1937 }
1938
svc_rqst_integrity_protected(struct svc_rqst * rqstp)1939 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1940 {
1941 struct svc_cred *cr = &rqstp->rq_cred;
1942 u32 service;
1943
1944 if (!cr->cr_gss_mech)
1945 return false;
1946 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1947 return service == RPC_GSS_SVC_INTEGRITY ||
1948 service == RPC_GSS_SVC_PRIVACY;
1949 }
1950
mach_creds_match(struct nfs4_client * cl,struct svc_rqst * rqstp)1951 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1952 {
1953 struct svc_cred *cr = &rqstp->rq_cred;
1954
1955 if (!cl->cl_mach_cred)
1956 return true;
1957 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1958 return false;
1959 if (!svc_rqst_integrity_protected(rqstp))
1960 return false;
1961 if (!cr->cr_principal)
1962 return false;
1963 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1964 }
1965
gen_confirm(struct nfs4_client * clp,struct nfsd_net * nn)1966 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
1967 {
1968 __be32 verf[2];
1969
1970 /*
1971 * This is opaque to client, so no need to byte-swap. Use
1972 * __force to keep sparse happy
1973 */
1974 verf[0] = (__force __be32)get_seconds();
1975 verf[1] = (__force __be32)nn->clverifier_counter++;
1976 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1977 }
1978
gen_clid(struct nfs4_client * clp,struct nfsd_net * nn)1979 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1980 {
1981 clp->cl_clientid.cl_boot = nn->boot_time;
1982 clp->cl_clientid.cl_id = nn->clientid_counter++;
1983 gen_confirm(clp, nn);
1984 }
1985
1986 static struct nfs4_stid *
find_stateid_locked(struct nfs4_client * cl,stateid_t * t)1987 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1988 {
1989 struct nfs4_stid *ret;
1990
1991 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1992 if (!ret || !ret->sc_type)
1993 return NULL;
1994 return ret;
1995 }
1996
1997 static struct nfs4_stid *
find_stateid_by_type(struct nfs4_client * cl,stateid_t * t,char typemask)1998 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1999 {
2000 struct nfs4_stid *s;
2001
2002 spin_lock(&cl->cl_lock);
2003 s = find_stateid_locked(cl, t);
2004 if (s != NULL) {
2005 if (typemask & s->sc_type)
2006 atomic_inc(&s->sc_count);
2007 else
2008 s = NULL;
2009 }
2010 spin_unlock(&cl->cl_lock);
2011 return s;
2012 }
2013
create_client(struct xdr_netobj name,struct svc_rqst * rqstp,nfs4_verifier * verf)2014 static struct nfs4_client *create_client(struct xdr_netobj name,
2015 struct svc_rqst *rqstp, nfs4_verifier *verf)
2016 {
2017 struct nfs4_client *clp;
2018 struct sockaddr *sa = svc_addr(rqstp);
2019 int ret;
2020 struct net *net = SVC_NET(rqstp);
2021
2022 clp = alloc_client(name);
2023 if (clp == NULL)
2024 return NULL;
2025
2026 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2027 if (ret) {
2028 free_client(clp);
2029 return NULL;
2030 }
2031 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2032 clp->cl_time = get_seconds();
2033 clear_bit(0, &clp->cl_cb_slot_busy);
2034 copy_verf(clp, verf);
2035 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
2036 clp->cl_cb_session = NULL;
2037 clp->net = net;
2038 return clp;
2039 }
2040
2041 static void
add_clp_to_name_tree(struct nfs4_client * new_clp,struct rb_root * root)2042 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2043 {
2044 struct rb_node **new = &(root->rb_node), *parent = NULL;
2045 struct nfs4_client *clp;
2046
2047 while (*new) {
2048 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2049 parent = *new;
2050
2051 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2052 new = &((*new)->rb_left);
2053 else
2054 new = &((*new)->rb_right);
2055 }
2056
2057 rb_link_node(&new_clp->cl_namenode, parent, new);
2058 rb_insert_color(&new_clp->cl_namenode, root);
2059 }
2060
2061 static struct nfs4_client *
find_clp_in_name_tree(struct xdr_netobj * name,struct rb_root * root)2062 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2063 {
2064 int cmp;
2065 struct rb_node *node = root->rb_node;
2066 struct nfs4_client *clp;
2067
2068 while (node) {
2069 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2070 cmp = compare_blob(&clp->cl_name, name);
2071 if (cmp > 0)
2072 node = node->rb_left;
2073 else if (cmp < 0)
2074 node = node->rb_right;
2075 else
2076 return clp;
2077 }
2078 return NULL;
2079 }
2080
2081 static void
add_to_unconfirmed(struct nfs4_client * clp)2082 add_to_unconfirmed(struct nfs4_client *clp)
2083 {
2084 unsigned int idhashval;
2085 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2086
2087 lockdep_assert_held(&nn->client_lock);
2088
2089 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2090 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2091 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2092 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2093 renew_client_locked(clp);
2094 }
2095
2096 static void
move_to_confirmed(struct nfs4_client * clp)2097 move_to_confirmed(struct nfs4_client *clp)
2098 {
2099 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2100 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2101
2102 lockdep_assert_held(&nn->client_lock);
2103
2104 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2105 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2106 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2107 add_clp_to_name_tree(clp, &nn->conf_name_tree);
2108 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2109 renew_client_locked(clp);
2110 }
2111
2112 static struct nfs4_client *
find_client_in_id_table(struct list_head * tbl,clientid_t * clid,bool sessions)2113 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2114 {
2115 struct nfs4_client *clp;
2116 unsigned int idhashval = clientid_hashval(clid->cl_id);
2117
2118 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2119 if (same_clid(&clp->cl_clientid, clid)) {
2120 if ((bool)clp->cl_minorversion != sessions)
2121 return NULL;
2122 renew_client_locked(clp);
2123 return clp;
2124 }
2125 }
2126 return NULL;
2127 }
2128
2129 static struct nfs4_client *
find_confirmed_client(clientid_t * clid,bool sessions,struct nfsd_net * nn)2130 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2131 {
2132 struct list_head *tbl = nn->conf_id_hashtbl;
2133
2134 lockdep_assert_held(&nn->client_lock);
2135 return find_client_in_id_table(tbl, clid, sessions);
2136 }
2137
2138 static struct nfs4_client *
find_unconfirmed_client(clientid_t * clid,bool sessions,struct nfsd_net * nn)2139 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2140 {
2141 struct list_head *tbl = nn->unconf_id_hashtbl;
2142
2143 lockdep_assert_held(&nn->client_lock);
2144 return find_client_in_id_table(tbl, clid, sessions);
2145 }
2146
clp_used_exchangeid(struct nfs4_client * clp)2147 static bool clp_used_exchangeid(struct nfs4_client *clp)
2148 {
2149 return clp->cl_exchange_flags != 0;
2150 }
2151
2152 static struct nfs4_client *
find_confirmed_client_by_name(struct xdr_netobj * name,struct nfsd_net * nn)2153 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2154 {
2155 lockdep_assert_held(&nn->client_lock);
2156 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2157 }
2158
2159 static struct nfs4_client *
find_unconfirmed_client_by_name(struct xdr_netobj * name,struct nfsd_net * nn)2160 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2161 {
2162 lockdep_assert_held(&nn->client_lock);
2163 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2164 }
2165
2166 static void
gen_callback(struct nfs4_client * clp,struct nfsd4_setclientid * se,struct svc_rqst * rqstp)2167 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2168 {
2169 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2170 struct sockaddr *sa = svc_addr(rqstp);
2171 u32 scopeid = rpc_get_scope_id(sa);
2172 unsigned short expected_family;
2173
2174 /* Currently, we only support tcp and tcp6 for the callback channel */
2175 if (se->se_callback_netid_len == 3 &&
2176 !memcmp(se->se_callback_netid_val, "tcp", 3))
2177 expected_family = AF_INET;
2178 else if (se->se_callback_netid_len == 4 &&
2179 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2180 expected_family = AF_INET6;
2181 else
2182 goto out_err;
2183
2184 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2185 se->se_callback_addr_len,
2186 (struct sockaddr *)&conn->cb_addr,
2187 sizeof(conn->cb_addr));
2188
2189 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2190 goto out_err;
2191
2192 if (conn->cb_addr.ss_family == AF_INET6)
2193 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2194
2195 conn->cb_prog = se->se_callback_prog;
2196 conn->cb_ident = se->se_callback_ident;
2197 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2198 return;
2199 out_err:
2200 conn->cb_addr.ss_family = AF_UNSPEC;
2201 conn->cb_addrlen = 0;
2202 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2203 "will not receive delegations\n",
2204 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2205
2206 return;
2207 }
2208
2209 /*
2210 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2211 */
2212 static void
nfsd4_store_cache_entry(struct nfsd4_compoundres * resp)2213 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2214 {
2215 struct xdr_buf *buf = resp->xdr.buf;
2216 struct nfsd4_slot *slot = resp->cstate.slot;
2217 unsigned int base;
2218
2219 dprintk("--> %s slot %p\n", __func__, slot);
2220
2221 slot->sl_opcnt = resp->opcnt;
2222 slot->sl_status = resp->cstate.status;
2223
2224 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2225 if (nfsd4_not_cached(resp)) {
2226 slot->sl_datalen = 0;
2227 return;
2228 }
2229 base = resp->cstate.data_offset;
2230 slot->sl_datalen = buf->len - base;
2231 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2232 WARN("%s: sessions DRC could not cache compound\n", __func__);
2233 return;
2234 }
2235
2236 /*
2237 * Encode the replay sequence operation from the slot values.
2238 * If cachethis is FALSE encode the uncached rep error on the next
2239 * operation which sets resp->p and increments resp->opcnt for
2240 * nfs4svc_encode_compoundres.
2241 *
2242 */
2243 static __be32
nfsd4_enc_sequence_replay(struct nfsd4_compoundargs * args,struct nfsd4_compoundres * resp)2244 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2245 struct nfsd4_compoundres *resp)
2246 {
2247 struct nfsd4_op *op;
2248 struct nfsd4_slot *slot = resp->cstate.slot;
2249
2250 /* Encode the replayed sequence operation */
2251 op = &args->ops[resp->opcnt - 1];
2252 nfsd4_encode_operation(resp, op);
2253
2254 /* Return nfserr_retry_uncached_rep in next operation. */
2255 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2256 op = &args->ops[resp->opcnt++];
2257 op->status = nfserr_retry_uncached_rep;
2258 nfsd4_encode_operation(resp, op);
2259 }
2260 return op->status;
2261 }
2262
2263 /*
2264 * The sequence operation is not cached because we can use the slot and
2265 * session values.
2266 */
2267 static __be32
nfsd4_replay_cache_entry(struct nfsd4_compoundres * resp,struct nfsd4_sequence * seq)2268 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2269 struct nfsd4_sequence *seq)
2270 {
2271 struct nfsd4_slot *slot = resp->cstate.slot;
2272 struct xdr_stream *xdr = &resp->xdr;
2273 __be32 *p;
2274 __be32 status;
2275
2276 dprintk("--> %s slot %p\n", __func__, slot);
2277
2278 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2279 if (status)
2280 return status;
2281
2282 p = xdr_reserve_space(xdr, slot->sl_datalen);
2283 if (!p) {
2284 WARN_ON_ONCE(1);
2285 return nfserr_serverfault;
2286 }
2287 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2288 xdr_commit_encode(xdr);
2289
2290 resp->opcnt = slot->sl_opcnt;
2291 return slot->sl_status;
2292 }
2293
2294 /*
2295 * Set the exchange_id flags returned by the server.
2296 */
2297 static void
nfsd4_set_ex_flags(struct nfs4_client * new,struct nfsd4_exchange_id * clid)2298 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2299 {
2300 #ifdef CONFIG_NFSD_PNFS
2301 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2302 #else
2303 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2304 #endif
2305
2306 /* Referrals are supported, Migration is not. */
2307 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2308
2309 /* set the wire flags to return to client. */
2310 clid->flags = new->cl_exchange_flags;
2311 }
2312
client_has_openowners(struct nfs4_client * clp)2313 static bool client_has_openowners(struct nfs4_client *clp)
2314 {
2315 struct nfs4_openowner *oo;
2316
2317 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2318 if (!list_empty(&oo->oo_owner.so_stateids))
2319 return true;
2320 }
2321 return false;
2322 }
2323
client_has_state(struct nfs4_client * clp)2324 static bool client_has_state(struct nfs4_client *clp)
2325 {
2326 return client_has_openowners(clp)
2327 #ifdef CONFIG_NFSD_PNFS
2328 || !list_empty(&clp->cl_lo_states)
2329 #endif
2330 || !list_empty(&clp->cl_delegations)
2331 || !list_empty(&clp->cl_sessions);
2332 }
2333
2334 __be32
nfsd4_exchange_id(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_exchange_id * exid)2335 nfsd4_exchange_id(struct svc_rqst *rqstp,
2336 struct nfsd4_compound_state *cstate,
2337 struct nfsd4_exchange_id *exid)
2338 {
2339 struct nfs4_client *conf, *new;
2340 struct nfs4_client *unconf = NULL;
2341 __be32 status;
2342 char addr_str[INET6_ADDRSTRLEN];
2343 nfs4_verifier verf = exid->verifier;
2344 struct sockaddr *sa = svc_addr(rqstp);
2345 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2346 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2347
2348 rpc_ntop(sa, addr_str, sizeof(addr_str));
2349 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2350 "ip_addr=%s flags %x, spa_how %d\n",
2351 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2352 addr_str, exid->flags, exid->spa_how);
2353
2354 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2355 return nfserr_inval;
2356
2357 switch (exid->spa_how) {
2358 case SP4_MACH_CRED:
2359 if (!svc_rqst_integrity_protected(rqstp))
2360 return nfserr_inval;
2361 case SP4_NONE:
2362 break;
2363 default: /* checked by xdr code */
2364 WARN_ON_ONCE(1);
2365 case SP4_SSV:
2366 return nfserr_encr_alg_unsupp;
2367 }
2368
2369 new = create_client(exid->clname, rqstp, &verf);
2370 if (new == NULL)
2371 return nfserr_jukebox;
2372
2373 /* Cases below refer to rfc 5661 section 18.35.4: */
2374 spin_lock(&nn->client_lock);
2375 conf = find_confirmed_client_by_name(&exid->clname, nn);
2376 if (conf) {
2377 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2378 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2379
2380 if (update) {
2381 if (!clp_used_exchangeid(conf)) { /* buggy client */
2382 status = nfserr_inval;
2383 goto out;
2384 }
2385 if (!mach_creds_match(conf, rqstp)) {
2386 status = nfserr_wrong_cred;
2387 goto out;
2388 }
2389 if (!creds_match) { /* case 9 */
2390 status = nfserr_perm;
2391 goto out;
2392 }
2393 if (!verfs_match) { /* case 8 */
2394 status = nfserr_not_same;
2395 goto out;
2396 }
2397 /* case 6 */
2398 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2399 goto out_copy;
2400 }
2401 if (!creds_match) { /* case 3 */
2402 if (client_has_state(conf)) {
2403 status = nfserr_clid_inuse;
2404 goto out;
2405 }
2406 goto out_new;
2407 }
2408 if (verfs_match) { /* case 2 */
2409 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2410 goto out_copy;
2411 }
2412 /* case 5, client reboot */
2413 conf = NULL;
2414 goto out_new;
2415 }
2416
2417 if (update) { /* case 7 */
2418 status = nfserr_noent;
2419 goto out;
2420 }
2421
2422 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2423 if (unconf) /* case 4, possible retry or client restart */
2424 unhash_client_locked(unconf);
2425
2426 /* case 1 (normal case) */
2427 out_new:
2428 if (conf) {
2429 status = mark_client_expired_locked(conf);
2430 if (status)
2431 goto out;
2432 }
2433 new->cl_minorversion = cstate->minorversion;
2434 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2435
2436 gen_clid(new, nn);
2437 add_to_unconfirmed(new);
2438 swap(new, conf);
2439 out_copy:
2440 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2441 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2442
2443 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2444 nfsd4_set_ex_flags(conf, exid);
2445
2446 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2447 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2448 status = nfs_ok;
2449
2450 out:
2451 spin_unlock(&nn->client_lock);
2452 if (new)
2453 expire_client(new);
2454 if (unconf)
2455 expire_client(unconf);
2456 return status;
2457 }
2458
2459 static __be32
check_slot_seqid(u32 seqid,u32 slot_seqid,int slot_inuse)2460 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2461 {
2462 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2463 slot_seqid);
2464
2465 /* The slot is in use, and no response has been sent. */
2466 if (slot_inuse) {
2467 if (seqid == slot_seqid)
2468 return nfserr_jukebox;
2469 else
2470 return nfserr_seq_misordered;
2471 }
2472 /* Note unsigned 32-bit arithmetic handles wraparound: */
2473 if (likely(seqid == slot_seqid + 1))
2474 return nfs_ok;
2475 if (seqid == slot_seqid)
2476 return nfserr_replay_cache;
2477 return nfserr_seq_misordered;
2478 }
2479
2480 /*
2481 * Cache the create session result into the create session single DRC
2482 * slot cache by saving the xdr structure. sl_seqid has been set.
2483 * Do this for solo or embedded create session operations.
2484 */
2485 static void
nfsd4_cache_create_session(struct nfsd4_create_session * cr_ses,struct nfsd4_clid_slot * slot,__be32 nfserr)2486 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2487 struct nfsd4_clid_slot *slot, __be32 nfserr)
2488 {
2489 slot->sl_status = nfserr;
2490 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2491 }
2492
2493 static __be32
nfsd4_replay_create_session(struct nfsd4_create_session * cr_ses,struct nfsd4_clid_slot * slot)2494 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2495 struct nfsd4_clid_slot *slot)
2496 {
2497 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2498 return slot->sl_status;
2499 }
2500
2501 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2502 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2503 1 + /* MIN tag is length with zero, only length */ \
2504 3 + /* version, opcount, opcode */ \
2505 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2506 /* seqid, slotID, slotID, cache */ \
2507 4 ) * sizeof(__be32))
2508
2509 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2510 2 + /* verifier: AUTH_NULL, length 0 */\
2511 1 + /* status */ \
2512 1 + /* MIN tag is length with zero, only length */ \
2513 3 + /* opcount, opcode, opstatus*/ \
2514 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2515 /* seqid, slotID, slotID, slotID, status */ \
2516 5 ) * sizeof(__be32))
2517
check_forechannel_attrs(struct nfsd4_channel_attrs * ca,struct nfsd_net * nn)2518 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2519 {
2520 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2521
2522 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2523 return nfserr_toosmall;
2524 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2525 return nfserr_toosmall;
2526 ca->headerpadsz = 0;
2527 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2528 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2529 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2530 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2531 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2532 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2533 /*
2534 * Note decreasing slot size below client's request may make it
2535 * difficult for client to function correctly, whereas
2536 * decreasing the number of slots will (just?) affect
2537 * performance. When short on memory we therefore prefer to
2538 * decrease number of slots instead of their size. Clients that
2539 * request larger slots than they need will get poor results:
2540 */
2541 ca->maxreqs = nfsd4_get_drc_mem(ca);
2542 if (!ca->maxreqs)
2543 return nfserr_jukebox;
2544
2545 return nfs_ok;
2546 }
2547
2548 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2549 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2550 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2551 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2552
check_backchannel_attrs(struct nfsd4_channel_attrs * ca)2553 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2554 {
2555 ca->headerpadsz = 0;
2556
2557 /*
2558 * These RPC_MAX_HEADER macros are overkill, especially since we
2559 * don't even do gss on the backchannel yet. But this is still
2560 * less than 1k. Tighten up this estimate in the unlikely event
2561 * it turns out to be a problem for some client:
2562 */
2563 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2564 return nfserr_toosmall;
2565 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2566 return nfserr_toosmall;
2567 ca->maxresp_cached = 0;
2568 if (ca->maxops < 2)
2569 return nfserr_toosmall;
2570
2571 return nfs_ok;
2572 }
2573
nfsd4_check_cb_sec(struct nfsd4_cb_sec * cbs)2574 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2575 {
2576 switch (cbs->flavor) {
2577 case RPC_AUTH_NULL:
2578 case RPC_AUTH_UNIX:
2579 return nfs_ok;
2580 default:
2581 /*
2582 * GSS case: the spec doesn't allow us to return this
2583 * error. But it also doesn't allow us not to support
2584 * GSS.
2585 * I'd rather this fail hard than return some error the
2586 * client might think it can already handle:
2587 */
2588 return nfserr_encr_alg_unsupp;
2589 }
2590 }
2591
2592 __be32
nfsd4_create_session(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_create_session * cr_ses)2593 nfsd4_create_session(struct svc_rqst *rqstp,
2594 struct nfsd4_compound_state *cstate,
2595 struct nfsd4_create_session *cr_ses)
2596 {
2597 struct sockaddr *sa = svc_addr(rqstp);
2598 struct nfs4_client *conf, *unconf;
2599 struct nfs4_client *old = NULL;
2600 struct nfsd4_session *new;
2601 struct nfsd4_conn *conn;
2602 struct nfsd4_clid_slot *cs_slot = NULL;
2603 __be32 status = 0;
2604 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2605
2606 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2607 return nfserr_inval;
2608 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2609 if (status)
2610 return status;
2611 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2612 if (status)
2613 return status;
2614 status = check_backchannel_attrs(&cr_ses->back_channel);
2615 if (status)
2616 goto out_release_drc_mem;
2617 status = nfserr_jukebox;
2618 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2619 if (!new)
2620 goto out_release_drc_mem;
2621 conn = alloc_conn_from_crses(rqstp, cr_ses);
2622 if (!conn)
2623 goto out_free_session;
2624
2625 spin_lock(&nn->client_lock);
2626 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2627 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2628 WARN_ON_ONCE(conf && unconf);
2629
2630 if (conf) {
2631 status = nfserr_wrong_cred;
2632 if (!mach_creds_match(conf, rqstp))
2633 goto out_free_conn;
2634 cs_slot = &conf->cl_cs_slot;
2635 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2636 if (status) {
2637 if (status == nfserr_replay_cache)
2638 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2639 goto out_free_conn;
2640 }
2641 } else if (unconf) {
2642 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2643 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2644 status = nfserr_clid_inuse;
2645 goto out_free_conn;
2646 }
2647 status = nfserr_wrong_cred;
2648 if (!mach_creds_match(unconf, rqstp))
2649 goto out_free_conn;
2650 cs_slot = &unconf->cl_cs_slot;
2651 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2652 if (status) {
2653 /* an unconfirmed replay returns misordered */
2654 status = nfserr_seq_misordered;
2655 goto out_free_conn;
2656 }
2657 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2658 if (old) {
2659 status = mark_client_expired_locked(old);
2660 if (status) {
2661 old = NULL;
2662 goto out_free_conn;
2663 }
2664 }
2665 move_to_confirmed(unconf);
2666 conf = unconf;
2667 } else {
2668 status = nfserr_stale_clientid;
2669 goto out_free_conn;
2670 }
2671 status = nfs_ok;
2672 /*
2673 * We do not support RDMA or persistent sessions
2674 */
2675 cr_ses->flags &= ~SESSION4_PERSIST;
2676 cr_ses->flags &= ~SESSION4_RDMA;
2677
2678 init_session(rqstp, new, conf, cr_ses);
2679 nfsd4_get_session_locked(new);
2680
2681 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2682 NFS4_MAX_SESSIONID_LEN);
2683 cs_slot->sl_seqid++;
2684 cr_ses->seqid = cs_slot->sl_seqid;
2685
2686 /* cache solo and embedded create sessions under the client_lock */
2687 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2688 spin_unlock(&nn->client_lock);
2689 /* init connection and backchannel */
2690 nfsd4_init_conn(rqstp, conn, new);
2691 nfsd4_put_session(new);
2692 if (old)
2693 expire_client(old);
2694 return status;
2695 out_free_conn:
2696 spin_unlock(&nn->client_lock);
2697 free_conn(conn);
2698 if (old)
2699 expire_client(old);
2700 out_free_session:
2701 __free_session(new);
2702 out_release_drc_mem:
2703 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2704 return status;
2705 }
2706
nfsd4_map_bcts_dir(u32 * dir)2707 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2708 {
2709 switch (*dir) {
2710 case NFS4_CDFC4_FORE:
2711 case NFS4_CDFC4_BACK:
2712 return nfs_ok;
2713 case NFS4_CDFC4_FORE_OR_BOTH:
2714 case NFS4_CDFC4_BACK_OR_BOTH:
2715 *dir = NFS4_CDFC4_BOTH;
2716 return nfs_ok;
2717 };
2718 return nfserr_inval;
2719 }
2720
nfsd4_backchannel_ctl(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_backchannel_ctl * bc)2721 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2722 {
2723 struct nfsd4_session *session = cstate->session;
2724 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2725 __be32 status;
2726
2727 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2728 if (status)
2729 return status;
2730 spin_lock(&nn->client_lock);
2731 session->se_cb_prog = bc->bc_cb_program;
2732 session->se_cb_sec = bc->bc_cb_sec;
2733 spin_unlock(&nn->client_lock);
2734
2735 nfsd4_probe_callback(session->se_client);
2736
2737 return nfs_ok;
2738 }
2739
nfsd4_bind_conn_to_session(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_bind_conn_to_session * bcts)2740 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2741 struct nfsd4_compound_state *cstate,
2742 struct nfsd4_bind_conn_to_session *bcts)
2743 {
2744 __be32 status;
2745 struct nfsd4_conn *conn;
2746 struct nfsd4_session *session;
2747 struct net *net = SVC_NET(rqstp);
2748 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2749
2750 if (!nfsd4_last_compound_op(rqstp))
2751 return nfserr_not_only_op;
2752 spin_lock(&nn->client_lock);
2753 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2754 spin_unlock(&nn->client_lock);
2755 if (!session)
2756 goto out_no_session;
2757 status = nfserr_wrong_cred;
2758 if (!mach_creds_match(session->se_client, rqstp))
2759 goto out;
2760 status = nfsd4_map_bcts_dir(&bcts->dir);
2761 if (status)
2762 goto out;
2763 conn = alloc_conn(rqstp, bcts->dir);
2764 status = nfserr_jukebox;
2765 if (!conn)
2766 goto out;
2767 nfsd4_init_conn(rqstp, conn, session);
2768 status = nfs_ok;
2769 out:
2770 nfsd4_put_session(session);
2771 out_no_session:
2772 return status;
2773 }
2774
nfsd4_compound_in_session(struct nfsd4_session * session,struct nfs4_sessionid * sid)2775 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2776 {
2777 if (!session)
2778 return 0;
2779 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2780 }
2781
2782 __be32
nfsd4_destroy_session(struct svc_rqst * r,struct nfsd4_compound_state * cstate,struct nfsd4_destroy_session * sessionid)2783 nfsd4_destroy_session(struct svc_rqst *r,
2784 struct nfsd4_compound_state *cstate,
2785 struct nfsd4_destroy_session *sessionid)
2786 {
2787 struct nfsd4_session *ses;
2788 __be32 status;
2789 int ref_held_by_me = 0;
2790 struct net *net = SVC_NET(r);
2791 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2792
2793 status = nfserr_not_only_op;
2794 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2795 if (!nfsd4_last_compound_op(r))
2796 goto out;
2797 ref_held_by_me++;
2798 }
2799 dump_sessionid(__func__, &sessionid->sessionid);
2800 spin_lock(&nn->client_lock);
2801 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2802 if (!ses)
2803 goto out_client_lock;
2804 status = nfserr_wrong_cred;
2805 if (!mach_creds_match(ses->se_client, r))
2806 goto out_put_session;
2807 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2808 if (status)
2809 goto out_put_session;
2810 unhash_session(ses);
2811 spin_unlock(&nn->client_lock);
2812
2813 nfsd4_probe_callback_sync(ses->se_client);
2814
2815 spin_lock(&nn->client_lock);
2816 status = nfs_ok;
2817 out_put_session:
2818 nfsd4_put_session_locked(ses);
2819 out_client_lock:
2820 spin_unlock(&nn->client_lock);
2821 out:
2822 return status;
2823 }
2824
__nfsd4_find_conn(struct svc_xprt * xpt,struct nfsd4_session * s)2825 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2826 {
2827 struct nfsd4_conn *c;
2828
2829 list_for_each_entry(c, &s->se_conns, cn_persession) {
2830 if (c->cn_xprt == xpt) {
2831 return c;
2832 }
2833 }
2834 return NULL;
2835 }
2836
nfsd4_sequence_check_conn(struct nfsd4_conn * new,struct nfsd4_session * ses)2837 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2838 {
2839 struct nfs4_client *clp = ses->se_client;
2840 struct nfsd4_conn *c;
2841 __be32 status = nfs_ok;
2842 int ret;
2843
2844 spin_lock(&clp->cl_lock);
2845 c = __nfsd4_find_conn(new->cn_xprt, ses);
2846 if (c)
2847 goto out_free;
2848 status = nfserr_conn_not_bound_to_session;
2849 if (clp->cl_mach_cred)
2850 goto out_free;
2851 __nfsd4_hash_conn(new, ses);
2852 spin_unlock(&clp->cl_lock);
2853 ret = nfsd4_register_conn(new);
2854 if (ret)
2855 /* oops; xprt is already down: */
2856 nfsd4_conn_lost(&new->cn_xpt_user);
2857 return nfs_ok;
2858 out_free:
2859 spin_unlock(&clp->cl_lock);
2860 free_conn(new);
2861 return status;
2862 }
2863
nfsd4_session_too_many_ops(struct svc_rqst * rqstp,struct nfsd4_session * session)2864 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2865 {
2866 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2867
2868 return args->opcnt > session->se_fchannel.maxops;
2869 }
2870
nfsd4_request_too_big(struct svc_rqst * rqstp,struct nfsd4_session * session)2871 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2872 struct nfsd4_session *session)
2873 {
2874 struct xdr_buf *xb = &rqstp->rq_arg;
2875
2876 return xb->len > session->se_fchannel.maxreq_sz;
2877 }
2878
2879 __be32
nfsd4_sequence(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_sequence * seq)2880 nfsd4_sequence(struct svc_rqst *rqstp,
2881 struct nfsd4_compound_state *cstate,
2882 struct nfsd4_sequence *seq)
2883 {
2884 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2885 struct xdr_stream *xdr = &resp->xdr;
2886 struct nfsd4_session *session;
2887 struct nfs4_client *clp;
2888 struct nfsd4_slot *slot;
2889 struct nfsd4_conn *conn;
2890 __be32 status;
2891 int buflen;
2892 struct net *net = SVC_NET(rqstp);
2893 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2894
2895 if (resp->opcnt != 1)
2896 return nfserr_sequence_pos;
2897
2898 /*
2899 * Will be either used or freed by nfsd4_sequence_check_conn
2900 * below.
2901 */
2902 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2903 if (!conn)
2904 return nfserr_jukebox;
2905
2906 spin_lock(&nn->client_lock);
2907 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2908 if (!session)
2909 goto out_no_session;
2910 clp = session->se_client;
2911
2912 status = nfserr_too_many_ops;
2913 if (nfsd4_session_too_many_ops(rqstp, session))
2914 goto out_put_session;
2915
2916 status = nfserr_req_too_big;
2917 if (nfsd4_request_too_big(rqstp, session))
2918 goto out_put_session;
2919
2920 status = nfserr_badslot;
2921 if (seq->slotid >= session->se_fchannel.maxreqs)
2922 goto out_put_session;
2923
2924 slot = session->se_slots[seq->slotid];
2925 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2926
2927 /* We do not negotiate the number of slots yet, so set the
2928 * maxslots to the session maxreqs which is used to encode
2929 * sr_highest_slotid and the sr_target_slot id to maxslots */
2930 seq->maxslots = session->se_fchannel.maxreqs;
2931
2932 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2933 slot->sl_flags & NFSD4_SLOT_INUSE);
2934 if (status == nfserr_replay_cache) {
2935 status = nfserr_seq_misordered;
2936 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2937 goto out_put_session;
2938 cstate->slot = slot;
2939 cstate->session = session;
2940 cstate->clp = clp;
2941 /* Return the cached reply status and set cstate->status
2942 * for nfsd4_proc_compound processing */
2943 status = nfsd4_replay_cache_entry(resp, seq);
2944 cstate->status = nfserr_replay_cache;
2945 goto out;
2946 }
2947 if (status)
2948 goto out_put_session;
2949
2950 status = nfsd4_sequence_check_conn(conn, session);
2951 conn = NULL;
2952 if (status)
2953 goto out_put_session;
2954
2955 buflen = (seq->cachethis) ?
2956 session->se_fchannel.maxresp_cached :
2957 session->se_fchannel.maxresp_sz;
2958 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2959 nfserr_rep_too_big;
2960 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2961 goto out_put_session;
2962 svc_reserve(rqstp, buflen);
2963
2964 status = nfs_ok;
2965 /* Success! bump slot seqid */
2966 slot->sl_seqid = seq->seqid;
2967 slot->sl_flags |= NFSD4_SLOT_INUSE;
2968 if (seq->cachethis)
2969 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2970 else
2971 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2972
2973 cstate->slot = slot;
2974 cstate->session = session;
2975 cstate->clp = clp;
2976
2977 out:
2978 switch (clp->cl_cb_state) {
2979 case NFSD4_CB_DOWN:
2980 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2981 break;
2982 case NFSD4_CB_FAULT:
2983 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2984 break;
2985 default:
2986 seq->status_flags = 0;
2987 }
2988 if (!list_empty(&clp->cl_revoked))
2989 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2990 out_no_session:
2991 if (conn)
2992 free_conn(conn);
2993 spin_unlock(&nn->client_lock);
2994 return status;
2995 out_put_session:
2996 nfsd4_put_session_locked(session);
2997 goto out_no_session;
2998 }
2999
3000 void
nfsd4_sequence_done(struct nfsd4_compoundres * resp)3001 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3002 {
3003 struct nfsd4_compound_state *cs = &resp->cstate;
3004
3005 if (nfsd4_has_session(cs)) {
3006 if (cs->status != nfserr_replay_cache) {
3007 nfsd4_store_cache_entry(resp);
3008 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3009 }
3010 /* Drop session reference that was taken in nfsd4_sequence() */
3011 nfsd4_put_session(cs->session);
3012 } else if (cs->clp)
3013 put_client_renew(cs->clp);
3014 }
3015
3016 __be32
nfsd4_destroy_clientid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_destroy_clientid * dc)3017 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
3018 {
3019 struct nfs4_client *conf, *unconf;
3020 struct nfs4_client *clp = NULL;
3021 __be32 status = 0;
3022 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3023
3024 spin_lock(&nn->client_lock);
3025 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3026 conf = find_confirmed_client(&dc->clientid, true, nn);
3027 WARN_ON_ONCE(conf && unconf);
3028
3029 if (conf) {
3030 if (client_has_state(conf)) {
3031 status = nfserr_clientid_busy;
3032 goto out;
3033 }
3034 status = mark_client_expired_locked(conf);
3035 if (status)
3036 goto out;
3037 clp = conf;
3038 } else if (unconf)
3039 clp = unconf;
3040 else {
3041 status = nfserr_stale_clientid;
3042 goto out;
3043 }
3044 if (!mach_creds_match(clp, rqstp)) {
3045 clp = NULL;
3046 status = nfserr_wrong_cred;
3047 goto out;
3048 }
3049 unhash_client_locked(clp);
3050 out:
3051 spin_unlock(&nn->client_lock);
3052 if (clp)
3053 expire_client(clp);
3054 return status;
3055 }
3056
3057 __be32
nfsd4_reclaim_complete(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_reclaim_complete * rc)3058 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
3059 {
3060 __be32 status = 0;
3061
3062 if (rc->rca_one_fs) {
3063 if (!cstate->current_fh.fh_dentry)
3064 return nfserr_nofilehandle;
3065 /*
3066 * We don't take advantage of the rca_one_fs case.
3067 * That's OK, it's optional, we can safely ignore it.
3068 */
3069 return nfs_ok;
3070 }
3071
3072 status = nfserr_complete_already;
3073 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3074 &cstate->session->se_client->cl_flags))
3075 goto out;
3076
3077 status = nfserr_stale_clientid;
3078 if (is_client_expired(cstate->session->se_client))
3079 /*
3080 * The following error isn't really legal.
3081 * But we only get here if the client just explicitly
3082 * destroyed the client. Surely it no longer cares what
3083 * error it gets back on an operation for the dead
3084 * client.
3085 */
3086 goto out;
3087
3088 status = nfs_ok;
3089 nfsd4_client_record_create(cstate->session->se_client);
3090 out:
3091 return status;
3092 }
3093
3094 __be32
nfsd4_setclientid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_setclientid * setclid)3095 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3096 struct nfsd4_setclientid *setclid)
3097 {
3098 struct xdr_netobj clname = setclid->se_name;
3099 nfs4_verifier clverifier = setclid->se_verf;
3100 struct nfs4_client *conf, *new;
3101 struct nfs4_client *unconf = NULL;
3102 __be32 status;
3103 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3104
3105 new = create_client(clname, rqstp, &clverifier);
3106 if (new == NULL)
3107 return nfserr_jukebox;
3108 /* Cases below refer to rfc 3530 section 14.2.33: */
3109 spin_lock(&nn->client_lock);
3110 conf = find_confirmed_client_by_name(&clname, nn);
3111 if (conf && client_has_state(conf)) {
3112 /* case 0: */
3113 status = nfserr_clid_inuse;
3114 if (clp_used_exchangeid(conf))
3115 goto out;
3116 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3117 char addr_str[INET6_ADDRSTRLEN];
3118 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3119 sizeof(addr_str));
3120 dprintk("NFSD: setclientid: string in use by client "
3121 "at %s\n", addr_str);
3122 goto out;
3123 }
3124 }
3125 unconf = find_unconfirmed_client_by_name(&clname, nn);
3126 if (unconf)
3127 unhash_client_locked(unconf);
3128 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3129 /* case 1: probable callback update */
3130 copy_clid(new, conf);
3131 gen_confirm(new, nn);
3132 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3133 gen_clid(new, nn);
3134 new->cl_minorversion = 0;
3135 gen_callback(new, setclid, rqstp);
3136 add_to_unconfirmed(new);
3137 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3138 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3139 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3140 new = NULL;
3141 status = nfs_ok;
3142 out:
3143 spin_unlock(&nn->client_lock);
3144 if (new)
3145 free_client(new);
3146 if (unconf)
3147 expire_client(unconf);
3148 return status;
3149 }
3150
3151
3152 __be32
nfsd4_setclientid_confirm(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_setclientid_confirm * setclientid_confirm)3153 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3154 struct nfsd4_compound_state *cstate,
3155 struct nfsd4_setclientid_confirm *setclientid_confirm)
3156 {
3157 struct nfs4_client *conf, *unconf;
3158 struct nfs4_client *old = NULL;
3159 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3160 clientid_t * clid = &setclientid_confirm->sc_clientid;
3161 __be32 status;
3162 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3163
3164 if (STALE_CLIENTID(clid, nn))
3165 return nfserr_stale_clientid;
3166
3167 spin_lock(&nn->client_lock);
3168 conf = find_confirmed_client(clid, false, nn);
3169 unconf = find_unconfirmed_client(clid, false, nn);
3170 /*
3171 * We try hard to give out unique clientid's, so if we get an
3172 * attempt to confirm the same clientid with a different cred,
3173 * the client may be buggy; this should never happen.
3174 *
3175 * Nevertheless, RFC 7530 recommends INUSE for this case:
3176 */
3177 status = nfserr_clid_inuse;
3178 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3179 goto out;
3180 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3181 goto out;
3182 /* cases below refer to rfc 3530 section 14.2.34: */
3183 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3184 if (conf && !unconf) /* case 2: probable retransmit */
3185 status = nfs_ok;
3186 else /* case 4: client hasn't noticed we rebooted yet? */
3187 status = nfserr_stale_clientid;
3188 goto out;
3189 }
3190 status = nfs_ok;
3191 if (conf) { /* case 1: callback update */
3192 old = unconf;
3193 unhash_client_locked(old);
3194 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3195 } else { /* case 3: normal case; new or rebooted client */
3196 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3197 if (old) {
3198 status = nfserr_clid_inuse;
3199 if (client_has_state(old)
3200 && !same_creds(&unconf->cl_cred,
3201 &old->cl_cred))
3202 goto out;
3203 status = mark_client_expired_locked(old);
3204 if (status) {
3205 old = NULL;
3206 goto out;
3207 }
3208 }
3209 move_to_confirmed(unconf);
3210 conf = unconf;
3211 }
3212 get_client_locked(conf);
3213 spin_unlock(&nn->client_lock);
3214 nfsd4_probe_callback(conf);
3215 spin_lock(&nn->client_lock);
3216 put_client_renew_locked(conf);
3217 out:
3218 spin_unlock(&nn->client_lock);
3219 if (old)
3220 expire_client(old);
3221 return status;
3222 }
3223
nfsd4_alloc_file(void)3224 static struct nfs4_file *nfsd4_alloc_file(void)
3225 {
3226 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3227 }
3228
3229 /* OPEN Share state helper functions */
nfsd4_init_file(struct knfsd_fh * fh,unsigned int hashval,struct nfs4_file * fp)3230 static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3231 struct nfs4_file *fp)
3232 {
3233 lockdep_assert_held(&state_lock);
3234
3235 atomic_set(&fp->fi_ref, 1);
3236 spin_lock_init(&fp->fi_lock);
3237 INIT_LIST_HEAD(&fp->fi_stateids);
3238 INIT_LIST_HEAD(&fp->fi_delegations);
3239 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3240 fh_copy_shallow(&fp->fi_fhandle, fh);
3241 fp->fi_deleg_file = NULL;
3242 fp->fi_had_conflict = false;
3243 fp->fi_share_deny = 0;
3244 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3245 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3246 #ifdef CONFIG_NFSD_PNFS
3247 INIT_LIST_HEAD(&fp->fi_lo_states);
3248 atomic_set(&fp->fi_lo_recalls, 0);
3249 #endif
3250 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
3251 }
3252
3253 void
nfsd4_free_slabs(void)3254 nfsd4_free_slabs(void)
3255 {
3256 kmem_cache_destroy(odstate_slab);
3257 kmem_cache_destroy(openowner_slab);
3258 kmem_cache_destroy(lockowner_slab);
3259 kmem_cache_destroy(file_slab);
3260 kmem_cache_destroy(stateid_slab);
3261 kmem_cache_destroy(deleg_slab);
3262 }
3263
3264 int
nfsd4_init_slabs(void)3265 nfsd4_init_slabs(void)
3266 {
3267 openowner_slab = kmem_cache_create("nfsd4_openowners",
3268 sizeof(struct nfs4_openowner), 0, 0, NULL);
3269 if (openowner_slab == NULL)
3270 goto out;
3271 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3272 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3273 if (lockowner_slab == NULL)
3274 goto out_free_openowner_slab;
3275 file_slab = kmem_cache_create("nfsd4_files",
3276 sizeof(struct nfs4_file), 0, 0, NULL);
3277 if (file_slab == NULL)
3278 goto out_free_lockowner_slab;
3279 stateid_slab = kmem_cache_create("nfsd4_stateids",
3280 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3281 if (stateid_slab == NULL)
3282 goto out_free_file_slab;
3283 deleg_slab = kmem_cache_create("nfsd4_delegations",
3284 sizeof(struct nfs4_delegation), 0, 0, NULL);
3285 if (deleg_slab == NULL)
3286 goto out_free_stateid_slab;
3287 odstate_slab = kmem_cache_create("nfsd4_odstate",
3288 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3289 if (odstate_slab == NULL)
3290 goto out_free_deleg_slab;
3291 return 0;
3292
3293 out_free_deleg_slab:
3294 kmem_cache_destroy(deleg_slab);
3295 out_free_stateid_slab:
3296 kmem_cache_destroy(stateid_slab);
3297 out_free_file_slab:
3298 kmem_cache_destroy(file_slab);
3299 out_free_lockowner_slab:
3300 kmem_cache_destroy(lockowner_slab);
3301 out_free_openowner_slab:
3302 kmem_cache_destroy(openowner_slab);
3303 out:
3304 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3305 return -ENOMEM;
3306 }
3307
init_nfs4_replay(struct nfs4_replay * rp)3308 static void init_nfs4_replay(struct nfs4_replay *rp)
3309 {
3310 rp->rp_status = nfserr_serverfault;
3311 rp->rp_buflen = 0;
3312 rp->rp_buf = rp->rp_ibuf;
3313 mutex_init(&rp->rp_mutex);
3314 }
3315
nfsd4_cstate_assign_replay(struct nfsd4_compound_state * cstate,struct nfs4_stateowner * so)3316 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3317 struct nfs4_stateowner *so)
3318 {
3319 if (!nfsd4_has_session(cstate)) {
3320 mutex_lock(&so->so_replay.rp_mutex);
3321 cstate->replay_owner = nfs4_get_stateowner(so);
3322 }
3323 }
3324
nfsd4_cstate_clear_replay(struct nfsd4_compound_state * cstate)3325 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3326 {
3327 struct nfs4_stateowner *so = cstate->replay_owner;
3328
3329 if (so != NULL) {
3330 cstate->replay_owner = NULL;
3331 mutex_unlock(&so->so_replay.rp_mutex);
3332 nfs4_put_stateowner(so);
3333 }
3334 }
3335
alloc_stateowner(struct kmem_cache * slab,struct xdr_netobj * owner,struct nfs4_client * clp)3336 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3337 {
3338 struct nfs4_stateowner *sop;
3339
3340 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3341 if (!sop)
3342 return NULL;
3343
3344 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3345 if (!sop->so_owner.data) {
3346 kmem_cache_free(slab, sop);
3347 return NULL;
3348 }
3349 sop->so_owner.len = owner->len;
3350
3351 INIT_LIST_HEAD(&sop->so_stateids);
3352 sop->so_client = clp;
3353 init_nfs4_replay(&sop->so_replay);
3354 atomic_set(&sop->so_count, 1);
3355 return sop;
3356 }
3357
hash_openowner(struct nfs4_openowner * oo,struct nfs4_client * clp,unsigned int strhashval)3358 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3359 {
3360 lockdep_assert_held(&clp->cl_lock);
3361
3362 list_add(&oo->oo_owner.so_strhash,
3363 &clp->cl_ownerstr_hashtbl[strhashval]);
3364 list_add(&oo->oo_perclient, &clp->cl_openowners);
3365 }
3366
nfs4_unhash_openowner(struct nfs4_stateowner * so)3367 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3368 {
3369 unhash_openowner_locked(openowner(so));
3370 }
3371
nfs4_free_openowner(struct nfs4_stateowner * so)3372 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3373 {
3374 struct nfs4_openowner *oo = openowner(so);
3375
3376 kmem_cache_free(openowner_slab, oo);
3377 }
3378
3379 static const struct nfs4_stateowner_operations openowner_ops = {
3380 .so_unhash = nfs4_unhash_openowner,
3381 .so_free = nfs4_free_openowner,
3382 };
3383
3384 static struct nfs4_ol_stateid *
nfsd4_find_existing_open(struct nfs4_file * fp,struct nfsd4_open * open)3385 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3386 {
3387 struct nfs4_ol_stateid *local, *ret = NULL;
3388 struct nfs4_openowner *oo = open->op_openowner;
3389
3390 lockdep_assert_held(&fp->fi_lock);
3391
3392 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3393 /* ignore lock owners */
3394 if (local->st_stateowner->so_is_open_owner == 0)
3395 continue;
3396 if (local->st_stateowner != &oo->oo_owner)
3397 continue;
3398 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
3399 ret = local;
3400 atomic_inc(&ret->st_stid.sc_count);
3401 break;
3402 }
3403 }
3404 return ret;
3405 }
3406
3407 static __be32
nfsd4_verify_open_stid(struct nfs4_stid * s)3408 nfsd4_verify_open_stid(struct nfs4_stid *s)
3409 {
3410 __be32 ret = nfs_ok;
3411
3412 switch (s->sc_type) {
3413 default:
3414 break;
3415 case NFS4_CLOSED_STID:
3416 case NFS4_CLOSED_DELEG_STID:
3417 ret = nfserr_bad_stateid;
3418 break;
3419 case NFS4_REVOKED_DELEG_STID:
3420 ret = nfserr_deleg_revoked;
3421 }
3422 return ret;
3423 }
3424
3425 /* Lock the stateid st_mutex, and deal with races with CLOSE */
3426 static __be32
nfsd4_lock_ol_stateid(struct nfs4_ol_stateid * stp)3427 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
3428 {
3429 __be32 ret;
3430
3431 mutex_lock(&stp->st_mutex);
3432 ret = nfsd4_verify_open_stid(&stp->st_stid);
3433 if (ret != nfs_ok)
3434 mutex_unlock(&stp->st_mutex);
3435 return ret;
3436 }
3437
3438 static struct nfs4_ol_stateid *
nfsd4_find_and_lock_existing_open(struct nfs4_file * fp,struct nfsd4_open * open)3439 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3440 {
3441 struct nfs4_ol_stateid *stp;
3442 for (;;) {
3443 spin_lock(&fp->fi_lock);
3444 stp = nfsd4_find_existing_open(fp, open);
3445 spin_unlock(&fp->fi_lock);
3446 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
3447 break;
3448 nfs4_put_stid(&stp->st_stid);
3449 }
3450 return stp;
3451 }
3452
3453 static struct nfs4_openowner *
alloc_init_open_stateowner(unsigned int strhashval,struct nfsd4_open * open,struct nfsd4_compound_state * cstate)3454 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3455 struct nfsd4_compound_state *cstate)
3456 {
3457 struct nfs4_client *clp = cstate->clp;
3458 struct nfs4_openowner *oo, *ret;
3459
3460 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3461 if (!oo)
3462 return NULL;
3463 oo->oo_owner.so_ops = &openowner_ops;
3464 oo->oo_owner.so_is_open_owner = 1;
3465 oo->oo_owner.so_seqid = open->op_seqid;
3466 oo->oo_flags = 0;
3467 if (nfsd4_has_session(cstate))
3468 oo->oo_flags |= NFS4_OO_CONFIRMED;
3469 oo->oo_time = 0;
3470 oo->oo_last_closed_stid = NULL;
3471 INIT_LIST_HEAD(&oo->oo_close_lru);
3472 spin_lock(&clp->cl_lock);
3473 ret = find_openstateowner_str_locked(strhashval, open, clp);
3474 if (ret == NULL) {
3475 hash_openowner(oo, clp, strhashval);
3476 ret = oo;
3477 } else
3478 nfs4_free_stateowner(&oo->oo_owner);
3479
3480 spin_unlock(&clp->cl_lock);
3481 return ret;
3482 }
3483
3484 static struct nfs4_ol_stateid *
init_open_stateid(struct nfs4_file * fp,struct nfsd4_open * open)3485 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
3486 {
3487
3488 struct nfs4_openowner *oo = open->op_openowner;
3489 struct nfs4_ol_stateid *retstp = NULL;
3490 struct nfs4_ol_stateid *stp;
3491
3492 stp = open->op_stp;
3493 /* We are moving these outside of the spinlocks to avoid the warnings */
3494 mutex_init(&stp->st_mutex);
3495 mutex_lock(&stp->st_mutex);
3496
3497 retry:
3498 spin_lock(&oo->oo_owner.so_client->cl_lock);
3499 spin_lock(&fp->fi_lock);
3500
3501 retstp = nfsd4_find_existing_open(fp, open);
3502 if (retstp)
3503 goto out_unlock;
3504
3505 open->op_stp = NULL;
3506 atomic_inc(&stp->st_stid.sc_count);
3507 stp->st_stid.sc_type = NFS4_OPEN_STID;
3508 INIT_LIST_HEAD(&stp->st_locks);
3509 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3510 get_nfs4_file(fp);
3511 stp->st_stid.sc_file = fp;
3512 stp->st_access_bmap = 0;
3513 stp->st_deny_bmap = 0;
3514 stp->st_openstp = NULL;
3515 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3516 list_add(&stp->st_perfile, &fp->fi_stateids);
3517
3518 out_unlock:
3519 spin_unlock(&fp->fi_lock);
3520 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3521 if (retstp) {
3522 /* Handle races with CLOSE */
3523 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
3524 nfs4_put_stid(&retstp->st_stid);
3525 goto retry;
3526 }
3527 /* To keep mutex tracking happy */
3528 mutex_unlock(&stp->st_mutex);
3529 stp = retstp;
3530 }
3531 return stp;
3532 }
3533
3534 /*
3535 * In the 4.0 case we need to keep the owners around a little while to handle
3536 * CLOSE replay. We still do need to release any file access that is held by
3537 * them before returning however.
3538 */
3539 static void
move_to_close_lru(struct nfs4_ol_stateid * s,struct net * net)3540 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3541 {
3542 struct nfs4_ol_stateid *last;
3543 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3544 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3545 nfsd_net_id);
3546
3547 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3548
3549 /*
3550 * We know that we hold one reference via nfsd4_close, and another
3551 * "persistent" reference for the client. If the refcount is higher
3552 * than 2, then there are still calls in progress that are using this
3553 * stateid. We can't put the sc_file reference until they are finished.
3554 * Wait for the refcount to drop to 2. Since it has been unhashed,
3555 * there should be no danger of the refcount going back up again at
3556 * this point.
3557 */
3558 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3559
3560 release_all_access(s);
3561 if (s->st_stid.sc_file) {
3562 put_nfs4_file(s->st_stid.sc_file);
3563 s->st_stid.sc_file = NULL;
3564 }
3565
3566 spin_lock(&nn->client_lock);
3567 last = oo->oo_last_closed_stid;
3568 oo->oo_last_closed_stid = s;
3569 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3570 oo->oo_time = get_seconds();
3571 spin_unlock(&nn->client_lock);
3572 if (last)
3573 nfs4_put_stid(&last->st_stid);
3574 }
3575
3576 /* search file_hashtbl[] for file */
3577 static struct nfs4_file *
find_file_locked(struct knfsd_fh * fh,unsigned int hashval)3578 find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
3579 {
3580 struct nfs4_file *fp;
3581
3582 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
3583 if (fh_match(&fp->fi_fhandle, fh)) {
3584 if (atomic_inc_not_zero(&fp->fi_ref))
3585 return fp;
3586 }
3587 }
3588 return NULL;
3589 }
3590
3591 struct nfs4_file *
find_file(struct knfsd_fh * fh)3592 find_file(struct knfsd_fh *fh)
3593 {
3594 struct nfs4_file *fp;
3595 unsigned int hashval = file_hashval(fh);
3596
3597 rcu_read_lock();
3598 fp = find_file_locked(fh, hashval);
3599 rcu_read_unlock();
3600 return fp;
3601 }
3602
3603 static struct nfs4_file *
find_or_add_file(struct nfs4_file * new,struct knfsd_fh * fh)3604 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3605 {
3606 struct nfs4_file *fp;
3607 unsigned int hashval = file_hashval(fh);
3608
3609 rcu_read_lock();
3610 fp = find_file_locked(fh, hashval);
3611 rcu_read_unlock();
3612 if (fp)
3613 return fp;
3614
3615 spin_lock(&state_lock);
3616 fp = find_file_locked(fh, hashval);
3617 if (likely(fp == NULL)) {
3618 nfsd4_init_file(fh, hashval, new);
3619 fp = new;
3620 }
3621 spin_unlock(&state_lock);
3622
3623 return fp;
3624 }
3625
3626 /*
3627 * Called to check deny when READ with all zero stateid or
3628 * WRITE with all zero or all one stateid
3629 */
3630 static __be32
nfs4_share_conflict(struct svc_fh * current_fh,unsigned int deny_type)3631 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3632 {
3633 struct nfs4_file *fp;
3634 __be32 ret = nfs_ok;
3635
3636 fp = find_file(¤t_fh->fh_handle);
3637 if (!fp)
3638 return ret;
3639 /* Check for conflicting share reservations */
3640 spin_lock(&fp->fi_lock);
3641 if (fp->fi_share_deny & deny_type)
3642 ret = nfserr_locked;
3643 spin_unlock(&fp->fi_lock);
3644 put_nfs4_file(fp);
3645 return ret;
3646 }
3647
nfsd4_cb_recall_prepare(struct nfsd4_callback * cb)3648 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3649 {
3650 struct nfs4_delegation *dp = cb_to_delegation(cb);
3651 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3652 nfsd_net_id);
3653
3654 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3655
3656 /*
3657 * We can't do this in nfsd_break_deleg_cb because it is
3658 * already holding inode->i_lock.
3659 *
3660 * If the dl_time != 0, then we know that it has already been
3661 * queued for a lease break. Don't queue it again.
3662 */
3663 spin_lock(&state_lock);
3664 if (delegation_hashed(dp) && dp->dl_time == 0) {
3665 dp->dl_time = get_seconds();
3666 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3667 }
3668 spin_unlock(&state_lock);
3669 }
3670
nfsd4_cb_recall_done(struct nfsd4_callback * cb,struct rpc_task * task)3671 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3672 struct rpc_task *task)
3673 {
3674 struct nfs4_delegation *dp = cb_to_delegation(cb);
3675
3676 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
3677 return 1;
3678
3679 switch (task->tk_status) {
3680 case 0:
3681 return 1;
3682 case -EBADHANDLE:
3683 case -NFS4ERR_BAD_STATEID:
3684 /*
3685 * Race: client probably got cb_recall before open reply
3686 * granting delegation.
3687 */
3688 if (dp->dl_retries--) {
3689 rpc_delay(task, 2 * HZ);
3690 return 0;
3691 }
3692 /*FALLTHRU*/
3693 default:
3694 return -1;
3695 }
3696 }
3697
nfsd4_cb_recall_release(struct nfsd4_callback * cb)3698 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3699 {
3700 struct nfs4_delegation *dp = cb_to_delegation(cb);
3701
3702 nfs4_put_stid(&dp->dl_stid);
3703 }
3704
3705 static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3706 .prepare = nfsd4_cb_recall_prepare,
3707 .done = nfsd4_cb_recall_done,
3708 .release = nfsd4_cb_recall_release,
3709 };
3710
nfsd_break_one_deleg(struct nfs4_delegation * dp)3711 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3712 {
3713 /*
3714 * We're assuming the state code never drops its reference
3715 * without first removing the lease. Since we're in this lease
3716 * callback (and since the lease code is serialized by the kernel
3717 * lock) we know the server hasn't removed the lease yet, we know
3718 * it's safe to take a reference.
3719 */
3720 atomic_inc(&dp->dl_stid.sc_count);
3721 nfsd4_run_cb(&dp->dl_recall);
3722 }
3723
3724 /* Called from break_lease() with i_lock held. */
3725 static bool
nfsd_break_deleg_cb(struct file_lock * fl)3726 nfsd_break_deleg_cb(struct file_lock *fl)
3727 {
3728 bool ret = false;
3729 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3730 struct nfs4_delegation *dp;
3731
3732 if (!fp) {
3733 WARN(1, "(%p)->fl_owner NULL\n", fl);
3734 return ret;
3735 }
3736 if (fp->fi_had_conflict) {
3737 WARN(1, "duplicate break on %p\n", fp);
3738 return ret;
3739 }
3740 /*
3741 * We don't want the locks code to timeout the lease for us;
3742 * we'll remove it ourself if a delegation isn't returned
3743 * in time:
3744 */
3745 fl->fl_break_time = 0;
3746
3747 spin_lock(&fp->fi_lock);
3748 fp->fi_had_conflict = true;
3749 /*
3750 * If there are no delegations on the list, then return true
3751 * so that the lease code will go ahead and delete it.
3752 */
3753 if (list_empty(&fp->fi_delegations))
3754 ret = true;
3755 else
3756 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3757 nfsd_break_one_deleg(dp);
3758 spin_unlock(&fp->fi_lock);
3759 return ret;
3760 }
3761
3762 static int
nfsd_change_deleg_cb(struct file_lock * onlist,int arg,struct list_head * dispose)3763 nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
3764 struct list_head *dispose)
3765 {
3766 if (arg & F_UNLCK)
3767 return lease_modify(onlist, arg, dispose);
3768 else
3769 return -EAGAIN;
3770 }
3771
3772 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3773 .lm_break = nfsd_break_deleg_cb,
3774 .lm_change = nfsd_change_deleg_cb,
3775 };
3776
nfsd4_check_seqid(struct nfsd4_compound_state * cstate,struct nfs4_stateowner * so,u32 seqid)3777 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3778 {
3779 if (nfsd4_has_session(cstate))
3780 return nfs_ok;
3781 if (seqid == so->so_seqid - 1)
3782 return nfserr_replay_me;
3783 if (seqid == so->so_seqid)
3784 return nfs_ok;
3785 return nfserr_bad_seqid;
3786 }
3787
lookup_clientid(clientid_t * clid,struct nfsd4_compound_state * cstate,struct nfsd_net * nn)3788 static __be32 lookup_clientid(clientid_t *clid,
3789 struct nfsd4_compound_state *cstate,
3790 struct nfsd_net *nn)
3791 {
3792 struct nfs4_client *found;
3793
3794 if (cstate->clp) {
3795 found = cstate->clp;
3796 if (!same_clid(&found->cl_clientid, clid))
3797 return nfserr_stale_clientid;
3798 return nfs_ok;
3799 }
3800
3801 if (STALE_CLIENTID(clid, nn))
3802 return nfserr_stale_clientid;
3803
3804 /*
3805 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3806 * cached already then we know this is for is for v4.0 and "sessions"
3807 * will be false.
3808 */
3809 WARN_ON_ONCE(cstate->session);
3810 spin_lock(&nn->client_lock);
3811 found = find_confirmed_client(clid, false, nn);
3812 if (!found) {
3813 spin_unlock(&nn->client_lock);
3814 return nfserr_expired;
3815 }
3816 atomic_inc(&found->cl_refcount);
3817 spin_unlock(&nn->client_lock);
3818
3819 /* Cache the nfs4_client in cstate! */
3820 cstate->clp = found;
3821 return nfs_ok;
3822 }
3823
3824 __be32
nfsd4_process_open1(struct nfsd4_compound_state * cstate,struct nfsd4_open * open,struct nfsd_net * nn)3825 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3826 struct nfsd4_open *open, struct nfsd_net *nn)
3827 {
3828 clientid_t *clientid = &open->op_clientid;
3829 struct nfs4_client *clp = NULL;
3830 unsigned int strhashval;
3831 struct nfs4_openowner *oo = NULL;
3832 __be32 status;
3833
3834 if (STALE_CLIENTID(&open->op_clientid, nn))
3835 return nfserr_stale_clientid;
3836 /*
3837 * In case we need it later, after we've already created the
3838 * file and don't want to risk a further failure:
3839 */
3840 open->op_file = nfsd4_alloc_file();
3841 if (open->op_file == NULL)
3842 return nfserr_jukebox;
3843
3844 status = lookup_clientid(clientid, cstate, nn);
3845 if (status)
3846 return status;
3847 clp = cstate->clp;
3848
3849 strhashval = ownerstr_hashval(&open->op_owner);
3850 oo = find_openstateowner_str(strhashval, open, clp);
3851 open->op_openowner = oo;
3852 if (!oo) {
3853 goto new_owner;
3854 }
3855 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3856 /* Replace unconfirmed owners without checking for replay. */
3857 release_openowner(oo);
3858 open->op_openowner = NULL;
3859 goto new_owner;
3860 }
3861 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3862 if (status)
3863 return status;
3864 goto alloc_stateid;
3865 new_owner:
3866 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3867 if (oo == NULL)
3868 return nfserr_jukebox;
3869 open->op_openowner = oo;
3870 alloc_stateid:
3871 open->op_stp = nfs4_alloc_open_stateid(clp);
3872 if (!open->op_stp)
3873 return nfserr_jukebox;
3874
3875 if (nfsd4_has_session(cstate) &&
3876 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
3877 open->op_odstate = alloc_clnt_odstate(clp);
3878 if (!open->op_odstate)
3879 return nfserr_jukebox;
3880 }
3881
3882 return nfs_ok;
3883 }
3884
3885 static inline __be32
nfs4_check_delegmode(struct nfs4_delegation * dp,int flags)3886 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3887 {
3888 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3889 return nfserr_openmode;
3890 else
3891 return nfs_ok;
3892 }
3893
share_access_to_flags(u32 share_access)3894 static int share_access_to_flags(u32 share_access)
3895 {
3896 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3897 }
3898
find_deleg_stateid(struct nfs4_client * cl,stateid_t * s)3899 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3900 {
3901 struct nfs4_stid *ret;
3902
3903 ret = find_stateid_by_type(cl, s,
3904 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
3905 if (!ret)
3906 return NULL;
3907 return delegstateid(ret);
3908 }
3909
nfsd4_is_deleg_cur(struct nfsd4_open * open)3910 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3911 {
3912 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3913 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3914 }
3915
3916 static __be32
nfs4_check_deleg(struct nfs4_client * cl,struct nfsd4_open * open,struct nfs4_delegation ** dp)3917 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3918 struct nfs4_delegation **dp)
3919 {
3920 int flags;
3921 __be32 status = nfserr_bad_stateid;
3922 struct nfs4_delegation *deleg;
3923
3924 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3925 if (deleg == NULL)
3926 goto out;
3927 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
3928 nfs4_put_stid(&deleg->dl_stid);
3929 if (cl->cl_minorversion)
3930 status = nfserr_deleg_revoked;
3931 goto out;
3932 }
3933 flags = share_access_to_flags(open->op_share_access);
3934 status = nfs4_check_delegmode(deleg, flags);
3935 if (status) {
3936 nfs4_put_stid(&deleg->dl_stid);
3937 goto out;
3938 }
3939 *dp = deleg;
3940 out:
3941 if (!nfsd4_is_deleg_cur(open))
3942 return nfs_ok;
3943 if (status)
3944 return status;
3945 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3946 return nfs_ok;
3947 }
3948
nfs4_access_to_access(u32 nfs4_access)3949 static inline int nfs4_access_to_access(u32 nfs4_access)
3950 {
3951 int flags = 0;
3952
3953 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3954 flags |= NFSD_MAY_READ;
3955 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3956 flags |= NFSD_MAY_WRITE;
3957 return flags;
3958 }
3959
3960 static inline __be32
nfsd4_truncate(struct svc_rqst * rqstp,struct svc_fh * fh,struct nfsd4_open * open)3961 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3962 struct nfsd4_open *open)
3963 {
3964 struct iattr iattr = {
3965 .ia_valid = ATTR_SIZE,
3966 .ia_size = 0,
3967 };
3968 if (!open->op_truncate)
3969 return 0;
3970 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3971 return nfserr_inval;
3972 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3973 }
3974
nfs4_get_vfs_file(struct svc_rqst * rqstp,struct nfs4_file * fp,struct svc_fh * cur_fh,struct nfs4_ol_stateid * stp,struct nfsd4_open * open)3975 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3976 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3977 struct nfsd4_open *open)
3978 {
3979 struct file *filp = NULL;
3980 __be32 status;
3981 int oflag = nfs4_access_to_omode(open->op_share_access);
3982 int access = nfs4_access_to_access(open->op_share_access);
3983 unsigned char old_access_bmap, old_deny_bmap;
3984
3985 spin_lock(&fp->fi_lock);
3986
3987 /*
3988 * Are we trying to set a deny mode that would conflict with
3989 * current access?
3990 */
3991 status = nfs4_file_check_deny(fp, open->op_share_deny);
3992 if (status != nfs_ok) {
3993 spin_unlock(&fp->fi_lock);
3994 goto out;
3995 }
3996
3997 /* set access to the file */
3998 status = nfs4_file_get_access(fp, open->op_share_access);
3999 if (status != nfs_ok) {
4000 spin_unlock(&fp->fi_lock);
4001 goto out;
4002 }
4003
4004 /* Set access bits in stateid */
4005 old_access_bmap = stp->st_access_bmap;
4006 set_access(open->op_share_access, stp);
4007
4008 /* Set new deny mask */
4009 old_deny_bmap = stp->st_deny_bmap;
4010 set_deny(open->op_share_deny, stp);
4011 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4012
4013 if (!fp->fi_fds[oflag]) {
4014 spin_unlock(&fp->fi_lock);
4015 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
4016 if (status)
4017 goto out_put_access;
4018 spin_lock(&fp->fi_lock);
4019 if (!fp->fi_fds[oflag]) {
4020 fp->fi_fds[oflag] = filp;
4021 filp = NULL;
4022 }
4023 }
4024 spin_unlock(&fp->fi_lock);
4025 if (filp)
4026 fput(filp);
4027
4028 status = nfsd4_truncate(rqstp, cur_fh, open);
4029 if (status)
4030 goto out_put_access;
4031 out:
4032 return status;
4033 out_put_access:
4034 stp->st_access_bmap = old_access_bmap;
4035 nfs4_file_put_access(fp, open->op_share_access);
4036 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4037 goto out;
4038 }
4039
4040 static __be32
nfs4_upgrade_open(struct svc_rqst * rqstp,struct nfs4_file * fp,struct svc_fh * cur_fh,struct nfs4_ol_stateid * stp,struct nfsd4_open * open)4041 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
4042 {
4043 __be32 status;
4044 unsigned char old_deny_bmap = stp->st_deny_bmap;
4045
4046 if (!test_access(open->op_share_access, stp))
4047 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4048
4049 /* test and set deny mode */
4050 spin_lock(&fp->fi_lock);
4051 status = nfs4_file_check_deny(fp, open->op_share_deny);
4052 if (status == nfs_ok) {
4053 set_deny(open->op_share_deny, stp);
4054 fp->fi_share_deny |=
4055 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4056 }
4057 spin_unlock(&fp->fi_lock);
4058
4059 if (status != nfs_ok)
4060 return status;
4061
4062 status = nfsd4_truncate(rqstp, cur_fh, open);
4063 if (status != nfs_ok)
4064 reset_union_bmap_deny(old_deny_bmap, stp);
4065 return status;
4066 }
4067
4068 /* Should we give out recallable state?: */
nfsd4_cb_channel_good(struct nfs4_client * clp)4069 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4070 {
4071 if (clp->cl_cb_state == NFSD4_CB_UP)
4072 return true;
4073 /*
4074 * In the sessions case, since we don't have to establish a
4075 * separate connection for callbacks, we assume it's OK
4076 * until we hear otherwise:
4077 */
4078 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4079 }
4080
nfs4_alloc_init_lease(struct nfs4_file * fp,int flag)4081 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
4082 {
4083 struct file_lock *fl;
4084
4085 fl = locks_alloc_lock();
4086 if (!fl)
4087 return NULL;
4088 fl->fl_lmops = &nfsd_lease_mng_ops;
4089 fl->fl_flags = FL_DELEG;
4090 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4091 fl->fl_end = OFFSET_MAX;
4092 fl->fl_owner = (fl_owner_t)fp;
4093 fl->fl_pid = current->tgid;
4094 return fl;
4095 }
4096
4097 /**
4098 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4099 * @dp: a pointer to the nfs4_delegation we're adding.
4100 *
4101 * Return:
4102 * On success: Return code will be 0 on success.
4103 *
4104 * On error: -EAGAIN if there was an existing delegation.
4105 * nonzero if there is an error in other cases.
4106 *
4107 */
4108
nfs4_setlease(struct nfs4_delegation * dp)4109 static int nfs4_setlease(struct nfs4_delegation *dp)
4110 {
4111 struct nfs4_file *fp = dp->dl_stid.sc_file;
4112 struct file_lock *fl;
4113 struct file *filp;
4114 int status = 0;
4115
4116 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
4117 if (!fl)
4118 return -ENOMEM;
4119 filp = find_readable_file(fp);
4120 if (!filp) {
4121 /* We should always have a readable file here */
4122 WARN_ON_ONCE(1);
4123 locks_free_lock(fl);
4124 return -EBADF;
4125 }
4126 fl->fl_file = filp;
4127 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
4128 if (fl)
4129 locks_free_lock(fl);
4130 if (status)
4131 goto out_fput;
4132 spin_lock(&state_lock);
4133 spin_lock(&fp->fi_lock);
4134 /* Did the lease get broken before we took the lock? */
4135 status = -EAGAIN;
4136 if (fp->fi_had_conflict)
4137 goto out_unlock;
4138 /* Race breaker */
4139 if (fp->fi_deleg_file) {
4140 status = hash_delegation_locked(dp, fp);
4141 goto out_unlock;
4142 }
4143 fp->fi_deleg_file = filp;
4144 fp->fi_delegees = 0;
4145 status = hash_delegation_locked(dp, fp);
4146 spin_unlock(&fp->fi_lock);
4147 spin_unlock(&state_lock);
4148 if (status) {
4149 /* Should never happen, this is a new fi_deleg_file */
4150 WARN_ON_ONCE(1);
4151 goto out_fput;
4152 }
4153 return 0;
4154 out_unlock:
4155 spin_unlock(&fp->fi_lock);
4156 spin_unlock(&state_lock);
4157 out_fput:
4158 fput(filp);
4159 return status;
4160 }
4161
4162 static struct nfs4_delegation *
nfs4_set_delegation(struct nfs4_client * clp,struct svc_fh * fh,struct nfs4_file * fp,struct nfs4_clnt_odstate * odstate)4163 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4164 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4165 {
4166 int status;
4167 struct nfs4_delegation *dp;
4168
4169 if (fp->fi_had_conflict)
4170 return ERR_PTR(-EAGAIN);
4171
4172 spin_lock(&state_lock);
4173 spin_lock(&fp->fi_lock);
4174 status = nfs4_get_existing_delegation(clp, fp);
4175 spin_unlock(&fp->fi_lock);
4176 spin_unlock(&state_lock);
4177
4178 if (status)
4179 return ERR_PTR(status);
4180
4181 dp = alloc_init_deleg(clp, fh, odstate);
4182 if (!dp)
4183 return ERR_PTR(-ENOMEM);
4184
4185 get_nfs4_file(fp);
4186 spin_lock(&state_lock);
4187 spin_lock(&fp->fi_lock);
4188 dp->dl_stid.sc_file = fp;
4189 if (!fp->fi_deleg_file) {
4190 spin_unlock(&fp->fi_lock);
4191 spin_unlock(&state_lock);
4192 status = nfs4_setlease(dp);
4193 goto out;
4194 }
4195 if (fp->fi_had_conflict) {
4196 status = -EAGAIN;
4197 goto out_unlock;
4198 }
4199 status = hash_delegation_locked(dp, fp);
4200 out_unlock:
4201 spin_unlock(&fp->fi_lock);
4202 spin_unlock(&state_lock);
4203 out:
4204 if (status) {
4205 put_clnt_odstate(dp->dl_clnt_odstate);
4206 nfs4_put_stid(&dp->dl_stid);
4207 return ERR_PTR(status);
4208 }
4209 return dp;
4210 }
4211
nfsd4_open_deleg_none_ext(struct nfsd4_open * open,int status)4212 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4213 {
4214 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4215 if (status == -EAGAIN)
4216 open->op_why_no_deleg = WND4_CONTENTION;
4217 else {
4218 open->op_why_no_deleg = WND4_RESOURCE;
4219 switch (open->op_deleg_want) {
4220 case NFS4_SHARE_WANT_READ_DELEG:
4221 case NFS4_SHARE_WANT_WRITE_DELEG:
4222 case NFS4_SHARE_WANT_ANY_DELEG:
4223 break;
4224 case NFS4_SHARE_WANT_CANCEL:
4225 open->op_why_no_deleg = WND4_CANCELLED;
4226 break;
4227 case NFS4_SHARE_WANT_NO_DELEG:
4228 WARN_ON_ONCE(1);
4229 }
4230 }
4231 }
4232
4233 /*
4234 * Attempt to hand out a delegation.
4235 *
4236 * Note we don't support write delegations, and won't until the vfs has
4237 * proper support for them.
4238 */
4239 static void
nfs4_open_delegation(struct svc_fh * fh,struct nfsd4_open * open,struct nfs4_ol_stateid * stp)4240 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4241 struct nfs4_ol_stateid *stp)
4242 {
4243 struct nfs4_delegation *dp;
4244 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4245 struct nfs4_client *clp = stp->st_stid.sc_client;
4246 int cb_up;
4247 int status = 0;
4248
4249 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
4250 open->op_recall = 0;
4251 switch (open->op_claim_type) {
4252 case NFS4_OPEN_CLAIM_PREVIOUS:
4253 if (!cb_up)
4254 open->op_recall = 1;
4255 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4256 goto out_no_deleg;
4257 break;
4258 case NFS4_OPEN_CLAIM_NULL:
4259 case NFS4_OPEN_CLAIM_FH:
4260 /*
4261 * Let's not give out any delegations till everyone's
4262 * had the chance to reclaim theirs, *and* until
4263 * NLM locks have all been reclaimed:
4264 */
4265 if (locks_in_grace(clp->net))
4266 goto out_no_deleg;
4267 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
4268 goto out_no_deleg;
4269 /*
4270 * Also, if the file was opened for write or
4271 * create, there's a good chance the client's
4272 * about to write to it, resulting in an
4273 * immediate recall (since we don't support
4274 * write delegations):
4275 */
4276 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
4277 goto out_no_deleg;
4278 if (open->op_create == NFS4_OPEN_CREATE)
4279 goto out_no_deleg;
4280 break;
4281 default:
4282 goto out_no_deleg;
4283 }
4284 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
4285 if (IS_ERR(dp))
4286 goto out_no_deleg;
4287
4288 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
4289
4290 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
4291 STATEID_VAL(&dp->dl_stid.sc_stateid));
4292 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
4293 nfs4_put_stid(&dp->dl_stid);
4294 return;
4295 out_no_deleg:
4296 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4297 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
4298 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
4299 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4300 open->op_recall = 1;
4301 }
4302
4303 /* 4.1 client asking for a delegation? */
4304 if (open->op_deleg_want)
4305 nfsd4_open_deleg_none_ext(open, status);
4306 return;
4307 }
4308
nfsd4_deleg_xgrade_none_ext(struct nfsd4_open * open,struct nfs4_delegation * dp)4309 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4310 struct nfs4_delegation *dp)
4311 {
4312 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4313 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4314 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4315 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4316 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4317 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4318 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4319 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4320 }
4321 /* Otherwise the client must be confused wanting a delegation
4322 * it already has, therefore we don't return
4323 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4324 */
4325 }
4326
4327 __be32
nfsd4_process_open2(struct svc_rqst * rqstp,struct svc_fh * current_fh,struct nfsd4_open * open)4328 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4329 {
4330 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4331 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4332 struct nfs4_file *fp = NULL;
4333 struct nfs4_ol_stateid *stp = NULL;
4334 struct nfs4_delegation *dp = NULL;
4335 __be32 status;
4336 bool new_stp = false;
4337
4338 /*
4339 * Lookup file; if found, lookup stateid and check open request,
4340 * and check for delegations in the process of being recalled.
4341 * If not found, create the nfs4_file struct
4342 */
4343 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4344 if (fp != open->op_file) {
4345 status = nfs4_check_deleg(cl, open, &dp);
4346 if (status)
4347 goto out;
4348 stp = nfsd4_find_and_lock_existing_open(fp, open);
4349 } else {
4350 open->op_file = NULL;
4351 status = nfserr_bad_stateid;
4352 if (nfsd4_is_deleg_cur(open))
4353 goto out;
4354 }
4355
4356 if (!stp) {
4357 stp = init_open_stateid(fp, open);
4358 if (!open->op_stp)
4359 new_stp = true;
4360 }
4361
4362 /*
4363 * OPEN the file, or upgrade an existing OPEN.
4364 * If truncate fails, the OPEN fails.
4365 *
4366 * stp is already locked.
4367 */
4368 if (!new_stp) {
4369 /* Stateid was found, this is an OPEN upgrade */
4370 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4371 if (status) {
4372 mutex_unlock(&stp->st_mutex);
4373 goto out;
4374 }
4375 } else {
4376 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4377 if (status) {
4378 stp->st_stid.sc_type = NFS4_CLOSED_STID;
4379 release_open_stateid(stp);
4380 mutex_unlock(&stp->st_mutex);
4381 goto out;
4382 }
4383
4384 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4385 open->op_odstate);
4386 if (stp->st_clnt_odstate == open->op_odstate)
4387 open->op_odstate = NULL;
4388 }
4389
4390 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
4391 mutex_unlock(&stp->st_mutex);
4392
4393 if (nfsd4_has_session(&resp->cstate)) {
4394 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4395 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4396 open->op_why_no_deleg = WND4_NOT_WANTED;
4397 goto nodeleg;
4398 }
4399 }
4400
4401 /*
4402 * Attempt to hand out a delegation. No error return, because the
4403 * OPEN succeeds even if we fail.
4404 */
4405 nfs4_open_delegation(current_fh, open, stp);
4406 nodeleg:
4407 status = nfs_ok;
4408
4409 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4410 STATEID_VAL(&stp->st_stid.sc_stateid));
4411 out:
4412 /* 4.1 client trying to upgrade/downgrade delegation? */
4413 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4414 open->op_deleg_want)
4415 nfsd4_deleg_xgrade_none_ext(open, dp);
4416
4417 if (fp)
4418 put_nfs4_file(fp);
4419 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4420 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4421 /*
4422 * To finish the open response, we just need to set the rflags.
4423 */
4424 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4425 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4426 !nfsd4_has_session(&resp->cstate))
4427 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4428 if (dp)
4429 nfs4_put_stid(&dp->dl_stid);
4430 if (stp)
4431 nfs4_put_stid(&stp->st_stid);
4432
4433 return status;
4434 }
4435
nfsd4_cleanup_open_state(struct nfsd4_compound_state * cstate,struct nfsd4_open * open)4436 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4437 struct nfsd4_open *open)
4438 {
4439 if (open->op_openowner) {
4440 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4441
4442 nfsd4_cstate_assign_replay(cstate, so);
4443 nfs4_put_stateowner(so);
4444 }
4445 if (open->op_file)
4446 kmem_cache_free(file_slab, open->op_file);
4447 if (open->op_stp)
4448 nfs4_put_stid(&open->op_stp->st_stid);
4449 if (open->op_odstate)
4450 kmem_cache_free(odstate_slab, open->op_odstate);
4451 }
4452
4453 __be32
nfsd4_renew(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,clientid_t * clid)4454 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4455 clientid_t *clid)
4456 {
4457 struct nfs4_client *clp;
4458 __be32 status;
4459 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4460
4461 dprintk("process_renew(%08x/%08x): starting\n",
4462 clid->cl_boot, clid->cl_id);
4463 status = lookup_clientid(clid, cstate, nn);
4464 if (status)
4465 goto out;
4466 clp = cstate->clp;
4467 status = nfserr_cb_path_down;
4468 if (!list_empty(&clp->cl_delegations)
4469 && clp->cl_cb_state != NFSD4_CB_UP)
4470 goto out;
4471 status = nfs_ok;
4472 out:
4473 return status;
4474 }
4475
4476 void
nfsd4_end_grace(struct nfsd_net * nn)4477 nfsd4_end_grace(struct nfsd_net *nn)
4478 {
4479 /* do nothing if grace period already ended */
4480 if (nn->grace_ended)
4481 return;
4482
4483 dprintk("NFSD: end of grace period\n");
4484 nn->grace_ended = true;
4485 /*
4486 * If the server goes down again right now, an NFSv4
4487 * client will still be allowed to reclaim after it comes back up,
4488 * even if it hasn't yet had a chance to reclaim state this time.
4489 *
4490 */
4491 nfsd4_record_grace_done(nn);
4492 /*
4493 * At this point, NFSv4 clients can still reclaim. But if the
4494 * server crashes, any that have not yet reclaimed will be out
4495 * of luck on the next boot.
4496 *
4497 * (NFSv4.1+ clients are considered to have reclaimed once they
4498 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4499 * have reclaimed after their first OPEN.)
4500 */
4501 locks_end_grace(&nn->nfsd4_manager);
4502 /*
4503 * At this point, and once lockd and/or any other containers
4504 * exit their grace period, further reclaims will fail and
4505 * regular locking can resume.
4506 */
4507 }
4508
4509 static time_t
nfs4_laundromat(struct nfsd_net * nn)4510 nfs4_laundromat(struct nfsd_net *nn)
4511 {
4512 struct nfs4_client *clp;
4513 struct nfs4_openowner *oo;
4514 struct nfs4_delegation *dp;
4515 struct nfs4_ol_stateid *stp;
4516 struct list_head *pos, *next, reaplist;
4517 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4518 time_t t, new_timeo = nn->nfsd4_lease;
4519
4520 dprintk("NFSD: laundromat service - starting\n");
4521 nfsd4_end_grace(nn);
4522 INIT_LIST_HEAD(&reaplist);
4523 spin_lock(&nn->client_lock);
4524 list_for_each_safe(pos, next, &nn->client_lru) {
4525 clp = list_entry(pos, struct nfs4_client, cl_lru);
4526 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4527 t = clp->cl_time - cutoff;
4528 new_timeo = min(new_timeo, t);
4529 break;
4530 }
4531 if (mark_client_expired_locked(clp)) {
4532 dprintk("NFSD: client in use (clientid %08x)\n",
4533 clp->cl_clientid.cl_id);
4534 continue;
4535 }
4536 list_add(&clp->cl_lru, &reaplist);
4537 }
4538 spin_unlock(&nn->client_lock);
4539 list_for_each_safe(pos, next, &reaplist) {
4540 clp = list_entry(pos, struct nfs4_client, cl_lru);
4541 dprintk("NFSD: purging unused client (clientid %08x)\n",
4542 clp->cl_clientid.cl_id);
4543 list_del_init(&clp->cl_lru);
4544 expire_client(clp);
4545 }
4546 spin_lock(&state_lock);
4547 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4548 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4549 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4550 t = dp->dl_time - cutoff;
4551 new_timeo = min(new_timeo, t);
4552 break;
4553 }
4554 WARN_ON(!unhash_delegation_locked(dp));
4555 list_add(&dp->dl_recall_lru, &reaplist);
4556 }
4557 spin_unlock(&state_lock);
4558 while (!list_empty(&reaplist)) {
4559 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4560 dl_recall_lru);
4561 list_del_init(&dp->dl_recall_lru);
4562 revoke_delegation(dp);
4563 }
4564
4565 spin_lock(&nn->client_lock);
4566 while (!list_empty(&nn->close_lru)) {
4567 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4568 oo_close_lru);
4569 if (time_after((unsigned long)oo->oo_time,
4570 (unsigned long)cutoff)) {
4571 t = oo->oo_time - cutoff;
4572 new_timeo = min(new_timeo, t);
4573 break;
4574 }
4575 list_del_init(&oo->oo_close_lru);
4576 stp = oo->oo_last_closed_stid;
4577 oo->oo_last_closed_stid = NULL;
4578 spin_unlock(&nn->client_lock);
4579 nfs4_put_stid(&stp->st_stid);
4580 spin_lock(&nn->client_lock);
4581 }
4582 spin_unlock(&nn->client_lock);
4583
4584 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4585 return new_timeo;
4586 }
4587
4588 static struct workqueue_struct *laundry_wq;
4589 static void laundromat_main(struct work_struct *);
4590
4591 static void
laundromat_main(struct work_struct * laundry)4592 laundromat_main(struct work_struct *laundry)
4593 {
4594 time_t t;
4595 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4596 work);
4597 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4598 laundromat_work);
4599
4600 t = nfs4_laundromat(nn);
4601 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4602 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4603 }
4604
nfs4_check_fh(struct svc_fh * fhp,struct nfs4_stid * stp)4605 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4606 {
4607 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4608 return nfserr_bad_stateid;
4609 return nfs_ok;
4610 }
4611
4612 static inline int
access_permit_read(struct nfs4_ol_stateid * stp)4613 access_permit_read(struct nfs4_ol_stateid *stp)
4614 {
4615 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4616 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4617 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4618 }
4619
4620 static inline int
access_permit_write(struct nfs4_ol_stateid * stp)4621 access_permit_write(struct nfs4_ol_stateid *stp)
4622 {
4623 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4624 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4625 }
4626
4627 static
nfs4_check_openmode(struct nfs4_ol_stateid * stp,int flags)4628 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4629 {
4630 __be32 status = nfserr_openmode;
4631
4632 /* For lock stateid's, we test the parent open, not the lock: */
4633 if (stp->st_openstp)
4634 stp = stp->st_openstp;
4635 if ((flags & WR_STATE) && !access_permit_write(stp))
4636 goto out;
4637 if ((flags & RD_STATE) && !access_permit_read(stp))
4638 goto out;
4639 status = nfs_ok;
4640 out:
4641 return status;
4642 }
4643
4644 static inline __be32
check_special_stateids(struct net * net,svc_fh * current_fh,stateid_t * stateid,int flags)4645 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4646 {
4647 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4648 return nfs_ok;
4649 else if (opens_in_grace(net)) {
4650 /* Answer in remaining cases depends on existence of
4651 * conflicting state; so we must wait out the grace period. */
4652 return nfserr_grace;
4653 } else if (flags & WR_STATE)
4654 return nfs4_share_conflict(current_fh,
4655 NFS4_SHARE_DENY_WRITE);
4656 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4657 return nfs4_share_conflict(current_fh,
4658 NFS4_SHARE_DENY_READ);
4659 }
4660
4661 /*
4662 * Allow READ/WRITE during grace period on recovered state only for files
4663 * that are not able to provide mandatory locking.
4664 */
4665 static inline int
grace_disallows_io(struct net * net,struct inode * inode)4666 grace_disallows_io(struct net *net, struct inode *inode)
4667 {
4668 return opens_in_grace(net) && mandatory_lock(inode);
4669 }
4670
4671 /* Returns true iff a is later than b: */
stateid_generation_after(stateid_t * a,stateid_t * b)4672 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4673 {
4674 return (s32)(a->si_generation - b->si_generation) > 0;
4675 }
4676
check_stateid_generation(stateid_t * in,stateid_t * ref,bool has_session)4677 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4678 {
4679 /*
4680 * When sessions are used the stateid generation number is ignored
4681 * when it is zero.
4682 */
4683 if (has_session && in->si_generation == 0)
4684 return nfs_ok;
4685
4686 if (in->si_generation == ref->si_generation)
4687 return nfs_ok;
4688
4689 /* If the client sends us a stateid from the future, it's buggy: */
4690 if (stateid_generation_after(in, ref))
4691 return nfserr_bad_stateid;
4692 /*
4693 * However, we could see a stateid from the past, even from a
4694 * non-buggy client. For example, if the client sends a lock
4695 * while some IO is outstanding, the lock may bump si_generation
4696 * while the IO is still in flight. The client could avoid that
4697 * situation by waiting for responses on all the IO requests,
4698 * but better performance may result in retrying IO that
4699 * receives an old_stateid error if requests are rarely
4700 * reordered in flight:
4701 */
4702 return nfserr_old_stateid;
4703 }
4704
nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid * ols)4705 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4706 {
4707 if (ols->st_stateowner->so_is_open_owner &&
4708 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4709 return nfserr_bad_stateid;
4710 return nfs_ok;
4711 }
4712
nfsd4_validate_stateid(struct nfs4_client * cl,stateid_t * stateid)4713 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4714 {
4715 struct nfs4_stid *s;
4716 __be32 status = nfserr_bad_stateid;
4717
4718 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
4719 CLOSE_STATEID(stateid))
4720 return status;
4721 /* Client debugging aid. */
4722 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4723 char addr_str[INET6_ADDRSTRLEN];
4724 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4725 sizeof(addr_str));
4726 pr_warn_ratelimited("NFSD: client %s testing state ID "
4727 "with incorrect client ID\n", addr_str);
4728 return status;
4729 }
4730 spin_lock(&cl->cl_lock);
4731 s = find_stateid_locked(cl, stateid);
4732 if (!s)
4733 goto out_unlock;
4734 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4735 if (status)
4736 goto out_unlock;
4737 switch (s->sc_type) {
4738 case NFS4_DELEG_STID:
4739 status = nfs_ok;
4740 break;
4741 case NFS4_REVOKED_DELEG_STID:
4742 status = nfserr_deleg_revoked;
4743 break;
4744 case NFS4_OPEN_STID:
4745 case NFS4_LOCK_STID:
4746 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
4747 break;
4748 default:
4749 printk("unknown stateid type %x\n", s->sc_type);
4750 /* Fallthrough */
4751 case NFS4_CLOSED_STID:
4752 case NFS4_CLOSED_DELEG_STID:
4753 status = nfserr_bad_stateid;
4754 }
4755 out_unlock:
4756 spin_unlock(&cl->cl_lock);
4757 return status;
4758 }
4759
4760 __be32
nfsd4_lookup_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid,unsigned char typemask,struct nfs4_stid ** s,struct nfsd_net * nn)4761 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4762 stateid_t *stateid, unsigned char typemask,
4763 struct nfs4_stid **s, struct nfsd_net *nn)
4764 {
4765 __be32 status;
4766 bool return_revoked = false;
4767
4768 /*
4769 * only return revoked delegations if explicitly asked.
4770 * otherwise we report revoked or bad_stateid status.
4771 */
4772 if (typemask & NFS4_REVOKED_DELEG_STID)
4773 return_revoked = true;
4774 else if (typemask & NFS4_DELEG_STID)
4775 typemask |= NFS4_REVOKED_DELEG_STID;
4776
4777 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
4778 CLOSE_STATEID(stateid))
4779 return nfserr_bad_stateid;
4780 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4781 if (status == nfserr_stale_clientid) {
4782 if (cstate->session)
4783 return nfserr_bad_stateid;
4784 return nfserr_stale_stateid;
4785 }
4786 if (status)
4787 return status;
4788 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4789 if (!*s)
4790 return nfserr_bad_stateid;
4791 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
4792 nfs4_put_stid(*s);
4793 if (cstate->minorversion)
4794 return nfserr_deleg_revoked;
4795 return nfserr_bad_stateid;
4796 }
4797 return nfs_ok;
4798 }
4799
4800 static struct file *
nfs4_find_file(struct nfs4_stid * s,int flags)4801 nfs4_find_file(struct nfs4_stid *s, int flags)
4802 {
4803 if (!s)
4804 return NULL;
4805
4806 switch (s->sc_type) {
4807 case NFS4_DELEG_STID:
4808 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4809 return NULL;
4810 return get_file(s->sc_file->fi_deleg_file);
4811 case NFS4_OPEN_STID:
4812 case NFS4_LOCK_STID:
4813 if (flags & RD_STATE)
4814 return find_readable_file(s->sc_file);
4815 else
4816 return find_writeable_file(s->sc_file);
4817 break;
4818 }
4819
4820 return NULL;
4821 }
4822
4823 static __be32
nfs4_check_olstateid(struct svc_fh * fhp,struct nfs4_ol_stateid * ols,int flags)4824 nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4825 {
4826 __be32 status;
4827
4828 status = nfsd4_check_openowner_confirmed(ols);
4829 if (status)
4830 return status;
4831 return nfs4_check_openmode(ols, flags);
4832 }
4833
4834 static __be32
nfs4_check_file(struct svc_rqst * rqstp,struct svc_fh * fhp,struct nfs4_stid * s,struct file ** filpp,bool * tmp_file,int flags)4835 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
4836 struct file **filpp, bool *tmp_file, int flags)
4837 {
4838 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
4839 struct file *file;
4840 __be32 status;
4841
4842 file = nfs4_find_file(s, flags);
4843 if (file) {
4844 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
4845 acc | NFSD_MAY_OWNER_OVERRIDE);
4846 if (status) {
4847 fput(file);
4848 return status;
4849 }
4850
4851 *filpp = file;
4852 } else {
4853 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
4854 if (status)
4855 return status;
4856
4857 if (tmp_file)
4858 *tmp_file = true;
4859 }
4860
4861 return 0;
4862 }
4863
4864 /*
4865 * Checks for stateid operations
4866 */
4867 __be32
nfs4_preprocess_stateid_op(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,stateid_t * stateid,int flags,struct file ** filpp,bool * tmp_file)4868 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
4869 struct nfsd4_compound_state *cstate, stateid_t *stateid,
4870 int flags, struct file **filpp, bool *tmp_file)
4871 {
4872 struct svc_fh *fhp = &cstate->current_fh;
4873 struct inode *ino = d_inode(fhp->fh_dentry);
4874 struct net *net = SVC_NET(rqstp);
4875 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4876 struct nfs4_stid *s = NULL;
4877 __be32 status;
4878
4879 if (filpp)
4880 *filpp = NULL;
4881 if (tmp_file)
4882 *tmp_file = false;
4883
4884 if (grace_disallows_io(net, ino))
4885 return nfserr_grace;
4886
4887 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
4888 status = check_special_stateids(net, fhp, stateid, flags);
4889 goto done;
4890 }
4891
4892 status = nfsd4_lookup_stateid(cstate, stateid,
4893 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4894 &s, nn);
4895 if (status)
4896 return status;
4897 status = check_stateid_generation(stateid, &s->sc_stateid,
4898 nfsd4_has_session(cstate));
4899 if (status)
4900 goto out;
4901
4902 switch (s->sc_type) {
4903 case NFS4_DELEG_STID:
4904 status = nfs4_check_delegmode(delegstateid(s), flags);
4905 break;
4906 case NFS4_OPEN_STID:
4907 case NFS4_LOCK_STID:
4908 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
4909 break;
4910 default:
4911 status = nfserr_bad_stateid;
4912 break;
4913 }
4914 if (status)
4915 goto out;
4916 status = nfs4_check_fh(fhp, s);
4917
4918 done:
4919 if (!status && filpp)
4920 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
4921 out:
4922 if (s)
4923 nfs4_put_stid(s);
4924 return status;
4925 }
4926
4927 /*
4928 * Test if the stateid is valid
4929 */
4930 __be32
nfsd4_test_stateid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_test_stateid * test_stateid)4931 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4932 struct nfsd4_test_stateid *test_stateid)
4933 {
4934 struct nfsd4_test_stateid_id *stateid;
4935 struct nfs4_client *cl = cstate->session->se_client;
4936
4937 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4938 stateid->ts_id_status =
4939 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4940
4941 return nfs_ok;
4942 }
4943
4944 static __be32
nfsd4_free_lock_stateid(stateid_t * stateid,struct nfs4_stid * s)4945 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
4946 {
4947 struct nfs4_ol_stateid *stp = openlockstateid(s);
4948 __be32 ret;
4949
4950 mutex_lock(&stp->st_mutex);
4951
4952 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4953 if (ret)
4954 goto out;
4955
4956 ret = nfserr_locks_held;
4957 if (check_for_locks(stp->st_stid.sc_file,
4958 lockowner(stp->st_stateowner)))
4959 goto out;
4960
4961 release_lock_stateid(stp);
4962 ret = nfs_ok;
4963
4964 out:
4965 mutex_unlock(&stp->st_mutex);
4966 nfs4_put_stid(s);
4967 return ret;
4968 }
4969
4970 __be32
nfsd4_free_stateid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_free_stateid * free_stateid)4971 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4972 struct nfsd4_free_stateid *free_stateid)
4973 {
4974 stateid_t *stateid = &free_stateid->fr_stateid;
4975 struct nfs4_stid *s;
4976 struct nfs4_delegation *dp;
4977 struct nfs4_client *cl = cstate->session->se_client;
4978 __be32 ret = nfserr_bad_stateid;
4979
4980 spin_lock(&cl->cl_lock);
4981 s = find_stateid_locked(cl, stateid);
4982 if (!s)
4983 goto out_unlock;
4984 switch (s->sc_type) {
4985 case NFS4_DELEG_STID:
4986 ret = nfserr_locks_held;
4987 break;
4988 case NFS4_OPEN_STID:
4989 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4990 if (ret)
4991 break;
4992 ret = nfserr_locks_held;
4993 break;
4994 case NFS4_LOCK_STID:
4995 atomic_inc(&s->sc_count);
4996 spin_unlock(&cl->cl_lock);
4997 ret = nfsd4_free_lock_stateid(stateid, s);
4998 goto out;
4999 case NFS4_REVOKED_DELEG_STID:
5000 dp = delegstateid(s);
5001 list_del_init(&dp->dl_recall_lru);
5002 spin_unlock(&cl->cl_lock);
5003 nfs4_put_stid(s);
5004 ret = nfs_ok;
5005 goto out;
5006 /* Default falls through and returns nfserr_bad_stateid */
5007 }
5008 out_unlock:
5009 spin_unlock(&cl->cl_lock);
5010 out:
5011 return ret;
5012 }
5013
5014 static inline int
setlkflg(int type)5015 setlkflg (int type)
5016 {
5017 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5018 RD_STATE : WR_STATE;
5019 }
5020
nfs4_seqid_op_checks(struct nfsd4_compound_state * cstate,stateid_t * stateid,u32 seqid,struct nfs4_ol_stateid * stp)5021 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
5022 {
5023 struct svc_fh *current_fh = &cstate->current_fh;
5024 struct nfs4_stateowner *sop = stp->st_stateowner;
5025 __be32 status;
5026
5027 status = nfsd4_check_seqid(cstate, sop, seqid);
5028 if (status)
5029 return status;
5030 status = nfsd4_lock_ol_stateid(stp);
5031 if (status != nfs_ok)
5032 return status;
5033 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
5034 if (status == nfs_ok)
5035 status = nfs4_check_fh(current_fh, &stp->st_stid);
5036 if (status != nfs_ok)
5037 mutex_unlock(&stp->st_mutex);
5038 return status;
5039 }
5040
5041 /*
5042 * Checks for sequence id mutating operations.
5043 */
5044 static __be32
nfs4_preprocess_seqid_op(struct nfsd4_compound_state * cstate,u32 seqid,stateid_t * stateid,char typemask,struct nfs4_ol_stateid ** stpp,struct nfsd_net * nn)5045 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5046 stateid_t *stateid, char typemask,
5047 struct nfs4_ol_stateid **stpp,
5048 struct nfsd_net *nn)
5049 {
5050 __be32 status;
5051 struct nfs4_stid *s;
5052 struct nfs4_ol_stateid *stp = NULL;
5053
5054 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5055 seqid, STATEID_VAL(stateid));
5056
5057 *stpp = NULL;
5058 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5059 if (status)
5060 return status;
5061 stp = openlockstateid(s);
5062 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
5063
5064 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5065 if (!status)
5066 *stpp = stp;
5067 else
5068 nfs4_put_stid(&stp->st_stid);
5069 return status;
5070 }
5071
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state * cstate,u32 seqid,stateid_t * stateid,struct nfs4_ol_stateid ** stpp,struct nfsd_net * nn)5072 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5073 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5074 {
5075 __be32 status;
5076 struct nfs4_openowner *oo;
5077 struct nfs4_ol_stateid *stp;
5078
5079 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
5080 NFS4_OPEN_STID, &stp, nn);
5081 if (status)
5082 return status;
5083 oo = openowner(stp->st_stateowner);
5084 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5085 mutex_unlock(&stp->st_mutex);
5086 nfs4_put_stid(&stp->st_stid);
5087 return nfserr_bad_stateid;
5088 }
5089 *stpp = stp;
5090 return nfs_ok;
5091 }
5092
5093 __be32
nfsd4_open_confirm(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_open_confirm * oc)5094 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5095 struct nfsd4_open_confirm *oc)
5096 {
5097 __be32 status;
5098 struct nfs4_openowner *oo;
5099 struct nfs4_ol_stateid *stp;
5100 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5101
5102 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5103 cstate->current_fh.fh_dentry);
5104
5105 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5106 if (status)
5107 return status;
5108
5109 status = nfs4_preprocess_seqid_op(cstate,
5110 oc->oc_seqid, &oc->oc_req_stateid,
5111 NFS4_OPEN_STID, &stp, nn);
5112 if (status)
5113 goto out;
5114 oo = openowner(stp->st_stateowner);
5115 status = nfserr_bad_stateid;
5116 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5117 mutex_unlock(&stp->st_mutex);
5118 goto put_stateid;
5119 }
5120 oo->oo_flags |= NFS4_OO_CONFIRMED;
5121 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
5122 mutex_unlock(&stp->st_mutex);
5123 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
5124 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
5125
5126 nfsd4_client_record_create(oo->oo_owner.so_client);
5127 status = nfs_ok;
5128 put_stateid:
5129 nfs4_put_stid(&stp->st_stid);
5130 out:
5131 nfsd4_bump_seqid(cstate, status);
5132 return status;
5133 }
5134
nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid * stp,u32 access)5135 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
5136 {
5137 if (!test_access(access, stp))
5138 return;
5139 nfs4_file_put_access(stp->st_stid.sc_file, access);
5140 clear_access(access, stp);
5141 }
5142
nfs4_stateid_downgrade(struct nfs4_ol_stateid * stp,u32 to_access)5143 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5144 {
5145 switch (to_access) {
5146 case NFS4_SHARE_ACCESS_READ:
5147 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5148 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5149 break;
5150 case NFS4_SHARE_ACCESS_WRITE:
5151 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5152 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5153 break;
5154 case NFS4_SHARE_ACCESS_BOTH:
5155 break;
5156 default:
5157 WARN_ON_ONCE(1);
5158 }
5159 }
5160
5161 __be32
nfsd4_open_downgrade(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_open_downgrade * od)5162 nfsd4_open_downgrade(struct svc_rqst *rqstp,
5163 struct nfsd4_compound_state *cstate,
5164 struct nfsd4_open_downgrade *od)
5165 {
5166 __be32 status;
5167 struct nfs4_ol_stateid *stp;
5168 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5169
5170 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5171 cstate->current_fh.fh_dentry);
5172
5173 /* We don't yet support WANT bits: */
5174 if (od->od_deleg_want)
5175 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5176 od->od_deleg_want);
5177
5178 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
5179 &od->od_stateid, &stp, nn);
5180 if (status)
5181 goto out;
5182 status = nfserr_inval;
5183 if (!test_access(od->od_share_access, stp)) {
5184 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5185 stp->st_access_bmap, od->od_share_access);
5186 goto put_stateid;
5187 }
5188 if (!test_deny(od->od_share_deny, stp)) {
5189 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5190 stp->st_deny_bmap, od->od_share_deny);
5191 goto put_stateid;
5192 }
5193 nfs4_stateid_downgrade(stp, od->od_share_access);
5194 reset_union_bmap_deny(od->od_share_deny, stp);
5195 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
5196 status = nfs_ok;
5197 put_stateid:
5198 mutex_unlock(&stp->st_mutex);
5199 nfs4_put_stid(&stp->st_stid);
5200 out:
5201 nfsd4_bump_seqid(cstate, status);
5202 return status;
5203 }
5204
nfsd4_close_open_stateid(struct nfs4_ol_stateid * s)5205 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5206 {
5207 struct nfs4_client *clp = s->st_stid.sc_client;
5208 bool unhashed;
5209 LIST_HEAD(reaplist);
5210
5211 spin_lock(&clp->cl_lock);
5212 unhashed = unhash_open_stateid(s, &reaplist);
5213
5214 if (clp->cl_minorversion) {
5215 if (unhashed)
5216 put_ol_stateid_locked(s, &reaplist);
5217 spin_unlock(&clp->cl_lock);
5218 free_ol_stateid_reaplist(&reaplist);
5219 } else {
5220 spin_unlock(&clp->cl_lock);
5221 free_ol_stateid_reaplist(&reaplist);
5222 if (unhashed)
5223 move_to_close_lru(s, clp->net);
5224 }
5225 }
5226
5227 /*
5228 * nfs4_unlock_state() called after encode
5229 */
5230 __be32
nfsd4_close(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_close * close)5231 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5232 struct nfsd4_close *close)
5233 {
5234 __be32 status;
5235 struct nfs4_ol_stateid *stp;
5236 struct net *net = SVC_NET(rqstp);
5237 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5238
5239 dprintk("NFSD: nfsd4_close on file %pd\n",
5240 cstate->current_fh.fh_dentry);
5241
5242 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5243 &close->cl_stateid,
5244 NFS4_OPEN_STID|NFS4_CLOSED_STID,
5245 &stp, nn);
5246 nfsd4_bump_seqid(cstate, status);
5247 if (status)
5248 goto out;
5249
5250 stp->st_stid.sc_type = NFS4_CLOSED_STID;
5251 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
5252
5253 nfsd4_close_open_stateid(stp);
5254 mutex_unlock(&stp->st_mutex);
5255
5256 /* See RFC5661 sectionm 18.2.4 */
5257 if (stp->st_stid.sc_client->cl_minorversion)
5258 memcpy(&close->cl_stateid, &close_stateid,
5259 sizeof(close->cl_stateid));
5260
5261 /* put reference from nfs4_preprocess_seqid_op */
5262 nfs4_put_stid(&stp->st_stid);
5263 out:
5264 return status;
5265 }
5266
5267 __be32
nfsd4_delegreturn(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_delegreturn * dr)5268 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5269 struct nfsd4_delegreturn *dr)
5270 {
5271 struct nfs4_delegation *dp;
5272 stateid_t *stateid = &dr->dr_stateid;
5273 struct nfs4_stid *s;
5274 __be32 status;
5275 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5276
5277 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5278 return status;
5279
5280 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
5281 if (status)
5282 goto out;
5283 dp = delegstateid(s);
5284 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
5285 if (status)
5286 goto put_stateid;
5287
5288 destroy_delegation(dp);
5289 put_stateid:
5290 nfs4_put_stid(&dp->dl_stid);
5291 out:
5292 return status;
5293 }
5294
5295 static inline u64
end_offset(u64 start,u64 len)5296 end_offset(u64 start, u64 len)
5297 {
5298 u64 end;
5299
5300 end = start + len;
5301 return end >= start ? end: NFS4_MAX_UINT64;
5302 }
5303
5304 /* last octet in a range */
5305 static inline u64
last_byte_offset(u64 start,u64 len)5306 last_byte_offset(u64 start, u64 len)
5307 {
5308 u64 end;
5309
5310 WARN_ON_ONCE(!len);
5311 end = start + len;
5312 return end > start ? end - 1: NFS4_MAX_UINT64;
5313 }
5314
5315 /*
5316 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5317 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5318 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5319 * locking, this prevents us from being completely protocol-compliant. The
5320 * real solution to this problem is to start using unsigned file offsets in
5321 * the VFS, but this is a very deep change!
5322 */
5323 static inline void
nfs4_transform_lock_offset(struct file_lock * lock)5324 nfs4_transform_lock_offset(struct file_lock *lock)
5325 {
5326 if (lock->fl_start < 0)
5327 lock->fl_start = OFFSET_MAX;
5328 if (lock->fl_end < 0)
5329 lock->fl_end = OFFSET_MAX;
5330 }
5331
5332 static fl_owner_t
nfsd4_fl_get_owner(fl_owner_t owner)5333 nfsd4_fl_get_owner(fl_owner_t owner)
5334 {
5335 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5336
5337 nfs4_get_stateowner(&lo->lo_owner);
5338 return owner;
5339 }
5340
5341 static void
nfsd4_fl_put_owner(fl_owner_t owner)5342 nfsd4_fl_put_owner(fl_owner_t owner)
5343 {
5344 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
5345
5346 if (lo)
5347 nfs4_put_stateowner(&lo->lo_owner);
5348 }
5349
5350 static const struct lock_manager_operations nfsd_posix_mng_ops = {
5351 .lm_get_owner = nfsd4_fl_get_owner,
5352 .lm_put_owner = nfsd4_fl_put_owner,
5353 };
5354
5355 static inline void
nfs4_set_lock_denied(struct file_lock * fl,struct nfsd4_lock_denied * deny)5356 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
5357 {
5358 struct nfs4_lockowner *lo;
5359
5360 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
5361 lo = (struct nfs4_lockowner *) fl->fl_owner;
5362 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
5363 lo->lo_owner.so_owner.len, GFP_KERNEL);
5364 if (!deny->ld_owner.data)
5365 /* We just don't care that much */
5366 goto nevermind;
5367 deny->ld_owner.len = lo->lo_owner.so_owner.len;
5368 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
5369 } else {
5370 nevermind:
5371 deny->ld_owner.len = 0;
5372 deny->ld_owner.data = NULL;
5373 deny->ld_clientid.cl_boot = 0;
5374 deny->ld_clientid.cl_id = 0;
5375 }
5376 deny->ld_start = fl->fl_start;
5377 deny->ld_length = NFS4_MAX_UINT64;
5378 if (fl->fl_end != NFS4_MAX_UINT64)
5379 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5380 deny->ld_type = NFS4_READ_LT;
5381 if (fl->fl_type != F_RDLCK)
5382 deny->ld_type = NFS4_WRITE_LT;
5383 }
5384
5385 static struct nfs4_lockowner *
find_lockowner_str_locked(struct nfs4_client * clp,struct xdr_netobj * owner)5386 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
5387 {
5388 unsigned int strhashval = ownerstr_hashval(owner);
5389 struct nfs4_stateowner *so;
5390
5391 lockdep_assert_held(&clp->cl_lock);
5392
5393 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5394 so_strhash) {
5395 if (so->so_is_open_owner)
5396 continue;
5397 if (same_owner_str(so, owner))
5398 return lockowner(nfs4_get_stateowner(so));
5399 }
5400 return NULL;
5401 }
5402
5403 static struct nfs4_lockowner *
find_lockowner_str(struct nfs4_client * clp,struct xdr_netobj * owner)5404 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
5405 {
5406 struct nfs4_lockowner *lo;
5407
5408 spin_lock(&clp->cl_lock);
5409 lo = find_lockowner_str_locked(clp, owner);
5410 spin_unlock(&clp->cl_lock);
5411 return lo;
5412 }
5413
nfs4_unhash_lockowner(struct nfs4_stateowner * sop)5414 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5415 {
5416 unhash_lockowner_locked(lockowner(sop));
5417 }
5418
nfs4_free_lockowner(struct nfs4_stateowner * sop)5419 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5420 {
5421 struct nfs4_lockowner *lo = lockowner(sop);
5422
5423 kmem_cache_free(lockowner_slab, lo);
5424 }
5425
5426 static const struct nfs4_stateowner_operations lockowner_ops = {
5427 .so_unhash = nfs4_unhash_lockowner,
5428 .so_free = nfs4_free_lockowner,
5429 };
5430
5431 /*
5432 * Alloc a lock owner structure.
5433 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5434 * occurred.
5435 *
5436 * strhashval = ownerstr_hashval
5437 */
5438 static struct nfs4_lockowner *
alloc_init_lock_stateowner(unsigned int strhashval,struct nfs4_client * clp,struct nfs4_ol_stateid * open_stp,struct nfsd4_lock * lock)5439 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5440 struct nfs4_ol_stateid *open_stp,
5441 struct nfsd4_lock *lock)
5442 {
5443 struct nfs4_lockowner *lo, *ret;
5444
5445 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5446 if (!lo)
5447 return NULL;
5448 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5449 lo->lo_owner.so_is_open_owner = 0;
5450 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5451 lo->lo_owner.so_ops = &lockowner_ops;
5452 spin_lock(&clp->cl_lock);
5453 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
5454 if (ret == NULL) {
5455 list_add(&lo->lo_owner.so_strhash,
5456 &clp->cl_ownerstr_hashtbl[strhashval]);
5457 ret = lo;
5458 } else
5459 nfs4_free_stateowner(&lo->lo_owner);
5460
5461 spin_unlock(&clp->cl_lock);
5462 return ret;
5463 }
5464
5465 static void
init_lock_stateid(struct nfs4_ol_stateid * stp,struct nfs4_lockowner * lo,struct nfs4_file * fp,struct inode * inode,struct nfs4_ol_stateid * open_stp)5466 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5467 struct nfs4_file *fp, struct inode *inode,
5468 struct nfs4_ol_stateid *open_stp)
5469 {
5470 struct nfs4_client *clp = lo->lo_owner.so_client;
5471
5472 lockdep_assert_held(&clp->cl_lock);
5473
5474 atomic_inc(&stp->st_stid.sc_count);
5475 stp->st_stid.sc_type = NFS4_LOCK_STID;
5476 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5477 get_nfs4_file(fp);
5478 stp->st_stid.sc_file = fp;
5479 stp->st_access_bmap = 0;
5480 stp->st_deny_bmap = open_stp->st_deny_bmap;
5481 stp->st_openstp = open_stp;
5482 mutex_init(&stp->st_mutex);
5483 list_add(&stp->st_locks, &open_stp->st_locks);
5484 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5485 spin_lock(&fp->fi_lock);
5486 list_add(&stp->st_perfile, &fp->fi_stateids);
5487 spin_unlock(&fp->fi_lock);
5488 }
5489
5490 static struct nfs4_ol_stateid *
find_lock_stateid(struct nfs4_lockowner * lo,struct nfs4_file * fp)5491 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5492 {
5493 struct nfs4_ol_stateid *lst;
5494 struct nfs4_client *clp = lo->lo_owner.so_client;
5495
5496 lockdep_assert_held(&clp->cl_lock);
5497
5498 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5499 if (lst->st_stid.sc_file == fp) {
5500 atomic_inc(&lst->st_stid.sc_count);
5501 return lst;
5502 }
5503 }
5504 return NULL;
5505 }
5506
5507 static struct nfs4_ol_stateid *
find_or_create_lock_stateid(struct nfs4_lockowner * lo,struct nfs4_file * fi,struct inode * inode,struct nfs4_ol_stateid * ost,bool * new)5508 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5509 struct inode *inode, struct nfs4_ol_stateid *ost,
5510 bool *new)
5511 {
5512 struct nfs4_stid *ns = NULL;
5513 struct nfs4_ol_stateid *lst;
5514 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5515 struct nfs4_client *clp = oo->oo_owner.so_client;
5516
5517 spin_lock(&clp->cl_lock);
5518 lst = find_lock_stateid(lo, fi);
5519 if (lst == NULL) {
5520 spin_unlock(&clp->cl_lock);
5521 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
5522 if (ns == NULL)
5523 return NULL;
5524
5525 spin_lock(&clp->cl_lock);
5526 lst = find_lock_stateid(lo, fi);
5527 if (likely(!lst)) {
5528 lst = openlockstateid(ns);
5529 init_lock_stateid(lst, lo, fi, inode, ost);
5530 ns = NULL;
5531 *new = true;
5532 }
5533 }
5534 spin_unlock(&clp->cl_lock);
5535 if (ns)
5536 nfs4_put_stid(ns);
5537 return lst;
5538 }
5539
5540 static int
check_lock_length(u64 offset,u64 length)5541 check_lock_length(u64 offset, u64 length)
5542 {
5543 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5544 (length > ~offset)));
5545 }
5546
get_lock_access(struct nfs4_ol_stateid * lock_stp,u32 access)5547 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5548 {
5549 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5550
5551 lockdep_assert_held(&fp->fi_lock);
5552
5553 if (test_access(access, lock_stp))
5554 return;
5555 __nfs4_file_get_access(fp, access);
5556 set_access(access, lock_stp);
5557 }
5558
5559 static __be32
lookup_or_create_lock_state(struct nfsd4_compound_state * cstate,struct nfs4_ol_stateid * ost,struct nfsd4_lock * lock,struct nfs4_ol_stateid ** plst,bool * new)5560 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5561 struct nfs4_ol_stateid *ost,
5562 struct nfsd4_lock *lock,
5563 struct nfs4_ol_stateid **plst, bool *new)
5564 {
5565 __be32 status;
5566 struct nfs4_file *fi = ost->st_stid.sc_file;
5567 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5568 struct nfs4_client *cl = oo->oo_owner.so_client;
5569 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
5570 struct nfs4_lockowner *lo;
5571 struct nfs4_ol_stateid *lst;
5572 unsigned int strhashval;
5573 bool hashed;
5574
5575 lo = find_lockowner_str(cl, &lock->lk_new_owner);
5576 if (!lo) {
5577 strhashval = ownerstr_hashval(&lock->lk_new_owner);
5578 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5579 if (lo == NULL)
5580 return nfserr_jukebox;
5581 } else {
5582 /* with an existing lockowner, seqids must be the same */
5583 status = nfserr_bad_seqid;
5584 if (!cstate->minorversion &&
5585 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5586 goto out;
5587 }
5588
5589 retry:
5590 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5591 if (lst == NULL) {
5592 status = nfserr_jukebox;
5593 goto out;
5594 }
5595
5596 mutex_lock(&lst->st_mutex);
5597
5598 /* See if it's still hashed to avoid race with FREE_STATEID */
5599 spin_lock(&cl->cl_lock);
5600 hashed = !list_empty(&lst->st_perfile);
5601 spin_unlock(&cl->cl_lock);
5602
5603 if (!hashed) {
5604 mutex_unlock(&lst->st_mutex);
5605 nfs4_put_stid(&lst->st_stid);
5606 goto retry;
5607 }
5608 status = nfs_ok;
5609 *plst = lst;
5610 out:
5611 nfs4_put_stateowner(&lo->lo_owner);
5612 return status;
5613 }
5614
5615 /*
5616 * LOCK operation
5617 */
5618 __be32
nfsd4_lock(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_lock * lock)5619 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5620 struct nfsd4_lock *lock)
5621 {
5622 struct nfs4_openowner *open_sop = NULL;
5623 struct nfs4_lockowner *lock_sop = NULL;
5624 struct nfs4_ol_stateid *lock_stp = NULL;
5625 struct nfs4_ol_stateid *open_stp = NULL;
5626 struct nfs4_file *fp;
5627 struct file *filp = NULL;
5628 struct file_lock *file_lock = NULL;
5629 struct file_lock *conflock = NULL;
5630 __be32 status = 0;
5631 int lkflg;
5632 int err;
5633 bool new = false;
5634 struct net *net = SVC_NET(rqstp);
5635 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5636
5637 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5638 (long long) lock->lk_offset,
5639 (long long) lock->lk_length);
5640
5641 if (check_lock_length(lock->lk_offset, lock->lk_length))
5642 return nfserr_inval;
5643
5644 if ((status = fh_verify(rqstp, &cstate->current_fh,
5645 S_IFREG, NFSD_MAY_LOCK))) {
5646 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5647 return status;
5648 }
5649
5650 if (lock->lk_is_new) {
5651 if (nfsd4_has_session(cstate))
5652 /* See rfc 5661 18.10.3: given clientid is ignored: */
5653 memcpy(&lock->lk_new_clientid,
5654 &cstate->session->se_client->cl_clientid,
5655 sizeof(clientid_t));
5656
5657 status = nfserr_stale_clientid;
5658 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5659 goto out;
5660
5661 /* validate and update open stateid and open seqid */
5662 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5663 lock->lk_new_open_seqid,
5664 &lock->lk_new_open_stateid,
5665 &open_stp, nn);
5666 if (status)
5667 goto out;
5668 mutex_unlock(&open_stp->st_mutex);
5669 open_sop = openowner(open_stp->st_stateowner);
5670 status = nfserr_bad_stateid;
5671 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5672 &lock->lk_new_clientid))
5673 goto out;
5674 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5675 &lock_stp, &new);
5676 } else {
5677 status = nfs4_preprocess_seqid_op(cstate,
5678 lock->lk_old_lock_seqid,
5679 &lock->lk_old_lock_stateid,
5680 NFS4_LOCK_STID, &lock_stp, nn);
5681 }
5682 if (status)
5683 goto out;
5684 lock_sop = lockowner(lock_stp->st_stateowner);
5685
5686 lkflg = setlkflg(lock->lk_type);
5687 status = nfs4_check_openmode(lock_stp, lkflg);
5688 if (status)
5689 goto out;
5690
5691 status = nfserr_grace;
5692 if (locks_in_grace(net) && !lock->lk_reclaim)
5693 goto out;
5694 status = nfserr_no_grace;
5695 if (!locks_in_grace(net) && lock->lk_reclaim)
5696 goto out;
5697
5698 file_lock = locks_alloc_lock();
5699 if (!file_lock) {
5700 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5701 status = nfserr_jukebox;
5702 goto out;
5703 }
5704
5705 fp = lock_stp->st_stid.sc_file;
5706 switch (lock->lk_type) {
5707 case NFS4_READ_LT:
5708 case NFS4_READW_LT:
5709 spin_lock(&fp->fi_lock);
5710 filp = find_readable_file_locked(fp);
5711 if (filp)
5712 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5713 spin_unlock(&fp->fi_lock);
5714 file_lock->fl_type = F_RDLCK;
5715 break;
5716 case NFS4_WRITE_LT:
5717 case NFS4_WRITEW_LT:
5718 spin_lock(&fp->fi_lock);
5719 filp = find_writeable_file_locked(fp);
5720 if (filp)
5721 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5722 spin_unlock(&fp->fi_lock);
5723 file_lock->fl_type = F_WRLCK;
5724 break;
5725 default:
5726 status = nfserr_inval;
5727 goto out;
5728 }
5729 if (!filp) {
5730 status = nfserr_openmode;
5731 goto out;
5732 }
5733
5734 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5735 file_lock->fl_pid = current->tgid;
5736 file_lock->fl_file = filp;
5737 file_lock->fl_flags = FL_POSIX;
5738 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5739 file_lock->fl_start = lock->lk_offset;
5740 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5741 nfs4_transform_lock_offset(file_lock);
5742
5743 conflock = locks_alloc_lock();
5744 if (!conflock) {
5745 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5746 status = nfserr_jukebox;
5747 goto out;
5748 }
5749
5750 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5751 switch (-err) {
5752 case 0: /* success! */
5753 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
5754 status = 0;
5755 break;
5756 case (EAGAIN): /* conflock holds conflicting lock */
5757 status = nfserr_denied;
5758 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5759 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5760 break;
5761 case (EDEADLK):
5762 status = nfserr_deadlock;
5763 break;
5764 default:
5765 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5766 status = nfserrno(err);
5767 break;
5768 }
5769 out:
5770 if (filp)
5771 fput(filp);
5772 if (lock_stp) {
5773 /* Bump seqid manually if the 4.0 replay owner is openowner */
5774 if (cstate->replay_owner &&
5775 cstate->replay_owner != &lock_sop->lo_owner &&
5776 seqid_mutating_err(ntohl(status)))
5777 lock_sop->lo_owner.so_seqid++;
5778
5779 mutex_unlock(&lock_stp->st_mutex);
5780
5781 /*
5782 * If this is a new, never-before-used stateid, and we are
5783 * returning an error, then just go ahead and release it.
5784 */
5785 if (status && new)
5786 release_lock_stateid(lock_stp);
5787
5788 nfs4_put_stid(&lock_stp->st_stid);
5789 }
5790 if (open_stp)
5791 nfs4_put_stid(&open_stp->st_stid);
5792 nfsd4_bump_seqid(cstate, status);
5793 if (file_lock)
5794 locks_free_lock(file_lock);
5795 if (conflock)
5796 locks_free_lock(conflock);
5797 return status;
5798 }
5799
5800 /*
5801 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5802 * so we do a temporary open here just to get an open file to pass to
5803 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5804 * inode operation.)
5805 */
nfsd_test_lock(struct svc_rqst * rqstp,struct svc_fh * fhp,struct file_lock * lock)5806 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5807 {
5808 struct file *file;
5809 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5810 if (!err) {
5811 err = nfserrno(vfs_test_lock(file, lock));
5812 fput(file);
5813 }
5814 return err;
5815 }
5816
5817 /*
5818 * LOCKT operation
5819 */
5820 __be32
nfsd4_lockt(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_lockt * lockt)5821 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5822 struct nfsd4_lockt *lockt)
5823 {
5824 struct file_lock *file_lock = NULL;
5825 struct nfs4_lockowner *lo = NULL;
5826 __be32 status;
5827 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5828
5829 if (locks_in_grace(SVC_NET(rqstp)))
5830 return nfserr_grace;
5831
5832 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5833 return nfserr_inval;
5834
5835 if (!nfsd4_has_session(cstate)) {
5836 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5837 if (status)
5838 goto out;
5839 }
5840
5841 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5842 goto out;
5843
5844 file_lock = locks_alloc_lock();
5845 if (!file_lock) {
5846 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5847 status = nfserr_jukebox;
5848 goto out;
5849 }
5850
5851 switch (lockt->lt_type) {
5852 case NFS4_READ_LT:
5853 case NFS4_READW_LT:
5854 file_lock->fl_type = F_RDLCK;
5855 break;
5856 case NFS4_WRITE_LT:
5857 case NFS4_WRITEW_LT:
5858 file_lock->fl_type = F_WRLCK;
5859 break;
5860 default:
5861 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5862 status = nfserr_inval;
5863 goto out;
5864 }
5865
5866 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
5867 if (lo)
5868 file_lock->fl_owner = (fl_owner_t)lo;
5869 file_lock->fl_pid = current->tgid;
5870 file_lock->fl_flags = FL_POSIX;
5871
5872 file_lock->fl_start = lockt->lt_offset;
5873 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5874
5875 nfs4_transform_lock_offset(file_lock);
5876
5877 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5878 if (status)
5879 goto out;
5880
5881 if (file_lock->fl_type != F_UNLCK) {
5882 status = nfserr_denied;
5883 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5884 }
5885 out:
5886 if (lo)
5887 nfs4_put_stateowner(&lo->lo_owner);
5888 if (file_lock)
5889 locks_free_lock(file_lock);
5890 return status;
5891 }
5892
5893 __be32
nfsd4_locku(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_locku * locku)5894 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5895 struct nfsd4_locku *locku)
5896 {
5897 struct nfs4_ol_stateid *stp;
5898 struct file *filp = NULL;
5899 struct file_lock *file_lock = NULL;
5900 __be32 status;
5901 int err;
5902 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5903
5904 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5905 (long long) locku->lu_offset,
5906 (long long) locku->lu_length);
5907
5908 if (check_lock_length(locku->lu_offset, locku->lu_length))
5909 return nfserr_inval;
5910
5911 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5912 &locku->lu_stateid, NFS4_LOCK_STID,
5913 &stp, nn);
5914 if (status)
5915 goto out;
5916 filp = find_any_file(stp->st_stid.sc_file);
5917 if (!filp) {
5918 status = nfserr_lock_range;
5919 goto put_stateid;
5920 }
5921 file_lock = locks_alloc_lock();
5922 if (!file_lock) {
5923 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5924 status = nfserr_jukebox;
5925 goto fput;
5926 }
5927
5928 file_lock->fl_type = F_UNLCK;
5929 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
5930 file_lock->fl_pid = current->tgid;
5931 file_lock->fl_file = filp;
5932 file_lock->fl_flags = FL_POSIX;
5933 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5934 file_lock->fl_start = locku->lu_offset;
5935
5936 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5937 locku->lu_length);
5938 nfs4_transform_lock_offset(file_lock);
5939
5940 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5941 if (err) {
5942 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5943 goto out_nfserr;
5944 }
5945 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
5946 fput:
5947 fput(filp);
5948 put_stateid:
5949 mutex_unlock(&stp->st_mutex);
5950 nfs4_put_stid(&stp->st_stid);
5951 out:
5952 nfsd4_bump_seqid(cstate, status);
5953 if (file_lock)
5954 locks_free_lock(file_lock);
5955 return status;
5956
5957 out_nfserr:
5958 status = nfserrno(err);
5959 goto fput;
5960 }
5961
5962 /*
5963 * returns
5964 * true: locks held by lockowner
5965 * false: no locks held by lockowner
5966 */
5967 static bool
check_for_locks(struct nfs4_file * fp,struct nfs4_lockowner * lowner)5968 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5969 {
5970 struct file_lock *fl;
5971 int status = false;
5972 struct file *filp = find_any_file(fp);
5973 struct inode *inode;
5974 struct file_lock_context *flctx;
5975
5976 if (!filp) {
5977 /* Any valid lock stateid should have some sort of access */
5978 WARN_ON_ONCE(1);
5979 return status;
5980 }
5981
5982 inode = file_inode(filp);
5983 flctx = inode->i_flctx;
5984
5985 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
5986 spin_lock(&flctx->flc_lock);
5987 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
5988 if (fl->fl_owner == (fl_owner_t)lowner) {
5989 status = true;
5990 break;
5991 }
5992 }
5993 spin_unlock(&flctx->flc_lock);
5994 }
5995 fput(filp);
5996 return status;
5997 }
5998
5999 __be32
nfsd4_release_lockowner(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_release_lockowner * rlockowner)6000 nfsd4_release_lockowner(struct svc_rqst *rqstp,
6001 struct nfsd4_compound_state *cstate,
6002 struct nfsd4_release_lockowner *rlockowner)
6003 {
6004 clientid_t *clid = &rlockowner->rl_clientid;
6005 struct nfs4_stateowner *sop;
6006 struct nfs4_lockowner *lo = NULL;
6007 struct nfs4_ol_stateid *stp;
6008 struct xdr_netobj *owner = &rlockowner->rl_owner;
6009 unsigned int hashval = ownerstr_hashval(owner);
6010 __be32 status;
6011 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6012 struct nfs4_client *clp;
6013 LIST_HEAD (reaplist);
6014
6015 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6016 clid->cl_boot, clid->cl_id);
6017
6018 status = lookup_clientid(clid, cstate, nn);
6019 if (status)
6020 return status;
6021
6022 clp = cstate->clp;
6023 /* Find the matching lock stateowner */
6024 spin_lock(&clp->cl_lock);
6025 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
6026 so_strhash) {
6027
6028 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
6029 continue;
6030
6031 /* see if there are still any locks associated with it */
6032 lo = lockowner(sop);
6033 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6034 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6035 status = nfserr_locks_held;
6036 spin_unlock(&clp->cl_lock);
6037 return status;
6038 }
6039 }
6040
6041 nfs4_get_stateowner(sop);
6042 break;
6043 }
6044 if (!lo) {
6045 spin_unlock(&clp->cl_lock);
6046 return status;
6047 }
6048
6049 unhash_lockowner_locked(lo);
6050 while (!list_empty(&lo->lo_owner.so_stateids)) {
6051 stp = list_first_entry(&lo->lo_owner.so_stateids,
6052 struct nfs4_ol_stateid,
6053 st_perstateowner);
6054 WARN_ON(!unhash_lock_stateid(stp));
6055 put_ol_stateid_locked(stp, &reaplist);
6056 }
6057 spin_unlock(&clp->cl_lock);
6058 free_ol_stateid_reaplist(&reaplist);
6059 nfs4_put_stateowner(&lo->lo_owner);
6060
6061 return status;
6062 }
6063
6064 static inline struct nfs4_client_reclaim *
alloc_reclaim(void)6065 alloc_reclaim(void)
6066 {
6067 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
6068 }
6069
6070 bool
nfs4_has_reclaimed_state(const char * name,struct nfsd_net * nn)6071 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
6072 {
6073 struct nfs4_client_reclaim *crp;
6074
6075 crp = nfsd4_find_reclaim_client(name, nn);
6076 return (crp && crp->cr_clp);
6077 }
6078
6079 /*
6080 * failure => all reset bets are off, nfserr_no_grace...
6081 */
6082 struct nfs4_client_reclaim *
nfs4_client_to_reclaim(const char * name,struct nfsd_net * nn)6083 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
6084 {
6085 unsigned int strhashval;
6086 struct nfs4_client_reclaim *crp;
6087
6088 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
6089 crp = alloc_reclaim();
6090 if (crp) {
6091 strhashval = clientstr_hashval(name);
6092 INIT_LIST_HEAD(&crp->cr_strhash);
6093 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6094 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
6095 crp->cr_clp = NULL;
6096 nn->reclaim_str_hashtbl_size++;
6097 }
6098 return crp;
6099 }
6100
6101 void
nfs4_remove_reclaim_record(struct nfs4_client_reclaim * crp,struct nfsd_net * nn)6102 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
6103 {
6104 list_del(&crp->cr_strhash);
6105 kfree(crp);
6106 nn->reclaim_str_hashtbl_size--;
6107 }
6108
6109 void
nfs4_release_reclaim(struct nfsd_net * nn)6110 nfs4_release_reclaim(struct nfsd_net *nn)
6111 {
6112 struct nfs4_client_reclaim *crp = NULL;
6113 int i;
6114
6115 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6116 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6117 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
6118 struct nfs4_client_reclaim, cr_strhash);
6119 nfs4_remove_reclaim_record(crp, nn);
6120 }
6121 }
6122 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
6123 }
6124
6125 /*
6126 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6127 struct nfs4_client_reclaim *
nfsd4_find_reclaim_client(const char * recdir,struct nfsd_net * nn)6128 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
6129 {
6130 unsigned int strhashval;
6131 struct nfs4_client_reclaim *crp = NULL;
6132
6133 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
6134
6135 strhashval = clientstr_hashval(recdir);
6136 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
6137 if (same_name(crp->cr_recdir, recdir)) {
6138 return crp;
6139 }
6140 }
6141 return NULL;
6142 }
6143
6144 /*
6145 * Called from OPEN. Look for clientid in reclaim list.
6146 */
6147 __be32
nfs4_check_open_reclaim(clientid_t * clid,struct nfsd4_compound_state * cstate,struct nfsd_net * nn)6148 nfs4_check_open_reclaim(clientid_t *clid,
6149 struct nfsd4_compound_state *cstate,
6150 struct nfsd_net *nn)
6151 {
6152 __be32 status;
6153
6154 /* find clientid in conf_id_hashtbl */
6155 status = lookup_clientid(clid, cstate, nn);
6156 if (status)
6157 return nfserr_reclaim_bad;
6158
6159 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6160 return nfserr_no_grace;
6161
6162 if (nfsd4_client_record_check(cstate->clp))
6163 return nfserr_reclaim_bad;
6164
6165 return nfs_ok;
6166 }
6167
6168 #ifdef CONFIG_NFSD_FAULT_INJECTION
6169 static inline void
put_client(struct nfs4_client * clp)6170 put_client(struct nfs4_client *clp)
6171 {
6172 atomic_dec(&clp->cl_refcount);
6173 }
6174
6175 static struct nfs4_client *
nfsd_find_client(struct sockaddr_storage * addr,size_t addr_size)6176 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6177 {
6178 struct nfs4_client *clp;
6179 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6180 nfsd_net_id);
6181
6182 if (!nfsd_netns_ready(nn))
6183 return NULL;
6184
6185 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6186 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6187 return clp;
6188 }
6189 return NULL;
6190 }
6191
6192 u64
nfsd_inject_print_clients(void)6193 nfsd_inject_print_clients(void)
6194 {
6195 struct nfs4_client *clp;
6196 u64 count = 0;
6197 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6198 nfsd_net_id);
6199 char buf[INET6_ADDRSTRLEN];
6200
6201 if (!nfsd_netns_ready(nn))
6202 return 0;
6203
6204 spin_lock(&nn->client_lock);
6205 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6206 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6207 pr_info("NFS Client: %s\n", buf);
6208 ++count;
6209 }
6210 spin_unlock(&nn->client_lock);
6211
6212 return count;
6213 }
6214
6215 u64
nfsd_inject_forget_client(struct sockaddr_storage * addr,size_t addr_size)6216 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
6217 {
6218 u64 count = 0;
6219 struct nfs4_client *clp;
6220 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6221 nfsd_net_id);
6222
6223 if (!nfsd_netns_ready(nn))
6224 return count;
6225
6226 spin_lock(&nn->client_lock);
6227 clp = nfsd_find_client(addr, addr_size);
6228 if (clp) {
6229 if (mark_client_expired_locked(clp) == nfs_ok)
6230 ++count;
6231 else
6232 clp = NULL;
6233 }
6234 spin_unlock(&nn->client_lock);
6235
6236 if (clp)
6237 expire_client(clp);
6238
6239 return count;
6240 }
6241
6242 u64
nfsd_inject_forget_clients(u64 max)6243 nfsd_inject_forget_clients(u64 max)
6244 {
6245 u64 count = 0;
6246 struct nfs4_client *clp, *next;
6247 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6248 nfsd_net_id);
6249 LIST_HEAD(reaplist);
6250
6251 if (!nfsd_netns_ready(nn))
6252 return count;
6253
6254 spin_lock(&nn->client_lock);
6255 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6256 if (mark_client_expired_locked(clp) == nfs_ok) {
6257 list_add(&clp->cl_lru, &reaplist);
6258 if (max != 0 && ++count >= max)
6259 break;
6260 }
6261 }
6262 spin_unlock(&nn->client_lock);
6263
6264 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
6265 expire_client(clp);
6266
6267 return count;
6268 }
6269
nfsd_print_count(struct nfs4_client * clp,unsigned int count,const char * type)6270 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
6271 const char *type)
6272 {
6273 char buf[INET6_ADDRSTRLEN];
6274 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6275 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
6276 }
6277
6278 static void
nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid * lst,struct list_head * collect)6279 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
6280 struct list_head *collect)
6281 {
6282 struct nfs4_client *clp = lst->st_stid.sc_client;
6283 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6284 nfsd_net_id);
6285
6286 if (!collect)
6287 return;
6288
6289 lockdep_assert_held(&nn->client_lock);
6290 atomic_inc(&clp->cl_refcount);
6291 list_add(&lst->st_locks, collect);
6292 }
6293
nfsd_foreach_client_lock(struct nfs4_client * clp,u64 max,struct list_head * collect,bool (* func)(struct nfs4_ol_stateid *))6294 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
6295 struct list_head *collect,
6296 bool (*func)(struct nfs4_ol_stateid *))
6297 {
6298 struct nfs4_openowner *oop;
6299 struct nfs4_ol_stateid *stp, *st_next;
6300 struct nfs4_ol_stateid *lst, *lst_next;
6301 u64 count = 0;
6302
6303 spin_lock(&clp->cl_lock);
6304 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
6305 list_for_each_entry_safe(stp, st_next,
6306 &oop->oo_owner.so_stateids, st_perstateowner) {
6307 list_for_each_entry_safe(lst, lst_next,
6308 &stp->st_locks, st_locks) {
6309 if (func) {
6310 if (func(lst))
6311 nfsd_inject_add_lock_to_list(lst,
6312 collect);
6313 }
6314 ++count;
6315 /*
6316 * Despite the fact that these functions deal
6317 * with 64-bit integers for "count", we must
6318 * ensure that it doesn't blow up the
6319 * clp->cl_refcount. Throw a warning if we
6320 * start to approach INT_MAX here.
6321 */
6322 WARN_ON_ONCE(count == (INT_MAX / 2));
6323 if (count == max)
6324 goto out;
6325 }
6326 }
6327 }
6328 out:
6329 spin_unlock(&clp->cl_lock);
6330
6331 return count;
6332 }
6333
6334 static u64
nfsd_collect_client_locks(struct nfs4_client * clp,struct list_head * collect,u64 max)6335 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
6336 u64 max)
6337 {
6338 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
6339 }
6340
6341 static u64
nfsd_print_client_locks(struct nfs4_client * clp)6342 nfsd_print_client_locks(struct nfs4_client *clp)
6343 {
6344 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
6345 nfsd_print_count(clp, count, "locked files");
6346 return count;
6347 }
6348
6349 u64
nfsd_inject_print_locks(void)6350 nfsd_inject_print_locks(void)
6351 {
6352 struct nfs4_client *clp;
6353 u64 count = 0;
6354 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6355 nfsd_net_id);
6356
6357 if (!nfsd_netns_ready(nn))
6358 return 0;
6359
6360 spin_lock(&nn->client_lock);
6361 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6362 count += nfsd_print_client_locks(clp);
6363 spin_unlock(&nn->client_lock);
6364
6365 return count;
6366 }
6367
6368 static void
nfsd_reap_locks(struct list_head * reaplist)6369 nfsd_reap_locks(struct list_head *reaplist)
6370 {
6371 struct nfs4_client *clp;
6372 struct nfs4_ol_stateid *stp, *next;
6373
6374 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
6375 list_del_init(&stp->st_locks);
6376 clp = stp->st_stid.sc_client;
6377 nfs4_put_stid(&stp->st_stid);
6378 put_client(clp);
6379 }
6380 }
6381
6382 u64
nfsd_inject_forget_client_locks(struct sockaddr_storage * addr,size_t addr_size)6383 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
6384 {
6385 unsigned int count = 0;
6386 struct nfs4_client *clp;
6387 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6388 nfsd_net_id);
6389 LIST_HEAD(reaplist);
6390
6391 if (!nfsd_netns_ready(nn))
6392 return count;
6393
6394 spin_lock(&nn->client_lock);
6395 clp = nfsd_find_client(addr, addr_size);
6396 if (clp)
6397 count = nfsd_collect_client_locks(clp, &reaplist, 0);
6398 spin_unlock(&nn->client_lock);
6399 nfsd_reap_locks(&reaplist);
6400 return count;
6401 }
6402
6403 u64
nfsd_inject_forget_locks(u64 max)6404 nfsd_inject_forget_locks(u64 max)
6405 {
6406 u64 count = 0;
6407 struct nfs4_client *clp;
6408 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6409 nfsd_net_id);
6410 LIST_HEAD(reaplist);
6411
6412 if (!nfsd_netns_ready(nn))
6413 return count;
6414
6415 spin_lock(&nn->client_lock);
6416 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6417 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6418 if (max != 0 && count >= max)
6419 break;
6420 }
6421 spin_unlock(&nn->client_lock);
6422 nfsd_reap_locks(&reaplist);
6423 return count;
6424 }
6425
6426 static u64
nfsd_foreach_client_openowner(struct nfs4_client * clp,u64 max,struct list_head * collect,void (* func)(struct nfs4_openowner *))6427 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6428 struct list_head *collect,
6429 void (*func)(struct nfs4_openowner *))
6430 {
6431 struct nfs4_openowner *oop, *next;
6432 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6433 nfsd_net_id);
6434 u64 count = 0;
6435
6436 lockdep_assert_held(&nn->client_lock);
6437
6438 spin_lock(&clp->cl_lock);
6439 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6440 if (func) {
6441 func(oop);
6442 if (collect) {
6443 atomic_inc(&clp->cl_refcount);
6444 list_add(&oop->oo_perclient, collect);
6445 }
6446 }
6447 ++count;
6448 /*
6449 * Despite the fact that these functions deal with
6450 * 64-bit integers for "count", we must ensure that
6451 * it doesn't blow up the clp->cl_refcount. Throw a
6452 * warning if we start to approach INT_MAX here.
6453 */
6454 WARN_ON_ONCE(count == (INT_MAX / 2));
6455 if (count == max)
6456 break;
6457 }
6458 spin_unlock(&clp->cl_lock);
6459
6460 return count;
6461 }
6462
6463 static u64
nfsd_print_client_openowners(struct nfs4_client * clp)6464 nfsd_print_client_openowners(struct nfs4_client *clp)
6465 {
6466 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6467
6468 nfsd_print_count(clp, count, "openowners");
6469 return count;
6470 }
6471
6472 static u64
nfsd_collect_client_openowners(struct nfs4_client * clp,struct list_head * collect,u64 max)6473 nfsd_collect_client_openowners(struct nfs4_client *clp,
6474 struct list_head *collect, u64 max)
6475 {
6476 return nfsd_foreach_client_openowner(clp, max, collect,
6477 unhash_openowner_locked);
6478 }
6479
6480 u64
nfsd_inject_print_openowners(void)6481 nfsd_inject_print_openowners(void)
6482 {
6483 struct nfs4_client *clp;
6484 u64 count = 0;
6485 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6486 nfsd_net_id);
6487
6488 if (!nfsd_netns_ready(nn))
6489 return 0;
6490
6491 spin_lock(&nn->client_lock);
6492 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6493 count += nfsd_print_client_openowners(clp);
6494 spin_unlock(&nn->client_lock);
6495
6496 return count;
6497 }
6498
6499 static void
nfsd_reap_openowners(struct list_head * reaplist)6500 nfsd_reap_openowners(struct list_head *reaplist)
6501 {
6502 struct nfs4_client *clp;
6503 struct nfs4_openowner *oop, *next;
6504
6505 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6506 list_del_init(&oop->oo_perclient);
6507 clp = oop->oo_owner.so_client;
6508 release_openowner(oop);
6509 put_client(clp);
6510 }
6511 }
6512
6513 u64
nfsd_inject_forget_client_openowners(struct sockaddr_storage * addr,size_t addr_size)6514 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6515 size_t addr_size)
6516 {
6517 unsigned int count = 0;
6518 struct nfs4_client *clp;
6519 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6520 nfsd_net_id);
6521 LIST_HEAD(reaplist);
6522
6523 if (!nfsd_netns_ready(nn))
6524 return count;
6525
6526 spin_lock(&nn->client_lock);
6527 clp = nfsd_find_client(addr, addr_size);
6528 if (clp)
6529 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6530 spin_unlock(&nn->client_lock);
6531 nfsd_reap_openowners(&reaplist);
6532 return count;
6533 }
6534
6535 u64
nfsd_inject_forget_openowners(u64 max)6536 nfsd_inject_forget_openowners(u64 max)
6537 {
6538 u64 count = 0;
6539 struct nfs4_client *clp;
6540 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6541 nfsd_net_id);
6542 LIST_HEAD(reaplist);
6543
6544 if (!nfsd_netns_ready(nn))
6545 return count;
6546
6547 spin_lock(&nn->client_lock);
6548 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6549 count += nfsd_collect_client_openowners(clp, &reaplist,
6550 max - count);
6551 if (max != 0 && count >= max)
6552 break;
6553 }
6554 spin_unlock(&nn->client_lock);
6555 nfsd_reap_openowners(&reaplist);
6556 return count;
6557 }
6558
nfsd_find_all_delegations(struct nfs4_client * clp,u64 max,struct list_head * victims)6559 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6560 struct list_head *victims)
6561 {
6562 struct nfs4_delegation *dp, *next;
6563 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6564 nfsd_net_id);
6565 u64 count = 0;
6566
6567 lockdep_assert_held(&nn->client_lock);
6568
6569 spin_lock(&state_lock);
6570 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6571 if (victims) {
6572 /*
6573 * It's not safe to mess with delegations that have a
6574 * non-zero dl_time. They might have already been broken
6575 * and could be processed by the laundromat outside of
6576 * the state_lock. Just leave them be.
6577 */
6578 if (dp->dl_time != 0)
6579 continue;
6580
6581 atomic_inc(&clp->cl_refcount);
6582 WARN_ON(!unhash_delegation_locked(dp));
6583 list_add(&dp->dl_recall_lru, victims);
6584 }
6585 ++count;
6586 /*
6587 * Despite the fact that these functions deal with
6588 * 64-bit integers for "count", we must ensure that
6589 * it doesn't blow up the clp->cl_refcount. Throw a
6590 * warning if we start to approach INT_MAX here.
6591 */
6592 WARN_ON_ONCE(count == (INT_MAX / 2));
6593 if (count == max)
6594 break;
6595 }
6596 spin_unlock(&state_lock);
6597 return count;
6598 }
6599
6600 static u64
nfsd_print_client_delegations(struct nfs4_client * clp)6601 nfsd_print_client_delegations(struct nfs4_client *clp)
6602 {
6603 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6604
6605 nfsd_print_count(clp, count, "delegations");
6606 return count;
6607 }
6608
6609 u64
nfsd_inject_print_delegations(void)6610 nfsd_inject_print_delegations(void)
6611 {
6612 struct nfs4_client *clp;
6613 u64 count = 0;
6614 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6615 nfsd_net_id);
6616
6617 if (!nfsd_netns_ready(nn))
6618 return 0;
6619
6620 spin_lock(&nn->client_lock);
6621 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6622 count += nfsd_print_client_delegations(clp);
6623 spin_unlock(&nn->client_lock);
6624
6625 return count;
6626 }
6627
6628 static void
nfsd_forget_delegations(struct list_head * reaplist)6629 nfsd_forget_delegations(struct list_head *reaplist)
6630 {
6631 struct nfs4_client *clp;
6632 struct nfs4_delegation *dp, *next;
6633
6634 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6635 list_del_init(&dp->dl_recall_lru);
6636 clp = dp->dl_stid.sc_client;
6637 revoke_delegation(dp);
6638 put_client(clp);
6639 }
6640 }
6641
6642 u64
nfsd_inject_forget_client_delegations(struct sockaddr_storage * addr,size_t addr_size)6643 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6644 size_t addr_size)
6645 {
6646 u64 count = 0;
6647 struct nfs4_client *clp;
6648 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6649 nfsd_net_id);
6650 LIST_HEAD(reaplist);
6651
6652 if (!nfsd_netns_ready(nn))
6653 return count;
6654
6655 spin_lock(&nn->client_lock);
6656 clp = nfsd_find_client(addr, addr_size);
6657 if (clp)
6658 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6659 spin_unlock(&nn->client_lock);
6660
6661 nfsd_forget_delegations(&reaplist);
6662 return count;
6663 }
6664
6665 u64
nfsd_inject_forget_delegations(u64 max)6666 nfsd_inject_forget_delegations(u64 max)
6667 {
6668 u64 count = 0;
6669 struct nfs4_client *clp;
6670 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6671 nfsd_net_id);
6672 LIST_HEAD(reaplist);
6673
6674 if (!nfsd_netns_ready(nn))
6675 return count;
6676
6677 spin_lock(&nn->client_lock);
6678 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6679 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6680 if (max != 0 && count >= max)
6681 break;
6682 }
6683 spin_unlock(&nn->client_lock);
6684 nfsd_forget_delegations(&reaplist);
6685 return count;
6686 }
6687
6688 static void
nfsd_recall_delegations(struct list_head * reaplist)6689 nfsd_recall_delegations(struct list_head *reaplist)
6690 {
6691 struct nfs4_client *clp;
6692 struct nfs4_delegation *dp, *next;
6693
6694 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6695 list_del_init(&dp->dl_recall_lru);
6696 clp = dp->dl_stid.sc_client;
6697 /*
6698 * We skipped all entries that had a zero dl_time before,
6699 * so we can now reset the dl_time back to 0. If a delegation
6700 * break comes in now, then it won't make any difference since
6701 * we're recalling it either way.
6702 */
6703 spin_lock(&state_lock);
6704 dp->dl_time = 0;
6705 spin_unlock(&state_lock);
6706 nfsd_break_one_deleg(dp);
6707 put_client(clp);
6708 }
6709 }
6710
6711 u64
nfsd_inject_recall_client_delegations(struct sockaddr_storage * addr,size_t addr_size)6712 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6713 size_t addr_size)
6714 {
6715 u64 count = 0;
6716 struct nfs4_client *clp;
6717 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6718 nfsd_net_id);
6719 LIST_HEAD(reaplist);
6720
6721 if (!nfsd_netns_ready(nn))
6722 return count;
6723
6724 spin_lock(&nn->client_lock);
6725 clp = nfsd_find_client(addr, addr_size);
6726 if (clp)
6727 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6728 spin_unlock(&nn->client_lock);
6729
6730 nfsd_recall_delegations(&reaplist);
6731 return count;
6732 }
6733
6734 u64
nfsd_inject_recall_delegations(u64 max)6735 nfsd_inject_recall_delegations(u64 max)
6736 {
6737 u64 count = 0;
6738 struct nfs4_client *clp, *next;
6739 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6740 nfsd_net_id);
6741 LIST_HEAD(reaplist);
6742
6743 if (!nfsd_netns_ready(nn))
6744 return count;
6745
6746 spin_lock(&nn->client_lock);
6747 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6748 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6749 if (max != 0 && ++count >= max)
6750 break;
6751 }
6752 spin_unlock(&nn->client_lock);
6753 nfsd_recall_delegations(&reaplist);
6754 return count;
6755 }
6756 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6757
6758 /*
6759 * Since the lifetime of a delegation isn't limited to that of an open, a
6760 * client may quite reasonably hang on to a delegation as long as it has
6761 * the inode cached. This becomes an obvious problem the first time a
6762 * client's inode cache approaches the size of the server's total memory.
6763 *
6764 * For now we avoid this problem by imposing a hard limit on the number
6765 * of delegations, which varies according to the server's memory size.
6766 */
6767 static void
set_max_delegations(void)6768 set_max_delegations(void)
6769 {
6770 /*
6771 * Allow at most 4 delegations per megabyte of RAM. Quick
6772 * estimates suggest that in the worst case (where every delegation
6773 * is for a different inode), a delegation could take about 1.5K,
6774 * giving a worst case usage of about 6% of memory.
6775 */
6776 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6777 }
6778
nfs4_state_create_net(struct net * net)6779 static int nfs4_state_create_net(struct net *net)
6780 {
6781 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6782 int i;
6783
6784 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6785 CLIENT_HASH_SIZE, GFP_KERNEL);
6786 if (!nn->conf_id_hashtbl)
6787 goto err;
6788 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6789 CLIENT_HASH_SIZE, GFP_KERNEL);
6790 if (!nn->unconf_id_hashtbl)
6791 goto err_unconf_id;
6792 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6793 SESSION_HASH_SIZE, GFP_KERNEL);
6794 if (!nn->sessionid_hashtbl)
6795 goto err_sessionid;
6796
6797 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6798 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6799 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6800 }
6801 for (i = 0; i < SESSION_HASH_SIZE; i++)
6802 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6803 nn->conf_name_tree = RB_ROOT;
6804 nn->unconf_name_tree = RB_ROOT;
6805 nn->boot_time = get_seconds();
6806 nn->grace_ended = false;
6807 nn->nfsd4_manager.block_opens = true;
6808 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
6809 INIT_LIST_HEAD(&nn->client_lru);
6810 INIT_LIST_HEAD(&nn->close_lru);
6811 INIT_LIST_HEAD(&nn->del_recall_lru);
6812 spin_lock_init(&nn->client_lock);
6813
6814 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6815 get_net(net);
6816
6817 return 0;
6818
6819 err_sessionid:
6820 kfree(nn->unconf_id_hashtbl);
6821 err_unconf_id:
6822 kfree(nn->conf_id_hashtbl);
6823 err:
6824 return -ENOMEM;
6825 }
6826
6827 static void
nfs4_state_destroy_net(struct net * net)6828 nfs4_state_destroy_net(struct net *net)
6829 {
6830 int i;
6831 struct nfs4_client *clp = NULL;
6832 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6833
6834 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6835 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6836 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6837 destroy_client(clp);
6838 }
6839 }
6840
6841 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6842 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6843 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6844 destroy_client(clp);
6845 }
6846 }
6847
6848 kfree(nn->sessionid_hashtbl);
6849 kfree(nn->unconf_id_hashtbl);
6850 kfree(nn->conf_id_hashtbl);
6851 put_net(net);
6852 }
6853
6854 int
nfs4_state_start_net(struct net * net)6855 nfs4_state_start_net(struct net *net)
6856 {
6857 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6858 int ret;
6859
6860 ret = nfs4_state_create_net(net);
6861 if (ret)
6862 return ret;
6863 locks_start_grace(net, &nn->nfsd4_manager);
6864 nfsd4_client_tracking_init(net);
6865 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6866 nn->nfsd4_grace, net);
6867 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6868 return 0;
6869 }
6870
6871 /* initialization to perform when the nfsd service is started: */
6872
6873 int
nfs4_state_start(void)6874 nfs4_state_start(void)
6875 {
6876 int ret;
6877
6878 ret = set_callback_cred();
6879 if (ret)
6880 return ret;
6881
6882 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
6883 if (laundry_wq == NULL) {
6884 ret = -ENOMEM;
6885 goto out_cleanup_cred;
6886 }
6887 ret = nfsd4_create_callback_queue();
6888 if (ret)
6889 goto out_free_laundry;
6890
6891 set_max_delegations();
6892 return 0;
6893
6894 out_free_laundry:
6895 destroy_workqueue(laundry_wq);
6896 out_cleanup_cred:
6897 cleanup_callback_cred();
6898 return ret;
6899 }
6900
6901 void
nfs4_state_shutdown_net(struct net * net)6902 nfs4_state_shutdown_net(struct net *net)
6903 {
6904 struct nfs4_delegation *dp = NULL;
6905 struct list_head *pos, *next, reaplist;
6906 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6907
6908 cancel_delayed_work_sync(&nn->laundromat_work);
6909 locks_end_grace(&nn->nfsd4_manager);
6910
6911 INIT_LIST_HEAD(&reaplist);
6912 spin_lock(&state_lock);
6913 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6914 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6915 WARN_ON(!unhash_delegation_locked(dp));
6916 list_add(&dp->dl_recall_lru, &reaplist);
6917 }
6918 spin_unlock(&state_lock);
6919 list_for_each_safe(pos, next, &reaplist) {
6920 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6921 list_del_init(&dp->dl_recall_lru);
6922 put_clnt_odstate(dp->dl_clnt_odstate);
6923 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6924 nfs4_put_stid(&dp->dl_stid);
6925 }
6926
6927 nfsd4_client_tracking_exit(net);
6928 nfs4_state_destroy_net(net);
6929 }
6930
6931 void
nfs4_state_shutdown(void)6932 nfs4_state_shutdown(void)
6933 {
6934 destroy_workqueue(laundry_wq);
6935 nfsd4_destroy_callback_queue();
6936 cleanup_callback_cred();
6937 }
6938
6939 static void
get_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid)6940 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6941 {
6942 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6943 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6944 }
6945
6946 static void
put_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid)6947 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6948 {
6949 if (cstate->minorversion) {
6950 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6951 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6952 }
6953 }
6954
6955 void
clear_current_stateid(struct nfsd4_compound_state * cstate)6956 clear_current_stateid(struct nfsd4_compound_state *cstate)
6957 {
6958 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6959 }
6960
6961 /*
6962 * functions to set current state id
6963 */
6964 void
nfsd4_set_opendowngradestateid(struct nfsd4_compound_state * cstate,struct nfsd4_open_downgrade * odp)6965 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6966 {
6967 put_stateid(cstate, &odp->od_stateid);
6968 }
6969
6970 void
nfsd4_set_openstateid(struct nfsd4_compound_state * cstate,struct nfsd4_open * open)6971 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6972 {
6973 put_stateid(cstate, &open->op_stateid);
6974 }
6975
6976 void
nfsd4_set_closestateid(struct nfsd4_compound_state * cstate,struct nfsd4_close * close)6977 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6978 {
6979 put_stateid(cstate, &close->cl_stateid);
6980 }
6981
6982 void
nfsd4_set_lockstateid(struct nfsd4_compound_state * cstate,struct nfsd4_lock * lock)6983 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6984 {
6985 put_stateid(cstate, &lock->lk_resp_stateid);
6986 }
6987
6988 /*
6989 * functions to consume current state id
6990 */
6991
6992 void
nfsd4_get_opendowngradestateid(struct nfsd4_compound_state * cstate,struct nfsd4_open_downgrade * odp)6993 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6994 {
6995 get_stateid(cstate, &odp->od_stateid);
6996 }
6997
6998 void
nfsd4_get_delegreturnstateid(struct nfsd4_compound_state * cstate,struct nfsd4_delegreturn * drp)6999 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
7000 {
7001 get_stateid(cstate, &drp->dr_stateid);
7002 }
7003
7004 void
nfsd4_get_freestateid(struct nfsd4_compound_state * cstate,struct nfsd4_free_stateid * fsp)7005 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
7006 {
7007 get_stateid(cstate, &fsp->fr_stateid);
7008 }
7009
7010 void
nfsd4_get_setattrstateid(struct nfsd4_compound_state * cstate,struct nfsd4_setattr * setattr)7011 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
7012 {
7013 get_stateid(cstate, &setattr->sa_stateid);
7014 }
7015
7016 void
nfsd4_get_closestateid(struct nfsd4_compound_state * cstate,struct nfsd4_close * close)7017 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
7018 {
7019 get_stateid(cstate, &close->cl_stateid);
7020 }
7021
7022 void
nfsd4_get_lockustateid(struct nfsd4_compound_state * cstate,struct nfsd4_locku * locku)7023 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
7024 {
7025 get_stateid(cstate, &locku->lu_stateid);
7026 }
7027
7028 void
nfsd4_get_readstateid(struct nfsd4_compound_state * cstate,struct nfsd4_read * read)7029 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
7030 {
7031 get_stateid(cstate, &read->rd_stateid);
7032 }
7033
7034 void
nfsd4_get_writestateid(struct nfsd4_compound_state * cstate,struct nfsd4_write * write)7035 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
7036 {
7037 get_stateid(cstate, &write->wr_stateid);
7038 }
7039