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