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