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 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(sizeof(*nbl), GFP_KERNEL);
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(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
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 nfs4_file_put_access(fp, NFS4_SHARE_ACCESS_READ);
1223 }
1224
nfsd4_finalize_deleg_timestamps(struct nfs4_delegation * dp,struct file * f)1225 static void nfsd4_finalize_deleg_timestamps(struct nfs4_delegation *dp, struct file *f)
1226 {
1227 struct iattr ia = { .ia_valid = ATTR_ATIME | ATTR_CTIME | ATTR_MTIME };
1228 struct inode *inode = file_inode(f);
1229 int ret;
1230
1231 /* don't do anything if FMODE_NOCMTIME isn't set */
1232 if ((READ_ONCE(f->f_mode) & FMODE_NOCMTIME) == 0)
1233 return;
1234
1235 spin_lock(&f->f_lock);
1236 f->f_mode &= ~FMODE_NOCMTIME;
1237 spin_unlock(&f->f_lock);
1238
1239 /* was it never written? */
1240 if (!dp->dl_written)
1241 return;
1242
1243 /* did it get a setattr for the timestamps at some point? */
1244 if (dp->dl_setattr)
1245 return;
1246
1247 /* Stamp everything to "now" */
1248 inode_lock(inode);
1249 ret = notify_change(&nop_mnt_idmap, f->f_path.dentry, &ia, NULL);
1250 inode_unlock(inode);
1251 if (ret) {
1252 struct inode *inode = file_inode(f);
1253
1254 pr_notice_ratelimited("Unable to update timestamps on inode %02x:%02x:%lu: %d\n",
1255 MAJOR(inode->i_sb->s_dev),
1256 MINOR(inode->i_sb->s_dev),
1257 inode->i_ino, ret);
1258 }
1259 }
1260
nfs4_unlock_deleg_lease(struct nfs4_delegation * dp)1261 static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1262 {
1263 struct nfs4_file *fp = dp->dl_stid.sc_file;
1264 struct nfsd_file *nf = fp->fi_deleg_file;
1265
1266 WARN_ON_ONCE(!fp->fi_delegees);
1267
1268 nfsd4_finalize_deleg_timestamps(dp, nf->nf_file);
1269 kernel_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1270 put_deleg_file(fp);
1271 }
1272
destroy_unhashed_deleg(struct nfs4_delegation * dp)1273 static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1274 {
1275 put_clnt_odstate(dp->dl_clnt_odstate);
1276 nfs4_unlock_deleg_lease(dp);
1277 nfs4_put_stid(&dp->dl_stid);
1278 }
1279
1280 /**
1281 * nfs4_delegation_exists - Discover if this delegation already exists
1282 * @clp: a pointer to the nfs4_client we're granting a delegation to
1283 * @fp: a pointer to the nfs4_file we're granting a delegation on
1284 *
1285 * Return:
1286 * On success: true iff an existing delegation is found
1287 */
1288
1289 static bool
nfs4_delegation_exists(struct nfs4_client * clp,struct nfs4_file * fp)1290 nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1291 {
1292 struct nfs4_delegation *searchdp = NULL;
1293 struct nfs4_client *searchclp = NULL;
1294
1295 lockdep_assert_held(&state_lock);
1296 lockdep_assert_held(&fp->fi_lock);
1297
1298 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1299 searchclp = searchdp->dl_stid.sc_client;
1300 if (clp == searchclp) {
1301 return true;
1302 }
1303 }
1304 return false;
1305 }
1306
1307 /**
1308 * hash_delegation_locked - Add a delegation to the appropriate lists
1309 * @dp: a pointer to the nfs4_delegation we are adding.
1310 * @fp: a pointer to the nfs4_file we're granting a delegation on
1311 *
1312 * Return:
1313 * On success: NULL if the delegation was successfully hashed.
1314 *
1315 * On error: -EAGAIN if one was previously granted to this
1316 * nfs4_client for this nfs4_file. Delegation is not hashed.
1317 *
1318 */
1319
1320 static int
hash_delegation_locked(struct nfs4_delegation * dp,struct nfs4_file * fp)1321 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1322 {
1323 struct nfs4_client *clp = dp->dl_stid.sc_client;
1324
1325 lockdep_assert_held(&state_lock);
1326 lockdep_assert_held(&fp->fi_lock);
1327 lockdep_assert_held(&clp->cl_lock);
1328
1329 if (nfs4_delegation_exists(clp, fp))
1330 return -EAGAIN;
1331 refcount_inc(&dp->dl_stid.sc_count);
1332 dp->dl_stid.sc_type = SC_TYPE_DELEG;
1333 list_add(&dp->dl_perfile, &fp->fi_delegations);
1334 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1335 return 0;
1336 }
1337
delegation_hashed(struct nfs4_delegation * dp)1338 static bool delegation_hashed(struct nfs4_delegation *dp)
1339 {
1340 return !(list_empty(&dp->dl_perfile));
1341 }
1342
1343 static bool
unhash_delegation_locked(struct nfs4_delegation * dp,unsigned short statusmask)1344 unhash_delegation_locked(struct nfs4_delegation *dp, unsigned short statusmask)
1345 {
1346 struct nfs4_file *fp = dp->dl_stid.sc_file;
1347
1348 lockdep_assert_held(&state_lock);
1349
1350 if (!delegation_hashed(dp))
1351 return false;
1352
1353 if (statusmask == SC_STATUS_REVOKED &&
1354 dp->dl_stid.sc_client->cl_minorversion == 0)
1355 statusmask = SC_STATUS_CLOSED;
1356 dp->dl_stid.sc_status |= statusmask;
1357 if (statusmask & SC_STATUS_ADMIN_REVOKED)
1358 atomic_inc(&dp->dl_stid.sc_client->cl_admin_revoked);
1359
1360 /* Ensure that deleg break won't try to requeue it */
1361 ++dp->dl_time;
1362 spin_lock(&fp->fi_lock);
1363 list_del_init(&dp->dl_perclnt);
1364 list_del_init(&dp->dl_recall_lru);
1365 list_del_init(&dp->dl_perfile);
1366 spin_unlock(&fp->fi_lock);
1367 return true;
1368 }
1369
destroy_delegation(struct nfs4_delegation * dp)1370 static void destroy_delegation(struct nfs4_delegation *dp)
1371 {
1372 bool unhashed;
1373
1374 spin_lock(&state_lock);
1375 unhashed = unhash_delegation_locked(dp, SC_STATUS_CLOSED);
1376 spin_unlock(&state_lock);
1377 if (unhashed)
1378 destroy_unhashed_deleg(dp);
1379 }
1380
1381 /**
1382 * revoke_delegation - perform nfs4 delegation structure cleanup
1383 * @dp: pointer to the delegation
1384 *
1385 * This function assumes that it's called either from the administrative
1386 * interface (nfsd4_revoke_states()) that's revoking a specific delegation
1387 * stateid or it's called from a laundromat thread (nfsd4_landromat()) that
1388 * determined that this specific state has expired and needs to be revoked
1389 * (both mark state with the appropriate stid sc_status mode). It is also
1390 * assumed that a reference was taken on the @dp state.
1391 *
1392 * If this function finds that the @dp state is SC_STATUS_FREED it means
1393 * that a FREE_STATEID operation for this stateid has been processed and
1394 * we can proceed to removing it from recalled list. However, if @dp state
1395 * isn't marked SC_STATUS_FREED, it means we need place it on the cl_revoked
1396 * list and wait for the FREE_STATEID to arrive from the client. At the same
1397 * time, we need to mark it as SC_STATUS_FREEABLE to indicate to the
1398 * nfsd4_free_stateid() function that this stateid has already been added
1399 * to the cl_revoked list and that nfsd4_free_stateid() is now responsible
1400 * for removing it from the list. Inspection of where the delegation state
1401 * in the revocation process is protected by the clp->cl_lock.
1402 */
revoke_delegation(struct nfs4_delegation * dp)1403 static void revoke_delegation(struct nfs4_delegation *dp)
1404 {
1405 struct nfs4_client *clp = dp->dl_stid.sc_client;
1406
1407 WARN_ON(!list_empty(&dp->dl_recall_lru));
1408 WARN_ON_ONCE(dp->dl_stid.sc_client->cl_minorversion > 0 &&
1409 !(dp->dl_stid.sc_status &
1410 (SC_STATUS_REVOKED | SC_STATUS_ADMIN_REVOKED)));
1411
1412 trace_nfsd_stid_revoke(&dp->dl_stid);
1413
1414 spin_lock(&clp->cl_lock);
1415 if (dp->dl_stid.sc_status & SC_STATUS_FREED) {
1416 list_del_init(&dp->dl_recall_lru);
1417 goto out;
1418 }
1419 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1420 dp->dl_stid.sc_status |= SC_STATUS_FREEABLE;
1421 out:
1422 spin_unlock(&clp->cl_lock);
1423 destroy_unhashed_deleg(dp);
1424 }
1425
1426 /*
1427 * SETCLIENTID state
1428 */
1429
clientid_hashval(u32 id)1430 static unsigned int clientid_hashval(u32 id)
1431 {
1432 return id & CLIENT_HASH_MASK;
1433 }
1434
clientstr_hashval(struct xdr_netobj name)1435 static unsigned int clientstr_hashval(struct xdr_netobj name)
1436 {
1437 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1438 }
1439
1440 /*
1441 * A stateid that had a deny mode associated with it is being released
1442 * or downgraded. Recalculate the deny mode on the file.
1443 */
1444 static void
recalculate_deny_mode(struct nfs4_file * fp)1445 recalculate_deny_mode(struct nfs4_file *fp)
1446 {
1447 struct nfs4_ol_stateid *stp;
1448 u32 old_deny;
1449
1450 spin_lock(&fp->fi_lock);
1451 old_deny = fp->fi_share_deny;
1452 fp->fi_share_deny = 0;
1453 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1454 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1455 if (fp->fi_share_deny == old_deny)
1456 break;
1457 }
1458 spin_unlock(&fp->fi_lock);
1459 }
1460
1461 static void
reset_union_bmap_deny(u32 deny,struct nfs4_ol_stateid * stp)1462 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1463 {
1464 int i;
1465 bool change = false;
1466
1467 for (i = 1; i < 4; i++) {
1468 if ((i & deny) != i) {
1469 change = true;
1470 clear_deny(i, stp);
1471 }
1472 }
1473
1474 /* Recalculate per-file deny mode if there was a change */
1475 if (change)
1476 recalculate_deny_mode(stp->st_stid.sc_file);
1477 }
1478
1479 /* release all access and file references for a given stateid */
1480 static void
release_all_access(struct nfs4_ol_stateid * stp)1481 release_all_access(struct nfs4_ol_stateid *stp)
1482 {
1483 int i;
1484 struct nfs4_file *fp = stp->st_stid.sc_file;
1485
1486 if (fp && stp->st_deny_bmap != 0)
1487 recalculate_deny_mode(fp);
1488
1489 for (i = 1; i < 4; i++) {
1490 if (test_access(i, stp))
1491 nfs4_file_put_access(stp->st_stid.sc_file, i);
1492 clear_access(i, stp);
1493 }
1494 }
1495
nfs4_free_stateowner(struct nfs4_stateowner * sop)1496 static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1497 {
1498 kfree(sop->so_owner.data);
1499 sop->so_ops->so_free(sop);
1500 }
1501
nfs4_put_stateowner(struct nfs4_stateowner * sop)1502 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1503 {
1504 struct nfs4_client *clp = sop->so_client;
1505
1506 might_lock(&clp->cl_lock);
1507
1508 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1509 return;
1510 sop->so_ops->so_unhash(sop);
1511 spin_unlock(&clp->cl_lock);
1512 nfs4_free_stateowner(sop);
1513 }
1514
1515 static bool
nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid * stp)1516 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1517 {
1518 return list_empty(&stp->st_perfile);
1519 }
1520
unhash_ol_stateid(struct nfs4_ol_stateid * stp)1521 static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1522 {
1523 struct nfs4_file *fp = stp->st_stid.sc_file;
1524
1525 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1526
1527 if (list_empty(&stp->st_perfile))
1528 return false;
1529
1530 spin_lock(&fp->fi_lock);
1531 list_del_init(&stp->st_perfile);
1532 spin_unlock(&fp->fi_lock);
1533 list_del(&stp->st_perstateowner);
1534 return true;
1535 }
1536
nfs4_free_ol_stateid(struct nfs4_stid * stid)1537 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1538 {
1539 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1540
1541 put_clnt_odstate(stp->st_clnt_odstate);
1542 release_all_access(stp);
1543 if (stp->st_stateowner)
1544 nfs4_put_stateowner(stp->st_stateowner);
1545 WARN_ON(!list_empty(&stid->sc_cp_list));
1546 kmem_cache_free(stateid_slab, stid);
1547 }
1548
nfs4_free_lock_stateid(struct nfs4_stid * stid)1549 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1550 {
1551 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1552 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1553 struct nfsd_file *nf;
1554
1555 nf = find_any_file(stp->st_stid.sc_file);
1556 if (nf) {
1557 get_file(nf->nf_file);
1558 filp_close(nf->nf_file, (fl_owner_t)lo);
1559 nfsd_file_put(nf);
1560 }
1561 nfs4_free_ol_stateid(stid);
1562 }
1563
1564 /*
1565 * Put the persistent reference to an already unhashed generic stateid, while
1566 * holding the cl_lock. If it's the last reference, then put it onto the
1567 * reaplist for later destruction.
1568 */
put_ol_stateid_locked(struct nfs4_ol_stateid * stp,struct list_head * reaplist)1569 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1570 struct list_head *reaplist)
1571 {
1572 struct nfs4_stid *s = &stp->st_stid;
1573 struct nfs4_client *clp = s->sc_client;
1574
1575 lockdep_assert_held(&clp->cl_lock);
1576
1577 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1578
1579 if (!refcount_dec_and_test(&s->sc_count)) {
1580 wake_up_all(&close_wq);
1581 return;
1582 }
1583
1584 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1585 if (s->sc_status & SC_STATUS_ADMIN_REVOKED)
1586 atomic_dec(&s->sc_client->cl_admin_revoked);
1587 list_add(&stp->st_locks, reaplist);
1588 }
1589
unhash_lock_stateid(struct nfs4_ol_stateid * stp)1590 static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1591 {
1592 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1593
1594 if (!unhash_ol_stateid(stp))
1595 return false;
1596 list_del_init(&stp->st_locks);
1597 stp->st_stid.sc_status |= SC_STATUS_CLOSED;
1598 return true;
1599 }
1600
release_lock_stateid(struct nfs4_ol_stateid * stp)1601 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1602 {
1603 struct nfs4_client *clp = stp->st_stid.sc_client;
1604 bool unhashed;
1605
1606 spin_lock(&clp->cl_lock);
1607 unhashed = unhash_lock_stateid(stp);
1608 spin_unlock(&clp->cl_lock);
1609 if (unhashed)
1610 nfs4_put_stid(&stp->st_stid);
1611 }
1612
unhash_lockowner_locked(struct nfs4_lockowner * lo)1613 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1614 {
1615 struct nfs4_client *clp = lo->lo_owner.so_client;
1616
1617 lockdep_assert_held(&clp->cl_lock);
1618
1619 list_del_init(&lo->lo_owner.so_strhash);
1620 }
1621
1622 /*
1623 * Free a list of generic stateids that were collected earlier after being
1624 * fully unhashed.
1625 */
1626 static void
free_ol_stateid_reaplist(struct list_head * reaplist)1627 free_ol_stateid_reaplist(struct list_head *reaplist)
1628 {
1629 struct nfs4_ol_stateid *stp;
1630 struct nfs4_file *fp;
1631
1632 might_sleep();
1633
1634 while (!list_empty(reaplist)) {
1635 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1636 st_locks);
1637 list_del(&stp->st_locks);
1638 fp = stp->st_stid.sc_file;
1639 stp->st_stid.sc_free(&stp->st_stid);
1640 if (fp)
1641 put_nfs4_file(fp);
1642 }
1643 }
1644
release_open_stateid_locks(struct nfs4_ol_stateid * open_stp,struct list_head * reaplist)1645 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1646 struct list_head *reaplist)
1647 {
1648 struct nfs4_ol_stateid *stp;
1649
1650 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1651
1652 while (!list_empty(&open_stp->st_locks)) {
1653 stp = list_entry(open_stp->st_locks.next,
1654 struct nfs4_ol_stateid, st_locks);
1655 unhash_lock_stateid(stp);
1656 put_ol_stateid_locked(stp, reaplist);
1657 }
1658 }
1659
unhash_open_stateid(struct nfs4_ol_stateid * stp,struct list_head * reaplist)1660 static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1661 struct list_head *reaplist)
1662 {
1663 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1664
1665 if (!unhash_ol_stateid(stp))
1666 return false;
1667 release_open_stateid_locks(stp, reaplist);
1668 return true;
1669 }
1670
release_open_stateid(struct nfs4_ol_stateid * stp)1671 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1672 {
1673 LIST_HEAD(reaplist);
1674
1675 spin_lock(&stp->st_stid.sc_client->cl_lock);
1676 stp->st_stid.sc_status |= SC_STATUS_CLOSED;
1677 if (unhash_open_stateid(stp, &reaplist))
1678 put_ol_stateid_locked(stp, &reaplist);
1679 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1680 free_ol_stateid_reaplist(&reaplist);
1681 }
1682
nfs4_openowner_unhashed(struct nfs4_openowner * oo)1683 static bool nfs4_openowner_unhashed(struct nfs4_openowner *oo)
1684 {
1685 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1686
1687 return list_empty(&oo->oo_owner.so_strhash) &&
1688 list_empty(&oo->oo_perclient);
1689 }
1690
unhash_openowner_locked(struct nfs4_openowner * oo)1691 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1692 {
1693 struct nfs4_client *clp = oo->oo_owner.so_client;
1694
1695 lockdep_assert_held(&clp->cl_lock);
1696
1697 list_del_init(&oo->oo_owner.so_strhash);
1698 list_del_init(&oo->oo_perclient);
1699 }
1700
release_last_closed_stateid(struct nfs4_openowner * oo)1701 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1702 {
1703 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1704 nfsd_net_id);
1705 struct nfs4_ol_stateid *s;
1706
1707 spin_lock(&nn->client_lock);
1708 s = oo->oo_last_closed_stid;
1709 if (s) {
1710 list_del_init(&oo->oo_close_lru);
1711 oo->oo_last_closed_stid = NULL;
1712 }
1713 spin_unlock(&nn->client_lock);
1714 if (s)
1715 nfs4_put_stid(&s->st_stid);
1716 }
1717
release_openowner(struct nfs4_openowner * oo)1718 static void release_openowner(struct nfs4_openowner *oo)
1719 {
1720 struct nfs4_ol_stateid *stp;
1721 struct nfs4_client *clp = oo->oo_owner.so_client;
1722 LIST_HEAD(reaplist);
1723
1724 spin_lock(&clp->cl_lock);
1725 unhash_openowner_locked(oo);
1726 while (!list_empty(&oo->oo_owner.so_stateids)) {
1727 stp = list_first_entry(&oo->oo_owner.so_stateids,
1728 struct nfs4_ol_stateid, st_perstateowner);
1729 if (unhash_open_stateid(stp, &reaplist))
1730 put_ol_stateid_locked(stp, &reaplist);
1731 }
1732 spin_unlock(&clp->cl_lock);
1733 free_ol_stateid_reaplist(&reaplist);
1734 release_last_closed_stateid(oo);
1735 nfs4_put_stateowner(&oo->oo_owner);
1736 }
1737
find_one_sb_stid(struct nfs4_client * clp,struct super_block * sb,unsigned int sc_types)1738 static struct nfs4_stid *find_one_sb_stid(struct nfs4_client *clp,
1739 struct super_block *sb,
1740 unsigned int sc_types)
1741 {
1742 unsigned long id, tmp;
1743 struct nfs4_stid *stid;
1744
1745 spin_lock(&clp->cl_lock);
1746 idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id)
1747 if ((stid->sc_type & sc_types) &&
1748 stid->sc_status == 0 &&
1749 stid->sc_file->fi_inode->i_sb == sb) {
1750 refcount_inc(&stid->sc_count);
1751 break;
1752 }
1753 spin_unlock(&clp->cl_lock);
1754 return stid;
1755 }
1756
1757 /**
1758 * nfsd4_revoke_states - revoke all nfsv4 states associated with given filesystem
1759 * @net: used to identify instance of nfsd (there is one per net namespace)
1760 * @sb: super_block used to identify target filesystem
1761 *
1762 * All nfs4 states (open, lock, delegation, layout) held by the server instance
1763 * and associated with a file on the given filesystem will be revoked resulting
1764 * in any files being closed and so all references from nfsd to the filesystem
1765 * being released. Thus nfsd will no longer prevent the filesystem from being
1766 * unmounted.
1767 *
1768 * The clients which own the states will subsequently being notified that the
1769 * states have been "admin-revoked".
1770 */
nfsd4_revoke_states(struct net * net,struct super_block * sb)1771 void nfsd4_revoke_states(struct net *net, struct super_block *sb)
1772 {
1773 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1774 unsigned int idhashval;
1775 unsigned int sc_types;
1776
1777 sc_types = SC_TYPE_OPEN | SC_TYPE_LOCK | SC_TYPE_DELEG | SC_TYPE_LAYOUT;
1778
1779 spin_lock(&nn->client_lock);
1780 for (idhashval = 0; idhashval < CLIENT_HASH_MASK; idhashval++) {
1781 struct list_head *head = &nn->conf_id_hashtbl[idhashval];
1782 struct nfs4_client *clp;
1783 retry:
1784 list_for_each_entry(clp, head, cl_idhash) {
1785 struct nfs4_stid *stid = find_one_sb_stid(clp, sb,
1786 sc_types);
1787 if (stid) {
1788 struct nfs4_ol_stateid *stp;
1789 struct nfs4_delegation *dp;
1790 struct nfs4_layout_stateid *ls;
1791
1792 spin_unlock(&nn->client_lock);
1793 switch (stid->sc_type) {
1794 case SC_TYPE_OPEN:
1795 stp = openlockstateid(stid);
1796 mutex_lock_nested(&stp->st_mutex,
1797 OPEN_STATEID_MUTEX);
1798
1799 spin_lock(&clp->cl_lock);
1800 if (stid->sc_status == 0) {
1801 stid->sc_status |=
1802 SC_STATUS_ADMIN_REVOKED;
1803 atomic_inc(&clp->cl_admin_revoked);
1804 spin_unlock(&clp->cl_lock);
1805 release_all_access(stp);
1806 } else
1807 spin_unlock(&clp->cl_lock);
1808 mutex_unlock(&stp->st_mutex);
1809 break;
1810 case SC_TYPE_LOCK:
1811 stp = openlockstateid(stid);
1812 mutex_lock_nested(&stp->st_mutex,
1813 LOCK_STATEID_MUTEX);
1814 spin_lock(&clp->cl_lock);
1815 if (stid->sc_status == 0) {
1816 struct nfs4_lockowner *lo =
1817 lockowner(stp->st_stateowner);
1818 struct nfsd_file *nf;
1819
1820 stid->sc_status |=
1821 SC_STATUS_ADMIN_REVOKED;
1822 atomic_inc(&clp->cl_admin_revoked);
1823 spin_unlock(&clp->cl_lock);
1824 nf = find_any_file(stp->st_stid.sc_file);
1825 if (nf) {
1826 get_file(nf->nf_file);
1827 filp_close(nf->nf_file,
1828 (fl_owner_t)lo);
1829 nfsd_file_put(nf);
1830 }
1831 release_all_access(stp);
1832 } else
1833 spin_unlock(&clp->cl_lock);
1834 mutex_unlock(&stp->st_mutex);
1835 break;
1836 case SC_TYPE_DELEG:
1837 refcount_inc(&stid->sc_count);
1838 dp = delegstateid(stid);
1839 spin_lock(&state_lock);
1840 if (!unhash_delegation_locked(
1841 dp, SC_STATUS_ADMIN_REVOKED))
1842 dp = NULL;
1843 spin_unlock(&state_lock);
1844 if (dp)
1845 revoke_delegation(dp);
1846 break;
1847 case SC_TYPE_LAYOUT:
1848 ls = layoutstateid(stid);
1849 nfsd4_close_layout(ls);
1850 break;
1851 }
1852 nfs4_put_stid(stid);
1853 spin_lock(&nn->client_lock);
1854 if (clp->cl_minorversion == 0)
1855 /* Allow cleanup after a lease period.
1856 * store_release ensures cleanup will
1857 * see any newly revoked states if it
1858 * sees the time updated.
1859 */
1860 nn->nfs40_last_revoke =
1861 ktime_get_boottime_seconds();
1862 goto retry;
1863 }
1864 }
1865 }
1866 spin_unlock(&nn->client_lock);
1867 }
1868
1869 static inline int
hash_sessionid(struct nfs4_sessionid * sessionid)1870 hash_sessionid(struct nfs4_sessionid *sessionid)
1871 {
1872 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1873
1874 return sid->sequence % SESSION_HASH_SIZE;
1875 }
1876
1877 #ifdef CONFIG_SUNRPC_DEBUG
1878 static inline void
dump_sessionid(const char * fn,struct nfs4_sessionid * sessionid)1879 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1880 {
1881 u32 *ptr = (u32 *)(&sessionid->data[0]);
1882 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1883 }
1884 #else
1885 static inline void
dump_sessionid(const char * fn,struct nfs4_sessionid * sessionid)1886 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1887 {
1888 }
1889 #endif
1890
1891 /*
1892 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1893 * won't be used for replay.
1894 */
nfsd4_bump_seqid(struct nfsd4_compound_state * cstate,__be32 nfserr)1895 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1896 {
1897 struct nfs4_stateowner *so = cstate->replay_owner;
1898
1899 if (nfserr == nfserr_replay_me)
1900 return;
1901
1902 if (!seqid_mutating_err(ntohl(nfserr))) {
1903 nfsd4_cstate_clear_replay(cstate);
1904 return;
1905 }
1906 if (!so)
1907 return;
1908 if (so->so_is_open_owner)
1909 release_last_closed_stateid(openowner(so));
1910 so->so_seqid++;
1911 return;
1912 }
1913
1914 static void
gen_sessionid(struct nfsd4_session * ses)1915 gen_sessionid(struct nfsd4_session *ses)
1916 {
1917 struct nfs4_client *clp = ses->se_client;
1918 struct nfsd4_sessionid *sid;
1919
1920 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1921 sid->clientid = clp->cl_clientid;
1922 sid->sequence = current_sessionid++;
1923 sid->reserved = 0;
1924 }
1925
1926 /*
1927 * The protocol defines ca_maxresponssize_cached to include the size of
1928 * the rpc header, but all we need to cache is the data starting after
1929 * the end of the initial SEQUENCE operation--the rest we regenerate
1930 * each time. Therefore we can advertise a ca_maxresponssize_cached
1931 * value that is the number of bytes in our cache plus a few additional
1932 * bytes. In order to stay on the safe side, and not promise more than
1933 * we can cache, those additional bytes must be the minimum possible: 24
1934 * bytes of rpc header (xid through accept state, with AUTH_NULL
1935 * verifier), 12 for the compound header (with zero-length tag), and 44
1936 * for the SEQUENCE op response:
1937 */
1938 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1939
1940 static struct shrinker *nfsd_slot_shrinker;
1941 static DEFINE_SPINLOCK(nfsd_session_list_lock);
1942 static LIST_HEAD(nfsd_session_list);
1943 /* The sum of "target_slots-1" on every session. The shrinker can push this
1944 * down, though it can take a little while for the memory to actually
1945 * be freed. The "-1" is because we can never free slot 0 while the
1946 * session is active.
1947 */
1948 static atomic_t nfsd_total_target_slots = ATOMIC_INIT(0);
1949
1950 static void
free_session_slots(struct nfsd4_session * ses,int from)1951 free_session_slots(struct nfsd4_session *ses, int from)
1952 {
1953 int i;
1954
1955 if (from >= ses->se_fchannel.maxreqs)
1956 return;
1957
1958 for (i = from; i < ses->se_fchannel.maxreqs; i++) {
1959 struct nfsd4_slot *slot = xa_load(&ses->se_slots, i);
1960
1961 /*
1962 * Save the seqid in case we reactivate this slot.
1963 * This will never require a memory allocation so GFP
1964 * flag is irrelevant
1965 */
1966 xa_store(&ses->se_slots, i, xa_mk_value(slot->sl_seqid), 0);
1967 free_svc_cred(&slot->sl_cred);
1968 kfree(slot);
1969 }
1970 ses->se_fchannel.maxreqs = from;
1971 if (ses->se_target_maxslots > from) {
1972 int new_target = from ?: 1;
1973 atomic_sub(ses->se_target_maxslots - new_target, &nfsd_total_target_slots);
1974 ses->se_target_maxslots = new_target;
1975 }
1976 }
1977
1978 /**
1979 * reduce_session_slots - reduce the target max-slots of a session if possible
1980 * @ses: The session to affect
1981 * @dec: how much to decrease the target by
1982 *
1983 * This interface can be used by a shrinker to reduce the target max-slots
1984 * for a session so that some slots can eventually be freed.
1985 * It uses spin_trylock() as it may be called in a context where another
1986 * spinlock is held that has a dependency on client_lock. As shrinkers are
1987 * best-effort, skiping a session is client_lock is already held has no
1988 * great coast
1989 *
1990 * Return value:
1991 * The number of slots that the target was reduced by.
1992 */
1993 static int
reduce_session_slots(struct nfsd4_session * ses,int dec)1994 reduce_session_slots(struct nfsd4_session *ses, int dec)
1995 {
1996 struct nfsd_net *nn = net_generic(ses->se_client->net,
1997 nfsd_net_id);
1998 int ret = 0;
1999
2000 if (ses->se_target_maxslots <= 1)
2001 return ret;
2002 if (!spin_trylock(&nn->client_lock))
2003 return ret;
2004 ret = min(dec, ses->se_target_maxslots-1);
2005 ses->se_target_maxslots -= ret;
2006 atomic_sub(ret, &nfsd_total_target_slots);
2007 ses->se_slot_gen += 1;
2008 if (ses->se_slot_gen == 0) {
2009 int i;
2010 ses->se_slot_gen = 1;
2011 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
2012 struct nfsd4_slot *slot = xa_load(&ses->se_slots, i);
2013 slot->sl_generation = 0;
2014 }
2015 }
2016 spin_unlock(&nn->client_lock);
2017 return ret;
2018 }
2019
nfsd4_alloc_slot(struct nfsd4_channel_attrs * fattrs,int index,gfp_t gfp)2020 static struct nfsd4_slot *nfsd4_alloc_slot(struct nfsd4_channel_attrs *fattrs,
2021 int index, gfp_t gfp)
2022 {
2023 struct nfsd4_slot *slot;
2024 size_t size;
2025
2026 /*
2027 * The RPC and NFS session headers are never saved in
2028 * the slot reply cache buffer.
2029 */
2030 size = fattrs->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ ?
2031 0 : fattrs->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
2032
2033 slot = kzalloc(struct_size(slot, sl_data, size), gfp);
2034 if (!slot)
2035 return NULL;
2036 slot->sl_index = index;
2037 return slot;
2038 }
2039
alloc_session(struct nfsd4_channel_attrs * fattrs,struct nfsd4_channel_attrs * battrs)2040 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
2041 struct nfsd4_channel_attrs *battrs)
2042 {
2043 int numslots = fattrs->maxreqs;
2044 struct nfsd4_session *new;
2045 struct nfsd4_slot *slot;
2046 int i;
2047
2048 new = kzalloc(sizeof(*new), GFP_KERNEL);
2049 if (!new)
2050 return NULL;
2051 xa_init(&new->se_slots);
2052
2053 slot = nfsd4_alloc_slot(fattrs, 0, GFP_KERNEL);
2054 if (!slot || xa_is_err(xa_store(&new->se_slots, 0, slot, GFP_KERNEL)))
2055 goto out_free;
2056
2057 for (i = 1; i < numslots; i++) {
2058 const gfp_t gfp = GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
2059 slot = nfsd4_alloc_slot(fattrs, i, gfp);
2060 if (!slot)
2061 break;
2062 if (xa_is_err(xa_store(&new->se_slots, i, slot, gfp))) {
2063 kfree(slot);
2064 break;
2065 }
2066 }
2067 fattrs->maxreqs = i;
2068 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
2069 new->se_target_maxslots = i;
2070 atomic_add(i - 1, &nfsd_total_target_slots);
2071 new->se_cb_slot_avail = ~0U;
2072 new->se_cb_highest_slot = min(battrs->maxreqs - 1,
2073 NFSD_BC_SLOT_TABLE_SIZE - 1);
2074 spin_lock_init(&new->se_lock);
2075 return new;
2076 out_free:
2077 kfree(slot);
2078 xa_destroy(&new->se_slots);
2079 kfree(new);
2080 return NULL;
2081 }
2082
free_conn(struct nfsd4_conn * c)2083 static void free_conn(struct nfsd4_conn *c)
2084 {
2085 svc_xprt_put(c->cn_xprt);
2086 kfree(c);
2087 }
2088
nfsd4_conn_lost(struct svc_xpt_user * u)2089 static void nfsd4_conn_lost(struct svc_xpt_user *u)
2090 {
2091 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
2092 struct nfs4_client *clp = c->cn_session->se_client;
2093
2094 trace_nfsd_cb_lost(clp);
2095
2096 spin_lock(&clp->cl_lock);
2097 if (!list_empty(&c->cn_persession)) {
2098 list_del(&c->cn_persession);
2099 free_conn(c);
2100 }
2101 nfsd4_probe_callback(clp);
2102 spin_unlock(&clp->cl_lock);
2103 }
2104
alloc_conn(struct svc_rqst * rqstp,u32 flags)2105 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
2106 {
2107 struct nfsd4_conn *conn;
2108
2109 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
2110 if (!conn)
2111 return NULL;
2112 svc_xprt_get(rqstp->rq_xprt);
2113 conn->cn_xprt = rqstp->rq_xprt;
2114 conn->cn_flags = flags;
2115 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
2116 return conn;
2117 }
2118
__nfsd4_hash_conn(struct nfsd4_conn * conn,struct nfsd4_session * ses)2119 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
2120 {
2121 conn->cn_session = ses;
2122 list_add(&conn->cn_persession, &ses->se_conns);
2123 }
2124
nfsd4_hash_conn(struct nfsd4_conn * conn,struct nfsd4_session * ses)2125 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
2126 {
2127 struct nfs4_client *clp = ses->se_client;
2128
2129 spin_lock(&clp->cl_lock);
2130 __nfsd4_hash_conn(conn, ses);
2131 spin_unlock(&clp->cl_lock);
2132 }
2133
nfsd4_register_conn(struct nfsd4_conn * conn)2134 static int nfsd4_register_conn(struct nfsd4_conn *conn)
2135 {
2136 conn->cn_xpt_user.callback = nfsd4_conn_lost;
2137 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
2138 }
2139
nfsd4_init_conn(struct svc_rqst * rqstp,struct nfsd4_conn * conn,struct nfsd4_session * ses)2140 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
2141 {
2142 int ret;
2143
2144 nfsd4_hash_conn(conn, ses);
2145 ret = nfsd4_register_conn(conn);
2146 if (ret)
2147 /* oops; xprt is already down: */
2148 nfsd4_conn_lost(&conn->cn_xpt_user);
2149 /* We may have gained or lost a callback channel: */
2150 nfsd4_probe_callback_sync(ses->se_client);
2151 }
2152
alloc_conn_from_crses(struct svc_rqst * rqstp,struct nfsd4_create_session * cses)2153 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
2154 {
2155 u32 dir = NFS4_CDFC4_FORE;
2156
2157 if (cses->flags & SESSION4_BACK_CHAN)
2158 dir |= NFS4_CDFC4_BACK;
2159 return alloc_conn(rqstp, dir);
2160 }
2161
2162 /* must be called under client_lock */
nfsd4_del_conns(struct nfsd4_session * s)2163 static void nfsd4_del_conns(struct nfsd4_session *s)
2164 {
2165 struct nfs4_client *clp = s->se_client;
2166 struct nfsd4_conn *c;
2167
2168 spin_lock(&clp->cl_lock);
2169 while (!list_empty(&s->se_conns)) {
2170 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
2171 list_del_init(&c->cn_persession);
2172 spin_unlock(&clp->cl_lock);
2173
2174 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
2175 free_conn(c);
2176
2177 spin_lock(&clp->cl_lock);
2178 }
2179 spin_unlock(&clp->cl_lock);
2180 }
2181
__free_session(struct nfsd4_session * ses)2182 static void __free_session(struct nfsd4_session *ses)
2183 {
2184 free_session_slots(ses, 0);
2185 xa_destroy(&ses->se_slots);
2186 kfree(ses);
2187 }
2188
free_session(struct nfsd4_session * ses)2189 static void free_session(struct nfsd4_session *ses)
2190 {
2191 nfsd4_del_conns(ses);
2192 __free_session(ses);
2193 }
2194
2195 static unsigned long
nfsd_slot_count(struct shrinker * s,struct shrink_control * sc)2196 nfsd_slot_count(struct shrinker *s, struct shrink_control *sc)
2197 {
2198 unsigned long cnt = atomic_read(&nfsd_total_target_slots);
2199
2200 return cnt ? cnt : SHRINK_EMPTY;
2201 }
2202
2203 static unsigned long
nfsd_slot_scan(struct shrinker * s,struct shrink_control * sc)2204 nfsd_slot_scan(struct shrinker *s, struct shrink_control *sc)
2205 {
2206 struct nfsd4_session *ses;
2207 unsigned long scanned = 0;
2208 unsigned long freed = 0;
2209
2210 spin_lock(&nfsd_session_list_lock);
2211 list_for_each_entry(ses, &nfsd_session_list, se_all_sessions) {
2212 freed += reduce_session_slots(ses, 1);
2213 scanned += 1;
2214 if (scanned >= sc->nr_to_scan) {
2215 /* Move starting point for next scan */
2216 list_move(&nfsd_session_list, &ses->se_all_sessions);
2217 break;
2218 }
2219 }
2220 spin_unlock(&nfsd_session_list_lock);
2221 sc->nr_scanned = scanned;
2222 return freed;
2223 }
2224
init_session(struct svc_rqst * rqstp,struct nfsd4_session * new,struct nfs4_client * clp,struct nfsd4_create_session * cses)2225 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
2226 {
2227 int idx;
2228 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2229
2230 new->se_client = clp;
2231 gen_sessionid(new);
2232
2233 INIT_LIST_HEAD(&new->se_conns);
2234
2235 atomic_set(&new->se_ref, 0);
2236 new->se_dead = false;
2237 new->se_cb_prog = cses->callback_prog;
2238 new->se_cb_sec = cses->cb_sec;
2239
2240 for (idx = 0; idx < NFSD_BC_SLOT_TABLE_SIZE; ++idx)
2241 new->se_cb_seq_nr[idx] = 1;
2242
2243 idx = hash_sessionid(&new->se_sessionid);
2244 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
2245 spin_lock(&clp->cl_lock);
2246 list_add(&new->se_perclnt, &clp->cl_sessions);
2247 spin_unlock(&clp->cl_lock);
2248
2249 spin_lock(&nfsd_session_list_lock);
2250 list_add_tail(&new->se_all_sessions, &nfsd_session_list);
2251 spin_unlock(&nfsd_session_list_lock);
2252
2253 {
2254 struct sockaddr *sa = svc_addr(rqstp);
2255 /*
2256 * This is a little silly; with sessions there's no real
2257 * use for the callback address. Use the peer address
2258 * as a reasonable default for now, but consider fixing
2259 * the rpc client not to require an address in the
2260 * future:
2261 */
2262 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
2263 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
2264 }
2265 }
2266
2267 /* caller must hold client_lock */
2268 static struct nfsd4_session *
__find_in_sessionid_hashtbl(struct nfs4_sessionid * sessionid,struct net * net)2269 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
2270 {
2271 struct nfsd4_session *elem;
2272 int idx;
2273 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2274
2275 lockdep_assert_held(&nn->client_lock);
2276
2277 dump_sessionid(__func__, sessionid);
2278 idx = hash_sessionid(sessionid);
2279 /* Search in the appropriate list */
2280 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
2281 if (!memcmp(elem->se_sessionid.data, sessionid->data,
2282 NFS4_MAX_SESSIONID_LEN)) {
2283 return elem;
2284 }
2285 }
2286
2287 dprintk("%s: session not found\n", __func__);
2288 return NULL;
2289 }
2290
2291 static struct nfsd4_session *
find_in_sessionid_hashtbl(struct nfs4_sessionid * sessionid,struct net * net,__be32 * ret)2292 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2293 __be32 *ret)
2294 {
2295 struct nfsd4_session *session;
2296 __be32 status = nfserr_badsession;
2297
2298 session = __find_in_sessionid_hashtbl(sessionid, net);
2299 if (!session)
2300 goto out;
2301 status = nfsd4_get_session_locked(session);
2302 if (status)
2303 session = NULL;
2304 out:
2305 *ret = status;
2306 return session;
2307 }
2308
2309 /* caller must hold client_lock */
2310 static void
unhash_session(struct nfsd4_session * ses)2311 unhash_session(struct nfsd4_session *ses)
2312 {
2313 struct nfs4_client *clp = ses->se_client;
2314 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2315
2316 lockdep_assert_held(&nn->client_lock);
2317
2318 list_del(&ses->se_hash);
2319 spin_lock(&ses->se_client->cl_lock);
2320 list_del(&ses->se_perclnt);
2321 spin_unlock(&ses->se_client->cl_lock);
2322 spin_lock(&nfsd_session_list_lock);
2323 list_del(&ses->se_all_sessions);
2324 spin_unlock(&nfsd_session_list_lock);
2325 }
2326
2327 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2328 static int
STALE_CLIENTID(clientid_t * clid,struct nfsd_net * nn)2329 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
2330 {
2331 /*
2332 * We're assuming the clid was not given out from a boot
2333 * precisely 2^32 (about 136 years) before this one. That seems
2334 * a safe assumption:
2335 */
2336 if (clid->cl_boot == (u32)nn->boot_time)
2337 return 0;
2338 trace_nfsd_clid_stale(clid);
2339 return 1;
2340 }
2341
alloc_client(struct xdr_netobj name,struct nfsd_net * nn)2342 static struct nfs4_client *alloc_client(struct xdr_netobj name,
2343 struct nfsd_net *nn)
2344 {
2345 struct nfs4_client *clp;
2346 int i;
2347
2348 if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients &&
2349 atomic_read(&nn->nfsd_courtesy_clients) > 0)
2350 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
2351
2352 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
2353 if (clp == NULL)
2354 return NULL;
2355 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
2356 if (clp->cl_name.data == NULL)
2357 goto err_no_name;
2358 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
2359 sizeof(struct list_head),
2360 GFP_KERNEL);
2361 if (!clp->cl_ownerstr_hashtbl)
2362 goto err_no_hashtbl;
2363 clp->cl_callback_wq = alloc_ordered_workqueue("nfsd4_callbacks", 0);
2364 if (!clp->cl_callback_wq)
2365 goto err_no_callback_wq;
2366
2367 for (i = 0; i < OWNER_HASH_SIZE; i++)
2368 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
2369 INIT_LIST_HEAD(&clp->cl_sessions);
2370 idr_init(&clp->cl_stateids);
2371 atomic_set(&clp->cl_rpc_users, 0);
2372 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
2373 clp->cl_state = NFSD4_ACTIVE;
2374 atomic_inc(&nn->nfs4_client_count);
2375 atomic_set(&clp->cl_delegs_in_recall, 0);
2376 INIT_LIST_HEAD(&clp->cl_idhash);
2377 INIT_LIST_HEAD(&clp->cl_openowners);
2378 INIT_LIST_HEAD(&clp->cl_delegations);
2379 INIT_LIST_HEAD(&clp->cl_lru);
2380 INIT_LIST_HEAD(&clp->cl_revoked);
2381 #ifdef CONFIG_NFSD_PNFS
2382 INIT_LIST_HEAD(&clp->cl_lo_states);
2383 #endif
2384 INIT_LIST_HEAD(&clp->async_copies);
2385 spin_lock_init(&clp->async_lock);
2386 spin_lock_init(&clp->cl_lock);
2387 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
2388 return clp;
2389 err_no_callback_wq:
2390 kfree(clp->cl_ownerstr_hashtbl);
2391 err_no_hashtbl:
2392 kfree(clp->cl_name.data);
2393 err_no_name:
2394 kmem_cache_free(client_slab, clp);
2395 return NULL;
2396 }
2397
__free_client(struct kref * k)2398 static void __free_client(struct kref *k)
2399 {
2400 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2401 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
2402
2403 free_svc_cred(&clp->cl_cred);
2404 destroy_workqueue(clp->cl_callback_wq);
2405 kfree(clp->cl_ownerstr_hashtbl);
2406 kfree(clp->cl_name.data);
2407 kfree(clp->cl_nii_domain.data);
2408 kfree(clp->cl_nii_name.data);
2409 idr_destroy(&clp->cl_stateids);
2410 kfree(clp->cl_ra);
2411 kmem_cache_free(client_slab, clp);
2412 }
2413
drop_client(struct nfs4_client * clp)2414 static void drop_client(struct nfs4_client *clp)
2415 {
2416 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2417 }
2418
2419 static void
free_client(struct nfs4_client * clp)2420 free_client(struct nfs4_client *clp)
2421 {
2422 while (!list_empty(&clp->cl_sessions)) {
2423 struct nfsd4_session *ses;
2424 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2425 se_perclnt);
2426 list_del(&ses->se_perclnt);
2427 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2428 free_session(ses);
2429 }
2430 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2431 if (clp->cl_nfsd_dentry) {
2432 nfsd_client_rmdir(clp->cl_nfsd_dentry);
2433 clp->cl_nfsd_dentry = NULL;
2434 wake_up_all(&expiry_wq);
2435 }
2436 drop_client(clp);
2437 }
2438
2439 /* must be called under the client_lock */
2440 static void
unhash_client_locked(struct nfs4_client * clp)2441 unhash_client_locked(struct nfs4_client *clp)
2442 {
2443 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2444 struct nfsd4_session *ses;
2445
2446 lockdep_assert_held(&nn->client_lock);
2447
2448 /* Mark the client as expired! */
2449 clp->cl_time = 0;
2450 /* Make it invisible */
2451 if (!list_empty(&clp->cl_idhash)) {
2452 list_del_init(&clp->cl_idhash);
2453 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2454 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2455 else
2456 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2457 }
2458 list_del_init(&clp->cl_lru);
2459 spin_lock(&clp->cl_lock);
2460 spin_lock(&nfsd_session_list_lock);
2461 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) {
2462 list_del_init(&ses->se_hash);
2463 list_del_init(&ses->se_all_sessions);
2464 }
2465 spin_unlock(&nfsd_session_list_lock);
2466 spin_unlock(&clp->cl_lock);
2467 }
2468
2469 static void
unhash_client(struct nfs4_client * clp)2470 unhash_client(struct nfs4_client *clp)
2471 {
2472 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2473
2474 spin_lock(&nn->client_lock);
2475 unhash_client_locked(clp);
2476 spin_unlock(&nn->client_lock);
2477 }
2478
mark_client_expired_locked(struct nfs4_client * clp)2479 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2480 {
2481 int users = atomic_read(&clp->cl_rpc_users);
2482
2483 trace_nfsd_mark_client_expired(clp, users);
2484
2485 if (users)
2486 return nfserr_jukebox;
2487 unhash_client_locked(clp);
2488 return nfs_ok;
2489 }
2490
2491 static void
__destroy_client(struct nfs4_client * clp)2492 __destroy_client(struct nfs4_client *clp)
2493 {
2494 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2495 int i;
2496 struct nfs4_openowner *oo;
2497 struct nfs4_delegation *dp;
2498 LIST_HEAD(reaplist);
2499
2500 spin_lock(&state_lock);
2501 while (!list_empty(&clp->cl_delegations)) {
2502 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2503 unhash_delegation_locked(dp, SC_STATUS_CLOSED);
2504 list_add(&dp->dl_recall_lru, &reaplist);
2505 }
2506 spin_unlock(&state_lock);
2507 while (!list_empty(&reaplist)) {
2508 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2509 list_del_init(&dp->dl_recall_lru);
2510 destroy_unhashed_deleg(dp);
2511 }
2512 while (!list_empty(&clp->cl_revoked)) {
2513 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2514 list_del_init(&dp->dl_recall_lru);
2515 nfs4_put_stid(&dp->dl_stid);
2516 }
2517 while (!list_empty(&clp->cl_openowners)) {
2518 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2519 nfs4_get_stateowner(&oo->oo_owner);
2520 release_openowner(oo);
2521 }
2522 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2523 struct nfs4_stateowner *so, *tmp;
2524
2525 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2526 so_strhash) {
2527 /* Should be no openowners at this point */
2528 WARN_ON_ONCE(so->so_is_open_owner);
2529 remove_blocked_locks(lockowner(so));
2530 }
2531 }
2532 nfsd4_return_all_client_layouts(clp);
2533 nfsd4_shutdown_copy(clp);
2534 nfsd4_shutdown_callback(clp);
2535 if (clp->cl_cb_conn.cb_xprt)
2536 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2537 atomic_add_unless(&nn->nfs4_client_count, -1, 0);
2538 nfsd4_dec_courtesy_client_count(nn, clp);
2539 free_client(clp);
2540 wake_up_all(&expiry_wq);
2541 }
2542
2543 static void
destroy_client(struct nfs4_client * clp)2544 destroy_client(struct nfs4_client *clp)
2545 {
2546 unhash_client(clp);
2547 __destroy_client(clp);
2548 }
2549
inc_reclaim_complete(struct nfs4_client * clp)2550 static void inc_reclaim_complete(struct nfs4_client *clp)
2551 {
2552 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2553
2554 if (!nn->track_reclaim_completes)
2555 return;
2556 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2557 return;
2558 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2559 nn->reclaim_str_hashtbl_size) {
2560 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2561 clp->net->ns.inum);
2562 nfsd4_end_grace(nn);
2563 }
2564 }
2565
expire_client(struct nfs4_client * clp)2566 static void expire_client(struct nfs4_client *clp)
2567 {
2568 unhash_client(clp);
2569 nfsd4_client_record_remove(clp);
2570 __destroy_client(clp);
2571 }
2572
copy_verf(struct nfs4_client * target,nfs4_verifier * source)2573 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2574 {
2575 memcpy(target->cl_verifier.data, source->data,
2576 sizeof(target->cl_verifier.data));
2577 }
2578
copy_clid(struct nfs4_client * target,struct nfs4_client * source)2579 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2580 {
2581 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2582 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2583 }
2584
copy_cred(struct svc_cred * target,struct svc_cred * source)2585 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2586 {
2587 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2588 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2589 GFP_KERNEL);
2590 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2591 if ((source->cr_principal && !target->cr_principal) ||
2592 (source->cr_raw_principal && !target->cr_raw_principal) ||
2593 (source->cr_targ_princ && !target->cr_targ_princ))
2594 return -ENOMEM;
2595
2596 target->cr_flavor = source->cr_flavor;
2597 target->cr_uid = source->cr_uid;
2598 target->cr_gid = source->cr_gid;
2599 target->cr_group_info = source->cr_group_info;
2600 get_group_info(target->cr_group_info);
2601 target->cr_gss_mech = source->cr_gss_mech;
2602 if (source->cr_gss_mech)
2603 gss_mech_get(source->cr_gss_mech);
2604 return 0;
2605 }
2606
2607 static int
compare_blob(const struct xdr_netobj * o1,const struct xdr_netobj * o2)2608 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2609 {
2610 if (o1->len < o2->len)
2611 return -1;
2612 if (o1->len > o2->len)
2613 return 1;
2614 return memcmp(o1->data, o2->data, o1->len);
2615 }
2616
2617 static int
same_verf(nfs4_verifier * v1,nfs4_verifier * v2)2618 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2619 {
2620 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2621 }
2622
2623 static int
same_clid(clientid_t * cl1,clientid_t * cl2)2624 same_clid(clientid_t *cl1, clientid_t *cl2)
2625 {
2626 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2627 }
2628
groups_equal(struct group_info * g1,struct group_info * g2)2629 static bool groups_equal(struct group_info *g1, struct group_info *g2)
2630 {
2631 int i;
2632
2633 if (g1->ngroups != g2->ngroups)
2634 return false;
2635 for (i=0; i<g1->ngroups; i++)
2636 if (!gid_eq(g1->gid[i], g2->gid[i]))
2637 return false;
2638 return true;
2639 }
2640
2641 /*
2642 * RFC 3530 language requires clid_inuse be returned when the
2643 * "principal" associated with a requests differs from that previously
2644 * used. We use uid, gid's, and gss principal string as our best
2645 * approximation. We also don't want to allow non-gss use of a client
2646 * established using gss: in theory cr_principal should catch that
2647 * change, but in practice cr_principal can be null even in the gss case
2648 * since gssd doesn't always pass down a principal string.
2649 */
is_gss_cred(struct svc_cred * cr)2650 static bool is_gss_cred(struct svc_cred *cr)
2651 {
2652 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2653 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2654 }
2655
2656
2657 static bool
same_creds(struct svc_cred * cr1,struct svc_cred * cr2)2658 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2659 {
2660 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2661 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2662 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2663 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2664 return false;
2665 /* XXX: check that cr_targ_princ fields match ? */
2666 if (cr1->cr_principal == cr2->cr_principal)
2667 return true;
2668 if (!cr1->cr_principal || !cr2->cr_principal)
2669 return false;
2670 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2671 }
2672
svc_rqst_integrity_protected(struct svc_rqst * rqstp)2673 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2674 {
2675 struct svc_cred *cr = &rqstp->rq_cred;
2676 u32 service;
2677
2678 if (!cr->cr_gss_mech)
2679 return false;
2680 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2681 return service == RPC_GSS_SVC_INTEGRITY ||
2682 service == RPC_GSS_SVC_PRIVACY;
2683 }
2684
nfsd4_mach_creds_match(struct nfs4_client * cl,struct svc_rqst * rqstp)2685 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2686 {
2687 struct svc_cred *cr = &rqstp->rq_cred;
2688
2689 if (!cl->cl_mach_cred)
2690 return true;
2691 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2692 return false;
2693 if (!svc_rqst_integrity_protected(rqstp))
2694 return false;
2695 if (cl->cl_cred.cr_raw_principal)
2696 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2697 cr->cr_raw_principal);
2698 if (!cr->cr_principal)
2699 return false;
2700 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2701 }
2702
gen_confirm(struct nfs4_client * clp,struct nfsd_net * nn)2703 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2704 {
2705 __be32 verf[2];
2706
2707 /*
2708 * This is opaque to client, so no need to byte-swap. Use
2709 * __force to keep sparse happy
2710 */
2711 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2712 verf[1] = (__force __be32)nn->clverifier_counter++;
2713 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2714 }
2715
gen_clid(struct nfs4_client * clp,struct nfsd_net * nn)2716 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2717 {
2718 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2719 clp->cl_clientid.cl_id = nn->clientid_counter++;
2720 gen_confirm(clp, nn);
2721 }
2722
2723 static struct nfs4_stid *
find_stateid_locked(struct nfs4_client * cl,stateid_t * t)2724 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2725 {
2726 struct nfs4_stid *ret;
2727
2728 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2729 if (!ret || !ret->sc_type)
2730 return NULL;
2731 return ret;
2732 }
2733
2734 static struct nfs4_stid *
find_stateid_by_type(struct nfs4_client * cl,stateid_t * t,unsigned short typemask,unsigned short ok_states)2735 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t,
2736 unsigned short typemask, unsigned short ok_states)
2737 {
2738 struct nfs4_stid *s;
2739
2740 spin_lock(&cl->cl_lock);
2741 s = find_stateid_locked(cl, t);
2742 if (s != NULL) {
2743 if ((s->sc_status & ~ok_states) == 0 &&
2744 (typemask & s->sc_type))
2745 refcount_inc(&s->sc_count);
2746 else
2747 s = NULL;
2748 }
2749 spin_unlock(&cl->cl_lock);
2750 return s;
2751 }
2752
get_nfsdfs_clp(struct inode * inode)2753 static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2754 {
2755 struct nfsdfs_client *nc;
2756 nc = get_nfsdfs_client(inode);
2757 if (!nc)
2758 return NULL;
2759 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2760 }
2761
seq_quote_mem(struct seq_file * m,char * data,int len)2762 static void seq_quote_mem(struct seq_file *m, char *data, int len)
2763 {
2764 seq_puts(m, "\"");
2765 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2766 seq_puts(m, "\"");
2767 }
2768
cb_state2str(int state)2769 static const char *cb_state2str(int state)
2770 {
2771 switch (state) {
2772 case NFSD4_CB_UP:
2773 return "UP";
2774 case NFSD4_CB_UNKNOWN:
2775 return "UNKNOWN";
2776 case NFSD4_CB_DOWN:
2777 return "DOWN";
2778 case NFSD4_CB_FAULT:
2779 return "FAULT";
2780 }
2781 return "UNDEFINED";
2782 }
2783
client_info_show(struct seq_file * m,void * v)2784 static int client_info_show(struct seq_file *m, void *v)
2785 {
2786 struct inode *inode = file_inode(m->file);
2787 struct nfsd4_session *ses;
2788 struct nfs4_client *clp;
2789 u64 clid;
2790
2791 clp = get_nfsdfs_clp(inode);
2792 if (!clp)
2793 return -ENXIO;
2794 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2795 seq_printf(m, "clientid: 0x%llx\n", clid);
2796 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2797
2798 if (clp->cl_state == NFSD4_COURTESY)
2799 seq_puts(m, "status: courtesy\n");
2800 else if (clp->cl_state == NFSD4_EXPIRABLE)
2801 seq_puts(m, "status: expirable\n");
2802 else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2803 seq_puts(m, "status: confirmed\n");
2804 else
2805 seq_puts(m, "status: unconfirmed\n");
2806 seq_printf(m, "seconds from last renew: %lld\n",
2807 ktime_get_boottime_seconds() - clp->cl_time);
2808 seq_puts(m, "name: ");
2809 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2810 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2811 if (clp->cl_nii_domain.data) {
2812 seq_puts(m, "Implementation domain: ");
2813 seq_quote_mem(m, clp->cl_nii_domain.data,
2814 clp->cl_nii_domain.len);
2815 seq_puts(m, "\nImplementation name: ");
2816 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2817 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2818 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2819 }
2820 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2821 seq_printf(m, "callback address: \"%pISpc\"\n", &clp->cl_cb_conn.cb_addr);
2822 seq_printf(m, "admin-revoked states: %d\n",
2823 atomic_read(&clp->cl_admin_revoked));
2824 spin_lock(&clp->cl_lock);
2825 seq_printf(m, "session slots:");
2826 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2827 seq_printf(m, " %u", ses->se_fchannel.maxreqs);
2828 seq_printf(m, "\nsession target slots:");
2829 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2830 seq_printf(m, " %u", ses->se_target_maxslots);
2831 spin_unlock(&clp->cl_lock);
2832 seq_puts(m, "\n");
2833
2834 drop_client(clp);
2835
2836 return 0;
2837 }
2838
2839 DEFINE_SHOW_ATTRIBUTE(client_info);
2840
states_start(struct seq_file * s,loff_t * pos)2841 static void *states_start(struct seq_file *s, loff_t *pos)
2842 __acquires(&clp->cl_lock)
2843 {
2844 struct nfs4_client *clp = s->private;
2845 unsigned long id = *pos;
2846 void *ret;
2847
2848 spin_lock(&clp->cl_lock);
2849 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2850 *pos = id;
2851 return ret;
2852 }
2853
states_next(struct seq_file * s,void * v,loff_t * pos)2854 static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2855 {
2856 struct nfs4_client *clp = s->private;
2857 unsigned long id = *pos;
2858 void *ret;
2859
2860 id = *pos;
2861 id++;
2862 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2863 *pos = id;
2864 return ret;
2865 }
2866
states_stop(struct seq_file * s,void * v)2867 static void states_stop(struct seq_file *s, void *v)
2868 __releases(&clp->cl_lock)
2869 {
2870 struct nfs4_client *clp = s->private;
2871
2872 spin_unlock(&clp->cl_lock);
2873 }
2874
nfs4_show_fname(struct seq_file * s,struct nfsd_file * f)2875 static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2876 {
2877 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2878 }
2879
nfs4_show_superblock(struct seq_file * s,struct nfsd_file * f)2880 static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2881 {
2882 struct inode *inode = file_inode(f->nf_file);
2883
2884 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2885 MAJOR(inode->i_sb->s_dev),
2886 MINOR(inode->i_sb->s_dev),
2887 inode->i_ino);
2888 }
2889
nfs4_show_owner(struct seq_file * s,struct nfs4_stateowner * oo)2890 static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2891 {
2892 seq_puts(s, "owner: ");
2893 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2894 }
2895
nfs4_show_stateid(struct seq_file * s,stateid_t * stid)2896 static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2897 {
2898 seq_printf(s, "0x%.8x", stid->si_generation);
2899 seq_printf(s, "%12phN", &stid->si_opaque);
2900 }
2901
nfs4_show_open(struct seq_file * s,struct nfs4_stid * st)2902 static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2903 {
2904 struct nfs4_ol_stateid *ols;
2905 struct nfs4_file *nf;
2906 struct nfsd_file *file;
2907 struct nfs4_stateowner *oo;
2908 unsigned int access, deny;
2909
2910 ols = openlockstateid(st);
2911 oo = ols->st_stateowner;
2912 nf = st->sc_file;
2913
2914 seq_puts(s, "- ");
2915 nfs4_show_stateid(s, &st->sc_stateid);
2916 seq_puts(s, ": { type: open, ");
2917
2918 access = bmap_to_share_mode(ols->st_access_bmap);
2919 deny = bmap_to_share_mode(ols->st_deny_bmap);
2920
2921 seq_printf(s, "access: %s%s, ",
2922 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2923 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2924 seq_printf(s, "deny: %s%s, ",
2925 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2926 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2927
2928 if (nf) {
2929 spin_lock(&nf->fi_lock);
2930 file = find_any_file_locked(nf);
2931 if (file) {
2932 nfs4_show_superblock(s, file);
2933 seq_puts(s, ", ");
2934 nfs4_show_fname(s, file);
2935 seq_puts(s, ", ");
2936 }
2937 spin_unlock(&nf->fi_lock);
2938 } else
2939 seq_puts(s, "closed, ");
2940 nfs4_show_owner(s, oo);
2941 if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
2942 seq_puts(s, ", admin-revoked");
2943 seq_puts(s, " }\n");
2944 return 0;
2945 }
2946
nfs4_show_lock(struct seq_file * s,struct nfs4_stid * st)2947 static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2948 {
2949 struct nfs4_ol_stateid *ols;
2950 struct nfs4_file *nf;
2951 struct nfsd_file *file;
2952 struct nfs4_stateowner *oo;
2953
2954 ols = openlockstateid(st);
2955 oo = ols->st_stateowner;
2956 nf = st->sc_file;
2957
2958 seq_puts(s, "- ");
2959 nfs4_show_stateid(s, &st->sc_stateid);
2960 seq_puts(s, ": { type: lock, ");
2961
2962 spin_lock(&nf->fi_lock);
2963 file = find_any_file_locked(nf);
2964 if (file) {
2965 /*
2966 * Note: a lock stateid isn't really the same thing as a lock,
2967 * it's the locking state held by one owner on a file, and there
2968 * may be multiple (or no) lock ranges associated with it.
2969 * (Same for the matter is true of open stateids.)
2970 */
2971
2972 nfs4_show_superblock(s, file);
2973 /* XXX: open stateid? */
2974 seq_puts(s, ", ");
2975 nfs4_show_fname(s, file);
2976 seq_puts(s, ", ");
2977 }
2978 nfs4_show_owner(s, oo);
2979 if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
2980 seq_puts(s, ", admin-revoked");
2981 seq_puts(s, " }\n");
2982 spin_unlock(&nf->fi_lock);
2983 return 0;
2984 }
2985
nfs4_show_deleg_type(u32 dl_type)2986 static char *nfs4_show_deleg_type(u32 dl_type)
2987 {
2988 switch (dl_type) {
2989 case OPEN_DELEGATE_READ:
2990 return "r";
2991 case OPEN_DELEGATE_WRITE:
2992 return "w";
2993 case OPEN_DELEGATE_READ_ATTRS_DELEG:
2994 return "ra";
2995 case OPEN_DELEGATE_WRITE_ATTRS_DELEG:
2996 return "wa";
2997 }
2998 return "?";
2999 }
3000
nfs4_show_deleg(struct seq_file * s,struct nfs4_stid * st)3001 static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
3002 {
3003 struct nfs4_delegation *ds;
3004 struct nfs4_file *nf;
3005 struct nfsd_file *file;
3006
3007 ds = delegstateid(st);
3008 nf = st->sc_file;
3009
3010 seq_puts(s, "- ");
3011 nfs4_show_stateid(s, &st->sc_stateid);
3012 seq_puts(s, ": { type: deleg, ");
3013
3014 seq_printf(s, "access: %s", nfs4_show_deleg_type(ds->dl_type));
3015
3016 /* XXX: lease time, whether it's being recalled. */
3017
3018 spin_lock(&nf->fi_lock);
3019 file = nf->fi_deleg_file;
3020 if (file) {
3021 seq_puts(s, ", ");
3022 nfs4_show_superblock(s, file);
3023 seq_puts(s, ", ");
3024 nfs4_show_fname(s, file);
3025 }
3026 spin_unlock(&nf->fi_lock);
3027 if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
3028 seq_puts(s, ", admin-revoked");
3029 seq_puts(s, " }\n");
3030 return 0;
3031 }
3032
nfs4_show_layout(struct seq_file * s,struct nfs4_stid * st)3033 static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
3034 {
3035 struct nfs4_layout_stateid *ls;
3036 struct nfsd_file *file;
3037
3038 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
3039
3040 seq_puts(s, "- ");
3041 nfs4_show_stateid(s, &st->sc_stateid);
3042 seq_puts(s, ": { type: layout");
3043
3044 /* XXX: What else would be useful? */
3045
3046 spin_lock(&ls->ls_stid.sc_file->fi_lock);
3047 file = ls->ls_file;
3048 if (file) {
3049 seq_puts(s, ", ");
3050 nfs4_show_superblock(s, file);
3051 seq_puts(s, ", ");
3052 nfs4_show_fname(s, file);
3053 }
3054 spin_unlock(&ls->ls_stid.sc_file->fi_lock);
3055 if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
3056 seq_puts(s, ", admin-revoked");
3057 seq_puts(s, " }\n");
3058
3059 return 0;
3060 }
3061
states_show(struct seq_file * s,void * v)3062 static int states_show(struct seq_file *s, void *v)
3063 {
3064 struct nfs4_stid *st = v;
3065
3066 switch (st->sc_type) {
3067 case SC_TYPE_OPEN:
3068 return nfs4_show_open(s, st);
3069 case SC_TYPE_LOCK:
3070 return nfs4_show_lock(s, st);
3071 case SC_TYPE_DELEG:
3072 return nfs4_show_deleg(s, st);
3073 case SC_TYPE_LAYOUT:
3074 return nfs4_show_layout(s, st);
3075 default:
3076 return 0; /* XXX: or SEQ_SKIP? */
3077 }
3078 /* XXX: copy stateids? */
3079 }
3080
3081 static struct seq_operations states_seq_ops = {
3082 .start = states_start,
3083 .next = states_next,
3084 .stop = states_stop,
3085 .show = states_show
3086 };
3087
client_states_open(struct inode * inode,struct file * file)3088 static int client_states_open(struct inode *inode, struct file *file)
3089 {
3090 struct seq_file *s;
3091 struct nfs4_client *clp;
3092 int ret;
3093
3094 clp = get_nfsdfs_clp(inode);
3095 if (!clp)
3096 return -ENXIO;
3097
3098 ret = seq_open(file, &states_seq_ops);
3099 if (ret)
3100 return ret;
3101 s = file->private_data;
3102 s->private = clp;
3103 return 0;
3104 }
3105
client_opens_release(struct inode * inode,struct file * file)3106 static int client_opens_release(struct inode *inode, struct file *file)
3107 {
3108 struct seq_file *m = file->private_data;
3109 struct nfs4_client *clp = m->private;
3110
3111 /* XXX: alternatively, we could get/drop in seq start/stop */
3112 drop_client(clp);
3113 return seq_release(inode, file);
3114 }
3115
3116 static const struct file_operations client_states_fops = {
3117 .open = client_states_open,
3118 .read = seq_read,
3119 .llseek = seq_lseek,
3120 .release = client_opens_release,
3121 };
3122
3123 /*
3124 * Normally we refuse to destroy clients that are in use, but here the
3125 * administrator is telling us to just do it. We also want to wait
3126 * so the caller has a guarantee that the client's locks are gone by
3127 * the time the write returns:
3128 */
force_expire_client(struct nfs4_client * clp)3129 static void force_expire_client(struct nfs4_client *clp)
3130 {
3131 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3132 bool already_expired;
3133
3134 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
3135
3136 spin_lock(&nn->client_lock);
3137 clp->cl_time = 0;
3138 spin_unlock(&nn->client_lock);
3139
3140 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
3141 spin_lock(&nn->client_lock);
3142 already_expired = list_empty(&clp->cl_lru);
3143 if (!already_expired)
3144 unhash_client_locked(clp);
3145 spin_unlock(&nn->client_lock);
3146
3147 if (!already_expired)
3148 expire_client(clp);
3149 else
3150 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
3151 }
3152
client_ctl_write(struct file * file,const char __user * buf,size_t size,loff_t * pos)3153 static ssize_t client_ctl_write(struct file *file, const char __user *buf,
3154 size_t size, loff_t *pos)
3155 {
3156 char *data;
3157 struct nfs4_client *clp;
3158
3159 data = simple_transaction_get(file, buf, size);
3160 if (IS_ERR(data))
3161 return PTR_ERR(data);
3162 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
3163 return -EINVAL;
3164 clp = get_nfsdfs_clp(file_inode(file));
3165 if (!clp)
3166 return -ENXIO;
3167 force_expire_client(clp);
3168 drop_client(clp);
3169 return 7;
3170 }
3171
3172 static const struct file_operations client_ctl_fops = {
3173 .write = client_ctl_write,
3174 .release = simple_transaction_release,
3175 };
3176
3177 static const struct tree_descr client_files[] = {
3178 [0] = {"info", &client_info_fops, S_IRUSR},
3179 [1] = {"states", &client_states_fops, S_IRUSR},
3180 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
3181 [3] = {""},
3182 };
3183
3184 static int
nfsd4_cb_recall_any_done(struct nfsd4_callback * cb,struct rpc_task * task)3185 nfsd4_cb_recall_any_done(struct nfsd4_callback *cb,
3186 struct rpc_task *task)
3187 {
3188 trace_nfsd_cb_recall_any_done(cb, task);
3189 switch (task->tk_status) {
3190 case -NFS4ERR_DELAY:
3191 rpc_delay(task, 2 * HZ);
3192 return 0;
3193 default:
3194 return 1;
3195 }
3196 }
3197
3198 static void
nfsd4_cb_recall_any_release(struct nfsd4_callback * cb)3199 nfsd4_cb_recall_any_release(struct nfsd4_callback *cb)
3200 {
3201 struct nfs4_client *clp = cb->cb_clp;
3202
3203 drop_client(clp);
3204 }
3205
3206 static int
nfsd4_cb_getattr_done(struct nfsd4_callback * cb,struct rpc_task * task)3207 nfsd4_cb_getattr_done(struct nfsd4_callback *cb, struct rpc_task *task)
3208 {
3209 struct nfs4_cb_fattr *ncf =
3210 container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
3211 struct nfs4_delegation *dp =
3212 container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
3213
3214 trace_nfsd_cb_getattr_done(&dp->dl_stid.sc_stateid, task);
3215 ncf->ncf_cb_status = task->tk_status;
3216 switch (task->tk_status) {
3217 case -NFS4ERR_DELAY:
3218 rpc_delay(task, 2 * HZ);
3219 return 0;
3220 default:
3221 return 1;
3222 }
3223 }
3224
3225 static void
nfsd4_cb_getattr_release(struct nfsd4_callback * cb)3226 nfsd4_cb_getattr_release(struct nfsd4_callback *cb)
3227 {
3228 struct nfs4_cb_fattr *ncf =
3229 container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
3230 struct nfs4_delegation *dp =
3231 container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
3232
3233 nfs4_put_stid(&dp->dl_stid);
3234 }
3235
3236 static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = {
3237 .done = nfsd4_cb_recall_any_done,
3238 .release = nfsd4_cb_recall_any_release,
3239 .opcode = OP_CB_RECALL_ANY,
3240 };
3241
3242 static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops = {
3243 .done = nfsd4_cb_getattr_done,
3244 .release = nfsd4_cb_getattr_release,
3245 .opcode = OP_CB_GETATTR,
3246 };
3247
nfs4_cb_getattr(struct nfs4_cb_fattr * ncf)3248 static void nfs4_cb_getattr(struct nfs4_cb_fattr *ncf)
3249 {
3250 struct nfs4_delegation *dp =
3251 container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
3252
3253 if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &ncf->ncf_getattr.cb_flags))
3254 return;
3255
3256 /* set to proper status when nfsd4_cb_getattr_done runs */
3257 ncf->ncf_cb_status = NFS4ERR_IO;
3258
3259 /* ensure that wake_bit is done when RUNNING is cleared */
3260 set_bit(NFSD4_CALLBACK_WAKE, &ncf->ncf_getattr.cb_flags);
3261
3262 refcount_inc(&dp->dl_stid.sc_count);
3263 nfsd4_run_cb(&ncf->ncf_getattr);
3264 }
3265
create_client(struct xdr_netobj name,struct svc_rqst * rqstp,nfs4_verifier * verf)3266 static struct nfs4_client *create_client(struct xdr_netobj name,
3267 struct svc_rqst *rqstp, nfs4_verifier *verf)
3268 {
3269 struct nfs4_client *clp;
3270 struct sockaddr *sa = svc_addr(rqstp);
3271 int ret;
3272 struct net *net = SVC_NET(rqstp);
3273 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3274 struct dentry *dentries[ARRAY_SIZE(client_files)];
3275
3276 clp = alloc_client(name, nn);
3277 if (clp == NULL)
3278 return NULL;
3279
3280 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
3281 if (ret) {
3282 free_client(clp);
3283 return NULL;
3284 }
3285 gen_clid(clp, nn);
3286 kref_init(&clp->cl_nfsdfs.cl_ref);
3287 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
3288 clp->cl_time = ktime_get_boottime_seconds();
3289 copy_verf(clp, verf);
3290 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
3291 clp->cl_cb_session = NULL;
3292 clp->net = net;
3293 clp->cl_nfsd_dentry = nfsd_client_mkdir(
3294 nn, &clp->cl_nfsdfs,
3295 clp->cl_clientid.cl_id - nn->clientid_base,
3296 client_files, dentries);
3297 clp->cl_nfsd_info_dentry = dentries[0];
3298 if (!clp->cl_nfsd_dentry) {
3299 free_client(clp);
3300 return NULL;
3301 }
3302 clp->cl_ra = kzalloc(sizeof(*clp->cl_ra), GFP_KERNEL);
3303 if (!clp->cl_ra) {
3304 free_client(clp);
3305 return NULL;
3306 }
3307 clp->cl_ra_time = 0;
3308 nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops,
3309 NFSPROC4_CLNT_CB_RECALL_ANY);
3310 return clp;
3311 }
3312
3313 static void
add_clp_to_name_tree(struct nfs4_client * new_clp,struct rb_root * root)3314 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
3315 {
3316 struct rb_node **new = &(root->rb_node), *parent = NULL;
3317 struct nfs4_client *clp;
3318
3319 while (*new) {
3320 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
3321 parent = *new;
3322
3323 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
3324 new = &((*new)->rb_left);
3325 else
3326 new = &((*new)->rb_right);
3327 }
3328
3329 rb_link_node(&new_clp->cl_namenode, parent, new);
3330 rb_insert_color(&new_clp->cl_namenode, root);
3331 }
3332
3333 static struct nfs4_client *
find_clp_in_name_tree(struct xdr_netobj * name,struct rb_root * root)3334 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
3335 {
3336 int cmp;
3337 struct rb_node *node = root->rb_node;
3338 struct nfs4_client *clp;
3339
3340 while (node) {
3341 clp = rb_entry(node, struct nfs4_client, cl_namenode);
3342 cmp = compare_blob(&clp->cl_name, name);
3343 if (cmp > 0)
3344 node = node->rb_left;
3345 else if (cmp < 0)
3346 node = node->rb_right;
3347 else
3348 return clp;
3349 }
3350 return NULL;
3351 }
3352
3353 static void
add_to_unconfirmed(struct nfs4_client * clp)3354 add_to_unconfirmed(struct nfs4_client *clp)
3355 {
3356 unsigned int idhashval;
3357 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3358
3359 lockdep_assert_held(&nn->client_lock);
3360
3361 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3362 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
3363 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3364 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
3365 renew_client_locked(clp);
3366 }
3367
3368 static void
move_to_confirmed(struct nfs4_client * clp)3369 move_to_confirmed(struct nfs4_client *clp)
3370 {
3371 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
3372 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3373
3374 lockdep_assert_held(&nn->client_lock);
3375
3376 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
3377 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
3378 add_clp_to_name_tree(clp, &nn->conf_name_tree);
3379 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3380 trace_nfsd_clid_confirmed(&clp->cl_clientid);
3381 renew_client_locked(clp);
3382 }
3383
3384 static struct nfs4_client *
find_client_in_id_table(struct list_head * tbl,clientid_t * clid,bool sessions)3385 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
3386 {
3387 struct nfs4_client *clp;
3388 unsigned int idhashval = clientid_hashval(clid->cl_id);
3389
3390 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
3391 if (same_clid(&clp->cl_clientid, clid)) {
3392 if ((bool)clp->cl_minorversion != sessions)
3393 return NULL;
3394 renew_client_locked(clp);
3395 return clp;
3396 }
3397 }
3398 return NULL;
3399 }
3400
3401 static struct nfs4_client *
find_confirmed_client(clientid_t * clid,bool sessions,struct nfsd_net * nn)3402 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3403 {
3404 struct list_head *tbl = nn->conf_id_hashtbl;
3405
3406 lockdep_assert_held(&nn->client_lock);
3407 return find_client_in_id_table(tbl, clid, sessions);
3408 }
3409
3410 static struct nfs4_client *
find_unconfirmed_client(clientid_t * clid,bool sessions,struct nfsd_net * nn)3411 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3412 {
3413 struct list_head *tbl = nn->unconf_id_hashtbl;
3414
3415 lockdep_assert_held(&nn->client_lock);
3416 return find_client_in_id_table(tbl, clid, sessions);
3417 }
3418
clp_used_exchangeid(struct nfs4_client * clp)3419 static bool clp_used_exchangeid(struct nfs4_client *clp)
3420 {
3421 return clp->cl_exchange_flags != 0;
3422 }
3423
3424 static struct nfs4_client *
find_confirmed_client_by_name(struct xdr_netobj * name,struct nfsd_net * nn)3425 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3426 {
3427 lockdep_assert_held(&nn->client_lock);
3428 return find_clp_in_name_tree(name, &nn->conf_name_tree);
3429 }
3430
3431 static struct nfs4_client *
find_unconfirmed_client_by_name(struct xdr_netobj * name,struct nfsd_net * nn)3432 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
3433 {
3434 lockdep_assert_held(&nn->client_lock);
3435 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
3436 }
3437
3438 static void
gen_callback(struct nfs4_client * clp,struct nfsd4_setclientid * se,struct svc_rqst * rqstp)3439 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
3440 {
3441 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
3442 struct sockaddr *sa = svc_addr(rqstp);
3443 u32 scopeid = rpc_get_scope_id(sa);
3444 unsigned short expected_family;
3445
3446 /* Currently, we only support tcp and tcp6 for the callback channel */
3447 if (se->se_callback_netid_len == 3 &&
3448 !memcmp(se->se_callback_netid_val, "tcp", 3))
3449 expected_family = AF_INET;
3450 else if (se->se_callback_netid_len == 4 &&
3451 !memcmp(se->se_callback_netid_val, "tcp6", 4))
3452 expected_family = AF_INET6;
3453 else
3454 goto out_err;
3455
3456 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3457 se->se_callback_addr_len,
3458 (struct sockaddr *)&conn->cb_addr,
3459 sizeof(conn->cb_addr));
3460
3461 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
3462 goto out_err;
3463
3464 if (conn->cb_addr.ss_family == AF_INET6)
3465 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3466
3467 conn->cb_prog = se->se_callback_prog;
3468 conn->cb_ident = se->se_callback_ident;
3469 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
3470 trace_nfsd_cb_args(clp, conn);
3471 return;
3472 out_err:
3473 conn->cb_addr.ss_family = AF_UNSPEC;
3474 conn->cb_addrlen = 0;
3475 trace_nfsd_cb_nodelegs(clp);
3476 return;
3477 }
3478
3479 /*
3480 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3481 */
3482 static void
nfsd4_store_cache_entry(struct nfsd4_compoundres * resp)3483 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3484 {
3485 struct xdr_buf *buf = resp->xdr->buf;
3486 struct nfsd4_slot *slot = resp->cstate.slot;
3487 unsigned int base;
3488
3489 dprintk("--> %s slot %p\n", __func__, slot);
3490
3491 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3492 slot->sl_opcnt = resp->opcnt;
3493 slot->sl_status = resp->cstate.status;
3494 free_svc_cred(&slot->sl_cred);
3495 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3496
3497 if (!nfsd4_cache_this(resp)) {
3498 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3499 return;
3500 }
3501 slot->sl_flags |= NFSD4_SLOT_CACHED;
3502
3503 base = resp->cstate.data_offset;
3504 slot->sl_datalen = buf->len - base;
3505 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3506 WARN(1, "%s: sessions DRC could not cache compound\n",
3507 __func__);
3508 return;
3509 }
3510
3511 /*
3512 * Encode the replay sequence operation from the slot values.
3513 * If cachethis is FALSE encode the uncached rep error on the next
3514 * operation which sets resp->p and increments resp->opcnt for
3515 * nfs4svc_encode_compoundres.
3516 *
3517 */
3518 static __be32
nfsd4_enc_sequence_replay(struct nfsd4_compoundargs * args,struct nfsd4_compoundres * resp)3519 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3520 struct nfsd4_compoundres *resp)
3521 {
3522 struct nfsd4_op *op;
3523 struct nfsd4_slot *slot = resp->cstate.slot;
3524
3525 /* Encode the replayed sequence operation */
3526 op = &args->ops[resp->opcnt - 1];
3527 nfsd4_encode_operation(resp, op);
3528
3529 if (slot->sl_flags & NFSD4_SLOT_CACHED)
3530 return op->status;
3531 if (args->opcnt == 1) {
3532 /*
3533 * The original operation wasn't a solo sequence--we
3534 * always cache those--so this retry must not match the
3535 * original:
3536 */
3537 op->status = nfserr_seq_false_retry;
3538 } else {
3539 op = &args->ops[resp->opcnt++];
3540 op->status = nfserr_retry_uncached_rep;
3541 nfsd4_encode_operation(resp, op);
3542 }
3543 return op->status;
3544 }
3545
3546 /*
3547 * The sequence operation is not cached because we can use the slot and
3548 * session values.
3549 */
3550 static __be32
nfsd4_replay_cache_entry(struct nfsd4_compoundres * resp,struct nfsd4_sequence * seq)3551 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3552 struct nfsd4_sequence *seq)
3553 {
3554 struct nfsd4_slot *slot = resp->cstate.slot;
3555 struct xdr_stream *xdr = resp->xdr;
3556 __be32 *p;
3557 __be32 status;
3558
3559 dprintk("--> %s slot %p\n", __func__, slot);
3560
3561 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
3562 if (status)
3563 return status;
3564
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 __be32
nfsd4_sequence(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4353 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4354 union nfsd4_op_u *u)
4355 {
4356 struct nfsd4_sequence *seq = &u->sequence;
4357 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4358 struct xdr_stream *xdr = resp->xdr;
4359 struct nfsd4_session *session;
4360 struct nfs4_client *clp;
4361 struct nfsd4_slot *slot;
4362 struct nfsd4_conn *conn;
4363 __be32 status;
4364 int buflen;
4365 struct net *net = SVC_NET(rqstp);
4366 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4367
4368 if (resp->opcnt != 1)
4369 return nfserr_sequence_pos;
4370
4371 /*
4372 * Will be either used or freed by nfsd4_sequence_check_conn
4373 * below.
4374 */
4375 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
4376 if (!conn)
4377 return nfserr_jukebox;
4378
4379 spin_lock(&nn->client_lock);
4380 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
4381 if (!session)
4382 goto out_no_session;
4383 clp = session->se_client;
4384
4385 status = nfserr_too_many_ops;
4386 if (nfsd4_session_too_many_ops(rqstp, session))
4387 goto out_put_session;
4388
4389 status = nfserr_req_too_big;
4390 if (nfsd4_request_too_big(rqstp, session))
4391 goto out_put_session;
4392
4393 status = nfserr_badslot;
4394 if (seq->slotid >= session->se_fchannel.maxreqs)
4395 goto out_put_session;
4396
4397 slot = xa_load(&session->se_slots, seq->slotid);
4398 dprintk("%s: slotid %d\n", __func__, seq->slotid);
4399
4400 trace_nfsd_slot_seqid_sequence(clp, seq, slot);
4401 status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_flags);
4402 if (status == nfserr_replay_cache) {
4403 status = nfserr_seq_misordered;
4404 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
4405 goto out_put_session;
4406 status = nfserr_seq_false_retry;
4407 if (!replay_matches_cache(rqstp, seq, slot))
4408 goto out_put_session;
4409 cstate->slot = slot;
4410 cstate->session = session;
4411 cstate->clp = clp;
4412 /* Return the cached reply status and set cstate->status
4413 * for nfsd4_proc_compound processing */
4414 status = nfsd4_replay_cache_entry(resp, seq);
4415 cstate->status = nfserr_replay_cache;
4416 goto out;
4417 }
4418 if (status)
4419 goto out_put_session;
4420
4421 status = nfsd4_sequence_check_conn(conn, session);
4422 conn = NULL;
4423 if (status)
4424 goto out_put_session;
4425
4426 if (session->se_target_maxslots < session->se_fchannel.maxreqs &&
4427 slot->sl_generation == session->se_slot_gen &&
4428 seq->maxslots <= session->se_target_maxslots)
4429 /* Client acknowledged our reduce maxreqs */
4430 free_session_slots(session, session->se_target_maxslots);
4431
4432 buflen = (seq->cachethis) ?
4433 session->se_fchannel.maxresp_cached :
4434 session->se_fchannel.maxresp_sz;
4435 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
4436 nfserr_rep_too_big;
4437 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
4438 goto out_put_session;
4439 svc_reserve_auth(rqstp, buflen);
4440
4441 status = nfs_ok;
4442 /* Success! accept new slot seqid */
4443 slot->sl_seqid = seq->seqid;
4444 slot->sl_flags &= ~NFSD4_SLOT_REUSED;
4445 slot->sl_flags |= NFSD4_SLOT_INUSE;
4446 slot->sl_generation = session->se_slot_gen;
4447 if (seq->cachethis)
4448 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
4449 else
4450 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
4451
4452 cstate->slot = slot;
4453 cstate->session = session;
4454 cstate->clp = clp;
4455
4456 /*
4457 * If the client ever uses the highest available slot,
4458 * gently try to allocate another 20%. This allows
4459 * fairly quick growth without grossly over-shooting what
4460 * the client might use.
4461 */
4462 if (seq->slotid == session->se_fchannel.maxreqs - 1 &&
4463 session->se_target_maxslots >= session->se_fchannel.maxreqs &&
4464 session->se_fchannel.maxreqs < NFSD_MAX_SLOTS_PER_SESSION) {
4465 int s = session->se_fchannel.maxreqs;
4466 int cnt = DIV_ROUND_UP(s, 5);
4467 void *prev_slot;
4468
4469 do {
4470 /*
4471 * GFP_NOWAIT both allows allocation under a
4472 * spinlock, and only succeeds if there is
4473 * plenty of memory.
4474 */
4475 slot = nfsd4_alloc_slot(&session->se_fchannel, s,
4476 GFP_NOWAIT);
4477 prev_slot = xa_load(&session->se_slots, s);
4478 if (xa_is_value(prev_slot) && slot) {
4479 slot->sl_seqid = xa_to_value(prev_slot);
4480 slot->sl_flags |= NFSD4_SLOT_REUSED;
4481 }
4482 if (slot &&
4483 !xa_is_err(xa_store(&session->se_slots, s, slot,
4484 GFP_NOWAIT))) {
4485 s += 1;
4486 session->se_fchannel.maxreqs = s;
4487 atomic_add(s - session->se_target_maxslots,
4488 &nfsd_total_target_slots);
4489 session->se_target_maxslots = s;
4490 } else {
4491 kfree(slot);
4492 slot = NULL;
4493 }
4494 } while (slot && --cnt > 0);
4495 }
4496
4497 out:
4498 seq->maxslots = max(session->se_target_maxslots, seq->maxslots);
4499 seq->target_maxslots = session->se_target_maxslots;
4500
4501 switch (clp->cl_cb_state) {
4502 case NFSD4_CB_DOWN:
4503 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
4504 break;
4505 case NFSD4_CB_FAULT:
4506 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
4507 break;
4508 default:
4509 seq->status_flags = 0;
4510 }
4511 if (!list_empty(&clp->cl_revoked))
4512 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
4513 if (atomic_read(&clp->cl_admin_revoked))
4514 seq->status_flags |= SEQ4_STATUS_ADMIN_STATE_REVOKED;
4515 trace_nfsd_seq4_status(rqstp, seq);
4516 out_no_session:
4517 if (conn)
4518 free_conn(conn);
4519 spin_unlock(&nn->client_lock);
4520 return status;
4521 out_put_session:
4522 nfsd4_put_session_locked(session);
4523 goto out_no_session;
4524 }
4525
4526 void
nfsd4_sequence_done(struct nfsd4_compoundres * resp)4527 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4528 {
4529 struct nfsd4_compound_state *cs = &resp->cstate;
4530
4531 if (nfsd4_has_session(cs)) {
4532 if (cs->status != nfserr_replay_cache) {
4533 nfsd4_store_cache_entry(resp);
4534 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4535 }
4536 /* Drop session reference that was taken in nfsd4_sequence() */
4537 nfsd4_put_session(cs->session);
4538 } else if (cs->clp)
4539 put_client_renew(cs->clp);
4540 }
4541
4542 __be32
nfsd4_destroy_clientid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4543 nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4544 struct nfsd4_compound_state *cstate,
4545 union nfsd4_op_u *u)
4546 {
4547 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
4548 struct nfs4_client *conf, *unconf;
4549 struct nfs4_client *clp = NULL;
4550 __be32 status = 0;
4551 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4552
4553 spin_lock(&nn->client_lock);
4554 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
4555 conf = find_confirmed_client(&dc->clientid, true, nn);
4556 WARN_ON_ONCE(conf && unconf);
4557
4558 if (conf) {
4559 if (client_has_state(conf)) {
4560 status = nfserr_clientid_busy;
4561 goto out;
4562 }
4563 status = mark_client_expired_locked(conf);
4564 if (status)
4565 goto out;
4566 clp = conf;
4567 } else if (unconf)
4568 clp = unconf;
4569 else {
4570 status = nfserr_stale_clientid;
4571 goto out;
4572 }
4573 if (!nfsd4_mach_creds_match(clp, rqstp)) {
4574 clp = NULL;
4575 status = nfserr_wrong_cred;
4576 goto out;
4577 }
4578 trace_nfsd_clid_destroyed(&clp->cl_clientid);
4579 unhash_client_locked(clp);
4580 out:
4581 spin_unlock(&nn->client_lock);
4582 if (clp)
4583 expire_client(clp);
4584 return status;
4585 }
4586
4587 __be32
nfsd4_reclaim_complete(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4588 nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4589 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
4590 {
4591 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
4592 struct nfs4_client *clp = cstate->clp;
4593 __be32 status = 0;
4594
4595 if (rc->rca_one_fs) {
4596 if (!cstate->current_fh.fh_dentry)
4597 return nfserr_nofilehandle;
4598 /*
4599 * We don't take advantage of the rca_one_fs case.
4600 * That's OK, it's optional, we can safely ignore it.
4601 */
4602 return nfs_ok;
4603 }
4604
4605 status = nfserr_complete_already;
4606 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4607 goto out;
4608
4609 status = nfserr_stale_clientid;
4610 if (is_client_expired(clp))
4611 /*
4612 * The following error isn't really legal.
4613 * But we only get here if the client just explicitly
4614 * destroyed the client. Surely it no longer cares what
4615 * error it gets back on an operation for the dead
4616 * client.
4617 */
4618 goto out;
4619
4620 status = nfs_ok;
4621 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4622 nfsd4_client_record_create(clp);
4623 inc_reclaim_complete(clp);
4624 out:
4625 return status;
4626 }
4627
4628 __be32
nfsd4_setclientid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4629 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4630 union nfsd4_op_u *u)
4631 {
4632 struct nfsd4_setclientid *setclid = &u->setclientid;
4633 struct xdr_netobj clname = setclid->se_name;
4634 nfs4_verifier clverifier = setclid->se_verf;
4635 struct nfs4_client *conf, *new;
4636 struct nfs4_client *unconf = NULL;
4637 __be32 status;
4638 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4639
4640 new = create_client(clname, rqstp, &clverifier);
4641 if (new == NULL)
4642 return nfserr_jukebox;
4643 spin_lock(&nn->client_lock);
4644 conf = find_confirmed_client_by_name(&clname, nn);
4645 if (conf && client_has_state(conf)) {
4646 status = nfserr_clid_inuse;
4647 if (clp_used_exchangeid(conf))
4648 goto out;
4649 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4650 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4651 goto out;
4652 }
4653 }
4654 unconf = find_unconfirmed_client_by_name(&clname, nn);
4655 if (unconf)
4656 unhash_client_locked(unconf);
4657 if (conf) {
4658 if (same_verf(&conf->cl_verifier, &clverifier)) {
4659 copy_clid(new, conf);
4660 gen_confirm(new, nn);
4661 } else
4662 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4663 &clverifier);
4664 } else
4665 trace_nfsd_clid_fresh(new);
4666 new->cl_minorversion = 0;
4667 gen_callback(new, setclid, rqstp);
4668 add_to_unconfirmed(new);
4669 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4670 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4671 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
4672 new = NULL;
4673 status = nfs_ok;
4674 out:
4675 spin_unlock(&nn->client_lock);
4676 if (new)
4677 free_client(new);
4678 if (unconf) {
4679 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
4680 expire_client(unconf);
4681 }
4682 return status;
4683 }
4684
4685 __be32
nfsd4_setclientid_confirm(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4686 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4687 struct nfsd4_compound_state *cstate,
4688 union nfsd4_op_u *u)
4689 {
4690 struct nfsd4_setclientid_confirm *setclientid_confirm =
4691 &u->setclientid_confirm;
4692 struct nfs4_client *conf, *unconf;
4693 struct nfs4_client *old = NULL;
4694 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4695 clientid_t * clid = &setclientid_confirm->sc_clientid;
4696 __be32 status;
4697 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4698
4699 if (STALE_CLIENTID(clid, nn))
4700 return nfserr_stale_clientid;
4701
4702 spin_lock(&nn->client_lock);
4703 conf = find_confirmed_client(clid, false, nn);
4704 unconf = find_unconfirmed_client(clid, false, nn);
4705 /*
4706 * We try hard to give out unique clientid's, so if we get an
4707 * attempt to confirm the same clientid with a different cred,
4708 * the client may be buggy; this should never happen.
4709 *
4710 * Nevertheless, RFC 7530 recommends INUSE for this case:
4711 */
4712 status = nfserr_clid_inuse;
4713 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4714 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
4715 goto out;
4716 }
4717 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4718 trace_nfsd_clid_cred_mismatch(conf, rqstp);
4719 goto out;
4720 }
4721 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4722 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4723 status = nfs_ok;
4724 } else
4725 status = nfserr_stale_clientid;
4726 goto out;
4727 }
4728 status = nfs_ok;
4729 if (conf) {
4730 if (get_client_locked(conf) == nfs_ok) {
4731 old = unconf;
4732 unhash_client_locked(old);
4733 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4734 } else {
4735 conf = NULL;
4736 }
4737 }
4738
4739 if (!conf) {
4740 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4741 if (old) {
4742 status = nfserr_clid_inuse;
4743 if (client_has_state(old)
4744 && !same_creds(&unconf->cl_cred,
4745 &old->cl_cred)) {
4746 old = NULL;
4747 goto out;
4748 }
4749 status = mark_client_expired_locked(old);
4750 if (status) {
4751 old = NULL;
4752 goto out;
4753 }
4754 trace_nfsd_clid_replaced(&old->cl_clientid);
4755 }
4756 status = get_client_locked(unconf);
4757 if (status != nfs_ok) {
4758 old = NULL;
4759 goto out;
4760 }
4761 move_to_confirmed(unconf);
4762 conf = unconf;
4763 }
4764 spin_unlock(&nn->client_lock);
4765 if (conf == unconf)
4766 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4767 nfsd4_probe_callback(conf);
4768 spin_lock(&nn->client_lock);
4769 put_client_renew_locked(conf);
4770 out:
4771 spin_unlock(&nn->client_lock);
4772 if (old)
4773 expire_client(old);
4774 return status;
4775 }
4776
nfsd4_alloc_file(void)4777 static struct nfs4_file *nfsd4_alloc_file(void)
4778 {
4779 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4780 }
4781
4782 /* OPEN Share state helper functions */
4783
nfsd4_file_init(const struct svc_fh * fh,struct nfs4_file * fp)4784 static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp)
4785 {
4786 refcount_set(&fp->fi_ref, 1);
4787 spin_lock_init(&fp->fi_lock);
4788 INIT_LIST_HEAD(&fp->fi_stateids);
4789 INIT_LIST_HEAD(&fp->fi_delegations);
4790 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4791 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
4792 fp->fi_deleg_file = NULL;
4793 fp->fi_rdeleg_file = NULL;
4794 fp->fi_had_conflict = false;
4795 fp->fi_share_deny = 0;
4796 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4797 memset(fp->fi_access, 0, sizeof(fp->fi_access));
4798 fp->fi_aliased = false;
4799 fp->fi_inode = d_inode(fh->fh_dentry);
4800 #ifdef CONFIG_NFSD_PNFS
4801 INIT_LIST_HEAD(&fp->fi_lo_states);
4802 atomic_set(&fp->fi_lo_recalls, 0);
4803 #endif
4804 }
4805
4806 void
nfsd4_free_slabs(void)4807 nfsd4_free_slabs(void)
4808 {
4809 kmem_cache_destroy(client_slab);
4810 kmem_cache_destroy(openowner_slab);
4811 kmem_cache_destroy(lockowner_slab);
4812 kmem_cache_destroy(file_slab);
4813 kmem_cache_destroy(stateid_slab);
4814 kmem_cache_destroy(deleg_slab);
4815 kmem_cache_destroy(odstate_slab);
4816 }
4817
4818 int
nfsd4_init_slabs(void)4819 nfsd4_init_slabs(void)
4820 {
4821 client_slab = KMEM_CACHE(nfs4_client, 0);
4822 if (client_slab == NULL)
4823 goto out;
4824 openowner_slab = KMEM_CACHE(nfs4_openowner, 0);
4825 if (openowner_slab == NULL)
4826 goto out_free_client_slab;
4827 lockowner_slab = KMEM_CACHE(nfs4_lockowner, 0);
4828 if (lockowner_slab == NULL)
4829 goto out_free_openowner_slab;
4830 file_slab = KMEM_CACHE(nfs4_file, 0);
4831 if (file_slab == NULL)
4832 goto out_free_lockowner_slab;
4833 stateid_slab = KMEM_CACHE(nfs4_ol_stateid, 0);
4834 if (stateid_slab == NULL)
4835 goto out_free_file_slab;
4836 deleg_slab = KMEM_CACHE(nfs4_delegation, 0);
4837 if (deleg_slab == NULL)
4838 goto out_free_stateid_slab;
4839 odstate_slab = KMEM_CACHE(nfs4_clnt_odstate, 0);
4840 if (odstate_slab == NULL)
4841 goto out_free_deleg_slab;
4842 return 0;
4843
4844 out_free_deleg_slab:
4845 kmem_cache_destroy(deleg_slab);
4846 out_free_stateid_slab:
4847 kmem_cache_destroy(stateid_slab);
4848 out_free_file_slab:
4849 kmem_cache_destroy(file_slab);
4850 out_free_lockowner_slab:
4851 kmem_cache_destroy(lockowner_slab);
4852 out_free_openowner_slab:
4853 kmem_cache_destroy(openowner_slab);
4854 out_free_client_slab:
4855 kmem_cache_destroy(client_slab);
4856 out:
4857 return -ENOMEM;
4858 }
4859
4860 static unsigned long
nfsd4_state_shrinker_count(struct shrinker * shrink,struct shrink_control * sc)4861 nfsd4_state_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
4862 {
4863 struct nfsd_net *nn = shrink->private_data;
4864 long count;
4865
4866 count = atomic_read(&nn->nfsd_courtesy_clients);
4867 if (!count)
4868 count = atomic_long_read(&num_delegations);
4869 if (count)
4870 queue_work(laundry_wq, &nn->nfsd_shrinker_work);
4871 return (unsigned long)count;
4872 }
4873
4874 static unsigned long
nfsd4_state_shrinker_scan(struct shrinker * shrink,struct shrink_control * sc)4875 nfsd4_state_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
4876 {
4877 return SHRINK_STOP;
4878 }
4879
4880 void
nfsd4_init_leases_net(struct nfsd_net * nn)4881 nfsd4_init_leases_net(struct nfsd_net *nn)
4882 {
4883 struct sysinfo si;
4884 u64 max_clients;
4885
4886 nn->nfsd4_lease = 90; /* default lease time */
4887 nn->nfsd4_grace = 90;
4888 nn->somebody_reclaimed = false;
4889 nn->track_reclaim_completes = false;
4890 nn->clverifier_counter = get_random_u32();
4891 nn->clientid_base = get_random_u32();
4892 nn->clientid_counter = nn->clientid_base + 1;
4893 nn->s2s_cp_cl_id = nn->clientid_counter++;
4894
4895 atomic_set(&nn->nfs4_client_count, 0);
4896 si_meminfo(&si);
4897 max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
4898 max_clients *= NFS4_CLIENTS_PER_GB;
4899 nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
4900
4901 atomic_set(&nn->nfsd_courtesy_clients, 0);
4902 }
4903
4904 enum rp_lock {
4905 RP_UNLOCKED,
4906 RP_LOCKED,
4907 RP_UNHASHED,
4908 };
4909
init_nfs4_replay(struct nfs4_replay * rp)4910 static void init_nfs4_replay(struct nfs4_replay *rp)
4911 {
4912 rp->rp_status = nfserr_serverfault;
4913 rp->rp_buflen = 0;
4914 rp->rp_buf = rp->rp_ibuf;
4915 rp->rp_locked = RP_UNLOCKED;
4916 }
4917
nfsd4_cstate_assign_replay(struct nfsd4_compound_state * cstate,struct nfs4_stateowner * so)4918 static int nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4919 struct nfs4_stateowner *so)
4920 {
4921 if (!nfsd4_has_session(cstate)) {
4922 wait_var_event(&so->so_replay.rp_locked,
4923 cmpxchg(&so->so_replay.rp_locked,
4924 RP_UNLOCKED, RP_LOCKED) != RP_LOCKED);
4925 if (so->so_replay.rp_locked == RP_UNHASHED)
4926 return -EAGAIN;
4927 cstate->replay_owner = nfs4_get_stateowner(so);
4928 }
4929 return 0;
4930 }
4931
nfsd4_cstate_clear_replay(struct nfsd4_compound_state * cstate)4932 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4933 {
4934 struct nfs4_stateowner *so = cstate->replay_owner;
4935
4936 if (so != NULL) {
4937 cstate->replay_owner = NULL;
4938 store_release_wake_up(&so->so_replay.rp_locked, RP_UNLOCKED);
4939 nfs4_put_stateowner(so);
4940 }
4941 }
4942
alloc_stateowner(struct kmem_cache * slab,struct xdr_netobj * owner,struct nfs4_client * clp)4943 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4944 {
4945 struct nfs4_stateowner *sop;
4946
4947 sop = kmem_cache_alloc(slab, GFP_KERNEL);
4948 if (!sop)
4949 return NULL;
4950
4951 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4952 if (!sop->so_owner.data) {
4953 kmem_cache_free(slab, sop);
4954 return NULL;
4955 }
4956
4957 INIT_LIST_HEAD(&sop->so_stateids);
4958 sop->so_client = clp;
4959 init_nfs4_replay(&sop->so_replay);
4960 atomic_set(&sop->so_count, 1);
4961 return sop;
4962 }
4963
hash_openowner(struct nfs4_openowner * oo,struct nfs4_client * clp,unsigned int strhashval)4964 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4965 {
4966 lockdep_assert_held(&clp->cl_lock);
4967
4968 list_add(&oo->oo_owner.so_strhash,
4969 &clp->cl_ownerstr_hashtbl[strhashval]);
4970 list_add(&oo->oo_perclient, &clp->cl_openowners);
4971 }
4972
nfs4_unhash_openowner(struct nfs4_stateowner * so)4973 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4974 {
4975 unhash_openowner_locked(openowner(so));
4976 }
4977
nfs4_free_openowner(struct nfs4_stateowner * so)4978 static void nfs4_free_openowner(struct nfs4_stateowner *so)
4979 {
4980 struct nfs4_openowner *oo = openowner(so);
4981
4982 kmem_cache_free(openowner_slab, oo);
4983 }
4984
4985 static const struct nfs4_stateowner_operations openowner_ops = {
4986 .so_unhash = nfs4_unhash_openowner,
4987 .so_free = nfs4_free_openowner,
4988 };
4989
4990 static struct nfs4_ol_stateid *
nfsd4_find_existing_open(struct nfs4_file * fp,struct nfsd4_open * open)4991 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4992 {
4993 struct nfs4_ol_stateid *local, *ret = NULL;
4994 struct nfs4_openowner *oo = open->op_openowner;
4995
4996 lockdep_assert_held(&fp->fi_lock);
4997
4998 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4999 /* ignore lock owners */
5000 if (local->st_stateowner->so_is_open_owner == 0)
5001 continue;
5002 if (local->st_stateowner != &oo->oo_owner)
5003 continue;
5004 if (local->st_stid.sc_type == SC_TYPE_OPEN &&
5005 !local->st_stid.sc_status) {
5006 ret = local;
5007 refcount_inc(&ret->st_stid.sc_count);
5008 break;
5009 }
5010 }
5011 return ret;
5012 }
5013
nfsd4_drop_revoked_stid(struct nfs4_stid * s)5014 static void nfsd4_drop_revoked_stid(struct nfs4_stid *s)
5015 __releases(&s->sc_client->cl_lock)
5016 {
5017 struct nfs4_client *cl = s->sc_client;
5018 LIST_HEAD(reaplist);
5019 struct nfs4_ol_stateid *stp;
5020 struct nfs4_delegation *dp;
5021 bool unhashed;
5022
5023 switch (s->sc_type) {
5024 case SC_TYPE_OPEN:
5025 stp = openlockstateid(s);
5026 if (unhash_open_stateid(stp, &reaplist))
5027 put_ol_stateid_locked(stp, &reaplist);
5028 spin_unlock(&cl->cl_lock);
5029 free_ol_stateid_reaplist(&reaplist);
5030 break;
5031 case SC_TYPE_LOCK:
5032 stp = openlockstateid(s);
5033 unhashed = unhash_lock_stateid(stp);
5034 spin_unlock(&cl->cl_lock);
5035 if (unhashed)
5036 nfs4_put_stid(s);
5037 break;
5038 case SC_TYPE_DELEG:
5039 dp = delegstateid(s);
5040 list_del_init(&dp->dl_recall_lru);
5041 spin_unlock(&cl->cl_lock);
5042 nfs4_put_stid(s);
5043 break;
5044 default:
5045 spin_unlock(&cl->cl_lock);
5046 }
5047 }
5048
nfsd40_drop_revoked_stid(struct nfs4_client * cl,stateid_t * stid)5049 static void nfsd40_drop_revoked_stid(struct nfs4_client *cl,
5050 stateid_t *stid)
5051 {
5052 /* NFSv4.0 has no way for the client to tell the server
5053 * that it can forget an admin-revoked stateid.
5054 * So we keep it around until the first time that the
5055 * client uses it, and drop it the first time
5056 * nfserr_admin_revoked is returned.
5057 * For v4.1 and later we wait until explicitly told
5058 * to free the stateid.
5059 */
5060 if (cl->cl_minorversion == 0) {
5061 struct nfs4_stid *st;
5062
5063 spin_lock(&cl->cl_lock);
5064 st = find_stateid_locked(cl, stid);
5065 if (st)
5066 nfsd4_drop_revoked_stid(st);
5067 else
5068 spin_unlock(&cl->cl_lock);
5069 }
5070 }
5071
5072 static __be32
nfsd4_verify_open_stid(struct nfs4_stid * s)5073 nfsd4_verify_open_stid(struct nfs4_stid *s)
5074 {
5075 __be32 ret = nfs_ok;
5076
5077 if (s->sc_status & SC_STATUS_ADMIN_REVOKED)
5078 ret = nfserr_admin_revoked;
5079 else if (s->sc_status & SC_STATUS_REVOKED)
5080 ret = nfserr_deleg_revoked;
5081 else if (s->sc_status & SC_STATUS_CLOSED)
5082 ret = nfserr_bad_stateid;
5083 return ret;
5084 }
5085
5086 /* Lock the stateid st_mutex, and deal with races with CLOSE */
5087 static __be32
nfsd4_lock_ol_stateid(struct nfs4_ol_stateid * stp)5088 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
5089 {
5090 __be32 ret;
5091
5092 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
5093 ret = nfsd4_verify_open_stid(&stp->st_stid);
5094 if (ret == nfserr_admin_revoked)
5095 nfsd40_drop_revoked_stid(stp->st_stid.sc_client,
5096 &stp->st_stid.sc_stateid);
5097
5098 if (ret != nfs_ok)
5099 mutex_unlock(&stp->st_mutex);
5100 return ret;
5101 }
5102
5103 static struct nfs4_ol_stateid *
nfsd4_find_and_lock_existing_open(struct nfs4_file * fp,struct nfsd4_open * open)5104 nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
5105 {
5106 struct nfs4_ol_stateid *stp;
5107 for (;;) {
5108 spin_lock(&fp->fi_lock);
5109 stp = nfsd4_find_existing_open(fp, open);
5110 spin_unlock(&fp->fi_lock);
5111 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
5112 break;
5113 nfs4_put_stid(&stp->st_stid);
5114 }
5115 return stp;
5116 }
5117
5118 static struct nfs4_openowner *
find_or_alloc_open_stateowner(unsigned int strhashval,struct nfsd4_open * open,struct nfsd4_compound_state * cstate)5119 find_or_alloc_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
5120 struct nfsd4_compound_state *cstate)
5121 {
5122 struct nfs4_client *clp = cstate->clp;
5123 struct nfs4_openowner *oo, *new = NULL;
5124
5125 retry:
5126 spin_lock(&clp->cl_lock);
5127 oo = find_openstateowner_str(strhashval, open, clp);
5128 if (!oo && new) {
5129 hash_openowner(new, clp, strhashval);
5130 spin_unlock(&clp->cl_lock);
5131 return new;
5132 }
5133 spin_unlock(&clp->cl_lock);
5134
5135 if (oo && !(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5136 /* Replace unconfirmed owners without checking for replay. */
5137 release_openowner(oo);
5138 oo = NULL;
5139 }
5140 if (oo) {
5141 if (new)
5142 nfs4_free_stateowner(&new->oo_owner);
5143 return oo;
5144 }
5145
5146 new = alloc_stateowner(openowner_slab, &open->op_owner, clp);
5147 if (!new)
5148 return NULL;
5149 new->oo_owner.so_ops = &openowner_ops;
5150 new->oo_owner.so_is_open_owner = 1;
5151 new->oo_owner.so_seqid = open->op_seqid;
5152 new->oo_flags = 0;
5153 if (nfsd4_has_session(cstate))
5154 new->oo_flags |= NFS4_OO_CONFIRMED;
5155 new->oo_time = 0;
5156 new->oo_last_closed_stid = NULL;
5157 INIT_LIST_HEAD(&new->oo_close_lru);
5158 goto retry;
5159 }
5160
5161 static struct nfs4_ol_stateid *
init_open_stateid(struct nfs4_file * fp,struct nfsd4_open * open)5162 init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
5163 {
5164
5165 struct nfs4_openowner *oo = open->op_openowner;
5166 struct nfs4_ol_stateid *retstp = NULL;
5167 struct nfs4_ol_stateid *stp;
5168
5169 stp = open->op_stp;
5170 /* We are moving these outside of the spinlocks to avoid the warnings */
5171 mutex_init(&stp->st_mutex);
5172 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
5173
5174 retry:
5175 spin_lock(&oo->oo_owner.so_client->cl_lock);
5176 spin_lock(&fp->fi_lock);
5177
5178 if (nfs4_openowner_unhashed(oo)) {
5179 mutex_unlock(&stp->st_mutex);
5180 stp = NULL;
5181 goto out_unlock;
5182 }
5183
5184 retstp = nfsd4_find_existing_open(fp, open);
5185 if (retstp)
5186 goto out_unlock;
5187
5188 open->op_stp = NULL;
5189 refcount_inc(&stp->st_stid.sc_count);
5190 stp->st_stid.sc_type = SC_TYPE_OPEN;
5191 INIT_LIST_HEAD(&stp->st_locks);
5192 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
5193 get_nfs4_file(fp);
5194 stp->st_stid.sc_file = fp;
5195 stp->st_access_bmap = 0;
5196 stp->st_deny_bmap = 0;
5197 stp->st_openstp = NULL;
5198 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
5199 list_add(&stp->st_perfile, &fp->fi_stateids);
5200
5201 out_unlock:
5202 spin_unlock(&fp->fi_lock);
5203 spin_unlock(&oo->oo_owner.so_client->cl_lock);
5204 if (retstp) {
5205 /* Handle races with CLOSE */
5206 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
5207 nfs4_put_stid(&retstp->st_stid);
5208 goto retry;
5209 }
5210 /* To keep mutex tracking happy */
5211 mutex_unlock(&stp->st_mutex);
5212 stp = retstp;
5213 }
5214 return stp;
5215 }
5216
5217 /*
5218 * In the 4.0 case we need to keep the owners around a little while to handle
5219 * CLOSE replay. We still do need to release any file access that is held by
5220 * them before returning however.
5221 */
5222 static void
move_to_close_lru(struct nfs4_ol_stateid * s,struct net * net)5223 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
5224 {
5225 struct nfs4_ol_stateid *last;
5226 struct nfs4_openowner *oo = openowner(s->st_stateowner);
5227 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
5228 nfsd_net_id);
5229
5230 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
5231
5232 /*
5233 * We know that we hold one reference via nfsd4_close, and another
5234 * "persistent" reference for the client. If the refcount is higher
5235 * than 2, then there are still calls in progress that are using this
5236 * stateid. We can't put the sc_file reference until they are finished.
5237 * Wait for the refcount to drop to 2. Since it has been unhashed,
5238 * there should be no danger of the refcount going back up again at
5239 * this point.
5240 * Some threads with a reference might be waiting for rp_locked,
5241 * so tell them to stop waiting.
5242 */
5243 store_release_wake_up(&oo->oo_owner.so_replay.rp_locked, RP_UNHASHED);
5244 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
5245
5246 release_all_access(s);
5247 if (s->st_stid.sc_file) {
5248 put_nfs4_file(s->st_stid.sc_file);
5249 s->st_stid.sc_file = NULL;
5250 }
5251
5252 spin_lock(&nn->client_lock);
5253 last = oo->oo_last_closed_stid;
5254 oo->oo_last_closed_stid = s;
5255 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
5256 oo->oo_time = ktime_get_boottime_seconds();
5257 spin_unlock(&nn->client_lock);
5258 if (last)
5259 nfs4_put_stid(&last->st_stid);
5260 }
5261
5262 static noinline_for_stack struct nfs4_file *
nfsd4_file_hash_lookup(const struct svc_fh * fhp)5263 nfsd4_file_hash_lookup(const struct svc_fh *fhp)
5264 {
5265 struct inode *inode = d_inode(fhp->fh_dentry);
5266 struct rhlist_head *tmp, *list;
5267 struct nfs4_file *fi;
5268
5269 rcu_read_lock();
5270 list = rhltable_lookup(&nfs4_file_rhltable, &inode,
5271 nfs4_file_rhash_params);
5272 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
5273 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
5274 if (refcount_inc_not_zero(&fi->fi_ref)) {
5275 rcu_read_unlock();
5276 return fi;
5277 }
5278 }
5279 }
5280 rcu_read_unlock();
5281 return NULL;
5282 }
5283
5284 /*
5285 * On hash insertion, identify entries with the same inode but
5286 * distinct filehandles. They will all be on the list returned
5287 * by rhltable_lookup().
5288 *
5289 * inode->i_lock prevents racing insertions from adding an entry
5290 * for the same inode/fhp pair twice.
5291 */
5292 static noinline_for_stack struct nfs4_file *
nfsd4_file_hash_insert(struct nfs4_file * new,const struct svc_fh * fhp)5293 nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp)
5294 {
5295 struct inode *inode = d_inode(fhp->fh_dentry);
5296 struct rhlist_head *tmp, *list;
5297 struct nfs4_file *ret = NULL;
5298 bool alias_found = false;
5299 struct nfs4_file *fi;
5300 int err;
5301
5302 rcu_read_lock();
5303 spin_lock(&inode->i_lock);
5304
5305 list = rhltable_lookup(&nfs4_file_rhltable, &inode,
5306 nfs4_file_rhash_params);
5307 rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
5308 if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
5309 if (refcount_inc_not_zero(&fi->fi_ref))
5310 ret = fi;
5311 } else
5312 fi->fi_aliased = alias_found = true;
5313 }
5314 if (ret)
5315 goto out_unlock;
5316
5317 nfsd4_file_init(fhp, new);
5318 err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist,
5319 nfs4_file_rhash_params);
5320 if (err)
5321 goto out_unlock;
5322
5323 new->fi_aliased = alias_found;
5324 ret = new;
5325
5326 out_unlock:
5327 spin_unlock(&inode->i_lock);
5328 rcu_read_unlock();
5329 return ret;
5330 }
5331
nfsd4_file_hash_remove(struct nfs4_file * fi)5332 static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi)
5333 {
5334 rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist,
5335 nfs4_file_rhash_params);
5336 }
5337
5338 /*
5339 * Called to check deny when READ with all zero stateid or
5340 * WRITE with all zero or all one stateid
5341 */
5342 static __be32
nfs4_share_conflict(struct svc_fh * current_fh,unsigned int deny_type)5343 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
5344 {
5345 struct nfs4_file *fp;
5346 __be32 ret = nfs_ok;
5347
5348 fp = nfsd4_file_hash_lookup(current_fh);
5349 if (!fp)
5350 return ret;
5351
5352 /* Check for conflicting share reservations */
5353 spin_lock(&fp->fi_lock);
5354 if (fp->fi_share_deny & deny_type)
5355 ret = nfserr_locked;
5356 spin_unlock(&fp->fi_lock);
5357 put_nfs4_file(fp);
5358 return ret;
5359 }
5360
nfsd4_deleg_present(const struct inode * inode)5361 static bool nfsd4_deleg_present(const struct inode *inode)
5362 {
5363 struct file_lock_context *ctx = locks_inode_context(inode);
5364
5365 return ctx && !list_empty_careful(&ctx->flc_lease);
5366 }
5367
5368 /**
5369 * nfsd_wait_for_delegreturn - wait for delegations to be returned
5370 * @rqstp: the RPC transaction being executed
5371 * @inode: in-core inode of the file being waited for
5372 *
5373 * The timeout prevents deadlock if all nfsd threads happen to be
5374 * tied up waiting for returning delegations.
5375 *
5376 * Return values:
5377 * %true: delegation was returned
5378 * %false: timed out waiting for delegreturn
5379 */
nfsd_wait_for_delegreturn(struct svc_rqst * rqstp,struct inode * inode)5380 bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode)
5381 {
5382 long __maybe_unused timeo;
5383
5384 timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode),
5385 NFSD_DELEGRETURN_TIMEOUT);
5386 trace_nfsd_delegret_wakeup(rqstp, inode, timeo);
5387 return timeo > 0;
5388 }
5389
nfsd4_cb_recall_prepare(struct nfsd4_callback * cb)5390 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
5391 {
5392 struct nfs4_delegation *dp = cb_to_delegation(cb);
5393 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
5394 nfsd_net_id);
5395
5396 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
5397
5398 /*
5399 * We can't do this in nfsd_break_deleg_cb because it is
5400 * already holding inode->i_lock.
5401 *
5402 * If the dl_time != 0, then we know that it has already been
5403 * queued for a lease break. Don't queue it again.
5404 */
5405 spin_lock(&state_lock);
5406 if (delegation_hashed(dp) && dp->dl_time == 0) {
5407 dp->dl_time = ktime_get_boottime_seconds();
5408 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
5409 }
5410 spin_unlock(&state_lock);
5411 }
5412
nfsd4_cb_recall_done(struct nfsd4_callback * cb,struct rpc_task * task)5413 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
5414 struct rpc_task *task)
5415 {
5416 struct nfs4_delegation *dp = cb_to_delegation(cb);
5417
5418 trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task);
5419
5420 if (dp->dl_stid.sc_status)
5421 /* CLOSED or REVOKED */
5422 return 1;
5423
5424 switch (task->tk_status) {
5425 case 0:
5426 return 1;
5427 case -NFS4ERR_DELAY:
5428 rpc_delay(task, 2 * HZ);
5429 return 0;
5430 case -EBADHANDLE:
5431 case -NFS4ERR_BAD_STATEID:
5432 /*
5433 * Race: client probably got cb_recall before open reply
5434 * granting delegation.
5435 */
5436 if (dp->dl_retries--) {
5437 rpc_delay(task, 2 * HZ);
5438 return 0;
5439 }
5440 fallthrough;
5441 default:
5442 return 1;
5443 }
5444 }
5445
nfsd4_cb_recall_release(struct nfsd4_callback * cb)5446 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
5447 {
5448 struct nfs4_delegation *dp = cb_to_delegation(cb);
5449
5450 nfs4_put_stid(&dp->dl_stid);
5451 }
5452
5453 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
5454 .prepare = nfsd4_cb_recall_prepare,
5455 .done = nfsd4_cb_recall_done,
5456 .release = nfsd4_cb_recall_release,
5457 .opcode = OP_CB_RECALL,
5458 };
5459
nfsd_break_one_deleg(struct nfs4_delegation * dp)5460 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
5461 {
5462 bool queued;
5463
5464 if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &dp->dl_recall.cb_flags))
5465 return;
5466
5467 /*
5468 * We're assuming the state code never drops its reference
5469 * without first removing the lease. Since we're in this lease
5470 * callback (and since the lease code is serialized by the
5471 * flc_lock) we know the server hasn't removed the lease yet, and
5472 * we know it's safe to take a reference.
5473 */
5474 refcount_inc(&dp->dl_stid.sc_count);
5475 queued = nfsd4_run_cb(&dp->dl_recall);
5476 WARN_ON_ONCE(!queued);
5477 if (!queued)
5478 refcount_dec(&dp->dl_stid.sc_count);
5479 }
5480
5481 /* Called from break_lease() with flc_lock held. */
5482 static bool
nfsd_break_deleg_cb(struct file_lease * fl)5483 nfsd_break_deleg_cb(struct file_lease *fl)
5484 {
5485 struct nfs4_delegation *dp = (struct nfs4_delegation *) fl->c.flc_owner;
5486 struct nfs4_file *fp = dp->dl_stid.sc_file;
5487 struct nfs4_client *clp = dp->dl_stid.sc_client;
5488 struct nfsd_net *nn;
5489
5490 trace_nfsd_cb_recall(&dp->dl_stid);
5491
5492 dp->dl_recalled = true;
5493 atomic_inc(&clp->cl_delegs_in_recall);
5494 if (try_to_expire_client(clp)) {
5495 nn = net_generic(clp->net, nfsd_net_id);
5496 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
5497 }
5498
5499 /*
5500 * We don't want the locks code to timeout the lease for us;
5501 * we'll remove it ourself if a delegation isn't returned
5502 * in time:
5503 */
5504 fl->fl_break_time = 0;
5505
5506 fp->fi_had_conflict = true;
5507 nfsd_break_one_deleg(dp);
5508 return false;
5509 }
5510
5511 /**
5512 * nfsd_breaker_owns_lease - Check if lease conflict was resolved
5513 * @fl: Lock state to check
5514 *
5515 * Return values:
5516 * %true: Lease conflict was resolved
5517 * %false: Lease conflict was not resolved.
5518 */
nfsd_breaker_owns_lease(struct file_lease * fl)5519 static bool nfsd_breaker_owns_lease(struct file_lease *fl)
5520 {
5521 struct nfs4_delegation *dl = fl->c.flc_owner;
5522 struct svc_rqst *rqst;
5523 struct nfs4_client *clp;
5524
5525 rqst = nfsd_current_rqst();
5526 if (!nfsd_v4client(rqst))
5527 return false;
5528 clp = *(rqst->rq_lease_breaker);
5529 return dl->dl_stid.sc_client == clp;
5530 }
5531
5532 static int
nfsd_change_deleg_cb(struct file_lease * onlist,int arg,struct list_head * dispose)5533 nfsd_change_deleg_cb(struct file_lease *onlist, int arg,
5534 struct list_head *dispose)
5535 {
5536 struct nfs4_delegation *dp = (struct nfs4_delegation *) onlist->c.flc_owner;
5537 struct nfs4_client *clp = dp->dl_stid.sc_client;
5538
5539 if (arg & F_UNLCK) {
5540 if (dp->dl_recalled)
5541 atomic_dec(&clp->cl_delegs_in_recall);
5542 return lease_modify(onlist, arg, dispose);
5543 } else
5544 return -EAGAIN;
5545 }
5546
5547 static const struct lease_manager_operations nfsd_lease_mng_ops = {
5548 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
5549 .lm_break = nfsd_break_deleg_cb,
5550 .lm_change = nfsd_change_deleg_cb,
5551 };
5552
nfsd4_check_seqid(struct nfsd4_compound_state * cstate,struct nfs4_stateowner * so,u32 seqid)5553 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
5554 {
5555 if (nfsd4_has_session(cstate))
5556 return nfs_ok;
5557 if (seqid == so->so_seqid - 1)
5558 return nfserr_replay_me;
5559 if (seqid == so->so_seqid)
5560 return nfs_ok;
5561 return nfserr_bad_seqid;
5562 }
5563
lookup_clientid(clientid_t * clid,bool sessions,struct nfsd_net * nn)5564 static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
5565 struct nfsd_net *nn)
5566 {
5567 struct nfs4_client *found;
5568
5569 spin_lock(&nn->client_lock);
5570 found = find_confirmed_client(clid, sessions, nn);
5571 if (found)
5572 atomic_inc(&found->cl_rpc_users);
5573 spin_unlock(&nn->client_lock);
5574 return found;
5575 }
5576
set_client(clientid_t * clid,struct nfsd4_compound_state * cstate,struct nfsd_net * nn)5577 static __be32 set_client(clientid_t *clid,
5578 struct nfsd4_compound_state *cstate,
5579 struct nfsd_net *nn)
5580 {
5581 if (cstate->clp) {
5582 if (!same_clid(&cstate->clp->cl_clientid, clid))
5583 return nfserr_stale_clientid;
5584 return nfs_ok;
5585 }
5586 if (STALE_CLIENTID(clid, nn))
5587 return nfserr_stale_clientid;
5588 /*
5589 * We're in the 4.0 case (otherwise the SEQUENCE op would have
5590 * set cstate->clp), so session = false:
5591 */
5592 cstate->clp = lookup_clientid(clid, false, nn);
5593 if (!cstate->clp)
5594 return nfserr_expired;
5595 return nfs_ok;
5596 }
5597
5598 __be32
nfsd4_process_open1(struct nfsd4_compound_state * cstate,struct nfsd4_open * open,struct nfsd_net * nn)5599 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
5600 struct nfsd4_open *open, struct nfsd_net *nn)
5601 {
5602 clientid_t *clientid = &open->op_clientid;
5603 struct nfs4_client *clp = NULL;
5604 unsigned int strhashval;
5605 struct nfs4_openowner *oo = NULL;
5606 __be32 status;
5607
5608 /*
5609 * In case we need it later, after we've already created the
5610 * file and don't want to risk a further failure:
5611 */
5612 open->op_file = nfsd4_alloc_file();
5613 if (open->op_file == NULL)
5614 return nfserr_jukebox;
5615
5616 status = set_client(clientid, cstate, nn);
5617 if (status)
5618 return status;
5619 clp = cstate->clp;
5620
5621 strhashval = ownerstr_hashval(&open->op_owner);
5622 retry:
5623 oo = find_or_alloc_open_stateowner(strhashval, open, cstate);
5624 open->op_openowner = oo;
5625 if (!oo)
5626 return nfserr_jukebox;
5627 if (nfsd4_cstate_assign_replay(cstate, &oo->oo_owner) == -EAGAIN) {
5628 nfs4_put_stateowner(&oo->oo_owner);
5629 goto retry;
5630 }
5631 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
5632 if (status)
5633 return status;
5634
5635 open->op_stp = nfs4_alloc_open_stateid(clp);
5636 if (!open->op_stp)
5637 return nfserr_jukebox;
5638
5639 if (nfsd4_has_session(cstate) &&
5640 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
5641 open->op_odstate = alloc_clnt_odstate(clp);
5642 if (!open->op_odstate)
5643 return nfserr_jukebox;
5644 }
5645
5646 return nfs_ok;
5647 }
5648
5649 static inline __be32
nfs4_check_delegmode(struct nfs4_delegation * dp,int flags)5650 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
5651 {
5652 if (!(flags & RD_STATE) && deleg_is_read(dp->dl_type))
5653 return nfserr_openmode;
5654 else
5655 return nfs_ok;
5656 }
5657
share_access_to_flags(u32 share_access)5658 static int share_access_to_flags(u32 share_access)
5659 {
5660 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
5661 }
5662
find_deleg_stateid(struct nfs4_client * cl,stateid_t * s)5663 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl,
5664 stateid_t *s)
5665 {
5666 struct nfs4_stid *ret;
5667
5668 ret = find_stateid_by_type(cl, s, SC_TYPE_DELEG, SC_STATUS_REVOKED);
5669 if (!ret)
5670 return NULL;
5671 return delegstateid(ret);
5672 }
5673
nfsd4_is_deleg_cur(struct nfsd4_open * open)5674 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
5675 {
5676 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
5677 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
5678 }
5679
5680 static __be32
nfs4_check_deleg(struct nfs4_client * cl,struct nfsd4_open * open,struct nfs4_delegation ** dp)5681 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
5682 struct nfs4_delegation **dp)
5683 {
5684 int flags;
5685 __be32 status = nfserr_bad_stateid;
5686 struct nfs4_delegation *deleg;
5687
5688 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5689 if (deleg == NULL)
5690 goto out;
5691 if (deleg->dl_stid.sc_status & SC_STATUS_ADMIN_REVOKED) {
5692 nfs4_put_stid(&deleg->dl_stid);
5693 status = nfserr_admin_revoked;
5694 goto out;
5695 }
5696 if (deleg->dl_stid.sc_status & SC_STATUS_REVOKED) {
5697 nfs4_put_stid(&deleg->dl_stid);
5698 nfsd40_drop_revoked_stid(cl, &open->op_delegate_stateid);
5699 status = nfserr_deleg_revoked;
5700 goto out;
5701 }
5702 flags = share_access_to_flags(open->op_share_access);
5703 status = nfs4_check_delegmode(deleg, flags);
5704 if (status) {
5705 nfs4_put_stid(&deleg->dl_stid);
5706 goto out;
5707 }
5708 *dp = deleg;
5709 out:
5710 if (!nfsd4_is_deleg_cur(open))
5711 return nfs_ok;
5712 if (status)
5713 return status;
5714 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5715 return nfs_ok;
5716 }
5717
nfs4_access_to_access(u32 nfs4_access)5718 static inline int nfs4_access_to_access(u32 nfs4_access)
5719 {
5720 int flags = 0;
5721
5722 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5723 flags |= NFSD_MAY_READ;
5724 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5725 flags |= NFSD_MAY_WRITE;
5726 return flags;
5727 }
5728
5729 static inline __be32
nfsd4_truncate(struct svc_rqst * rqstp,struct svc_fh * fh,struct nfsd4_open * open)5730 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5731 struct nfsd4_open *open)
5732 {
5733 struct iattr iattr = {
5734 .ia_valid = ATTR_SIZE,
5735 .ia_size = 0,
5736 };
5737 struct nfsd_attrs attrs = {
5738 .na_iattr = &iattr,
5739 };
5740 if (!open->op_truncate)
5741 return 0;
5742 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
5743 return nfserr_inval;
5744 return nfsd_setattr(rqstp, fh, &attrs, NULL);
5745 }
5746
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)5747 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
5748 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5749 struct nfsd4_open *open, bool new_stp)
5750 {
5751 struct nfsd_file *nf = NULL;
5752 __be32 status;
5753 int oflag = nfs4_access_to_omode(open->op_share_access);
5754 int access = nfs4_access_to_access(open->op_share_access);
5755 unsigned char old_access_bmap, old_deny_bmap;
5756
5757 spin_lock(&fp->fi_lock);
5758
5759 /*
5760 * Are we trying to set a deny mode that would conflict with
5761 * current access?
5762 */
5763 status = nfs4_file_check_deny(fp, open->op_share_deny);
5764 if (status != nfs_ok) {
5765 if (status != nfserr_share_denied) {
5766 spin_unlock(&fp->fi_lock);
5767 goto out;
5768 }
5769 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5770 stp, open->op_share_deny, false))
5771 status = nfserr_jukebox;
5772 spin_unlock(&fp->fi_lock);
5773 goto out;
5774 }
5775
5776 /* set access to the file */
5777 status = nfs4_file_get_access(fp, open->op_share_access);
5778 if (status != nfs_ok) {
5779 if (status != nfserr_share_denied) {
5780 spin_unlock(&fp->fi_lock);
5781 goto out;
5782 }
5783 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5784 stp, open->op_share_access, true))
5785 status = nfserr_jukebox;
5786 spin_unlock(&fp->fi_lock);
5787 goto out;
5788 }
5789
5790 /* Set access bits in stateid */
5791 old_access_bmap = stp->st_access_bmap;
5792 set_access(open->op_share_access, stp);
5793
5794 /* Set new deny mask */
5795 old_deny_bmap = stp->st_deny_bmap;
5796 set_deny(open->op_share_deny, stp);
5797 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5798
5799 if (!fp->fi_fds[oflag]) {
5800 spin_unlock(&fp->fi_lock);
5801
5802 status = nfsd_file_acquire_opened(rqstp, cur_fh, access,
5803 open->op_filp, &nf);
5804 if (status != nfs_ok)
5805 goto out_put_access;
5806
5807 spin_lock(&fp->fi_lock);
5808 if (!fp->fi_fds[oflag]) {
5809 fp->fi_fds[oflag] = nf;
5810 nf = NULL;
5811 }
5812 }
5813 spin_unlock(&fp->fi_lock);
5814 if (nf)
5815 nfsd_file_put(nf);
5816
5817 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5818 access));
5819 if (status)
5820 goto out_put_access;
5821
5822 status = nfsd4_truncate(rqstp, cur_fh, open);
5823 if (status)
5824 goto out_put_access;
5825 out:
5826 return status;
5827 out_put_access:
5828 stp->st_access_bmap = old_access_bmap;
5829 nfs4_file_put_access(fp, open->op_share_access);
5830 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5831 goto out;
5832 }
5833
5834 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)5835 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
5836 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5837 struct nfsd4_open *open)
5838 {
5839 __be32 status;
5840 unsigned char old_deny_bmap = stp->st_deny_bmap;
5841
5842 if (!test_access(open->op_share_access, stp))
5843 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
5844
5845 /* test and set deny mode */
5846 spin_lock(&fp->fi_lock);
5847 status = nfs4_file_check_deny(fp, open->op_share_deny);
5848 switch (status) {
5849 case nfs_ok:
5850 set_deny(open->op_share_deny, stp);
5851 fp->fi_share_deny |=
5852 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5853 break;
5854 case nfserr_share_denied:
5855 if (nfs4_resolve_deny_conflicts_locked(fp, false,
5856 stp, open->op_share_deny, false))
5857 status = nfserr_jukebox;
5858 break;
5859 }
5860 spin_unlock(&fp->fi_lock);
5861
5862 if (status != nfs_ok)
5863 return status;
5864
5865 status = nfsd4_truncate(rqstp, cur_fh, open);
5866 if (status != nfs_ok)
5867 reset_union_bmap_deny(old_deny_bmap, stp);
5868 return status;
5869 }
5870
5871 /* Should we give out recallable state?: */
nfsd4_cb_channel_good(struct nfs4_client * clp)5872 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5873 {
5874 if (clp->cl_cb_state == NFSD4_CB_UP)
5875 return true;
5876 /*
5877 * In the sessions case, since we don't have to establish a
5878 * separate connection for callbacks, we assume it's OK
5879 * until we hear otherwise:
5880 */
5881 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5882 }
5883
nfs4_alloc_init_lease(struct nfs4_delegation * dp)5884 static struct file_lease *nfs4_alloc_init_lease(struct nfs4_delegation *dp)
5885 {
5886 struct file_lease *fl;
5887
5888 fl = locks_alloc_lease();
5889 if (!fl)
5890 return NULL;
5891 fl->fl_lmops = &nfsd_lease_mng_ops;
5892 fl->c.flc_flags = FL_DELEG;
5893 fl->c.flc_type = deleg_is_read(dp->dl_type) ? F_RDLCK : F_WRLCK;
5894 fl->c.flc_owner = (fl_owner_t)dp;
5895 fl->c.flc_pid = current->tgid;
5896 fl->c.flc_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
5897 return fl;
5898 }
5899
nfsd4_check_conflicting_opens(struct nfs4_client * clp,struct nfs4_file * fp)5900 static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5901 struct nfs4_file *fp)
5902 {
5903 struct nfs4_ol_stateid *st;
5904 struct file *f = fp->fi_deleg_file->nf_file;
5905 struct inode *ino = file_inode(f);
5906 int writes;
5907
5908 writes = atomic_read(&ino->i_writecount);
5909 if (!writes)
5910 return 0;
5911 /*
5912 * There could be multiple filehandles (hence multiple
5913 * nfs4_files) referencing this file, but that's not too
5914 * common; let's just give up in that case rather than
5915 * trying to go look up all the clients using that other
5916 * nfs4_file as well:
5917 */
5918 if (fp->fi_aliased)
5919 return -EAGAIN;
5920 /*
5921 * If there's a close in progress, make sure that we see it
5922 * clear any fi_fds[] entries before we see it decrement
5923 * i_writecount:
5924 */
5925 smp_mb__after_atomic();
5926
5927 if (fp->fi_fds[O_WRONLY])
5928 writes--;
5929 if (fp->fi_fds[O_RDWR])
5930 writes--;
5931 if (writes > 0)
5932 return -EAGAIN; /* There may be non-NFSv4 writers */
5933 /*
5934 * It's possible there are non-NFSv4 write opens in progress,
5935 * but if they haven't incremented i_writecount yet then they
5936 * also haven't called break lease yet; so, they'll break this
5937 * lease soon enough. So, all that's left to check for is NFSv4
5938 * opens:
5939 */
5940 spin_lock(&fp->fi_lock);
5941 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5942 if (st->st_openstp == NULL /* it's an open */ &&
5943 access_permit_write(st) &&
5944 st->st_stid.sc_client != clp) {
5945 spin_unlock(&fp->fi_lock);
5946 return -EAGAIN;
5947 }
5948 }
5949 spin_unlock(&fp->fi_lock);
5950 /*
5951 * There's a small chance that we could be racing with another
5952 * NFSv4 open. However, any open that hasn't added itself to
5953 * the fi_stateids list also hasn't called break_lease yet; so,
5954 * they'll break this lease soon enough.
5955 */
5956 return 0;
5957 }
5958
5959 /*
5960 * It's possible that between opening the dentry and setting the delegation,
5961 * that it has been renamed or unlinked. Redo the lookup to verify that this
5962 * hasn't happened.
5963 */
5964 static int
nfsd4_verify_deleg_dentry(struct nfsd4_open * open,struct nfs4_file * fp,struct svc_fh * parent)5965 nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
5966 struct svc_fh *parent)
5967 {
5968 struct svc_export *exp;
5969 struct dentry *child;
5970 __be32 err;
5971
5972 err = nfsd_lookup_dentry(open->op_rqstp, parent,
5973 open->op_fname, open->op_fnamelen,
5974 &exp, &child);
5975
5976 if (err)
5977 return -EAGAIN;
5978
5979 exp_put(exp);
5980 dput(child);
5981 if (child != file_dentry(fp->fi_deleg_file->nf_file))
5982 return -EAGAIN;
5983
5984 return 0;
5985 }
5986
5987 /*
5988 * We avoid breaking delegations held by a client due to its own activity, but
5989 * clearing setuid/setgid bits on a write is an implicit activity and the client
5990 * may not notice and continue using the old mode. Avoid giving out a delegation
5991 * on setuid/setgid files when the client is requesting an open for write.
5992 */
5993 static int
nfsd4_verify_setuid_write(struct nfsd4_open * open,struct nfsd_file * nf)5994 nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf)
5995 {
5996 struct inode *inode = file_inode(nf->nf_file);
5997
5998 if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) &&
5999 (inode->i_mode & (S_ISUID|S_ISGID)))
6000 return -EAGAIN;
6001 return 0;
6002 }
6003
6004 #ifdef CONFIG_NFSD_V4_DELEG_TIMESTAMPS
nfsd4_want_deleg_timestamps(const struct nfsd4_open * open)6005 static bool nfsd4_want_deleg_timestamps(const struct nfsd4_open *open)
6006 {
6007 return open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_DELEG_TIMESTAMPS;
6008 }
6009 #else /* CONFIG_NFSD_V4_DELEG_TIMESTAMPS */
nfsd4_want_deleg_timestamps(const struct nfsd4_open * open)6010 static bool nfsd4_want_deleg_timestamps(const struct nfsd4_open *open)
6011 {
6012 return false;
6013 }
6014 #endif /* CONFIG NFSD_V4_DELEG_TIMESTAMPS */
6015
6016 static struct nfs4_delegation *
nfs4_set_delegation(struct nfsd4_open * open,struct nfs4_ol_stateid * stp,struct svc_fh * parent)6017 nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
6018 struct svc_fh *parent)
6019 {
6020 bool deleg_ts = nfsd4_want_deleg_timestamps(open);
6021 struct nfs4_client *clp = stp->st_stid.sc_client;
6022 struct nfs4_file *fp = stp->st_stid.sc_file;
6023 struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
6024 struct nfs4_delegation *dp;
6025 struct nfsd_file *nf = NULL;
6026 struct file_lease *fl;
6027 int status = 0;
6028 u32 dl_type;
6029
6030 /*
6031 * The fi_had_conflict and nfs_get_existing_delegation checks
6032 * here are just optimizations; we'll need to recheck them at
6033 * the end:
6034 */
6035 if (fp->fi_had_conflict)
6036 return ERR_PTR(-EAGAIN);
6037
6038 /*
6039 * Try for a write delegation first. RFC8881 section 10.4 says:
6040 *
6041 * "An OPEN_DELEGATE_WRITE delegation allows the client to handle,
6042 * on its own, all opens."
6043 *
6044 * Furthermore, section 9.1.2 says:
6045 *
6046 * "In the case of READ, the server may perform the corresponding
6047 * check on the access mode, or it may choose to allow READ for
6048 * OPEN4_SHARE_ACCESS_WRITE, to accommodate clients whose WRITE
6049 * implementation may unavoidably do reads (e.g., due to buffer
6050 * cache constraints)."
6051 *
6052 * We choose to offer a write delegation for OPEN with the
6053 * OPEN4_SHARE_ACCESS_WRITE access mode to accommodate such clients.
6054 */
6055 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
6056 nf = find_writeable_file(fp);
6057 dl_type = deleg_ts ? OPEN_DELEGATE_WRITE_ATTRS_DELEG : OPEN_DELEGATE_WRITE;
6058 }
6059
6060 /*
6061 * If the file is being opened O_RDONLY or we couldn't get a O_RDWR
6062 * file for some reason, then try for a read delegation instead.
6063 */
6064 if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) {
6065 nf = find_readable_file(fp);
6066 dl_type = deleg_ts ? OPEN_DELEGATE_READ_ATTRS_DELEG : OPEN_DELEGATE_READ;
6067 }
6068
6069 if (!nf)
6070 return ERR_PTR(-EAGAIN);
6071
6072 /*
6073 * File delegations and associated locks cannot be recovered if the
6074 * export is from an NFS proxy server.
6075 */
6076 if (exportfs_cannot_lock(nf->nf_file->f_path.mnt->mnt_sb->s_export_op)) {
6077 nfsd_file_put(nf);
6078 return ERR_PTR(-EOPNOTSUPP);
6079 }
6080
6081 spin_lock(&state_lock);
6082 spin_lock(&fp->fi_lock);
6083 if (nfs4_delegation_exists(clp, fp))
6084 status = -EAGAIN;
6085 else if (nfsd4_verify_setuid_write(open, nf))
6086 status = -EAGAIN;
6087 else if (!fp->fi_deleg_file) {
6088 fp->fi_deleg_file = nf;
6089 /* increment early to prevent fi_deleg_file from being
6090 * cleared */
6091 fp->fi_delegees = 1;
6092 nf = NULL;
6093 } else
6094 fp->fi_delegees++;
6095 spin_unlock(&fp->fi_lock);
6096 spin_unlock(&state_lock);
6097 if (nf)
6098 nfsd_file_put(nf);
6099 if (status)
6100 return ERR_PTR(status);
6101
6102 status = -ENOMEM;
6103 dp = alloc_init_deleg(clp, fp, odstate, dl_type);
6104 if (!dp)
6105 goto out_delegees;
6106
6107 fl = nfs4_alloc_init_lease(dp);
6108 if (!fl)
6109 goto out_clnt_odstate;
6110
6111 status = kernel_setlease(fp->fi_deleg_file->nf_file,
6112 fl->c.flc_type, &fl, NULL);
6113 if (fl)
6114 locks_free_lease(fl);
6115 if (status)
6116 goto out_clnt_odstate;
6117
6118 if (parent) {
6119 status = nfsd4_verify_deleg_dentry(open, fp, parent);
6120 if (status)
6121 goto out_unlock;
6122 }
6123
6124 status = nfsd4_check_conflicting_opens(clp, fp);
6125 if (status)
6126 goto out_unlock;
6127
6128 /*
6129 * Now that the deleg is set, check again to ensure that nothing
6130 * raced in and changed the mode while we weren't looking.
6131 */
6132 status = nfsd4_verify_setuid_write(open, fp->fi_deleg_file);
6133 if (status)
6134 goto out_unlock;
6135
6136 status = -EAGAIN;
6137 if (fp->fi_had_conflict)
6138 goto out_unlock;
6139
6140 spin_lock(&state_lock);
6141 spin_lock(&clp->cl_lock);
6142 spin_lock(&fp->fi_lock);
6143 status = hash_delegation_locked(dp, fp);
6144 spin_unlock(&fp->fi_lock);
6145 spin_unlock(&clp->cl_lock);
6146 spin_unlock(&state_lock);
6147
6148 if (status)
6149 goto out_unlock;
6150
6151 return dp;
6152 out_unlock:
6153 kernel_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
6154 out_clnt_odstate:
6155 put_clnt_odstate(dp->dl_clnt_odstate);
6156 nfs4_put_stid(&dp->dl_stid);
6157 out_delegees:
6158 put_deleg_file(fp);
6159 return ERR_PTR(status);
6160 }
6161
nfsd4_open_deleg_none_ext(struct nfsd4_open * open,int status)6162 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
6163 {
6164 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
6165 if (status == -EAGAIN)
6166 open->op_why_no_deleg = WND4_CONTENTION;
6167 else {
6168 open->op_why_no_deleg = WND4_RESOURCE;
6169 switch (open->op_deleg_want) {
6170 case OPEN4_SHARE_ACCESS_WANT_READ_DELEG:
6171 case OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG:
6172 case OPEN4_SHARE_ACCESS_WANT_ANY_DELEG:
6173 break;
6174 case OPEN4_SHARE_ACCESS_WANT_CANCEL:
6175 open->op_why_no_deleg = WND4_CANCELLED;
6176 break;
6177 case OPEN4_SHARE_ACCESS_WANT_NO_DELEG:
6178 WARN_ON_ONCE(1);
6179 }
6180 }
6181 }
6182
6183 static bool
nfs4_delegation_stat(struct nfs4_delegation * dp,struct svc_fh * currentfh,struct kstat * stat)6184 nfs4_delegation_stat(struct nfs4_delegation *dp, struct svc_fh *currentfh,
6185 struct kstat *stat)
6186 {
6187 struct nfsd_file *nf = find_writeable_file(dp->dl_stid.sc_file);
6188 struct path path;
6189 int rc;
6190
6191 if (!nf)
6192 return false;
6193
6194 path.mnt = currentfh->fh_export->ex_path.mnt;
6195 path.dentry = file_dentry(nf->nf_file);
6196
6197 rc = vfs_getattr(&path, stat,
6198 STATX_MODE | STATX_SIZE | STATX_ATIME |
6199 STATX_MTIME | STATX_CTIME | STATX_CHANGE_COOKIE,
6200 AT_STATX_SYNC_AS_STAT);
6201
6202 nfsd_file_put(nf);
6203 return rc == 0;
6204 }
6205
6206 /*
6207 * Add NFS4_SHARE_ACCESS_READ to the write delegation granted on OPEN
6208 * with NFS4_SHARE_ACCESS_WRITE by allocating separate nfsd_file and
6209 * struct file to be used for read with delegation stateid.
6210 *
6211 */
6212 static bool
nfsd4_add_rdaccess_to_wrdeleg(struct svc_rqst * rqstp,struct nfsd4_open * open,struct svc_fh * fh,struct nfs4_ol_stateid * stp)6213 nfsd4_add_rdaccess_to_wrdeleg(struct svc_rqst *rqstp, struct nfsd4_open *open,
6214 struct svc_fh *fh, struct nfs4_ol_stateid *stp)
6215 {
6216 struct nfs4_file *fp;
6217 struct nfsd_file *nf = NULL;
6218
6219 if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) ==
6220 NFS4_SHARE_ACCESS_WRITE) {
6221 if (nfsd_file_acquire_opened(rqstp, fh, NFSD_MAY_READ, NULL, &nf))
6222 return (false);
6223 fp = stp->st_stid.sc_file;
6224 spin_lock(&fp->fi_lock);
6225 __nfs4_file_get_access(fp, NFS4_SHARE_ACCESS_READ);
6226 fp = stp->st_stid.sc_file;
6227 fp->fi_fds[O_RDONLY] = nf;
6228 fp->fi_rdeleg_file = nf;
6229 spin_unlock(&fp->fi_lock);
6230 }
6231 return true;
6232 }
6233
6234 /*
6235 * The Linux NFS server does not offer write delegations to NFSv4.0
6236 * clients in order to avoid conflicts between write delegations and
6237 * GETATTRs requesting CHANGE or SIZE attributes.
6238 *
6239 * With NFSv4.1 and later minorversions, the SEQUENCE operation that
6240 * begins each COMPOUND contains a client ID. Delegation recall can
6241 * be avoided when the server recognizes the client sending a
6242 * GETATTR also holds write delegation it conflicts with.
6243 *
6244 * However, the NFSv4.0 protocol does not enable a server to
6245 * determine that a GETATTR originated from the client holding the
6246 * conflicting delegation versus coming from some other client. Per
6247 * RFC 7530 Section 16.7.5, the server must recall or send a
6248 * CB_GETATTR even when the GETATTR originates from the client that
6249 * holds the conflicting delegation.
6250 *
6251 * An NFSv4.0 client can trigger a pathological situation if it
6252 * always sends a DELEGRETURN preceded by a conflicting GETATTR in
6253 * the same COMPOUND. COMPOUND execution will always stop at the
6254 * GETATTR and the DELEGRETURN will never get executed. The server
6255 * eventually revokes the delegation, which can result in loss of
6256 * open or lock state.
6257 */
6258 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)6259 nfs4_open_delegation(struct svc_rqst *rqstp, struct nfsd4_open *open,
6260 struct nfs4_ol_stateid *stp, struct svc_fh *currentfh,
6261 struct svc_fh *fh)
6262 {
6263 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
6264 bool deleg_ts = nfsd4_want_deleg_timestamps(open);
6265 struct nfs4_client *clp = stp->st_stid.sc_client;
6266 struct svc_fh *parent = NULL;
6267 struct nfs4_delegation *dp;
6268 struct kstat stat;
6269 int status = 0;
6270 int cb_up;
6271
6272 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
6273 open->op_recall = false;
6274 switch (open->op_claim_type) {
6275 case NFS4_OPEN_CLAIM_PREVIOUS:
6276 if (!cb_up)
6277 open->op_recall = true;
6278 break;
6279 case NFS4_OPEN_CLAIM_NULL:
6280 parent = currentfh;
6281 fallthrough;
6282 case NFS4_OPEN_CLAIM_FH:
6283 /*
6284 * Let's not give out any delegations till everyone's
6285 * had the chance to reclaim theirs, *and* until
6286 * NLM locks have all been reclaimed:
6287 */
6288 if (locks_in_grace(clp->net))
6289 goto out_no_deleg;
6290 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
6291 goto out_no_deleg;
6292 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE &&
6293 !clp->cl_minorversion)
6294 goto out_no_deleg;
6295 break;
6296 default:
6297 goto out_no_deleg;
6298 }
6299 dp = nfs4_set_delegation(open, stp, parent);
6300 if (IS_ERR(dp))
6301 goto out_no_deleg;
6302
6303 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
6304
6305 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
6306 struct file *f = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
6307
6308 if (!nfsd4_add_rdaccess_to_wrdeleg(rqstp, open, fh, stp) ||
6309 !nfs4_delegation_stat(dp, currentfh, &stat)) {
6310 nfs4_put_stid(&dp->dl_stid);
6311 destroy_delegation(dp);
6312 goto out_no_deleg;
6313 }
6314 open->op_delegate_type = deleg_ts ? OPEN_DELEGATE_WRITE_ATTRS_DELEG :
6315 OPEN_DELEGATE_WRITE;
6316 dp->dl_cb_fattr.ncf_cur_fsize = stat.size;
6317 dp->dl_cb_fattr.ncf_initial_cinfo = nfsd4_change_attribute(&stat);
6318 dp->dl_atime = stat.atime;
6319 dp->dl_ctime = stat.ctime;
6320 dp->dl_mtime = stat.mtime;
6321 spin_lock(&f->f_lock);
6322 f->f_mode |= FMODE_NOCMTIME;
6323 spin_unlock(&f->f_lock);
6324 trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid);
6325 } else {
6326 open->op_delegate_type = deleg_ts && nfs4_delegation_stat(dp, currentfh, &stat) ?
6327 OPEN_DELEGATE_READ_ATTRS_DELEG : OPEN_DELEGATE_READ;
6328 dp->dl_atime = stat.atime;
6329 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
6330 }
6331 nfs4_put_stid(&dp->dl_stid);
6332 return;
6333 out_no_deleg:
6334 open->op_delegate_type = OPEN_DELEGATE_NONE;
6335 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
6336 open->op_delegate_type != OPEN_DELEGATE_NONE) {
6337 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
6338 open->op_recall = true;
6339 }
6340
6341 /* 4.1 client asking for a delegation? */
6342 if (open->op_deleg_want)
6343 nfsd4_open_deleg_none_ext(open, status);
6344 return;
6345 }
6346
nfsd4_deleg_xgrade_none_ext(struct nfsd4_open * open,struct nfs4_delegation * dp)6347 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
6348 struct nfs4_delegation *dp)
6349 {
6350 if (deleg_is_write(dp->dl_type)) {
6351 if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_READ_DELEG) {
6352 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
6353 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
6354 } else if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG) {
6355 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
6356 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
6357 }
6358 }
6359 /* Otherwise the client must be confused wanting a delegation
6360 * it already has, therefore we don't return
6361 * OPEN_DELEGATE_NONE_EXT and reason.
6362 */
6363 }
6364
6365 /* Are we returning only a delegation stateid? */
open_xor_delegation(struct nfsd4_open * open)6366 static bool open_xor_delegation(struct nfsd4_open *open)
6367 {
6368 if (!(open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_OPEN_XOR_DELEGATION))
6369 return false;
6370 /* Did we actually get a delegation? */
6371 if (!deleg_is_read(open->op_delegate_type) && !deleg_is_write(open->op_delegate_type))
6372 return false;
6373 return true;
6374 }
6375
6376 /**
6377 * nfsd4_process_open2 - finish open processing
6378 * @rqstp: the RPC transaction being executed
6379 * @current_fh: NFSv4 COMPOUND's current filehandle
6380 * @open: OPEN arguments
6381 *
6382 * If successful, (1) truncate the file if open->op_truncate was
6383 * set, (2) set open->op_stateid, (3) set open->op_delegation.
6384 *
6385 * Returns %nfs_ok on success; otherwise an nfs4stat value in
6386 * network byte order is returned.
6387 */
6388 __be32
nfsd4_process_open2(struct svc_rqst * rqstp,struct svc_fh * current_fh,struct nfsd4_open * open)6389 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
6390 {
6391 struct nfsd4_compoundres *resp = rqstp->rq_resp;
6392 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
6393 struct nfs4_file *fp = NULL;
6394 struct nfs4_ol_stateid *stp = NULL;
6395 struct nfs4_delegation *dp = NULL;
6396 __be32 status;
6397 bool new_stp = false;
6398
6399 /*
6400 * Lookup file; if found, lookup stateid and check open request,
6401 * and check for delegations in the process of being recalled.
6402 * If not found, create the nfs4_file struct
6403 */
6404 fp = nfsd4_file_hash_insert(open->op_file, current_fh);
6405 if (unlikely(!fp))
6406 return nfserr_jukebox;
6407 if (fp != open->op_file) {
6408 status = nfs4_check_deleg(cl, open, &dp);
6409 if (status)
6410 goto out;
6411 if (dp && nfsd4_is_deleg_cur(open) &&
6412 (dp->dl_stid.sc_file != fp)) {
6413 /*
6414 * RFC8881 section 8.2.4 mandates the server to return
6415 * NFS4ERR_BAD_STATEID if the selected table entry does
6416 * not match the current filehandle. However returning
6417 * NFS4ERR_BAD_STATEID in the OPEN can cause the client
6418 * to repeatedly retry the operation with the same
6419 * stateid, since the stateid itself is valid. To avoid
6420 * this situation NFSD returns NFS4ERR_INVAL instead.
6421 */
6422 status = nfserr_inval;
6423 goto out;
6424 }
6425 stp = nfsd4_find_and_lock_existing_open(fp, open);
6426 } else {
6427 open->op_file = NULL;
6428 status = nfserr_bad_stateid;
6429 if (nfsd4_is_deleg_cur(open))
6430 goto out;
6431 }
6432
6433 if (!stp) {
6434 stp = init_open_stateid(fp, open);
6435 if (!stp) {
6436 status = nfserr_jukebox;
6437 goto out;
6438 }
6439
6440 if (!open->op_stp)
6441 new_stp = true;
6442 }
6443
6444 /*
6445 * OPEN the file, or upgrade an existing OPEN.
6446 * If truncate fails, the OPEN fails.
6447 *
6448 * stp is already locked.
6449 */
6450 if (!new_stp) {
6451 /* Stateid was found, this is an OPEN upgrade */
6452 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
6453 if (status) {
6454 mutex_unlock(&stp->st_mutex);
6455 goto out;
6456 }
6457 } else {
6458 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
6459 if (status) {
6460 release_open_stateid(stp);
6461 mutex_unlock(&stp->st_mutex);
6462 goto out;
6463 }
6464
6465 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
6466 open->op_odstate);
6467 if (stp->st_clnt_odstate == open->op_odstate)
6468 open->op_odstate = NULL;
6469 }
6470
6471 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
6472 mutex_unlock(&stp->st_mutex);
6473
6474 if (nfsd4_has_session(&resp->cstate)) {
6475 if (open->op_deleg_want & OPEN4_SHARE_ACCESS_WANT_NO_DELEG) {
6476 open->op_delegate_type = OPEN_DELEGATE_NONE_EXT;
6477 open->op_why_no_deleg = WND4_NOT_WANTED;
6478 goto nodeleg;
6479 }
6480 }
6481
6482 /*
6483 * Attempt to hand out a delegation. No error return, because the
6484 * OPEN succeeds even if we fail.
6485 */
6486 nfs4_open_delegation(rqstp, open, stp,
6487 &resp->cstate.current_fh, current_fh);
6488
6489 /*
6490 * If there is an existing open stateid, it must be updated and
6491 * returned. Only respect WANT_OPEN_XOR_DELEGATION when a new
6492 * open stateid would have to be created.
6493 */
6494 if (new_stp && open_xor_delegation(open)) {
6495 memcpy(&open->op_stateid, &zero_stateid, sizeof(open->op_stateid));
6496 open->op_rflags |= OPEN4_RESULT_NO_OPEN_STATEID;
6497 release_open_stateid(stp);
6498 }
6499 nodeleg:
6500 status = nfs_ok;
6501 trace_nfsd_open(&stp->st_stid.sc_stateid);
6502 out:
6503 /* 4.1 client trying to upgrade/downgrade delegation? */
6504 if (open->op_delegate_type == OPEN_DELEGATE_NONE && dp &&
6505 open->op_deleg_want)
6506 nfsd4_deleg_xgrade_none_ext(open, dp);
6507
6508 if (fp)
6509 put_nfs4_file(fp);
6510 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
6511 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
6512 /*
6513 * To finish the open response, we just need to set the rflags.
6514 */
6515 open->op_rflags |= NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
6516 if (nfsd4_has_session(&resp->cstate))
6517 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
6518 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
6519 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
6520
6521 if (dp)
6522 nfs4_put_stid(&dp->dl_stid);
6523 if (stp)
6524 nfs4_put_stid(&stp->st_stid);
6525
6526 return status;
6527 }
6528
nfsd4_cleanup_open_state(struct nfsd4_compound_state * cstate,struct nfsd4_open * open)6529 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
6530 struct nfsd4_open *open)
6531 {
6532 if (open->op_openowner)
6533 nfs4_put_stateowner(&open->op_openowner->oo_owner);
6534 if (open->op_file)
6535 kmem_cache_free(file_slab, open->op_file);
6536 if (open->op_stp)
6537 nfs4_put_stid(&open->op_stp->st_stid);
6538 if (open->op_odstate)
6539 kmem_cache_free(odstate_slab, open->op_odstate);
6540 }
6541
6542 __be32
nfsd4_renew(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)6543 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6544 union nfsd4_op_u *u)
6545 {
6546 clientid_t *clid = &u->renew;
6547 struct nfs4_client *clp;
6548 __be32 status;
6549 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6550
6551 trace_nfsd_clid_renew(clid);
6552 status = set_client(clid, cstate, nn);
6553 if (status)
6554 return status;
6555 clp = cstate->clp;
6556 if (!list_empty(&clp->cl_delegations)
6557 && clp->cl_cb_state != NFSD4_CB_UP)
6558 return nfserr_cb_path_down;
6559 return nfs_ok;
6560 }
6561
6562 void
nfsd4_end_grace(struct nfsd_net * nn)6563 nfsd4_end_grace(struct nfsd_net *nn)
6564 {
6565 /* do nothing if grace period already ended */
6566 if (nn->grace_ended)
6567 return;
6568
6569 trace_nfsd_grace_complete(nn);
6570 nn->grace_ended = true;
6571 /*
6572 * If the server goes down again right now, an NFSv4
6573 * client will still be allowed to reclaim after it comes back up,
6574 * even if it hasn't yet had a chance to reclaim state this time.
6575 *
6576 */
6577 nfsd4_record_grace_done(nn);
6578 /*
6579 * At this point, NFSv4 clients can still reclaim. But if the
6580 * server crashes, any that have not yet reclaimed will be out
6581 * of luck on the next boot.
6582 *
6583 * (NFSv4.1+ clients are considered to have reclaimed once they
6584 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
6585 * have reclaimed after their first OPEN.)
6586 */
6587 locks_end_grace(&nn->nfsd4_manager);
6588 /*
6589 * At this point, and once lockd and/or any other containers
6590 * exit their grace period, further reclaims will fail and
6591 * regular locking can resume.
6592 */
6593 }
6594
6595 /*
6596 * If we've waited a lease period but there are still clients trying to
6597 * reclaim, wait a little longer to give them a chance to finish.
6598 */
clients_still_reclaiming(struct nfsd_net * nn)6599 static bool clients_still_reclaiming(struct nfsd_net *nn)
6600 {
6601 time64_t double_grace_period_end = nn->boot_time +
6602 2 * nn->nfsd4_lease;
6603
6604 if (nn->track_reclaim_completes &&
6605 atomic_read(&nn->nr_reclaim_complete) ==
6606 nn->reclaim_str_hashtbl_size)
6607 return false;
6608 if (!nn->somebody_reclaimed)
6609 return false;
6610 nn->somebody_reclaimed = false;
6611 /*
6612 * If we've given them *two* lease times to reclaim, and they're
6613 * still not done, give up:
6614 */
6615 if (ktime_get_boottime_seconds() > double_grace_period_end)
6616 return false;
6617 return true;
6618 }
6619
6620 struct laundry_time {
6621 time64_t cutoff;
6622 time64_t new_timeo;
6623 };
6624
state_expired(struct laundry_time * lt,time64_t last_refresh)6625 static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
6626 {
6627 time64_t time_remaining;
6628
6629 if (last_refresh < lt->cutoff)
6630 return true;
6631 time_remaining = last_refresh - lt->cutoff;
6632 lt->new_timeo = min(lt->new_timeo, time_remaining);
6633 return false;
6634 }
6635
6636 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
nfsd4_ssc_init_umount_work(struct nfsd_net * nn)6637 void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
6638 {
6639 spin_lock_init(&nn->nfsd_ssc_lock);
6640 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
6641 init_waitqueue_head(&nn->nfsd_ssc_waitq);
6642 }
6643
6644 /*
6645 * This is called when nfsd is being shutdown, after all inter_ssc
6646 * cleanup were done, to destroy the ssc delayed unmount list.
6647 */
nfsd4_ssc_shutdown_umount(struct nfsd_net * nn)6648 static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
6649 {
6650 struct nfsd4_ssc_umount_item *ni = NULL;
6651 struct nfsd4_ssc_umount_item *tmp;
6652
6653 spin_lock(&nn->nfsd_ssc_lock);
6654 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
6655 list_del(&ni->nsui_list);
6656 spin_unlock(&nn->nfsd_ssc_lock);
6657 mntput(ni->nsui_vfsmount);
6658 kfree(ni);
6659 spin_lock(&nn->nfsd_ssc_lock);
6660 }
6661 spin_unlock(&nn->nfsd_ssc_lock);
6662 }
6663
nfsd4_ssc_expire_umount(struct nfsd_net * nn)6664 static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
6665 {
6666 bool do_wakeup = false;
6667 struct nfsd4_ssc_umount_item *ni = NULL;
6668 struct nfsd4_ssc_umount_item *tmp;
6669
6670 spin_lock(&nn->nfsd_ssc_lock);
6671 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
6672 if (time_after(jiffies, ni->nsui_expire)) {
6673 if (refcount_read(&ni->nsui_refcnt) > 1)
6674 continue;
6675
6676 /* mark being unmount */
6677 ni->nsui_busy = true;
6678 spin_unlock(&nn->nfsd_ssc_lock);
6679 mntput(ni->nsui_vfsmount);
6680 spin_lock(&nn->nfsd_ssc_lock);
6681
6682 /* waiters need to start from begin of list */
6683 list_del(&ni->nsui_list);
6684 kfree(ni);
6685
6686 /* wakeup ssc_connect waiters */
6687 do_wakeup = true;
6688 continue;
6689 }
6690 break;
6691 }
6692 if (do_wakeup)
6693 wake_up_all(&nn->nfsd_ssc_waitq);
6694 spin_unlock(&nn->nfsd_ssc_lock);
6695 }
6696 #endif
6697
6698 /* Check if any lock belonging to this lockowner has any blockers */
6699 static bool
nfs4_lockowner_has_blockers(struct nfs4_lockowner * lo)6700 nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
6701 {
6702 struct file_lock_context *ctx;
6703 struct nfs4_ol_stateid *stp;
6704 struct nfs4_file *nf;
6705
6706 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
6707 nf = stp->st_stid.sc_file;
6708 ctx = locks_inode_context(nf->fi_inode);
6709 if (!ctx)
6710 continue;
6711 if (locks_owner_has_blockers(ctx, lo))
6712 return true;
6713 }
6714 return false;
6715 }
6716
6717 static bool
nfs4_anylock_blockers(struct nfs4_client * clp)6718 nfs4_anylock_blockers(struct nfs4_client *clp)
6719 {
6720 int i;
6721 struct nfs4_stateowner *so;
6722 struct nfs4_lockowner *lo;
6723
6724 if (atomic_read(&clp->cl_delegs_in_recall))
6725 return true;
6726 spin_lock(&clp->cl_lock);
6727 for (i = 0; i < OWNER_HASH_SIZE; i++) {
6728 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
6729 so_strhash) {
6730 if (so->so_is_open_owner)
6731 continue;
6732 lo = lockowner(so);
6733 if (nfs4_lockowner_has_blockers(lo)) {
6734 spin_unlock(&clp->cl_lock);
6735 return true;
6736 }
6737 }
6738 }
6739 spin_unlock(&clp->cl_lock);
6740 return false;
6741 }
6742
6743 static void
nfs4_get_client_reaplist(struct nfsd_net * nn,struct list_head * reaplist,struct laundry_time * lt)6744 nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
6745 struct laundry_time *lt)
6746 {
6747 unsigned int maxreap, reapcnt = 0;
6748 struct list_head *pos, *next;
6749 struct nfs4_client *clp;
6750
6751 maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
6752 NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
6753 INIT_LIST_HEAD(reaplist);
6754 spin_lock(&nn->client_lock);
6755 list_for_each_safe(pos, next, &nn->client_lru) {
6756 clp = list_entry(pos, struct nfs4_client, cl_lru);
6757 if (clp->cl_state == NFSD4_EXPIRABLE)
6758 goto exp_client;
6759 if (!state_expired(lt, clp->cl_time))
6760 break;
6761 if (!atomic_read(&clp->cl_rpc_users)) {
6762 if (clp->cl_state == NFSD4_ACTIVE)
6763 atomic_inc(&nn->nfsd_courtesy_clients);
6764 clp->cl_state = NFSD4_COURTESY;
6765 }
6766 if (!client_has_state(clp))
6767 goto exp_client;
6768 if (!nfs4_anylock_blockers(clp))
6769 if (reapcnt >= maxreap)
6770 continue;
6771 exp_client:
6772 if (!mark_client_expired_locked(clp)) {
6773 list_add(&clp->cl_lru, reaplist);
6774 reapcnt++;
6775 }
6776 }
6777 spin_unlock(&nn->client_lock);
6778 }
6779
6780 static void
nfs4_get_courtesy_client_reaplist(struct nfsd_net * nn,struct list_head * reaplist)6781 nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn,
6782 struct list_head *reaplist)
6783 {
6784 unsigned int maxreap = 0, reapcnt = 0;
6785 struct list_head *pos, *next;
6786 struct nfs4_client *clp;
6787
6788 maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN;
6789 INIT_LIST_HEAD(reaplist);
6790
6791 spin_lock(&nn->client_lock);
6792 list_for_each_safe(pos, next, &nn->client_lru) {
6793 clp = list_entry(pos, struct nfs4_client, cl_lru);
6794 if (clp->cl_state == NFSD4_ACTIVE)
6795 break;
6796 if (reapcnt >= maxreap)
6797 break;
6798 if (!mark_client_expired_locked(clp)) {
6799 list_add(&clp->cl_lru, reaplist);
6800 reapcnt++;
6801 }
6802 }
6803 spin_unlock(&nn->client_lock);
6804 }
6805
6806 static void
nfs4_process_client_reaplist(struct list_head * reaplist)6807 nfs4_process_client_reaplist(struct list_head *reaplist)
6808 {
6809 struct list_head *pos, *next;
6810 struct nfs4_client *clp;
6811
6812 list_for_each_safe(pos, next, reaplist) {
6813 clp = list_entry(pos, struct nfs4_client, cl_lru);
6814 trace_nfsd_clid_purged(&clp->cl_clientid);
6815 list_del_init(&clp->cl_lru);
6816 expire_client(clp);
6817 }
6818 }
6819
nfs40_clean_admin_revoked(struct nfsd_net * nn,struct laundry_time * lt)6820 static void nfs40_clean_admin_revoked(struct nfsd_net *nn,
6821 struct laundry_time *lt)
6822 {
6823 struct nfs4_client *clp;
6824
6825 spin_lock(&nn->client_lock);
6826 if (nn->nfs40_last_revoke == 0 ||
6827 nn->nfs40_last_revoke > lt->cutoff) {
6828 spin_unlock(&nn->client_lock);
6829 return;
6830 }
6831 nn->nfs40_last_revoke = 0;
6832
6833 retry:
6834 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6835 unsigned long id, tmp;
6836 struct nfs4_stid *stid;
6837
6838 if (atomic_read(&clp->cl_admin_revoked) == 0)
6839 continue;
6840
6841 spin_lock(&clp->cl_lock);
6842 idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id)
6843 if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) {
6844 refcount_inc(&stid->sc_count);
6845 spin_unlock(&nn->client_lock);
6846 /* this function drops ->cl_lock */
6847 nfsd4_drop_revoked_stid(stid);
6848 nfs4_put_stid(stid);
6849 spin_lock(&nn->client_lock);
6850 goto retry;
6851 }
6852 spin_unlock(&clp->cl_lock);
6853 }
6854 spin_unlock(&nn->client_lock);
6855 }
6856
6857 static time64_t
nfs4_laundromat(struct nfsd_net * nn)6858 nfs4_laundromat(struct nfsd_net *nn)
6859 {
6860 struct nfs4_openowner *oo;
6861 struct nfs4_delegation *dp;
6862 struct nfs4_ol_stateid *stp;
6863 struct nfsd4_blocked_lock *nbl;
6864 struct list_head *pos, *next, reaplist;
6865 struct laundry_time lt = {
6866 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
6867 .new_timeo = nn->nfsd4_lease
6868 };
6869 struct nfs4_cpntf_state *cps;
6870 copy_stateid_t *cps_t;
6871 int i;
6872
6873 if (clients_still_reclaiming(nn)) {
6874 lt.new_timeo = 0;
6875 goto out;
6876 }
6877 nfsd4_end_grace(nn);
6878
6879 spin_lock(&nn->s2s_cp_lock);
6880 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
6881 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
6882 if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID &&
6883 state_expired(<, cps->cpntf_time))
6884 _free_cpntf_state_locked(nn, cps);
6885 }
6886 spin_unlock(&nn->s2s_cp_lock);
6887 nfsd4_async_copy_reaper(nn);
6888 nfs4_get_client_reaplist(nn, &reaplist, <);
6889 nfs4_process_client_reaplist(&reaplist);
6890
6891 nfs40_clean_admin_revoked(nn, <);
6892
6893 spin_lock(&state_lock);
6894 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6895 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6896 if (!state_expired(<, dp->dl_time))
6897 break;
6898 refcount_inc(&dp->dl_stid.sc_count);
6899 unhash_delegation_locked(dp, SC_STATUS_REVOKED);
6900 list_add(&dp->dl_recall_lru, &reaplist);
6901 }
6902 spin_unlock(&state_lock);
6903 while (!list_empty(&reaplist)) {
6904 dp = list_first_entry(&reaplist, struct nfs4_delegation,
6905 dl_recall_lru);
6906 list_del_init(&dp->dl_recall_lru);
6907 revoke_delegation(dp);
6908 }
6909
6910 spin_lock(&nn->client_lock);
6911 while (!list_empty(&nn->close_lru)) {
6912 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
6913 oo_close_lru);
6914 if (!state_expired(<, oo->oo_time))
6915 break;
6916 list_del_init(&oo->oo_close_lru);
6917 stp = oo->oo_last_closed_stid;
6918 oo->oo_last_closed_stid = NULL;
6919 spin_unlock(&nn->client_lock);
6920 nfs4_put_stid(&stp->st_stid);
6921 spin_lock(&nn->client_lock);
6922 }
6923 spin_unlock(&nn->client_lock);
6924
6925 /*
6926 * It's possible for a client to try and acquire an already held lock
6927 * that is being held for a long time, and then lose interest in it.
6928 * So, we clean out any un-revisited request after a lease period
6929 * under the assumption that the client is no longer interested.
6930 *
6931 * RFC5661, sec. 9.6 states that the client must not rely on getting
6932 * notifications and must continue to poll for locks, even when the
6933 * server supports them. Thus this shouldn't lead to clients blocking
6934 * indefinitely once the lock does become free.
6935 */
6936 BUG_ON(!list_empty(&reaplist));
6937 spin_lock(&nn->blocked_locks_lock);
6938 while (!list_empty(&nn->blocked_locks_lru)) {
6939 nbl = list_first_entry(&nn->blocked_locks_lru,
6940 struct nfsd4_blocked_lock, nbl_lru);
6941 if (!state_expired(<, nbl->nbl_time))
6942 break;
6943 list_move(&nbl->nbl_lru, &reaplist);
6944 list_del_init(&nbl->nbl_list);
6945 }
6946 spin_unlock(&nn->blocked_locks_lock);
6947
6948 while (!list_empty(&reaplist)) {
6949 nbl = list_first_entry(&reaplist,
6950 struct nfsd4_blocked_lock, nbl_lru);
6951 list_del_init(&nbl->nbl_lru);
6952 free_blocked_lock(nbl);
6953 }
6954 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
6955 /* service the server-to-server copy delayed unmount list */
6956 nfsd4_ssc_expire_umount(nn);
6957 #endif
6958 if (atomic_long_read(&num_delegations) >= max_delegations)
6959 deleg_reaper(nn);
6960 out:
6961 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
6962 }
6963
6964 static void laundromat_main(struct work_struct *);
6965
6966 static void
laundromat_main(struct work_struct * laundry)6967 laundromat_main(struct work_struct *laundry)
6968 {
6969 time64_t t;
6970 struct delayed_work *dwork = to_delayed_work(laundry);
6971 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6972 laundromat_work);
6973
6974 t = nfs4_laundromat(nn);
6975 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
6976 }
6977
6978 static void
courtesy_client_reaper(struct nfsd_net * nn)6979 courtesy_client_reaper(struct nfsd_net *nn)
6980 {
6981 struct list_head reaplist;
6982
6983 nfs4_get_courtesy_client_reaplist(nn, &reaplist);
6984 nfs4_process_client_reaplist(&reaplist);
6985 }
6986
6987 static void
deleg_reaper(struct nfsd_net * nn)6988 deleg_reaper(struct nfsd_net *nn)
6989 {
6990 struct list_head *pos, *next;
6991 struct nfs4_client *clp;
6992
6993 spin_lock(&nn->client_lock);
6994 list_for_each_safe(pos, next, &nn->client_lru) {
6995 clp = list_entry(pos, struct nfs4_client, cl_lru);
6996
6997 if (clp->cl_state != NFSD4_ACTIVE)
6998 continue;
6999 if (list_empty(&clp->cl_delegations))
7000 continue;
7001 if (atomic_read(&clp->cl_delegs_in_recall))
7002 continue;
7003 if (test_and_set_bit(NFSD4_CALLBACK_RUNNING, &clp->cl_ra->ra_cb.cb_flags))
7004 continue;
7005 if (ktime_get_boottime_seconds() - clp->cl_ra_time < 5)
7006 continue;
7007 if (clp->cl_cb_state != NFSD4_CB_UP)
7008 continue;
7009
7010 /* release in nfsd4_cb_recall_any_release */
7011 kref_get(&clp->cl_nfsdfs.cl_ref);
7012 clp->cl_ra_time = ktime_get_boottime_seconds();
7013 clp->cl_ra->ra_keep = 0;
7014 clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG) |
7015 BIT(RCA4_TYPE_MASK_WDATA_DLG);
7016 trace_nfsd_cb_recall_any(clp->cl_ra);
7017 nfsd4_run_cb(&clp->cl_ra->ra_cb);
7018 }
7019 spin_unlock(&nn->client_lock);
7020 }
7021
7022 static void
nfsd4_state_shrinker_worker(struct work_struct * work)7023 nfsd4_state_shrinker_worker(struct work_struct *work)
7024 {
7025 struct nfsd_net *nn = container_of(work, struct nfsd_net,
7026 nfsd_shrinker_work);
7027
7028 courtesy_client_reaper(nn);
7029 deleg_reaper(nn);
7030 }
7031
nfs4_check_fh(struct svc_fh * fhp,struct nfs4_stid * stp)7032 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
7033 {
7034 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
7035 return nfserr_bad_stateid;
7036 return nfs_ok;
7037 }
7038
7039 static
nfs4_check_openmode(struct nfs4_ol_stateid * stp,int flags)7040 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
7041 {
7042 __be32 status = nfserr_openmode;
7043
7044 /* For lock stateid's, we test the parent open, not the lock: */
7045 if (stp->st_openstp)
7046 stp = stp->st_openstp;
7047 if ((flags & WR_STATE) && !access_permit_write(stp))
7048 goto out;
7049 if ((flags & RD_STATE) && !access_permit_read(stp))
7050 goto out;
7051 status = nfs_ok;
7052 out:
7053 return status;
7054 }
7055
7056 static inline __be32
check_special_stateids(struct net * net,svc_fh * current_fh,stateid_t * stateid,int flags)7057 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
7058 {
7059 if (ONE_STATEID(stateid) && (flags & RD_STATE))
7060 return nfs_ok;
7061 else if (opens_in_grace(net)) {
7062 /* Answer in remaining cases depends on existence of
7063 * conflicting state; so we must wait out the grace period. */
7064 return nfserr_grace;
7065 } else if (flags & WR_STATE)
7066 return nfs4_share_conflict(current_fh,
7067 NFS4_SHARE_DENY_WRITE);
7068 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
7069 return nfs4_share_conflict(current_fh,
7070 NFS4_SHARE_DENY_READ);
7071 }
7072
check_stateid_generation(stateid_t * in,stateid_t * ref,bool has_session)7073 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
7074 {
7075 /*
7076 * When sessions are used the stateid generation number is ignored
7077 * when it is zero.
7078 */
7079 if (has_session && in->si_generation == 0)
7080 return nfs_ok;
7081
7082 if (in->si_generation == ref->si_generation)
7083 return nfs_ok;
7084
7085 /* If the client sends us a stateid from the future, it's buggy: */
7086 if (nfsd4_stateid_generation_after(in, ref))
7087 return nfserr_bad_stateid;
7088 /*
7089 * However, we could see a stateid from the past, even from a
7090 * non-buggy client. For example, if the client sends a lock
7091 * while some IO is outstanding, the lock may bump si_generation
7092 * while the IO is still in flight. The client could avoid that
7093 * situation by waiting for responses on all the IO requests,
7094 * but better performance may result in retrying IO that
7095 * receives an old_stateid error if requests are rarely
7096 * reordered in flight:
7097 */
7098 return nfserr_old_stateid;
7099 }
7100
nfsd4_stid_check_stateid_generation(stateid_t * in,struct nfs4_stid * s,bool has_session)7101 static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
7102 {
7103 __be32 ret;
7104
7105 spin_lock(&s->sc_lock);
7106 ret = nfsd4_verify_open_stid(s);
7107 if (ret == nfs_ok)
7108 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
7109 spin_unlock(&s->sc_lock);
7110 if (ret == nfserr_admin_revoked)
7111 nfsd40_drop_revoked_stid(s->sc_client,
7112 &s->sc_stateid);
7113 return ret;
7114 }
7115
nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid * ols)7116 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
7117 {
7118 if (ols->st_stateowner->so_is_open_owner &&
7119 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
7120 return nfserr_bad_stateid;
7121 return nfs_ok;
7122 }
7123
nfsd4_validate_stateid(struct nfs4_client * cl,stateid_t * stateid)7124 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
7125 {
7126 struct nfs4_stid *s;
7127 __be32 status = nfserr_bad_stateid;
7128
7129 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
7130 CLOSE_STATEID(stateid))
7131 return status;
7132 spin_lock(&cl->cl_lock);
7133 s = find_stateid_locked(cl, stateid);
7134 if (!s)
7135 goto out_unlock;
7136 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
7137 if (status)
7138 goto out_unlock;
7139 status = nfsd4_verify_open_stid(s);
7140 if (status)
7141 goto out_unlock;
7142
7143 switch (s->sc_type) {
7144 case SC_TYPE_DELEG:
7145 status = nfs_ok;
7146 break;
7147 case SC_TYPE_OPEN:
7148 case SC_TYPE_LOCK:
7149 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
7150 break;
7151 default:
7152 printk("unknown stateid type %x\n", s->sc_type);
7153 status = nfserr_bad_stateid;
7154 }
7155 out_unlock:
7156 spin_unlock(&cl->cl_lock);
7157 if (status == nfserr_admin_revoked)
7158 nfsd40_drop_revoked_stid(cl, stateid);
7159 return status;
7160 }
7161
7162 __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)7163 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
7164 stateid_t *stateid,
7165 unsigned short typemask, unsigned short statusmask,
7166 struct nfs4_stid **s, struct nfsd_net *nn)
7167 {
7168 __be32 status;
7169 struct nfs4_stid *stid;
7170 bool return_revoked = false;
7171
7172 /*
7173 * only return revoked delegations if explicitly asked.
7174 * otherwise we report revoked or bad_stateid status.
7175 */
7176 if (statusmask & SC_STATUS_REVOKED)
7177 return_revoked = true;
7178 if (typemask & SC_TYPE_DELEG)
7179 /* Always allow REVOKED for DELEG so we can
7180 * return the appropriate error.
7181 */
7182 statusmask |= SC_STATUS_REVOKED;
7183
7184 statusmask |= SC_STATUS_ADMIN_REVOKED | SC_STATUS_FREEABLE;
7185
7186 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
7187 CLOSE_STATEID(stateid))
7188 return nfserr_bad_stateid;
7189 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
7190 if (status == nfserr_stale_clientid) {
7191 if (cstate->session)
7192 return nfserr_bad_stateid;
7193 return nfserr_stale_stateid;
7194 }
7195 if (status)
7196 return status;
7197 stid = find_stateid_by_type(cstate->clp, stateid, typemask, statusmask);
7198 if (!stid)
7199 return nfserr_bad_stateid;
7200 if ((stid->sc_status & SC_STATUS_REVOKED) && !return_revoked) {
7201 nfs4_put_stid(stid);
7202 return nfserr_deleg_revoked;
7203 }
7204 if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) {
7205 nfsd40_drop_revoked_stid(cstate->clp, stateid);
7206 nfs4_put_stid(stid);
7207 return nfserr_admin_revoked;
7208 }
7209 *s = stid;
7210 return nfs_ok;
7211 }
7212
7213 static struct nfsd_file *
nfs4_find_file(struct nfs4_stid * s,int flags)7214 nfs4_find_file(struct nfs4_stid *s, int flags)
7215 {
7216 struct nfsd_file *ret = NULL;
7217
7218 if (!s || s->sc_status)
7219 return NULL;
7220
7221 switch (s->sc_type) {
7222 case SC_TYPE_DELEG:
7223 case SC_TYPE_OPEN:
7224 case SC_TYPE_LOCK:
7225 if (flags & RD_STATE)
7226 ret = find_readable_file(s->sc_file);
7227 else
7228 ret = find_writeable_file(s->sc_file);
7229 }
7230
7231 return ret;
7232 }
7233
7234 static __be32
nfs4_check_olstateid(struct nfs4_ol_stateid * ols,int flags)7235 nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
7236 {
7237 __be32 status;
7238
7239 status = nfsd4_check_openowner_confirmed(ols);
7240 if (status)
7241 return status;
7242 return nfs4_check_openmode(ols, flags);
7243 }
7244
7245 static __be32
nfs4_check_file(struct svc_rqst * rqstp,struct svc_fh * fhp,struct nfs4_stid * s,struct nfsd_file ** nfp,int flags)7246 nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
7247 struct nfsd_file **nfp, int flags)
7248 {
7249 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
7250 struct nfsd_file *nf;
7251 __be32 status;
7252
7253 nf = nfs4_find_file(s, flags);
7254 if (nf) {
7255 status = nfsd_permission(&rqstp->rq_cred,
7256 fhp->fh_export, fhp->fh_dentry,
7257 acc | NFSD_MAY_OWNER_OVERRIDE);
7258 if (status) {
7259 nfsd_file_put(nf);
7260 goto out;
7261 }
7262 } else {
7263 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
7264 if (status)
7265 return status;
7266 }
7267 *nfp = nf;
7268 out:
7269 return status;
7270 }
7271 static void
_free_cpntf_state_locked(struct nfsd_net * nn,struct nfs4_cpntf_state * cps)7272 _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
7273 {
7274 WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID);
7275 if (!refcount_dec_and_test(&cps->cp_stateid.cs_count))
7276 return;
7277 list_del(&cps->cp_list);
7278 idr_remove(&nn->s2s_cp_stateids,
7279 cps->cp_stateid.cs_stid.si_opaque.so_id);
7280 kfree(cps);
7281 }
7282 /*
7283 * A READ from an inter server to server COPY will have a
7284 * copy stateid. Look up the copy notify stateid from the
7285 * idr structure and take a reference on it.
7286 */
manage_cpntf_state(struct nfsd_net * nn,stateid_t * st,struct nfs4_client * clp,struct nfs4_cpntf_state ** cps)7287 __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
7288 struct nfs4_client *clp,
7289 struct nfs4_cpntf_state **cps)
7290 {
7291 copy_stateid_t *cps_t;
7292 struct nfs4_cpntf_state *state = NULL;
7293
7294 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
7295 return nfserr_bad_stateid;
7296 spin_lock(&nn->s2s_cp_lock);
7297 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
7298 if (cps_t) {
7299 state = container_of(cps_t, struct nfs4_cpntf_state,
7300 cp_stateid);
7301 if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) {
7302 state = NULL;
7303 goto unlock;
7304 }
7305 if (!clp)
7306 refcount_inc(&state->cp_stateid.cs_count);
7307 else
7308 _free_cpntf_state_locked(nn, state);
7309 }
7310 unlock:
7311 spin_unlock(&nn->s2s_cp_lock);
7312 if (!state)
7313 return nfserr_bad_stateid;
7314 if (!clp)
7315 *cps = state;
7316 return 0;
7317 }
7318
find_cpntf_state(struct nfsd_net * nn,stateid_t * st,struct nfs4_stid ** stid)7319 static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
7320 struct nfs4_stid **stid)
7321 {
7322 __be32 status;
7323 struct nfs4_cpntf_state *cps = NULL;
7324 struct nfs4_client *found;
7325
7326 status = manage_cpntf_state(nn, st, NULL, &cps);
7327 if (status)
7328 return status;
7329
7330 cps->cpntf_time = ktime_get_boottime_seconds();
7331
7332 status = nfserr_expired;
7333 found = lookup_clientid(&cps->cp_p_clid, true, nn);
7334 if (!found)
7335 goto out;
7336
7337 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
7338 SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK,
7339 0);
7340 if (*stid)
7341 status = nfs_ok;
7342 else
7343 status = nfserr_bad_stateid;
7344
7345 put_client_renew(found);
7346 out:
7347 nfs4_put_cpntf_state(nn, cps);
7348 return status;
7349 }
7350
nfs4_put_cpntf_state(struct nfsd_net * nn,struct nfs4_cpntf_state * cps)7351 void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
7352 {
7353 spin_lock(&nn->s2s_cp_lock);
7354 _free_cpntf_state_locked(nn, cps);
7355 spin_unlock(&nn->s2s_cp_lock);
7356 }
7357
7358 /**
7359 * nfs4_preprocess_stateid_op - find and prep stateid for an operation
7360 * @rqstp: incoming request from client
7361 * @cstate: current compound state
7362 * @fhp: filehandle associated with requested stateid
7363 * @stateid: stateid (provided by client)
7364 * @flags: flags describing type of operation to be done
7365 * @nfp: optional nfsd_file return pointer (may be NULL)
7366 * @cstid: optional returned nfs4_stid pointer (may be NULL)
7367 *
7368 * Given info from the client, look up a nfs4_stid for the operation. On
7369 * success, it returns a reference to the nfs4_stid and/or the nfsd_file
7370 * associated with it.
7371 */
7372 __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)7373 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
7374 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
7375 stateid_t *stateid, int flags, struct nfsd_file **nfp,
7376 struct nfs4_stid **cstid)
7377 {
7378 struct net *net = SVC_NET(rqstp);
7379 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7380 struct nfs4_stid *s = NULL;
7381 __be32 status;
7382
7383 if (nfp)
7384 *nfp = NULL;
7385
7386 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
7387 status = check_special_stateids(net, fhp, stateid, flags);
7388 goto done;
7389 }
7390
7391 status = nfsd4_lookup_stateid(cstate, stateid,
7392 SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK,
7393 0, &s, nn);
7394 if (status == nfserr_bad_stateid)
7395 status = find_cpntf_state(nn, stateid, &s);
7396 if (status)
7397 return status;
7398 status = nfsd4_stid_check_stateid_generation(stateid, s,
7399 nfsd4_has_session(cstate));
7400 if (status)
7401 goto out;
7402
7403 switch (s->sc_type) {
7404 case SC_TYPE_DELEG:
7405 status = nfs4_check_delegmode(delegstateid(s), flags);
7406 break;
7407 case SC_TYPE_OPEN:
7408 case SC_TYPE_LOCK:
7409 status = nfs4_check_olstateid(openlockstateid(s), flags);
7410 break;
7411 }
7412 if (status)
7413 goto out;
7414 status = nfs4_check_fh(fhp, s);
7415
7416 done:
7417 if (status == nfs_ok && nfp)
7418 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
7419 out:
7420 if (s) {
7421 if (!status && cstid)
7422 *cstid = s;
7423 else
7424 nfs4_put_stid(s);
7425 }
7426 return status;
7427 }
7428
7429 /*
7430 * Test if the stateid is valid
7431 */
7432 __be32
nfsd4_test_stateid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7433 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7434 union nfsd4_op_u *u)
7435 {
7436 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
7437 struct nfsd4_test_stateid_id *stateid;
7438 struct nfs4_client *cl = cstate->clp;
7439
7440 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
7441 stateid->ts_id_status =
7442 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
7443
7444 return nfs_ok;
7445 }
7446
7447 static __be32
nfsd4_free_lock_stateid(stateid_t * stateid,struct nfs4_stid * s)7448 nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
7449 {
7450 struct nfs4_ol_stateid *stp = openlockstateid(s);
7451 __be32 ret;
7452
7453 ret = nfsd4_lock_ol_stateid(stp);
7454 if (ret)
7455 goto out_put_stid;
7456
7457 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
7458 if (ret)
7459 goto out;
7460
7461 ret = nfserr_locks_held;
7462 if (check_for_locks(stp->st_stid.sc_file,
7463 lockowner(stp->st_stateowner)))
7464 goto out;
7465
7466 release_lock_stateid(stp);
7467 ret = nfs_ok;
7468
7469 out:
7470 mutex_unlock(&stp->st_mutex);
7471 out_put_stid:
7472 nfs4_put_stid(s);
7473 return ret;
7474 }
7475
7476 __be32
nfsd4_free_stateid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7477 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7478 union nfsd4_op_u *u)
7479 {
7480 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
7481 stateid_t *stateid = &free_stateid->fr_stateid;
7482 struct nfs4_stid *s;
7483 struct nfs4_delegation *dp;
7484 struct nfs4_client *cl = cstate->clp;
7485 __be32 ret = nfserr_bad_stateid;
7486
7487 spin_lock(&cl->cl_lock);
7488 s = find_stateid_locked(cl, stateid);
7489 if (!s || s->sc_status & SC_STATUS_CLOSED)
7490 goto out_unlock;
7491 if (s->sc_status & SC_STATUS_ADMIN_REVOKED) {
7492 nfsd4_drop_revoked_stid(s);
7493 ret = nfs_ok;
7494 goto out;
7495 }
7496 spin_lock(&s->sc_lock);
7497 switch (s->sc_type) {
7498 case SC_TYPE_DELEG:
7499 if (s->sc_status & SC_STATUS_REVOKED) {
7500 s->sc_status |= SC_STATUS_CLOSED;
7501 spin_unlock(&s->sc_lock);
7502 dp = delegstateid(s);
7503 if (s->sc_status & SC_STATUS_FREEABLE)
7504 list_del_init(&dp->dl_recall_lru);
7505 s->sc_status |= SC_STATUS_FREED;
7506 spin_unlock(&cl->cl_lock);
7507 nfs4_put_stid(s);
7508 ret = nfs_ok;
7509 goto out;
7510 }
7511 ret = nfserr_locks_held;
7512 break;
7513 case SC_TYPE_OPEN:
7514 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
7515 if (ret)
7516 break;
7517 ret = nfserr_locks_held;
7518 break;
7519 case SC_TYPE_LOCK:
7520 spin_unlock(&s->sc_lock);
7521 refcount_inc(&s->sc_count);
7522 spin_unlock(&cl->cl_lock);
7523 ret = nfsd4_free_lock_stateid(stateid, s);
7524 goto out;
7525 }
7526 spin_unlock(&s->sc_lock);
7527 out_unlock:
7528 spin_unlock(&cl->cl_lock);
7529 out:
7530 return ret;
7531 }
7532
7533 static inline int
setlkflg(int type)7534 setlkflg (int type)
7535 {
7536 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
7537 RD_STATE : WR_STATE;
7538 }
7539
nfs4_seqid_op_checks(struct nfsd4_compound_state * cstate,stateid_t * stateid,u32 seqid,struct nfs4_ol_stateid * stp)7540 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
7541 {
7542 struct svc_fh *current_fh = &cstate->current_fh;
7543 struct nfs4_stateowner *sop = stp->st_stateowner;
7544 __be32 status;
7545
7546 status = nfsd4_check_seqid(cstate, sop, seqid);
7547 if (status)
7548 return status;
7549 status = nfsd4_lock_ol_stateid(stp);
7550 if (status != nfs_ok)
7551 return status;
7552 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
7553 if (status == nfs_ok)
7554 status = nfs4_check_fh(current_fh, &stp->st_stid);
7555 if (status != nfs_ok)
7556 mutex_unlock(&stp->st_mutex);
7557 return status;
7558 }
7559
7560 /**
7561 * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op
7562 * @cstate: compund state
7563 * @seqid: seqid (provided by client)
7564 * @stateid: stateid (provided by client)
7565 * @typemask: mask of allowable types for this operation
7566 * @statusmask: mask of allowed states: 0 or STID_CLOSED
7567 * @stpp: return pointer for the stateid found
7568 * @nn: net namespace for request
7569 *
7570 * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and
7571 * return it in @stpp. On a nfs_ok return, the returned stateid will
7572 * have its st_mutex locked.
7573 */
7574 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)7575 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
7576 stateid_t *stateid,
7577 unsigned short typemask, unsigned short statusmask,
7578 struct nfs4_ol_stateid **stpp,
7579 struct nfsd_net *nn)
7580 {
7581 __be32 status;
7582 struct nfs4_stid *s;
7583 struct nfs4_ol_stateid *stp = NULL;
7584
7585 trace_nfsd_preprocess(seqid, stateid);
7586
7587 *stpp = NULL;
7588 retry:
7589 status = nfsd4_lookup_stateid(cstate, stateid,
7590 typemask, statusmask, &s, nn);
7591 if (status)
7592 return status;
7593 stp = openlockstateid(s);
7594 if (nfsd4_cstate_assign_replay(cstate, stp->st_stateowner) == -EAGAIN) {
7595 nfs4_put_stateowner(stp->st_stateowner);
7596 goto retry;
7597 }
7598
7599 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
7600 if (!status)
7601 *stpp = stp;
7602 else
7603 nfs4_put_stid(&stp->st_stid);
7604 return status;
7605 }
7606
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state * cstate,u32 seqid,stateid_t * stateid,struct nfs4_ol_stateid ** stpp,struct nfsd_net * nn)7607 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
7608 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
7609 {
7610 __be32 status;
7611 struct nfs4_openowner *oo;
7612 struct nfs4_ol_stateid *stp;
7613
7614 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
7615 SC_TYPE_OPEN, 0, &stp, nn);
7616 if (status)
7617 return status;
7618 oo = openowner(stp->st_stateowner);
7619 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
7620 mutex_unlock(&stp->st_mutex);
7621 nfs4_put_stid(&stp->st_stid);
7622 return nfserr_bad_stateid;
7623 }
7624 *stpp = stp;
7625 return nfs_ok;
7626 }
7627
7628 __be32
nfsd4_open_confirm(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7629 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7630 union nfsd4_op_u *u)
7631 {
7632 struct nfsd4_open_confirm *oc = &u->open_confirm;
7633 __be32 status;
7634 struct nfs4_openowner *oo;
7635 struct nfs4_ol_stateid *stp;
7636 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7637
7638 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
7639 cstate->current_fh.fh_dentry);
7640
7641 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
7642 if (status)
7643 return status;
7644
7645 status = nfs4_preprocess_seqid_op(cstate,
7646 oc->oc_seqid, &oc->oc_req_stateid,
7647 SC_TYPE_OPEN, 0, &stp, nn);
7648 if (status)
7649 goto out;
7650 oo = openowner(stp->st_stateowner);
7651 status = nfserr_bad_stateid;
7652 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
7653 mutex_unlock(&stp->st_mutex);
7654 goto put_stateid;
7655 }
7656 oo->oo_flags |= NFS4_OO_CONFIRMED;
7657 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
7658 mutex_unlock(&stp->st_mutex);
7659 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
7660 nfsd4_client_record_create(oo->oo_owner.so_client);
7661 status = nfs_ok;
7662 put_stateid:
7663 nfs4_put_stid(&stp->st_stid);
7664 out:
7665 nfsd4_bump_seqid(cstate, status);
7666 return status;
7667 }
7668
nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid * stp,u32 access)7669 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
7670 {
7671 if (!test_access(access, stp))
7672 return;
7673 nfs4_file_put_access(stp->st_stid.sc_file, access);
7674 clear_access(access, stp);
7675 }
7676
nfs4_stateid_downgrade(struct nfs4_ol_stateid * stp,u32 to_access)7677 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
7678 {
7679 switch (to_access) {
7680 case NFS4_SHARE_ACCESS_READ:
7681 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
7682 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7683 break;
7684 case NFS4_SHARE_ACCESS_WRITE:
7685 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
7686 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
7687 break;
7688 case NFS4_SHARE_ACCESS_BOTH:
7689 break;
7690 default:
7691 WARN_ON_ONCE(1);
7692 }
7693 }
7694
7695 __be32
nfsd4_open_downgrade(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7696 nfsd4_open_downgrade(struct svc_rqst *rqstp,
7697 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
7698 {
7699 struct nfsd4_open_downgrade *od = &u->open_downgrade;
7700 __be32 status;
7701 struct nfs4_ol_stateid *stp;
7702 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7703
7704 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
7705 cstate->current_fh.fh_dentry);
7706
7707 /* We don't yet support WANT bits: */
7708 if (od->od_deleg_want)
7709 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
7710 od->od_deleg_want);
7711
7712 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
7713 &od->od_stateid, &stp, nn);
7714 if (status)
7715 goto out;
7716 status = nfserr_inval;
7717 if (!test_access(od->od_share_access, stp)) {
7718 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
7719 stp->st_access_bmap, od->od_share_access);
7720 goto put_stateid;
7721 }
7722 if (!test_deny(od->od_share_deny, stp)) {
7723 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
7724 stp->st_deny_bmap, od->od_share_deny);
7725 goto put_stateid;
7726 }
7727 nfs4_stateid_downgrade(stp, od->od_share_access);
7728 reset_union_bmap_deny(od->od_share_deny, stp);
7729 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
7730 status = nfs_ok;
7731 put_stateid:
7732 mutex_unlock(&stp->st_mutex);
7733 nfs4_put_stid(&stp->st_stid);
7734 out:
7735 nfsd4_bump_seqid(cstate, status);
7736 return status;
7737 }
7738
nfsd4_close_open_stateid(struct nfs4_ol_stateid * s)7739 static bool nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
7740 {
7741 struct nfs4_client *clp = s->st_stid.sc_client;
7742 bool unhashed;
7743 LIST_HEAD(reaplist);
7744 struct nfs4_ol_stateid *stp;
7745
7746 spin_lock(&clp->cl_lock);
7747 unhashed = unhash_open_stateid(s, &reaplist);
7748
7749 if (clp->cl_minorversion) {
7750 if (unhashed)
7751 put_ol_stateid_locked(s, &reaplist);
7752 spin_unlock(&clp->cl_lock);
7753 list_for_each_entry(stp, &reaplist, st_locks)
7754 nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
7755 free_ol_stateid_reaplist(&reaplist);
7756 return false;
7757 } else {
7758 spin_unlock(&clp->cl_lock);
7759 free_ol_stateid_reaplist(&reaplist);
7760 return unhashed;
7761 }
7762 }
7763
7764 /*
7765 * nfs4_unlock_state() called after encode
7766 */
7767 __be32
nfsd4_close(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7768 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7769 union nfsd4_op_u *u)
7770 {
7771 struct nfsd4_close *close = &u->close;
7772 __be32 status;
7773 struct nfs4_ol_stateid *stp;
7774 struct net *net = SVC_NET(rqstp);
7775 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7776 bool need_move_to_close_list;
7777
7778 dprintk("NFSD: nfsd4_close on file %pd\n",
7779 cstate->current_fh.fh_dentry);
7780
7781 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
7782 &close->cl_stateid,
7783 SC_TYPE_OPEN, SC_STATUS_CLOSED,
7784 &stp, nn);
7785 nfsd4_bump_seqid(cstate, status);
7786 if (status)
7787 goto out;
7788
7789 spin_lock(&stp->st_stid.sc_client->cl_lock);
7790 stp->st_stid.sc_status |= SC_STATUS_CLOSED;
7791 spin_unlock(&stp->st_stid.sc_client->cl_lock);
7792
7793 /*
7794 * Technically we don't _really_ have to increment or copy it, since
7795 * it should just be gone after this operation and we clobber the
7796 * copied value below, but we continue to do so here just to ensure
7797 * that racing ops see that there was a state change.
7798 */
7799 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
7800
7801 need_move_to_close_list = nfsd4_close_open_stateid(stp);
7802 mutex_unlock(&stp->st_mutex);
7803 if (need_move_to_close_list)
7804 move_to_close_lru(stp, net);
7805
7806 /* v4.1+ suggests that we send a special stateid in here, since the
7807 * clients should just ignore this anyway. Since this is not useful
7808 * for v4.0 clients either, we set it to the special close_stateid
7809 * universally.
7810 *
7811 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
7812 */
7813 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
7814
7815 /* put reference from nfs4_preprocess_seqid_op */
7816 nfs4_put_stid(&stp->st_stid);
7817 out:
7818 return status;
7819 }
7820
7821 __be32
nfsd4_delegreturn(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7822 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7823 union nfsd4_op_u *u)
7824 {
7825 struct nfsd4_delegreturn *dr = &u->delegreturn;
7826 struct nfs4_delegation *dp;
7827 stateid_t *stateid = &dr->dr_stateid;
7828 struct nfs4_stid *s;
7829 __be32 status;
7830 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7831
7832 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7833 return status;
7834
7835 status = nfsd4_lookup_stateid(cstate, stateid, SC_TYPE_DELEG, SC_STATUS_REVOKED, &s, nn);
7836 if (status)
7837 goto out;
7838 dp = delegstateid(s);
7839 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
7840 if (status)
7841 goto put_stateid;
7842
7843 trace_nfsd_deleg_return(stateid);
7844 destroy_delegation(dp);
7845 smp_mb__after_atomic();
7846 wake_up_var(d_inode(cstate->current_fh.fh_dentry));
7847 put_stateid:
7848 nfs4_put_stid(&dp->dl_stid);
7849 out:
7850 return status;
7851 }
7852
7853 /* last octet in a range */
7854 static inline u64
last_byte_offset(u64 start,u64 len)7855 last_byte_offset(u64 start, u64 len)
7856 {
7857 u64 end;
7858
7859 WARN_ON_ONCE(!len);
7860 end = start + len;
7861 return end > start ? end - 1: NFS4_MAX_UINT64;
7862 }
7863
7864 /*
7865 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
7866 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
7867 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
7868 * locking, this prevents us from being completely protocol-compliant. The
7869 * real solution to this problem is to start using unsigned file offsets in
7870 * the VFS, but this is a very deep change!
7871 */
7872 static inline void
nfs4_transform_lock_offset(struct file_lock * lock)7873 nfs4_transform_lock_offset(struct file_lock *lock)
7874 {
7875 if (lock->fl_start < 0)
7876 lock->fl_start = OFFSET_MAX;
7877 if (lock->fl_end < 0)
7878 lock->fl_end = OFFSET_MAX;
7879 }
7880
7881 static fl_owner_t
nfsd4_lm_get_owner(fl_owner_t owner)7882 nfsd4_lm_get_owner(fl_owner_t owner)
7883 {
7884 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7885
7886 nfs4_get_stateowner(&lo->lo_owner);
7887 return owner;
7888 }
7889
7890 static void
nfsd4_lm_put_owner(fl_owner_t owner)7891 nfsd4_lm_put_owner(fl_owner_t owner)
7892 {
7893 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7894
7895 if (lo)
7896 nfs4_put_stateowner(&lo->lo_owner);
7897 }
7898
7899 /* return pointer to struct nfs4_client if client is expirable */
7900 static bool
nfsd4_lm_lock_expirable(struct file_lock * cfl)7901 nfsd4_lm_lock_expirable(struct file_lock *cfl)
7902 {
7903 struct nfs4_lockowner *lo = (struct nfs4_lockowner *) cfl->c.flc_owner;
7904 struct nfs4_client *clp = lo->lo_owner.so_client;
7905 struct nfsd_net *nn;
7906
7907 if (try_to_expire_client(clp)) {
7908 nn = net_generic(clp->net, nfsd_net_id);
7909 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
7910 return true;
7911 }
7912 return false;
7913 }
7914
7915 /* schedule laundromat to run immediately and wait for it to complete */
7916 static void
nfsd4_lm_expire_lock(void)7917 nfsd4_lm_expire_lock(void)
7918 {
7919 flush_workqueue(laundry_wq);
7920 }
7921
7922 static void
nfsd4_lm_notify(struct file_lock * fl)7923 nfsd4_lm_notify(struct file_lock *fl)
7924 {
7925 struct nfs4_lockowner *lo = (struct nfs4_lockowner *) fl->c.flc_owner;
7926 struct net *net = lo->lo_owner.so_client->net;
7927 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7928 struct nfsd4_blocked_lock *nbl = container_of(fl,
7929 struct nfsd4_blocked_lock, nbl_lock);
7930 bool queue = false;
7931
7932 /* An empty list means that something else is going to be using it */
7933 spin_lock(&nn->blocked_locks_lock);
7934 if (!list_empty(&nbl->nbl_list)) {
7935 list_del_init(&nbl->nbl_list);
7936 list_del_init(&nbl->nbl_lru);
7937 queue = true;
7938 }
7939 spin_unlock(&nn->blocked_locks_lock);
7940
7941 if (queue) {
7942 trace_nfsd_cb_notify_lock(lo, nbl);
7943 nfsd4_try_run_cb(&nbl->nbl_cb);
7944 }
7945 }
7946
7947 static const struct lock_manager_operations nfsd_posix_mng_ops = {
7948 .lm_mod_owner = THIS_MODULE,
7949 .lm_notify = nfsd4_lm_notify,
7950 .lm_get_owner = nfsd4_lm_get_owner,
7951 .lm_put_owner = nfsd4_lm_put_owner,
7952 .lm_lock_expirable = nfsd4_lm_lock_expirable,
7953 .lm_expire_lock = nfsd4_lm_expire_lock,
7954 };
7955
7956 static inline void
nfs4_set_lock_denied(struct file_lock * fl,struct nfsd4_lock_denied * deny)7957 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
7958 {
7959 struct nfs4_lockowner *lo;
7960
7961 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
7962 lo = (struct nfs4_lockowner *) fl->c.flc_owner;
7963 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
7964 GFP_KERNEL);
7965 if (!deny->ld_owner.data)
7966 /* We just don't care that much */
7967 goto nevermind;
7968 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
7969 } else {
7970 nevermind:
7971 deny->ld_owner.len = 0;
7972 deny->ld_owner.data = NULL;
7973 deny->ld_clientid.cl_boot = 0;
7974 deny->ld_clientid.cl_id = 0;
7975 }
7976 deny->ld_start = fl->fl_start;
7977 deny->ld_length = NFS4_MAX_UINT64;
7978 if (fl->fl_end != NFS4_MAX_UINT64)
7979 deny->ld_length = fl->fl_end - fl->fl_start + 1;
7980 deny->ld_type = NFS4_READ_LT;
7981 if (fl->c.flc_type != F_RDLCK)
7982 deny->ld_type = NFS4_WRITE_LT;
7983 }
7984
7985 static struct nfs4_lockowner *
find_lockowner_str_locked(struct nfs4_client * clp,struct xdr_netobj * owner)7986 find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
7987 {
7988 unsigned int strhashval = ownerstr_hashval(owner);
7989 struct nfs4_stateowner *so;
7990
7991 lockdep_assert_held(&clp->cl_lock);
7992
7993 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
7994 so_strhash) {
7995 if (so->so_is_open_owner)
7996 continue;
7997 if (same_owner_str(so, owner))
7998 return lockowner(nfs4_get_stateowner(so));
7999 }
8000 return NULL;
8001 }
8002
8003 static struct nfs4_lockowner *
find_lockowner_str(struct nfs4_client * clp,struct xdr_netobj * owner)8004 find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
8005 {
8006 struct nfs4_lockowner *lo;
8007
8008 spin_lock(&clp->cl_lock);
8009 lo = find_lockowner_str_locked(clp, owner);
8010 spin_unlock(&clp->cl_lock);
8011 return lo;
8012 }
8013
nfs4_unhash_lockowner(struct nfs4_stateowner * sop)8014 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
8015 {
8016 unhash_lockowner_locked(lockowner(sop));
8017 }
8018
nfs4_free_lockowner(struct nfs4_stateowner * sop)8019 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
8020 {
8021 struct nfs4_lockowner *lo = lockowner(sop);
8022
8023 kmem_cache_free(lockowner_slab, lo);
8024 }
8025
8026 static const struct nfs4_stateowner_operations lockowner_ops = {
8027 .so_unhash = nfs4_unhash_lockowner,
8028 .so_free = nfs4_free_lockowner,
8029 };
8030
8031 /*
8032 * Alloc a lock owner structure.
8033 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
8034 * occurred.
8035 *
8036 * strhashval = ownerstr_hashval
8037 */
8038 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)8039 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
8040 struct nfs4_ol_stateid *open_stp,
8041 struct nfsd4_lock *lock)
8042 {
8043 struct nfs4_lockowner *lo, *ret;
8044
8045 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
8046 if (!lo)
8047 return NULL;
8048 INIT_LIST_HEAD(&lo->lo_blocked);
8049 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
8050 lo->lo_owner.so_is_open_owner = 0;
8051 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
8052 lo->lo_owner.so_ops = &lockowner_ops;
8053 spin_lock(&clp->cl_lock);
8054 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
8055 if (ret == NULL) {
8056 list_add(&lo->lo_owner.so_strhash,
8057 &clp->cl_ownerstr_hashtbl[strhashval]);
8058 ret = lo;
8059 } else
8060 nfs4_free_stateowner(&lo->lo_owner);
8061
8062 spin_unlock(&clp->cl_lock);
8063 return ret;
8064 }
8065
8066 static struct nfs4_ol_stateid *
find_lock_stateid(const struct nfs4_lockowner * lo,const struct nfs4_ol_stateid * ost)8067 find_lock_stateid(const struct nfs4_lockowner *lo,
8068 const struct nfs4_ol_stateid *ost)
8069 {
8070 struct nfs4_ol_stateid *lst;
8071
8072 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
8073
8074 /* If ost is not hashed, ost->st_locks will not be valid */
8075 if (!nfs4_ol_stateid_unhashed(ost))
8076 list_for_each_entry(lst, &ost->st_locks, st_locks) {
8077 if (lst->st_stateowner == &lo->lo_owner) {
8078 refcount_inc(&lst->st_stid.sc_count);
8079 return lst;
8080 }
8081 }
8082 return NULL;
8083 }
8084
8085 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)8086 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
8087 struct nfs4_file *fp, struct inode *inode,
8088 struct nfs4_ol_stateid *open_stp)
8089 {
8090 struct nfs4_client *clp = lo->lo_owner.so_client;
8091 struct nfs4_ol_stateid *retstp;
8092
8093 mutex_init(&stp->st_mutex);
8094 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
8095 retry:
8096 spin_lock(&clp->cl_lock);
8097 if (nfs4_ol_stateid_unhashed(open_stp))
8098 goto out_close;
8099 retstp = find_lock_stateid(lo, open_stp);
8100 if (retstp)
8101 goto out_found;
8102 refcount_inc(&stp->st_stid.sc_count);
8103 stp->st_stid.sc_type = SC_TYPE_LOCK;
8104 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
8105 get_nfs4_file(fp);
8106 stp->st_stid.sc_file = fp;
8107 stp->st_access_bmap = 0;
8108 stp->st_deny_bmap = open_stp->st_deny_bmap;
8109 stp->st_openstp = open_stp;
8110 spin_lock(&fp->fi_lock);
8111 list_add(&stp->st_locks, &open_stp->st_locks);
8112 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
8113 list_add(&stp->st_perfile, &fp->fi_stateids);
8114 spin_unlock(&fp->fi_lock);
8115 spin_unlock(&clp->cl_lock);
8116 return stp;
8117 out_found:
8118 spin_unlock(&clp->cl_lock);
8119 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
8120 nfs4_put_stid(&retstp->st_stid);
8121 goto retry;
8122 }
8123 /* To keep mutex tracking happy */
8124 mutex_unlock(&stp->st_mutex);
8125 return retstp;
8126 out_close:
8127 spin_unlock(&clp->cl_lock);
8128 mutex_unlock(&stp->st_mutex);
8129 return NULL;
8130 }
8131
8132 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)8133 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
8134 struct inode *inode, struct nfs4_ol_stateid *ost,
8135 bool *new)
8136 {
8137 struct nfs4_stid *ns = NULL;
8138 struct nfs4_ol_stateid *lst;
8139 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
8140 struct nfs4_client *clp = oo->oo_owner.so_client;
8141
8142 *new = false;
8143 spin_lock(&clp->cl_lock);
8144 lst = find_lock_stateid(lo, ost);
8145 spin_unlock(&clp->cl_lock);
8146 if (lst != NULL) {
8147 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
8148 goto out;
8149 nfs4_put_stid(&lst->st_stid);
8150 }
8151 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
8152 if (ns == NULL)
8153 return NULL;
8154
8155 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
8156 if (lst == openlockstateid(ns))
8157 *new = true;
8158 else
8159 nfs4_put_stid(ns);
8160 out:
8161 return lst;
8162 }
8163
8164 static int
check_lock_length(u64 offset,u64 length)8165 check_lock_length(u64 offset, u64 length)
8166 {
8167 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
8168 (length > ~offset)));
8169 }
8170
get_lock_access(struct nfs4_ol_stateid * lock_stp,u32 access)8171 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
8172 {
8173 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
8174
8175 lockdep_assert_held(&fp->fi_lock);
8176
8177 if (test_access(access, lock_stp))
8178 return;
8179 __nfs4_file_get_access(fp, access);
8180 set_access(access, lock_stp);
8181 }
8182
8183 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)8184 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
8185 struct nfs4_ol_stateid *ost,
8186 struct nfsd4_lock *lock,
8187 struct nfs4_ol_stateid **plst, bool *new)
8188 {
8189 __be32 status;
8190 struct nfs4_file *fi = ost->st_stid.sc_file;
8191 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
8192 struct nfs4_client *cl = oo->oo_owner.so_client;
8193 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
8194 struct nfs4_lockowner *lo;
8195 struct nfs4_ol_stateid *lst;
8196 unsigned int strhashval;
8197
8198 lo = find_lockowner_str(cl, &lock->lk_new_owner);
8199 if (!lo) {
8200 strhashval = ownerstr_hashval(&lock->lk_new_owner);
8201 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
8202 if (lo == NULL)
8203 return nfserr_jukebox;
8204 } else {
8205 /* with an existing lockowner, seqids must be the same */
8206 status = nfserr_bad_seqid;
8207 if (!cstate->minorversion &&
8208 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
8209 goto out;
8210 }
8211
8212 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
8213 if (lst == NULL) {
8214 status = nfserr_jukebox;
8215 goto out;
8216 }
8217
8218 status = nfs_ok;
8219 *plst = lst;
8220 out:
8221 nfs4_put_stateowner(&lo->lo_owner);
8222 return status;
8223 }
8224
8225 /*
8226 * LOCK operation
8227 */
8228 __be32
nfsd4_lock(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8229 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
8230 union nfsd4_op_u *u)
8231 {
8232 struct nfsd4_lock *lock = &u->lock;
8233 struct nfs4_openowner *open_sop = NULL;
8234 struct nfs4_lockowner *lock_sop = NULL;
8235 struct nfs4_ol_stateid *lock_stp = NULL;
8236 struct nfs4_ol_stateid *open_stp = NULL;
8237 struct nfs4_file *fp;
8238 struct nfsd_file *nf = NULL;
8239 struct nfsd4_blocked_lock *nbl = NULL;
8240 struct file_lock *file_lock = NULL;
8241 struct file_lock *conflock = NULL;
8242 __be32 status = 0;
8243 int lkflg;
8244 int err;
8245 bool new = false;
8246 unsigned char type;
8247 unsigned int flags = FL_POSIX;
8248 struct net *net = SVC_NET(rqstp);
8249 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8250
8251 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
8252 (long long) lock->lk_offset,
8253 (long long) lock->lk_length);
8254
8255 if (check_lock_length(lock->lk_offset, lock->lk_length))
8256 return nfserr_inval;
8257
8258 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
8259 if (status != nfs_ok)
8260 return status;
8261 if (exportfs_cannot_lock(cstate->current_fh.fh_dentry->d_sb->s_export_op)) {
8262 status = nfserr_notsupp;
8263 goto out;
8264 }
8265
8266 if (lock->lk_is_new) {
8267 if (nfsd4_has_session(cstate))
8268 /* See rfc 5661 18.10.3: given clientid is ignored: */
8269 memcpy(&lock->lk_new_clientid,
8270 &cstate->clp->cl_clientid,
8271 sizeof(clientid_t));
8272
8273 /* validate and update open stateid and open seqid */
8274 status = nfs4_preprocess_confirmed_seqid_op(cstate,
8275 lock->lk_new_open_seqid,
8276 &lock->lk_new_open_stateid,
8277 &open_stp, nn);
8278 if (status)
8279 goto out;
8280 mutex_unlock(&open_stp->st_mutex);
8281 open_sop = openowner(open_stp->st_stateowner);
8282 status = nfserr_bad_stateid;
8283 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
8284 &lock->lk_new_clientid))
8285 goto out;
8286 status = lookup_or_create_lock_state(cstate, open_stp, lock,
8287 &lock_stp, &new);
8288 } else {
8289 status = nfs4_preprocess_seqid_op(cstate,
8290 lock->lk_old_lock_seqid,
8291 &lock->lk_old_lock_stateid,
8292 SC_TYPE_LOCK, 0, &lock_stp,
8293 nn);
8294 }
8295 if (status)
8296 goto out;
8297 lock_sop = lockowner(lock_stp->st_stateowner);
8298
8299 lkflg = setlkflg(lock->lk_type);
8300 status = nfs4_check_openmode(lock_stp, lkflg);
8301 if (status)
8302 goto out;
8303
8304 status = nfserr_grace;
8305 if (locks_in_grace(net) && !lock->lk_reclaim)
8306 goto out;
8307 status = nfserr_no_grace;
8308 if (!locks_in_grace(net) && lock->lk_reclaim)
8309 goto out;
8310
8311 if (lock->lk_reclaim)
8312 flags |= FL_RECLAIM;
8313
8314 fp = lock_stp->st_stid.sc_file;
8315 switch (lock->lk_type) {
8316 case NFS4_READW_LT:
8317 fallthrough;
8318 case NFS4_READ_LT:
8319 spin_lock(&fp->fi_lock);
8320 nf = find_readable_file_locked(fp);
8321 if (nf)
8322 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
8323 spin_unlock(&fp->fi_lock);
8324 type = F_RDLCK;
8325 break;
8326 case NFS4_WRITEW_LT:
8327 fallthrough;
8328 case NFS4_WRITE_LT:
8329 spin_lock(&fp->fi_lock);
8330 nf = find_writeable_file_locked(fp);
8331 if (nf)
8332 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
8333 spin_unlock(&fp->fi_lock);
8334 type = F_WRLCK;
8335 break;
8336 default:
8337 status = nfserr_inval;
8338 goto out;
8339 }
8340
8341 if (!nf) {
8342 status = nfserr_openmode;
8343 goto out;
8344 }
8345
8346 if (lock->lk_type & (NFS4_READW_LT | NFS4_WRITEW_LT) &&
8347 nfsd4_has_session(cstate) &&
8348 locks_can_async_lock(nf->nf_file->f_op))
8349 flags |= FL_SLEEP;
8350
8351 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
8352 if (!nbl) {
8353 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
8354 status = nfserr_jukebox;
8355 goto out;
8356 }
8357
8358 file_lock = &nbl->nbl_lock;
8359 file_lock->c.flc_type = type;
8360 file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
8361 file_lock->c.flc_pid = current->tgid;
8362 file_lock->c.flc_file = nf->nf_file;
8363 file_lock->c.flc_flags = flags;
8364 file_lock->fl_lmops = &nfsd_posix_mng_ops;
8365 file_lock->fl_start = lock->lk_offset;
8366 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
8367 nfs4_transform_lock_offset(file_lock);
8368
8369 conflock = locks_alloc_lock();
8370 if (!conflock) {
8371 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8372 status = nfserr_jukebox;
8373 goto out;
8374 }
8375
8376 if (flags & FL_SLEEP) {
8377 nbl->nbl_time = ktime_get_boottime_seconds();
8378 spin_lock(&nn->blocked_locks_lock);
8379 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
8380 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
8381 kref_get(&nbl->nbl_kref);
8382 spin_unlock(&nn->blocked_locks_lock);
8383 }
8384
8385 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
8386 switch (err) {
8387 case 0: /* success! */
8388 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
8389 status = 0;
8390 if (lock->lk_reclaim)
8391 nn->somebody_reclaimed = true;
8392 break;
8393 case FILE_LOCK_DEFERRED:
8394 kref_put(&nbl->nbl_kref, free_nbl);
8395 nbl = NULL;
8396 fallthrough;
8397 case -EAGAIN: /* conflock holds conflicting lock */
8398 status = nfserr_denied;
8399 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
8400 nfs4_set_lock_denied(conflock, &lock->lk_denied);
8401 break;
8402 case -EDEADLK:
8403 status = nfserr_deadlock;
8404 break;
8405 default:
8406 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
8407 status = nfserrno(err);
8408 break;
8409 }
8410 out:
8411 if (nbl) {
8412 /* dequeue it if we queued it before */
8413 if (flags & FL_SLEEP) {
8414 spin_lock(&nn->blocked_locks_lock);
8415 if (!list_empty(&nbl->nbl_list) &&
8416 !list_empty(&nbl->nbl_lru)) {
8417 list_del_init(&nbl->nbl_list);
8418 list_del_init(&nbl->nbl_lru);
8419 kref_put(&nbl->nbl_kref, free_nbl);
8420 }
8421 /* nbl can use one of lists to be linked to reaplist */
8422 spin_unlock(&nn->blocked_locks_lock);
8423 }
8424 free_blocked_lock(nbl);
8425 }
8426 if (nf)
8427 nfsd_file_put(nf);
8428 if (lock_stp) {
8429 /* Bump seqid manually if the 4.0 replay owner is openowner */
8430 if (cstate->replay_owner &&
8431 cstate->replay_owner != &lock_sop->lo_owner &&
8432 seqid_mutating_err(ntohl(status)))
8433 lock_sop->lo_owner.so_seqid++;
8434
8435 /*
8436 * If this is a new, never-before-used stateid, and we are
8437 * returning an error, then just go ahead and release it.
8438 */
8439 if (status && new)
8440 release_lock_stateid(lock_stp);
8441
8442 mutex_unlock(&lock_stp->st_mutex);
8443
8444 nfs4_put_stid(&lock_stp->st_stid);
8445 }
8446 if (open_stp)
8447 nfs4_put_stid(&open_stp->st_stid);
8448 nfsd4_bump_seqid(cstate, status);
8449 if (conflock)
8450 locks_free_lock(conflock);
8451 return status;
8452 }
8453
nfsd4_lock_release(union nfsd4_op_u * u)8454 void nfsd4_lock_release(union nfsd4_op_u *u)
8455 {
8456 struct nfsd4_lock *lock = &u->lock;
8457 struct nfsd4_lock_denied *deny = &lock->lk_denied;
8458
8459 kfree(deny->ld_owner.data);
8460 }
8461
8462 /*
8463 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
8464 * so we do a temporary open here just to get an open file to pass to
8465 * vfs_test_lock.
8466 */
nfsd_test_lock(struct svc_rqst * rqstp,struct svc_fh * fhp,struct file_lock * lock)8467 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
8468 {
8469 struct nfsd_file *nf;
8470 struct inode *inode;
8471 __be32 err;
8472
8473 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
8474 if (err)
8475 return err;
8476 inode = fhp->fh_dentry->d_inode;
8477 inode_lock(inode); /* to block new leases till after test_lock: */
8478 err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
8479 if (err)
8480 goto out;
8481 lock->c.flc_file = nf->nf_file;
8482 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
8483 lock->c.flc_file = NULL;
8484 out:
8485 inode_unlock(inode);
8486 nfsd_file_put(nf);
8487 return err;
8488 }
8489
8490 /*
8491 * LOCKT operation
8492 */
8493 __be32
nfsd4_lockt(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8494 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
8495 union nfsd4_op_u *u)
8496 {
8497 struct nfsd4_lockt *lockt = &u->lockt;
8498 struct file_lock *file_lock = NULL;
8499 struct nfs4_lockowner *lo = NULL;
8500 __be32 status;
8501 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8502
8503 if (locks_in_grace(SVC_NET(rqstp)))
8504 return nfserr_grace;
8505
8506 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
8507 return nfserr_inval;
8508
8509 if (!nfsd4_has_session(cstate)) {
8510 status = set_client(&lockt->lt_clientid, cstate, nn);
8511 if (status)
8512 goto out;
8513 }
8514
8515 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
8516 goto out;
8517
8518 file_lock = locks_alloc_lock();
8519 if (!file_lock) {
8520 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8521 status = nfserr_jukebox;
8522 goto out;
8523 }
8524
8525 switch (lockt->lt_type) {
8526 case NFS4_READ_LT:
8527 case NFS4_READW_LT:
8528 file_lock->c.flc_type = F_RDLCK;
8529 break;
8530 case NFS4_WRITE_LT:
8531 case NFS4_WRITEW_LT:
8532 file_lock->c.flc_type = F_WRLCK;
8533 break;
8534 default:
8535 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
8536 status = nfserr_inval;
8537 goto out;
8538 }
8539
8540 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
8541 if (lo)
8542 file_lock->c.flc_owner = (fl_owner_t)lo;
8543 file_lock->c.flc_pid = current->tgid;
8544 file_lock->c.flc_flags = FL_POSIX;
8545
8546 file_lock->fl_start = lockt->lt_offset;
8547 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
8548
8549 nfs4_transform_lock_offset(file_lock);
8550
8551 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
8552 if (status)
8553 goto out;
8554
8555 if (file_lock->c.flc_type != F_UNLCK) {
8556 status = nfserr_denied;
8557 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
8558 }
8559 out:
8560 if (lo)
8561 nfs4_put_stateowner(&lo->lo_owner);
8562 if (file_lock)
8563 locks_free_lock(file_lock);
8564 return status;
8565 }
8566
nfsd4_lockt_release(union nfsd4_op_u * u)8567 void nfsd4_lockt_release(union nfsd4_op_u *u)
8568 {
8569 struct nfsd4_lockt *lockt = &u->lockt;
8570 struct nfsd4_lock_denied *deny = &lockt->lt_denied;
8571
8572 kfree(deny->ld_owner.data);
8573 }
8574
8575 __be32
nfsd4_locku(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8576 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
8577 union nfsd4_op_u *u)
8578 {
8579 struct nfsd4_locku *locku = &u->locku;
8580 struct nfs4_ol_stateid *stp;
8581 struct nfsd_file *nf = NULL;
8582 struct file_lock *file_lock = NULL;
8583 __be32 status;
8584 int err;
8585 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8586
8587 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
8588 (long long) locku->lu_offset,
8589 (long long) locku->lu_length);
8590
8591 if (check_lock_length(locku->lu_offset, locku->lu_length))
8592 return nfserr_inval;
8593
8594 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
8595 &locku->lu_stateid, SC_TYPE_LOCK, 0,
8596 &stp, nn);
8597 if (status)
8598 goto out;
8599 nf = find_any_file(stp->st_stid.sc_file);
8600 if (!nf) {
8601 status = nfserr_lock_range;
8602 goto put_stateid;
8603 }
8604 if (exportfs_cannot_lock(nf->nf_file->f_path.mnt->mnt_sb->s_export_op)) {
8605 status = nfserr_notsupp;
8606 goto put_file;
8607 }
8608
8609 file_lock = locks_alloc_lock();
8610 if (!file_lock) {
8611 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
8612 status = nfserr_jukebox;
8613 goto put_file;
8614 }
8615
8616 file_lock->c.flc_type = F_UNLCK;
8617 file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
8618 file_lock->c.flc_pid = current->tgid;
8619 file_lock->c.flc_file = nf->nf_file;
8620 file_lock->c.flc_flags = FL_POSIX;
8621 file_lock->fl_lmops = &nfsd_posix_mng_ops;
8622 file_lock->fl_start = locku->lu_offset;
8623
8624 file_lock->fl_end = last_byte_offset(locku->lu_offset,
8625 locku->lu_length);
8626 nfs4_transform_lock_offset(file_lock);
8627
8628 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
8629 if (err) {
8630 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
8631 goto out_nfserr;
8632 }
8633 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
8634 put_file:
8635 nfsd_file_put(nf);
8636 put_stateid:
8637 mutex_unlock(&stp->st_mutex);
8638 nfs4_put_stid(&stp->st_stid);
8639 out:
8640 nfsd4_bump_seqid(cstate, status);
8641 if (file_lock)
8642 locks_free_lock(file_lock);
8643 return status;
8644
8645 out_nfserr:
8646 status = nfserrno(err);
8647 goto put_file;
8648 }
8649
8650 /*
8651 * returns
8652 * true: locks held by lockowner
8653 * false: no locks held by lockowner
8654 */
8655 static bool
check_for_locks(struct nfs4_file * fp,struct nfs4_lockowner * lowner)8656 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
8657 {
8658 struct file_lock *fl;
8659 int status = false;
8660 struct nfsd_file *nf;
8661 struct inode *inode;
8662 struct file_lock_context *flctx;
8663
8664 spin_lock(&fp->fi_lock);
8665 nf = find_any_file_locked(fp);
8666 if (!nf) {
8667 /* Any valid lock stateid should have some sort of access */
8668 WARN_ON_ONCE(1);
8669 goto out;
8670 }
8671
8672 inode = file_inode(nf->nf_file);
8673 flctx = locks_inode_context(inode);
8674
8675 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
8676 spin_lock(&flctx->flc_lock);
8677 for_each_file_lock(fl, &flctx->flc_posix) {
8678 if (fl->c.flc_owner == (fl_owner_t)lowner) {
8679 status = true;
8680 break;
8681 }
8682 }
8683 spin_unlock(&flctx->flc_lock);
8684 }
8685 out:
8686 spin_unlock(&fp->fi_lock);
8687 return status;
8688 }
8689
8690 /**
8691 * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
8692 * @rqstp: RPC transaction
8693 * @cstate: NFSv4 COMPOUND state
8694 * @u: RELEASE_LOCKOWNER arguments
8695 *
8696 * Check if there are any locks still held and if not, free the lockowner
8697 * and any lock state that is owned.
8698 *
8699 * Return values:
8700 * %nfs_ok: lockowner released or not found
8701 * %nfserr_locks_held: lockowner still in use
8702 * %nfserr_stale_clientid: clientid no longer active
8703 * %nfserr_expired: clientid not recognized
8704 */
8705 __be32
nfsd4_release_lockowner(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8706 nfsd4_release_lockowner(struct svc_rqst *rqstp,
8707 struct nfsd4_compound_state *cstate,
8708 union nfsd4_op_u *u)
8709 {
8710 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
8711 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
8712 clientid_t *clid = &rlockowner->rl_clientid;
8713 struct nfs4_ol_stateid *stp;
8714 struct nfs4_lockowner *lo;
8715 struct nfs4_client *clp;
8716 LIST_HEAD(reaplist);
8717 __be32 status;
8718
8719 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
8720 clid->cl_boot, clid->cl_id);
8721
8722 status = set_client(clid, cstate, nn);
8723 if (status)
8724 return status;
8725 clp = cstate->clp;
8726
8727 spin_lock(&clp->cl_lock);
8728 lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
8729 if (!lo) {
8730 spin_unlock(&clp->cl_lock);
8731 return nfs_ok;
8732 }
8733
8734 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
8735 if (check_for_locks(stp->st_stid.sc_file, lo)) {
8736 spin_unlock(&clp->cl_lock);
8737 nfs4_put_stateowner(&lo->lo_owner);
8738 return nfserr_locks_held;
8739 }
8740 }
8741 unhash_lockowner_locked(lo);
8742 while (!list_empty(&lo->lo_owner.so_stateids)) {
8743 stp = list_first_entry(&lo->lo_owner.so_stateids,
8744 struct nfs4_ol_stateid,
8745 st_perstateowner);
8746 unhash_lock_stateid(stp);
8747 put_ol_stateid_locked(stp, &reaplist);
8748 }
8749 spin_unlock(&clp->cl_lock);
8750
8751 free_ol_stateid_reaplist(&reaplist);
8752 remove_blocked_locks(lo);
8753 nfs4_put_stateowner(&lo->lo_owner);
8754 return nfs_ok;
8755 }
8756
8757 static inline struct nfs4_client_reclaim *
alloc_reclaim(void)8758 alloc_reclaim(void)
8759 {
8760 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
8761 }
8762
8763 bool
nfs4_has_reclaimed_state(struct xdr_netobj name,struct nfsd_net * nn)8764 nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
8765 {
8766 struct nfs4_client_reclaim *crp;
8767
8768 crp = nfsd4_find_reclaim_client(name, nn);
8769 return (crp && crp->cr_clp);
8770 }
8771
8772 /*
8773 * failure => all reset bets are off, nfserr_no_grace...
8774 *
8775 * The caller is responsible for freeing name.data if NULL is returned (it
8776 * will be freed in nfs4_remove_reclaim_record in the normal case).
8777 */
8778 struct nfs4_client_reclaim *
nfs4_client_to_reclaim(struct xdr_netobj name,struct xdr_netobj princhash,struct nfsd_net * nn)8779 nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
8780 struct nfsd_net *nn)
8781 {
8782 unsigned int strhashval;
8783 struct nfs4_client_reclaim *crp;
8784
8785 crp = alloc_reclaim();
8786 if (crp) {
8787 strhashval = clientstr_hashval(name);
8788 INIT_LIST_HEAD(&crp->cr_strhash);
8789 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
8790 crp->cr_name.data = name.data;
8791 crp->cr_name.len = name.len;
8792 crp->cr_princhash.data = princhash.data;
8793 crp->cr_princhash.len = princhash.len;
8794 crp->cr_clp = NULL;
8795 nn->reclaim_str_hashtbl_size++;
8796 }
8797 return crp;
8798 }
8799
8800 void
nfs4_remove_reclaim_record(struct nfs4_client_reclaim * crp,struct nfsd_net * nn)8801 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
8802 {
8803 list_del(&crp->cr_strhash);
8804 kfree(crp->cr_name.data);
8805 kfree(crp->cr_princhash.data);
8806 kfree(crp);
8807 nn->reclaim_str_hashtbl_size--;
8808 }
8809
8810 void
nfs4_release_reclaim(struct nfsd_net * nn)8811 nfs4_release_reclaim(struct nfsd_net *nn)
8812 {
8813 struct nfs4_client_reclaim *crp = NULL;
8814 int i;
8815
8816 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8817 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
8818 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
8819 struct nfs4_client_reclaim, cr_strhash);
8820 nfs4_remove_reclaim_record(crp, nn);
8821 }
8822 }
8823 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
8824 }
8825
8826 /*
8827 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
8828 struct nfs4_client_reclaim *
nfsd4_find_reclaim_client(struct xdr_netobj name,struct nfsd_net * nn)8829 nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
8830 {
8831 unsigned int strhashval;
8832 struct nfs4_client_reclaim *crp = NULL;
8833
8834 strhashval = clientstr_hashval(name);
8835 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
8836 if (compare_blob(&crp->cr_name, &name) == 0) {
8837 return crp;
8838 }
8839 }
8840 return NULL;
8841 }
8842
8843 __be32
nfs4_check_open_reclaim(struct nfs4_client * clp)8844 nfs4_check_open_reclaim(struct nfs4_client *clp)
8845 {
8846 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
8847 return nfserr_no_grace;
8848
8849 if (nfsd4_client_record_check(clp))
8850 return nfserr_reclaim_bad;
8851
8852 return nfs_ok;
8853 }
8854
8855 /*
8856 * Since the lifetime of a delegation isn't limited to that of an open, a
8857 * client may quite reasonably hang on to a delegation as long as it has
8858 * the inode cached. This becomes an obvious problem the first time a
8859 * client's inode cache approaches the size of the server's total memory.
8860 *
8861 * For now we avoid this problem by imposing a hard limit on the number
8862 * of delegations, which varies according to the server's memory size.
8863 */
8864 static void
set_max_delegations(void)8865 set_max_delegations(void)
8866 {
8867 /*
8868 * Allow at most 4 delegations per megabyte of RAM. Quick
8869 * estimates suggest that in the worst case (where every delegation
8870 * is for a different inode), a delegation could take about 1.5K,
8871 * giving a worst case usage of about 6% of memory.
8872 */
8873 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
8874 }
8875
nfs4_state_create_net(struct net * net)8876 static int nfs4_state_create_net(struct net *net)
8877 {
8878 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8879 int i;
8880
8881 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
8882 sizeof(struct list_head),
8883 GFP_KERNEL);
8884 if (!nn->conf_id_hashtbl)
8885 goto err;
8886 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
8887 sizeof(struct list_head),
8888 GFP_KERNEL);
8889 if (!nn->unconf_id_hashtbl)
8890 goto err_unconf_id;
8891 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
8892 sizeof(struct list_head),
8893 GFP_KERNEL);
8894 if (!nn->sessionid_hashtbl)
8895 goto err_sessionid;
8896
8897 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8898 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
8899 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
8900 }
8901 for (i = 0; i < SESSION_HASH_SIZE; i++)
8902 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
8903 nn->conf_name_tree = RB_ROOT;
8904 nn->unconf_name_tree = RB_ROOT;
8905 nn->boot_time = ktime_get_real_seconds();
8906 nn->grace_ended = false;
8907 nn->nfsd4_manager.block_opens = true;
8908 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
8909 INIT_LIST_HEAD(&nn->client_lru);
8910 INIT_LIST_HEAD(&nn->close_lru);
8911 INIT_LIST_HEAD(&nn->del_recall_lru);
8912 spin_lock_init(&nn->client_lock);
8913 spin_lock_init(&nn->s2s_cp_lock);
8914 idr_init(&nn->s2s_cp_stateids);
8915 atomic_set(&nn->pending_async_copies, 0);
8916
8917 spin_lock_init(&nn->blocked_locks_lock);
8918 INIT_LIST_HEAD(&nn->blocked_locks_lru);
8919
8920 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
8921 INIT_WORK(&nn->nfsd_shrinker_work, nfsd4_state_shrinker_worker);
8922 get_net(net);
8923
8924 nn->nfsd_client_shrinker = shrinker_alloc(0, "nfsd-client");
8925 if (!nn->nfsd_client_shrinker)
8926 goto err_shrinker;
8927
8928 nn->nfsd_client_shrinker->scan_objects = nfsd4_state_shrinker_scan;
8929 nn->nfsd_client_shrinker->count_objects = nfsd4_state_shrinker_count;
8930 nn->nfsd_client_shrinker->private_data = nn;
8931
8932 shrinker_register(nn->nfsd_client_shrinker);
8933
8934 return 0;
8935
8936 err_shrinker:
8937 put_net(net);
8938 kfree(nn->sessionid_hashtbl);
8939 err_sessionid:
8940 kfree(nn->unconf_id_hashtbl);
8941 err_unconf_id:
8942 kfree(nn->conf_id_hashtbl);
8943 err:
8944 return -ENOMEM;
8945 }
8946
8947 static void
nfs4_state_destroy_net(struct net * net)8948 nfs4_state_destroy_net(struct net *net)
8949 {
8950 int i;
8951 struct nfs4_client *clp = NULL;
8952 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8953
8954 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8955 while (!list_empty(&nn->conf_id_hashtbl[i])) {
8956 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8957 destroy_client(clp);
8958 }
8959 }
8960
8961 WARN_ON(!list_empty(&nn->blocked_locks_lru));
8962
8963 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8964 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
8965 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8966 destroy_client(clp);
8967 }
8968 }
8969
8970 kfree(nn->sessionid_hashtbl);
8971 kfree(nn->unconf_id_hashtbl);
8972 kfree(nn->conf_id_hashtbl);
8973 put_net(net);
8974 }
8975
8976 int
nfs4_state_start_net(struct net * net)8977 nfs4_state_start_net(struct net *net)
8978 {
8979 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8980 int ret;
8981
8982 ret = nfs4_state_create_net(net);
8983 if (ret)
8984 return ret;
8985 locks_start_grace(net, &nn->nfsd4_manager);
8986 nfsd4_client_tracking_init(net);
8987 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
8988 goto skip_grace;
8989 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
8990 nn->nfsd4_grace, net->ns.inum);
8991 trace_nfsd_grace_start(nn);
8992 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
8993 return 0;
8994
8995 skip_grace:
8996 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
8997 net->ns.inum);
8998 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
8999 nfsd4_end_grace(nn);
9000 return 0;
9001 }
9002
9003 /* initialization to perform when the nfsd service is started: */
9004 int
nfs4_state_start(void)9005 nfs4_state_start(void)
9006 {
9007 int ret;
9008
9009 ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params);
9010 if (ret)
9011 return ret;
9012
9013 nfsd_slot_shrinker = shrinker_alloc(0, "nfsd-DRC-slot");
9014 if (!nfsd_slot_shrinker) {
9015 rhltable_destroy(&nfs4_file_rhltable);
9016 return -ENOMEM;
9017 }
9018 nfsd_slot_shrinker->count_objects = nfsd_slot_count;
9019 nfsd_slot_shrinker->scan_objects = nfsd_slot_scan;
9020 shrinker_register(nfsd_slot_shrinker);
9021
9022 set_max_delegations();
9023 return 0;
9024 }
9025
9026 void
nfs4_state_shutdown_net(struct net * net)9027 nfs4_state_shutdown_net(struct net *net)
9028 {
9029 struct nfs4_delegation *dp = NULL;
9030 struct list_head *pos, *next, reaplist;
9031 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9032
9033 shrinker_free(nn->nfsd_client_shrinker);
9034 cancel_work_sync(&nn->nfsd_shrinker_work);
9035 cancel_delayed_work_sync(&nn->laundromat_work);
9036 locks_end_grace(&nn->nfsd4_manager);
9037
9038 INIT_LIST_HEAD(&reaplist);
9039 spin_lock(&state_lock);
9040 list_for_each_safe(pos, next, &nn->del_recall_lru) {
9041 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
9042 unhash_delegation_locked(dp, SC_STATUS_CLOSED);
9043 list_add(&dp->dl_recall_lru, &reaplist);
9044 }
9045 spin_unlock(&state_lock);
9046 list_for_each_safe(pos, next, &reaplist) {
9047 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
9048 list_del_init(&dp->dl_recall_lru);
9049 destroy_unhashed_deleg(dp);
9050 }
9051
9052 nfsd4_client_tracking_exit(net);
9053 nfs4_state_destroy_net(net);
9054 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
9055 nfsd4_ssc_shutdown_umount(nn);
9056 #endif
9057 }
9058
9059 void
nfs4_state_shutdown(void)9060 nfs4_state_shutdown(void)
9061 {
9062 rhltable_destroy(&nfs4_file_rhltable);
9063 shrinker_free(nfsd_slot_shrinker);
9064 }
9065
9066 static void
get_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid)9067 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
9068 {
9069 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
9070 CURRENT_STATEID(stateid))
9071 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
9072 }
9073
9074 static void
put_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid)9075 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
9076 {
9077 if (cstate->minorversion) {
9078 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
9079 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
9080 }
9081 }
9082
9083 void
clear_current_stateid(struct nfsd4_compound_state * cstate)9084 clear_current_stateid(struct nfsd4_compound_state *cstate)
9085 {
9086 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
9087 }
9088
9089 /*
9090 * functions to set current state id
9091 */
9092 void
nfsd4_set_opendowngradestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9093 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
9094 union nfsd4_op_u *u)
9095 {
9096 put_stateid(cstate, &u->open_downgrade.od_stateid);
9097 }
9098
9099 void
nfsd4_set_openstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9100 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
9101 union nfsd4_op_u *u)
9102 {
9103 put_stateid(cstate, &u->open.op_stateid);
9104 }
9105
9106 void
nfsd4_set_closestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9107 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
9108 union nfsd4_op_u *u)
9109 {
9110 put_stateid(cstate, &u->close.cl_stateid);
9111 }
9112
9113 void
nfsd4_set_lockstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9114 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
9115 union nfsd4_op_u *u)
9116 {
9117 put_stateid(cstate, &u->lock.lk_resp_stateid);
9118 }
9119
9120 /*
9121 * functions to consume current state id
9122 */
9123
9124 void
nfsd4_get_opendowngradestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9125 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
9126 union nfsd4_op_u *u)
9127 {
9128 get_stateid(cstate, &u->open_downgrade.od_stateid);
9129 }
9130
9131 void
nfsd4_get_delegreturnstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9132 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
9133 union nfsd4_op_u *u)
9134 {
9135 get_stateid(cstate, &u->delegreturn.dr_stateid);
9136 }
9137
9138 void
nfsd4_get_freestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9139 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
9140 union nfsd4_op_u *u)
9141 {
9142 get_stateid(cstate, &u->free_stateid.fr_stateid);
9143 }
9144
9145 void
nfsd4_get_setattrstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9146 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
9147 union nfsd4_op_u *u)
9148 {
9149 get_stateid(cstate, &u->setattr.sa_stateid);
9150 }
9151
9152 void
nfsd4_get_closestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9153 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
9154 union nfsd4_op_u *u)
9155 {
9156 get_stateid(cstate, &u->close.cl_stateid);
9157 }
9158
9159 void
nfsd4_get_lockustateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9160 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
9161 union nfsd4_op_u *u)
9162 {
9163 get_stateid(cstate, &u->locku.lu_stateid);
9164 }
9165
9166 void
nfsd4_get_readstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9167 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
9168 union nfsd4_op_u *u)
9169 {
9170 get_stateid(cstate, &u->read.rd_stateid);
9171 }
9172
9173 void
nfsd4_get_writestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)9174 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
9175 union nfsd4_op_u *u)
9176 {
9177 get_stateid(cstate, &u->write.wr_stateid);
9178 }
9179
9180 /**
9181 * nfsd4_vet_deleg_time - vet and set the timespec for a delegated timestamp update
9182 * @req: timestamp from the client
9183 * @orig: original timestamp in the inode
9184 * @now: current time
9185 *
9186 * Given a timestamp from the client response, check it against the
9187 * current timestamp in the inode and the current time. Returns true
9188 * if the inode's timestamp needs to be updated, and false otherwise.
9189 * @req may also be changed if the timestamp needs to be clamped.
9190 */
nfsd4_vet_deleg_time(struct timespec64 * req,const struct timespec64 * orig,const struct timespec64 * now)9191 bool nfsd4_vet_deleg_time(struct timespec64 *req, const struct timespec64 *orig,
9192 const struct timespec64 *now)
9193 {
9194
9195 /*
9196 * "When the time presented is before the original time, then the
9197 * update is ignored." Also no need to update if there is no change.
9198 */
9199 if (timespec64_compare(req, orig) <= 0)
9200 return false;
9201
9202 /*
9203 * "When the time presented is in the future, the server can either
9204 * clamp the new time to the current time, or it may
9205 * return NFS4ERR_DELAY to the client, allowing it to retry."
9206 */
9207 if (timespec64_compare(req, now) > 0)
9208 *req = *now;
9209
9210 return true;
9211 }
9212
cb_getattr_update_times(struct dentry * dentry,struct nfs4_delegation * dp)9213 static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation *dp)
9214 {
9215 struct inode *inode = d_inode(dentry);
9216 struct nfs4_cb_fattr *ncf = &dp->dl_cb_fattr;
9217 struct iattr attrs = { };
9218 int ret;
9219
9220 if (deleg_attrs_deleg(dp->dl_type)) {
9221 struct timespec64 now = current_time(inode);
9222
9223 attrs.ia_atime = ncf->ncf_cb_atime;
9224 attrs.ia_mtime = ncf->ncf_cb_mtime;
9225
9226 if (nfsd4_vet_deleg_time(&attrs.ia_atime, &dp->dl_atime, &now))
9227 attrs.ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
9228
9229 if (nfsd4_vet_deleg_time(&attrs.ia_mtime, &dp->dl_mtime, &now)) {
9230 attrs.ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
9231 attrs.ia_ctime = attrs.ia_mtime;
9232 if (nfsd4_vet_deleg_time(&attrs.ia_ctime, &dp->dl_ctime, &now))
9233 attrs.ia_valid |= ATTR_CTIME | ATTR_CTIME_SET;
9234 }
9235 } else {
9236 attrs.ia_valid |= ATTR_MTIME | ATTR_CTIME;
9237 }
9238
9239 if (!attrs.ia_valid)
9240 return 0;
9241
9242 attrs.ia_valid |= ATTR_DELEG;
9243 inode_lock(inode);
9244 ret = notify_change(&nop_mnt_idmap, dentry, &attrs, NULL);
9245 inode_unlock(inode);
9246 return ret;
9247 }
9248
9249 /**
9250 * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict
9251 * @rqstp: RPC transaction context
9252 * @dentry: dentry of inode to be checked for a conflict
9253 * @pdp: returned WRITE delegation, if one was found
9254 *
9255 * This function is called when there is a conflict between a write
9256 * delegation and a change/size GETATTR from another client. The server
9257 * must either use the CB_GETATTR to get the current values of the
9258 * attributes from the client that holds the delegation or recall the
9259 * delegation before replying to the GETATTR. See RFC 8881 section
9260 * 18.7.4.
9261 *
9262 * Returns 0 if there is no conflict; otherwise an nfs_stat
9263 * code is returned. If @pdp is set to a non-NULL value, then the
9264 * caller must put the reference.
9265 */
9266 __be32
nfsd4_deleg_getattr_conflict(struct svc_rqst * rqstp,struct dentry * dentry,struct nfs4_delegation ** pdp)9267 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry,
9268 struct nfs4_delegation **pdp)
9269 {
9270 __be32 status;
9271 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
9272 struct file_lock_context *ctx;
9273 struct nfs4_delegation *dp = NULL;
9274 struct file_lease *fl;
9275 struct nfs4_cb_fattr *ncf;
9276 struct inode *inode = d_inode(dentry);
9277
9278 ctx = locks_inode_context(inode);
9279 if (!ctx)
9280 return nfs_ok;
9281
9282 #define NON_NFSD_LEASE ((void *)1)
9283
9284 spin_lock(&ctx->flc_lock);
9285 for_each_file_lock(fl, &ctx->flc_lease) {
9286 if (fl->c.flc_flags == FL_LAYOUT)
9287 continue;
9288 if (fl->c.flc_type == F_WRLCK) {
9289 if (fl->fl_lmops == &nfsd_lease_mng_ops)
9290 dp = fl->c.flc_owner;
9291 else
9292 dp = NON_NFSD_LEASE;
9293 }
9294 break;
9295 }
9296 if (dp == NULL || dp == NON_NFSD_LEASE ||
9297 dp->dl_recall.cb_clp == *(rqstp->rq_lease_breaker)) {
9298 spin_unlock(&ctx->flc_lock);
9299 if (dp == NON_NFSD_LEASE) {
9300 status = nfserrno(nfsd_open_break_lease(inode,
9301 NFSD_MAY_READ));
9302 if (status != nfserr_jukebox ||
9303 !nfsd_wait_for_delegreturn(rqstp, inode))
9304 return status;
9305 }
9306 return 0;
9307 }
9308
9309 nfsd_stats_wdeleg_getattr_inc(nn);
9310 refcount_inc(&dp->dl_stid.sc_count);
9311 ncf = &dp->dl_cb_fattr;
9312 nfs4_cb_getattr(&dp->dl_cb_fattr);
9313 spin_unlock(&ctx->flc_lock);
9314
9315 wait_on_bit_timeout(&ncf->ncf_getattr.cb_flags, NFSD4_CALLBACK_RUNNING,
9316 TASK_UNINTERRUPTIBLE, NFSD_CB_GETATTR_TIMEOUT);
9317 if (ncf->ncf_cb_status) {
9318 /* Recall delegation only if client didn't respond */
9319 status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
9320 if (status != nfserr_jukebox ||
9321 !nfsd_wait_for_delegreturn(rqstp, inode))
9322 goto out_status;
9323 }
9324 if (!ncf->ncf_file_modified &&
9325 (ncf->ncf_initial_cinfo != ncf->ncf_cb_change ||
9326 ncf->ncf_cur_fsize != ncf->ncf_cb_fsize))
9327 ncf->ncf_file_modified = true;
9328 if (ncf->ncf_file_modified) {
9329 int err;
9330
9331 /*
9332 * Per section 10.4.3 of RFC 8881, the server would
9333 * not update the file's metadata with the client's
9334 * modified size
9335 */
9336 err = cb_getattr_update_times(dentry, dp);
9337 if (err) {
9338 status = nfserrno(err);
9339 goto out_status;
9340 }
9341 ncf->ncf_cur_fsize = ncf->ncf_cb_fsize;
9342 *pdp = dp;
9343 return nfs_ok;
9344 }
9345 status = nfs_ok;
9346 out_status:
9347 nfs4_put_stid(&dp->dl_stid);
9348 return status;
9349 }
9350