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