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