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 <linux/string_helpers.h>
46 #include <linux/fsnotify.h>
47 #include <linux/rhashtable.h>
48 #include <linux/nfs_ssc.h>
49
50 #include "xdr4.h"
51 #include "xdr4cb.h"
52 #include "vfs.h"
53 #include "current_stateid.h"
54
55 #include "netns.h"
56 #include "pnfs.h"
57 #include "filecache.h"
58 #include "trace.h"
59
60 #define NFSDDBG_FACILITY NFSDDBG_PROC
61
62 #define all_ones {{ ~0, ~0}, ~0}
63 static const stateid_t one_stateid = {
64 .si_generation = ~0,
65 .si_opaque = all_ones,
66 };
67 static const stateid_t zero_stateid = {
68 /* all fields zero */
69 };
70 static const stateid_t currentstateid = {
71 .si_generation = 1,
72 };
73 static const stateid_t close_stateid = {
74 .si_generation = 0xffffffffU,
75 };
76
77 static u64 current_sessionid = 1;
78
79 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
80 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
81 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
82 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
83
84 /* forward declarations */
85 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
86 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
87 static void nfsd4_end_grace(struct nfsd_net *nn);
88 static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
89 static void nfsd4_file_hash_remove(struct nfs4_file *fi);
90 static void deleg_reaper(struct nfsd_net *nn);
91
92 /* Locking: */
93
94 /*
95 * Currently used for the del_recall_lru and file hash table. In an
96 * effort to decrease the scope of the client_mutex, this spinlock may
97 * eventually cover more:
98 */
99 static DEFINE_SPINLOCK(state_lock);
100
101 enum nfsd4_st_mutex_lock_subclass {
102 OPEN_STATEID_MUTEX = 0,
103 LOCK_STATEID_MUTEX = 1,
104 };
105
106 /*
107 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
108 * the refcount on the open stateid to drop.
109 */
110 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
111
112 /*
113 * A waitqueue where a writer to clients/#/ctl destroying a client can
114 * wait for cl_rpc_users to drop to 0 and then for the client to be
115 * unhashed.
116 */
117 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
118
119 static struct kmem_cache *client_slab;
120 static struct kmem_cache *openowner_slab;
121 static struct kmem_cache *lockowner_slab;
122 static struct kmem_cache *file_slab;
123 static struct kmem_cache *stateid_slab;
124 static struct kmem_cache *deleg_slab;
125 static struct kmem_cache *odstate_slab;
126
127 static void free_session(struct nfsd4_session *);
128
129 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
130 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
131 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops;
132
133 static struct workqueue_struct *laundry_wq;
134
nfsd4_create_laundry_wq(void)135 int nfsd4_create_laundry_wq(void)
136 {
137 int rc = 0;
138
139 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
140 if (laundry_wq == NULL)
141 rc = -ENOMEM;
142 return rc;
143 }
144
nfsd4_destroy_laundry_wq(void)145 void nfsd4_destroy_laundry_wq(void)
146 {
147 destroy_workqueue(laundry_wq);
148 }
149
is_session_dead(struct nfsd4_session * ses)150 static bool is_session_dead(struct nfsd4_session *ses)
151 {
152 return ses->se_dead;
153 }
154
mark_session_dead_locked(struct nfsd4_session * ses,int ref_held_by_me)155 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
156 {
157 if (atomic_read(&ses->se_ref) > ref_held_by_me)
158 return nfserr_jukebox;
159 ses->se_dead = true;
160 return nfs_ok;
161 }
162
is_client_expired(struct nfs4_client * clp)163 static bool is_client_expired(struct nfs4_client *clp)
164 {
165 return clp->cl_time == 0;
166 }
167
nfsd4_dec_courtesy_client_count(struct nfsd_net * nn,struct nfs4_client * clp)168 static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn,
169 struct nfs4_client *clp)
170 {
171 if (clp->cl_state != NFSD4_ACTIVE)
172 atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0);
173 }
174
get_client_locked(struct nfs4_client * clp)175 static __be32 get_client_locked(struct nfs4_client *clp)
176 {
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178
179 lockdep_assert_held(&nn->client_lock);
180
181 if (is_client_expired(clp))
182 return nfserr_expired;
183 atomic_inc(&clp->cl_rpc_users);
184 nfsd4_dec_courtesy_client_count(nn, clp);
185 clp->cl_state = NFSD4_ACTIVE;
186 return nfs_ok;
187 }
188
189 /* must be called under the client_lock */
190 static inline void
renew_client_locked(struct nfs4_client * clp)191 renew_client_locked(struct nfs4_client *clp)
192 {
193 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
194
195 if (is_client_expired(clp)) {
196 WARN_ON(1);
197 printk("%s: client (clientid %08x/%08x) already expired\n",
198 __func__,
199 clp->cl_clientid.cl_boot,
200 clp->cl_clientid.cl_id);
201 return;
202 }
203
204 list_move_tail(&clp->cl_lru, &nn->client_lru);
205 clp->cl_time = ktime_get_boottime_seconds();
206 nfsd4_dec_courtesy_client_count(nn, clp);
207 clp->cl_state = NFSD4_ACTIVE;
208 }
209
put_client_renew_locked(struct nfs4_client * clp)210 static void put_client_renew_locked(struct nfs4_client *clp)
211 {
212 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
213
214 lockdep_assert_held(&nn->client_lock);
215
216 if (!atomic_dec_and_test(&clp->cl_rpc_users))
217 return;
218 if (!is_client_expired(clp))
219 renew_client_locked(clp);
220 else
221 wake_up_all(&expiry_wq);
222 }
223
put_client_renew(struct nfs4_client * clp)224 static void put_client_renew(struct nfs4_client *clp)
225 {
226 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
227
228 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
229 return;
230 if (!is_client_expired(clp))
231 renew_client_locked(clp);
232 else
233 wake_up_all(&expiry_wq);
234 spin_unlock(&nn->client_lock);
235 }
236
nfsd4_get_session_locked(struct nfsd4_session * ses)237 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
238 {
239 __be32 status;
240
241 if (is_session_dead(ses))
242 return nfserr_badsession;
243 status = get_client_locked(ses->se_client);
244 if (status)
245 return status;
246 atomic_inc(&ses->se_ref);
247 return nfs_ok;
248 }
249
nfsd4_put_session_locked(struct nfsd4_session * ses)250 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
251 {
252 struct nfs4_client *clp = ses->se_client;
253 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
254
255 lockdep_assert_held(&nn->client_lock);
256
257 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
258 free_session(ses);
259 put_client_renew_locked(clp);
260 }
261
nfsd4_put_session(struct nfsd4_session * ses)262 static void nfsd4_put_session(struct nfsd4_session *ses)
263 {
264 struct nfs4_client *clp = ses->se_client;
265 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
266
267 spin_lock(&nn->client_lock);
268 nfsd4_put_session_locked(ses);
269 spin_unlock(&nn->client_lock);
270 }
271
272 static struct nfsd4_blocked_lock *
find_blocked_lock(struct nfs4_lockowner * lo,struct knfsd_fh * fh,struct nfsd_net * nn)273 find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
274 struct nfsd_net *nn)
275 {
276 struct nfsd4_blocked_lock *cur, *found = NULL;
277
278 spin_lock(&nn->blocked_locks_lock);
279 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
280 if (fh_match(fh, &cur->nbl_fh)) {
281 list_del_init(&cur->nbl_list);
282 WARN_ON(list_empty(&cur->nbl_lru));
283 list_del_init(&cur->nbl_lru);
284 found = cur;
285 break;
286 }
287 }
288 spin_unlock(&nn->blocked_locks_lock);
289 if (found)
290 locks_delete_block(&found->nbl_lock);
291 return found;
292 }
293
294 static struct nfsd4_blocked_lock *
find_or_allocate_block(struct nfs4_lockowner * lo,struct knfsd_fh * fh,struct nfsd_net * nn)295 find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
296 struct nfsd_net *nn)
297 {
298 struct nfsd4_blocked_lock *nbl;
299
300 nbl = find_blocked_lock(lo, fh, nn);
301 if (!nbl) {
302 nbl = kmalloc_obj(*nbl);
303 if (nbl) {
304 INIT_LIST_HEAD(&nbl->nbl_list);
305 INIT_LIST_HEAD(&nbl->nbl_lru);
306 fh_copy_shallow(&nbl->nbl_fh, fh);
307 locks_init_lock(&nbl->nbl_lock);
308 kref_init(&nbl->nbl_kref);
309 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
310 &nfsd4_cb_notify_lock_ops,
311 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
312 }
313 }
314 return nbl;
315 }
316
317 static void
free_nbl(struct kref * kref)318 free_nbl(struct kref *kref)
319 {
320 struct nfsd4_blocked_lock *nbl;
321
322 nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref);
323 locks_release_private(&nbl->nbl_lock);
324 kfree(nbl);
325 }
326
327 static void
free_blocked_lock(struct nfsd4_blocked_lock * nbl)328 free_blocked_lock(struct nfsd4_blocked_lock *nbl)
329 {
330 locks_delete_block(&nbl->nbl_lock);
331 kref_put(&nbl->nbl_kref, free_nbl);
332 }
333
334 static void
remove_blocked_locks(struct nfs4_lockowner * lo)335 remove_blocked_locks(struct nfs4_lockowner *lo)
336 {
337 struct nfs4_client *clp = lo->lo_owner.so_client;
338 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
339 struct nfsd4_blocked_lock *nbl;
340 LIST_HEAD(reaplist);
341
342 /* Dequeue all blocked locks */
343 spin_lock(&nn->blocked_locks_lock);
344 while (!list_empty(&lo->lo_blocked)) {
345 nbl = list_first_entry(&lo->lo_blocked,
346 struct nfsd4_blocked_lock,
347 nbl_list);
348 list_del_init(&nbl->nbl_list);
349 WARN_ON(list_empty(&nbl->nbl_lru));
350 list_move(&nbl->nbl_lru, &reaplist);
351 }
352 spin_unlock(&nn->blocked_locks_lock);
353
354 /* Now free them */
355 while (!list_empty(&reaplist)) {
356 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
357 nbl_lru);
358 list_del_init(&nbl->nbl_lru);
359 free_blocked_lock(nbl);
360 }
361 }
362
363 static void
nfsd4_cb_notify_lock_prepare(struct nfsd4_callback * cb)364 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
365 {
366 struct nfsd4_blocked_lock *nbl = container_of(cb,
367 struct nfsd4_blocked_lock, nbl_cb);
368 locks_delete_block(&nbl->nbl_lock);
369 }
370
371 static int
nfsd4_cb_notify_lock_done(struct nfsd4_callback * cb,struct rpc_task * task)372 nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
373 {
374 trace_nfsd_cb_notify_lock_done(&zero_stateid, task);
375
376 /*
377 * Since this is just an optimization, we don't try very hard if it
378 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
379 * just quit trying on anything else.
380 */
381 switch (task->tk_status) {
382 case -NFS4ERR_DELAY:
383 rpc_delay(task, 1 * HZ);
384 return 0;
385 default:
386 return 1;
387 }
388 }
389
390 static void
nfsd4_cb_notify_lock_release(struct nfsd4_callback * cb)391 nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
392 {
393 struct nfsd4_blocked_lock *nbl = container_of(cb,
394 struct nfsd4_blocked_lock, nbl_cb);
395
396 free_blocked_lock(nbl);
397 }
398
399 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
400 .prepare = nfsd4_cb_notify_lock_prepare,
401 .done = nfsd4_cb_notify_lock_done,
402 .release = nfsd4_cb_notify_lock_release,
403 .opcode = OP_CB_NOTIFY_LOCK,
404 };
405
406 /*
407 * We store the NONE, READ, WRITE, and BOTH bits separately in the
408 * st_{access,deny}_bmap field of the stateid, in order to track not
409 * only what share bits are currently in force, but also what
410 * combinations of share bits previous opens have used. This allows us
411 * to enforce the recommendation in
412 * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that
413 * the server return an error if the client attempt to downgrade to a
414 * combination of share bits not explicable by closing some of its
415 * previous opens.
416 *
417 * This enforcement is arguably incomplete, since we don't keep
418 * track of access/deny bit combinations; so, e.g., we allow:
419 *
420 * OPEN allow read, deny write
421 * OPEN allow both, deny none
422 * DOWNGRADE allow read, deny none
423 *
424 * which we should reject.
425 *
426 * But you could also argue that our current code is already overkill,
427 * since it only exists to return NFS4ERR_INVAL on incorrect client
428 * behavior.
429 */
430 static unsigned int
bmap_to_share_mode(unsigned long bmap)431 bmap_to_share_mode(unsigned long bmap)
432 {
433 int i;
434 unsigned int access = 0;
435
436 for (i = 1; i < 4; i++) {
437 if (test_bit(i, &bmap))
438 access |= i;
439 }
440 return access;
441 }
442
443 /* set share access for a given stateid */
444 static inline void
set_access(u32 access,struct nfs4_ol_stateid * stp)445 set_access(u32 access, struct nfs4_ol_stateid *stp)
446 {
447 unsigned char mask = 1 << access;
448
449 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
450 stp->st_access_bmap |= mask;
451 }
452
453 /* clear share access for a given stateid */
454 static inline void
clear_access(u32 access,struct nfs4_ol_stateid * stp)455 clear_access(u32 access, struct nfs4_ol_stateid *stp)
456 {
457 unsigned char mask = 1 << access;
458
459 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
460 stp->st_access_bmap &= ~mask;
461 }
462
463 /* test whether a given stateid has access */
464 static inline bool
test_access(u32 access,struct nfs4_ol_stateid * stp)465 test_access(u32 access, struct nfs4_ol_stateid *stp)
466 {
467 unsigned char mask = 1 << access;
468
469 return (bool)(stp->st_access_bmap & mask);
470 }
471
472 /* set share deny for a given stateid */
473 static inline void
set_deny(u32 deny,struct nfs4_ol_stateid * stp)474 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
475 {
476 unsigned char mask = 1 << deny;
477
478 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
479 stp->st_deny_bmap |= mask;
480 }
481
482 /* clear share deny for a given stateid */
483 static inline void
clear_deny(u32 deny,struct nfs4_ol_stateid * stp)484 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
485 {
486 unsigned char mask = 1 << deny;
487
488 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
489 stp->st_deny_bmap &= ~mask;
490 }
491
492 /* test whether a given stateid is denying specific access */
493 static inline bool
test_deny(u32 deny,struct nfs4_ol_stateid * stp)494 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
495 {
496 unsigned char mask = 1 << deny;
497
498 return (bool)(stp->st_deny_bmap & mask);
499 }
500
nfs4_access_to_omode(u32 access)501 static int nfs4_access_to_omode(u32 access)
502 {
503 switch (access & NFS4_SHARE_ACCESS_BOTH) {
504 case NFS4_SHARE_ACCESS_READ:
505 return O_RDONLY;
506 case NFS4_SHARE_ACCESS_WRITE:
507 return O_WRONLY;
508 case NFS4_SHARE_ACCESS_BOTH:
509 return O_RDWR;
510 }
511 WARN_ON_ONCE(1);
512 return O_RDONLY;
513 }
514
515 static inline int
access_permit_read(struct nfs4_ol_stateid * stp)516 access_permit_read(struct nfs4_ol_stateid *stp)
517 {
518 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
519 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
520 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
521 }
522
523 static inline int
access_permit_write(struct nfs4_ol_stateid * stp)524 access_permit_write(struct nfs4_ol_stateid *stp)
525 {
526 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
527 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
528 }
529
530 static inline struct nfs4_stateowner *
nfs4_get_stateowner(struct nfs4_stateowner * sop)531 nfs4_get_stateowner(struct nfs4_stateowner *sop)
532 {
533 atomic_inc(&sop->so_count);
534 return sop;
535 }
536
537 static int
same_owner_str(struct nfs4_stateowner * sop,struct xdr_netobj * owner)538 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
539 {
540 return (sop->so_owner.len == owner->len) &&
541 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
542 }
543
544 static struct nfs4_openowner *
find_openstateowner_str(unsigned int hashval,struct nfsd4_open * open,struct nfs4_client * clp)545 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
546 struct nfs4_client *clp)
547 {
548 struct nfs4_stateowner *so;
549
550 lockdep_assert_held(&clp->cl_lock);
551
552 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
553 so_strhash) {
554 if (!so->so_is_open_owner)
555 continue;
556 if (same_owner_str(so, &open->op_owner))
557 return openowner(nfs4_get_stateowner(so));
558 }
559 return NULL;
560 }
561
562 static inline u32
opaque_hashval(const void * ptr,int nbytes)563 opaque_hashval(const void *ptr, int nbytes)
564 {
565 unsigned char *cptr = (unsigned char *) ptr;
566
567 u32 x = 0;
568 while (nbytes--) {
569 x *= 37;
570 x += *cptr++;
571 }
572 return x;
573 }
574
575 void
put_nfs4_file(struct nfs4_file * fi)576 put_nfs4_file(struct nfs4_file *fi)
577 {
578 if (refcount_dec_and_test(&fi->fi_ref)) {
579 nfsd4_file_hash_remove(fi);
580 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
581 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
582 kfree_rcu(fi, fi_rcu);
583 }
584 }
585
586 static struct nfsd_file *
find_writeable_file_locked(struct nfs4_file * f)587 find_writeable_file_locked(struct nfs4_file *f)
588 {
589 struct nfsd_file *ret;
590
591 lockdep_assert_held(&f->fi_lock);
592
593 ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
594 if (!ret)
595 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
596 return ret;
597 }
598
599 static struct nfsd_file *
find_writeable_file(struct nfs4_file * f)600 find_writeable_file(struct nfs4_file *f)
601 {
602 struct nfsd_file *ret;
603
604 spin_lock(&f->fi_lock);
605 ret = find_writeable_file_locked(f);
606 spin_unlock(&f->fi_lock);
607
608 return ret;
609 }
610
611 static struct nfsd_file *
find_readable_file_locked(struct nfs4_file * f)612 find_readable_file_locked(struct nfs4_file *f)
613 {
614 struct nfsd_file *ret;
615
616 lockdep_assert_held(&f->fi_lock);
617
618 ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
619 if (!ret)
620 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
621 return ret;
622 }
623
624 static struct nfsd_file *
find_readable_file(struct nfs4_file * f)625 find_readable_file(struct nfs4_file *f)
626 {
627 struct nfsd_file *ret;
628
629 spin_lock(&f->fi_lock);
630 ret = find_readable_file_locked(f);
631 spin_unlock(&f->fi_lock);
632
633 return ret;
634 }
635
636 struct nfsd_file *
find_any_file(struct nfs4_file * f)637 find_any_file(struct nfs4_file *f)
638 {
639 struct nfsd_file *ret;
640
641 if (!f)
642 return NULL;
643 spin_lock(&f->fi_lock);
644 ret = nfsd_file_get(f->fi_fds[O_RDWR]);
645 if (!ret) {
646 ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
647 if (!ret)
648 ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
649 }
650 spin_unlock(&f->fi_lock);
651 return ret;
652 }
653
find_any_file_locked(struct nfs4_file * f)654 static struct nfsd_file *find_any_file_locked(struct nfs4_file *f)
655 {
656 lockdep_assert_held(&f->fi_lock);
657
658 if (f->fi_fds[O_RDWR])
659 return f->fi_fds[O_RDWR];
660 if (f->fi_fds[O_WRONLY])
661 return f->fi_fds[O_WRONLY];
662 if (f->fi_fds[O_RDONLY])
663 return f->fi_fds[O_RDONLY];
664 return NULL;
665 }
666
667 static atomic_long_t num_delegations;
668 unsigned long max_delegations;
669
670 /*
671 * Open owner state (share locks)
672 */
673
674 /* hash tables for lock and open owners */
675 #define OWNER_HASH_BITS 8
676 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
677 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
678
ownerstr_hashval(struct xdr_netobj * ownername)679 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
680 {
681 unsigned int ret;
682
683 ret = opaque_hashval(ownername->data, ownername->len);
684 return ret & OWNER_HASH_MASK;
685 }
686
687 static struct rhltable nfs4_file_rhltable ____cacheline_aligned_in_smp;
688
689 static const struct rhashtable_params nfs4_file_rhash_params = {
690 .key_len = sizeof_field(struct nfs4_file, fi_inode),
691 .key_offset = offsetof(struct nfs4_file, fi_inode),
692 .head_offset = offsetof(struct nfs4_file, fi_rlist),
693
694 /*
695 * Start with a single page hash table to reduce resizing churn
696 * on light workloads.
697 */
698 .min_size = 256,
699 .automatic_shrinking = true,
700 };
701
702 /*
703 * Check if courtesy clients have conflicting access and resolve it if possible
704 *
705 * access: is op_share_access if share_access is true.
706 * Check if access mode, op_share_access, would conflict with
707 * the current deny mode of the file 'fp'.
708 * access: is op_share_deny if share_access is false.
709 * Check if the deny mode, op_share_deny, would conflict with
710 * current access of the file 'fp'.
711 * stp: skip checking this entry.
712 * new_stp: normal open, not open upgrade.
713 *
714 * Function returns:
715 * false - access/deny mode conflict with normal client.
716 * true - no conflict or conflict with courtesy client(s) is resolved.
717 */
718 static bool
nfs4_resolve_deny_conflicts_locked(struct nfs4_file * fp,bool new_stp,struct nfs4_ol_stateid * stp,u32 access,bool share_access)719 nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
720 struct nfs4_ol_stateid *stp, u32 access, bool share_access)
721 {
722 struct nfs4_ol_stateid *st;
723 bool resolvable = true;
724 unsigned char bmap;
725 struct nfsd_net *nn;
726 struct nfs4_client *clp;
727
728 lockdep_assert_held(&fp->fi_lock);
729 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
730 /* ignore lock stateid */
731 if (st->st_openstp)
732 continue;
733 if (st == stp && new_stp)
734 continue;
735 /* check file access against deny mode or vice versa */
736 bmap = share_access ? st->st_deny_bmap : st->st_access_bmap;
737 if (!(access & bmap_to_share_mode(bmap)))
738 continue;
739 clp = st->st_stid.sc_client;
740 if (try_to_expire_client(clp))
741 continue;
742 resolvable = false;
743 break;
744 }
745 if (resolvable) {
746 clp = stp->st_stid.sc_client;
747 nn = net_generic(clp->net, nfsd_net_id);
748 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
749 }
750 return resolvable;
751 }
752
753 static void
__nfs4_file_get_access(struct nfs4_file * fp,u32 access)754 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
755 {
756 lockdep_assert_held(&fp->fi_lock);
757
758 if (access & NFS4_SHARE_ACCESS_WRITE)
759 atomic_inc(&fp->fi_access[O_WRONLY]);
760 if (access & NFS4_SHARE_ACCESS_READ)
761 atomic_inc(&fp->fi_access[O_RDONLY]);
762 }
763
764 static __be32
nfs4_file_get_access(struct nfs4_file * fp,u32 access)765 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
766 {
767 lockdep_assert_held(&fp->fi_lock);
768
769 /* Does this access mode make sense? */
770 if (access & ~NFS4_SHARE_ACCESS_BOTH)
771 return nfserr_inval;
772
773 /* Does it conflict with a deny mode already set? */
774 if ((access & fp->fi_share_deny) != 0)
775 return nfserr_share_denied;
776
777 __nfs4_file_get_access(fp, access);
778 return nfs_ok;
779 }
780
nfs4_file_check_deny(struct nfs4_file * fp,u32 deny)781 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
782 {
783 /* Common case is that there is no deny mode. */
784 if (deny) {
785 /* Does this deny mode make sense? */
786 if (deny & ~NFS4_SHARE_DENY_BOTH)
787 return nfserr_inval;
788
789 if ((deny & NFS4_SHARE_DENY_READ) &&
790 atomic_read(&fp->fi_access[O_RDONLY]))
791 return nfserr_share_denied;
792
793 if ((deny & NFS4_SHARE_DENY_WRITE) &&
794 atomic_read(&fp->fi_access[O_WRONLY]))
795 return nfserr_share_denied;
796 }
797 return nfs_ok;
798 }
799
__nfs4_file_put_access(struct nfs4_file * fp,int oflag)800 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
801 {
802 might_lock(&fp->fi_lock);
803
804 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
805 struct nfsd_file *f1 = NULL;
806 struct nfsd_file *f2 = NULL;
807
808 swap(f1, fp->fi_fds[oflag]);
809 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
810 swap(f2, fp->fi_fds[O_RDWR]);
811 spin_unlock(&fp->fi_lock);
812 if (f1)
813 nfsd_file_put(f1);
814 if (f2)
815 nfsd_file_put(f2);
816 }
817 }
818
nfs4_file_put_access(struct nfs4_file * fp,u32 access)819 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
820 {
821 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
822
823 if (access & NFS4_SHARE_ACCESS_WRITE)
824 __nfs4_file_put_access(fp, O_WRONLY);
825 if (access & NFS4_SHARE_ACCESS_READ)
826 __nfs4_file_put_access(fp, O_RDONLY);
827 }
828
829 /*
830 * Allocate a new open/delegation state counter. This is needed for
831 * pNFS for proper return on close semantics.
832 *
833 * Note that we only allocate it for pNFS-enabled exports, otherwise
834 * all pointers to struct nfs4_clnt_odstate are always NULL.
835 */
836 static struct nfs4_clnt_odstate *
alloc_clnt_odstate(struct nfs4_client * clp)837 alloc_clnt_odstate(struct nfs4_client *clp)
838 {
839 struct nfs4_clnt_odstate *co;
840
841 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
842 if (co) {
843 co->co_client = clp;
844 refcount_set(&co->co_odcount, 1);
845 }
846 return co;
847 }
848
849 static void
hash_clnt_odstate_locked(struct nfs4_clnt_odstate * co)850 hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
851 {
852 struct nfs4_file *fp = co->co_file;
853
854 lockdep_assert_held(&fp->fi_lock);
855 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
856 }
857
858 static inline void
get_clnt_odstate(struct nfs4_clnt_odstate * co)859 get_clnt_odstate(struct nfs4_clnt_odstate *co)
860 {
861 if (co)
862 refcount_inc(&co->co_odcount);
863 }
864
865 static void
put_clnt_odstate(struct nfs4_clnt_odstate * co)866 put_clnt_odstate(struct nfs4_clnt_odstate *co)
867 {
868 struct nfs4_file *fp;
869
870 if (!co)
871 return;
872
873 fp = co->co_file;
874 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
875 list_del(&co->co_perfile);
876 spin_unlock(&fp->fi_lock);
877
878 nfsd4_return_all_file_layouts(co->co_client, fp);
879 kmem_cache_free(odstate_slab, co);
880 }
881 }
882
883 static struct nfs4_clnt_odstate *
find_or_hash_clnt_odstate(struct nfs4_file * fp,struct nfs4_clnt_odstate * new)884 find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
885 {
886 struct nfs4_clnt_odstate *co;
887 struct nfs4_client *cl;
888
889 if (!new)
890 return NULL;
891
892 cl = new->co_client;
893
894 spin_lock(&fp->fi_lock);
895 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
896 if (co->co_client == cl) {
897 get_clnt_odstate(co);
898 goto out;
899 }
900 }
901 co = new;
902 co->co_file = fp;
903 hash_clnt_odstate_locked(new);
904 out:
905 spin_unlock(&fp->fi_lock);
906 return co;
907 }
908
nfs4_alloc_stid(struct nfs4_client * cl,struct kmem_cache * slab,void (* sc_free)(struct nfs4_stid *))909 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
910 void (*sc_free)(struct nfs4_stid *))
911 {
912 struct nfs4_stid *stid;
913 int new_id;
914
915 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
916 if (!stid)
917 return NULL;
918
919 idr_preload(GFP_KERNEL);
920 spin_lock(&cl->cl_lock);
921 /* Reserving 0 for start of file in nfsdfs "states" file: */
922 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
923 spin_unlock(&cl->cl_lock);
924 idr_preload_end();
925 if (new_id < 0)
926 goto out_free;
927
928 stid->sc_free = sc_free;
929 stid->sc_client = cl;
930 stid->sc_stateid.si_opaque.so_id = new_id;
931 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
932 /* Will be incremented before return to client: */
933 refcount_set(&stid->sc_count, 1);
934 spin_lock_init(&stid->sc_lock);
935 INIT_LIST_HEAD(&stid->sc_cp_list);
936
937 return stid;
938 out_free:
939 kmem_cache_free(slab, stid);
940 return NULL;
941 }
942
943 /*
944 * Create a unique stateid_t to represent each COPY.
945 */
nfs4_init_cp_state(struct nfsd_net * nn,copy_stateid_t * stid,unsigned char cs_type)946 static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
947 unsigned char cs_type)
948 {
949 int new_id;
950
951 stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
952 stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
953
954 idr_preload(GFP_KERNEL);
955 spin_lock(&nn->s2s_cp_lock);
956 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
957 stid->cs_stid.si_opaque.so_id = new_id;
958 stid->cs_stid.si_generation = 1;
959 spin_unlock(&nn->s2s_cp_lock);
960 idr_preload_end();
961 if (new_id < 0)
962 return 0;
963 stid->cs_type = cs_type;
964 return 1;
965 }
966
nfs4_init_copy_state(struct nfsd_net * nn,struct nfsd4_copy * copy)967 int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
968 {
969 return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID);
970 }
971
nfs4_alloc_init_cpntf_state(struct nfsd_net * nn,struct nfs4_stid * p_stid)972 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
973 struct nfs4_stid *p_stid)
974 {
975 struct nfs4_cpntf_state *cps;
976
977 cps = kzalloc_obj(struct nfs4_cpntf_state);
978 if (!cps)
979 return NULL;
980 cps->cpntf_time = ktime_get_boottime_seconds();
981 refcount_set(&cps->cp_stateid.cs_count, 1);
982 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
983 goto out_free;
984 spin_lock(&nn->s2s_cp_lock);
985 list_add(&cps->cp_list, &p_stid->sc_cp_list);
986 spin_unlock(&nn->s2s_cp_lock);
987 return cps;
988 out_free:
989 kfree(cps);
990 return NULL;
991 }
992
nfs4_free_copy_state(struct nfsd4_copy * copy)993 void nfs4_free_copy_state(struct nfsd4_copy *copy)
994 {
995 struct nfsd_net *nn;
996
997 if (copy->cp_stateid.cs_type != NFS4_COPY_STID)
998 return;
999 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
1000 spin_lock(&nn->s2s_cp_lock);
1001 idr_remove(&nn->s2s_cp_stateids,
1002 copy->cp_stateid.cs_stid.si_opaque.so_id);
1003 spin_unlock(&nn->s2s_cp_lock);
1004 }
1005
nfs4_free_cpntf_statelist(struct net * net,struct nfs4_stid * stid)1006 static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
1007 {
1008 struct nfs4_cpntf_state *cps;
1009 struct nfsd_net *nn;
1010
1011 nn = net_generic(net, nfsd_net_id);
1012 spin_lock(&nn->s2s_cp_lock);
1013 while (!list_empty(&stid->sc_cp_list)) {
1014 cps = list_first_entry(&stid->sc_cp_list,
1015 struct nfs4_cpntf_state, cp_list);
1016 _free_cpntf_state_locked(nn, cps);
1017 }
1018 spin_unlock(&nn->s2s_cp_lock);
1019 }
1020
nfs4_alloc_open_stateid(struct nfs4_client * clp)1021 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
1022 {
1023 struct nfs4_stid *stid;
1024
1025 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
1026 if (!stid)
1027 return NULL;
1028
1029 return openlockstateid(stid);
1030 }
1031
1032 /*
1033 * As the sc_free callback of deleg, this may be called by nfs4_put_stid
1034 * in nfsd_break_one_deleg.
1035 * Considering nfsd_break_one_deleg is called with the flc->flc_lock held,
1036 * this function mustn't ever sleep.
1037 */
nfs4_free_deleg(struct nfs4_stid * stid)1038 static void nfs4_free_deleg(struct nfs4_stid *stid)
1039 {
1040 struct nfs4_delegation *dp = delegstateid(stid);
1041
1042 WARN_ON_ONCE(!list_empty(&stid->sc_cp_list));
1043 WARN_ON_ONCE(!list_empty(&dp->dl_perfile));
1044 WARN_ON_ONCE(!list_empty(&dp->dl_perclnt));
1045 WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru));
1046 kmem_cache_free(deleg_slab, stid);
1047 atomic_long_dec(&num_delegations);
1048 }
1049
1050 /*
1051 * When we recall a delegation, we should be careful not to hand it
1052 * out again straight away.
1053 * To ensure this we keep a pair of bloom filters ('new' and 'old')
1054 * in which the filehandles of recalled delegations are "stored".
1055 * If a filehandle appear in either filter, a delegation is blocked.
1056 * When a delegation is recalled, the filehandle is stored in the "new"
1057 * filter.
1058 * Every 30 seconds we swap the filters and clear the "new" one,
1059 * unless both are empty of course. This results in delegations for a
1060 * given filehandle being blocked for between 30 and 60 seconds.
1061 *
1062 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
1063 * low 3 bytes as hash-table indices.
1064 *
1065 * 'blocked_delegations_lock', which is always taken in block_delegations(),
1066 * is used to manage concurrent access. Testing does not need the lock
1067 * except when swapping the two filters.
1068 */
1069 static DEFINE_SPINLOCK(blocked_delegations_lock);
1070 static struct bloom_pair {
1071 int entries, old_entries;
1072 time64_t swap_time;
1073 int new; /* index into 'set' */
1074 DECLARE_BITMAP(set[2], 256);
1075 } blocked_delegations;
1076
delegation_blocked(struct knfsd_fh * fh)1077 static int delegation_blocked(struct knfsd_fh *fh)
1078 {
1079 u32 hash;
1080 struct bloom_pair *bd = &blocked_delegations;
1081
1082 if (bd->entries == 0)
1083 return 0;
1084 if (ktime_get_seconds() - bd->swap_time > 30) {
1085 spin_lock(&blocked_delegations_lock);
1086 if (ktime_get_seconds() - bd->swap_time > 30) {
1087 bd->entries -= bd->old_entries;
1088 bd->old_entries = bd->entries;
1089 bd->new = 1-bd->new;
1090 memset(bd->set[bd->new], 0,
1091 sizeof(bd->set[0]));
1092 bd->swap_time = ktime_get_seconds();
1093 }
1094 spin_unlock(&blocked_delegations_lock);
1095 }
1096 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1097 if (test_bit(hash&255, bd->set[0]) &&
1098 test_bit((hash>>8)&255, bd->set[0]) &&
1099 test_bit((hash>>16)&255, bd->set[0]))
1100 return 1;
1101
1102 if (test_bit(hash&255, bd->set[1]) &&
1103 test_bit((hash>>8)&255, bd->set[1]) &&
1104 test_bit((hash>>16)&255, bd->set[1]))
1105 return 1;
1106
1107 return 0;
1108 }
1109
block_delegations(struct knfsd_fh * fh)1110 static void block_delegations(struct knfsd_fh *fh)
1111 {
1112 u32 hash;
1113 struct bloom_pair *bd = &blocked_delegations;
1114
1115 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
1116
1117 spin_lock(&blocked_delegations_lock);
1118 __set_bit(hash&255, bd->set[bd->new]);
1119 __set_bit((hash>>8)&255, bd->set[bd->new]);
1120 __set_bit((hash>>16)&255, bd->set[bd->new]);
1121 if (bd->entries == 0)
1122 bd->swap_time = ktime_get_seconds();
1123 bd->entries += 1;
1124 spin_unlock(&blocked_delegations_lock);
1125 }
1126
1127 static struct nfs4_delegation *
alloc_init_deleg(struct nfs4_client * clp,struct nfs4_file * fp,struct nfs4_clnt_odstate * odstate,u32 dl_type)1128 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
1129 struct nfs4_clnt_odstate *odstate, u32 dl_type)
1130 {
1131 struct nfs4_delegation *dp;
1132 struct nfs4_stid *stid;
1133 long n;
1134
1135 dprintk("NFSD alloc_init_deleg\n");
1136 n = atomic_long_inc_return(&num_delegations);
1137 if (n < 0 || n > max_delegations)
1138 goto out_dec;
1139 if (delegation_blocked(&fp->fi_fhandle))
1140 goto out_dec;
1141 stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg);
1142 if (stid == NULL)
1143 goto out_dec;
1144 dp = delegstateid(stid);
1145
1146 /*
1147 * delegation seqid's are never incremented. The 4.1 special
1148 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1149 * 0 anyway just for consistency and use 1:
1150 */
1151 dp->dl_stid.sc_stateid.si_generation = 1;
1152 INIT_LIST_HEAD(&dp->dl_perfile);
1153 INIT_LIST_HEAD(&dp->dl_perclnt);
1154 INIT_LIST_HEAD(&dp->dl_recall_lru);
1155 dp->dl_clnt_odstate = odstate;
1156 get_clnt_odstate(odstate);
1157 dp->dl_type = dl_type;
1158 dp->dl_retries = 1;
1159 dp->dl_recalled = false;
1160 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
1161 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
1162 nfsd4_init_cb(&dp->dl_cb_fattr.ncf_getattr, dp->dl_stid.sc_client,
1163 &nfsd4_cb_getattr_ops, NFSPROC4_CLNT_CB_GETATTR);
1164 dp->dl_cb_fattr.ncf_file_modified = false;
1165 get_nfs4_file(fp);
1166 dp->dl_stid.sc_file = fp;
1167 return dp;
1168 out_dec:
1169 atomic_long_dec(&num_delegations);
1170 return NULL;
1171 }
1172
1173 void
nfs4_put_stid(struct nfs4_stid * s)1174 nfs4_put_stid(struct nfs4_stid *s)
1175 {
1176 struct nfs4_file *fp = s->sc_file;
1177 struct nfs4_client *clp = s->sc_client;
1178
1179 might_lock(&clp->cl_lock);
1180
1181 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1182 wake_up_all(&close_wq);
1183 return;
1184 }
1185 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1186 if (s->sc_status & SC_STATUS_ADMIN_REVOKED)
1187 atomic_dec(&s->sc_client->cl_admin_revoked);
1188 nfs4_free_cpntf_statelist(clp->net, s);
1189 spin_unlock(&clp->cl_lock);
1190 s->sc_free(s);
1191 if (fp)
1192 put_nfs4_file(fp);
1193 }
1194
1195 void
nfs4_inc_and_copy_stateid(stateid_t * dst,struct nfs4_stid * stid)1196 nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1197 {
1198 stateid_t *src = &stid->sc_stateid;
1199
1200 spin_lock(&stid->sc_lock);
1201 if (unlikely(++src->si_generation == 0))
1202 src->si_generation = 1;
1203 memcpy(dst, src, sizeof(*dst));
1204 spin_unlock(&stid->sc_lock);
1205 }
1206
put_deleg_file(struct nfs4_file * fp)1207 static void put_deleg_file(struct nfs4_file *fp)
1208 {
1209 struct nfsd_file *rnf = NULL;
1210 struct nfsd_file *nf = NULL;
1211
1212 spin_lock(&fp->fi_lock);
1213 if (--fp->fi_delegees == 0) {
1214 swap(nf, fp->fi_deleg_file);
1215 swap(rnf, fp->fi_rdeleg_file);
1216 }
1217 spin_unlock(&fp->fi_lock);
1218
1219 if (nf)
1220 nfsd_file_put(nf);
1221 if (rnf) {
1222 nfsd_file_put(rnf);
1223 nfs4_file_put_access(fp, NFS4_SHARE_ACCESS_READ);
1224 }
1225 }
1226
nfsd4_finalize_deleg_timestamps(struct nfs4_delegation * dp,struct file * f)1227 static void nfsd4_finalize_deleg_timestamps(struct nfs4_delegation *dp, struct file *f)
1228 {
1229 struct iattr ia = { .ia_valid = ATTR_ATIME | ATTR_CTIME | ATTR_MTIME | ATTR_DELEG };
1230 struct inode *inode = file_inode(f);
1231 int ret;
1232
1233 /* don't do anything if FMODE_NOCMTIME isn't set */
1234 if ((READ_ONCE(f->f_mode) & FMODE_NOCMTIME) == 0)
1235 return;
1236
1237 spin_lock(&f->f_lock);
1238 f->f_mode &= ~FMODE_NOCMTIME;
1239 spin_unlock(&f->f_lock);
1240
1241 /* was it never written? */
1242 if (!dp->dl_written)
1243 return;
1244
1245 /* did it get a setattr for the timestamps at some point? */
1246 if (dp->dl_setattr)
1247 return;
1248
1249 /* Stamp everything to "now" */
1250 inode_lock(inode);
1251 ret = notify_change(&nop_mnt_idmap, f->f_path.dentry, &ia, NULL);
1252 inode_unlock(inode);
1253 if (ret) {
1254 struct inode *inode = file_inode(f);
1255
1256 pr_notice_ratelimited("nfsd: Unable to update timestamps on inode %02x:%02x:%lu: %d\n",
1257 MAJOR(inode->i_sb->s_dev),
1258 MINOR(inode->i_sb->s_dev),
1259 inode->i_ino, ret);
1260 }
1261 }
1262
nfs4_unlock_deleg_lease(struct nfs4_delegation * dp)1263 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1264 {
1265 struct nfs4_file *fp = dp->dl_stid.sc_file;
1266 struct nfsd_file *nf = fp->fi_deleg_file;
1267
1268 WARN_ON_ONCE(!fp->fi_delegees);
1269
1270 nfsd4_finalize_deleg_timestamps(dp, nf->nf_file);
1271 kernel_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1272 put_deleg_file(fp);
1273 }
1274
destroy_unhashed_deleg(struct nfs4_delegation * dp)1275 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1276 {
1277 put_clnt_odstate(dp->dl_clnt_odstate);
1278 nfs4_unlock_deleg_lease(dp);
1279 nfs4_put_stid(&dp->dl_stid);
1280 }
1281
1282 /**
1283 * nfs4_delegation_exists - Discover if this delegation already exists
1284 * @clp: a pointer to the nfs4_client we're granting a delegation to
1285 * @fp: a pointer to the nfs4_file we're granting a delegation on
1286 *
1287 * Return:
1288 * On success: true iff an existing delegation is found
1289 */
1290
1291 static bool
nfs4_delegation_exists(struct nfs4_client * clp,struct nfs4_file * fp)1292 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1293 {
1294 struct nfs4_delegation *searchdp = NULL;
1295 struct nfs4_client *searchclp = NULL;
1296
1297 lockdep_assert_held(&state_lock);
1298 lockdep_assert_held(&fp->fi_lock);
1299
1300 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1301 searchclp = searchdp->dl_stid.sc_client;
1302 if (clp == searchclp) {
1303 return true;
1304 }
1305 }
1306 return false;
1307 }
1308
1309 /**
1310 * hash_delegation_locked - Add a delegation to the appropriate lists
1311 * @dp: a pointer to the nfs4_delegation we are adding.
1312 * @fp: a pointer to the nfs4_file we're granting a delegation on
1313 *
1314 * Return:
1315 * On success: NULL if the delegation was successfully hashed.
1316 *
1317 * On error: -EAGAIN if one was previously granted to this
1318 * nfs4_client for this nfs4_file. Delegation is not hashed.
1319 *
1320 */
1321
1322 static int
hash_delegation_locked(struct nfs4_delegation * dp,struct nfs4_file * fp)1323 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1324 {
1325 struct nfs4_client *clp = dp->dl_stid.sc_client;
1326
1327 lockdep_assert_held(&state_lock);
1328 lockdep_assert_held(&fp->fi_lock);
1329 lockdep_assert_held(&clp->cl_lock);
1330
1331 if (nfs4_delegation_exists(clp, fp))
1332 return -EAGAIN;
1333 refcount_inc(&dp->dl_stid.sc_count);
1334 dp->dl_stid.sc_type = SC_TYPE_DELEG;
1335 list_add(&dp->dl_perfile, &fp->fi_delegations);
1336 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1337 return 0;
1338 }
1339
delegation_hashed(struct nfs4_delegation * dp)1340 static bool delegation_hashed(struct nfs4_delegation *dp)
1341 {
1342 return !(list_empty(&dp->dl_perfile));
1343 }
1344
1345 static bool
unhash_delegation_locked(struct nfs4_delegation * dp,unsigned short statusmask)1346 unhash_delegation_locked(struct nfs4_delegation *dp, unsigned short statusmask)
1347 {
1348 struct nfs4_file *fp = dp->dl_stid.sc_file;
1349
1350 lockdep_assert_held(&state_lock);
1351
1352 if (!delegation_hashed(dp))
1353 return false;
1354
1355 if (statusmask == SC_STATUS_REVOKED &&
1356 dp->dl_stid.sc_client->cl_minorversion == 0)
1357 statusmask = SC_STATUS_CLOSED;
1358 dp->dl_stid.sc_status |= statusmask;
1359 if (statusmask & SC_STATUS_ADMIN_REVOKED)
1360 atomic_inc(&dp->dl_stid.sc_client->cl_admin_revoked);
1361
1362 /* Ensure that deleg break won't try to requeue it */
1363 ++dp->dl_time;
1364 spin_lock(&fp->fi_lock);
1365 list_del_init(&dp->dl_perclnt);
1366 list_del_init(&dp->dl_recall_lru);
1367 list_del_init(&dp->dl_perfile);
1368 spin_unlock(&fp->fi_lock);
1369 return true;
1370 }
1371
destroy_delegation(struct nfs4_delegation * dp)1372 static void destroy_delegation(struct nfs4_delegation *dp)
1373 {
1374 bool unhashed;
1375
1376 spin_lock(&state_lock);
1377 unhashed = unhash_delegation_locked(dp, SC_STATUS_CLOSED);
1378 spin_unlock(&state_lock);
1379 if (unhashed)
1380 destroy_unhashed_deleg(dp);
1381 }
1382
1383 /**
1384 * revoke_delegation - perform nfs4 delegation structure cleanup
1385 * @dp: pointer to the delegation
1386 *
1387 * This function assumes that it's called either from the administrative
1388 * interface (nfsd4_revoke_states()) that's revoking a specific delegation
1389 * stateid or it's called from a laundromat thread (nfsd4_landromat()) that
1390 * determined that this specific state has expired and needs to be revoked
1391 * (both mark state with the appropriate stid sc_status mode). It is also
1392 * assumed that a reference was taken on the @dp state.
1393 *
1394 * If this function finds that the @dp state is SC_STATUS_FREED it means
1395 * that a FREE_STATEID operation for this stateid has been processed and
1396 * we can proceed to removing it from recalled list. However, if @dp state
1397 * isn't marked SC_STATUS_FREED, it means we need place it on the cl_revoked
1398 * list and wait for the FREE_STATEID to arrive from the client. At the same
1399 * time, we need to mark it as SC_STATUS_FREEABLE to indicate to the
1400 * nfsd4_free_stateid() function that this stateid has already been added
1401 * to the cl_revoked list and that nfsd4_free_stateid() is now responsible
1402 * for removing it from the list. Inspection of where the delegation state
1403 * in the revocation process is protected by the clp->cl_lock.
1404 */
revoke_delegation(struct nfs4_delegation * dp)1405 static void revoke_delegation(struct nfs4_delegation *dp)
1406 {
1407 struct nfs4_client *clp = dp->dl_stid.sc_client;
1408
1409 WARN_ON(!list_empty(&dp->dl_recall_lru));
1410 WARN_ON_ONCE(dp->dl_stid.sc_client->cl_minorversion > 0 &&
1411 !(dp->dl_stid.sc_status &
1412 (SC_STATUS_REVOKED | SC_STATUS_ADMIN_REVOKED)));
1413
1414 trace_nfsd_stid_revoke(&dp->dl_stid);
1415
1416 spin_lock(&clp->cl_lock);
1417 if (dp->dl_stid.sc_status & SC_STATUS_FREED) {
1418 list_del_init(&dp->dl_recall_lru);
1419 goto out;
1420 }
1421 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1422 dp->dl_stid.sc_status |= SC_STATUS_FREEABLE;
1423 out:
1424 spin_unlock(&clp->cl_lock);
1425 destroy_unhashed_deleg(dp);
1426 }
1427
1428 /*
1429 * SETCLIENTID state
1430 */
1431
clientid_hashval(u32 id)1432 static unsigned int clientid_hashval(u32 id)
1433 {
1434 return id & CLIENT_HASH_MASK;
1435 }
1436
clientstr_hashval(struct xdr_netobj name)1437 static unsigned int clientstr_hashval(struct xdr_netobj name)
1438 {
1439 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1440 }
1441
1442 /*
1443 * A stateid that had a deny mode associated with it is being released
1444 * or downgraded. Recalculate the deny mode on the file.
1445 */
1446 static void
recalculate_deny_mode(struct nfs4_file * fp)1447 recalculate_deny_mode(struct nfs4_file *fp)
1448 {
1449 struct nfs4_ol_stateid *stp;
1450 u32 old_deny;
1451
1452 spin_lock(&fp->fi_lock);
1453 old_deny = fp->fi_share_deny;
1454 fp->fi_share_deny = 0;
1455 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1456 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1457 if (fp->fi_share_deny == old_deny)
1458 break;
1459 }
1460 spin_unlock(&fp->fi_lock);
1461 }
1462
1463 static void
reset_union_bmap_deny(u32 deny,struct nfs4_ol_stateid * stp)1464 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1465 {
1466 int i;
1467 bool change = false;
1468
1469 for (i = 1; i < 4; i++) {
1470 if ((i & deny) != i) {
1471 change = true;
1472 clear_deny(i, stp);
1473 }
1474 }
1475
1476 /* Recalculate per-file deny mode if there was a change */
1477 if (change)
1478 recalculate_deny_mode(stp->st_stid.sc_file);
1479 }
1480
1481 /* release all access and file references for a given stateid */
1482 static void
release_all_access(struct nfs4_ol_stateid * stp)1483 release_all_access(struct nfs4_ol_stateid *stp)
1484 {
1485 int i;
1486 struct nfs4_file *fp = stp->st_stid.sc_file;
1487
1488 if (fp && stp->st_deny_bmap != 0)
1489 recalculate_deny_mode(fp);
1490
1491 for (i = 1; i < 4; i++) {
1492 if (test_access(i, stp))
1493 nfs4_file_put_access(stp->st_stid.sc_file, i);
1494 clear_access(i, stp);
1495 }
1496 }
1497
nfs4_free_stateowner(struct nfs4_stateowner * sop)1498 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1499 {
1500 kfree(sop->so_owner.data);
1501 sop->so_ops->so_free(sop);
1502 }
1503
nfs4_put_stateowner(struct nfs4_stateowner * sop)1504 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1505 {
1506 struct nfs4_client *clp = sop->so_client;
1507
1508 might_lock(&clp->cl_lock);
1509
1510 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1511 return;
1512 sop->so_ops->so_unhash(sop);
1513 spin_unlock(&clp->cl_lock);
1514 nfs4_free_stateowner(sop);
1515 }
1516
1517 static bool
nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid * stp)1518 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1519 {
1520 return list_empty(&stp->st_perfile);
1521 }
1522
unhash_ol_stateid(struct nfs4_ol_stateid * stp)1523 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1524 {
1525 struct nfs4_file *fp = stp->st_stid.sc_file;
1526
1527 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1528
1529 if (list_empty(&stp->st_perfile))
1530 return false;
1531
1532 spin_lock(&fp->fi_lock);
1533 list_del_init(&stp->st_perfile);
1534 spin_unlock(&fp->fi_lock);
1535 list_del(&stp->st_perstateowner);
1536 return true;
1537 }
1538
nfs4_free_ol_stateid(struct nfs4_stid * stid)1539 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1540 {
1541 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1542
1543 put_clnt_odstate(stp->st_clnt_odstate);
1544 release_all_access(stp);
1545 if (stp->st_stateowner)
1546 nfs4_put_stateowner(stp->st_stateowner);
1547 if (!list_empty(&stid->sc_cp_list))
1548 nfs4_free_cpntf_statelist(stid->sc_client->net, stid);
1549 kmem_cache_free(stateid_slab, stid);
1550 }
1551
nfs4_free_lock_stateid(struct nfs4_stid * stid)1552 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1553 {
1554 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1555 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1556 struct nfsd_file *nf;
1557
1558 nf = find_any_file(stp->st_stid.sc_file);
1559 if (nf) {
1560 get_file(nf->nf_file);
1561 filp_close(nf->nf_file, (fl_owner_t)lo);
1562 nfsd_file_put(nf);
1563 }
1564 nfs4_free_ol_stateid(stid);
1565 }
1566
1567 /*
1568 * Put the persistent reference to an already unhashed generic stateid, while
1569 * holding the cl_lock. If it's the last reference, then put it onto the
1570 * reaplist for later destruction.
1571 */
put_ol_stateid_locked(struct nfs4_ol_stateid * stp,struct list_head * reaplist)1572 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1573 struct list_head *reaplist)
1574 {
1575 struct nfs4_stid *s = &stp->st_stid;
1576 struct nfs4_client *clp = s->sc_client;
1577
1578 lockdep_assert_held(&clp->cl_lock);
1579
1580 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1581
1582 if (!refcount_dec_and_test(&s->sc_count)) {
1583 wake_up_all(&close_wq);
1584 return;
1585 }
1586
1587 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1588 if (s->sc_status & SC_STATUS_ADMIN_REVOKED)
1589 atomic_dec(&s->sc_client->cl_admin_revoked);
1590 list_add(&stp->st_locks, reaplist);
1591 }
1592
unhash_lock_stateid(struct nfs4_ol_stateid * stp)1593 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1594 {
1595 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1596
1597 if (!unhash_ol_stateid(stp))
1598 return false;
1599 list_del_init(&stp->st_locks);
1600 stp->st_stid.sc_status |= SC_STATUS_CLOSED;
1601 return true;
1602 }
1603
release_lock_stateid(struct nfs4_ol_stateid * stp)1604 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1605 {
1606 struct nfs4_client *clp = stp->st_stid.sc_client;
1607 bool unhashed;
1608
1609 spin_lock(&clp->cl_lock);
1610 unhashed = unhash_lock_stateid(stp);
1611 spin_unlock(&clp->cl_lock);
1612 if (unhashed)
1613 nfs4_put_stid(&stp->st_stid);
1614 }
1615
unhash_lockowner_locked(struct nfs4_lockowner * lo)1616 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1617 {
1618 struct nfs4_client *clp = lo->lo_owner.so_client;
1619
1620 lockdep_assert_held(&clp->cl_lock);
1621
1622 list_del_init(&lo->lo_owner.so_strhash);
1623 }
1624
1625 /*
1626 * Free a list of generic stateids that were collected earlier after being
1627 * fully unhashed.
1628 */
1629 static void
free_ol_stateid_reaplist(struct list_head * reaplist)1630 free_ol_stateid_reaplist(struct list_head *reaplist)
1631 {
1632 struct nfs4_ol_stateid *stp;
1633 struct nfs4_file *fp;
1634
1635 might_sleep();
1636
1637 while (!list_empty(reaplist)) {
1638 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1639 st_locks);
1640 list_del(&stp->st_locks);
1641 fp = stp->st_stid.sc_file;
1642 stp->st_stid.sc_free(&stp->st_stid);
1643 if (fp)
1644 put_nfs4_file(fp);
1645 }
1646 }
1647
release_open_stateid_locks(struct nfs4_ol_stateid * open_stp,struct list_head * reaplist)1648 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1649 struct list_head *reaplist)
1650 {
1651 struct nfs4_ol_stateid *stp;
1652
1653 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1654
1655 while (!list_empty(&open_stp->st_locks)) {
1656 stp = list_entry(open_stp->st_locks.next,
1657 struct nfs4_ol_stateid, st_locks);
1658 unhash_lock_stateid(stp);
1659 put_ol_stateid_locked(stp, reaplist);
1660 }
1661 }
1662
unhash_open_stateid(struct nfs4_ol_stateid * stp,struct list_head * reaplist)1663 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1664 struct list_head *reaplist)
1665 {
1666 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1667
1668 if (!unhash_ol_stateid(stp))
1669 return false;
1670 release_open_stateid_locks(stp, reaplist);
1671 return true;
1672 }
1673
release_open_stateid(struct nfs4_ol_stateid * stp)1674 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1675 {
1676 LIST_HEAD(reaplist);
1677
1678 spin_lock(&stp->st_stid.sc_client->cl_lock);
1679 stp->st_stid.sc_status |= SC_STATUS_CLOSED;
1680 if (unhash_open_stateid(stp, &reaplist))
1681 put_ol_stateid_locked(stp, &reaplist);
1682 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1683 free_ol_stateid_reaplist(&reaplist);
1684 }
1685
nfs4_openowner_unhashed(struct nfs4_openowner * oo)1686 static bool nfs4_openowner_unhashed(struct nfs4_openowner *oo)
1687 {
1688 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1689
1690 return list_empty(&oo->oo_owner.so_strhash) &&
1691 list_empty(&oo->oo_perclient);
1692 }
1693
unhash_openowner_locked(struct nfs4_openowner * oo)1694 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1695 {
1696 struct nfs4_client *clp = oo->oo_owner.so_client;
1697
1698 lockdep_assert_held(&clp->cl_lock);
1699
1700 list_del_init(&oo->oo_owner.so_strhash);
1701 list_del_init(&oo->oo_perclient);
1702 }
1703
release_last_closed_stateid(struct nfs4_openowner * oo)1704 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1705 {
1706 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1707 nfsd_net_id);
1708 struct nfs4_ol_stateid *s;
1709
1710 spin_lock(&nn->client_lock);
1711 s = oo->oo_last_closed_stid;
1712 if (s) {
1713 list_del_init(&oo->oo_close_lru);
1714 oo->oo_last_closed_stid = NULL;
1715 }
1716 spin_unlock(&nn->client_lock);
1717 if (s)
1718 nfs4_put_stid(&s->st_stid);
1719 }
1720
release_openowner(struct nfs4_openowner * oo)1721 static void release_openowner(struct nfs4_openowner *oo)
1722 {
1723 struct nfs4_ol_stateid *stp;
1724 struct nfs4_client *clp = oo->oo_owner.so_client;
1725 LIST_HEAD(reaplist);
1726
1727 spin_lock(&clp->cl_lock);
1728 unhash_openowner_locked(oo);
1729 while (!list_empty(&oo->oo_owner.so_stateids)) {
1730 stp = list_first_entry(&oo->oo_owner.so_stateids,
1731 struct nfs4_ol_stateid, st_perstateowner);
1732 if (unhash_open_stateid(stp, &reaplist))
1733 put_ol_stateid_locked(stp, &reaplist);
1734 }
1735 spin_unlock(&clp->cl_lock);
1736 free_ol_stateid_reaplist(&reaplist);
1737 release_last_closed_stateid(oo);
1738 nfs4_put_stateowner(&oo->oo_owner);
1739 }
1740
find_one_sb_stid(struct nfs4_client * clp,struct super_block * sb,unsigned int sc_types)1741 static struct nfs4_stid *find_one_sb_stid(struct nfs4_client *clp,
1742 struct super_block *sb,
1743 unsigned int sc_types)
1744 {
1745 unsigned long id, tmp;
1746 struct nfs4_stid *stid;
1747
1748 spin_lock(&clp->cl_lock);
1749 idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id)
1750 if ((stid->sc_type & sc_types) &&
1751 stid->sc_status == 0 &&
1752 stid->sc_file->fi_inode->i_sb == sb) {
1753 refcount_inc(&stid->sc_count);
1754 break;
1755 }
1756 spin_unlock(&clp->cl_lock);
1757 return stid;
1758 }
1759
1760 /**
1761 * nfsd4_revoke_states - revoke all nfsv4 states associated with given filesystem
1762 * @nn: used to identify instance of nfsd (there is one per net namespace)
1763 * @sb: super_block used to identify target filesystem
1764 *
1765 * All nfs4 states (open, lock, delegation, layout) held by the server instance
1766 * and associated with a file on the given filesystem will be revoked resulting
1767 * in any files being closed and so all references from nfsd to the filesystem
1768 * being released. Thus nfsd will no longer prevent the filesystem from being
1769 * unmounted.
1770 *
1771 * The clients which own the states will subsequently being notified that the
1772 * states have been "admin-revoked".
1773 */
nfsd4_revoke_states(struct nfsd_net * nn,struct super_block * sb)1774 void nfsd4_revoke_states(struct nfsd_net *nn, struct super_block *sb)
1775 {
1776 unsigned int idhashval;
1777 unsigned int sc_types;
1778
1779 sc_types = SC_TYPE_OPEN | SC_TYPE_LOCK | SC_TYPE_DELEG | SC_TYPE_LAYOUT;
1780
1781 spin_lock(&nn->client_lock);
1782 for (idhashval = 0; idhashval < CLIENT_HASH_SIZE; idhashval++) {
1783 struct list_head *head = &nn->conf_id_hashtbl[idhashval];
1784 struct nfs4_client *clp;
1785 retry:
1786 list_for_each_entry(clp, head, cl_idhash) {
1787 struct nfs4_stid *stid = find_one_sb_stid(clp, sb,
1788 sc_types);
1789 if (stid) {
1790 struct nfs4_ol_stateid *stp;
1791 struct nfs4_delegation *dp;
1792 struct nfs4_layout_stateid *ls;
1793
1794 spin_unlock(&nn->client_lock);
1795 switch (stid->sc_type) {
1796 case SC_TYPE_OPEN:
1797 stp = openlockstateid(stid);
1798 mutex_lock_nested(&stp->st_mutex,
1799 OPEN_STATEID_MUTEX);
1800
1801 spin_lock(&clp->cl_lock);
1802 if (stid->sc_status == 0) {
1803 stid->sc_status |=
1804 SC_STATUS_ADMIN_REVOKED;
1805 atomic_inc(&clp->cl_admin_revoked);
1806 spin_unlock(&clp->cl_lock);
1807 release_all_access(stp);
1808 } else
1809 spin_unlock(&clp->cl_lock);
1810 mutex_unlock(&stp->st_mutex);
1811 break;
1812 case SC_TYPE_LOCK:
1813 stp = openlockstateid(stid);
1814 mutex_lock_nested(&stp->st_mutex,
1815 LOCK_STATEID_MUTEX);
1816 spin_lock(&clp->cl_lock);
1817 if (stid->sc_status == 0) {
1818 struct nfs4_lockowner *lo =
1819 lockowner(stp->st_stateowner);
1820 struct nfsd_file *nf;
1821
1822 stid->sc_status |=
1823 SC_STATUS_ADMIN_REVOKED;
1824 atomic_inc(&clp->cl_admin_revoked);
1825 spin_unlock(&clp->cl_lock);
1826 nf = find_any_file(stp->st_stid.sc_file);
1827 if (nf) {
1828 get_file(nf->nf_file);
1829 filp_close(nf->nf_file,
1830 (fl_owner_t)lo);
1831 nfsd_file_put(nf);
1832 }
1833 release_all_access(stp);
1834 } else
1835 spin_unlock(&clp->cl_lock);
1836 mutex_unlock(&stp->st_mutex);
1837 break;
1838 case SC_TYPE_DELEG:
1839 refcount_inc(&stid->sc_count);
1840 dp = delegstateid(stid);
1841 spin_lock(&state_lock);
1842 if (!unhash_delegation_locked(
1843 dp, SC_STATUS_ADMIN_REVOKED))
1844 dp = NULL;
1845 spin_unlock(&state_lock);
1846 if (dp)
1847 revoke_delegation(dp);
1848 break;
1849 case SC_TYPE_LAYOUT:
1850 ls = layoutstateid(stid);
1851 nfsd4_close_layout(ls);
1852 break;
1853 }
1854 nfs4_put_stid(stid);
1855 spin_lock(&nn->client_lock);
1856 if (clp->cl_minorversion == 0)
1857 /* Allow cleanup after a lease period.
1858 * store_release ensures cleanup will
1859 * see any newly revoked states if it
1860 * sees the time updated.
1861 */
1862 nn->nfs40_last_revoke =
1863 ktime_get_boottime_seconds();
1864 goto retry;
1865 }
1866 }
1867 }
1868 spin_unlock(&nn->client_lock);
1869 }
1870
1871 static inline int
hash_sessionid(struct nfs4_sessionid * sessionid)1872 hash_sessionid(struct nfs4_sessionid *sessionid)
1873 {
1874 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1875
1876 return sid->sequence % SESSION_HASH_SIZE;
1877 }
1878
1879 #ifdef CONFIG_SUNRPC_DEBUG
1880 static inline void
dump_sessionid(const char * fn,struct nfs4_sessionid * sessionid)1881 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1882 {
1883 u32 *ptr = (u32 *)(&sessionid->data[0]);
1884 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1885 }
1886 #else
1887 static inline void
dump_sessionid(const char * fn,struct nfs4_sessionid * sessionid)1888 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1889 {
1890 }
1891 #endif
1892
1893 /*
1894 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1895 * won't be used for replay.
1896 */
nfsd4_bump_seqid(struct nfsd4_compound_state * cstate,__be32 nfserr)1897 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1898 {
1899 struct nfs4_stateowner *so = cstate->replay_owner;
1900
1901 if (nfserr == nfserr_replay_me)
1902 return;
1903
1904 if (!seqid_mutating_err(ntohl(nfserr))) {
1905 nfsd4_cstate_clear_replay(cstate);
1906 return;
1907 }
1908 if (!so)
1909 return;
1910 if (so->so_is_open_owner)
1911 release_last_closed_stateid(openowner(so));
1912 so->so_seqid++;
1913 return;
1914 }
1915
1916 static void
gen_sessionid(struct nfsd4_session * ses)1917 gen_sessionid(struct nfsd4_session *ses)
1918 {
1919 struct nfs4_client *clp = ses->se_client;
1920 struct nfsd4_sessionid *sid;
1921
1922 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1923 sid->clientid = clp->cl_clientid;
1924 sid->sequence = current_sessionid++;
1925 sid->reserved = 0;
1926 }
1927
1928 /*
1929 * The protocol defines ca_maxresponssize_cached to include the size of
1930 * the rpc header, but all we need to cache is the data starting after
1931 * the end of the initial SEQUENCE operation--the rest we regenerate
1932 * each time. Therefore we can advertise a ca_maxresponssize_cached
1933 * value that is the number of bytes in our cache plus a few additional
1934 * bytes. In order to stay on the safe side, and not promise more than
1935 * we can cache, those additional bytes must be the minimum possible: 24
1936 * bytes of rpc header (xid through accept state, with AUTH_NULL
1937 * verifier), 12 for the compound header (with zero-length tag), and 44
1938 * for the SEQUENCE op response:
1939 */
1940 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1941
1942 static struct shrinker *nfsd_slot_shrinker;
1943 static DEFINE_SPINLOCK(nfsd_session_list_lock);
1944 static LIST_HEAD(nfsd_session_list);
1945 /* The sum of "target_slots-1" on every session. The shrinker can push this
1946 * down, though it can take a little while for the memory to actually
1947 * be freed. The "-1" is because we can never free slot 0 while the
1948 * session is active.
1949 */
1950 static atomic_t nfsd_total_target_slots = ATOMIC_INIT(0);
1951
1952 static void
free_session_slots(struct nfsd4_session * ses,int from)1953 free_session_slots(struct nfsd4_session *ses, int from)
1954 {
1955 int i;
1956
1957 if (from >= ses->se_fchannel.maxreqs)
1958 return;
1959
1960 for (i = from; i < ses->se_fchannel.maxreqs; i++) {
1961 struct nfsd4_slot *slot = xa_load(&ses->se_slots, i);
1962
1963 /*
1964 * Save the seqid in case we reactivate this slot.
1965 * This will never require a memory allocation so GFP
1966 * flag is irrelevant
1967 */
1968 xa_store(&ses->se_slots, i, xa_mk_value(slot->sl_seqid), 0);
1969 free_svc_cred(&slot->sl_cred);
1970 kfree(slot);
1971 }
1972 ses->se_fchannel.maxreqs = from;
1973 if (ses->se_target_maxslots > from) {
1974 int new_target = from ?: 1;
1975 atomic_sub(ses->se_target_maxslots - new_target, &nfsd_total_target_slots);
1976 ses->se_target_maxslots = new_target;
1977 }
1978 }
1979
1980 /**
1981 * reduce_session_slots - reduce the target max-slots of a session if possible
1982 * @ses: The session to affect
1983 * @dec: how much to decrease the target by
1984 *
1985 * This interface can be used by a shrinker to reduce the target max-slots
1986 * for a session so that some slots can eventually be freed.
1987 * It uses spin_trylock() as it may be called in a context where another
1988 * spinlock is held that has a dependency on client_lock. As shrinkers are
1989 * best-effort, skiping a session is client_lock is already held has no
1990 * great coast
1991 *
1992 * Return value:
1993 * The number of slots that the target was reduced by.
1994 */
1995 static int
reduce_session_slots(struct nfsd4_session * ses,int dec)1996 reduce_session_slots(struct nfsd4_session *ses, int dec)
1997 {
1998 struct nfsd_net *nn = net_generic(ses->se_client->net,
1999 nfsd_net_id);
2000 int ret = 0;
2001
2002 if (ses->se_target_maxslots <= 1)
2003 return ret;
2004 if (!spin_trylock(&nn->client_lock))
2005 return ret;
2006 ret = min(dec, ses->se_target_maxslots-1);
2007 ses->se_target_maxslots -= ret;
2008 atomic_sub(ret, &nfsd_total_target_slots);
2009 ses->se_slot_gen += 1;
2010 if (ses->se_slot_gen == 0) {
2011 int i;
2012 ses->se_slot_gen = 1;
2013 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
2014 struct nfsd4_slot *slot = xa_load(&ses->se_slots, i);
2015 slot->sl_generation = 0;
2016 }
2017 }
2018 spin_unlock(&nn->client_lock);
2019 return ret;
2020 }
2021
nfsd4_alloc_slot(struct nfsd4_channel_attrs * fattrs,int index,gfp_t gfp)2022 static struct nfsd4_slot *nfsd4_alloc_slot(struct nfsd4_channel_attrs *fattrs,
2023 int index, gfp_t gfp)
2024 {
2025 struct nfsd4_slot *slot;
2026 size_t size;
2027
2028 /*
2029 * The RPC and NFS session headers are never saved in
2030 * the slot reply cache buffer.
2031 */
2032 size = fattrs->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ ?
2033 0 : fattrs->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
2034
2035 slot = kzalloc_flex(*slot, sl_data, size, gfp);
2036 if (!slot)
2037 return NULL;
2038 slot->sl_index = index;
2039 return slot;
2040 }
2041
alloc_session(struct nfsd4_channel_attrs * fattrs,struct nfsd4_channel_attrs * battrs)2042 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
2043 struct nfsd4_channel_attrs *battrs)
2044 {
2045 int numslots = fattrs->maxreqs;
2046 struct nfsd4_session *new;
2047 struct nfsd4_slot *slot;
2048 int i;
2049
2050 new = kzalloc_obj(*new);
2051 if (!new)
2052 return NULL;
2053 xa_init(&new->se_slots);
2054
2055 slot = nfsd4_alloc_slot(fattrs, 0, GFP_KERNEL);
2056 if (!slot || xa_is_err(xa_store(&new->se_slots, 0, slot, GFP_KERNEL)))
2057 goto out_free;
2058
2059 for (i = 1; i < numslots; i++) {
2060 const gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
2061 slot = nfsd4_alloc_slot(fattrs, i, gfp);
2062 if (!slot)
2063 break;
2064 if (xa_is_err(xa_store(&new->se_slots, i, slot, gfp))) {
2065 kfree(slot);
2066 break;
2067 }
2068 }
2069 fattrs->maxreqs = i;
2070 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
2071 new->se_target_maxslots = i;
2072 atomic_add(i - 1, &nfsd_total_target_slots);
2073 new->se_cb_slot_avail = ~0U;
2074 new->se_cb_highest_slot = min(battrs->maxreqs - 1,
2075 NFSD_BC_SLOT_TABLE_SIZE - 1);
2076 spin_lock_init(&new->se_lock);
2077 return new;
2078 out_free:
2079 kfree(slot);
2080 xa_destroy(&new->se_slots);
2081 kfree(new);
2082 return NULL;
2083 }
2084
free_conn(struct nfsd4_conn * c)2085 static void free_conn(struct nfsd4_conn *c)
2086 {
2087 svc_xprt_put(c->cn_xprt);
2088 kfree(c);
2089 }
2090
nfsd4_conn_lost(struct svc_xpt_user * u)2091 static void nfsd4_conn_lost(struct svc_xpt_user *u)
2092 {
2093 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
2094 struct nfs4_client *clp = c->cn_session->se_client;
2095
2096 trace_nfsd_cb_lost(clp);
2097
2098 spin_lock(&clp->cl_lock);
2099 if (!list_empty(&c->cn_persession)) {
2100 list_del(&c->cn_persession);
2101 free_conn(c);
2102 }
2103 nfsd4_probe_callback(clp);
2104 spin_unlock(&clp->cl_lock);
2105 }
2106
alloc_conn(struct svc_rqst * rqstp,u32 flags)2107 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
2108 {
2109 struct nfsd4_conn *conn;
2110
2111 conn = kmalloc_obj(struct nfsd4_conn);
2112 if (!conn)
2113 return NULL;
2114 svc_xprt_get(rqstp->rq_xprt);
2115 conn->cn_xprt = rqstp->rq_xprt;
2116 conn->cn_flags = flags;
2117 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
2118 return conn;
2119 }
2120
__nfsd4_hash_conn(struct nfsd4_conn * conn,struct nfsd4_session * ses)2121 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
2122 {
2123 conn->cn_session = ses;
2124 list_add(&conn->cn_persession, &ses->se_conns);
2125 }
2126
nfsd4_hash_conn(struct nfsd4_conn * conn,struct nfsd4_session * ses)2127 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
2128 {
2129 struct nfs4_client *clp = ses->se_client;
2130
2131 spin_lock(&clp->cl_lock);
2132 __nfsd4_hash_conn(conn, ses);
2133 spin_unlock(&clp->cl_lock);
2134 }
2135
nfsd4_register_conn(struct nfsd4_conn * conn)2136 static int nfsd4_register_conn(struct nfsd4_conn *conn)
2137 {
2138 conn->cn_xpt_user.callback = nfsd4_conn_lost;
2139 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
2140 }
2141
nfsd4_init_conn(struct svc_rqst * rqstp,struct nfsd4_conn * conn,struct nfsd4_session * ses)2142 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
2143 {
2144 int ret;
2145
2146 nfsd4_hash_conn(conn, ses);
2147 ret = nfsd4_register_conn(conn);
2148 if (ret)
2149 /* oops; xprt is already down: */
2150 nfsd4_conn_lost(&conn->cn_xpt_user);
2151 /* We may have gained or lost a callback channel: */
2152 nfsd4_probe_callback_sync(ses->se_client);
2153 }
2154
alloc_conn_from_crses(struct svc_rqst * rqstp,struct nfsd4_create_session * cses)2155 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
2156 {
2157 u32 dir = NFS4_CDFC4_FORE;
2158
2159 if (cses->flags & SESSION4_BACK_CHAN)
2160 dir |= NFS4_CDFC4_BACK;
2161 return alloc_conn(rqstp, dir);
2162 }
2163
2164 /* must be called under client_lock */
nfsd4_del_conns(struct nfsd4_session * s)2165 static void nfsd4_del_conns(struct nfsd4_session *s)
2166 {
2167 struct nfs4_client *clp = s->se_client;
2168 struct nfsd4_conn *c;
2169
2170 spin_lock(&clp->cl_lock);
2171 while (!list_empty(&s->se_conns)) {
2172 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
2173 list_del_init(&c->cn_persession);
2174 spin_unlock(&clp->cl_lock);
2175
2176 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
2177 free_conn(c);
2178
2179 spin_lock(&clp->cl_lock);
2180 }
2181 spin_unlock(&clp->cl_lock);
2182 }
2183
__free_session(struct nfsd4_session * ses)2184 static void __free_session(struct nfsd4_session *ses)
2185 {
2186 free_session_slots(ses, 0);
2187 xa_destroy(&ses->se_slots);
2188 kfree(ses);
2189 }
2190
free_session(struct nfsd4_session * ses)2191 static void free_session(struct nfsd4_session *ses)
2192 {
2193 nfsd4_del_conns(ses);
2194 __free_session(ses);
2195 }
2196
2197 static unsigned long
nfsd_slot_count(struct shrinker * s,struct shrink_control * sc)2198 nfsd_slot_count(struct shrinker *s, struct shrink_control *sc)
2199 {
2200 unsigned long cnt = atomic_read(&nfsd_total_target_slots);
2201
2202 return cnt ? cnt : SHRINK_EMPTY;
2203 }
2204
2205 static unsigned long
nfsd_slot_scan(struct shrinker * s,struct shrink_control * sc)2206 nfsd_slot_scan(struct shrinker *s, struct shrink_control *sc)
2207 {
2208 struct nfsd4_session *ses;
2209 unsigned long scanned = 0;
2210 unsigned long freed = 0;
2211
2212 spin_lock(&nfsd_session_list_lock);
2213 list_for_each_entry(ses, &nfsd_session_list, se_all_sessions) {
2214 freed += reduce_session_slots(ses, 1);
2215 scanned += 1;
2216 if (scanned >= sc->nr_to_scan) {
2217 /* Move starting point for next scan */
2218 list_move(&nfsd_session_list, &ses->se_all_sessions);
2219 break;
2220 }
2221 }
2222 spin_unlock(&nfsd_session_list_lock);
2223 sc->nr_scanned = scanned;
2224 return freed;
2225 }
2226
init_session(struct svc_rqst * rqstp,struct nfsd4_session * new,struct nfs4_client * clp,struct nfsd4_create_session * cses)2227 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
2228 {
2229 int idx;
2230 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2231
2232 new->se_client = clp;
2233 gen_sessionid(new);
2234
2235 INIT_LIST_HEAD(&new->se_conns);
2236
2237 atomic_set(&new->se_ref, 0);
2238 new->se_dead = false;
2239 new->se_cb_prog = cses->callback_prog;
2240 new->se_cb_sec = cses->cb_sec;
2241
2242 for (idx = 0; idx < NFSD_BC_SLOT_TABLE_SIZE; ++idx)
2243 new->se_cb_seq_nr[idx] = 1;
2244
2245 idx = hash_sessionid(&new->se_sessionid);
2246 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
2247 spin_lock(&clp->cl_lock);
2248 list_add(&new->se_perclnt, &clp->cl_sessions);
2249 spin_unlock(&clp->cl_lock);
2250
2251 spin_lock(&nfsd_session_list_lock);
2252 list_add_tail(&new->se_all_sessions, &nfsd_session_list);
2253 spin_unlock(&nfsd_session_list_lock);
2254
2255 {
2256 struct sockaddr *sa = svc_addr(rqstp);
2257 /*
2258 * This is a little silly; with sessions there's no real
2259 * use for the callback address. Use the peer address
2260 * as a reasonable default for now, but consider fixing
2261 * the rpc client not to require an address in the
2262 * future:
2263 */
2264 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
2265 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
2266 }
2267 }
2268
2269 /* caller must hold client_lock */
2270 static struct nfsd4_session *
__find_in_sessionid_hashtbl(struct nfs4_sessionid * sessionid,struct net * net)2271 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
2272 {
2273 struct nfsd4_session *elem;
2274 int idx;
2275 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2276
2277 lockdep_assert_held(&nn->client_lock);
2278
2279 dump_sessionid(__func__, sessionid);
2280 idx = hash_sessionid(sessionid);
2281 /* Search in the appropriate list */
2282 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
2283 if (!memcmp(elem->se_sessionid.data, sessionid->data,
2284 NFS4_MAX_SESSIONID_LEN)) {
2285 return elem;
2286 }
2287 }
2288
2289 dprintk("%s: session not found\n", __func__);
2290 return NULL;
2291 }
2292
2293 static struct nfsd4_session *
find_in_sessionid_hashtbl(struct nfs4_sessionid * sessionid,struct net * net,__be32 * ret)2294 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2295 __be32 *ret)
2296 {
2297 struct nfsd4_session *session;
2298 __be32 status = nfserr_badsession;
2299
2300 session = __find_in_sessionid_hashtbl(sessionid, net);
2301 if (!session)
2302 goto out;
2303 status = nfsd4_get_session_locked(session);
2304 if (status)
2305 session = NULL;
2306 out:
2307 *ret = status;
2308 return session;
2309 }
2310
2311 /* caller must hold client_lock */
2312 static void
unhash_session(struct nfsd4_session * ses)2313 unhash_session(struct nfsd4_session *ses)
2314 {
2315 struct nfs4_client *clp = ses->se_client;
2316 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2317
2318 lockdep_assert_held(&nn->client_lock);
2319
2320 list_del(&ses->se_hash);
2321 spin_lock(&ses->se_client->cl_lock);
2322 list_del(&ses->se_perclnt);
2323 spin_unlock(&ses->se_client->cl_lock);
2324 spin_lock(&nfsd_session_list_lock);
2325 list_del(&ses->se_all_sessions);
2326 spin_unlock(&nfsd_session_list_lock);
2327 }
2328
2329 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2330 static int
STALE_CLIENTID(clientid_t * clid,struct nfsd_net * nn)2331 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
2332 {
2333 /*
2334 * We're assuming the clid was not given out from a boot
2335 * precisely 2^32 (about 136 years) before this one. That seems
2336 * a safe assumption:
2337 */
2338 if (clid->cl_boot == (u32)nn->boot_time)
2339 return 0;
2340 trace_nfsd_clid_stale(clid);
2341 return 1;
2342 }
2343
alloc_client(struct xdr_netobj name,struct nfsd_net * nn)2344 static struct nfs4_client *alloc_client(struct xdr_netobj name,
2345 struct nfsd_net *nn)
2346 {
2347 struct nfs4_client *clp;
2348 int i;
2349
2350 if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients &&
2351 atomic_read(&nn->nfsd_courtesy_clients) > 0)
2352 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
2353
2354 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
2355 if (clp == NULL)
2356 return NULL;
2357 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
2358 if (clp->cl_name.data == NULL)
2359 goto err_no_name;
2360 clp->cl_ownerstr_hashtbl = kmalloc_objs(struct list_head,
2361 OWNER_HASH_SIZE);
2362 if (!clp->cl_ownerstr_hashtbl)
2363 goto err_no_hashtbl;
2364 clp->cl_callback_wq = alloc_ordered_workqueue("nfsd4_callbacks", 0);
2365 if (!clp->cl_callback_wq)
2366 goto err_no_callback_wq;
2367
2368 for (i = 0; i < OWNER_HASH_SIZE; i++)
2369 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
2370 INIT_LIST_HEAD(&clp->cl_sessions);
2371 idr_init(&clp->cl_stateids);
2372 atomic_set(&clp->cl_rpc_users, 0);
2373 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2374 clp->cl_state = NFSD4_ACTIVE;
2375 atomic_inc(&nn->nfs4_client_count);
2376 atomic_set(&clp->cl_delegs_in_recall, 0);
2377 INIT_LIST_HEAD(&clp->cl_idhash);
2378 INIT_LIST_HEAD(&clp->cl_openowners);
2379 INIT_LIST_HEAD(&clp->cl_delegations);
2380 INIT_LIST_HEAD(&clp->cl_lru);
2381 INIT_LIST_HEAD(&clp->cl_revoked);
2382 #ifdef CONFIG_NFSD_PNFS
2383 INIT_LIST_HEAD(&clp->cl_lo_states);
2384 #endif
2385 INIT_LIST_HEAD(&clp->async_copies);
2386 spin_lock_init(&clp->async_lock);
2387 spin_lock_init(&clp->cl_lock);
2388 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2389 return clp;
2390 err_no_callback_wq:
2391 kfree(clp->cl_ownerstr_hashtbl);
2392 err_no_hashtbl:
2393 kfree(clp->cl_name.data);
2394 err_no_name:
2395 kmem_cache_free(client_slab, clp);
2396 return NULL;
2397 }
2398
__free_client(struct kref * k)2399 static void __free_client(struct kref *k)
2400 {
2401 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2402 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
2403
2404 free_svc_cred(&clp->cl_cred);
2405 destroy_workqueue(clp->cl_callback_wq);
2406 kfree(clp->cl_ownerstr_hashtbl);
2407 kfree(clp->cl_name.data);
2408 kfree(clp->cl_nii_domain.data);
2409 kfree(clp->cl_nii_name.data);
2410 idr_destroy(&clp->cl_stateids);
2411 kfree(clp->cl_ra);
2412 kmem_cache_free(client_slab, clp);
2413 }
2414
2415 /**
2416 * nfsd4_put_client - release a reference on an nfs4_client
2417 * @clp: the client to be released
2418 *
2419 * When the last reference is released, the client is freed.
2420 */
nfsd4_put_client(struct nfs4_client * clp)2421 void nfsd4_put_client(struct nfs4_client *clp)
2422 {
2423 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2424 }
2425
2426 static void
free_client(struct nfs4_client * clp)2427 free_client(struct nfs4_client *clp)
2428 {
2429 while (!list_empty(&clp->cl_sessions)) {
2430 struct nfsd4_session *ses;
2431 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2432 se_perclnt);
2433 list_del(&ses->se_perclnt);
2434 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2435 free_session(ses);
2436 }
2437 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2438 if (clp->cl_nfsd_dentry) {
2439 nfsd_client_rmdir(clp->cl_nfsd_dentry);
2440 clp->cl_nfsd_dentry = NULL;
2441 wake_up_all(&expiry_wq);
2442 }
2443 nfsd4_put_client(clp);
2444 }
2445
2446 /* must be called under the client_lock */
2447 static void
unhash_client_locked(struct nfs4_client * clp)2448 unhash_client_locked(struct nfs4_client *clp)
2449 {
2450 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2451 struct nfsd4_session *ses;
2452
2453 lockdep_assert_held(&nn->client_lock);
2454
2455 /* Mark the client as expired! */
2456 clp->cl_time = 0;
2457 /* Make it invisible */
2458 if (!list_empty(&clp->cl_idhash)) {
2459 list_del_init(&clp->cl_idhash);
2460 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2461 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2462 else
2463 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2464 }
2465 list_del_init(&clp->cl_lru);
2466 spin_lock(&clp->cl_lock);
2467 spin_lock(&nfsd_session_list_lock);
2468 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) {
2469 list_del_init(&ses->se_hash);
2470 list_del_init(&ses->se_all_sessions);
2471 }
2472 spin_unlock(&nfsd_session_list_lock);
2473 spin_unlock(&clp->cl_lock);
2474 }
2475
2476 static void
unhash_client(struct nfs4_client * clp)2477 unhash_client(struct nfs4_client *clp)
2478 {
2479 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2480
2481 spin_lock(&nn->client_lock);
2482 unhash_client_locked(clp);
2483 spin_unlock(&nn->client_lock);
2484 }
2485
mark_client_expired_locked(struct nfs4_client * clp)2486 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2487 {
2488 int users = atomic_read(&clp->cl_rpc_users);
2489
2490 trace_nfsd_mark_client_expired(clp, users);
2491
2492 if (users)
2493 return nfserr_jukebox;
2494 unhash_client_locked(clp);
2495 return nfs_ok;
2496 }
2497
2498 static void
__destroy_client(struct nfs4_client * clp)2499 __destroy_client(struct nfs4_client *clp)
2500 {
2501 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2502 int i;
2503 struct nfs4_openowner *oo;
2504 struct nfs4_delegation *dp;
2505 LIST_HEAD(reaplist);
2506
2507 spin_lock(&state_lock);
2508 while (!list_empty(&clp->cl_delegations)) {
2509 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2510 unhash_delegation_locked(dp, SC_STATUS_CLOSED);
2511 list_add(&dp->dl_recall_lru, &reaplist);
2512 }
2513 spin_unlock(&state_lock);
2514 while (!list_empty(&reaplist)) {
2515 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2516 list_del_init(&dp->dl_recall_lru);
2517 destroy_unhashed_deleg(dp);
2518 }
2519 while (!list_empty(&clp->cl_revoked)) {
2520 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2521 list_del_init(&dp->dl_recall_lru);
2522 nfs4_put_stid(&dp->dl_stid);
2523 }
2524 while (!list_empty(&clp->cl_openowners)) {
2525 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2526 nfs4_get_stateowner(&oo->oo_owner);
2527 release_openowner(oo);
2528 }
2529 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2530 struct nfs4_stateowner *so, *tmp;
2531
2532 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2533 so_strhash) {
2534 /* Should be no openowners at this point */
2535 WARN_ON_ONCE(so->so_is_open_owner);
2536 remove_blocked_locks(lockowner(so));
2537 }
2538 }
2539 nfsd4_return_all_client_layouts(clp);
2540 nfsd4_shutdown_copy(clp);
2541 nfsd4_shutdown_callback(clp);
2542 if (clp->cl_cb_conn.cb_xprt)
2543 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2544 atomic_add_unless(&nn->nfs4_client_count, -1, 0);
2545 nfsd4_dec_courtesy_client_count(nn, clp);
2546 free_client(clp);
2547 wake_up_all(&expiry_wq);
2548 }
2549
2550 static void
destroy_client(struct nfs4_client * clp)2551 destroy_client(struct nfs4_client *clp)
2552 {
2553 unhash_client(clp);
2554 __destroy_client(clp);
2555 }
2556
inc_reclaim_complete(struct nfs4_client * clp)2557 static void inc_reclaim_complete(struct nfs4_client *clp)
2558 {
2559 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2560
2561 if (!nn->track_reclaim_completes)
2562 return;
2563 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2564 return;
2565 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2566 nn->reclaim_str_hashtbl_size) {
2567 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2568 clp->net->ns.inum);
2569 nfsd4_end_grace(nn);
2570 }
2571 }
2572
expire_client(struct nfs4_client * clp)2573 static void expire_client(struct nfs4_client *clp)
2574 {
2575 unhash_client(clp);
2576 nfsd4_client_record_remove(clp);
2577 __destroy_client(clp);
2578 }
2579
copy_verf(struct nfs4_client * target,nfs4_verifier * source)2580 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2581 {
2582 memcpy(target->cl_verifier.data, source->data,
2583 sizeof(target->cl_verifier.data));
2584 }
2585
copy_clid(struct nfs4_client * target,struct nfs4_client * source)2586 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2587 {
2588 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2589 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2590 }
2591
copy_cred(struct svc_cred * target,struct svc_cred * source)2592 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2593 {
2594 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2595 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2596 GFP_KERNEL);
2597 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2598 if ((source->cr_principal && !target->cr_principal) ||
2599 (source->cr_raw_principal && !target->cr_raw_principal) ||
2600 (source->cr_targ_princ && !target->cr_targ_princ))
2601 return -ENOMEM;
2602
2603 target->cr_flavor = source->cr_flavor;
2604 target->cr_uid = source->cr_uid;
2605 target->cr_gid = source->cr_gid;
2606 target->cr_group_info = source->cr_group_info;
2607 get_group_info(target->cr_group_info);
2608 target->cr_gss_mech = source->cr_gss_mech;
2609 if (source->cr_gss_mech)
2610 gss_mech_get(source->cr_gss_mech);
2611 return 0;
2612 }
2613
2614 static int
compare_blob(const struct xdr_netobj * o1,const struct xdr_netobj * o2)2615 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2616 {
2617 if (o1->len < o2->len)
2618 return -1;
2619 if (o1->len > o2->len)
2620 return 1;
2621 return memcmp(o1->data, o2->data, o1->len);
2622 }
2623
2624 static int
same_verf(nfs4_verifier * v1,nfs4_verifier * v2)2625 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2626 {
2627 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2628 }
2629
2630 static int
same_clid(clientid_t * cl1,clientid_t * cl2)2631 same_clid(clientid_t *cl1, clientid_t *cl2)
2632 {
2633 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2634 }
2635
groups_equal(struct group_info * g1,struct group_info * g2)2636 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2637 {
2638 int i;
2639
2640 if (g1->ngroups != g2->ngroups)
2641 return false;
2642 for (i=0; i<g1->ngroups; i++)
2643 if (!gid_eq(g1->gid[i], g2->gid[i]))
2644 return false;
2645 return true;
2646 }
2647
2648 /*
2649 * RFC 3530 language requires clid_inuse be returned when the
2650 * "principal" associated with a requests differs from that previously
2651 * used. We use uid, gid's, and gss principal string as our best
2652 * approximation. We also don't want to allow non-gss use of a client
2653 * established using gss: in theory cr_principal should catch that
2654 * change, but in practice cr_principal can be null even in the gss case
2655 * since gssd doesn't always pass down a principal string.
2656 */
is_gss_cred(struct svc_cred * cr)2657 static bool is_gss_cred(struct svc_cred *cr)
2658 {
2659 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2660 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2661 }
2662
2663
2664 static bool
same_creds(struct svc_cred * cr1,struct svc_cred * cr2)2665 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2666 {
2667 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2668 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2669 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2670 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2671 return false;
2672 /* XXX: check that cr_targ_princ fields match ? */
2673 if (cr1->cr_principal == cr2->cr_principal)
2674 return true;
2675 if (!cr1->cr_principal || !cr2->cr_principal)
2676 return false;
2677 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2678 }
2679
svc_rqst_integrity_protected(struct svc_rqst * rqstp)2680 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2681 {
2682 struct svc_cred *cr = &rqstp->rq_cred;
2683 u32 service;
2684
2685 if (!cr->cr_gss_mech)
2686 return false;
2687 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2688 return service == RPC_GSS_SVC_INTEGRITY ||
2689 service == RPC_GSS_SVC_PRIVACY;
2690 }
2691
nfsd4_mach_creds_match(struct nfs4_client * cl,struct svc_rqst * rqstp)2692 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2693 {
2694 struct svc_cred *cr = &rqstp->rq_cred;
2695
2696 if (!cl->cl_mach_cred)
2697 return true;
2698 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2699 return false;
2700 if (!svc_rqst_integrity_protected(rqstp))
2701 return false;
2702 if (cl->cl_cred.cr_raw_principal)
2703 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2704 cr->cr_raw_principal);
2705 if (!cr->cr_principal)
2706 return false;
2707 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2708 }
2709
gen_confirm(struct nfs4_client * clp,struct nfsd_net * nn)2710 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2711 {
2712 __be32 verf[2];
2713
2714 /*
2715 * This is opaque to client, so no need to byte-swap. Use
2716 * __force to keep sparse happy
2717 */
2718 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2719 verf[1] = (__force __be32)nn->clverifier_counter++;
2720 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2721 }
2722
gen_clid(struct nfs4_client * clp,struct nfsd_net * nn)2723 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2724 {
2725 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2726 clp->cl_clientid.cl_id = nn->clientid_counter++;
2727 gen_confirm(clp, nn);
2728 }
2729
2730 static struct nfs4_stid *
find_stateid_locked(struct nfs4_client * cl,stateid_t * t)2731 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2732 {
2733 struct nfs4_stid *ret;
2734
2735 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2736 if (!ret || !ret->sc_type)
2737 return NULL;
2738 return ret;
2739 }
2740
2741 static struct nfs4_stid *
find_stateid_by_type(struct nfs4_client * cl,stateid_t * t,unsigned short typemask,unsigned short ok_states)2742 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t,
2743 unsigned short typemask, unsigned short ok_states)
2744 {
2745 struct nfs4_stid *s;
2746
2747 spin_lock(&cl->cl_lock);
2748 s = find_stateid_locked(cl, t);
2749 if (s != NULL) {
2750 if ((s->sc_status & ~ok_states) == 0 &&
2751 (typemask & s->sc_type))
2752 refcount_inc(&s->sc_count);
2753 else
2754 s = NULL;
2755 }
2756 spin_unlock(&cl->cl_lock);
2757 return s;
2758 }
2759
get_nfsdfs_clp(struct inode * inode)2760 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2761 {
2762 struct nfsdfs_client *nc;
2763 nc = get_nfsdfs_client(inode);
2764 if (!nc)
2765 return NULL;
2766 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2767 }
2768
seq_quote_mem(struct seq_file * m,char * data,int len)2769 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2770 {
2771 seq_puts(m, "\"");
2772 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2773 seq_puts(m, "\"");
2774 }
2775
cb_state2str(int state)2776 static const char *cb_state2str(int state)
2777 {
2778 switch (state) {
2779 case NFSD4_CB_UP:
2780 return "UP";
2781 case NFSD4_CB_UNKNOWN:
2782 return "UNKNOWN";
2783 case NFSD4_CB_DOWN:
2784 return "DOWN";
2785 case NFSD4_CB_FAULT:
2786 return "FAULT";
2787 }
2788 return "UNDEFINED";
2789 }
2790
client_info_show(struct seq_file * m,void * v)2791 static int client_info_show(struct seq_file *m, void *v)
2792 {
2793 struct inode *inode = file_inode(m->file);
2794 struct nfsd4_session *ses;
2795 struct nfs4_client *clp;
2796 u64 clid;
2797
2798 clp = get_nfsdfs_clp(inode);
2799 if (!clp)
2800 return -ENXIO;
2801 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2802 seq_printf(m, "clientid: 0x%llx\n", clid);
2803 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2804
2805 if (clp->cl_state == NFSD4_COURTESY)
2806 seq_puts(m, "status: courtesy\n");
2807 else if (clp->cl_state == NFSD4_EXPIRABLE)
2808 seq_puts(m, "status: expirable\n");
2809 else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2810 seq_puts(m, "status: confirmed\n");
2811 else
2812 seq_puts(m, "status: unconfirmed\n");
2813 seq_printf(m, "seconds from last renew: %lld\n",
2814 ktime_get_boottime_seconds() - clp->cl_time);
2815 seq_puts(m, "name: ");
2816 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2817 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2818 if (clp->cl_nii_domain.data) {
2819 seq_puts(m, "Implementation domain: ");
2820 seq_quote_mem(m, clp->cl_nii_domain.data,
2821 clp->cl_nii_domain.len);
2822 seq_puts(m, "\nImplementation name: ");
2823 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2824 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2825 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2826 }
2827 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2828 seq_printf(m, "callback address: \"%pISpc\"\n", &clp->cl_cb_conn.cb_addr);
2829 seq_printf(m, "admin-revoked states: %d\n",
2830 atomic_read(&clp->cl_admin_revoked));
2831 spin_lock(&clp->cl_lock);
2832 seq_printf(m, "session slots:");
2833 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2834 seq_printf(m, " %u", ses->se_fchannel.maxreqs);
2835 seq_printf(m, "\nsession target slots:");
2836 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2837 seq_printf(m, " %u", ses->se_target_maxslots);
2838 spin_unlock(&clp->cl_lock);
2839 seq_puts(m, "\n");
2840
2841 nfsd4_put_client(clp);
2842
2843 return 0;
2844 }
2845
2846 DEFINE_SHOW_ATTRIBUTE(client_info);
2847
states_start(struct seq_file * s,loff_t * pos)2848 static void *states_start(struct seq_file *s, loff_t *pos)
2849 __acquires(&clp->cl_lock)
2850 {
2851 struct nfs4_client *clp = s->private;
2852 unsigned long id = *pos;
2853 void *ret;
2854
2855 spin_lock(&clp->cl_lock);
2856 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2857 *pos = id;
2858 return ret;
2859 }
2860
states_next(struct seq_file * s,void * v,loff_t * pos)2861 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2862 {
2863 struct nfs4_client *clp = s->private;
2864 unsigned long id = *pos;
2865 void *ret;
2866
2867 id = *pos;
2868 id++;
2869 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2870 *pos = id;
2871 return ret;
2872 }
2873
states_stop(struct seq_file * s,void * v)2874 static void states_stop(struct seq_file *s, void *v)
2875 __releases(&clp->cl_lock)
2876 {
2877 struct nfs4_client *clp = s->private;
2878
2879 spin_unlock(&clp->cl_lock);
2880 }
2881
nfs4_show_fname(struct seq_file * s,struct nfsd_file * f)2882 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2883 {
2884 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2885 }
2886
nfs4_show_superblock(struct seq_file * s,struct nfsd_file * f)2887 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2888 {
2889 struct inode *inode = file_inode(f->nf_file);
2890
2891 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2892 MAJOR(inode->i_sb->s_dev),
2893 MINOR(inode->i_sb->s_dev),
2894 inode->i_ino);
2895 }
2896
nfs4_show_owner(struct seq_file * s,struct nfs4_stateowner * oo)2897 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2898 {
2899 seq_puts(s, "owner: ");
2900 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2901 }
2902
nfs4_show_stateid(struct seq_file * s,stateid_t * stid)2903 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2904 {
2905 seq_printf(s, "0x%.8x", stid->si_generation);
2906 seq_printf(s, "%12phN", &stid->si_opaque);
2907 }
2908
nfs4_show_open(struct seq_file * s,struct nfs4_stid * st)2909 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2910 {
2911 struct nfs4_ol_stateid *ols;
2912 struct nfs4_file *nf;
2913 struct nfsd_file *file;
2914 struct nfs4_stateowner *oo;
2915 unsigned int access, deny;
2916
2917 ols = openlockstateid(st);
2918 oo = ols->st_stateowner;
2919 nf = st->sc_file;
2920
2921 seq_puts(s, "- ");
2922 nfs4_show_stateid(s, &st->sc_stateid);
2923 seq_puts(s, ": { type: open, ");
2924
2925 access = bmap_to_share_mode(ols->st_access_bmap);
2926 deny = bmap_to_share_mode(ols->st_deny_bmap);
2927
2928 seq_printf(s, "access: %s%s, ",
2929 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2930 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2931 seq_printf(s, "deny: %s%s, ",
2932 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2933 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2934
2935 if (nf) {
2936 spin_lock(&nf->fi_lock);
2937 file = find_any_file_locked(nf);
2938 if (file) {
2939 nfs4_show_superblock(s, file);
2940 seq_puts(s, ", ");
2941 nfs4_show_fname(s, file);
2942 seq_puts(s, ", ");
2943 }
2944 spin_unlock(&nf->fi_lock);
2945 } else
2946 seq_puts(s, "closed, ");
2947 nfs4_show_owner(s, oo);
2948 if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
2949 seq_puts(s, ", admin-revoked");
2950 seq_puts(s, " }\n");
2951 return 0;
2952 }
2953
nfs4_show_lock(struct seq_file * s,struct nfs4_stid * st)2954 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2955 {
2956 struct nfs4_ol_stateid *ols;
2957 struct nfs4_file *nf;
2958 struct nfsd_file *file;
2959 struct nfs4_stateowner *oo;
2960
2961 ols = openlockstateid(st);
2962 oo = ols->st_stateowner;
2963 nf = st->sc_file;
2964
2965 seq_puts(s, "- ");
2966 nfs4_show_stateid(s, &st->sc_stateid);
2967 seq_puts(s, ": { type: lock, ");
2968
2969 spin_lock(&nf->fi_lock);
2970 file = find_any_file_locked(nf);
2971 if (file) {
2972 /*
2973 * Note: a lock stateid isn't really the same thing as a lock,
2974 * it's the locking state held by one owner on a file, and there
2975 * may be multiple (or no) lock ranges associated with it.
2976 * (Same for the matter is true of open stateids.)
2977 */
2978
2979 nfs4_show_superblock(s, file);
2980 /* XXX: open stateid? */
2981 seq_puts(s, ", ");
2982 nfs4_show_fname(s, file);
2983 seq_puts(s, ", ");
2984 }
2985 nfs4_show_owner(s, oo);
2986 if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
2987 seq_puts(s, ", admin-revoked");
2988 seq_puts(s, " }\n");
2989 spin_unlock(&nf->fi_lock);
2990 return 0;
2991 }
2992
nfs4_show_deleg_type(u32 dl_type)2993 static char *nfs4_show_deleg_type(u32 dl_type)
2994 {
2995 switch (dl_type) {
2996 case OPEN_DELEGATE_READ:
2997 return "r";
2998 case OPEN_DELEGATE_WRITE:
2999 return "w";
3000 case OPEN_DELEGATE_READ_ATTRS_DELEG:
3001 return "ra";
3002 case OPEN_DELEGATE_WRITE_ATTRS_DELEG:
3003 return "wa";
3004 }
3005 return "?";
3006 }
3007
nfs4_show_deleg(struct seq_file * s,struct nfs4_stid * st)3008 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
3009 {
3010 struct nfs4_delegation *ds;
3011 struct nfs4_file *nf;
3012 struct nfsd_file *file;
3013
3014 ds = delegstateid(st);
3015 nf = st->sc_file;
3016
3017 seq_puts(s, "- ");
3018 nfs4_show_stateid(s, &st->sc_stateid);
3019 seq_puts(s, ": { type: deleg, ");
3020
3021 seq_printf(s, "access: %s", nfs4_show_deleg_type(ds->dl_type));
3022
3023 /* XXX: lease time, whether it's being recalled. */
3024
3025 spin_lock(&nf->fi_lock);
3026 file = nf->fi_deleg_file;
3027 if (file) {
3028 seq_puts(s, ", ");
3029 nfs4_show_superblock(s, file);
3030 seq_puts(s, ", ");
3031 nfs4_show_fname(s, file);
3032 }
3033 spin_unlock(&nf->fi_lock);
3034 if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
3035 seq_puts(s, ", admin-revoked");
3036 seq_puts(s, " }\n");
3037 return 0;
3038 }
3039
nfs4_show_layout(struct seq_file * s,struct nfs4_stid * st)3040 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
3041 {
3042 struct nfs4_layout_stateid *ls;
3043 struct nfsd_file *file;
3044
3045 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
3046
3047 seq_puts(s, "- ");
3048 nfs4_show_stateid(s, &st->sc_stateid);
3049 seq_puts(s, ": { type: layout");
3050
3051 /* XXX: What else would be useful? */
3052
3053 spin_lock(&ls->ls_stid.sc_file->fi_lock);
3054 file = ls->ls_file;
3055 if (file) {
3056 seq_puts(s, ", ");
3057 nfs4_show_superblock(s, file);
3058 seq_puts(s, ", ");
3059 nfs4_show_fname(s, file);
3060 }
3061 spin_unlock(&ls->ls_stid.sc_file->fi_lock);
3062 if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
3063 seq_puts(s, ", admin-revoked");
3064 seq_puts(s, " }\n");
3065
3066 return 0;
3067 }
3068
states_show(struct seq_file * s,void * v)3069 static int states_show(struct seq_file *s, void *v)
3070 {
3071 struct nfs4_stid *st = v;
3072
3073 switch (st->sc_type) {
3074 case SC_TYPE_OPEN:
3075 return nfs4_show_open(s, st);
3076 case SC_TYPE_LOCK:
3077 return nfs4_show_lock(s, st);
3078 case SC_TYPE_DELEG:
3079 return nfs4_show_deleg(s, st);
3080 case SC_TYPE_LAYOUT:
3081 return nfs4_show_layout(s, st);
3082 default:
3083 return 0; /* XXX: or SEQ_SKIP? */
3084 }
3085 /* XXX: copy stateids? */
3086 }
3087
3088 static struct seq_operations states_seq_ops = {
3089 .start = states_start,
3090 .next = states_next,
3091 .stop = states_stop,
3092 .show = states_show
3093 };
3094
client_states_open(struct inode * inode,struct file * file)3095 static int client_states_open(struct inode *inode, struct file *file)
3096 {
3097 struct seq_file *s;
3098 struct nfs4_client *clp;
3099 int ret;
3100
3101 clp = get_nfsdfs_clp(inode);
3102 if (!clp)
3103 return -ENXIO;
3104
3105 ret = seq_open(file, &states_seq_ops);
3106 if (ret) {
3107 nfsd4_put_client(clp);
3108 return ret;
3109 }
3110 s = file->private_data;
3111 s->private = clp;
3112 return 0;
3113 }
3114
client_opens_release(struct inode * inode,struct file * file)3115 static int client_opens_release(struct inode *inode, struct file *file)
3116 {
3117 struct seq_file *m = file->private_data;
3118 struct nfs4_client *clp = m->private;
3119
3120 /* XXX: alternatively, we could get/drop in seq start/stop */
3121 nfsd4_put_client(clp);
3122 return seq_release(inode, file);
3123 }
3124
3125 static const struct file_operations client_states_fops = {
3126 .open = client_states_open,
3127 .read = seq_read,
3128 .llseek = seq_lseek,
3129 .release = client_opens_release,
3130 };
3131
3132 /*
3133 * Normally we refuse to destroy clients that are in use, but here the
3134 * administrator is telling us to just do it. We also want to wait
3135 * so the caller has a guarantee that the client's locks are gone by
3136 * the time the write returns:
3137 */
force_expire_client(struct nfs4_client * clp)3138 static void force_expire_client(struct nfs4_client *clp)
3139 {
3140 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3141 bool already_expired;
3142
3143 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
3144
3145 spin_lock(&nn->client_lock);
3146 clp->cl_time = 0;
3147 spin_unlock(&nn->client_lock);
3148
3149 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
3150 spin_lock(&nn->client_lock);
3151 already_expired = list_empty(&clp->cl_lru);
3152 if (!already_expired)
3153 unhash_client_locked(clp);
3154 spin_unlock(&nn->client_lock);
3155
3156 if (!already_expired)
3157 expire_client(clp);
3158 else
3159 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
3160 }
3161
client_ctl_write(struct file * file,const char __user * buf,size_t size,loff_t * pos)3162 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
3163 size_t size, loff_t *pos)
3164 {
3165 char *data;
3166 struct nfs4_client *clp;
3167
3168 data = simple_transaction_get(file, buf, size);
3169 if (IS_ERR(data))
3170 return PTR_ERR(data);
3171 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
3172 return -EINVAL;
3173 clp = get_nfsdfs_clp(file_inode(file));
3174 if (!clp)
3175 return -ENXIO;
3176 force_expire_client(clp);
3177 nfsd4_put_client(clp);
3178 return 7;
3179 }
3180
3181 static const struct file_operations client_ctl_fops = {
3182 .write = client_ctl_write,
3183 .release = simple_transaction_release,
3184 };
3185
3186 static const struct tree_descr client_files[] = {
3187 [0] = {"info", &client_info_fops, S_IRUSR},
3188 [1] = {"states", &client_states_fops, S_IRUSR},
3189 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
3190 [3] = {""},
3191 };
3192
3193 static int
nfsd4_cb_recall_any_done(struct nfsd4_callback * cb,struct rpc_task * task)3194 nfsd4_cb_recall_any_done(struct nfsd4_callback *cb,
3195 struct rpc_task *task)
3196 {
3197 trace_nfsd_cb_recall_any_done(cb, task);
3198 switch (task->tk_status) {
3199 case -NFS4ERR_DELAY:
3200 rpc_delay(task, 2 * HZ);
3201 return 0;
3202 default:
3203 return 1;
3204 }
3205 }
3206
3207 static void
nfsd4_cb_recall_any_release(struct nfsd4_callback * cb)3208 nfsd4_cb_recall_any_release(struct nfsd4_callback *cb)
3209 {
3210 struct nfs4_client *clp = cb->cb_clp;
3211
3212 nfsd4_put_client(clp);
3213 }
3214
3215 static int
nfsd4_cb_getattr_done(struct nfsd4_callback * cb,struct rpc_task * task)3216 nfsd4_cb_getattr_done(struct nfsd4_callback *cb, struct rpc_task *task)
3217 {
3218 struct nfs4_cb_fattr *ncf =
3219 container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
3220 struct nfs4_delegation *dp =
3221 container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
3222
3223 trace_nfsd_cb_getattr_done(&dp->dl_stid.sc_stateid, task);
3224 ncf->ncf_cb_status = task->tk_status;
3225 switch (task->tk_status) {
3226 case -NFS4ERR_DELAY:
3227 rpc_delay(task, 2 * HZ);
3228 return 0;
3229 default:
3230 return 1;
3231 }
3232 }
3233
3234 static void
nfsd4_cb_getattr_release(struct nfsd4_callback * cb)3235 nfsd4_cb_getattr_release(struct nfsd4_callback *cb)
3236 {
3237 struct nfs4_cb_fattr *ncf =
3238 container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
3239 struct nfs4_delegation *dp =
3240 container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
3241
3242 nfs4_put_stid(&dp->dl_stid);
3243 }
3244
3245 static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = {
3246 .done = nfsd4_cb_recall_any_done,
3247 .release = nfsd4_cb_recall_any_release,
3248 .opcode = OP_CB_RECALL_ANY,
3249 };
3250
3251 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops = {
3252 .done = nfsd4_cb_getattr_done,
3253 .release = nfsd4_cb_getattr_release,
3254 .opcode = OP_CB_GETATTR,
3255 };
3256
nfs4_cb_getattr(struct nfs4_cb_fattr * ncf)3257 static void nfs4_cb_getattr(struct nfs4_cb_fattr *ncf)
3258 {
3259 struct nfs4_delegation *dp =
3260 container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
3261
3262 if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &ncf->ncf_getattr.cb_flags))
3263 return;
3264
3265 /* set to proper status when nfsd4_cb_getattr_done runs */
3266 ncf->ncf_cb_status = NFS4ERR_IO;
3267
3268 /* ensure that wake_bit is done when RUNNING is cleared */
3269 set_bit(NFSD4_CALLBACK_WAKE, &ncf->ncf_getattr.cb_flags);
3270
3271 refcount_inc(&dp->dl_stid.sc_count);
3272 nfsd4_run_cb(&ncf->ncf_getattr);
3273 }
3274
create_client(struct xdr_netobj name,struct svc_rqst * rqstp,nfs4_verifier * verf)3275 static struct nfs4_client *create_client(struct xdr_netobj name,
3276 struct svc_rqst *rqstp, nfs4_verifier *verf)
3277 {
3278 struct nfs4_client *clp;
3279 struct sockaddr *sa = svc_addr(rqstp);
3280 int ret;
3281 struct net *net = SVC_NET(rqstp);
3282 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3283 struct dentry *dentries[ARRAY_SIZE(client_files)];
3284
3285 clp = alloc_client(name, nn);
3286 if (clp == NULL)
3287 return NULL;
3288
3289 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
3290 if (ret) {
3291 free_client(clp);
3292 return NULL;
3293 }
3294 gen_clid(clp, nn);
3295 kref_init(&clp->cl_nfsdfs.cl_ref);
3296 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
3297 clp->cl_time = ktime_get_boottime_seconds();
3298 copy_verf(clp, verf);
3299 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
3300 clp->cl_cb_session = NULL;
3301 clp->net = net;
3302 clp->cl_nfsd_dentry = nfsd_client_mkdir(
3303 nn, &clp->cl_nfsdfs,
3304 clp->cl_clientid.cl_id - nn->clientid_base,
3305 client_files, dentries);
3306 clp->cl_nfsd_info_dentry = dentries[0];
3307 if (!clp->cl_nfsd_dentry) {
3308 free_client(clp);
3309 return NULL;
3310 }
3311 clp->cl_ra = kzalloc_obj(*clp->cl_ra);
3312 if (!clp->cl_ra) {
3313 free_client(clp);
3314 return NULL;
3315 }
3316 clp->cl_ra_time = 0;
3317 nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops,
3318 NFSPROC4_CLNT_CB_RECALL_ANY);
3319 return clp;
3320 }
3321
3322 static void
add_clp_to_name_tree(struct nfs4_client * new_clp,struct rb_root * root)3323 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
3324 {
3325 struct rb_node **new = &(root->rb_node), *parent = NULL;
3326 struct nfs4_client *clp;
3327
3328 while (*new) {
3329 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
3330 parent = *new;
3331
3332 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
3333 new = &((*new)->rb_left);
3334 else
3335 new = &((*new)->rb_right);
3336 }
3337
3338 rb_link_node(&new_clp->cl_namenode, parent, new);
3339 rb_insert_color(&new_clp->cl_namenode, root);
3340 }
3341
3342 static struct nfs4_client *
find_clp_in_name_tree(struct xdr_netobj * name,struct rb_root * root)3343 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
3344 {
3345 int cmp;
3346 struct rb_node *node = root->rb_node;
3347 struct nfs4_client *clp;
3348
3349 while (node) {
3350 clp = rb_entry(node, struct nfs4_client, cl_namenode);
3351 cmp = compare_blob(&clp->cl_name, name);
3352 if (cmp > 0)
3353 node = node->rb_left;
3354 else if (cmp < 0)
3355 node = node->rb_right;
3356 else
3357 return clp;
3358 }
3359 return NULL;
3360 }
3361
3362 static void
add_to_unconfirmed(struct nfs4_client * clp)3363 add_to_unconfirmed(struct nfs4_client *clp)
3364 {
3365 unsigned int idhashval;
3366 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3367
3368 lockdep_assert_held(&nn->client_lock);
3369
3370 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3371 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
3372 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3373 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
3374 renew_client_locked(clp);
3375 }
3376
3377 static void
move_to_confirmed(struct nfs4_client * clp)3378 move_to_confirmed(struct nfs4_client *clp)
3379 {
3380 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3381 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3382
3383 lockdep_assert_held(&nn->client_lock);
3384
3385 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
3386 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
3387 add_clp_to_name_tree(clp, &nn->conf_name_tree);
3388 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3389 trace_nfsd_clid_confirmed(&clp->cl_clientid);
3390 renew_client_locked(clp);
3391 }
3392
3393 static struct nfs4_client *
find_client_in_id_table(struct list_head * tbl,clientid_t * clid,bool sessions)3394 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
3395 {
3396 struct nfs4_client *clp;
3397 unsigned int idhashval = clientid_hashval(clid->cl_id);
3398
3399 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
3400 if (same_clid(&clp->cl_clientid, clid)) {
3401 if ((bool)clp->cl_minorversion != sessions)
3402 return NULL;
3403 renew_client_locked(clp);
3404 return clp;
3405 }
3406 }
3407 return NULL;
3408 }
3409
3410 static struct nfs4_client *
find_confirmed_client(clientid_t * clid,bool sessions,struct nfsd_net * nn)3411 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3412 {
3413 struct list_head *tbl = nn->conf_id_hashtbl;
3414
3415 lockdep_assert_held(&nn->client_lock);
3416 return find_client_in_id_table(tbl, clid, sessions);
3417 }
3418
3419 static struct nfs4_client *
find_unconfirmed_client(clientid_t * clid,bool sessions,struct nfsd_net * nn)3420 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3421 {
3422 struct list_head *tbl = nn->unconf_id_hashtbl;
3423
3424 lockdep_assert_held(&nn->client_lock);
3425 return find_client_in_id_table(tbl, clid, sessions);
3426 }
3427
clp_used_exchangeid(struct nfs4_client * clp)3428 static bool clp_used_exchangeid(struct nfs4_client *clp)
3429 {
3430 return clp->cl_exchange_flags != 0;
3431 }
3432
3433 static struct nfs4_client *
find_confirmed_client_by_name(struct xdr_netobj * name,struct nfsd_net * nn)3434 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3435 {
3436 lockdep_assert_held(&nn->client_lock);
3437 return find_clp_in_name_tree(name, &nn->conf_name_tree);
3438 }
3439
3440 static struct nfs4_client *
find_unconfirmed_client_by_name(struct xdr_netobj * name,struct nfsd_net * nn)3441 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3442 {
3443 lockdep_assert_held(&nn->client_lock);
3444 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
3445 }
3446
3447 static void
gen_callback(struct nfs4_client * clp,struct nfsd4_setclientid * se,struct svc_rqst * rqstp)3448 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
3449 {
3450 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
3451 struct sockaddr *sa = svc_addr(rqstp);
3452 u32 scopeid = rpc_get_scope_id(sa);
3453 unsigned short expected_family;
3454
3455 /* Currently, we only support tcp and tcp6 for the callback channel */
3456 if (se->se_callback_netid_len == 3 &&
3457 !memcmp(se->se_callback_netid_val, "tcp", 3))
3458 expected_family = AF_INET;
3459 else if (se->se_callback_netid_len == 4 &&
3460 !memcmp(se->se_callback_netid_val, "tcp6", 4))
3461 expected_family = AF_INET6;
3462 else
3463 goto out_err;
3464
3465 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3466 se->se_callback_addr_len,
3467 (struct sockaddr *)&conn->cb_addr,
3468 sizeof(conn->cb_addr));
3469
3470 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
3471 goto out_err;
3472
3473 if (conn->cb_addr.ss_family == AF_INET6)
3474 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3475
3476 conn->cb_prog = se->se_callback_prog;
3477 conn->cb_ident = se->se_callback_ident;
3478 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
3479 trace_nfsd_cb_args(clp, conn);
3480 return;
3481 out_err:
3482 conn->cb_addr.ss_family = AF_UNSPEC;
3483 conn->cb_addrlen = 0;
3484 trace_nfsd_cb_nodelegs(clp);
3485 return;
3486 }
3487
3488 /*
3489 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3490 */
3491 static void
nfsd4_store_cache_entry(struct nfsd4_compoundres * resp)3492 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3493 {
3494 struct xdr_buf *buf = resp->xdr->buf;
3495 struct nfsd4_slot *slot = resp->cstate.slot;
3496 unsigned int base;
3497
3498 /*
3499 * RFC 5661 Section 2.10.6.1.2:
3500 *
3501 * Any time SEQUENCE ... returns an error ... [t]he replier MUST NOT
3502 * modify the reply cache entry for the slot whenever an error is
3503 * returned from SEQUENCE ...
3504 *
3505 * Because nfsd4_store_cache_entry is called only by
3506 * nfsd4_sequence_done(), nfsd4_store_cache_entry() is called only
3507 * when a SEQUENCE operation was part of the COMPOUND.
3508 * nfs41_check_op_ordering() ensures SEQUENCE is the first op.
3509 */
3510 if (resp->opcnt == 1 && resp->cstate.status != nfs_ok)
3511 return;
3512
3513 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3514 slot->sl_opcnt = resp->opcnt;
3515 slot->sl_status = resp->cstate.status;
3516 free_svc_cred(&slot->sl_cred);
3517 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3518
3519 if (!(resp->cstate.slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
3520 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3521 return;
3522 }
3523 slot->sl_flags |= NFSD4_SLOT_CACHED;
3524
3525 base = resp->cstate.data_offset;
3526 slot->sl_datalen = buf->len - base;
3527 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3528 WARN(1, "%s: sessions DRC could not cache compound\n",
3529 __func__);
3530 return;
3531 }
3532
3533 /*
3534 * The sequence operation is not cached because we can use the slot and
3535 * session values.
3536 */
3537 static __be32
nfsd4_replay_cache_entry(struct nfsd4_compoundres * resp,struct nfsd4_sequence * seq)3538 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3539 struct nfsd4_sequence *seq)
3540 {
3541 struct nfsd4_compoundargs *args = resp->rqstp->rq_argp;
3542 struct nfsd4_slot *slot = resp->cstate.slot;
3543 struct xdr_stream *xdr = resp->xdr;
3544 __be32 *p;
3545
3546 dprintk("--> %s slot %p\n", __func__, slot);
3547
3548 /* Always encode the SEQUENCE response. */
3549 nfsd4_encode_operation(resp, &args->ops[0]);
3550 if (args->opcnt == 1)
3551 /* A solo SEQUENCE - nothing was cached */
3552 return args->ops[0].status;
3553
3554 if (!(slot->sl_flags & NFSD4_SLOT_CACHED)) {
3555 /* We weren't asked to cache this. */
3556 struct nfsd4_op *op;
3557
3558 op = &args->ops[resp->opcnt++];
3559 op->status = nfserr_retry_uncached_rep;
3560 nfsd4_encode_operation(resp, op);
3561 return op->status;
3562 }
3563
3564 /* return reply from cache */
3565 p = xdr_reserve_space(xdr, slot->sl_datalen);
3566 if (!p) {
3567 WARN_ON_ONCE(1);
3568 return nfserr_serverfault;
3569 }
3570 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3571 xdr_commit_encode(xdr);
3572
3573 resp->opcnt = slot->sl_opcnt;
3574 return slot->sl_status;
3575 }
3576
3577 /*
3578 * Set the exchange_id flags returned by the server.
3579 */
3580 static void
nfsd4_set_ex_flags(struct nfs4_client * new,struct nfsd4_exchange_id * clid)3581 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3582 {
3583 #ifdef CONFIG_NFSD_PNFS
3584 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3585 #else
3586 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3587 #endif
3588
3589 /* Referrals are supported, Migration is not. */
3590 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3591
3592 /* set the wire flags to return to client. */
3593 clid->flags = new->cl_exchange_flags;
3594 }
3595
client_has_openowners(struct nfs4_client * clp)3596 static bool client_has_openowners(struct nfs4_client *clp)
3597 {
3598 struct nfs4_openowner *oo;
3599
3600 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3601 if (!list_empty(&oo->oo_owner.so_stateids))
3602 return true;
3603 }
3604 return false;
3605 }
3606
client_has_state(struct nfs4_client * clp)3607 static bool client_has_state(struct nfs4_client *clp)
3608 {
3609 return client_has_openowners(clp)
3610 #ifdef CONFIG_NFSD_PNFS
3611 || !list_empty(&clp->cl_lo_states)
3612 #endif
3613 || !list_empty(&clp->cl_delegations)
3614 || !list_empty(&clp->cl_sessions)
3615 || nfsd4_has_active_async_copies(clp);
3616 }
3617
copy_impl_id(struct nfs4_client * clp,struct nfsd4_exchange_id * exid)3618 static __be32 copy_impl_id(struct nfs4_client *clp,
3619 struct nfsd4_exchange_id *exid)
3620 {
3621 if (!exid->nii_domain.data)
3622 return 0;
3623 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3624 if (!clp->cl_nii_domain.data)
3625 return nfserr_jukebox;
3626 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3627 if (!clp->cl_nii_name.data)
3628 return nfserr_jukebox;
3629 clp->cl_nii_time = exid->nii_time;
3630 return 0;
3631 }
3632
3633 __be32
nfsd4_exchange_id(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3634 nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3635 union nfsd4_op_u *u)
3636 {
3637 struct nfsd4_exchange_id *exid = &u->exchange_id;
3638 struct nfs4_client *conf, *new;
3639 struct nfs4_client *unconf = NULL;
3640 __be32 status;
3641 char addr_str[INET6_ADDRSTRLEN];
3642 nfs4_verifier verf = exid->verifier;
3643 struct sockaddr *sa = svc_addr(rqstp);
3644 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3645 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3646
3647 rpc_ntop(sa, addr_str, sizeof(addr_str));
3648 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3649 "ip_addr=%s flags %x, spa_how %u\n",
3650 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
3651 addr_str, exid->flags, exid->spa_how);
3652
3653 exid->server_impl_name = kasprintf(GFP_KERNEL, "%s %s %s %s",
3654 utsname()->sysname, utsname()->release,
3655 utsname()->version, utsname()->machine);
3656 if (!exid->server_impl_name)
3657 return nfserr_jukebox;
3658
3659 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3660 return nfserr_inval;
3661
3662 new = create_client(exid->clname, rqstp, &verf);
3663 if (new == NULL)
3664 return nfserr_jukebox;
3665 status = copy_impl_id(new, exid);
3666 if (status)
3667 goto out_nolock;
3668
3669 switch (exid->spa_how) {
3670 case SP4_MACH_CRED:
3671 exid->spo_must_enforce[0] = 0;
3672 exid->spo_must_enforce[1] = (
3673 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3674 1 << (OP_EXCHANGE_ID - 32) |
3675 1 << (OP_CREATE_SESSION - 32) |
3676 1 << (OP_DESTROY_SESSION - 32) |
3677 1 << (OP_DESTROY_CLIENTID - 32));
3678
3679 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3680 1 << (OP_OPEN_DOWNGRADE) |
3681 1 << (OP_LOCKU) |
3682 1 << (OP_DELEGRETURN));
3683
3684 exid->spo_must_allow[1] &= (
3685 1 << (OP_TEST_STATEID - 32) |
3686 1 << (OP_FREE_STATEID - 32));
3687 if (!svc_rqst_integrity_protected(rqstp)) {
3688 status = nfserr_inval;
3689 goto out_nolock;
3690 }
3691 /*
3692 * Sometimes userspace doesn't give us a principal.
3693 * Which is a bug, really. Anyway, we can't enforce
3694 * MACH_CRED in that case, better to give up now:
3695 */
3696 if (!new->cl_cred.cr_principal &&
3697 !new->cl_cred.cr_raw_principal) {
3698 status = nfserr_serverfault;
3699 goto out_nolock;
3700 }
3701 new->cl_mach_cred = true;
3702 break;
3703 case SP4_NONE:
3704 break;
3705 default: /* checked by xdr code */
3706 WARN_ON_ONCE(1);
3707 fallthrough;
3708 case SP4_SSV:
3709 status = nfserr_encr_alg_unsupp;
3710 goto out_nolock;
3711 }
3712
3713 /* Cases below refer to rfc 5661 section 18.35.4: */
3714 spin_lock(&nn->client_lock);
3715 conf = find_confirmed_client_by_name(&exid->clname, nn);
3716 if (conf) {
3717 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3718 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3719
3720 if (update) {
3721 if (!clp_used_exchangeid(conf)) { /* buggy client */
3722 status = nfserr_inval;
3723 goto out;
3724 }
3725 if (!nfsd4_mach_creds_match(conf, rqstp)) {
3726 status = nfserr_wrong_cred;
3727 goto out;
3728 }
3729 if (!creds_match) { /* case 9 */
3730 status = nfserr_perm;
3731 goto out;
3732 }
3733 if (!verfs_match) { /* case 8 */
3734 status = nfserr_not_same;
3735 goto out;
3736 }
3737 /* case 6 */
3738 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3739 trace_nfsd_clid_confirmed_r(conf);
3740 goto out_copy;
3741 }
3742 if (!creds_match) { /* case 3 */
3743 if (client_has_state(conf)) {
3744 status = nfserr_clid_inuse;
3745 trace_nfsd_clid_cred_mismatch(conf, rqstp);
3746 goto out;
3747 }
3748 goto out_new;
3749 }
3750 if (verfs_match) { /* case 2 */
3751 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3752 trace_nfsd_clid_confirmed_r(conf);
3753 goto out_copy;
3754 }
3755 /* case 5, client reboot */
3756 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
3757 conf = NULL;
3758 goto out_new;
3759 }
3760
3761 if (update) { /* case 7 */
3762 status = nfserr_noent;
3763 goto out;
3764 }
3765
3766 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
3767 if (unconf) /* case 4, possible retry or client restart */
3768 unhash_client_locked(unconf);
3769
3770 /* case 1, new owner ID */
3771 trace_nfsd_clid_fresh(new);
3772
3773 out_new:
3774 if (conf) {
3775 status = mark_client_expired_locked(conf);
3776 if (status)
3777 goto out;
3778 trace_nfsd_clid_replaced(&conf->cl_clientid);
3779 }
3780 new->cl_minorversion = cstate->minorversion;
3781 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3782 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3783
3784 /* Contrived initial CREATE_SESSION response */
3785 new->cl_cs_slot.sl_status = nfserr_seq_misordered;
3786
3787 add_to_unconfirmed(new);
3788 swap(new, conf);
3789 out_copy:
3790 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3791 exid->clientid.cl_id = conf->cl_clientid.cl_id;
3792
3793 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3794 nfsd4_set_ex_flags(conf, exid);
3795
3796 exid->nii_domain.len = sizeof("kernel.org") - 1;
3797 exid->nii_domain.data = "kernel.org";
3798
3799 /*
3800 * Note that RFC 8881 places no length limit on
3801 * nii_name, but this implementation permits no
3802 * more than NFS4_OPAQUE_LIMIT bytes.
3803 */
3804 exid->nii_name.len = strlen(exid->server_impl_name);
3805 if (exid->nii_name.len > NFS4_OPAQUE_LIMIT)
3806 exid->nii_name.len = NFS4_OPAQUE_LIMIT;
3807 exid->nii_name.data = exid->server_impl_name;
3808
3809 /* just send zeros - the date is in nii_name */
3810 exid->nii_time.tv_sec = 0;
3811 exid->nii_time.tv_nsec = 0;
3812
3813 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3814 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3815 status = nfs_ok;
3816
3817 out:
3818 spin_unlock(&nn->client_lock);
3819 out_nolock:
3820 if (new)
3821 expire_client(new);
3822 if (unconf) {
3823 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
3824 expire_client(unconf);
3825 }
3826 return status;
3827 }
3828
3829 void
nfsd4_exchange_id_release(union nfsd4_op_u * u)3830 nfsd4_exchange_id_release(union nfsd4_op_u *u)
3831 {
3832 struct nfsd4_exchange_id *exid = &u->exchange_id;
3833
3834 kfree(exid->server_impl_name);
3835 }
3836
check_slot_seqid(u32 seqid,u32 slot_seqid,u8 flags)3837 static __be32 check_slot_seqid(u32 seqid, u32 slot_seqid, u8 flags)
3838 {
3839 /* The slot is in use, and no response has been sent. */
3840 if (flags & NFSD4_SLOT_INUSE) {
3841 if (seqid == slot_seqid)
3842 return nfserr_jukebox;
3843 else
3844 return nfserr_seq_misordered;
3845 }
3846 /* Note unsigned 32-bit arithmetic handles wraparound: */
3847 if (likely(seqid == slot_seqid + 1))
3848 return nfs_ok;
3849 if ((flags & NFSD4_SLOT_REUSED) && seqid == 1)
3850 return nfs_ok;
3851 if (seqid == slot_seqid)
3852 return nfserr_replay_cache;
3853 return nfserr_seq_misordered;
3854 }
3855
3856 /*
3857 * Cache the create session result into the create session single DRC
3858 * slot cache by saving the xdr structure. sl_seqid has been set.
3859 * Do this for solo or embedded create session operations.
3860 */
3861 static void
nfsd4_cache_create_session(struct nfsd4_create_session * cr_ses,struct nfsd4_clid_slot * slot,__be32 nfserr)3862 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3863 struct nfsd4_clid_slot *slot, __be32 nfserr)
3864 {
3865 slot->sl_status = nfserr;
3866 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3867 }
3868
3869 static __be32
nfsd4_replay_create_session(struct nfsd4_create_session * cr_ses,struct nfsd4_clid_slot * slot)3870 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3871 struct nfsd4_clid_slot *slot)
3872 {
3873 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3874 return slot->sl_status;
3875 }
3876
3877 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3878 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3879 1 + /* MIN tag is length with zero, only length */ \
3880 3 + /* version, opcount, opcode */ \
3881 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3882 /* seqid, slotID, slotID, cache */ \
3883 4 ) * sizeof(__be32))
3884
3885 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3886 2 + /* verifier: AUTH_NULL, length 0 */\
3887 1 + /* status */ \
3888 1 + /* MIN tag is length with zero, only length */ \
3889 3 + /* opcount, opcode, opstatus*/ \
3890 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3891 /* seqid, slotID, slotID, slotID, status */ \
3892 5 ) * sizeof(__be32))
3893
check_forechannel_attrs(struct nfsd4_channel_attrs * ca,struct nfsd_net * nn)3894 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3895 {
3896 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3897
3898 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3899 return nfserr_toosmall;
3900 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3901 return nfserr_toosmall;
3902 ca->headerpadsz = 0;
3903 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3904 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3905 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3906 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3907 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3908 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3909
3910 return nfs_ok;
3911 }
3912
3913 /*
3914 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3915 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3916 */
3917 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3918 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3919
3920 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3921 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3922
3923 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3924 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3925 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3926 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3927 sizeof(__be32))
3928
check_backchannel_attrs(struct nfsd4_channel_attrs * ca)3929 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3930 {
3931 ca->headerpadsz = 0;
3932
3933 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3934 return nfserr_toosmall;
3935 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3936 return nfserr_toosmall;
3937 ca->maxresp_cached = 0;
3938 if (ca->maxops < 2)
3939 return nfserr_toosmall;
3940
3941 return nfs_ok;
3942 }
3943
nfsd4_check_cb_sec(struct nfsd4_cb_sec * cbs)3944 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3945 {
3946 switch (cbs->flavor) {
3947 case RPC_AUTH_NULL:
3948 case RPC_AUTH_UNIX:
3949 return nfs_ok;
3950 default:
3951 /*
3952 * GSS case: the spec doesn't allow us to return this
3953 * error. But it also doesn't allow us not to support
3954 * GSS.
3955 * I'd rather this fail hard than return some error the
3956 * client might think it can already handle:
3957 */
3958 return nfserr_encr_alg_unsupp;
3959 }
3960 }
3961
3962 __be32
nfsd4_create_session(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3963 nfsd4_create_session(struct svc_rqst *rqstp,
3964 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3965 {
3966 struct nfsd4_create_session *cr_ses = &u->create_session;
3967 struct sockaddr *sa = svc_addr(rqstp);
3968 struct nfs4_client *conf, *unconf;
3969 struct nfsd4_clid_slot *cs_slot;
3970 struct nfs4_client *old = NULL;
3971 struct nfsd4_session *new;
3972 struct nfsd4_conn *conn;
3973 __be32 status = 0;
3974 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3975
3976 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3977 return nfserr_inval;
3978 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3979 if (status)
3980 return status;
3981 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3982 if (status)
3983 return status;
3984 status = check_backchannel_attrs(&cr_ses->back_channel);
3985 if (status)
3986 goto out_err;
3987 status = nfserr_jukebox;
3988 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3989 if (!new)
3990 goto out_err;
3991 conn = alloc_conn_from_crses(rqstp, cr_ses);
3992 if (!conn)
3993 goto out_free_session;
3994
3995 spin_lock(&nn->client_lock);
3996
3997 /* RFC 8881 Section 18.36.4 Phase 1: Client record look-up. */
3998 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3999 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
4000 if (!conf && !unconf) {
4001 status = nfserr_stale_clientid;
4002 goto out_free_conn;
4003 }
4004
4005 /* RFC 8881 Section 18.36.4 Phase 2: Sequence ID processing. */
4006 if (conf) {
4007 cs_slot = &conf->cl_cs_slot;
4008 trace_nfsd_slot_seqid_conf(conf, cr_ses);
4009 } else {
4010 cs_slot = &unconf->cl_cs_slot;
4011 trace_nfsd_slot_seqid_unconf(unconf, cr_ses);
4012 }
4013 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
4014 switch (status) {
4015 case nfs_ok:
4016 cs_slot->sl_seqid++;
4017 cr_ses->seqid = cs_slot->sl_seqid;
4018 break;
4019 case nfserr_replay_cache:
4020 status = nfsd4_replay_create_session(cr_ses, cs_slot);
4021 fallthrough;
4022 case nfserr_jukebox:
4023 /* The server MUST NOT cache NFS4ERR_DELAY */
4024 goto out_free_conn;
4025 default:
4026 goto out_cache_error;
4027 }
4028
4029 /* RFC 8881 Section 18.36.4 Phase 3: Client ID confirmation. */
4030 if (conf) {
4031 status = nfserr_wrong_cred;
4032 if (!nfsd4_mach_creds_match(conf, rqstp))
4033 goto out_cache_error;
4034 } else {
4035 status = nfserr_clid_inuse;
4036 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
4037 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
4038 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4039 goto out_cache_error;
4040 }
4041 status = nfserr_wrong_cred;
4042 if (!nfsd4_mach_creds_match(unconf, rqstp))
4043 goto out_cache_error;
4044 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4045 if (old) {
4046 status = mark_client_expired_locked(old);
4047 if (status)
4048 goto out_expired_error;
4049 trace_nfsd_clid_replaced(&old->cl_clientid);
4050 }
4051 move_to_confirmed(unconf);
4052 conf = unconf;
4053 }
4054
4055 /* RFC 8881 Section 18.36.4 Phase 4: Session creation. */
4056 status = nfs_ok;
4057 /* Persistent sessions are not supported */
4058 cr_ses->flags &= ~SESSION4_PERSIST;
4059 /* Upshifting from TCP to RDMA is not supported */
4060 cr_ses->flags &= ~SESSION4_RDMA;
4061 /* Report the correct number of backchannel slots */
4062 cr_ses->back_channel.maxreqs = new->se_cb_highest_slot + 1;
4063
4064 init_session(rqstp, new, conf, cr_ses);
4065 nfsd4_get_session_locked(new);
4066
4067 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
4068 NFS4_MAX_SESSIONID_LEN);
4069
4070 /* cache solo and embedded create sessions under the client_lock */
4071 nfsd4_cache_create_session(cr_ses, cs_slot, status);
4072 spin_unlock(&nn->client_lock);
4073 if (conf == unconf)
4074 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4075 /* init connection and backchannel */
4076 nfsd4_init_conn(rqstp, conn, new);
4077 nfsd4_put_session(new);
4078 if (old)
4079 expire_client(old);
4080 return status;
4081
4082 out_expired_error:
4083 /*
4084 * Revert the slot seq_nr change so the server will process
4085 * the client's resend instead of returning a cached response.
4086 */
4087 if (status == nfserr_jukebox) {
4088 cs_slot->sl_seqid--;
4089 cr_ses->seqid = cs_slot->sl_seqid;
4090 goto out_free_conn;
4091 }
4092 out_cache_error:
4093 nfsd4_cache_create_session(cr_ses, cs_slot, status);
4094 out_free_conn:
4095 spin_unlock(&nn->client_lock);
4096 free_conn(conn);
4097 out_free_session:
4098 __free_session(new);
4099 out_err:
4100 return status;
4101 }
4102
nfsd4_map_bcts_dir(u32 * dir)4103 static __be32 nfsd4_map_bcts_dir(u32 *dir)
4104 {
4105 switch (*dir) {
4106 case NFS4_CDFC4_FORE:
4107 case NFS4_CDFC4_BACK:
4108 return nfs_ok;
4109 case NFS4_CDFC4_FORE_OR_BOTH:
4110 case NFS4_CDFC4_BACK_OR_BOTH:
4111 *dir = NFS4_CDFC4_BOTH;
4112 return nfs_ok;
4113 }
4114 return nfserr_inval;
4115 }
4116
nfsd4_backchannel_ctl(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4117 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
4118 struct nfsd4_compound_state *cstate,
4119 union nfsd4_op_u *u)
4120 {
4121 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
4122 struct nfsd4_session *session = cstate->session;
4123 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4124 __be32 status;
4125
4126 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
4127 if (status)
4128 return status;
4129 spin_lock(&nn->client_lock);
4130 session->se_cb_prog = bc->bc_cb_program;
4131 session->se_cb_sec = bc->bc_cb_sec;
4132 spin_unlock(&nn->client_lock);
4133
4134 nfsd4_probe_callback(session->se_client);
4135
4136 return nfs_ok;
4137 }
4138
__nfsd4_find_conn(struct svc_xprt * xpt,struct nfsd4_session * s)4139 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
4140 {
4141 struct nfsd4_conn *c;
4142
4143 list_for_each_entry(c, &s->se_conns, cn_persession) {
4144 if (c->cn_xprt == xpt) {
4145 return c;
4146 }
4147 }
4148 return NULL;
4149 }
4150
nfsd4_match_existing_connection(struct svc_rqst * rqst,struct nfsd4_session * session,u32 req,struct nfsd4_conn ** conn)4151 static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
4152 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
4153 {
4154 struct nfs4_client *clp = session->se_client;
4155 struct svc_xprt *xpt = rqst->rq_xprt;
4156 struct nfsd4_conn *c;
4157 __be32 status;
4158
4159 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
4160 spin_lock(&clp->cl_lock);
4161 c = __nfsd4_find_conn(xpt, session);
4162 if (!c)
4163 status = nfserr_noent;
4164 else if (req == c->cn_flags)
4165 status = nfs_ok;
4166 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
4167 c->cn_flags != NFS4_CDFC4_BACK)
4168 status = nfs_ok;
4169 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
4170 c->cn_flags != NFS4_CDFC4_FORE)
4171 status = nfs_ok;
4172 else
4173 status = nfserr_inval;
4174 spin_unlock(&clp->cl_lock);
4175 if (status == nfs_ok && conn)
4176 *conn = c;
4177 return status;
4178 }
4179
nfsd4_bind_conn_to_session(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4180 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
4181 struct nfsd4_compound_state *cstate,
4182 union nfsd4_op_u *u)
4183 {
4184 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
4185 __be32 status;
4186 struct nfsd4_conn *conn;
4187 struct nfsd4_session *session;
4188 struct net *net = SVC_NET(rqstp);
4189 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4190
4191 if (!nfsd4_last_compound_op(rqstp))
4192 return nfserr_not_only_op;
4193 spin_lock(&nn->client_lock);
4194 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
4195 spin_unlock(&nn->client_lock);
4196 if (!session)
4197 goto out_no_session;
4198 status = nfserr_wrong_cred;
4199 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
4200 goto out;
4201 status = nfsd4_match_existing_connection(rqstp, session,
4202 bcts->dir, &conn);
4203 if (status == nfs_ok) {
4204 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
4205 bcts->dir == NFS4_CDFC4_BACK)
4206 conn->cn_flags |= NFS4_CDFC4_BACK;
4207 nfsd4_probe_callback(session->se_client);
4208 goto out;
4209 }
4210 if (status == nfserr_inval)
4211 goto out;
4212 status = nfsd4_map_bcts_dir(&bcts->dir);
4213 if (status)
4214 goto out;
4215 conn = alloc_conn(rqstp, bcts->dir);
4216 status = nfserr_jukebox;
4217 if (!conn)
4218 goto out;
4219 nfsd4_init_conn(rqstp, conn, session);
4220 status = nfs_ok;
4221 out:
4222 nfsd4_put_session(session);
4223 out_no_session:
4224 return status;
4225 }
4226
nfsd4_compound_in_session(struct nfsd4_compound_state * cstate,struct nfs4_sessionid * sid)4227 static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
4228 {
4229 if (!cstate->session)
4230 return false;
4231 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
4232 }
4233
4234 __be32
nfsd4_destroy_session(struct svc_rqst * r,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4235 nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
4236 union nfsd4_op_u *u)
4237 {
4238 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
4239 struct nfsd4_session *ses;
4240 __be32 status;
4241 int ref_held_by_me = 0;
4242 struct net *net = SVC_NET(r);
4243 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4244
4245 status = nfserr_not_only_op;
4246 if (nfsd4_compound_in_session(cstate, sessionid)) {
4247 if (!nfsd4_last_compound_op(r))
4248 goto out;
4249 ref_held_by_me++;
4250 }
4251 dump_sessionid(__func__, sessionid);
4252 spin_lock(&nn->client_lock);
4253 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
4254 if (!ses)
4255 goto out_client_lock;
4256 status = nfserr_wrong_cred;
4257 if (!nfsd4_mach_creds_match(ses->se_client, r))
4258 goto out_put_session;
4259 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
4260 if (status)
4261 goto out_put_session;
4262 unhash_session(ses);
4263 spin_unlock(&nn->client_lock);
4264
4265 nfsd4_probe_callback_sync(ses->se_client);
4266
4267 spin_lock(&nn->client_lock);
4268 status = nfs_ok;
4269 out_put_session:
4270 nfsd4_put_session_locked(ses);
4271 out_client_lock:
4272 spin_unlock(&nn->client_lock);
4273 out:
4274 return status;
4275 }
4276
nfsd4_sequence_check_conn(struct nfsd4_conn * new,struct nfsd4_session * ses)4277 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
4278 {
4279 struct nfs4_client *clp = ses->se_client;
4280 struct nfsd4_conn *c;
4281 __be32 status = nfs_ok;
4282 int ret;
4283
4284 spin_lock(&clp->cl_lock);
4285 c = __nfsd4_find_conn(new->cn_xprt, ses);
4286 if (c)
4287 goto out_free;
4288 status = nfserr_conn_not_bound_to_session;
4289 if (clp->cl_mach_cred)
4290 goto out_free;
4291 __nfsd4_hash_conn(new, ses);
4292 spin_unlock(&clp->cl_lock);
4293 ret = nfsd4_register_conn(new);
4294 if (ret)
4295 /* oops; xprt is already down: */
4296 nfsd4_conn_lost(&new->cn_xpt_user);
4297 return nfs_ok;
4298 out_free:
4299 spin_unlock(&clp->cl_lock);
4300 free_conn(new);
4301 return status;
4302 }
4303
nfsd4_session_too_many_ops(struct svc_rqst * rqstp,struct nfsd4_session * session)4304 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
4305 {
4306 struct nfsd4_compoundargs *args = rqstp->rq_argp;
4307
4308 return args->opcnt > session->se_fchannel.maxops;
4309 }
4310
nfsd4_request_too_big(struct svc_rqst * rqstp,struct nfsd4_session * session)4311 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
4312 struct nfsd4_session *session)
4313 {
4314 struct xdr_buf *xb = &rqstp->rq_arg;
4315
4316 return xb->len > session->se_fchannel.maxreq_sz;
4317 }
4318
replay_matches_cache(struct svc_rqst * rqstp,struct nfsd4_sequence * seq,struct nfsd4_slot * slot)4319 static bool replay_matches_cache(struct svc_rqst *rqstp,
4320 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
4321 {
4322 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
4323
4324 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
4325 (bool)seq->cachethis)
4326 return false;
4327 /*
4328 * If there's an error then the reply can have fewer ops than
4329 * the call.
4330 */
4331 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
4332 return false;
4333 /*
4334 * But if we cached a reply with *more* ops than the call you're
4335 * sending us now, then this new call is clearly not really a
4336 * replay of the old one:
4337 */
4338 if (slot->sl_opcnt > argp->opcnt)
4339 return false;
4340 /* This is the only check explicitly called by spec: */
4341 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
4342 return false;
4343 /*
4344 * There may be more comparisons we could actually do, but the
4345 * spec doesn't require us to catch every case where the calls
4346 * don't match (that would require caching the call as well as
4347 * the reply), so we don't bother.
4348 */
4349 return true;
4350 }
4351
4352 /*
4353 * Note that the response is constructed here both for the case
4354 * of a new SEQUENCE request and for a replayed SEQUENCE request.
4355 * We do not cache SEQUENCE responses as SEQUENCE is idempotent.
4356 */
nfsd4_construct_sequence_response(struct nfsd4_session * session,struct nfsd4_sequence * seq)4357 static void nfsd4_construct_sequence_response(struct nfsd4_session *session,
4358 struct nfsd4_sequence *seq)
4359 {
4360 struct nfs4_client *clp = session->se_client;
4361
4362 seq->maxslots_response = max(session->se_target_maxslots,
4363 seq->maxslots);
4364 seq->target_maxslots = session->se_target_maxslots;
4365
4366 switch (clp->cl_cb_state) {
4367 case NFSD4_CB_DOWN:
4368 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
4369 break;
4370 case NFSD4_CB_FAULT:
4371 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
4372 break;
4373 default:
4374 seq->status_flags = 0;
4375 }
4376 if (!list_empty(&clp->cl_revoked))
4377 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
4378 if (atomic_read(&clp->cl_admin_revoked))
4379 seq->status_flags |= SEQ4_STATUS_ADMIN_STATE_REVOKED;
4380 }
4381
4382 __be32
nfsd4_sequence(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4383 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4384 union nfsd4_op_u *u)
4385 {
4386 struct nfsd4_sequence *seq = &u->sequence;
4387 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4388 struct xdr_stream *xdr = resp->xdr;
4389 struct nfsd4_session *session;
4390 struct nfs4_client *clp;
4391 struct nfsd4_slot *slot;
4392 struct nfsd4_conn *conn;
4393 __be32 status;
4394 int buflen;
4395 struct net *net = SVC_NET(rqstp);
4396 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4397
4398 if (resp->opcnt != 1)
4399 return nfserr_sequence_pos;
4400
4401 /*
4402 * Will be either used or freed by nfsd4_sequence_check_conn
4403 * below.
4404 */
4405 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
4406 if (!conn)
4407 return nfserr_jukebox;
4408
4409 spin_lock(&nn->client_lock);
4410 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
4411 if (!session)
4412 goto out_no_session;
4413 clp = session->se_client;
4414
4415 status = nfserr_too_many_ops;
4416 if (nfsd4_session_too_many_ops(rqstp, session))
4417 goto out_put_session;
4418
4419 status = nfserr_req_too_big;
4420 if (nfsd4_request_too_big(rqstp, session))
4421 goto out_put_session;
4422
4423 status = nfserr_badslot;
4424 if (seq->slotid >= session->se_fchannel.maxreqs)
4425 goto out_put_session;
4426
4427 slot = xa_load(&session->se_slots, seq->slotid);
4428 dprintk("%s: slotid %d\n", __func__, seq->slotid);
4429
4430 trace_nfsd_slot_seqid_sequence(clp, seq, slot);
4431
4432 nfsd4_construct_sequence_response(session, seq);
4433
4434 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_flags);
4435 if (status == nfserr_replay_cache) {
4436 status = nfserr_seq_misordered;
4437 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
4438 goto out_put_session;
4439 status = nfserr_seq_false_retry;
4440 if (!replay_matches_cache(rqstp, seq, slot))
4441 goto out_put_session;
4442 cstate->slot = slot;
4443 cstate->session = session;
4444 cstate->clp = clp;
4445 /* Return the cached reply status and set cstate->status
4446 * for nfsd4_proc_compound processing */
4447 status = nfsd4_replay_cache_entry(resp, seq);
4448 cstate->status = nfserr_replay_cache;
4449 goto out;
4450 }
4451 if (status)
4452 goto out_put_session;
4453
4454 status = nfsd4_sequence_check_conn(conn, session);
4455 conn = NULL;
4456 if (status)
4457 goto out_put_session;
4458
4459 if (session->se_target_maxslots < session->se_fchannel.maxreqs &&
4460 slot->sl_generation == session->se_slot_gen &&
4461 seq->maxslots <= session->se_target_maxslots)
4462 /* Client acknowledged our reduce maxreqs */
4463 free_session_slots(session, session->se_target_maxslots);
4464
4465 buflen = (seq->cachethis) ?
4466 session->se_fchannel.maxresp_cached :
4467 session->se_fchannel.maxresp_sz;
4468 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
4469 nfserr_rep_too_big;
4470 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
4471 goto out_put_session;
4472 svc_reserve_auth(rqstp, buflen);
4473
4474 status = nfs_ok;
4475 /* Success! accept new slot seqid */
4476 slot->sl_seqid = seq->seqid;
4477 slot->sl_flags &= ~NFSD4_SLOT_REUSED;
4478 slot->sl_flags |= NFSD4_SLOT_INUSE;
4479 slot->sl_generation = session->se_slot_gen;
4480 if (seq->cachethis)
4481 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
4482 else
4483 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
4484
4485 cstate->slot = slot;
4486 cstate->session = session;
4487 cstate->clp = clp;
4488
4489 /*
4490 * If the client ever uses the highest available slot,
4491 * gently try to allocate another 20%. This allows
4492 * fairly quick growth without grossly over-shooting what
4493 * the client might use.
4494 */
4495 if (seq->slotid == session->se_fchannel.maxreqs - 1 &&
4496 session->se_target_maxslots >= session->se_fchannel.maxreqs &&
4497 session->se_fchannel.maxreqs < NFSD_MAX_SLOTS_PER_SESSION) {
4498 int s = session->se_fchannel.maxreqs;
4499 int cnt = DIV_ROUND_UP(s, 5);
4500 void *prev_slot;
4501
4502 do {
4503 /*
4504 * GFP_NOWAIT both allows allocation under a
4505 * spinlock, and only succeeds if there is
4506 * plenty of memory.
4507 */
4508 slot = nfsd4_alloc_slot(&session->se_fchannel, s,
4509 GFP_NOWAIT);
4510 prev_slot = xa_load(&session->se_slots, s);
4511 if (xa_is_value(prev_slot) && slot) {
4512 slot->sl_seqid = xa_to_value(prev_slot);
4513 slot->sl_flags |= NFSD4_SLOT_REUSED;
4514 }
4515 if (slot &&
4516 !xa_is_err(xa_store(&session->se_slots, s, slot,
4517 GFP_NOWAIT))) {
4518 s += 1;
4519 session->se_fchannel.maxreqs = s;
4520 atomic_add(s - session->se_target_maxslots,
4521 &nfsd_total_target_slots);
4522 session->se_target_maxslots = s;
4523 } else {
4524 kfree(slot);
4525 slot = NULL;
4526 }
4527 } while (slot && --cnt > 0);
4528 }
4529
4530 out:
4531 trace_nfsd_seq4_status(rqstp, seq);
4532 out_no_session:
4533 if (conn)
4534 free_conn(conn);
4535 spin_unlock(&nn->client_lock);
4536 return status;
4537 out_put_session:
4538 nfsd4_put_session_locked(session);
4539 goto out_no_session;
4540 }
4541
4542 void
nfsd4_sequence_done(struct nfsd4_compoundres * resp)4543 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4544 {
4545 struct nfsd4_compound_state *cs = &resp->cstate;
4546
4547 if (nfsd4_has_session(cs)) {
4548 if (cs->status != nfserr_replay_cache) {
4549 nfsd4_store_cache_entry(resp);
4550 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4551 }
4552 /* Drop session reference that was taken in nfsd4_sequence() */
4553 nfsd4_put_session(cs->session);
4554 } else if (cs->clp)
4555 put_client_renew(cs->clp);
4556 }
4557
4558 __be32
nfsd4_destroy_clientid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4559 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4560 struct nfsd4_compound_state *cstate,
4561 union nfsd4_op_u *u)
4562 {
4563 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
4564 struct nfs4_client *conf, *unconf;
4565 struct nfs4_client *clp = NULL;
4566 __be32 status = 0;
4567 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4568
4569 spin_lock(&nn->client_lock);
4570 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
4571 conf = find_confirmed_client(&dc->clientid, true, nn);
4572 WARN_ON_ONCE(conf && unconf);
4573
4574 if (conf) {
4575 if (client_has_state(conf)) {
4576 status = nfserr_clientid_busy;
4577 goto out;
4578 }
4579 status = mark_client_expired_locked(conf);
4580 if (status)
4581 goto out;
4582 clp = conf;
4583 } else if (unconf)
4584 clp = unconf;
4585 else {
4586 status = nfserr_stale_clientid;
4587 goto out;
4588 }
4589 if (!nfsd4_mach_creds_match(clp, rqstp)) {
4590 clp = NULL;
4591 status = nfserr_wrong_cred;
4592 goto out;
4593 }
4594 trace_nfsd_clid_destroyed(&clp->cl_clientid);
4595 unhash_client_locked(clp);
4596 out:
4597 spin_unlock(&nn->client_lock);
4598 if (clp)
4599 expire_client(clp);
4600 return status;
4601 }
4602
4603 __be32
nfsd4_reclaim_complete(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4604 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4605 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
4606 {
4607 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
4608 struct nfs4_client *clp = cstate->clp;
4609 __be32 status = 0;
4610
4611 if (rc->rca_one_fs) {
4612 if (!cstate->current_fh.fh_dentry)
4613 return nfserr_nofilehandle;
4614 /*
4615 * We don't take advantage of the rca_one_fs case.
4616 * That's OK, it's optional, we can safely ignore it.
4617 */
4618 return nfs_ok;
4619 }
4620
4621 status = nfserr_complete_already;
4622 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4623 goto out;
4624
4625 status = nfserr_stale_clientid;
4626 if (is_client_expired(clp))
4627 /*
4628 * The following error isn't really legal.
4629 * But we only get here if the client just explicitly
4630 * destroyed the client. Surely it no longer cares what
4631 * error it gets back on an operation for the dead
4632 * client.
4633 */
4634 goto out;
4635
4636 status = nfs_ok;
4637 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4638 nfsd4_client_record_create(clp);
4639 inc_reclaim_complete(clp);
4640 out:
4641 return status;
4642 }
4643
4644 __be32
nfsd4_setclientid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4645 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4646 union nfsd4_op_u *u)
4647 {
4648 struct nfsd4_setclientid *setclid = &u->setclientid;
4649 struct xdr_netobj clname = setclid->se_name;
4650 nfs4_verifier clverifier = setclid->se_verf;
4651 struct nfs4_client *conf, *new;
4652 struct nfs4_client *unconf = NULL;
4653 __be32 status;
4654 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4655
4656 new = create_client(clname, rqstp, &clverifier);
4657 if (new == NULL)
4658 return nfserr_jukebox;
4659 spin_lock(&nn->client_lock);
4660 conf = find_confirmed_client_by_name(&clname, nn);
4661 if (conf && client_has_state(conf)) {
4662 status = nfserr_clid_inuse;
4663 if (clp_used_exchangeid(conf))
4664 goto out;
4665 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4666 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4667 goto out;
4668 }
4669 }
4670 unconf = find_unconfirmed_client_by_name(&clname, nn);
4671 if (unconf)
4672 unhash_client_locked(unconf);
4673 if (conf) {
4674 if (same_verf(&conf->cl_verifier, &clverifier)) {
4675 copy_clid(new, conf);
4676 gen_confirm(new, nn);
4677 } else
4678 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4679 &clverifier);
4680 } else
4681 trace_nfsd_clid_fresh(new);
4682 new->cl_minorversion = 0;
4683 gen_callback(new, setclid, rqstp);
4684 add_to_unconfirmed(new);
4685 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4686 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4687 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4688 new = NULL;
4689 status = nfs_ok;
4690 out:
4691 spin_unlock(&nn->client_lock);
4692 if (new)
4693 free_client(new);
4694 if (unconf) {
4695 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4696 expire_client(unconf);
4697 }
4698 return status;
4699 }
4700
4701 __be32
nfsd4_setclientid_confirm(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4702 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4703 struct nfsd4_compound_state *cstate,
4704 union nfsd4_op_u *u)
4705 {
4706 struct nfsd4_setclientid_confirm *setclientid_confirm =
4707 &u->setclientid_confirm;
4708 struct nfs4_client *conf, *unconf;
4709 struct nfs4_client *old = NULL;
4710 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4711 clientid_t * clid = &setclientid_confirm->sc_clientid;
4712 __be32 status;
4713 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4714
4715 if (STALE_CLIENTID(clid, nn))
4716 return nfserr_stale_clientid;
4717
4718 spin_lock(&nn->client_lock);
4719 conf = find_confirmed_client(clid, false, nn);
4720 unconf = find_unconfirmed_client(clid, false, nn);
4721 /*
4722 * We try hard to give out unique clientid's, so if we get an
4723 * attempt to confirm the same clientid with a different cred,
4724 * the client may be buggy; this should never happen.
4725 *
4726 * Nevertheless, RFC 7530 recommends INUSE for this case:
4727 */
4728 status = nfserr_clid_inuse;
4729 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4730 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4731 goto out;
4732 }
4733 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4734 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4735 goto out;
4736 }
4737 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4738 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4739 status = nfs_ok;
4740 } else
4741 status = nfserr_stale_clientid;
4742 goto out;
4743 }
4744 status = nfs_ok;
4745 if (conf) {
4746 if (get_client_locked(conf) == nfs_ok) {
4747 old = unconf;
4748 unhash_client_locked(old);
4749 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4750 } else {
4751 conf = NULL;
4752 }
4753 }
4754
4755 if (!conf) {
4756 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4757 if (old) {
4758 status = nfserr_clid_inuse;
4759 if (client_has_state(old)
4760 && !same_creds(&unconf->cl_cred,
4761 &old->cl_cred)) {
4762 old = NULL;
4763 goto out;
4764 }
4765 status = mark_client_expired_locked(old);
4766 if (status) {
4767 old = NULL;
4768 goto out;
4769 }
4770 trace_nfsd_clid_replaced(&old->cl_clientid);
4771 }
4772 status = get_client_locked(unconf);
4773 if (status != nfs_ok) {
4774 old = NULL;
4775 goto out;
4776 }
4777 move_to_confirmed(unconf);
4778 conf = unconf;
4779 }
4780 spin_unlock(&nn->client_lock);
4781 if (conf == unconf)
4782 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4783 nfsd4_probe_callback(conf);
4784 spin_lock(&nn->client_lock);
4785 put_client_renew_locked(conf);
4786 out:
4787 spin_unlock(&nn->client_lock);
4788 if (old)
4789 expire_client(old);
4790 return status;
4791 }
4792
nfsd4_alloc_file(void)4793 static struct nfs4_file *nfsd4_alloc_file(void)
4794 {
4795 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4796 }
4797
4798 /* OPEN Share state helper functions */
4799
nfsd4_file_init(const struct svc_fh * fh,struct nfs4_file * fp)4800 static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp)
4801 {
4802 refcount_set(&fp->fi_ref, 1);
4803 spin_lock_init(&fp->fi_lock);
4804 INIT_LIST_HEAD(&fp->fi_stateids);
4805 INIT_LIST_HEAD(&fp->fi_delegations);
4806 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4807 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
4808 fp->fi_deleg_file = NULL;
4809 fp->fi_rdeleg_file = NULL;
4810 fp->fi_had_conflict = false;
4811 fp->fi_share_deny = 0;
4812 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4813 memset(fp->fi_access, 0, sizeof(fp->fi_access));
4814 fp->fi_aliased = false;
4815 fp->fi_inode = d_inode(fh->fh_dentry);
4816 #ifdef CONFIG_NFSD_PNFS
4817 INIT_LIST_HEAD(&fp->fi_lo_states);
4818 atomic_set(&fp->fi_lo_recalls, 0);
4819 #endif
4820 }
4821
4822 void
nfsd4_free_slabs(void)4823 nfsd4_free_slabs(void)
4824 {
4825 kmem_cache_destroy(client_slab);
4826 kmem_cache_destroy(openowner_slab);
4827 kmem_cache_destroy(lockowner_slab);
4828 kmem_cache_destroy(file_slab);
4829 kmem_cache_destroy(stateid_slab);
4830 kmem_cache_destroy(deleg_slab);
4831 kmem_cache_destroy(odstate_slab);
4832 }
4833
4834 int
nfsd4_init_slabs(void)4835 nfsd4_init_slabs(void)
4836 {
4837 client_slab = KMEM_CACHE(nfs4_client, 0);
4838 if (client_slab == NULL)
4839 goto out;
4840 openowner_slab = KMEM_CACHE(nfs4_openowner, 0);
4841 if (openowner_slab == NULL)
4842 goto out_free_client_slab;
4843 lockowner_slab = KMEM_CACHE(nfs4_lockowner, 0);
4844 if (lockowner_slab == NULL)
4845 goto out_free_openowner_slab;
4846 file_slab = KMEM_CACHE(nfs4_file, 0);
4847 if (file_slab == NULL)
4848 goto out_free_lockowner_slab;
4849 stateid_slab = KMEM_CACHE(nfs4_ol_stateid, 0);
4850 if (stateid_slab == NULL)
4851 goto out_free_file_slab;
4852 deleg_slab = KMEM_CACHE(nfs4_delegation, 0);
4853 if (deleg_slab == NULL)
4854 goto out_free_stateid_slab;
4855 odstate_slab = KMEM_CACHE(nfs4_clnt_odstate, 0);
4856 if (odstate_slab == NULL)
4857 goto out_free_deleg_slab;
4858 return 0;
4859
4860 out_free_deleg_slab:
4861 kmem_cache_destroy(deleg_slab);
4862 out_free_stateid_slab:
4863 kmem_cache_destroy(stateid_slab);
4864 out_free_file_slab:
4865 kmem_cache_destroy(file_slab);
4866 out_free_lockowner_slab:
4867 kmem_cache_destroy(lockowner_slab);
4868 out_free_openowner_slab:
4869 kmem_cache_destroy(openowner_slab);
4870 out_free_client_slab:
4871 kmem_cache_destroy(client_slab);
4872 out:
4873 return -ENOMEM;
4874 }
4875
4876 static unsigned long
nfsd4_state_shrinker_count(struct shrinker * shrink,struct shrink_control * sc)4877 nfsd4_state_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
4878 {
4879 struct nfsd_net *nn = shrink->private_data;
4880 long count;
4881
4882 count = atomic_read(&nn->nfsd_courtesy_clients);
4883 if (!count)
4884 count = atomic_long_read(&num_delegations);
4885 if (count)
4886 queue_work(laundry_wq, &nn->nfsd_shrinker_work);
4887 return (unsigned long)count;
4888 }
4889
4890 static unsigned long
nfsd4_state_shrinker_scan(struct shrinker * shrink,struct shrink_control * sc)4891 nfsd4_state_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
4892 {
4893 return SHRINK_STOP;
4894 }
4895
4896 void
nfsd4_init_leases_net(struct nfsd_net * nn)4897 nfsd4_init_leases_net(struct nfsd_net *nn)
4898 {
4899 struct sysinfo si;
4900 u64 max_clients;
4901
4902 nn->nfsd4_lease = 90; /* default lease time */
4903 nn->nfsd4_grace = 90;
4904 nn->somebody_reclaimed = false;
4905 nn->track_reclaim_completes = false;
4906 nn->clverifier_counter = get_random_u32();
4907 nn->clientid_base = get_random_u32();
4908 nn->clientid_counter = nn->clientid_base + 1;
4909 nn->s2s_cp_cl_id = nn->clientid_counter++;
4910
4911 atomic_set(&nn->nfs4_client_count, 0);
4912 si_meminfo(&si);
4913 max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
4914 max_clients *= NFS4_CLIENTS_PER_GB;
4915 nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
4916
4917 atomic_set(&nn->nfsd_courtesy_clients, 0);
4918 }
4919
4920 enum rp_lock {
4921 RP_UNLOCKED,
4922 RP_LOCKED,
4923 RP_UNHASHED,
4924 };
4925
init_nfs4_replay(struct nfs4_replay * rp)4926 static void init_nfs4_replay(struct nfs4_replay *rp)
4927 {
4928 rp->rp_status = nfserr_serverfault;
4929 rp->rp_buflen = 0;
4930 rp->rp_buf = rp->rp_ibuf;
4931 rp->rp_locked = RP_UNLOCKED;
4932 }
4933
nfsd4_cstate_assign_replay(struct nfsd4_compound_state * cstate,struct nfs4_stateowner * so)4934 static int nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4935 struct nfs4_stateowner *so)
4936 {
4937 if (!nfsd4_has_session(cstate)) {
4938 wait_var_event(&so->so_replay.rp_locked,
4939 cmpxchg(&so->so_replay.rp_locked,
4940 RP_UNLOCKED, RP_LOCKED) != RP_LOCKED);
4941 if (so->so_replay.rp_locked == RP_UNHASHED)
4942 return -EAGAIN;
4943 cstate->replay_owner = nfs4_get_stateowner(so);
4944 }
4945 return 0;
4946 }
4947
nfsd4_cstate_clear_replay(struct nfsd4_compound_state * cstate)4948 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4949 {
4950 struct nfs4_stateowner *so = cstate->replay_owner;
4951
4952 if (so != NULL) {
4953 cstate->replay_owner = NULL;
4954 store_release_wake_up(&so->so_replay.rp_locked, RP_UNLOCKED);
4955 nfs4_put_stateowner(so);
4956 }
4957 }
4958
alloc_stateowner(struct kmem_cache * slab,struct xdr_netobj * owner,struct nfs4_client * clp)4959 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4960 {
4961 struct nfs4_stateowner *sop;
4962
4963 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4964 if (!sop)
4965 return NULL;
4966
4967 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4968 if (!sop->so_owner.data) {
4969 kmem_cache_free(slab, sop);
4970 return NULL;
4971 }
4972
4973 INIT_LIST_HEAD(&sop->so_stateids);
4974 sop->so_client = clp;
4975 init_nfs4_replay(&sop->so_replay);
4976 atomic_set(&sop->so_count, 1);
4977 return sop;
4978 }
4979
hash_openowner(struct nfs4_openowner * oo,struct nfs4_client * clp,unsigned int strhashval)4980 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4981 {
4982 lockdep_assert_held(&clp->cl_lock);
4983
4984 list_add(&oo->oo_owner.so_strhash,
4985 &clp->cl_ownerstr_hashtbl[strhashval]);
4986 list_add(&oo->oo_perclient, &clp->cl_openowners);
4987 }
4988
nfs4_unhash_openowner(struct nfs4_stateowner * so)4989 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4990 {
4991 unhash_openowner_locked(openowner(so));
4992 }
4993
nfs4_free_openowner(struct nfs4_stateowner * so)4994 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4995 {
4996 struct nfs4_openowner *oo = openowner(so);
4997
4998 kmem_cache_free(openowner_slab, oo);
4999 }
5000
5001 static const struct nfs4_stateowner_operations openowner_ops = {
5002 .so_unhash = nfs4_unhash_openowner,
5003 .so_free = nfs4_free_openowner,
5004 };
5005
5006 static struct nfs4_ol_stateid *
nfsd4_find_existing_open(struct nfs4_file * fp,struct nfsd4_open * open)5007 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
5008 {
5009 struct nfs4_ol_stateid *local, *ret = NULL;
5010 struct nfs4_openowner *oo = open->op_openowner;
5011
5012 lockdep_assert_held(&fp->fi_lock);
5013
5014 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
5015 /* ignore lock owners */
5016 if (local->st_stateowner->so_is_open_owner == 0)
5017 continue;
5018 if (local->st_stateowner != &oo->oo_owner)
5019 continue;
5020 if (local->st_stid.sc_type == SC_TYPE_OPEN &&
5021 !local->st_stid.sc_status) {
5022 ret = local;
5023 refcount_inc(&ret->st_stid.sc_count);
5024 break;
5025 }
5026 }
5027 return ret;
5028 }
5029
nfsd4_drop_revoked_stid(struct nfs4_stid * s)5030 static void nfsd4_drop_revoked_stid(struct nfs4_stid *s)
5031 __releases(&s->sc_client->cl_lock)
5032 {
5033 struct nfs4_client *cl = s->sc_client;
5034 LIST_HEAD(reaplist);
5035 struct nfs4_ol_stateid *stp;
5036 struct nfs4_delegation *dp;
5037 bool unhashed;
5038
5039 switch (s->sc_type) {
5040 case SC_TYPE_OPEN:
5041 stp = openlockstateid(s);
5042 if (unhash_open_stateid(stp, &reaplist))
5043 put_ol_stateid_locked(stp, &reaplist);
5044 spin_unlock(&cl->cl_lock);
5045 free_ol_stateid_reaplist(&reaplist);
5046 break;
5047 case SC_TYPE_LOCK:
5048 stp = openlockstateid(s);
5049 unhashed = unhash_lock_stateid(stp);
5050 spin_unlock(&cl->cl_lock);
5051 if (unhashed)
5052 nfs4_put_stid(s);
5053 break;
5054 case SC_TYPE_DELEG:
5055 dp = delegstateid(s);
5056 list_del_init(&dp->dl_recall_lru);
5057 spin_unlock(&cl->cl_lock);
5058 nfs4_put_stid(s);
5059 break;
5060 default:
5061 spin_unlock(&cl->cl_lock);
5062 }
5063 }
5064
nfsd40_drop_revoked_stid(struct nfs4_client * cl,stateid_t * stid)5065 static void nfsd40_drop_revoked_stid(struct nfs4_client *cl,
5066 stateid_t *stid)
5067 {
5068 /* NFSv4.0 has no way for the client to tell the server
5069 * that it can forget an admin-revoked stateid.
5070 * So we keep it around until the first time that the
5071 * client uses it, and drop it the first time
5072 * nfserr_admin_revoked is returned.
5073 * For v4.1 and later we wait until explicitly told
5074 * to free the stateid.
5075 */
5076 if (cl->cl_minorversion == 0) {
5077 struct nfs4_stid *st;
5078
5079 spin_lock(&cl->cl_lock);
5080 st = find_stateid_locked(cl, stid);
5081 if (st)
5082 nfsd4_drop_revoked_stid(st);
5083 else
5084 spin_unlock(&cl->cl_lock);
5085 }
5086 }
5087
5088 static __be32
nfsd4_verify_open_stid(struct nfs4_stid * s)5089 nfsd4_verify_open_stid(struct nfs4_stid *s)
5090 {
5091 __be32 ret = nfs_ok;
5092
5093 if (s->sc_status & SC_STATUS_ADMIN_REVOKED)
5094 ret = nfserr_admin_revoked;
5095 else if (s->sc_status & SC_STATUS_REVOKED)
5096 ret = nfserr_deleg_revoked;
5097 else if (s->sc_status & SC_STATUS_CLOSED)
5098 ret = nfserr_bad_stateid;
5099 return ret;
5100 }
5101
5102 /* Lock the stateid st_mutex, and deal with races with CLOSE */
5103 static __be32
nfsd4_lock_ol_stateid(struct nfs4_ol_stateid * stp)5104 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
5105 {
5106 __be32 ret;
5107
5108 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
5109 ret = nfsd4_verify_open_stid(&stp->st_stid);
5110 if (ret == nfserr_admin_revoked)
5111 nfsd40_drop_revoked_stid(stp->st_stid.sc_client,
5112 &stp->st_stid.sc_stateid);
5113
5114 if (ret != nfs_ok)
5115 mutex_unlock(&stp->st_mutex);
5116 return ret;
5117 }
5118
5119 static struct nfs4_ol_stateid *
nfsd4_find_and_lock_existing_open(struct nfs4_file * fp,struct nfsd4_open * open)5120 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
5121 {
5122 struct nfs4_ol_stateid *stp;
5123 for (;;) {
5124 spin_lock(&fp->fi_lock);
5125 stp = nfsd4_find_existing_open(fp, open);
5126 spin_unlock(&fp->fi_lock);
5127 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
5128 break;
5129 nfs4_put_stid(&stp->st_stid);
5130 }
5131 return stp;
5132 }
5133
5134 static struct nfs4_openowner *
find_or_alloc_open_stateowner(unsigned int strhashval,struct nfsd4_open * open,struct nfsd4_compound_state * cstate)5135 find_or_alloc_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
5136 struct nfsd4_compound_state *cstate)
5137 {
5138 struct nfs4_client *clp = cstate->clp;
5139 struct nfs4_openowner *oo, *new = NULL;
5140
5141 retry:
5142 spin_lock(&clp->cl_lock);
5143 oo = find_openstateowner_str(strhashval, open, clp);
5144 if (!oo && new) {
5145 hash_openowner(new, clp, strhashval);
5146 spin_unlock(&clp->cl_lock);
5147 return new;
5148 }
5149 spin_unlock(&clp->cl_lock);
5150
5151 if (oo && !(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5152 /* Replace unconfirmed owners without checking for replay. */
5153 release_openowner(oo);
5154 oo = NULL;
5155 }
5156 if (oo) {
5157 if (new)
5158 nfs4_free_stateowner(&new->oo_owner);
5159 return oo;
5160 }
5161
5162 new = alloc_stateowner(openowner_slab, &open->op_owner, clp);
5163 if (!new)
5164 return NULL;
5165 new->oo_owner.so_ops = &openowner_ops;
5166 new->oo_owner.so_is_open_owner = 1;
5167 new->oo_owner.so_seqid = open->op_seqid;
5168 new->oo_flags = 0;
5169 if (nfsd4_has_session(cstate))
5170 new->oo_flags |= NFS4_OO_CONFIRMED;
5171 new->oo_time = 0;
5172 new->oo_last_closed_stid = NULL;
5173 INIT_LIST_HEAD(&new->oo_close_lru);
5174 goto retry;
5175 }
5176
5177 static struct nfs4_ol_stateid *
init_open_stateid(struct nfs4_file * fp,struct nfsd4_open * open)5178 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
5179 {
5180
5181 struct nfs4_openowner *oo = open->op_openowner;
5182 struct nfs4_ol_stateid *retstp = NULL;
5183 struct nfs4_ol_stateid *stp;
5184
5185 stp = open->op_stp;
5186 /* We are moving these outside of the spinlocks to avoid the warnings */
5187 mutex_init(&stp->st_mutex);
5188 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
5189
5190 retry:
5191 spin_lock(&oo->oo_owner.so_client->cl_lock);
5192 spin_lock(&fp->fi_lock);
5193
5194 if (nfs4_openowner_unhashed(oo)) {
5195 mutex_unlock(&stp->st_mutex);
5196 stp = NULL;
5197 goto out_unlock;
5198 }
5199
5200 retstp = nfsd4_find_existing_open(fp, open);
5201 if (retstp)
5202 goto out_unlock;
5203
5204 open->op_stp = NULL;
5205 refcount_inc(&stp->st_stid.sc_count);
5206 stp->st_stid.sc_type = SC_TYPE_OPEN;
5207 INIT_LIST_HEAD(&stp->st_locks);
5208 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
5209 get_nfs4_file(fp);
5210 stp->st_stid.sc_file = fp;
5211 stp->st_access_bmap = 0;
5212 stp->st_deny_bmap = 0;
5213 stp->st_openstp = NULL;
5214 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
5215 list_add(&stp->st_perfile, &fp->fi_stateids);
5216
5217 out_unlock:
5218 spin_unlock(&fp->fi_lock);
5219 spin_unlock(&oo->oo_owner.so_client->cl_lock);
5220 if (retstp) {
5221 /* Handle races with CLOSE */
5222 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
5223 nfs4_put_stid(&retstp->st_stid);
5224 goto retry;
5225 }
5226 /* To keep mutex tracking happy */
5227 mutex_unlock(&stp->st_mutex);
5228 stp = retstp;
5229 }
5230 return stp;
5231 }
5232
5233 /*
5234 * In the 4.0 case we need to keep the owners around a little while to handle
5235 * CLOSE replay. We still do need to release any file access that is held by
5236 * them before returning however.
5237 */
5238 static void
move_to_close_lru(struct nfs4_ol_stateid * s,struct net * net)5239 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
5240 {
5241 struct nfs4_ol_stateid *last;
5242 struct nfs4_openowner *oo = openowner(s->st_stateowner);
5243 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
5244 nfsd_net_id);
5245
5246 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
5247
5248 /*
5249 * We know that we hold one reference via nfsd4_close, and another
5250 * "persistent" reference for the client. If the refcount is higher
5251 * than 2, then there are still calls in progress that are using this
5252 * stateid. We can't put the sc_file reference until they are finished.
5253 * Wait for the refcount to drop to 2. Since it has been unhashed,
5254 * there should be no danger of the refcount going back up again at
5255 * this point.
5256 * Some threads with a reference might be waiting for rp_locked,
5257 * so tell them to stop waiting.
5258 */
5259 store_release_wake_up(&oo->oo_owner.so_replay.rp_locked, RP_UNHASHED);
5260 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
5261
5262 release_all_access(s);
5263 if (s->st_stid.sc_file) {
5264 put_nfs4_file(s->st_stid.sc_file);
5265 s->st_stid.sc_file = NULL;
5266 }
5267
5268 spin_lock(&nn->client_lock);
5269 last = oo->oo_last_closed_stid;
5270 oo->oo_last_closed_stid = s;
5271 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
5272 oo->oo_time = ktime_get_boottime_seconds();
5273 spin_unlock(&nn->client_lock);
5274 if (last)
5275 nfs4_put_stid(&last->st_stid);
5276 }
5277
5278 static noinline_for_stack struct nfs4_file *
nfsd4_file_hash_lookup(const struct svc_fh * fhp)5279 nfsd4_file_hash_lookup(const struct svc_fh *fhp)
5280 {
5281 struct inode *inode = d_inode(fhp->fh_dentry);
5282 struct rhlist_head *tmp, *list;
5283 struct nfs4_file *fi;
5284
5285 rcu_read_lock();
5286 list = rhltable_lookup(&nfs4_file_rhltable, &inode,
5287 nfs4_file_rhash_params);
5288 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
5289 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
5290 if (refcount_inc_not_zero(&fi->fi_ref)) {
5291 rcu_read_unlock();
5292 return fi;
5293 }
5294 }
5295 }
5296 rcu_read_unlock();
5297 return NULL;
5298 }
5299
5300 /*
5301 * On hash insertion, identify entries with the same inode but
5302 * distinct filehandles. They will all be on the list returned
5303 * by rhltable_lookup().
5304 *
5305 * inode->i_lock prevents racing insertions from adding an entry
5306 * for the same inode/fhp pair twice.
5307 */
5308 static noinline_for_stack struct nfs4_file *
nfsd4_file_hash_insert(struct nfs4_file * new,const struct svc_fh * fhp)5309 nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp)
5310 {
5311 struct inode *inode = d_inode(fhp->fh_dentry);
5312 struct rhlist_head *tmp, *list;
5313 struct nfs4_file *ret = NULL;
5314 bool alias_found = false;
5315 struct nfs4_file *fi;
5316 int err;
5317
5318 rcu_read_lock();
5319 spin_lock(&inode->i_lock);
5320
5321 list = rhltable_lookup(&nfs4_file_rhltable, &inode,
5322 nfs4_file_rhash_params);
5323 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
5324 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
5325 if (refcount_inc_not_zero(&fi->fi_ref))
5326 ret = fi;
5327 } else
5328 fi->fi_aliased = alias_found = true;
5329 }
5330 if (ret)
5331 goto out_unlock;
5332
5333 nfsd4_file_init(fhp, new);
5334 err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist,
5335 nfs4_file_rhash_params);
5336 if (err)
5337 goto out_unlock;
5338
5339 new->fi_aliased = alias_found;
5340 ret = new;
5341
5342 out_unlock:
5343 spin_unlock(&inode->i_lock);
5344 rcu_read_unlock();
5345 return ret;
5346 }
5347
nfsd4_file_hash_remove(struct nfs4_file * fi)5348 static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi)
5349 {
5350 rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist,
5351 nfs4_file_rhash_params);
5352 }
5353
5354 /*
5355 * Called to check deny when READ with all zero stateid or
5356 * WRITE with all zero or all one stateid
5357 */
5358 static __be32
nfs4_share_conflict(struct svc_fh * current_fh,unsigned int deny_type)5359 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
5360 {
5361 struct nfs4_file *fp;
5362 __be32 ret = nfs_ok;
5363
5364 fp = nfsd4_file_hash_lookup(current_fh);
5365 if (!fp)
5366 return ret;
5367
5368 /* Check for conflicting share reservations */
5369 spin_lock(&fp->fi_lock);
5370 if (fp->fi_share_deny & deny_type)
5371 ret = nfserr_locked;
5372 spin_unlock(&fp->fi_lock);
5373 put_nfs4_file(fp);
5374 return ret;
5375 }
5376
nfsd4_deleg_present(const struct inode * inode)5377 static bool nfsd4_deleg_present(const struct inode *inode)
5378 {
5379 struct file_lock_context *ctx = locks_inode_context(inode);
5380
5381 return ctx && !list_empty_careful(&ctx->flc_lease);
5382 }
5383
5384 /**
5385 * nfsd_wait_for_delegreturn - wait for delegations to be returned
5386 * @rqstp: the RPC transaction being executed
5387 * @inode: in-core inode of the file being waited for
5388 *
5389 * The timeout prevents deadlock if all nfsd threads happen to be
5390 * tied up waiting for returning delegations.
5391 *
5392 * Return values:
5393 * %true: delegation was returned
5394 * %false: timed out waiting for delegreturn
5395 */
nfsd_wait_for_delegreturn(struct svc_rqst * rqstp,struct inode * inode)5396 bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode)
5397 {
5398 long __maybe_unused timeo;
5399
5400 timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode),
5401 NFSD_DELEGRETURN_TIMEOUT);
5402 trace_nfsd_delegret_wakeup(rqstp, inode, timeo);
5403 return timeo > 0;
5404 }
5405
nfsd4_cb_recall_prepare(struct nfsd4_callback * cb)5406 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
5407 {
5408 struct nfs4_delegation *dp = cb_to_delegation(cb);
5409 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
5410 nfsd_net_id);
5411
5412 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
5413
5414 /*
5415 * We can't do this in nfsd_break_deleg_cb because it is
5416 * already holding inode->i_lock.
5417 *
5418 * If the dl_time != 0, then we know that it has already been
5419 * queued for a lease break. Don't queue it again.
5420 */
5421 spin_lock(&state_lock);
5422 if (delegation_hashed(dp) && dp->dl_time == 0) {
5423 dp->dl_time = ktime_get_boottime_seconds();
5424 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
5425 }
5426 spin_unlock(&state_lock);
5427 }
5428
nfsd4_cb_recall_done(struct nfsd4_callback * cb,struct rpc_task * task)5429 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
5430 struct rpc_task *task)
5431 {
5432 struct nfs4_delegation *dp = cb_to_delegation(cb);
5433
5434 trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task);
5435
5436 if (dp->dl_stid.sc_status)
5437 /* CLOSED or REVOKED */
5438 return 1;
5439
5440 switch (task->tk_status) {
5441 case 0:
5442 return 1;
5443 case -NFS4ERR_DELAY:
5444 rpc_delay(task, 2 * HZ);
5445 return 0;
5446 case -EBADHANDLE:
5447 case -NFS4ERR_BAD_STATEID:
5448 /*
5449 * Race: client probably got cb_recall before open reply
5450 * granting delegation.
5451 */
5452 if (dp->dl_retries--) {
5453 rpc_delay(task, 2 * HZ);
5454 return 0;
5455 }
5456 fallthrough;
5457 default:
5458 return 1;
5459 }
5460 }
5461
nfsd4_cb_recall_release(struct nfsd4_callback * cb)5462 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
5463 {
5464 struct nfs4_delegation *dp = cb_to_delegation(cb);
5465
5466 nfs4_put_stid(&dp->dl_stid);
5467 }
5468
5469 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
5470 .prepare = nfsd4_cb_recall_prepare,
5471 .done = nfsd4_cb_recall_done,
5472 .release = nfsd4_cb_recall_release,
5473 .opcode = OP_CB_RECALL,
5474 };
5475
nfsd_break_one_deleg(struct nfs4_delegation * dp)5476 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
5477 {
5478 bool queued;
5479
5480 if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &dp->dl_recall.cb_flags))
5481 return;
5482
5483 /*
5484 * We're assuming the state code never drops its reference
5485 * without first removing the lease. Since we're in this lease
5486 * callback (and since the lease code is serialized by the
5487 * flc_lock) we know the server hasn't removed the lease yet, and
5488 * we know it's safe to take a reference.
5489 */
5490 refcount_inc(&dp->dl_stid.sc_count);
5491 queued = nfsd4_run_cb(&dp->dl_recall);
5492 WARN_ON_ONCE(!queued);
5493 if (!queued)
5494 refcount_dec(&dp->dl_stid.sc_count);
5495 }
5496
5497 /* Called from break_lease() with flc_lock held. */
5498 static bool
nfsd_break_deleg_cb(struct file_lease * fl)5499 nfsd_break_deleg_cb(struct file_lease *fl)
5500 {
5501 struct nfs4_delegation *dp = (struct nfs4_delegation *) fl->c.flc_owner;
5502 struct nfs4_file *fp = dp->dl_stid.sc_file;
5503 struct nfs4_client *clp = dp->dl_stid.sc_client;
5504 struct nfsd_net *nn;
5505
5506 trace_nfsd_cb_recall(&dp->dl_stid);
5507
5508 dp->dl_recalled = true;
5509 atomic_inc(&clp->cl_delegs_in_recall);
5510 if (try_to_expire_client(clp)) {
5511 nn = net_generic(clp->net, nfsd_net_id);
5512 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
5513 }
5514
5515 /*
5516 * We don't want the locks code to timeout the lease for us;
5517 * we'll remove it ourself if a delegation isn't returned
5518 * in time:
5519 */
5520 fl->fl_break_time = 0;
5521
5522 fp->fi_had_conflict = true;
5523 nfsd_break_one_deleg(dp);
5524 return false;
5525 }
5526
5527 /**
5528 * nfsd_breaker_owns_lease - Check if lease conflict was resolved
5529 * @fl: Lock state to check
5530 *
5531 * Return values:
5532 * %true: Lease conflict was resolved
5533 * %false: Lease conflict was not resolved.
5534 */
nfsd_breaker_owns_lease(struct file_lease * fl)5535 static bool nfsd_breaker_owns_lease(struct file_lease *fl)
5536 {
5537 struct nfs4_delegation *dl = fl->c.flc_owner;
5538 struct svc_rqst *rqst;
5539 struct nfs4_client *clp;
5540
5541 rqst = nfsd_current_rqst();
5542 if (!nfsd_v4client(rqst))
5543 return false;
5544 clp = *(rqst->rq_lease_breaker);
5545 return dl->dl_stid.sc_client == clp;
5546 }
5547
5548 static int
nfsd_change_deleg_cb(struct file_lease * onlist,int arg,struct list_head * dispose)5549 nfsd_change_deleg_cb(struct file_lease *onlist, int arg,
5550 struct list_head *dispose)
5551 {
5552 struct nfs4_delegation *dp = (struct nfs4_delegation *) onlist->c.flc_owner;
5553 struct nfs4_client *clp = dp->dl_stid.sc_client;
5554
5555 if (arg & F_UNLCK) {
5556 if (dp->dl_recalled)
5557 atomic_dec(&clp->cl_delegs_in_recall);
5558 return lease_modify(onlist, arg, dispose);
5559 } else
5560 return -EAGAIN;
5561 }
5562
5563 /**
5564 * nfsd4_deleg_lm_open_conflict - see if the given file points to an inode that has
5565 * an existing open that would conflict with the
5566 * desired lease.
5567 * @filp: file to check
5568 * @arg: type of lease that we're trying to acquire
5569 *
5570 * The kernel will call into this operation to determine whether there
5571 * are conflicting opens that may prevent the deleg from being granted.
5572 * For nfsd, that check is done at a higher level, so this trivially
5573 * returns 0.
5574 */
5575 static int
nfsd4_deleg_lm_open_conflict(struct file * filp,int arg)5576 nfsd4_deleg_lm_open_conflict(struct file *filp, int arg)
5577 {
5578 return 0;
5579 }
5580
5581 static const struct lease_manager_operations nfsd_lease_mng_ops = {
5582 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
5583 .lm_break = nfsd_break_deleg_cb,
5584 .lm_change = nfsd_change_deleg_cb,
5585 .lm_open_conflict = nfsd4_deleg_lm_open_conflict,
5586 };
5587
nfsd4_check_seqid(struct nfsd4_compound_state * cstate,struct nfs4_stateowner * so,u32 seqid)5588 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
5589 {
5590 if (nfsd4_has_session(cstate))
5591 return nfs_ok;
5592 if (seqid == so->so_seqid - 1)
5593 return nfserr_replay_me;
5594 if (seqid == so->so_seqid)
5595 return nfs_ok;
5596 return nfserr_bad_seqid;
5597 }
5598
lookup_clientid(clientid_t * clid,bool sessions,struct nfsd_net * nn)5599 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
5600 struct nfsd_net *nn)
5601 {
5602 struct nfs4_client *found;
5603
5604 spin_lock(&nn->client_lock);
5605 found = find_confirmed_client(clid, sessions, nn);
5606 if (found)
5607 atomic_inc(&found->cl_rpc_users);
5608 spin_unlock(&nn->client_lock);
5609 return found;
5610 }
5611
set_client(clientid_t * clid,struct nfsd4_compound_state * cstate,struct nfsd_net * nn)5612 static __be32 set_client(clientid_t *clid,
5613 struct nfsd4_compound_state *cstate,
5614 struct nfsd_net *nn)
5615 {
5616 if (cstate->clp) {
5617 if (!same_clid(&cstate->clp->cl_clientid, clid))
5618 return nfserr_stale_clientid;
5619 return nfs_ok;
5620 }
5621 if (STALE_CLIENTID(clid, nn))
5622 return nfserr_stale_clientid;
5623 /*
5624 * We're in the 4.0 case (otherwise the SEQUENCE op would have
5625 * set cstate->clp), so session = false:
5626 */
5627 cstate->clp = lookup_clientid(clid, false, nn);
5628 if (!cstate->clp)
5629 return nfserr_expired;
5630 return nfs_ok;
5631 }
5632
5633 __be32
nfsd4_process_open1(struct nfsd4_compound_state * cstate,struct nfsd4_open * open,struct nfsd_net * nn)5634 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
5635 struct nfsd4_open *open, struct nfsd_net *nn)
5636 {
5637 clientid_t *clientid = &open->op_clientid;
5638 struct nfs4_client *clp = NULL;
5639 unsigned int strhashval;
5640 struct nfs4_openowner *oo = NULL;
5641 __be32 status;
5642
5643 /*
5644 * In case we need it later, after we've already created the
5645 * file and don't want to risk a further failure:
5646 */
5647 open->op_file = nfsd4_alloc_file();
5648 if (open->op_file == NULL)
5649 return nfserr_jukebox;
5650
5651 status = set_client(clientid, cstate, nn);
5652 if (status)
5653 return status;
5654 clp = cstate->clp;
5655
5656 strhashval = ownerstr_hashval(&open->op_owner);
5657 retry:
5658 oo = find_or_alloc_open_stateowner(strhashval, open, cstate);
5659 open->op_openowner = oo;
5660 if (!oo)
5661 return nfserr_jukebox;
5662 if (nfsd4_cstate_assign_replay(cstate, &oo->oo_owner) == -EAGAIN) {
5663 nfs4_put_stateowner(&oo->oo_owner);
5664 goto retry;
5665 }
5666 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
5667 if (status)
5668 return status;
5669
5670 open->op_stp = nfs4_alloc_open_stateid(clp);
5671 if (!open->op_stp)
5672 return nfserr_jukebox;
5673
5674 if (nfsd4_has_session(cstate) &&
5675 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
5676 open->op_odstate = alloc_clnt_odstate(clp);
5677 if (!open->op_odstate)
5678 return nfserr_jukebox;
5679 }
5680
5681 return nfs_ok;
5682 }
5683
5684 static inline __be32
nfs4_check_delegmode(struct nfs4_delegation * dp,int flags)5685 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
5686 {
5687 if (!(flags & RD_STATE) && deleg_is_read(dp->dl_type))
5688 return nfserr_openmode;
5689 else
5690 return nfs_ok;
5691 }
5692
share_access_to_flags(u32 share_access)5693 static int share_access_to_flags(u32 share_access)
5694 {
5695 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
5696 }
5697
find_deleg_stateid(struct nfs4_client * cl,stateid_t * s)5698 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl,
5699 stateid_t *s)
5700 {
5701 struct nfs4_stid *ret;
5702
5703 ret = find_stateid_by_type(cl, s, SC_TYPE_DELEG, SC_STATUS_REVOKED);
5704 if (!ret)
5705 return NULL;
5706 return delegstateid(ret);
5707 }
5708
nfsd4_is_deleg_cur(struct nfsd4_open * open)5709 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
5710 {
5711 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
5712 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
5713 }
5714
5715 static __be32
nfs4_check_deleg(struct nfs4_client * cl,struct nfsd4_open * open,struct nfs4_delegation ** dp)5716 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
5717 struct nfs4_delegation **dp)
5718 {
5719 int flags;
5720 __be32 status = nfserr_bad_stateid;
5721 struct nfs4_delegation *deleg;
5722
5723 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5724 if (deleg == NULL)
5725 goto out;
5726 if (deleg->dl_stid.sc_status & SC_STATUS_ADMIN_REVOKED) {
5727 nfs4_put_stid(&deleg->dl_stid);
5728 status = nfserr_admin_revoked;
5729 goto out;
5730 }
5731 if (deleg->dl_stid.sc_status & SC_STATUS_REVOKED) {
5732 nfs4_put_stid(&deleg->dl_stid);
5733 nfsd40_drop_revoked_stid(cl, &open->op_delegate_stateid);
5734 status = nfserr_deleg_revoked;
5735 goto out;
5736 }
5737 flags = share_access_to_flags(open->op_share_access);
5738 status = nfs4_check_delegmode(deleg, flags);
5739 if (status) {
5740 nfs4_put_stid(&deleg->dl_stid);
5741 goto out;
5742 }
5743 *dp = deleg;
5744 out:
5745 if (!nfsd4_is_deleg_cur(open))
5746 return nfs_ok;
5747 if (status)
5748 return status;
5749 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5750 return nfs_ok;
5751 }
5752
nfs4_access_to_access(u32 nfs4_access)5753 static inline int nfs4_access_to_access(u32 nfs4_access)
5754 {
5755 int flags = 0;
5756
5757 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5758 flags |= NFSD_MAY_READ;
5759 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5760 flags |= NFSD_MAY_WRITE;
5761 return flags;
5762 }
5763
5764 static inline __be32
nfsd4_truncate(struct svc_rqst * rqstp,struct svc_fh * fh,struct nfsd4_open * open)5765 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5766 struct nfsd4_open *open)
5767 {
5768 struct iattr iattr = {
5769 .ia_valid = ATTR_SIZE,
5770 .ia_size = 0,
5771 };
5772 struct nfsd_attrs attrs = {
5773 .na_iattr = &iattr,
5774 };
5775 if (!open->op_truncate)
5776 return 0;
5777 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
5778 return nfserr_inval;
5779 return nfsd_setattr(rqstp, fh, &attrs, NULL);
5780 }
5781
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,bool new_stp)5782 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
5783 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5784 struct nfsd4_open *open, bool new_stp)
5785 {
5786 struct nfsd_file *nf = NULL;
5787 __be32 status;
5788 int oflag = nfs4_access_to_omode(open->op_share_access);
5789 int access = nfs4_access_to_access(open->op_share_access);
5790 unsigned char old_access_bmap, old_deny_bmap;
5791
5792 spin_lock(&fp->fi_lock);
5793
5794 /*
5795 * Are we trying to set a deny mode that would conflict with
5796 * current access?
5797 */
5798 status = nfs4_file_check_deny(fp, open->op_share_deny);
5799 if (status != nfs_ok) {
5800 if (status != nfserr_share_denied) {
5801 spin_unlock(&fp->fi_lock);
5802 goto out;
5803 }
5804 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5805 stp, open->op_share_deny, false))
5806 status = nfserr_jukebox;
5807 spin_unlock(&fp->fi_lock);
5808 goto out;
5809 }
5810
5811 /* set access to the file */
5812 status = nfs4_file_get_access(fp, open->op_share_access);
5813 if (status != nfs_ok) {
5814 if (status != nfserr_share_denied) {
5815 spin_unlock(&fp->fi_lock);
5816 goto out;
5817 }
5818 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5819 stp, open->op_share_access, true))
5820 status = nfserr_jukebox;
5821 spin_unlock(&fp->fi_lock);
5822 goto out;
5823 }
5824
5825 /* Set access bits in stateid */
5826 old_access_bmap = stp->st_access_bmap;
5827 set_access(open->op_share_access, stp);
5828
5829 /* Set new deny mask */
5830 old_deny_bmap = stp->st_deny_bmap;
5831 set_deny(open->op_share_deny, stp);
5832 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5833
5834 if (!fp->fi_fds[oflag]) {
5835 spin_unlock(&fp->fi_lock);
5836
5837 status = nfsd_file_acquire_opened(rqstp, cur_fh, access,
5838 open->op_filp, &nf);
5839 if (status != nfs_ok)
5840 goto out_put_access;
5841
5842 spin_lock(&fp->fi_lock);
5843 if (!fp->fi_fds[oflag]) {
5844 fp->fi_fds[oflag] = nf;
5845 nf = NULL;
5846 }
5847 }
5848 spin_unlock(&fp->fi_lock);
5849 if (nf)
5850 nfsd_file_put(nf);
5851
5852 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5853 access));
5854 if (status)
5855 goto out_put_access;
5856
5857 status = nfsd4_truncate(rqstp, cur_fh, open);
5858 if (status)
5859 goto out_put_access;
5860 out:
5861 return status;
5862 out_put_access:
5863 stp->st_access_bmap = old_access_bmap;
5864 nfs4_file_put_access(fp, open->op_share_access);
5865 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5866 goto out;
5867 }
5868
5869 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)5870 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
5871 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5872 struct nfsd4_open *open)
5873 {
5874 __be32 status;
5875 unsigned char old_deny_bmap = stp->st_deny_bmap;
5876
5877 if (!test_access(open->op_share_access, stp))
5878 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
5879
5880 /* test and set deny mode */
5881 spin_lock(&fp->fi_lock);
5882 status = nfs4_file_check_deny(fp, open->op_share_deny);
5883 switch (status) {
5884 case nfs_ok:
5885 set_deny(open->op_share_deny, stp);
5886 fp->fi_share_deny |=
5887 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5888 break;
5889 case nfserr_share_denied:
5890 if (nfs4_resolve_deny_conflicts_locked(fp, false,
5891 stp, open->op_share_deny, false))
5892 status = nfserr_jukebox;
5893 break;
5894 }
5895 spin_unlock(&fp->fi_lock);
5896
5897 if (status != nfs_ok)
5898 return status;
5899
5900 status = nfsd4_truncate(rqstp, cur_fh, open);
5901 if (status != nfs_ok)
5902 reset_union_bmap_deny(old_deny_bmap, stp);
5903 return status;
5904 }
5905
5906 /* Should we give out recallable state?: */
nfsd4_cb_channel_good(struct nfs4_client * clp)5907 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5908 {
5909 if (clp->cl_cb_state == NFSD4_CB_UP)
5910 return true;
5911 /*
5912 * In the sessions case, since we don't have to establish a
5913 * separate connection for callbacks, we assume it's OK
5914 * until we hear otherwise:
5915 */
5916 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5917 }
5918
nfs4_alloc_init_lease(struct nfs4_delegation * dp)5919 static struct file_lease *nfs4_alloc_init_lease(struct nfs4_delegation *dp)
5920 {
5921 struct file_lease *fl;
5922
5923 fl = locks_alloc_lease();
5924 if (!fl)
5925 return NULL;
5926 fl->fl_lmops = &nfsd_lease_mng_ops;
5927 fl->c.flc_flags = FL_DELEG;
5928 fl->c.flc_type = deleg_is_read(dp->dl_type) ? F_RDLCK : F_WRLCK;
5929 fl->c.flc_owner = (fl_owner_t)dp;
5930 fl->c.flc_pid = current->tgid;
5931 fl->c.flc_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
5932 return fl;
5933 }
5934
nfsd4_check_conflicting_opens(struct nfs4_client * clp,struct nfs4_file * fp)5935 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5936 struct nfs4_file *fp)
5937 {
5938 struct nfs4_ol_stateid *st;
5939 struct file *f = fp->fi_deleg_file->nf_file;
5940 struct inode *ino = file_inode(f);
5941 int writes;
5942
5943 writes = atomic_read(&ino->i_writecount);
5944 if (!writes)
5945 return 0;
5946 /*
5947 * There could be multiple filehandles (hence multiple
5948 * nfs4_files) referencing this file, but that's not too
5949 * common; let's just give up in that case rather than
5950 * trying to go look up all the clients using that other
5951 * nfs4_file as well:
5952 */
5953 if (fp->fi_aliased)
5954 return -EAGAIN;
5955 /*
5956 * If there's a close in progress, make sure that we see it
5957 * clear any fi_fds[] entries before we see it decrement
5958 * i_writecount:
5959 */
5960 smp_mb__after_atomic();
5961
5962 if (fp->fi_fds[O_WRONLY])
5963 writes--;
5964 if (fp->fi_fds[O_RDWR])
5965 writes--;
5966 if (writes > 0)
5967 return -EAGAIN; /* There may be non-NFSv4 writers */
5968 /*
5969 * It's possible there are non-NFSv4 write opens in progress,
5970 * but if they haven't incremented i_writecount yet then they
5971 * also haven't called break lease yet; so, they'll break this
5972 * lease soon enough. So, all that's left to check for is NFSv4
5973 * opens:
5974 */
5975 spin_lock(&fp->fi_lock);
5976 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5977 if (st->st_openstp == NULL /* it's an open */ &&
5978 access_permit_write(st) &&
5979 st->st_stid.sc_client != clp) {
5980 spin_unlock(&fp->fi_lock);
5981 return -EAGAIN;
5982 }
5983 }
5984 spin_unlock(&fp->fi_lock);
5985 /*
5986 * There's a small chance that we could be racing with another
5987 * NFSv4 open. However, any open that hasn't added itself to
5988 * the fi_stateids list also hasn't called break_lease yet; so,
5989 * they'll break this lease soon enough.
5990 */
5991 return 0;
5992 }
5993
5994 /*
5995 * It's possible that between opening the dentry and setting the delegation,
5996 * that it has been renamed or unlinked. Redo the lookup to verify that this
5997 * hasn't happened.
5998 */
5999 static int
nfsd4_verify_deleg_dentry(struct nfsd4_open * open,struct nfs4_file * fp,struct svc_fh * parent)6000 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
6001 struct svc_fh *parent)
6002 {
6003 struct svc_export *exp;
6004 struct dentry *child;
6005 __be32 err;
6006
6007 err = nfsd_lookup_dentry(open->op_rqstp, parent,
6008 open->op_fname, open->op_fnamelen,
6009 &exp, &child);
6010
6011 if (err)
6012 return -EAGAIN;
6013
6014 exp_put(exp);
6015 dput(child);
6016 if (child != file_dentry(fp->fi_deleg_file->nf_file))
6017 return -EAGAIN;
6018
6019 return 0;
6020 }
6021
6022 /*
6023 * We avoid breaking delegations held by a client due to its own activity, but
6024 * clearing setuid/setgid bits on a write is an implicit activity and the client
6025 * may not notice and continue using the old mode. Avoid giving out a delegation
6026 * on setuid/setgid files when the client is requesting an open for write.
6027 */
6028 static int
nfsd4_verify_setuid_write(struct nfsd4_open * open,struct nfsd_file * nf)6029 nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf)
6030 {
6031 struct inode *inode = file_inode(nf->nf_file);
6032
6033 if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) &&
6034 (inode->i_mode & (S_ISUID|S_ISGID)))
6035 return -EAGAIN;
6036 return 0;
6037 }
6038
6039 #ifdef CONFIG_NFSD_V4_DELEG_TIMESTAMPS
nfsd4_want_deleg_timestamps(const struct nfsd4_open * open)6040 static bool nfsd4_want_deleg_timestamps(const struct nfsd4_open *open)
6041 {
6042 return open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_DELEG_TIMESTAMPS;
6043 }
6044 #else /* CONFIG_NFSD_V4_DELEG_TIMESTAMPS */
nfsd4_want_deleg_timestamps(const struct nfsd4_open * open)6045 static bool nfsd4_want_deleg_timestamps(const struct nfsd4_open *open)
6046 {
6047 return false;
6048 }
6049 #endif /* CONFIG NFSD_V4_DELEG_TIMESTAMPS */
6050
6051 static struct nfs4_delegation *
nfs4_set_delegation(struct nfsd4_open * open,struct nfs4_ol_stateid * stp,struct svc_fh * parent)6052 nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
6053 struct svc_fh *parent)
6054 {
6055 bool deleg_ts = nfsd4_want_deleg_timestamps(open);
6056 struct nfs4_client *clp = stp->st_stid.sc_client;
6057 struct nfs4_file *fp = stp->st_stid.sc_file;
6058 struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
6059 struct nfs4_delegation *dp;
6060 struct nfsd_file *nf = NULL;
6061 struct file_lease *fl;
6062 int status = 0;
6063 u32 dl_type;
6064
6065 /*
6066 * The fi_had_conflict and nfs_get_existing_delegation checks
6067 * here are just optimizations; we'll need to recheck them at
6068 * the end:
6069 */
6070 if (fp->fi_had_conflict)
6071 return ERR_PTR(-EAGAIN);
6072
6073 /*
6074 * Try for a write delegation first. RFC8881 section 10.4 says:
6075 *
6076 * "An OPEN_DELEGATE_WRITE delegation allows the client to handle,
6077 * on its own, all opens."
6078 *
6079 * Furthermore, section 9.1.2 says:
6080 *
6081 * "In the case of READ, the server may perform the corresponding
6082 * check on the access mode, or it may choose to allow READ for
6083 * OPEN4_SHARE_ACCESS_WRITE, to accommodate clients whose WRITE
6084 * implementation may unavoidably do reads (e.g., due to buffer
6085 * cache constraints)."
6086 *
6087 * We choose to offer a write delegation for OPEN with the
6088 * OPEN4_SHARE_ACCESS_WRITE access mode to accommodate such clients.
6089 */
6090 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
6091 nf = find_writeable_file(fp);
6092 dl_type = deleg_ts ? OPEN_DELEGATE_WRITE_ATTRS_DELEG : OPEN_DELEGATE_WRITE;
6093 }
6094
6095 /*
6096 * If the file is being opened O_RDONLY or we couldn't get a O_RDWR
6097 * file for some reason, then try for a read delegation instead.
6098 */
6099 if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) {
6100 nf = find_readable_file(fp);
6101 dl_type = deleg_ts ? OPEN_DELEGATE_READ_ATTRS_DELEG : OPEN_DELEGATE_READ;
6102 }
6103
6104 if (!nf)
6105 return ERR_PTR(-EAGAIN);
6106
6107 /*
6108 * File delegations and associated locks cannot be recovered if the
6109 * export is from an NFS proxy server.
6110 */
6111 if (exportfs_cannot_lock(nf->nf_file->f_path.mnt->mnt_sb->s_export_op)) {
6112 nfsd_file_put(nf);
6113 return ERR_PTR(-EOPNOTSUPP);
6114 }
6115
6116 spin_lock(&state_lock);
6117 spin_lock(&fp->fi_lock);
6118 if (nfs4_delegation_exists(clp, fp))
6119 status = -EAGAIN;
6120 else if (nfsd4_verify_setuid_write(open, nf))
6121 status = -EAGAIN;
6122 else if (!fp->fi_deleg_file) {
6123 fp->fi_deleg_file = nf;
6124 /* increment early to prevent fi_deleg_file from being
6125 * cleared */
6126 fp->fi_delegees = 1;
6127 nf = NULL;
6128 } else
6129 fp->fi_delegees++;
6130 spin_unlock(&fp->fi_lock);
6131 spin_unlock(&state_lock);
6132 if (nf)
6133 nfsd_file_put(nf);
6134 if (status)
6135 return ERR_PTR(status);
6136
6137 status = -ENOMEM;
6138 dp = alloc_init_deleg(clp, fp, odstate, dl_type);
6139 if (!dp)
6140 goto out_delegees;
6141
6142 fl = nfs4_alloc_init_lease(dp);
6143 if (!fl)
6144 goto out_clnt_odstate;
6145
6146 status = kernel_setlease(fp->fi_deleg_file->nf_file,
6147 fl->c.flc_type, &fl, NULL);
6148 if (fl)
6149 locks_free_lease(fl);
6150 if (status)
6151 goto out_clnt_odstate;
6152
6153 if (parent) {
6154 status = nfsd4_verify_deleg_dentry(open, fp, parent);
6155 if (status)
6156 goto out_unlock;
6157 }
6158
6159 status = nfsd4_check_conflicting_opens(clp, fp);
6160 if (status)
6161 goto out_unlock;
6162
6163 /*
6164 * Now that the deleg is set, check again to ensure that nothing
6165 * raced in and changed the mode while we weren't looking.
6166 */
6167 status = nfsd4_verify_setuid_write(open, fp->fi_deleg_file);
6168 if (status)
6169 goto out_unlock;
6170
6171 status = -EAGAIN;
6172 if (fp->fi_had_conflict)
6173 goto out_unlock;
6174
6175 spin_lock(&state_lock);
6176 spin_lock(&clp->cl_lock);
6177 spin_lock(&fp->fi_lock);
6178 status = hash_delegation_locked(dp, fp);
6179 spin_unlock(&fp->fi_lock);
6180 spin_unlock(&clp->cl_lock);
6181 spin_unlock(&state_lock);
6182
6183 if (status)
6184 goto out_unlock;
6185
6186 return dp;
6187 out_unlock:
6188 kernel_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
6189 out_clnt_odstate:
6190 put_clnt_odstate(dp->dl_clnt_odstate);
6191 nfs4_put_stid(&dp->dl_stid);
6192 out_delegees:
6193 put_deleg_file(fp);
6194 return ERR_PTR(status);
6195 }
6196
nfsd4_open_deleg_none_ext(struct nfsd4_open * open,int status)6197 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
6198 {
6199 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
6200 if (status == -EAGAIN)
6201 open->op_why_no_deleg = WND4_CONTENTION;
6202 else {
6203 open->op_why_no_deleg = WND4_RESOURCE;
6204 switch (open->op_deleg_want) {
6205 case OPEN4_SHARE_ACCESS_WANT_READ_DELEG:
6206 case OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG:
6207 case OPEN4_SHARE_ACCESS_WANT_ANY_DELEG:
6208 break;
6209 case OPEN4_SHARE_ACCESS_WANT_CANCEL:
6210 open->op_why_no_deleg = WND4_CANCELLED;
6211 break;
6212 case OPEN4_SHARE_ACCESS_WANT_NO_DELEG:
6213 WARN_ON_ONCE(1);
6214 }
6215 }
6216 }
6217
6218 static bool
nfs4_delegation_stat(struct nfs4_delegation * dp,struct svc_fh * currentfh,struct kstat * stat)6219 nfs4_delegation_stat(struct nfs4_delegation *dp, struct svc_fh *currentfh,
6220 struct kstat *stat)
6221 {
6222 struct nfsd_file *nf = find_writeable_file(dp->dl_stid.sc_file);
6223 struct path path;
6224 int rc;
6225
6226 if (!nf)
6227 return false;
6228
6229 path.mnt = currentfh->fh_export->ex_path.mnt;
6230 path.dentry = file_dentry(nf->nf_file);
6231
6232 rc = vfs_getattr(&path, stat,
6233 STATX_MODE | STATX_SIZE | STATX_ATIME |
6234 STATX_MTIME | STATX_CTIME | STATX_CHANGE_COOKIE,
6235 AT_STATX_SYNC_AS_STAT);
6236
6237 nfsd_file_put(nf);
6238 return rc == 0;
6239 }
6240
6241 /*
6242 * Add NFS4_SHARE_ACCESS_READ to the write delegation granted on OPEN
6243 * with NFS4_SHARE_ACCESS_WRITE by allocating separate nfsd_file and
6244 * struct file to be used for read with delegation stateid.
6245 *
6246 */
6247 static bool
nfsd4_add_rdaccess_to_wrdeleg(struct svc_rqst * rqstp,struct nfsd4_open * open,struct svc_fh * fh,struct nfs4_ol_stateid * stp)6248 nfsd4_add_rdaccess_to_wrdeleg(struct svc_rqst *rqstp, struct nfsd4_open *open,
6249 struct svc_fh *fh, struct nfs4_ol_stateid *stp)
6250 {
6251 struct nfs4_file *fp;
6252 struct nfsd_file *nf = NULL;
6253
6254 if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) ==
6255 NFS4_SHARE_ACCESS_WRITE) {
6256 if (nfsd_file_acquire_opened(rqstp, fh, NFSD_MAY_READ, NULL, &nf))
6257 return (false);
6258 fp = stp->st_stid.sc_file;
6259 spin_lock(&fp->fi_lock);
6260 __nfs4_file_get_access(fp, NFS4_SHARE_ACCESS_READ);
6261 if (!fp->fi_fds[O_RDONLY]) {
6262 fp->fi_fds[O_RDONLY] = nf;
6263 nf = NULL;
6264 }
6265 fp->fi_rdeleg_file = nfsd_file_get(fp->fi_fds[O_RDONLY]);
6266 spin_unlock(&fp->fi_lock);
6267 if (nf)
6268 nfsd_file_put(nf);
6269 }
6270 return true;
6271 }
6272
6273 /*
6274 * The Linux NFS server does not offer write delegations to NFSv4.0
6275 * clients in order to avoid conflicts between write delegations and
6276 * GETATTRs requesting CHANGE or SIZE attributes.
6277 *
6278 * With NFSv4.1 and later minorversions, the SEQUENCE operation that
6279 * begins each COMPOUND contains a client ID. Delegation recall can
6280 * be avoided when the server recognizes the client sending a
6281 * GETATTR also holds write delegation it conflicts with.
6282 *
6283 * However, the NFSv4.0 protocol does not enable a server to
6284 * determine that a GETATTR originated from the client holding the
6285 * conflicting delegation versus coming from some other client. Per
6286 * RFC 7530 Section 16.7.5, the server must recall or send a
6287 * CB_GETATTR even when the GETATTR originates from the client that
6288 * holds the conflicting delegation.
6289 *
6290 * An NFSv4.0 client can trigger a pathological situation if it
6291 * always sends a DELEGRETURN preceded by a conflicting GETATTR in
6292 * the same COMPOUND. COMPOUND execution will always stop at the
6293 * GETATTR and the DELEGRETURN will never get executed. The server
6294 * eventually revokes the delegation, which can result in loss of
6295 * open or lock state.
6296 */
6297 static void
nfs4_open_delegation(struct svc_rqst * rqstp,struct nfsd4_open * open,struct nfs4_ol_stateid * stp,struct svc_fh * currentfh,struct svc_fh * fh)6298 nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open,
6299 struct nfs4_ol_stateid *stp, struct svc_fh *currentfh,
6300 struct svc_fh *fh)
6301 {
6302 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
6303 bool deleg_ts = nfsd4_want_deleg_timestamps(open);
6304 struct nfs4_client *clp = stp->st_stid.sc_client;
6305 struct svc_fh *parent = NULL;
6306 struct nfs4_delegation *dp;
6307 struct kstat stat;
6308 int status = 0;
6309 int cb_up;
6310
6311 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
6312 open->op_recall = false;
6313 switch (open->op_claim_type) {
6314 case NFS4_OPEN_CLAIM_PREVIOUS:
6315 if (!cb_up)
6316 open->op_recall = true;
6317 break;
6318 case NFS4_OPEN_CLAIM_NULL:
6319 parent = currentfh;
6320 fallthrough;
6321 case NFS4_OPEN_CLAIM_FH:
6322 /*
6323 * Let's not give out any delegations till everyone's
6324 * had the chance to reclaim theirs, *and* until
6325 * NLM locks have all been reclaimed:
6326 */
6327 if (locks_in_grace(clp->net))
6328 goto out_no_deleg;
6329 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
6330 goto out_no_deleg;
6331 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE &&
6332 !clp->cl_minorversion)
6333 goto out_no_deleg;
6334 break;
6335 default:
6336 goto out_no_deleg;
6337 }
6338 dp = nfs4_set_delegation(open, stp, parent);
6339 if (IS_ERR(dp))
6340 goto out_no_deleg;
6341
6342 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
6343
6344 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
6345 struct file *f = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
6346
6347 if (!nfsd4_add_rdaccess_to_wrdeleg(rqstp, open, fh, stp) ||
6348 !nfs4_delegation_stat(dp, currentfh, &stat)) {
6349 nfs4_put_stid(&dp->dl_stid);
6350 destroy_delegation(dp);
6351 goto out_no_deleg;
6352 }
6353 open->op_delegate_type = deleg_ts ? OPEN_DELEGATE_WRITE_ATTRS_DELEG :
6354 OPEN_DELEGATE_WRITE;
6355 dp->dl_cb_fattr.ncf_cur_fsize = stat.size;
6356 dp->dl_cb_fattr.ncf_initial_cinfo = nfsd4_change_attribute(&stat);
6357 dp->dl_atime = stat.atime;
6358 dp->dl_ctime = stat.ctime;
6359 dp->dl_mtime = stat.mtime;
6360 spin_lock(&f->f_lock);
6361 if (deleg_ts)
6362 f->f_mode |= FMODE_NOCMTIME;
6363 spin_unlock(&f->f_lock);
6364 trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid);
6365 } else {
6366 open->op_delegate_type = deleg_ts && nfs4_delegation_stat(dp, currentfh, &stat) ?
6367 OPEN_DELEGATE_READ_ATTRS_DELEG : OPEN_DELEGATE_READ;
6368 dp->dl_atime = stat.atime;
6369 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
6370 }
6371 nfs4_put_stid(&dp->dl_stid);
6372 return;
6373 out_no_deleg:
6374 open->op_delegate_type = OPEN_DELEGATE_NONE;
6375
6376 /* 4.1 client asking for a delegation? */
6377 if (open->op_deleg_want)
6378 nfsd4_open_deleg_none_ext(open, status);
6379 return;
6380 }
6381
nfsd4_deleg_xgrade_none_ext(struct nfsd4_open * open,struct nfs4_delegation * dp)6382 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
6383 struct nfs4_delegation *dp)
6384 {
6385 if (deleg_is_write(dp->dl_type)) {
6386 if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_READ_DELEG) {
6387 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
6388 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
6389 } else if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG) {
6390 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
6391 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
6392 }
6393 }
6394 /* Otherwise the client must be confused wanting a delegation
6395 * it already has, therefore we don't return
6396 * OPEN_DELEGATE_NONE_EXT and reason.
6397 */
6398 }
6399
6400 /* Are we returning only a delegation stateid? */
open_xor_delegation(struct nfsd4_open * open)6401 static bool open_xor_delegation(struct nfsd4_open *open)
6402 {
6403 if (!(open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_OPEN_XOR_DELEGATION))
6404 return false;
6405 /* Did we actually get a delegation? */
6406 if (!deleg_is_read(open->op_delegate_type) && !deleg_is_write(open->op_delegate_type))
6407 return false;
6408 return true;
6409 }
6410
6411 /**
6412 * nfsd4_process_open2 - finish open processing
6413 * @rqstp: the RPC transaction being executed
6414 * @current_fh: NFSv4 COMPOUND's current filehandle
6415 * @open: OPEN arguments
6416 *
6417 * If successful, (1) truncate the file if open->op_truncate was
6418 * set, (2) set open->op_stateid, (3) set open->op_delegation.
6419 *
6420 * Returns %nfs_ok on success; otherwise an nfs4stat value in
6421 * network byte order is returned.
6422 */
6423 __be32
nfsd4_process_open2(struct svc_rqst * rqstp,struct svc_fh * current_fh,struct nfsd4_open * open)6424 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
6425 {
6426 struct nfsd4_compoundres *resp = rqstp->rq_resp;
6427 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
6428 struct nfs4_file *fp = NULL;
6429 struct nfs4_ol_stateid *stp = NULL;
6430 struct nfs4_delegation *dp = NULL;
6431 __be32 status;
6432 bool new_stp = false;
6433
6434 /*
6435 * Lookup file; if found, lookup stateid and check open request,
6436 * and check for delegations in the process of being recalled.
6437 * If not found, create the nfs4_file struct
6438 */
6439 fp = nfsd4_file_hash_insert(open->op_file, current_fh);
6440 if (unlikely(!fp))
6441 return nfserr_jukebox;
6442 if (fp != open->op_file) {
6443 status = nfs4_check_deleg(cl, open, &dp);
6444 if (status)
6445 goto out;
6446 if (dp && nfsd4_is_deleg_cur(open) &&
6447 (dp->dl_stid.sc_file != fp)) {
6448 /*
6449 * RFC8881 section 8.2.4 mandates the server to return
6450 * NFS4ERR_BAD_STATEID if the selected table entry does
6451 * not match the current filehandle. However returning
6452 * NFS4ERR_BAD_STATEID in the OPEN can cause the client
6453 * to repeatedly retry the operation with the same
6454 * stateid, since the stateid itself is valid. To avoid
6455 * this situation NFSD returns NFS4ERR_INVAL instead.
6456 */
6457 status = nfserr_inval;
6458 goto out;
6459 }
6460 stp = nfsd4_find_and_lock_existing_open(fp, open);
6461 } else {
6462 open->op_file = NULL;
6463 status = nfserr_bad_stateid;
6464 if (nfsd4_is_deleg_cur(open))
6465 goto out;
6466 }
6467
6468 if (!stp) {
6469 stp = init_open_stateid(fp, open);
6470 if (!stp) {
6471 status = nfserr_jukebox;
6472 goto out;
6473 }
6474
6475 if (!open->op_stp)
6476 new_stp = true;
6477 }
6478
6479 /*
6480 * OPEN the file, or upgrade an existing OPEN.
6481 * If truncate fails, the OPEN fails.
6482 *
6483 * stp is already locked.
6484 */
6485 if (!new_stp) {
6486 /* Stateid was found, this is an OPEN upgrade */
6487 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
6488 if (status) {
6489 mutex_unlock(&stp->st_mutex);
6490 goto out;
6491 }
6492 } else {
6493 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
6494 if (status) {
6495 release_open_stateid(stp);
6496 mutex_unlock(&stp->st_mutex);
6497 goto out;
6498 }
6499
6500 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
6501 open->op_odstate);
6502 if (stp->st_clnt_odstate == open->op_odstate)
6503 open->op_odstate = NULL;
6504 }
6505
6506 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
6507 mutex_unlock(&stp->st_mutex);
6508
6509 if (nfsd4_has_session(&resp->cstate)) {
6510 if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_NO_DELEG) {
6511 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
6512 open->op_why_no_deleg = WND4_NOT_WANTED;
6513 goto nodeleg;
6514 }
6515 }
6516
6517 /*
6518 * Attempt to hand out a delegation. No error return, because the
6519 * OPEN succeeds even if we fail.
6520 */
6521 nfs4_open_delegation(rqstp, open, stp,
6522 &resp->cstate.current_fh, current_fh);
6523
6524 /*
6525 * If there is an existing open stateid, it must be updated and
6526 * returned. Only respect WANT_OPEN_XOR_DELEGATION when a new
6527 * open stateid would have to be created.
6528 */
6529 if (new_stp && open_xor_delegation(open)) {
6530 memcpy(&open->op_stateid, &zero_stateid, sizeof(open->op_stateid));
6531 open->op_rflags |= OPEN4_RESULT_NO_OPEN_STATEID;
6532 release_open_stateid(stp);
6533 }
6534 nodeleg:
6535 status = nfs_ok;
6536 trace_nfsd_open(&stp->st_stid.sc_stateid);
6537 out:
6538 /* 4.1 client trying to upgrade/downgrade delegation? */
6539 if (open->op_delegate_type == OPEN_DELEGATE_NONE && dp &&
6540 open->op_deleg_want)
6541 nfsd4_deleg_xgrade_none_ext(open, dp);
6542
6543 if (fp)
6544 put_nfs4_file(fp);
6545 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
6546 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
6547 /*
6548 * To finish the open response, we just need to set the rflags.
6549 */
6550 open->op_rflags |= NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
6551 if (nfsd4_has_session(&resp->cstate))
6552 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
6553 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
6554 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
6555
6556 if (dp)
6557 nfs4_put_stid(&dp->dl_stid);
6558 if (stp)
6559 nfs4_put_stid(&stp->st_stid);
6560
6561 return status;
6562 }
6563
nfsd4_cleanup_open_state(struct nfsd4_compound_state * cstate,struct nfsd4_open * open)6564 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
6565 struct nfsd4_open *open)
6566 {
6567 if (open->op_openowner)
6568 nfs4_put_stateowner(&open->op_openowner->oo_owner);
6569 if (open->op_file)
6570 kmem_cache_free(file_slab, open->op_file);
6571 if (open->op_stp)
6572 nfs4_put_stid(&open->op_stp->st_stid);
6573 if (open->op_odstate)
6574 kmem_cache_free(odstate_slab, open->op_odstate);
6575 }
6576
6577 __be32
nfsd4_renew(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)6578 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6579 union nfsd4_op_u *u)
6580 {
6581 clientid_t *clid = &u->renew;
6582 struct nfs4_client *clp;
6583 __be32 status;
6584 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6585
6586 trace_nfsd_clid_renew(clid);
6587 status = set_client(clid, cstate, nn);
6588 if (status)
6589 return status;
6590 clp = cstate->clp;
6591 if (!list_empty(&clp->cl_delegations)
6592 && clp->cl_cb_state != NFSD4_CB_UP)
6593 return nfserr_cb_path_down;
6594 return nfs_ok;
6595 }
6596
6597 static void
nfsd4_end_grace(struct nfsd_net * nn)6598 nfsd4_end_grace(struct nfsd_net *nn)
6599 {
6600 /* do nothing if grace period already ended */
6601 if (nn->grace_ended)
6602 return;
6603
6604 trace_nfsd_grace_complete(nn);
6605 nn->grace_ended = true;
6606 /*
6607 * If the server goes down again right now, an NFSv4
6608 * client will still be allowed to reclaim after it comes back up,
6609 * even if it hasn't yet had a chance to reclaim state this time.
6610 *
6611 */
6612 nfsd4_record_grace_done(nn);
6613 /*
6614 * At this point, NFSv4 clients can still reclaim. But if the
6615 * server crashes, any that have not yet reclaimed will be out
6616 * of luck on the next boot.
6617 *
6618 * (NFSv4.1+ clients are considered to have reclaimed once they
6619 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
6620 * have reclaimed after their first OPEN.)
6621 */
6622 locks_end_grace(&nn->nfsd4_manager);
6623 /*
6624 * At this point, and once lockd and/or any other containers
6625 * exit their grace period, further reclaims will fail and
6626 * regular locking can resume.
6627 */
6628 }
6629
6630 /**
6631 * nfsd4_force_end_grace - forcibly end the NFSv4 grace period
6632 * @nn: network namespace for the server instance to be updated
6633 *
6634 * Forces bypass of normal grace period completion, then schedules
6635 * the laundromat to end the grace period immediately. Does not wait
6636 * for the grace period to fully terminate before returning.
6637 *
6638 * Return values:
6639 * %true: Grace termination schedule
6640 * %false: No action was taken
6641 */
nfsd4_force_end_grace(struct nfsd_net * nn)6642 bool nfsd4_force_end_grace(struct nfsd_net *nn)
6643 {
6644 if (!nn->client_tracking_ops)
6645 return false;
6646 if (READ_ONCE(nn->grace_ended))
6647 return false;
6648 /* laundromat_work must be initialised now, though it might be disabled */
6649 WRITE_ONCE(nn->grace_end_forced, true);
6650 /* mod_delayed_work() doesn't queue work after
6651 * nfs4_state_shutdown_net() has called disable_delayed_work_sync()
6652 */
6653 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
6654 return true;
6655 }
6656
6657 /*
6658 * If we've waited a lease period but there are still clients trying to
6659 * reclaim, wait a little longer to give them a chance to finish.
6660 */
clients_still_reclaiming(struct nfsd_net * nn)6661 static bool clients_still_reclaiming(struct nfsd_net *nn)
6662 {
6663 time64_t double_grace_period_end = nn->boot_time +
6664 2 * nn->nfsd4_lease;
6665
6666 if (READ_ONCE(nn->grace_end_forced))
6667 return false;
6668 if (nn->track_reclaim_completes &&
6669 atomic_read(&nn->nr_reclaim_complete) ==
6670 nn->reclaim_str_hashtbl_size)
6671 return false;
6672 if (!nn->somebody_reclaimed)
6673 return false;
6674 nn->somebody_reclaimed = false;
6675 /*
6676 * If we've given them *two* lease times to reclaim, and they're
6677 * still not done, give up:
6678 */
6679 if (ktime_get_boottime_seconds() > double_grace_period_end)
6680 return false;
6681 return true;
6682 }
6683
6684 struct laundry_time {
6685 time64_t cutoff;
6686 time64_t new_timeo;
6687 };
6688
state_expired(struct laundry_time * lt,time64_t last_refresh)6689 static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
6690 {
6691 time64_t time_remaining;
6692
6693 if (last_refresh < lt->cutoff)
6694 return true;
6695 time_remaining = last_refresh - lt->cutoff;
6696 lt->new_timeo = min(lt->new_timeo, time_remaining);
6697 return false;
6698 }
6699
6700 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
nfsd4_ssc_init_umount_work(struct nfsd_net * nn)6701 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
6702 {
6703 spin_lock_init(&nn->nfsd_ssc_lock);
6704 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
6705 init_waitqueue_head(&nn->nfsd_ssc_waitq);
6706 }
6707
6708 /*
6709 * This is called when nfsd is being shutdown, after all inter_ssc
6710 * cleanup were done, to destroy the ssc delayed unmount list.
6711 */
nfsd4_ssc_shutdown_umount(struct nfsd_net * nn)6712 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
6713 {
6714 struct nfsd4_ssc_umount_item *ni = NULL;
6715 struct nfsd4_ssc_umount_item *tmp;
6716
6717 spin_lock(&nn->nfsd_ssc_lock);
6718 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
6719 list_del(&ni->nsui_list);
6720 spin_unlock(&nn->nfsd_ssc_lock);
6721 mntput(ni->nsui_vfsmount);
6722 kfree(ni);
6723 spin_lock(&nn->nfsd_ssc_lock);
6724 }
6725 spin_unlock(&nn->nfsd_ssc_lock);
6726 }
6727
nfsd4_ssc_expire_umount(struct nfsd_net * nn)6728 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
6729 {
6730 bool do_wakeup = false;
6731 struct nfsd4_ssc_umount_item *ni = NULL;
6732 struct nfsd4_ssc_umount_item *tmp;
6733
6734 spin_lock(&nn->nfsd_ssc_lock);
6735 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
6736 if (time_after(jiffies, ni->nsui_expire)) {
6737 if (refcount_read(&ni->nsui_refcnt) > 1)
6738 continue;
6739
6740 /* mark being unmount */
6741 ni->nsui_busy = true;
6742 spin_unlock(&nn->nfsd_ssc_lock);
6743 mntput(ni->nsui_vfsmount);
6744 spin_lock(&nn->nfsd_ssc_lock);
6745
6746 /* waiters need to start from begin of list */
6747 list_del(&ni->nsui_list);
6748 kfree(ni);
6749
6750 /* wakeup ssc_connect waiters */
6751 do_wakeup = true;
6752 continue;
6753 }
6754 break;
6755 }
6756 if (do_wakeup)
6757 wake_up_all(&nn->nfsd_ssc_waitq);
6758 spin_unlock(&nn->nfsd_ssc_lock);
6759 }
6760 #endif
6761
6762 /* Check if any lock belonging to this lockowner has any blockers */
6763 static bool
nfs4_lockowner_has_blockers(struct nfs4_lockowner * lo)6764 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
6765 {
6766 struct file_lock_context *ctx;
6767 struct nfs4_ol_stateid *stp;
6768 struct nfs4_file *nf;
6769
6770 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
6771 nf = stp->st_stid.sc_file;
6772 ctx = locks_inode_context(nf->fi_inode);
6773 if (!ctx)
6774 continue;
6775 if (locks_owner_has_blockers(ctx, lo))
6776 return true;
6777 }
6778 return false;
6779 }
6780
6781 static bool
nfs4_anylock_blockers(struct nfs4_client * clp)6782 nfs4_anylock_blockers(struct nfs4_client *clp)
6783 {
6784 int i;
6785 struct nfs4_stateowner *so;
6786 struct nfs4_lockowner *lo;
6787
6788 if (atomic_read(&clp->cl_delegs_in_recall))
6789 return true;
6790 spin_lock(&clp->cl_lock);
6791 for (i = 0; i < OWNER_HASH_SIZE; i++) {
6792 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
6793 so_strhash) {
6794 if (so->so_is_open_owner)
6795 continue;
6796 lo = lockowner(so);
6797 if (nfs4_lockowner_has_blockers(lo)) {
6798 spin_unlock(&clp->cl_lock);
6799 return true;
6800 }
6801 }
6802 }
6803 spin_unlock(&clp->cl_lock);
6804 return false;
6805 }
6806
6807 static void
nfs4_get_client_reaplist(struct nfsd_net * nn,struct list_head * reaplist,struct laundry_time * lt)6808 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
6809 struct laundry_time *lt)
6810 {
6811 unsigned int maxreap, reapcnt = 0;
6812 struct list_head *pos, *next;
6813 struct nfs4_client *clp;
6814
6815 maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
6816 NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
6817 INIT_LIST_HEAD(reaplist);
6818 spin_lock(&nn->client_lock);
6819 list_for_each_safe(pos, next, &nn->client_lru) {
6820 clp = list_entry(pos, struct nfs4_client, cl_lru);
6821 if (clp->cl_state == NFSD4_EXPIRABLE)
6822 goto exp_client;
6823 if (!state_expired(lt, clp->cl_time))
6824 break;
6825 if (!atomic_read(&clp->cl_rpc_users)) {
6826 if (clp->cl_state == NFSD4_ACTIVE)
6827 atomic_inc(&nn->nfsd_courtesy_clients);
6828 clp->cl_state = NFSD4_COURTESY;
6829 }
6830 if (!client_has_state(clp))
6831 goto exp_client;
6832 if (!nfs4_anylock_blockers(clp))
6833 if (reapcnt >= maxreap)
6834 continue;
6835 exp_client:
6836 if (!mark_client_expired_locked(clp)) {
6837 list_add(&clp->cl_lru, reaplist);
6838 reapcnt++;
6839 }
6840 }
6841 spin_unlock(&nn->client_lock);
6842 }
6843
6844 static void
nfs4_get_courtesy_client_reaplist(struct nfsd_net * nn,struct list_head * reaplist)6845 nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn,
6846 struct list_head *reaplist)
6847 {
6848 unsigned int maxreap = 0, reapcnt = 0;
6849 struct list_head *pos, *next;
6850 struct nfs4_client *clp;
6851
6852 maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN;
6853 INIT_LIST_HEAD(reaplist);
6854
6855 spin_lock(&nn->client_lock);
6856 list_for_each_safe(pos, next, &nn->client_lru) {
6857 clp = list_entry(pos, struct nfs4_client, cl_lru);
6858 if (clp->cl_state == NFSD4_ACTIVE)
6859 break;
6860 if (reapcnt >= maxreap)
6861 break;
6862 if (!mark_client_expired_locked(clp)) {
6863 list_add(&clp->cl_lru, reaplist);
6864 reapcnt++;
6865 }
6866 }
6867 spin_unlock(&nn->client_lock);
6868 }
6869
6870 static void
nfs4_process_client_reaplist(struct list_head * reaplist)6871 nfs4_process_client_reaplist(struct list_head *reaplist)
6872 {
6873 struct list_head *pos, *next;
6874 struct nfs4_client *clp;
6875
6876 list_for_each_safe(pos, next, reaplist) {
6877 clp = list_entry(pos, struct nfs4_client, cl_lru);
6878 trace_nfsd_clid_purged(&clp->cl_clientid);
6879 list_del_init(&clp->cl_lru);
6880 expire_client(clp);
6881 }
6882 }
6883
nfs40_clean_admin_revoked(struct nfsd_net * nn,struct laundry_time * lt)6884 static void nfs40_clean_admin_revoked(struct nfsd_net *nn,
6885 struct laundry_time *lt)
6886 {
6887 struct nfs4_client *clp;
6888
6889 spin_lock(&nn->client_lock);
6890 if (nn->nfs40_last_revoke == 0 ||
6891 nn->nfs40_last_revoke > lt->cutoff) {
6892 spin_unlock(&nn->client_lock);
6893 return;
6894 }
6895 nn->nfs40_last_revoke = 0;
6896
6897 retry:
6898 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6899 unsigned long id, tmp;
6900 struct nfs4_stid *stid;
6901
6902 if (atomic_read(&clp->cl_admin_revoked) == 0)
6903 continue;
6904
6905 spin_lock(&clp->cl_lock);
6906 idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id)
6907 if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) {
6908 refcount_inc(&stid->sc_count);
6909 spin_unlock(&nn->client_lock);
6910 /* this function drops ->cl_lock */
6911 nfsd4_drop_revoked_stid(stid);
6912 nfs4_put_stid(stid);
6913 spin_lock(&nn->client_lock);
6914 goto retry;
6915 }
6916 spin_unlock(&clp->cl_lock);
6917 }
6918 spin_unlock(&nn->client_lock);
6919 }
6920
6921 static time64_t
nfs4_laundromat(struct nfsd_net * nn)6922 nfs4_laundromat(struct nfsd_net *nn)
6923 {
6924 struct nfs4_openowner *oo;
6925 struct nfs4_delegation *dp;
6926 struct nfs4_ol_stateid *stp;
6927 struct nfsd4_blocked_lock *nbl;
6928 struct list_head *pos, *next, reaplist;
6929 struct laundry_time lt = {
6930 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
6931 .new_timeo = nn->nfsd4_lease
6932 };
6933 struct nfs4_cpntf_state *cps;
6934 copy_stateid_t *cps_t;
6935 int i;
6936
6937 if (clients_still_reclaiming(nn)) {
6938 lt.new_timeo = 0;
6939 goto out;
6940 }
6941 nfsd4_end_grace(nn);
6942
6943 spin_lock(&nn->s2s_cp_lock);
6944 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
6945 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
6946 if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID &&
6947 state_expired(<, cps->cpntf_time))
6948 _free_cpntf_state_locked(nn, cps);
6949 }
6950 spin_unlock(&nn->s2s_cp_lock);
6951 nfsd4_async_copy_reaper(nn);
6952 nfs4_get_client_reaplist(nn, &reaplist, <);
6953 nfs4_process_client_reaplist(&reaplist);
6954
6955 nfs40_clean_admin_revoked(nn, <);
6956
6957 spin_lock(&state_lock);
6958 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6959 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6960 if (!state_expired(<, dp->dl_time))
6961 break;
6962 refcount_inc(&dp->dl_stid.sc_count);
6963 unhash_delegation_locked(dp, SC_STATUS_REVOKED);
6964 list_add(&dp->dl_recall_lru, &reaplist);
6965 }
6966 spin_unlock(&state_lock);
6967 while (!list_empty(&reaplist)) {
6968 dp = list_first_entry(&reaplist, struct nfs4_delegation,
6969 dl_recall_lru);
6970 list_del_init(&dp->dl_recall_lru);
6971 revoke_delegation(dp);
6972 }
6973
6974 spin_lock(&nn->client_lock);
6975 while (!list_empty(&nn->close_lru)) {
6976 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
6977 oo_close_lru);
6978 if (!state_expired(<, oo->oo_time))
6979 break;
6980 list_del_init(&oo->oo_close_lru);
6981 stp = oo->oo_last_closed_stid;
6982 oo->oo_last_closed_stid = NULL;
6983 spin_unlock(&nn->client_lock);
6984 nfs4_put_stid(&stp->st_stid);
6985 spin_lock(&nn->client_lock);
6986 }
6987 spin_unlock(&nn->client_lock);
6988
6989 /*
6990 * It's possible for a client to try and acquire an already held lock
6991 * that is being held for a long time, and then lose interest in it.
6992 * So, we clean out any un-revisited request after a lease period
6993 * under the assumption that the client is no longer interested.
6994 *
6995 * RFC5661, sec. 9.6 states that the client must not rely on getting
6996 * notifications and must continue to poll for locks, even when the
6997 * server supports them. Thus this shouldn't lead to clients blocking
6998 * indefinitely once the lock does become free.
6999 */
7000 BUG_ON(!list_empty(&reaplist));
7001 spin_lock(&nn->blocked_locks_lock);
7002 while (!list_empty(&nn->blocked_locks_lru)) {
7003 nbl = list_first_entry(&nn->blocked_locks_lru,
7004 struct nfsd4_blocked_lock, nbl_lru);
7005 if (!state_expired(<, nbl->nbl_time))
7006 break;
7007 list_move(&nbl->nbl_lru, &reaplist);
7008 list_del_init(&nbl->nbl_list);
7009 }
7010 spin_unlock(&nn->blocked_locks_lock);
7011
7012 while (!list_empty(&reaplist)) {
7013 nbl = list_first_entry(&reaplist,
7014 struct nfsd4_blocked_lock, nbl_lru);
7015 list_del_init(&nbl->nbl_lru);
7016 free_blocked_lock(nbl);
7017 }
7018 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
7019 /* service the server-to-server copy delayed unmount list */
7020 nfsd4_ssc_expire_umount(nn);
7021 #endif
7022 if (atomic_long_read(&num_delegations) >= max_delegations)
7023 deleg_reaper(nn);
7024 out:
7025 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
7026 }
7027
7028 static void laundromat_main(struct work_struct *);
7029
7030 static void
laundromat_main(struct work_struct * laundry)7031 laundromat_main(struct work_struct *laundry)
7032 {
7033 time64_t t;
7034 struct delayed_work *dwork = to_delayed_work(laundry);
7035 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
7036 laundromat_work);
7037
7038 t = nfs4_laundromat(nn);
7039 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
7040 }
7041
7042 static void
courtesy_client_reaper(struct nfsd_net * nn)7043 courtesy_client_reaper(struct nfsd_net *nn)
7044 {
7045 struct list_head reaplist;
7046
7047 nfs4_get_courtesy_client_reaplist(nn, &reaplist);
7048 nfs4_process_client_reaplist(&reaplist);
7049 }
7050
7051 static void
deleg_reaper(struct nfsd_net * nn)7052 deleg_reaper(struct nfsd_net *nn)
7053 {
7054 struct list_head *pos, *next;
7055 struct nfs4_client *clp;
7056
7057 spin_lock(&nn->client_lock);
7058 list_for_each_safe(pos, next, &nn->client_lru) {
7059 clp = list_entry(pos, struct nfs4_client, cl_lru);
7060
7061 if (clp->cl_state != NFSD4_ACTIVE)
7062 continue;
7063 if (list_empty(&clp->cl_delegations))
7064 continue;
7065 if (atomic_read(&clp->cl_delegs_in_recall))
7066 continue;
7067 if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &clp->cl_ra->ra_cb.cb_flags))
7068 continue;
7069 if (ktime_get_boottime_seconds() - clp->cl_ra_time < 5)
7070 continue;
7071 if (clp->cl_cb_state != NFSD4_CB_UP)
7072 continue;
7073
7074 /* release in nfsd4_cb_recall_any_release */
7075 kref_get(&clp->cl_nfsdfs.cl_ref);
7076 clp->cl_ra_time = ktime_get_boottime_seconds();
7077 clp->cl_ra->ra_keep = 0;
7078 clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG) |
7079 BIT(RCA4_TYPE_MASK_WDATA_DLG);
7080 trace_nfsd_cb_recall_any(clp->cl_ra);
7081 nfsd4_run_cb(&clp->cl_ra->ra_cb);
7082 }
7083 spin_unlock(&nn->client_lock);
7084 }
7085
7086 static void
nfsd4_state_shrinker_worker(struct work_struct * work)7087 nfsd4_state_shrinker_worker(struct work_struct *work)
7088 {
7089 struct nfsd_net *nn = container_of(work, struct nfsd_net,
7090 nfsd_shrinker_work);
7091
7092 courtesy_client_reaper(nn);
7093 deleg_reaper(nn);
7094 }
7095
nfs4_check_fh(struct svc_fh * fhp,struct nfs4_stid * stp)7096 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
7097 {
7098 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
7099 return nfserr_bad_stateid;
7100 return nfs_ok;
7101 }
7102
7103 static
nfs4_check_openmode(struct nfs4_ol_stateid * stp,int flags)7104 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
7105 {
7106 __be32 status = nfserr_openmode;
7107
7108 /* For lock stateid's, we test the parent open, not the lock: */
7109 if (stp->st_openstp)
7110 stp = stp->st_openstp;
7111 if ((flags & WR_STATE) && !access_permit_write(stp))
7112 goto out;
7113 if ((flags & RD_STATE) && !access_permit_read(stp))
7114 goto out;
7115 status = nfs_ok;
7116 out:
7117 return status;
7118 }
7119
7120 static inline __be32
check_special_stateids(struct net * net,svc_fh * current_fh,stateid_t * stateid,int flags)7121 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
7122 {
7123 if (ONE_STATEID(stateid) && (flags & RD_STATE))
7124 return nfs_ok;
7125 else if (opens_in_grace(net)) {
7126 /* Answer in remaining cases depends on existence of
7127 * conflicting state; so we must wait out the grace period. */
7128 return nfserr_grace;
7129 } else if (flags & WR_STATE)
7130 return nfs4_share_conflict(current_fh,
7131 NFS4_SHARE_DENY_WRITE);
7132 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
7133 return nfs4_share_conflict(current_fh,
7134 NFS4_SHARE_DENY_READ);
7135 }
7136
check_stateid_generation(stateid_t * in,stateid_t * ref,bool has_session)7137 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
7138 {
7139 /*
7140 * When sessions are used the stateid generation number is ignored
7141 * when it is zero.
7142 */
7143 if (has_session && in->si_generation == 0)
7144 return nfs_ok;
7145
7146 if (in->si_generation == ref->si_generation)
7147 return nfs_ok;
7148
7149 /* If the client sends us a stateid from the future, it's buggy: */
7150 if (nfsd4_stateid_generation_after(in, ref))
7151 return nfserr_bad_stateid;
7152 /*
7153 * However, we could see a stateid from the past, even from a
7154 * non-buggy client. For example, if the client sends a lock
7155 * while some IO is outstanding, the lock may bump si_generation
7156 * while the IO is still in flight. The client could avoid that
7157 * situation by waiting for responses on all the IO requests,
7158 * but better performance may result in retrying IO that
7159 * receives an old_stateid error if requests are rarely
7160 * reordered in flight:
7161 */
7162 return nfserr_old_stateid;
7163 }
7164
nfsd4_stid_check_stateid_generation(stateid_t * in,struct nfs4_stid * s,bool has_session)7165 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
7166 {
7167 __be32 ret;
7168
7169 spin_lock(&s->sc_lock);
7170 ret = nfsd4_verify_open_stid(s);
7171 if (ret == nfs_ok)
7172 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
7173 spin_unlock(&s->sc_lock);
7174 if (ret == nfserr_admin_revoked)
7175 nfsd40_drop_revoked_stid(s->sc_client,
7176 &s->sc_stateid);
7177 return ret;
7178 }
7179
nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid * ols)7180 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
7181 {
7182 if (ols->st_stateowner->so_is_open_owner &&
7183 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
7184 return nfserr_bad_stateid;
7185 return nfs_ok;
7186 }
7187
nfsd4_validate_stateid(struct nfs4_client * cl,stateid_t * stateid)7188 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
7189 {
7190 struct nfs4_stid *s;
7191 __be32 status = nfserr_bad_stateid;
7192
7193 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
7194 CLOSE_STATEID(stateid))
7195 return status;
7196 spin_lock(&cl->cl_lock);
7197 s = find_stateid_locked(cl, stateid);
7198 if (!s)
7199 goto out_unlock;
7200 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
7201 if (status)
7202 goto out_unlock;
7203 status = nfsd4_verify_open_stid(s);
7204 if (status)
7205 goto out_unlock;
7206
7207 switch (s->sc_type) {
7208 case SC_TYPE_DELEG:
7209 status = nfs_ok;
7210 break;
7211 case SC_TYPE_OPEN:
7212 case SC_TYPE_LOCK:
7213 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
7214 break;
7215 default:
7216 printk("unknown stateid type %x\n", s->sc_type);
7217 status = nfserr_bad_stateid;
7218 }
7219 out_unlock:
7220 spin_unlock(&cl->cl_lock);
7221 if (status == nfserr_admin_revoked)
7222 nfsd40_drop_revoked_stid(cl, stateid);
7223 return status;
7224 }
7225
7226 __be32
nfsd4_lookup_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid,unsigned short typemask,unsigned short statusmask,struct nfs4_stid ** s,struct nfsd_net * nn)7227 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
7228 stateid_t *stateid,
7229 unsigned short typemask, unsigned short statusmask,
7230 struct nfs4_stid **s, struct nfsd_net *nn)
7231 {
7232 __be32 status;
7233 struct nfs4_stid *stid;
7234 bool return_revoked = false;
7235
7236 /*
7237 * only return revoked delegations if explicitly asked.
7238 * otherwise we report revoked or bad_stateid status.
7239 */
7240 if (statusmask & SC_STATUS_REVOKED)
7241 return_revoked = true;
7242 if (typemask & SC_TYPE_DELEG)
7243 /* Always allow REVOKED for DELEG so we can
7244 * return the appropriate error.
7245 */
7246 statusmask |= SC_STATUS_REVOKED;
7247
7248 statusmask |= SC_STATUS_ADMIN_REVOKED | SC_STATUS_FREEABLE;
7249
7250 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
7251 CLOSE_STATEID(stateid))
7252 return nfserr_bad_stateid;
7253 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
7254 if (status == nfserr_stale_clientid) {
7255 if (cstate->session)
7256 return nfserr_bad_stateid;
7257 return nfserr_stale_stateid;
7258 }
7259 if (status)
7260 return status;
7261 stid = find_stateid_by_type(cstate->clp, stateid, typemask, statusmask);
7262 if (!stid)
7263 return nfserr_bad_stateid;
7264 if ((stid->sc_status & SC_STATUS_REVOKED) && !return_revoked) {
7265 nfs4_put_stid(stid);
7266 return nfserr_deleg_revoked;
7267 }
7268 if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) {
7269 nfsd40_drop_revoked_stid(cstate->clp, stateid);
7270 nfs4_put_stid(stid);
7271 return nfserr_admin_revoked;
7272 }
7273 *s = stid;
7274 return nfs_ok;
7275 }
7276
7277 static struct nfsd_file *
nfs4_find_file(struct nfs4_stid * s,int flags)7278 nfs4_find_file(struct nfs4_stid *s, int flags)
7279 {
7280 struct nfsd_file *ret = NULL;
7281
7282 if (!s || s->sc_status)
7283 return NULL;
7284
7285 switch (s->sc_type) {
7286 case SC_TYPE_DELEG:
7287 case SC_TYPE_OPEN:
7288 case SC_TYPE_LOCK:
7289 if (flags & RD_STATE)
7290 ret = find_readable_file(s->sc_file);
7291 else
7292 ret = find_writeable_file(s->sc_file);
7293 }
7294
7295 return ret;
7296 }
7297
7298 static __be32
nfs4_check_olstateid(struct nfs4_ol_stateid * ols,int flags)7299 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
7300 {
7301 __be32 status;
7302
7303 status = nfsd4_check_openowner_confirmed(ols);
7304 if (status)
7305 return status;
7306 return nfs4_check_openmode(ols, flags);
7307 }
7308
7309 static __be32
nfs4_check_file(struct svc_rqst * rqstp,struct svc_fh * fhp,struct nfs4_stid * s,struct nfsd_file ** nfp,int flags)7310 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
7311 struct nfsd_file **nfp, int flags)
7312 {
7313 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
7314 struct nfsd_file *nf;
7315 __be32 status;
7316
7317 nf = nfs4_find_file(s, flags);
7318 if (nf) {
7319 status = nfsd_permission(&rqstp->rq_cred,
7320 fhp->fh_export, fhp->fh_dentry,
7321 acc | NFSD_MAY_OWNER_OVERRIDE);
7322 if (status) {
7323 nfsd_file_put(nf);
7324 goto out;
7325 }
7326 } else {
7327 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
7328 if (status)
7329 return status;
7330 }
7331 *nfp = nf;
7332 out:
7333 return status;
7334 }
7335 static void
_free_cpntf_state_locked(struct nfsd_net * nn,struct nfs4_cpntf_state * cps)7336 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
7337 {
7338 WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID);
7339 if (!refcount_dec_and_test(&cps->cp_stateid.cs_count))
7340 return;
7341 list_del(&cps->cp_list);
7342 idr_remove(&nn->s2s_cp_stateids,
7343 cps->cp_stateid.cs_stid.si_opaque.so_id);
7344 kfree(cps);
7345 }
7346 /*
7347 * A READ from an inter server to server COPY will have a
7348 * copy stateid. Look up the copy notify stateid from the
7349 * idr structure and take a reference on it.
7350 */
manage_cpntf_state(struct nfsd_net * nn,stateid_t * st,struct nfs4_client * clp,struct nfs4_cpntf_state ** cps)7351 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
7352 struct nfs4_client *clp,
7353 struct nfs4_cpntf_state **cps)
7354 {
7355 copy_stateid_t *cps_t;
7356 struct nfs4_cpntf_state *state = NULL;
7357
7358 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
7359 return nfserr_bad_stateid;
7360 spin_lock(&nn->s2s_cp_lock);
7361 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
7362 if (cps_t) {
7363 state = container_of(cps_t, struct nfs4_cpntf_state,
7364 cp_stateid);
7365 if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) {
7366 state = NULL;
7367 goto unlock;
7368 }
7369 if (!clp)
7370 refcount_inc(&state->cp_stateid.cs_count);
7371 else
7372 _free_cpntf_state_locked(nn, state);
7373 }
7374 unlock:
7375 spin_unlock(&nn->s2s_cp_lock);
7376 if (!state)
7377 return nfserr_bad_stateid;
7378 if (!clp)
7379 *cps = state;
7380 return 0;
7381 }
7382
find_cpntf_state(struct nfsd_net * nn,stateid_t * st,struct nfs4_stid ** stid)7383 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
7384 struct nfs4_stid **stid)
7385 {
7386 __be32 status;
7387 struct nfs4_cpntf_state *cps = NULL;
7388 struct nfs4_client *found;
7389
7390 status = manage_cpntf_state(nn, st, NULL, &cps);
7391 if (status)
7392 return status;
7393
7394 cps->cpntf_time = ktime_get_boottime_seconds();
7395
7396 status = nfserr_expired;
7397 found = lookup_clientid(&cps->cp_p_clid, true, nn);
7398 if (!found)
7399 goto out;
7400
7401 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
7402 SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK,
7403 0);
7404 if (*stid)
7405 status = nfs_ok;
7406 else
7407 status = nfserr_bad_stateid;
7408
7409 put_client_renew(found);
7410 out:
7411 nfs4_put_cpntf_state(nn, cps);
7412 return status;
7413 }
7414
nfs4_put_cpntf_state(struct nfsd_net * nn,struct nfs4_cpntf_state * cps)7415 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
7416 {
7417 spin_lock(&nn->s2s_cp_lock);
7418 _free_cpntf_state_locked(nn, cps);
7419 spin_unlock(&nn->s2s_cp_lock);
7420 }
7421
7422 /**
7423 * nfs4_preprocess_stateid_op - find and prep stateid for an operation
7424 * @rqstp: incoming request from client
7425 * @cstate: current compound state
7426 * @fhp: filehandle associated with requested stateid
7427 * @stateid: stateid (provided by client)
7428 * @flags: flags describing type of operation to be done
7429 * @nfp: optional nfsd_file return pointer (may be NULL)
7430 * @cstid: optional returned nfs4_stid pointer (may be NULL)
7431 *
7432 * Given info from the client, look up a nfs4_stid for the operation. On
7433 * success, it returns a reference to the nfs4_stid and/or the nfsd_file
7434 * associated with it.
7435 */
7436 __be32
nfs4_preprocess_stateid_op(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct svc_fh * fhp,stateid_t * stateid,int flags,struct nfsd_file ** nfp,struct nfs4_stid ** cstid)7437 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
7438 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
7439 stateid_t *stateid, int flags, struct nfsd_file **nfp,
7440 struct nfs4_stid **cstid)
7441 {
7442 struct net *net = SVC_NET(rqstp);
7443 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7444 struct nfs4_stid *s = NULL;
7445 __be32 status;
7446
7447 if (nfp)
7448 *nfp = NULL;
7449
7450 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
7451 status = check_special_stateids(net, fhp, stateid, flags);
7452 goto done;
7453 }
7454
7455 status = nfsd4_lookup_stateid(cstate, stateid,
7456 SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK,
7457 0, &s, nn);
7458 if (status == nfserr_bad_stateid)
7459 status = find_cpntf_state(nn, stateid, &s);
7460 if (status)
7461 return status;
7462 status = nfsd4_stid_check_stateid_generation(stateid, s,
7463 nfsd4_has_session(cstate));
7464 if (status)
7465 goto out;
7466
7467 switch (s->sc_type) {
7468 case SC_TYPE_DELEG:
7469 status = nfs4_check_delegmode(delegstateid(s), flags);
7470 break;
7471 case SC_TYPE_OPEN:
7472 case SC_TYPE_LOCK:
7473 status = nfs4_check_olstateid(openlockstateid(s), flags);
7474 break;
7475 }
7476 if (status)
7477 goto out;
7478 status = nfs4_check_fh(fhp, s);
7479
7480 done:
7481 if (status == nfs_ok && nfp)
7482 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
7483 out:
7484 if (s) {
7485 if (!status && cstid)
7486 *cstid = s;
7487 else
7488 nfs4_put_stid(s);
7489 }
7490 return status;
7491 }
7492
7493 /*
7494 * Test if the stateid is valid
7495 */
7496 __be32
nfsd4_test_stateid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7497 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7498 union nfsd4_op_u *u)
7499 {
7500 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
7501 struct nfsd4_test_stateid_id *stateid;
7502 struct nfs4_client *cl = cstate->clp;
7503
7504 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
7505 stateid->ts_id_status =
7506 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
7507
7508 return nfs_ok;
7509 }
7510
7511 static __be32
nfsd4_free_lock_stateid(stateid_t * stateid,struct nfs4_stid * s)7512 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
7513 {
7514 struct nfs4_ol_stateid *stp = openlockstateid(s);
7515 __be32 ret;
7516
7517 ret = nfsd4_lock_ol_stateid(stp);
7518 if (ret)
7519 goto out_put_stid;
7520
7521 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
7522 if (ret)
7523 goto out;
7524
7525 ret = nfserr_locks_held;
7526 if (check_for_locks(stp->st_stid.sc_file,
7527 lockowner(stp->st_stateowner)))
7528 goto out;
7529
7530 release_lock_stateid(stp);
7531 ret = nfs_ok;
7532
7533 out:
7534 mutex_unlock(&stp->st_mutex);
7535 out_put_stid:
7536 nfs4_put_stid(s);
7537 return ret;
7538 }
7539
7540 __be32
nfsd4_free_stateid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7541 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7542 union nfsd4_op_u *u)
7543 {
7544 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
7545 stateid_t *stateid = &free_stateid->fr_stateid;
7546 struct nfs4_stid *s;
7547 struct nfs4_delegation *dp;
7548 struct nfs4_client *cl = cstate->clp;
7549 __be32 ret = nfserr_bad_stateid;
7550
7551 spin_lock(&cl->cl_lock);
7552 s = find_stateid_locked(cl, stateid);
7553 if (!s || s->sc_status & SC_STATUS_CLOSED)
7554 goto out_unlock;
7555 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) {
7556 nfsd4_drop_revoked_stid(s);
7557 ret = nfs_ok;
7558 goto out;
7559 }
7560 spin_lock(&s->sc_lock);
7561 switch (s->sc_type) {
7562 case SC_TYPE_DELEG:
7563 if (s->sc_status & SC_STATUS_REVOKED) {
7564 s->sc_status |= SC_STATUS_CLOSED;
7565 spin_unlock(&s->sc_lock);
7566 dp = delegstateid(s);
7567 if (s->sc_status & SC_STATUS_FREEABLE)
7568 list_del_init(&dp->dl_recall_lru);
7569 s->sc_status |= SC_STATUS_FREED;
7570 spin_unlock(&cl->cl_lock);
7571 nfs4_put_stid(s);
7572 ret = nfs_ok;
7573 goto out;
7574 }
7575 ret = nfserr_locks_held;
7576 break;
7577 case SC_TYPE_OPEN:
7578 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
7579 if (ret)
7580 break;
7581 ret = nfserr_locks_held;
7582 break;
7583 case SC_TYPE_LOCK:
7584 spin_unlock(&s->sc_lock);
7585 refcount_inc(&s->sc_count);
7586 spin_unlock(&cl->cl_lock);
7587 ret = nfsd4_free_lock_stateid(stateid, s);
7588 goto out;
7589 }
7590 spin_unlock(&s->sc_lock);
7591 out_unlock:
7592 spin_unlock(&cl->cl_lock);
7593 out:
7594 return ret;
7595 }
7596
7597 static inline int
setlkflg(int type)7598 setlkflg (int type)
7599 {
7600 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
7601 RD_STATE : WR_STATE;
7602 }
7603
nfs4_seqid_op_checks(struct nfsd4_compound_state * cstate,stateid_t * stateid,u32 seqid,struct nfs4_ol_stateid * stp)7604 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
7605 {
7606 struct svc_fh *current_fh = &cstate->current_fh;
7607 struct nfs4_stateowner *sop = stp->st_stateowner;
7608 __be32 status;
7609
7610 status = nfsd4_check_seqid(cstate, sop, seqid);
7611 if (status)
7612 return status;
7613 status = nfsd4_lock_ol_stateid(stp);
7614 if (status != nfs_ok)
7615 return status;
7616 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
7617 if (status == nfs_ok)
7618 status = nfs4_check_fh(current_fh, &stp->st_stid);
7619 if (status != nfs_ok)
7620 mutex_unlock(&stp->st_mutex);
7621 return status;
7622 }
7623
7624 /**
7625 * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op
7626 * @cstate: compund state
7627 * @seqid: seqid (provided by client)
7628 * @stateid: stateid (provided by client)
7629 * @typemask: mask of allowable types for this operation
7630 * @statusmask: mask of allowed states: 0 or STID_CLOSED
7631 * @stpp: return pointer for the stateid found
7632 * @nn: net namespace for request
7633 *
7634 * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and
7635 * return it in @stpp. On a nfs_ok return, the returned stateid will
7636 * have its st_mutex locked.
7637 */
7638 static __be32
nfs4_preprocess_seqid_op(struct nfsd4_compound_state * cstate,u32 seqid,stateid_t * stateid,unsigned short typemask,unsigned short statusmask,struct nfs4_ol_stateid ** stpp,struct nfsd_net * nn)7639 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
7640 stateid_t *stateid,
7641 unsigned short typemask, unsigned short statusmask,
7642 struct nfs4_ol_stateid **stpp,
7643 struct nfsd_net *nn)
7644 {
7645 __be32 status;
7646 struct nfs4_stid *s;
7647 struct nfs4_ol_stateid *stp = NULL;
7648
7649 trace_nfsd_preprocess(seqid, stateid);
7650
7651 *stpp = NULL;
7652 retry:
7653 status = nfsd4_lookup_stateid(cstate, stateid,
7654 typemask, statusmask, &s, nn);
7655 if (status)
7656 return status;
7657 stp = openlockstateid(s);
7658 if (nfsd4_cstate_assign_replay(cstate, stp->st_stateowner) == -EAGAIN) {
7659 nfs4_put_stateowner(stp->st_stateowner);
7660 goto retry;
7661 }
7662
7663 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
7664 if (!status)
7665 *stpp = stp;
7666 else
7667 nfs4_put_stid(&stp->st_stid);
7668 return status;
7669 }
7670
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state * cstate,u32 seqid,stateid_t * stateid,struct nfs4_ol_stateid ** stpp,struct nfsd_net * nn)7671 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
7672 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
7673 {
7674 __be32 status;
7675 struct nfs4_openowner *oo;
7676 struct nfs4_ol_stateid *stp;
7677
7678 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
7679 SC_TYPE_OPEN, 0, &stp, nn);
7680 if (status)
7681 return status;
7682 oo = openowner(stp->st_stateowner);
7683 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
7684 mutex_unlock(&stp->st_mutex);
7685 nfs4_put_stid(&stp->st_stid);
7686 return nfserr_bad_stateid;
7687 }
7688 *stpp = stp;
7689 return nfs_ok;
7690 }
7691
7692 __be32
nfsd4_open_confirm(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7693 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7694 union nfsd4_op_u *u)
7695 {
7696 struct nfsd4_open_confirm *oc = &u->open_confirm;
7697 __be32 status;
7698 struct nfs4_openowner *oo;
7699 struct nfs4_ol_stateid *stp;
7700 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7701
7702 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
7703 cstate->current_fh.fh_dentry);
7704
7705 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
7706 if (status)
7707 return status;
7708
7709 status = nfs4_preprocess_seqid_op(cstate,
7710 oc->oc_seqid, &oc->oc_req_stateid,
7711 SC_TYPE_OPEN, 0, &stp, nn);
7712 if (status)
7713 goto out;
7714 oo = openowner(stp->st_stateowner);
7715 status = nfserr_bad_stateid;
7716 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
7717 mutex_unlock(&stp->st_mutex);
7718 goto put_stateid;
7719 }
7720 oo->oo_flags |= NFS4_OO_CONFIRMED;
7721 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
7722 mutex_unlock(&stp->st_mutex);
7723 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
7724 nfsd4_client_record_create(oo->oo_owner.so_client);
7725 status = nfs_ok;
7726 put_stateid:
7727 nfs4_put_stid(&stp->st_stid);
7728 out:
7729 nfsd4_bump_seqid(cstate, status);
7730 return status;
7731 }
7732
nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid * stp,u32 access)7733 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
7734 {
7735 if (!test_access(access, stp))
7736 return;
7737 nfs4_file_put_access(stp->st_stid.sc_file, access);
7738 clear_access(access, stp);
7739 }
7740
nfs4_stateid_downgrade(struct nfs4_ol_stateid * stp,u32 to_access)7741 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
7742 {
7743 switch (to_access) {
7744 case NFS4_SHARE_ACCESS_READ:
7745 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
7746 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7747 break;
7748 case NFS4_SHARE_ACCESS_WRITE:
7749 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
7750 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7751 break;
7752 case NFS4_SHARE_ACCESS_BOTH:
7753 break;
7754 default:
7755 WARN_ON_ONCE(1);
7756 }
7757 }
7758
7759 __be32
nfsd4_open_downgrade(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7760 nfsd4_open_downgrade(struct svc_rqst *rqstp,
7761 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
7762 {
7763 struct nfsd4_open_downgrade *od = &u->open_downgrade;
7764 __be32 status;
7765 struct nfs4_ol_stateid *stp;
7766 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7767
7768 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
7769 cstate->current_fh.fh_dentry);
7770
7771 /* We don't yet support WANT bits: */
7772 if (od->od_deleg_want)
7773 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
7774 od->od_deleg_want);
7775
7776 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
7777 &od->od_stateid, &stp, nn);
7778 if (status)
7779 goto out;
7780 status = nfserr_inval;
7781 if (!test_access(od->od_share_access, stp)) {
7782 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
7783 stp->st_access_bmap, od->od_share_access);
7784 goto put_stateid;
7785 }
7786 if (!test_deny(od->od_share_deny, stp)) {
7787 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
7788 stp->st_deny_bmap, od->od_share_deny);
7789 goto put_stateid;
7790 }
7791 nfs4_stateid_downgrade(stp, od->od_share_access);
7792 reset_union_bmap_deny(od->od_share_deny, stp);
7793 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
7794 status = nfs_ok;
7795 put_stateid:
7796 mutex_unlock(&stp->st_mutex);
7797 nfs4_put_stid(&stp->st_stid);
7798 out:
7799 nfsd4_bump_seqid(cstate, status);
7800 return status;
7801 }
7802
nfsd4_close_open_stateid(struct nfs4_ol_stateid * s)7803 static bool nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
7804 {
7805 struct nfs4_client *clp = s->st_stid.sc_client;
7806 bool unhashed;
7807 LIST_HEAD(reaplist);
7808 struct nfs4_ol_stateid *stp;
7809
7810 spin_lock(&clp->cl_lock);
7811 unhashed = unhash_open_stateid(s, &reaplist);
7812
7813 if (clp->cl_minorversion) {
7814 if (unhashed)
7815 put_ol_stateid_locked(s, &reaplist);
7816 spin_unlock(&clp->cl_lock);
7817 list_for_each_entry(stp, &reaplist, st_locks)
7818 nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
7819 free_ol_stateid_reaplist(&reaplist);
7820 return false;
7821 } else {
7822 spin_unlock(&clp->cl_lock);
7823 free_ol_stateid_reaplist(&reaplist);
7824 return unhashed;
7825 }
7826 }
7827
7828 /*
7829 * nfs4_unlock_state() called after encode
7830 */
7831 __be32
nfsd4_close(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7832 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7833 union nfsd4_op_u *u)
7834 {
7835 struct nfsd4_close *close = &u->close;
7836 __be32 status;
7837 struct nfs4_ol_stateid *stp;
7838 struct net *net = SVC_NET(rqstp);
7839 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7840 bool need_move_to_close_list;
7841
7842 dprintk("NFSD: nfsd4_close on file %pd\n",
7843 cstate->current_fh.fh_dentry);
7844
7845 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
7846 &close->cl_stateid,
7847 SC_TYPE_OPEN, SC_STATUS_CLOSED,
7848 &stp, nn);
7849 nfsd4_bump_seqid(cstate, status);
7850 if (status)
7851 goto out;
7852
7853 spin_lock(&stp->st_stid.sc_client->cl_lock);
7854 stp->st_stid.sc_status |= SC_STATUS_CLOSED;
7855 spin_unlock(&stp->st_stid.sc_client->cl_lock);
7856
7857 /*
7858 * Technically we don't _really_ have to increment or copy it, since
7859 * it should just be gone after this operation and we clobber the
7860 * copied value below, but we continue to do so here just to ensure
7861 * that racing ops see that there was a state change.
7862 */
7863 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
7864
7865 need_move_to_close_list = nfsd4_close_open_stateid(stp);
7866 mutex_unlock(&stp->st_mutex);
7867 if (need_move_to_close_list)
7868 move_to_close_lru(stp, net);
7869
7870 /* v4.1+ suggests that we send a special stateid in here, since the
7871 * clients should just ignore this anyway. Since this is not useful
7872 * for v4.0 clients either, we set it to the special close_stateid
7873 * universally.
7874 *
7875 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
7876 */
7877 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
7878
7879 /* put reference from nfs4_preprocess_seqid_op */
7880 nfs4_put_stid(&stp->st_stid);
7881 out:
7882 return status;
7883 }
7884
7885 __be32
nfsd4_delegreturn(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7886 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7887 union nfsd4_op_u *u)
7888 {
7889 struct nfsd4_delegreturn *dr = &u->delegreturn;
7890 struct nfs4_delegation *dp;
7891 stateid_t *stateid = &dr->dr_stateid;
7892 struct nfs4_stid *s;
7893 __be32 status;
7894 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7895
7896 status = fh_verify(rqstp, &cstate->current_fh, 0, 0);
7897 if (status)
7898 return status;
7899
7900 status = nfsd4_lookup_stateid(cstate, stateid, SC_TYPE_DELEG, SC_STATUS_REVOKED, &s, nn);
7901 if (status)
7902 goto out;
7903 dp = delegstateid(s);
7904 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
7905 if (status)
7906 goto put_stateid;
7907
7908 trace_nfsd_deleg_return(stateid);
7909 destroy_delegation(dp);
7910 smp_mb__after_atomic();
7911 wake_up_var(d_inode(cstate->current_fh.fh_dentry));
7912 put_stateid:
7913 nfs4_put_stid(&dp->dl_stid);
7914 out:
7915 return status;
7916 }
7917
7918 /* last octet in a range */
7919 static inline u64
last_byte_offset(u64 start,u64 len)7920 last_byte_offset(u64 start, u64 len)
7921 {
7922 u64 end;
7923
7924 WARN_ON_ONCE(!len);
7925 end = start + len;
7926 return end > start ? end - 1: NFS4_MAX_UINT64;
7927 }
7928
7929 /*
7930 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
7931 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
7932 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
7933 * locking, this prevents us from being completely protocol-compliant. The
7934 * real solution to this problem is to start using unsigned file offsets in
7935 * the VFS, but this is a very deep change!
7936 */
7937 static inline void
nfs4_transform_lock_offset(struct file_lock * lock)7938 nfs4_transform_lock_offset(struct file_lock *lock)
7939 {
7940 if (lock->fl_start < 0)
7941 lock->fl_start = OFFSET_MAX;
7942 if (lock->fl_end < 0)
7943 lock->fl_end = OFFSET_MAX;
7944 }
7945
7946 static fl_owner_t
nfsd4_lm_get_owner(fl_owner_t owner)7947 nfsd4_lm_get_owner(fl_owner_t owner)
7948 {
7949 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7950
7951 nfs4_get_stateowner(&lo->lo_owner);
7952 return owner;
7953 }
7954
7955 static void
nfsd4_lm_put_owner(fl_owner_t owner)7956 nfsd4_lm_put_owner(fl_owner_t owner)
7957 {
7958 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7959
7960 if (lo)
7961 nfs4_put_stateowner(&lo->lo_owner);
7962 }
7963
7964 /* return pointer to struct nfs4_client if client is expirable */
7965 static bool
nfsd4_lm_lock_expirable(struct file_lock * cfl)7966 nfsd4_lm_lock_expirable(struct file_lock *cfl)
7967 {
7968 struct nfs4_lockowner *lo = (struct nfs4_lockowner *) cfl->c.flc_owner;
7969 struct nfs4_client *clp = lo->lo_owner.so_client;
7970 struct nfsd_net *nn;
7971
7972 if (try_to_expire_client(clp)) {
7973 nn = net_generic(clp->net, nfsd_net_id);
7974 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
7975 return true;
7976 }
7977 return false;
7978 }
7979
7980 /* schedule laundromat to run immediately and wait for it to complete */
7981 static void
nfsd4_lm_expire_lock(void)7982 nfsd4_lm_expire_lock(void)
7983 {
7984 flush_workqueue(laundry_wq);
7985 }
7986
7987 static void
nfsd4_lm_notify(struct file_lock * fl)7988 nfsd4_lm_notify(struct file_lock *fl)
7989 {
7990 struct nfs4_lockowner *lo = (struct nfs4_lockowner *) fl->c.flc_owner;
7991 struct net *net = lo->lo_owner.so_client->net;
7992 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7993 struct nfsd4_blocked_lock *nbl = container_of(fl,
7994 struct nfsd4_blocked_lock, nbl_lock);
7995 bool queue = false;
7996
7997 /* An empty list means that something else is going to be using it */
7998 spin_lock(&nn->blocked_locks_lock);
7999 if (!list_empty(&nbl->nbl_list)) {
8000 list_del_init(&nbl->nbl_list);
8001 list_del_init(&nbl->nbl_lru);
8002 queue = true;
8003 }
8004 spin_unlock(&nn->blocked_locks_lock);
8005
8006 if (queue) {
8007 trace_nfsd_cb_notify_lock(lo, nbl);
8008 nfsd4_try_run_cb(&nbl->nbl_cb);
8009 }
8010 }
8011
8012 static const struct lock_manager_operations nfsd_posix_mng_ops = {
8013 .lm_mod_owner = THIS_MODULE,
8014 .lm_notify = nfsd4_lm_notify,
8015 .lm_get_owner = nfsd4_lm_get_owner,
8016 .lm_put_owner = nfsd4_lm_put_owner,
8017 .lm_lock_expirable = nfsd4_lm_lock_expirable,
8018 .lm_expire_lock = nfsd4_lm_expire_lock,
8019 };
8020
8021 static inline void
nfs4_set_lock_denied(struct file_lock * fl,struct nfsd4_lock_denied * deny)8022 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
8023 {
8024 struct nfs4_lockowner *lo;
8025
8026 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
8027 lo = (struct nfs4_lockowner *) fl->c.flc_owner;
8028 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
8029 GFP_KERNEL);
8030 if (!deny->ld_owner.data)
8031 /* We just don't care that much */
8032 goto nevermind;
8033 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
8034 } else {
8035 nevermind:
8036 deny->ld_owner.len = 0;
8037 deny->ld_owner.data = NULL;
8038 deny->ld_clientid.cl_boot = 0;
8039 deny->ld_clientid.cl_id = 0;
8040 }
8041 deny->ld_start = fl->fl_start;
8042 deny->ld_length = NFS4_MAX_UINT64;
8043 if (fl->fl_end != NFS4_MAX_UINT64)
8044 deny->ld_length = fl->fl_end - fl->fl_start + 1;
8045 deny->ld_type = NFS4_READ_LT;
8046 if (fl->c.flc_type != F_RDLCK)
8047 deny->ld_type = NFS4_WRITE_LT;
8048 }
8049
8050 static struct nfs4_lockowner *
find_lockowner_str_locked(struct nfs4_client * clp,struct xdr_netobj * owner)8051 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
8052 {
8053 unsigned int strhashval = ownerstr_hashval(owner);
8054 struct nfs4_stateowner *so;
8055
8056 lockdep_assert_held(&clp->cl_lock);
8057
8058 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
8059 so_strhash) {
8060 if (so->so_is_open_owner)
8061 continue;
8062 if (same_owner_str(so, owner))
8063 return lockowner(nfs4_get_stateowner(so));
8064 }
8065 return NULL;
8066 }
8067
8068 static struct nfs4_lockowner *
find_lockowner_str(struct nfs4_client * clp,struct xdr_netobj * owner)8069 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
8070 {
8071 struct nfs4_lockowner *lo;
8072
8073 spin_lock(&clp->cl_lock);
8074 lo = find_lockowner_str_locked(clp, owner);
8075 spin_unlock(&clp->cl_lock);
8076 return lo;
8077 }
8078
nfs4_unhash_lockowner(struct nfs4_stateowner * sop)8079 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
8080 {
8081 unhash_lockowner_locked(lockowner(sop));
8082 }
8083
nfs4_free_lockowner(struct nfs4_stateowner * sop)8084 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
8085 {
8086 struct nfs4_lockowner *lo = lockowner(sop);
8087
8088 kmem_cache_free(lockowner_slab, lo);
8089 }
8090
8091 static const struct nfs4_stateowner_operations lockowner_ops = {
8092 .so_unhash = nfs4_unhash_lockowner,
8093 .so_free = nfs4_free_lockowner,
8094 };
8095
8096 /*
8097 * Alloc a lock owner structure.
8098 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
8099 * occurred.
8100 *
8101 * strhashval = ownerstr_hashval
8102 */
8103 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)8104 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
8105 struct nfs4_ol_stateid *open_stp,
8106 struct nfsd4_lock *lock)
8107 {
8108 struct nfs4_lockowner *lo, *ret;
8109
8110 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
8111 if (!lo)
8112 return NULL;
8113 INIT_LIST_HEAD(&lo->lo_blocked);
8114 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
8115 lo->lo_owner.so_is_open_owner = 0;
8116 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
8117 lo->lo_owner.so_ops = &lockowner_ops;
8118 spin_lock(&clp->cl_lock);
8119 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
8120 if (ret == NULL) {
8121 list_add(&lo->lo_owner.so_strhash,
8122 &clp->cl_ownerstr_hashtbl[strhashval]);
8123 ret = lo;
8124 } else
8125 nfs4_free_stateowner(&lo->lo_owner);
8126
8127 spin_unlock(&clp->cl_lock);
8128 return ret;
8129 }
8130
8131 static struct nfs4_ol_stateid *
find_lock_stateid(const struct nfs4_lockowner * lo,const struct nfs4_ol_stateid * ost)8132 find_lock_stateid(const struct nfs4_lockowner *lo,
8133 const struct nfs4_ol_stateid *ost)
8134 {
8135 struct nfs4_ol_stateid *lst;
8136
8137 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
8138
8139 /* If ost is not hashed, ost->st_locks will not be valid */
8140 if (!nfs4_ol_stateid_unhashed(ost))
8141 list_for_each_entry(lst, &ost->st_locks, st_locks) {
8142 if (lst->st_stateowner == &lo->lo_owner) {
8143 refcount_inc(&lst->st_stid.sc_count);
8144 return lst;
8145 }
8146 }
8147 return NULL;
8148 }
8149
8150 static struct nfs4_ol_stateid *
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)8151 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
8152 struct nfs4_file *fp, struct inode *inode,
8153 struct nfs4_ol_stateid *open_stp)
8154 {
8155 struct nfs4_client *clp = lo->lo_owner.so_client;
8156 struct nfs4_ol_stateid *retstp;
8157
8158 mutex_init(&stp->st_mutex);
8159 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
8160 retry:
8161 spin_lock(&clp->cl_lock);
8162 if (nfs4_ol_stateid_unhashed(open_stp))
8163 goto out_close;
8164 retstp = find_lock_stateid(lo, open_stp);
8165 if (retstp)
8166 goto out_found;
8167 refcount_inc(&stp->st_stid.sc_count);
8168 stp->st_stid.sc_type = SC_TYPE_LOCK;
8169 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
8170 get_nfs4_file(fp);
8171 stp->st_stid.sc_file = fp;
8172 stp->st_access_bmap = 0;
8173 stp->st_deny_bmap = open_stp->st_deny_bmap;
8174 stp->st_openstp = open_stp;
8175 spin_lock(&fp->fi_lock);
8176 list_add(&stp->st_locks, &open_stp->st_locks);
8177 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
8178 list_add(&stp->st_perfile, &fp->fi_stateids);
8179 spin_unlock(&fp->fi_lock);
8180 spin_unlock(&clp->cl_lock);
8181 return stp;
8182 out_found:
8183 spin_unlock(&clp->cl_lock);
8184 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
8185 nfs4_put_stid(&retstp->st_stid);
8186 goto retry;
8187 }
8188 /* To keep mutex tracking happy */
8189 mutex_unlock(&stp->st_mutex);
8190 return retstp;
8191 out_close:
8192 spin_unlock(&clp->cl_lock);
8193 mutex_unlock(&stp->st_mutex);
8194 return NULL;
8195 }
8196
8197 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)8198 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
8199 struct inode *inode, struct nfs4_ol_stateid *ost,
8200 bool *new)
8201 {
8202 struct nfs4_stid *ns = NULL;
8203 struct nfs4_ol_stateid *lst;
8204 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
8205 struct nfs4_client *clp = oo->oo_owner.so_client;
8206
8207 *new = false;
8208 spin_lock(&clp->cl_lock);
8209 lst = find_lock_stateid(lo, ost);
8210 spin_unlock(&clp->cl_lock);
8211 if (lst != NULL) {
8212 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
8213 goto out;
8214 nfs4_put_stid(&lst->st_stid);
8215 }
8216 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
8217 if (ns == NULL)
8218 return NULL;
8219
8220 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
8221 if (lst == openlockstateid(ns))
8222 *new = true;
8223 else
8224 nfs4_put_stid(ns);
8225 out:
8226 return lst;
8227 }
8228
8229 static int
check_lock_length(u64 offset,u64 length)8230 check_lock_length(u64 offset, u64 length)
8231 {
8232 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
8233 (length > ~offset)));
8234 }
8235
get_lock_access(struct nfs4_ol_stateid * lock_stp,u32 access)8236 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
8237 {
8238 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
8239
8240 lockdep_assert_held(&fp->fi_lock);
8241
8242 if (test_access(access, lock_stp))
8243 return;
8244 __nfs4_file_get_access(fp, access);
8245 set_access(access, lock_stp);
8246 }
8247
8248 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)8249 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
8250 struct nfs4_ol_stateid *ost,
8251 struct nfsd4_lock *lock,
8252 struct nfs4_ol_stateid **plst, bool *new)
8253 {
8254 __be32 status;
8255 struct nfs4_file *fi = ost->st_stid.sc_file;
8256 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
8257 struct nfs4_client *cl = oo->oo_owner.so_client;
8258 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
8259 struct nfs4_lockowner *lo;
8260 struct nfs4_ol_stateid *lst;
8261 unsigned int strhashval;
8262
8263 lo = find_lockowner_str(cl, &lock->lk_new_owner);
8264 if (!lo) {
8265 strhashval = ownerstr_hashval(&lock->lk_new_owner);
8266 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
8267 if (lo == NULL)
8268 return nfserr_jukebox;
8269 } else {
8270 /* with an existing lockowner, seqids must be the same */
8271 status = nfserr_bad_seqid;
8272 if (!cstate->minorversion &&
8273 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
8274 goto out;
8275 }
8276
8277 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
8278 if (lst == NULL) {
8279 status = nfserr_jukebox;
8280 goto out;
8281 }
8282
8283 status = nfs_ok;
8284 *plst = lst;
8285 out:
8286 nfs4_put_stateowner(&lo->lo_owner);
8287 return status;
8288 }
8289
8290 /*
8291 * LOCK operation
8292 */
8293 __be32
nfsd4_lock(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8294 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
8295 union nfsd4_op_u *u)
8296 {
8297 struct nfsd4_lock *lock = &u->lock;
8298 struct nfs4_openowner *open_sop = NULL;
8299 struct nfs4_lockowner *lock_sop = NULL;
8300 struct nfs4_ol_stateid *lock_stp = NULL;
8301 struct nfs4_ol_stateid *open_stp = NULL;
8302 struct nfs4_file *fp;
8303 struct nfsd_file *nf = NULL;
8304 struct nfsd4_blocked_lock *nbl = NULL;
8305 struct file_lock *file_lock = NULL;
8306 struct file_lock *conflock = NULL;
8307 __be32 status = 0;
8308 int lkflg;
8309 int err;
8310 bool new = false;
8311 unsigned char type;
8312 unsigned int flags = FL_POSIX;
8313 struct net *net = SVC_NET(rqstp);
8314 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8315
8316 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
8317 (long long) lock->lk_offset,
8318 (long long) lock->lk_length);
8319
8320 if (check_lock_length(lock->lk_offset, lock->lk_length))
8321 return nfserr_inval;
8322
8323 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
8324 if (status != nfs_ok)
8325 return status;
8326 if (exportfs_cannot_lock(cstate->current_fh.fh_dentry->d_sb->s_export_op)) {
8327 status = nfserr_notsupp;
8328 goto out;
8329 }
8330
8331 if (lock->lk_is_new) {
8332 if (nfsd4_has_session(cstate))
8333 /* See rfc 5661 18.10.3: given clientid is ignored: */
8334 memcpy(&lock->lk_new_clientid,
8335 &cstate->clp->cl_clientid,
8336 sizeof(clientid_t));
8337
8338 /* validate and update open stateid and open seqid */
8339 status = nfs4_preprocess_confirmed_seqid_op(cstate,
8340 lock->lk_new_open_seqid,
8341 &lock->lk_new_open_stateid,
8342 &open_stp, nn);
8343 if (status)
8344 goto out;
8345 mutex_unlock(&open_stp->st_mutex);
8346 open_sop = openowner(open_stp->st_stateowner);
8347 status = nfserr_bad_stateid;
8348 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
8349 &lock->lk_new_clientid))
8350 goto out;
8351 status = lookup_or_create_lock_state(cstate, open_stp, lock,
8352 &lock_stp, &new);
8353 } else {
8354 status = nfs4_preprocess_seqid_op(cstate,
8355 lock->lk_old_lock_seqid,
8356 &lock->lk_old_lock_stateid,
8357 SC_TYPE_LOCK, 0, &lock_stp,
8358 nn);
8359 }
8360 if (status)
8361 goto out;
8362 lock_sop = lockowner(lock_stp->st_stateowner);
8363
8364 lkflg = setlkflg(lock->lk_type);
8365 status = nfs4_check_openmode(lock_stp, lkflg);
8366 if (status)
8367 goto out;
8368
8369 status = nfserr_grace;
8370 if (locks_in_grace(net) && !lock->lk_reclaim)
8371 goto out;
8372 status = nfserr_no_grace;
8373 if (!locks_in_grace(net) && lock->lk_reclaim)
8374 goto out;
8375
8376 if (lock->lk_reclaim)
8377 flags |= FL_RECLAIM;
8378
8379 fp = lock_stp->st_stid.sc_file;
8380 switch (lock->lk_type) {
8381 case NFS4_READW_LT:
8382 fallthrough;
8383 case NFS4_READ_LT:
8384 spin_lock(&fp->fi_lock);
8385 nf = find_readable_file_locked(fp);
8386 if (nf)
8387 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
8388 spin_unlock(&fp->fi_lock);
8389 type = F_RDLCK;
8390 break;
8391 case NFS4_WRITEW_LT:
8392 fallthrough;
8393 case NFS4_WRITE_LT:
8394 spin_lock(&fp->fi_lock);
8395 nf = find_writeable_file_locked(fp);
8396 if (nf)
8397 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
8398 spin_unlock(&fp->fi_lock);
8399 type = F_WRLCK;
8400 break;
8401 default:
8402 status = nfserr_inval;
8403 goto out;
8404 }
8405
8406 if (!nf) {
8407 status = nfserr_openmode;
8408 goto out;
8409 }
8410
8411 if (lock->lk_type & (NFS4_READW_LT | NFS4_WRITEW_LT) &&
8412 nfsd4_has_session(cstate) &&
8413 locks_can_async_lock(nf->nf_file->f_op))
8414 flags |= FL_SLEEP;
8415
8416 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
8417 if (!nbl) {
8418 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
8419 status = nfserr_jukebox;
8420 goto out;
8421 }
8422
8423 file_lock = &nbl->nbl_lock;
8424 file_lock->c.flc_type = type;
8425 file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
8426 file_lock->c.flc_pid = current->tgid;
8427 file_lock->c.flc_file = nf->nf_file;
8428 file_lock->c.flc_flags = flags;
8429 file_lock->fl_lmops = &nfsd_posix_mng_ops;
8430 file_lock->fl_start = lock->lk_offset;
8431 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
8432 nfs4_transform_lock_offset(file_lock);
8433
8434 conflock = locks_alloc_lock();
8435 if (!conflock) {
8436 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8437 status = nfserr_jukebox;
8438 goto out;
8439 }
8440
8441 if (flags & FL_SLEEP) {
8442 nbl->nbl_time = ktime_get_boottime_seconds();
8443 spin_lock(&nn->blocked_locks_lock);
8444 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
8445 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
8446 kref_get(&nbl->nbl_kref);
8447 spin_unlock(&nn->blocked_locks_lock);
8448 }
8449
8450 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
8451 switch (err) {
8452 case 0: /* success! */
8453 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
8454 status = 0;
8455 if (lock->lk_reclaim)
8456 nn->somebody_reclaimed = true;
8457 break;
8458 case FILE_LOCK_DEFERRED:
8459 kref_put(&nbl->nbl_kref, free_nbl);
8460 nbl = NULL;
8461 fallthrough;
8462 case -EAGAIN: /* conflock holds conflicting lock */
8463 status = nfserr_denied;
8464 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
8465 nfs4_set_lock_denied(conflock, &lock->lk_denied);
8466 break;
8467 case -EDEADLK:
8468 status = nfserr_deadlock;
8469 break;
8470 default:
8471 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
8472 status = nfserrno(err);
8473 break;
8474 }
8475 out:
8476 if (nbl) {
8477 /* dequeue it if we queued it before */
8478 if (flags & FL_SLEEP) {
8479 spin_lock(&nn->blocked_locks_lock);
8480 if (!list_empty(&nbl->nbl_list) &&
8481 !list_empty(&nbl->nbl_lru)) {
8482 list_del_init(&nbl->nbl_list);
8483 list_del_init(&nbl->nbl_lru);
8484 kref_put(&nbl->nbl_kref, free_nbl);
8485 }
8486 /* nbl can use one of lists to be linked to reaplist */
8487 spin_unlock(&nn->blocked_locks_lock);
8488 }
8489 free_blocked_lock(nbl);
8490 }
8491 if (nf)
8492 nfsd_file_put(nf);
8493 if (lock_stp) {
8494 /* Bump seqid manually if the 4.0 replay owner is openowner */
8495 if (cstate->replay_owner &&
8496 cstate->replay_owner != &lock_sop->lo_owner &&
8497 seqid_mutating_err(ntohl(status)))
8498 lock_sop->lo_owner.so_seqid++;
8499
8500 /*
8501 * If this is a new, never-before-used stateid, and we are
8502 * returning an error, then just go ahead and release it.
8503 */
8504 if (status && new)
8505 release_lock_stateid(lock_stp);
8506
8507 mutex_unlock(&lock_stp->st_mutex);
8508
8509 nfs4_put_stid(&lock_stp->st_stid);
8510 }
8511 if (open_stp)
8512 nfs4_put_stid(&open_stp->st_stid);
8513 nfsd4_bump_seqid(cstate, status);
8514 if (conflock)
8515 locks_free_lock(conflock);
8516 return status;
8517 }
8518
nfsd4_lock_release(union nfsd4_op_u * u)8519 void nfsd4_lock_release(union nfsd4_op_u *u)
8520 {
8521 struct nfsd4_lock *lock = &u->lock;
8522 struct nfsd4_lock_denied *deny = &lock->lk_denied;
8523
8524 kfree(deny->ld_owner.data);
8525 }
8526
8527 /*
8528 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
8529 * so we do a temporary open here just to get an open file to pass to
8530 * vfs_test_lock.
8531 */
nfsd_test_lock(struct svc_rqst * rqstp,struct svc_fh * fhp,struct file_lock * lock)8532 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
8533 {
8534 struct nfsd_file *nf;
8535 struct inode *inode;
8536 __be32 err;
8537
8538 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
8539 if (err)
8540 return err;
8541 inode = fhp->fh_dentry->d_inode;
8542 inode_lock(inode); /* to block new leases till after test_lock: */
8543 err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
8544 if (err)
8545 goto out;
8546 lock->c.flc_file = nf->nf_file;
8547 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
8548 lock->c.flc_file = NULL;
8549 out:
8550 inode_unlock(inode);
8551 nfsd_file_put(nf);
8552 return err;
8553 }
8554
8555 /*
8556 * LOCKT operation
8557 */
8558 __be32
nfsd4_lockt(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8559 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
8560 union nfsd4_op_u *u)
8561 {
8562 struct nfsd4_lockt *lockt = &u->lockt;
8563 struct file_lock *file_lock = NULL;
8564 struct nfs4_lockowner *lo = NULL;
8565 __be32 status;
8566 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8567
8568 if (locks_in_grace(SVC_NET(rqstp)))
8569 return nfserr_grace;
8570
8571 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
8572 return nfserr_inval;
8573
8574 if (!nfsd4_has_session(cstate)) {
8575 status = set_client(&lockt->lt_clientid, cstate, nn);
8576 if (status)
8577 goto out;
8578 }
8579
8580 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
8581 goto out;
8582
8583 file_lock = locks_alloc_lock();
8584 if (!file_lock) {
8585 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8586 status = nfserr_jukebox;
8587 goto out;
8588 }
8589
8590 switch (lockt->lt_type) {
8591 case NFS4_READ_LT:
8592 case NFS4_READW_LT:
8593 file_lock->c.flc_type = F_RDLCK;
8594 break;
8595 case NFS4_WRITE_LT:
8596 case NFS4_WRITEW_LT:
8597 file_lock->c.flc_type = F_WRLCK;
8598 break;
8599 default:
8600 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
8601 status = nfserr_inval;
8602 goto out;
8603 }
8604
8605 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
8606 if (lo)
8607 file_lock->c.flc_owner = (fl_owner_t)lo;
8608 file_lock->c.flc_pid = current->tgid;
8609 file_lock->c.flc_flags = FL_POSIX;
8610
8611 file_lock->fl_start = lockt->lt_offset;
8612 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
8613
8614 nfs4_transform_lock_offset(file_lock);
8615
8616 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
8617 if (status)
8618 goto out;
8619
8620 if (file_lock->c.flc_type != F_UNLCK) {
8621 status = nfserr_denied;
8622 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
8623 }
8624 out:
8625 if (lo)
8626 nfs4_put_stateowner(&lo->lo_owner);
8627 if (file_lock)
8628 locks_free_lock(file_lock);
8629 return status;
8630 }
8631
nfsd4_lockt_release(union nfsd4_op_u * u)8632 void nfsd4_lockt_release(union nfsd4_op_u *u)
8633 {
8634 struct nfsd4_lockt *lockt = &u->lockt;
8635 struct nfsd4_lock_denied *deny = &lockt->lt_denied;
8636
8637 kfree(deny->ld_owner.data);
8638 }
8639
8640 __be32
nfsd4_locku(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8641 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
8642 union nfsd4_op_u *u)
8643 {
8644 struct nfsd4_locku *locku = &u->locku;
8645 struct nfs4_ol_stateid *stp;
8646 struct nfsd_file *nf = NULL;
8647 struct file_lock *file_lock = NULL;
8648 __be32 status;
8649 int err;
8650 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8651
8652 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
8653 (long long) locku->lu_offset,
8654 (long long) locku->lu_length);
8655
8656 if (check_lock_length(locku->lu_offset, locku->lu_length))
8657 return nfserr_inval;
8658
8659 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
8660 &locku->lu_stateid, SC_TYPE_LOCK, 0,
8661 &stp, nn);
8662 if (status)
8663 goto out;
8664 nf = find_any_file(stp->st_stid.sc_file);
8665 if (!nf) {
8666 status = nfserr_lock_range;
8667 goto put_stateid;
8668 }
8669 if (exportfs_cannot_lock(nf->nf_file->f_path.mnt->mnt_sb->s_export_op)) {
8670 status = nfserr_notsupp;
8671 goto put_file;
8672 }
8673
8674 file_lock = locks_alloc_lock();
8675 if (!file_lock) {
8676 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8677 status = nfserr_jukebox;
8678 goto put_file;
8679 }
8680
8681 file_lock->c.flc_type = F_UNLCK;
8682 file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
8683 file_lock->c.flc_pid = current->tgid;
8684 file_lock->c.flc_file = nf->nf_file;
8685 file_lock->c.flc_flags = FL_POSIX;
8686 file_lock->fl_lmops = &nfsd_posix_mng_ops;
8687 file_lock->fl_start = locku->lu_offset;
8688
8689 file_lock->fl_end = last_byte_offset(locku->lu_offset,
8690 locku->lu_length);
8691 nfs4_transform_lock_offset(file_lock);
8692
8693 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
8694 if (err) {
8695 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
8696 goto out_nfserr;
8697 }
8698 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
8699 put_file:
8700 nfsd_file_put(nf);
8701 put_stateid:
8702 mutex_unlock(&stp->st_mutex);
8703 nfs4_put_stid(&stp->st_stid);
8704 out:
8705 nfsd4_bump_seqid(cstate, status);
8706 if (file_lock)
8707 locks_free_lock(file_lock);
8708 return status;
8709
8710 out_nfserr:
8711 status = nfserrno(err);
8712 goto put_file;
8713 }
8714
8715 /*
8716 * returns
8717 * true: locks held by lockowner
8718 * false: no locks held by lockowner
8719 */
8720 static bool
check_for_locks(struct nfs4_file * fp,struct nfs4_lockowner * lowner)8721 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
8722 {
8723 struct file_lock *fl;
8724 int status = false;
8725 struct nfsd_file *nf;
8726 struct inode *inode;
8727 struct file_lock_context *flctx;
8728
8729 spin_lock(&fp->fi_lock);
8730 nf = find_any_file_locked(fp);
8731 if (!nf) {
8732 /* Any valid lock stateid should have some sort of access */
8733 WARN_ON_ONCE(1);
8734 goto out;
8735 }
8736
8737 inode = file_inode(nf->nf_file);
8738 flctx = locks_inode_context(inode);
8739
8740 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
8741 spin_lock(&flctx->flc_lock);
8742 for_each_file_lock(fl, &flctx->flc_posix) {
8743 if (fl->c.flc_owner == (fl_owner_t)lowner) {
8744 status = true;
8745 break;
8746 }
8747 }
8748 spin_unlock(&flctx->flc_lock);
8749 }
8750 out:
8751 spin_unlock(&fp->fi_lock);
8752 return status;
8753 }
8754
8755 /**
8756 * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
8757 * @rqstp: RPC transaction
8758 * @cstate: NFSv4 COMPOUND state
8759 * @u: RELEASE_LOCKOWNER arguments
8760 *
8761 * Check if there are any locks still held and if not, free the lockowner
8762 * and any lock state that is owned.
8763 *
8764 * Return values:
8765 * %nfs_ok: lockowner released or not found
8766 * %nfserr_locks_held: lockowner still in use
8767 * %nfserr_stale_clientid: clientid no longer active
8768 * %nfserr_expired: clientid not recognized
8769 */
8770 __be32
nfsd4_release_lockowner(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8771 nfsd4_release_lockowner(struct svc_rqst *rqstp,
8772 struct nfsd4_compound_state *cstate,
8773 union nfsd4_op_u *u)
8774 {
8775 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
8776 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8777 clientid_t *clid = &rlockowner->rl_clientid;
8778 struct nfs4_ol_stateid *stp;
8779 struct nfs4_lockowner *lo;
8780 struct nfs4_client *clp;
8781 LIST_HEAD(reaplist);
8782 __be32 status;
8783
8784 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
8785 clid->cl_boot, clid->cl_id);
8786
8787 status = set_client(clid, cstate, nn);
8788 if (status)
8789 return status;
8790 clp = cstate->clp;
8791
8792 spin_lock(&clp->cl_lock);
8793 lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
8794 if (!lo) {
8795 spin_unlock(&clp->cl_lock);
8796 return nfs_ok;
8797 }
8798
8799 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
8800 if (check_for_locks(stp->st_stid.sc_file, lo)) {
8801 spin_unlock(&clp->cl_lock);
8802 nfs4_put_stateowner(&lo->lo_owner);
8803 return nfserr_locks_held;
8804 }
8805 }
8806 unhash_lockowner_locked(lo);
8807 while (!list_empty(&lo->lo_owner.so_stateids)) {
8808 stp = list_first_entry(&lo->lo_owner.so_stateids,
8809 struct nfs4_ol_stateid,
8810 st_perstateowner);
8811 unhash_lock_stateid(stp);
8812 put_ol_stateid_locked(stp, &reaplist);
8813 }
8814 spin_unlock(&clp->cl_lock);
8815
8816 free_ol_stateid_reaplist(&reaplist);
8817 remove_blocked_locks(lo);
8818 nfs4_put_stateowner(&lo->lo_owner);
8819 return nfs_ok;
8820 }
8821
8822 static inline struct nfs4_client_reclaim *
alloc_reclaim(void)8823 alloc_reclaim(void)
8824 {
8825 return kmalloc_obj(struct nfs4_client_reclaim);
8826 }
8827
8828 bool
nfs4_has_reclaimed_state(struct xdr_netobj name,struct nfsd_net * nn)8829 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
8830 {
8831 struct nfs4_client_reclaim *crp;
8832
8833 crp = nfsd4_find_reclaim_client(name, nn);
8834 return (crp && crp->cr_clp);
8835 }
8836
8837 /*
8838 * failure => all reset bets are off, nfserr_no_grace...
8839 */
8840 struct nfs4_client_reclaim *
nfs4_client_to_reclaim(struct xdr_netobj name,struct xdr_netobj princhash,struct nfsd_net * nn)8841 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
8842 struct nfsd_net *nn)
8843 {
8844 unsigned int strhashval;
8845 struct nfs4_client_reclaim *crp;
8846
8847 name.data = kmemdup(name.data, name.len, GFP_KERNEL);
8848 if (!name.data) {
8849 dprintk("%s: failed to allocate memory for name.data!\n",
8850 __func__);
8851 return NULL;
8852 }
8853 if (princhash.len) {
8854 princhash.data = kmemdup(princhash.data, princhash.len, GFP_KERNEL);
8855 if (!princhash.data) {
8856 dprintk("%s: failed to allocate memory for princhash.data!\n",
8857 __func__);
8858 kfree(name.data);
8859 return NULL;
8860 }
8861 } else
8862 princhash.data = NULL;
8863 crp = alloc_reclaim();
8864 if (crp) {
8865 strhashval = clientstr_hashval(name);
8866 INIT_LIST_HEAD(&crp->cr_strhash);
8867 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
8868 crp->cr_name.data = name.data;
8869 crp->cr_name.len = name.len;
8870 crp->cr_princhash.data = princhash.data;
8871 crp->cr_princhash.len = princhash.len;
8872 crp->cr_clp = NULL;
8873 nn->reclaim_str_hashtbl_size++;
8874 } else {
8875 kfree(name.data);
8876 kfree(princhash.data);
8877 }
8878 return crp;
8879 }
8880
8881 void
nfs4_remove_reclaim_record(struct nfs4_client_reclaim * crp,struct nfsd_net * nn)8882 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
8883 {
8884 list_del(&crp->cr_strhash);
8885 kfree(crp->cr_name.data);
8886 kfree(crp->cr_princhash.data);
8887 kfree(crp);
8888 nn->reclaim_str_hashtbl_size--;
8889 }
8890
8891 void
nfs4_release_reclaim(struct nfsd_net * nn)8892 nfs4_release_reclaim(struct nfsd_net *nn)
8893 {
8894 struct nfs4_client_reclaim *crp = NULL;
8895 int i;
8896
8897 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8898 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
8899 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
8900 struct nfs4_client_reclaim, cr_strhash);
8901 nfs4_remove_reclaim_record(crp, nn);
8902 }
8903 }
8904 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
8905 }
8906
8907 /*
8908 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
8909 struct nfs4_client_reclaim *
nfsd4_find_reclaim_client(struct xdr_netobj name,struct nfsd_net * nn)8910 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
8911 {
8912 unsigned int strhashval;
8913 struct nfs4_client_reclaim *crp = NULL;
8914
8915 strhashval = clientstr_hashval(name);
8916 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
8917 if (compare_blob(&crp->cr_name, &name) == 0) {
8918 return crp;
8919 }
8920 }
8921 return NULL;
8922 }
8923
8924 __be32
nfs4_check_open_reclaim(struct nfs4_client * clp)8925 nfs4_check_open_reclaim(struct nfs4_client *clp)
8926 {
8927 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
8928 return nfserr_no_grace;
8929
8930 if (nfsd4_client_record_check(clp))
8931 return nfserr_reclaim_bad;
8932
8933 return nfs_ok;
8934 }
8935
8936 /*
8937 * Since the lifetime of a delegation isn't limited to that of an open, a
8938 * client may quite reasonably hang on to a delegation as long as it has
8939 * the inode cached. This becomes an obvious problem the first time a
8940 * client's inode cache approaches the size of the server's total memory.
8941 *
8942 * For now we avoid this problem by imposing a hard limit on the number
8943 * of delegations, which varies according to the server's memory size.
8944 */
8945 static void
set_max_delegations(void)8946 set_max_delegations(void)
8947 {
8948 /*
8949 * Allow at most 4 delegations per megabyte of RAM. Quick
8950 * estimates suggest that in the worst case (where every delegation
8951 * is for a different inode), a delegation could take about 1.5K,
8952 * giving a worst case usage of about 6% of memory.
8953 */
8954 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
8955 }
8956
nfs4_state_create_net(struct net * net)8957 static int nfs4_state_create_net(struct net *net)
8958 {
8959 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8960 int i;
8961
8962 nn->conf_id_hashtbl = kmalloc_objs(struct list_head, CLIENT_HASH_SIZE);
8963 if (!nn->conf_id_hashtbl)
8964 goto err;
8965 nn->unconf_id_hashtbl = kmalloc_objs(struct list_head, CLIENT_HASH_SIZE);
8966 if (!nn->unconf_id_hashtbl)
8967 goto err_unconf_id;
8968 nn->sessionid_hashtbl = kmalloc_objs(struct list_head,
8969 SESSION_HASH_SIZE);
8970 if (!nn->sessionid_hashtbl)
8971 goto err_sessionid;
8972
8973 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8974 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
8975 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
8976 }
8977 for (i = 0; i < SESSION_HASH_SIZE; i++)
8978 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
8979 nn->conf_name_tree = RB_ROOT;
8980 nn->unconf_name_tree = RB_ROOT;
8981 nn->boot_time = ktime_get_real_seconds();
8982 nn->grace_ended = false;
8983 nn->grace_end_forced = false;
8984 nn->nfsd4_manager.block_opens = true;
8985 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
8986 INIT_LIST_HEAD(&nn->client_lru);
8987 INIT_LIST_HEAD(&nn->close_lru);
8988 INIT_LIST_HEAD(&nn->del_recall_lru);
8989 spin_lock_init(&nn->client_lock);
8990 spin_lock_init(&nn->s2s_cp_lock);
8991 idr_init(&nn->s2s_cp_stateids);
8992 atomic_set(&nn->pending_async_copies, 0);
8993
8994 spin_lock_init(&nn->blocked_locks_lock);
8995 INIT_LIST_HEAD(&nn->blocked_locks_lru);
8996
8997 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
8998 /* Make sure this cannot run until client tracking is initialised */
8999 disable_delayed_work(&nn->laundromat_work);
9000 INIT_WORK(&nn->nfsd_shrinker_work, nfsd4_state_shrinker_worker);
9001 get_net(net);
9002
9003 nn->nfsd_client_shrinker = shrinker_alloc(0, "nfsd-client");
9004 if (!nn->nfsd_client_shrinker)
9005 goto err_shrinker;
9006
9007 nn->nfsd_client_shrinker->scan_objects = nfsd4_state_shrinker_scan;
9008 nn->nfsd_client_shrinker->count_objects = nfsd4_state_shrinker_count;
9009 nn->nfsd_client_shrinker->private_data = nn;
9010
9011 shrinker_register(nn->nfsd_client_shrinker);
9012
9013 return 0;
9014
9015 err_shrinker:
9016 put_net(net);
9017 kfree(nn->sessionid_hashtbl);
9018 err_sessionid:
9019 kfree(nn->unconf_id_hashtbl);
9020 err_unconf_id:
9021 kfree(nn->conf_id_hashtbl);
9022 err:
9023 return -ENOMEM;
9024 }
9025
9026 static void
nfs4_state_destroy_net(struct net * net)9027 nfs4_state_destroy_net(struct net *net)
9028 {
9029 int i;
9030 struct nfs4_client *clp = NULL;
9031 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9032
9033 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
9034 while (!list_empty(&nn->conf_id_hashtbl[i])) {
9035 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
9036 destroy_client(clp);
9037 }
9038 }
9039
9040 WARN_ON(!list_empty(&nn->blocked_locks_lru));
9041
9042 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
9043 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
9044 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
9045 destroy_client(clp);
9046 }
9047 }
9048
9049 kfree(nn->sessionid_hashtbl);
9050 kfree(nn->unconf_id_hashtbl);
9051 kfree(nn->conf_id_hashtbl);
9052 put_net(net);
9053 }
9054
9055 int
nfs4_state_start_net(struct net * net)9056 nfs4_state_start_net(struct net *net)
9057 {
9058 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9059 int ret;
9060
9061 ret = nfs4_state_create_net(net);
9062 if (ret)
9063 return ret;
9064 locks_start_grace(net, &nn->nfsd4_manager);
9065 nfsd4_client_tracking_init(net);
9066 /* safe for laundromat to run now */
9067 enable_delayed_work(&nn->laundromat_work);
9068 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
9069 goto skip_grace;
9070 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
9071 nn->nfsd4_grace, net->ns.inum);
9072 trace_nfsd_grace_start(nn);
9073 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
9074 return 0;
9075
9076 skip_grace:
9077 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
9078 net->ns.inum);
9079 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
9080 nfsd4_end_grace(nn);
9081 return 0;
9082 }
9083
9084 /* initialization to perform when the nfsd service is started: */
9085 int
nfs4_state_start(void)9086 nfs4_state_start(void)
9087 {
9088 int ret;
9089
9090 ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params);
9091 if (ret)
9092 return ret;
9093
9094 nfsd_slot_shrinker = shrinker_alloc(0, "nfsd-DRC-slot");
9095 if (!nfsd_slot_shrinker) {
9096 rhltable_destroy(&nfs4_file_rhltable);
9097 return -ENOMEM;
9098 }
9099 nfsd_slot_shrinker->count_objects = nfsd_slot_count;
9100 nfsd_slot_shrinker->scan_objects = nfsd_slot_scan;
9101 shrinker_register(nfsd_slot_shrinker);
9102
9103 set_max_delegations();
9104 return 0;
9105 }
9106
9107 void
nfs4_state_shutdown_net(struct net * net)9108 nfs4_state_shutdown_net(struct net *net)
9109 {
9110 struct nfs4_delegation *dp = NULL;
9111 struct list_head *pos, *next, reaplist;
9112 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9113
9114 shrinker_free(nn->nfsd_client_shrinker);
9115 cancel_work_sync(&nn->nfsd_shrinker_work);
9116 disable_delayed_work_sync(&nn->laundromat_work);
9117 locks_end_grace(&nn->nfsd4_manager);
9118
9119 INIT_LIST_HEAD(&reaplist);
9120 spin_lock(&state_lock);
9121 list_for_each_safe(pos, next, &nn->del_recall_lru) {
9122 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
9123 unhash_delegation_locked(dp, SC_STATUS_CLOSED);
9124 list_add(&dp->dl_recall_lru, &reaplist);
9125 }
9126 spin_unlock(&state_lock);
9127 list_for_each_safe(pos, next, &reaplist) {
9128 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
9129 list_del_init(&dp->dl_recall_lru);
9130 destroy_unhashed_deleg(dp);
9131 }
9132
9133 nfsd4_client_tracking_exit(net);
9134 nfs4_state_destroy_net(net);
9135 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
9136 nfsd4_ssc_shutdown_umount(nn);
9137 #endif
9138 }
9139
9140 void
nfs4_state_shutdown(void)9141 nfs4_state_shutdown(void)
9142 {
9143 rhltable_destroy(&nfs4_file_rhltable);
9144 shrinker_free(nfsd_slot_shrinker);
9145 }
9146
9147 static void
get_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid)9148 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
9149 {
9150 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
9151 CURRENT_STATEID(stateid))
9152 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
9153 }
9154
9155 static void
put_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid)9156 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
9157 {
9158 if (cstate->minorversion) {
9159 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
9160 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
9161 }
9162 }
9163
9164 void
clear_current_stateid(struct nfsd4_compound_state * cstate)9165 clear_current_stateid(struct nfsd4_compound_state *cstate)
9166 {
9167 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
9168 }
9169
9170 /*
9171 * functions to set current state id
9172 */
9173 void
nfsd4_set_opendowngradestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9174 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
9175 union nfsd4_op_u *u)
9176 {
9177 put_stateid(cstate, &u->open_downgrade.od_stateid);
9178 }
9179
9180 void
nfsd4_set_openstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9181 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
9182 union nfsd4_op_u *u)
9183 {
9184 put_stateid(cstate, &u->open.op_stateid);
9185 }
9186
9187 void
nfsd4_set_closestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9188 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
9189 union nfsd4_op_u *u)
9190 {
9191 put_stateid(cstate, &u->close.cl_stateid);
9192 }
9193
9194 void
nfsd4_set_lockstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9195 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
9196 union nfsd4_op_u *u)
9197 {
9198 put_stateid(cstate, &u->lock.lk_resp_stateid);
9199 }
9200
9201 /*
9202 * functions to consume current state id
9203 */
9204
9205 void
nfsd4_get_opendowngradestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9206 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
9207 union nfsd4_op_u *u)
9208 {
9209 get_stateid(cstate, &u->open_downgrade.od_stateid);
9210 }
9211
9212 void
nfsd4_get_delegreturnstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9213 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
9214 union nfsd4_op_u *u)
9215 {
9216 get_stateid(cstate, &u->delegreturn.dr_stateid);
9217 }
9218
9219 void
nfsd4_get_freestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9220 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
9221 union nfsd4_op_u *u)
9222 {
9223 get_stateid(cstate, &u->free_stateid.fr_stateid);
9224 }
9225
9226 void
nfsd4_get_setattrstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9227 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
9228 union nfsd4_op_u *u)
9229 {
9230 get_stateid(cstate, &u->setattr.sa_stateid);
9231 }
9232
9233 void
nfsd4_get_closestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9234 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
9235 union nfsd4_op_u *u)
9236 {
9237 get_stateid(cstate, &u->close.cl_stateid);
9238 }
9239
9240 void
nfsd4_get_lockustateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9241 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
9242 union nfsd4_op_u *u)
9243 {
9244 get_stateid(cstate, &u->locku.lu_stateid);
9245 }
9246
9247 void
nfsd4_get_readstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9248 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
9249 union nfsd4_op_u *u)
9250 {
9251 get_stateid(cstate, &u->read.rd_stateid);
9252 }
9253
9254 void
nfsd4_get_writestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9255 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
9256 union nfsd4_op_u *u)
9257 {
9258 get_stateid(cstate, &u->write.wr_stateid);
9259 }
9260
9261 /**
9262 * nfsd4_vet_deleg_time - vet and set the timespec for a delegated timestamp update
9263 * @req: timestamp from the client
9264 * @orig: original timestamp in the inode
9265 * @now: current time
9266 *
9267 * Given a timestamp from the client response, check it against the
9268 * current timestamp in the inode and the current time. Returns true
9269 * if the inode's timestamp needs to be updated, and false otherwise.
9270 * @req may also be changed if the timestamp needs to be clamped.
9271 */
nfsd4_vet_deleg_time(struct timespec64 * req,const struct timespec64 * orig,const struct timespec64 * now)9272 bool nfsd4_vet_deleg_time(struct timespec64 *req, const struct timespec64 *orig,
9273 const struct timespec64 *now)
9274 {
9275
9276 /*
9277 * "When the time presented is before the original time, then the
9278 * update is ignored." Also no need to update if there is no change.
9279 */
9280 if (timespec64_compare(req, orig) <= 0)
9281 return false;
9282
9283 /*
9284 * "When the time presented is in the future, the server can either
9285 * clamp the new time to the current time, or it may
9286 * return NFS4ERR_DELAY to the client, allowing it to retry."
9287 */
9288 if (timespec64_compare(req, now) > 0)
9289 *req = *now;
9290
9291 return true;
9292 }
9293
cb_getattr_update_times(struct dentry * dentry,struct nfs4_delegation * dp)9294 static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation *dp)
9295 {
9296 struct inode *inode = d_inode(dentry);
9297 struct nfs4_cb_fattr *ncf = &dp->dl_cb_fattr;
9298 struct iattr attrs = { };
9299 int ret;
9300
9301 if (deleg_attrs_deleg(dp->dl_type)) {
9302 struct timespec64 now = current_time(inode);
9303
9304 attrs.ia_atime = ncf->ncf_cb_atime;
9305 attrs.ia_mtime = ncf->ncf_cb_mtime;
9306
9307 if (nfsd4_vet_deleg_time(&attrs.ia_atime, &dp->dl_atime, &now))
9308 attrs.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
9309
9310 if (nfsd4_vet_deleg_time(&attrs.ia_mtime, &dp->dl_mtime, &now)) {
9311 attrs.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
9312 attrs.ia_ctime = attrs.ia_mtime;
9313 if (nfsd4_vet_deleg_time(&attrs.ia_ctime, &dp->dl_ctime, &now))
9314 attrs.ia_valid |= ATTR_CTIME | ATTR_CTIME_SET;
9315 }
9316 } else {
9317 attrs.ia_valid |= ATTR_MTIME | ATTR_CTIME;
9318 }
9319
9320 if (!attrs.ia_valid)
9321 return 0;
9322
9323 attrs.ia_valid |= ATTR_DELEG;
9324 inode_lock(inode);
9325 ret = notify_change(&nop_mnt_idmap, dentry, &attrs, NULL);
9326 inode_unlock(inode);
9327 return ret;
9328 }
9329
9330 /**
9331 * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict
9332 * @rqstp: RPC transaction context
9333 * @dentry: dentry of inode to be checked for a conflict
9334 * @pdp: returned WRITE delegation, if one was found
9335 *
9336 * This function is called when there is a conflict between a write
9337 * delegation and a change/size GETATTR from another client. The server
9338 * must either use the CB_GETATTR to get the current values of the
9339 * attributes from the client that holds the delegation or recall the
9340 * delegation before replying to the GETATTR. See RFC 8881 section
9341 * 18.7.4.
9342 *
9343 * Returns 0 if there is no conflict; otherwise an nfs_stat
9344 * code is returned. If @pdp is set to a non-NULL value, then the
9345 * caller must put the reference.
9346 */
9347 __be32
nfsd4_deleg_getattr_conflict(struct svc_rqst * rqstp,struct dentry * dentry,struct nfs4_delegation ** pdp)9348 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry,
9349 struct nfs4_delegation **pdp)
9350 {
9351 __be32 status;
9352 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
9353 struct file_lock_context *ctx;
9354 struct nfs4_delegation *dp = NULL;
9355 struct file_lease *fl;
9356 struct nfs4_cb_fattr *ncf;
9357 struct inode *inode = d_inode(dentry);
9358
9359 ctx = locks_inode_context(inode);
9360 if (!ctx)
9361 return nfs_ok;
9362
9363 #define NON_NFSD_LEASE ((void *)1)
9364
9365 spin_lock(&ctx->flc_lock);
9366 for_each_file_lock(fl, &ctx->flc_lease) {
9367 if (fl->c.flc_flags == FL_LAYOUT)
9368 continue;
9369 if (fl->c.flc_type == F_WRLCK) {
9370 if (fl->fl_lmops == &nfsd_lease_mng_ops)
9371 dp = fl->c.flc_owner;
9372 else
9373 dp = NON_NFSD_LEASE;
9374 }
9375 break;
9376 }
9377 if (dp == NULL || dp == NON_NFSD_LEASE ||
9378 dp->dl_recall.cb_clp == *(rqstp->rq_lease_breaker)) {
9379 spin_unlock(&ctx->flc_lock);
9380 if (dp == NON_NFSD_LEASE) {
9381 status = nfserrno(nfsd_open_break_lease(inode,
9382 NFSD_MAY_READ));
9383 if (status != nfserr_jukebox ||
9384 !nfsd_wait_for_delegreturn(rqstp, inode))
9385 return status;
9386 }
9387 return 0;
9388 }
9389
9390 nfsd_stats_wdeleg_getattr_inc(nn);
9391 refcount_inc(&dp->dl_stid.sc_count);
9392 ncf = &dp->dl_cb_fattr;
9393 nfs4_cb_getattr(&dp->dl_cb_fattr);
9394 spin_unlock(&ctx->flc_lock);
9395
9396 wait_on_bit_timeout(&ncf->ncf_getattr.cb_flags, NFSD4_CALLBACK_RUNNING,
9397 TASK_UNINTERRUPTIBLE, NFSD_CB_GETATTR_TIMEOUT);
9398 if (ncf->ncf_cb_status) {
9399 /* Recall delegation only if client didn't respond */
9400 status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
9401 if (status != nfserr_jukebox ||
9402 !nfsd_wait_for_delegreturn(rqstp, inode))
9403 goto out_status;
9404 }
9405 if (!ncf->ncf_file_modified &&
9406 (ncf->ncf_initial_cinfo != ncf->ncf_cb_change ||
9407 ncf->ncf_cur_fsize != ncf->ncf_cb_fsize))
9408 ncf->ncf_file_modified = true;
9409 if (ncf->ncf_file_modified) {
9410 int err;
9411
9412 /*
9413 * Per section 10.4.3 of RFC 8881, the server would
9414 * not update the file's metadata with the client's
9415 * modified size
9416 */
9417 err = cb_getattr_update_times(dentry, dp);
9418 if (err) {
9419 status = nfserrno(err);
9420 goto out_status;
9421 }
9422 ncf->ncf_cur_fsize = ncf->ncf_cb_fsize;
9423 *pdp = dp;
9424 return nfs_ok;
9425 }
9426 status = nfs_ok;
9427 out_status:
9428 nfs4_put_stid(&dp->dl_stid);
9429 return status;
9430 }
9431
9432 /**
9433 * nfsd_get_dir_deleg - attempt to get a directory delegation
9434 * @cstate: compound state
9435 * @gdd: GET_DIR_DELEGATION arg/resp structure
9436 * @nf: nfsd_file opened on the directory
9437 *
9438 * Given a GET_DIR_DELEGATION request @gdd, attempt to acquire a delegation
9439 * on the directory to which @nf refers. Note that this does not set up any
9440 * sort of async notifications for the delegation.
9441 */
9442 struct nfs4_delegation *
nfsd_get_dir_deleg(struct nfsd4_compound_state * cstate,struct nfsd4_get_dir_delegation * gdd,struct nfsd_file * nf)9443 nfsd_get_dir_deleg(struct nfsd4_compound_state *cstate,
9444 struct nfsd4_get_dir_delegation *gdd,
9445 struct nfsd_file *nf)
9446 {
9447 struct nfs4_client *clp = cstate->clp;
9448 struct nfs4_delegation *dp;
9449 struct file_lease *fl;
9450 struct nfs4_file *fp, *rfp;
9451 int status = 0;
9452
9453 fp = nfsd4_alloc_file();
9454 if (!fp)
9455 return ERR_PTR(-ENOMEM);
9456
9457 nfsd4_file_init(&cstate->current_fh, fp);
9458
9459 rfp = nfsd4_file_hash_insert(fp, &cstate->current_fh);
9460 if (unlikely(!rfp)) {
9461 put_nfs4_file(fp);
9462 return ERR_PTR(-ENOMEM);
9463 }
9464
9465 if (rfp != fp) {
9466 put_nfs4_file(fp);
9467 fp = rfp;
9468 }
9469
9470 /* if this client already has one, return that it's unavailable */
9471 spin_lock(&state_lock);
9472 spin_lock(&fp->fi_lock);
9473 /* existing delegation? */
9474 if (nfs4_delegation_exists(clp, fp)) {
9475 status = -EAGAIN;
9476 } else if (!fp->fi_deleg_file) {
9477 fp->fi_deleg_file = nfsd_file_get(nf);
9478 fp->fi_delegees = 1;
9479 } else {
9480 ++fp->fi_delegees;
9481 }
9482 spin_unlock(&fp->fi_lock);
9483 spin_unlock(&state_lock);
9484
9485 if (status) {
9486 put_nfs4_file(fp);
9487 return ERR_PTR(status);
9488 }
9489
9490 /* Try to set up the lease */
9491 status = -ENOMEM;
9492 dp = alloc_init_deleg(clp, fp, NULL, NFS4_OPEN_DELEGATE_READ);
9493 if (!dp)
9494 goto out_delegees;
9495
9496 fl = nfs4_alloc_init_lease(dp);
9497 if (!fl)
9498 goto out_put_stid;
9499
9500 status = kernel_setlease(nf->nf_file,
9501 fl->c.flc_type, &fl, NULL);
9502 if (fl)
9503 locks_free_lease(fl);
9504 if (status)
9505 goto out_put_stid;
9506
9507 /*
9508 * Now, try to hash it. This can fail if we race another nfsd task
9509 * trying to set a delegation on the same file. If that happens,
9510 * then just say UNAVAIL.
9511 */
9512 spin_lock(&state_lock);
9513 spin_lock(&clp->cl_lock);
9514 spin_lock(&fp->fi_lock);
9515 status = hash_delegation_locked(dp, fp);
9516 spin_unlock(&fp->fi_lock);
9517 spin_unlock(&clp->cl_lock);
9518 spin_unlock(&state_lock);
9519
9520 if (!status) {
9521 put_nfs4_file(fp);
9522 return dp;
9523 }
9524
9525 /* Something failed. Drop the lease and clean up the stid */
9526 kernel_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
9527 out_put_stid:
9528 nfs4_put_stid(&dp->dl_stid);
9529 out_delegees:
9530 put_deleg_file(fp);
9531 put_nfs4_file(fp);
9532 return ERR_PTR(status);
9533 }
9534