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