blob: 88c0a5cd7b75515075c738af9a700e8ae926078e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* 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
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.h>
J. Bruce Fields169319f2019-06-19 12:39:46 -040045#include <linux/string_helpers.h>
NeilBrown472d1552021-03-20 09:38:04 +110046#include <linux/fsnotify.h>
Dai Ngof4e44b32021-05-21 15:09:37 -040047#include <linux/nfs_ssc.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020048#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040049#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050050#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040051#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040053#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020054#include "pnfs.h"
Jeff Laytonfd4f83f2019-08-18 14:18:52 -040055#include "filecache.h"
Chuck Leverdd5e3fb2020-04-05 11:42:29 -040056#include "trace.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define NFSDDBG_FACILITY NFSDDBG_PROC
59
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050060#define all_ones {{~0,~0},~0}
61static const stateid_t one_stateid = {
62 .si_generation = ~0,
63 .si_opaque = all_ones,
64};
65static const stateid_t zero_stateid = {
66 /* all fields zero */
67};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010068static const stateid_t currentstateid = {
69 .si_generation = 1,
70};
Trond Myklebustfb500a72017-11-03 08:00:12 -040071static const stateid_t close_stateid = {
72 .si_generation = 0xffffffffU,
73};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050074
Andy Adamsonec6b5d72009-04-03 08:28:28 +030075static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070076
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050077#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
78#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010079#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elbleae254dac2017-11-09 13:41:10 -050080#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040083static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040084static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Scott Mayhew362063a2019-03-26 18:06:28 -040085void nfsd4_end_grace(struct nfsd_net *nn);
Olga Kornievskaia624322f2019-10-04 16:34:26 -040086static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
J. Bruce Fields8b671b82009-02-22 14:51:34 -080088/* Locking: */
89
J. Bruce Fields8b671b82009-02-22 14:51:34 -080090/*
91 * Currently used for the del_recall_lru and file hash table. In an
92 * effort to decrease the scope of the client_mutex, this spinlock may
93 * eventually cover more:
94 */
Benny Halevycdc975052014-05-30 09:09:30 -040095static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080096
Andrew Elble4f34bd02017-11-08 17:29:51 -050097enum nfsd4_st_mutex_lock_subclass {
98 OPEN_STATEID_MUTEX = 0,
99 LOCK_STATEID_MUTEX = 1,
100};
101
Jeff Laytonb401be222014-07-29 21:34:33 -0400102/*
103 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
104 * the refcount on the open stateid to drop.
105 */
106static DECLARE_WAIT_QUEUE_HEAD(close_wq);
107
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400108/*
109 * A waitqueue where a writer to clients/#/ctl destroying a client can
110 * wait for cl_rpc_users to drop to 0 and then for the client to be
111 * unhashed.
112 */
113static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
114
Jeff Layton9258a2d2018-03-16 09:47:22 -0400115static struct kmem_cache *client_slab;
Christoph Hellwigabf11352014-05-21 07:43:03 -0700116static struct kmem_cache *openowner_slab;
117static struct kmem_cache *lockowner_slab;
118static struct kmem_cache *file_slab;
119static struct kmem_cache *stateid_slab;
120static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200121static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700122
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400123static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800124
Julia Lawallc4cb8972015-11-21 22:57:39 +0100125static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400126static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200127
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400128static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800129{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400130 return ses->se_flags & NFS4_SESSION_DEAD;
131}
132
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400133static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
134{
135 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400136 return nfserr_jukebox;
137 ses->se_flags |= NFS4_SESSION_DEAD;
138 return nfs_ok;
139}
140
J. Bruce Fields221a6872013-04-01 22:23:49 -0400141static bool is_client_expired(struct nfs4_client *clp)
142{
143 return clp->cl_time == 0;
144}
145
J. Bruce Fields221a6872013-04-01 22:23:49 -0400146static __be32 get_client_locked(struct nfs4_client *clp)
147{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400148 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
149
150 lockdep_assert_held(&nn->client_lock);
151
J. Bruce Fields221a6872013-04-01 22:23:49 -0400152 if (is_client_expired(clp))
153 return nfserr_expired;
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400154 atomic_inc(&clp->cl_rpc_users);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400155 return nfs_ok;
156}
157
158/* must be called under the client_lock */
159static inline void
160renew_client_locked(struct nfs4_client *clp)
161{
162 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
163
164 if (is_client_expired(clp)) {
165 WARN_ON(1);
166 printk("%s: client (clientid %08x/%08x) already expired\n",
167 __func__,
168 clp->cl_clientid.cl_boot,
169 clp->cl_clientid.cl_id);
170 return;
171 }
172
J. Bruce Fields221a6872013-04-01 22:23:49 -0400173 list_move_tail(&clp->cl_lru, &nn->client_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100174 clp->cl_time = ktime_get_boottime_seconds();
J. Bruce Fields221a6872013-04-01 22:23:49 -0400175}
176
Fengguang Wuba138432013-04-16 22:14:15 -0400177static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400178{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400179 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
180
181 lockdep_assert_held(&nn->client_lock);
182
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400183 if (!atomic_dec_and_test(&clp->cl_rpc_users))
J. Bruce Fields221a6872013-04-01 22:23:49 -0400184 return;
185 if (!is_client_expired(clp))
186 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400187 else
188 wake_up_all(&expiry_wq);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400189}
190
Jeff Layton4b24ca72014-06-30 11:48:44 -0400191static void put_client_renew(struct nfs4_client *clp)
192{
193 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
194
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400195 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
Jeff Laytond6c249b2014-07-08 14:02:50 -0400196 return;
197 if (!is_client_expired(clp))
198 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400199 else
200 wake_up_all(&expiry_wq);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400201 spin_unlock(&nn->client_lock);
202}
203
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400204static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
205{
206 __be32 status;
207
208 if (is_session_dead(ses))
209 return nfserr_badsession;
210 status = get_client_locked(ses->se_client);
211 if (status)
212 return status;
213 atomic_inc(&ses->se_ref);
214 return nfs_ok;
215}
216
217static void nfsd4_put_session_locked(struct nfsd4_session *ses)
218{
219 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400220 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
221
222 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400223
224 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
225 free_session(ses);
226 put_client_renew_locked(clp);
227}
228
229static void nfsd4_put_session(struct nfsd4_session *ses)
230{
231 struct nfs4_client *clp = ses->se_client;
232 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
233
234 spin_lock(&nn->client_lock);
235 nfsd4_put_session_locked(ses);
236 spin_unlock(&nn->client_lock);
237}
238
Jeff Layton76d348f2016-09-16 16:28:24 -0400239static struct nfsd4_blocked_lock *
240find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
241 struct nfsd_net *nn)
242{
243 struct nfsd4_blocked_lock *cur, *found = NULL;
244
Jeff Layton0cc11a62016-10-20 09:34:31 -0400245 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400246 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
247 if (fh_match(fh, &cur->nbl_fh)) {
248 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400249 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400250 found = cur;
251 break;
252 }
253 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400254 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400255 if (found)
NeilBrowncb03f942018-11-30 10:04:08 +1100256 locks_delete_block(&found->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400257 return found;
258}
259
260static struct nfsd4_blocked_lock *
261find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
262 struct nfsd_net *nn)
263{
264 struct nfsd4_blocked_lock *nbl;
265
266 nbl = find_blocked_lock(lo, fh, nn);
267 if (!nbl) {
268 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
269 if (nbl) {
Vasily Averine1e83992020-03-27 07:50:40 +0300270 INIT_LIST_HEAD(&nbl->nbl_list);
271 INIT_LIST_HEAD(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400272 fh_copy_shallow(&nbl->nbl_fh, fh);
273 locks_init_lock(&nbl->nbl_lock);
274 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
275 &nfsd4_cb_notify_lock_ops,
276 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
277 }
278 }
279 return nbl;
280}
281
282static void
283free_blocked_lock(struct nfsd4_blocked_lock *nbl)
284{
Jeff Layton6aaafc42019-04-22 12:34:23 -0400285 locks_delete_block(&nbl->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400286 locks_release_private(&nbl->nbl_lock);
287 kfree(nbl);
288}
289
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400290static void
291remove_blocked_locks(struct nfs4_lockowner *lo)
292{
293 struct nfs4_client *clp = lo->lo_owner.so_client;
294 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
295 struct nfsd4_blocked_lock *nbl;
296 LIST_HEAD(reaplist);
297
298 /* Dequeue all blocked locks */
299 spin_lock(&nn->blocked_locks_lock);
300 while (!list_empty(&lo->lo_blocked)) {
301 nbl = list_first_entry(&lo->lo_blocked,
302 struct nfsd4_blocked_lock,
303 nbl_list);
304 list_del_init(&nbl->nbl_list);
305 list_move(&nbl->nbl_lru, &reaplist);
306 }
307 spin_unlock(&nn->blocked_locks_lock);
308
309 /* Now free them */
310 while (!list_empty(&reaplist)) {
311 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
312 nbl_lru);
313 list_del_init(&nbl->nbl_lru);
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400314 free_blocked_lock(nbl);
315 }
316}
317
Jeff Laytonf4564582019-04-22 12:34:24 -0400318static void
319nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
320{
321 struct nfsd4_blocked_lock *nbl = container_of(cb,
322 struct nfsd4_blocked_lock, nbl_cb);
323 locks_delete_block(&nbl->nbl_lock);
324}
325
Jeff Layton76d348f2016-09-16 16:28:24 -0400326static int
327nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
328{
329 /*
330 * Since this is just an optimization, we don't try very hard if it
331 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
332 * just quit trying on anything else.
333 */
334 switch (task->tk_status) {
335 case -NFS4ERR_DELAY:
336 rpc_delay(task, 1 * HZ);
337 return 0;
338 default:
339 return 1;
340 }
341}
342
343static void
344nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
345{
346 struct nfsd4_blocked_lock *nbl = container_of(cb,
347 struct nfsd4_blocked_lock, nbl_cb);
348
349 free_blocked_lock(nbl);
350}
351
352static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
Jeff Laytonf4564582019-04-22 12:34:24 -0400353 .prepare = nfsd4_cb_notify_lock_prepare,
Jeff Layton76d348f2016-09-16 16:28:24 -0400354 .done = nfsd4_cb_notify_lock_done,
355 .release = nfsd4_cb_notify_lock_release,
356};
357
J. Bruce Fieldsebd9d2c2021-04-16 14:00:17 -0400358/*
359 * We store the NONE, READ, WRITE, and BOTH bits separately in the
360 * st_{access,deny}_bmap field of the stateid, in order to track not
361 * only what share bits are currently in force, but also what
362 * combinations of share bits previous opens have used. This allows us
363 * to enforce the recommendation of rfc 3530 14.2.19 that the server
364 * return an error if the client attempt to downgrade to a combination
365 * of share bits not explicable by closing some of its previous opens.
366 *
367 * XXX: This enforcement is actually incomplete, since we don't keep
368 * track of access/deny bit combinations; so, e.g., we allow:
369 *
370 * OPEN allow read, deny write
371 * OPEN allow both, deny none
372 * DOWNGRADE allow read, deny none
373 *
374 * which we should reject.
375 */
376static unsigned int
377bmap_to_share_mode(unsigned long bmap)
378{
379 int i;
380 unsigned int access = 0;
381
382 for (i = 1; i < 4; i++) {
383 if (test_bit(i, &bmap))
384 access |= i;
385 }
386 return access;
387}
388
389/* set share access for a given stateid */
390static inline void
391set_access(u32 access, struct nfs4_ol_stateid *stp)
392{
393 unsigned char mask = 1 << access;
394
395 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
396 stp->st_access_bmap |= mask;
397}
398
399/* clear share access for a given stateid */
400static inline void
401clear_access(u32 access, struct nfs4_ol_stateid *stp)
402{
403 unsigned char mask = 1 << access;
404
405 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
406 stp->st_access_bmap &= ~mask;
407}
408
409/* test whether a given stateid has access */
410static inline bool
411test_access(u32 access, struct nfs4_ol_stateid *stp)
412{
413 unsigned char mask = 1 << access;
414
415 return (bool)(stp->st_access_bmap & mask);
416}
417
418/* set share deny for a given stateid */
419static inline void
420set_deny(u32 deny, struct nfs4_ol_stateid *stp)
421{
422 unsigned char mask = 1 << deny;
423
424 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
425 stp->st_deny_bmap |= mask;
426}
427
428/* clear share deny for a given stateid */
429static inline void
430clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
431{
432 unsigned char mask = 1 << deny;
433
434 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
435 stp->st_deny_bmap &= ~mask;
436}
437
438/* test whether a given stateid is denying specific access */
439static inline bool
440test_deny(u32 deny, struct nfs4_ol_stateid *stp)
441{
442 unsigned char mask = 1 << deny;
443
444 return (bool)(stp->st_deny_bmap & mask);
445}
446
447static int nfs4_access_to_omode(u32 access)
448{
449 switch (access & NFS4_SHARE_ACCESS_BOTH) {
450 case NFS4_SHARE_ACCESS_READ:
451 return O_RDONLY;
452 case NFS4_SHARE_ACCESS_WRITE:
453 return O_WRONLY;
454 case NFS4_SHARE_ACCESS_BOTH:
455 return O_RDWR;
456 }
457 WARN_ON_ONCE(1);
458 return O_RDONLY;
459}
460
461static inline int
462access_permit_read(struct nfs4_ol_stateid *stp)
463{
464 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
465 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
466 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
467}
468
469static inline int
470access_permit_write(struct nfs4_ol_stateid *stp)
471{
472 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
473 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
474}
475
Kinglong Meeb5971af2014-08-22 10:18:43 -0400476static inline struct nfs4_stateowner *
477nfs4_get_stateowner(struct nfs4_stateowner *sop)
478{
479 atomic_inc(&sop->so_count);
480 return sop;
481}
482
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400483static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400484same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400485{
486 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400487 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400488}
489
490static struct nfs4_openowner *
491find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400492 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400493{
494 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400495
Trond Myklebustd4f04892014-07-29 21:34:36 -0400496 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400497
Trond Myklebustd4f04892014-07-29 21:34:36 -0400498 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
499 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400500 if (!so->so_is_open_owner)
501 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400502 if (same_owner_str(so, &open->op_owner))
503 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400504 }
505 return NULL;
506}
507
508static struct nfs4_openowner *
509find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400510 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400511{
512 struct nfs4_openowner *oo;
513
Trond Myklebustd4f04892014-07-29 21:34:36 -0400514 spin_lock(&clp->cl_lock);
515 oo = find_openstateowner_str_locked(hashval, open, clp);
516 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400517 return oo;
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520static inline u32
521opaque_hashval(const void *ptr, int nbytes)
522{
523 unsigned char *cptr = (unsigned char *) ptr;
524
525 u32 x = 0;
526 while (nbytes--) {
527 x *= 37;
528 x += *cptr++;
529 }
530 return x;
531}
532
Jeff Layton5b095e92014-10-23 08:01:02 -0400533static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400534{
Jeff Layton5b095e92014-10-23 08:01:02 -0400535 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
536
537 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400538}
539
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200540void
NeilBrown13cd2182005-06-23 22:03:10 -0700541put_nfs4_file(struct nfs4_file *fi)
542{
Jeff Layton02e12152014-07-16 10:31:57 -0400543 might_lock(&state_lock);
544
Elena Reshetova818a34e2017-10-20 12:53:30 +0300545 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400546 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400547 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200548 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400549 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
550 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800551 }
NeilBrown13cd2182005-06-23 22:03:10 -0700552}
553
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400554static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400555__nfs4_get_fd(struct nfs4_file *f, int oflag)
556{
557 if (f->fi_fds[oflag])
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400558 return nfsd_file_get(f->fi_fds[oflag]);
Trond Myklebustde186432014-07-10 14:07:26 -0400559 return NULL;
560}
561
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400562static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400563find_writeable_file_locked(struct nfs4_file *f)
564{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400565 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400566
567 lockdep_assert_held(&f->fi_lock);
568
569 ret = __nfs4_get_fd(f, O_WRONLY);
570 if (!ret)
571 ret = __nfs4_get_fd(f, O_RDWR);
572 return ret;
573}
574
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400575static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400576find_writeable_file(struct nfs4_file *f)
577{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400578 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400579
580 spin_lock(&f->fi_lock);
581 ret = find_writeable_file_locked(f);
582 spin_unlock(&f->fi_lock);
583
584 return ret;
585}
586
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400587static struct nfsd_file *
588find_readable_file_locked(struct nfs4_file *f)
Trond Myklebustde186432014-07-10 14:07:26 -0400589{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400590 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400591
592 lockdep_assert_held(&f->fi_lock);
593
594 ret = __nfs4_get_fd(f, O_RDONLY);
595 if (!ret)
596 ret = __nfs4_get_fd(f, O_RDWR);
597 return ret;
598}
599
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400600static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400601find_readable_file(struct nfs4_file *f)
602{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400603 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400604
605 spin_lock(&f->fi_lock);
606 ret = find_readable_file_locked(f);
607 spin_unlock(&f->fi_lock);
608
609 return ret;
610}
611
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400612struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400613find_any_file(struct nfs4_file *f)
614{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400615 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400616
Trond Myklebusta451b122020-03-01 18:21:38 -0500617 if (!f)
618 return NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400619 spin_lock(&f->fi_lock);
620 ret = __nfs4_get_fd(f, O_RDWR);
621 if (!ret) {
622 ret = __nfs4_get_fd(f, O_WRONLY);
623 if (!ret)
624 ret = __nfs4_get_fd(f, O_RDONLY);
625 }
626 spin_unlock(&f->fi_lock);
627 return ret;
628}
629
J. Bruce Fields9affa432020-07-15 13:31:36 -0400630static struct nfsd_file *find_deleg_file(struct nfs4_file *f)
631{
632 struct nfsd_file *ret = NULL;
633
634 spin_lock(&f->fi_lock);
635 if (f->fi_deleg_file)
636 ret = nfsd_file_get(f->fi_deleg_file);
637 spin_unlock(&f->fi_lock);
638 return ret;
639}
640
Trond Myklebust02a35082014-07-25 07:34:22 -0400641static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800642unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700643
644/*
645 * Open owner state (share locks)
646 */
647
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500648/* hash tables for lock and open owners */
649#define OWNER_HASH_BITS 8
650#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
651#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700652
Trond Myklebustd4f04892014-07-29 21:34:36 -0400653static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400654{
655 unsigned int ret;
656
657 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500658 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400659}
NeilBrownef0f3392006-04-10 22:55:41 -0700660
NeilBrownef0f3392006-04-10 22:55:41 -0700661/* hash table for nfs4_file */
662#define FILE_HASH_BITS 8
663#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800664
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400665static unsigned int file_hashval(struct svc_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400666{
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400667 struct inode *inode = d_inode(fh->fh_dentry);
Trond Myklebustca943212014-07-23 16:17:39 -0400668
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400669 /* XXX: why not (here & in file cache) use inode? */
670 return (unsigned int)hash_long(inode->i_ino, FILE_HASH_BITS);
Trond Myklebustca943212014-07-23 16:17:39 -0400671}
672
Jeff Layton89876f82013-04-02 09:01:59 -0400673static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700674
Jeff Layton12659652014-07-10 14:07:28 -0400675static void
676__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400677{
Jeff Layton7214e862014-07-10 14:07:33 -0400678 lockdep_assert_held(&fp->fi_lock);
679
Jeff Layton12659652014-07-10 14:07:28 -0400680 if (access & NFS4_SHARE_ACCESS_WRITE)
681 atomic_inc(&fp->fi_access[O_WRONLY]);
682 if (access & NFS4_SHARE_ACCESS_READ)
683 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400684}
685
Jeff Layton12659652014-07-10 14:07:28 -0400686static __be32
687nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400688{
Jeff Layton7214e862014-07-10 14:07:33 -0400689 lockdep_assert_held(&fp->fi_lock);
690
Jeff Layton12659652014-07-10 14:07:28 -0400691 /* Does this access mode make sense? */
692 if (access & ~NFS4_SHARE_ACCESS_BOTH)
693 return nfserr_inval;
694
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400695 /* Does it conflict with a deny mode already set? */
696 if ((access & fp->fi_share_deny) != 0)
697 return nfserr_share_denied;
698
Jeff Layton12659652014-07-10 14:07:28 -0400699 __nfs4_file_get_access(fp, access);
700 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400701}
702
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400703static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
704{
705 /* Common case is that there is no deny mode. */
706 if (deny) {
707 /* Does this deny mode make sense? */
708 if (deny & ~NFS4_SHARE_DENY_BOTH)
709 return nfserr_inval;
710
711 if ((deny & NFS4_SHARE_DENY_READ) &&
712 atomic_read(&fp->fi_access[O_RDONLY]))
713 return nfserr_share_denied;
714
715 if ((deny & NFS4_SHARE_DENY_WRITE) &&
716 atomic_read(&fp->fi_access[O_WRONLY]))
717 return nfserr_share_denied;
718 }
719 return nfs_ok;
720}
721
J. Bruce Fields998db522010-08-07 09:21:41 -0400722static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400723{
Trond Myklebustde186432014-07-10 14:07:26 -0400724 might_lock(&fp->fi_lock);
725
726 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400727 struct nfsd_file *f1 = NULL;
728 struct nfsd_file *f2 = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400729
Jeff Layton6d338b52014-07-10 14:07:29 -0400730 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400731 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400732 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400733 spin_unlock(&fp->fi_lock);
734 if (f1)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400735 nfsd_file_put(f1);
Trond Myklebustde186432014-07-10 14:07:26 -0400736 if (f2)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400737 nfsd_file_put(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400738 }
739}
740
Jeff Layton12659652014-07-10 14:07:28 -0400741static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400742{
Jeff Layton12659652014-07-10 14:07:28 -0400743 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
744
745 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400746 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400747 if (access & NFS4_SHARE_ACCESS_READ)
748 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400749}
750
Sachin Bhamare8287f002015-04-27 14:50:14 +0200751/*
752 * Allocate a new open/delegation state counter. This is needed for
753 * pNFS for proper return on close semantics.
754 *
755 * Note that we only allocate it for pNFS-enabled exports, otherwise
756 * all pointers to struct nfs4_clnt_odstate are always NULL.
757 */
758static struct nfs4_clnt_odstate *
759alloc_clnt_odstate(struct nfs4_client *clp)
760{
761 struct nfs4_clnt_odstate *co;
762
763 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
764 if (co) {
765 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300766 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200767 }
768 return co;
769}
770
771static void
772hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
773{
774 struct nfs4_file *fp = co->co_file;
775
776 lockdep_assert_held(&fp->fi_lock);
777 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
778}
779
780static inline void
781get_clnt_odstate(struct nfs4_clnt_odstate *co)
782{
783 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300784 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200785}
786
787static void
788put_clnt_odstate(struct nfs4_clnt_odstate *co)
789{
790 struct nfs4_file *fp;
791
792 if (!co)
793 return;
794
795 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300796 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200797 list_del(&co->co_perfile);
798 spin_unlock(&fp->fi_lock);
799
800 nfsd4_return_all_file_layouts(co->co_client, fp);
801 kmem_cache_free(odstate_slab, co);
802 }
803}
804
805static struct nfs4_clnt_odstate *
806find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
807{
808 struct nfs4_clnt_odstate *co;
809 struct nfs4_client *cl;
810
811 if (!new)
812 return NULL;
813
814 cl = new->co_client;
815
816 spin_lock(&fp->fi_lock);
817 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
818 if (co->co_client == cl) {
819 get_clnt_odstate(co);
820 goto out;
821 }
822 }
823 co = new;
824 co->co_file = fp;
825 hash_clnt_odstate_locked(new);
826out:
827 spin_unlock(&fp->fi_lock);
828 return co;
829}
830
Kinglong Meed19fb702017-01-18 19:04:42 +0800831struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
832 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400833{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500834 struct nfs4_stid *stid;
835 int new_id;
836
Trond Myklebustf8338832014-07-25 07:34:19 -0400837 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500838 if (!stid)
839 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400840
Jeff Layton4770d722014-07-29 21:34:10 -0400841 idr_preload(GFP_KERNEL);
842 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400843 /* Reserving 0 for start of file in nfsdfs "states" file: */
844 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400845 spin_unlock(&cl->cl_lock);
846 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700847 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500848 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800849
850 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500851 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500852 stid->sc_stateid.si_opaque.so_id = new_id;
853 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
854 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300855 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400856 spin_lock_init(&stid->sc_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400857 INIT_LIST_HEAD(&stid->sc_cp_list);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500858
J. Bruce Fields996e0932011-10-17 11:14:48 -0400859 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500860 * It shouldn't be a problem to reuse an opaque stateid value.
861 * I don't think it is for 4.1. But with 4.0 I worry that, for
862 * example, a stray write retransmission could be accepted by
863 * the server when it should have been rejected. Therefore,
864 * adopt a trick from the sctp code to attempt to maximize the
865 * amount of time until an id is reused, by ensuring they always
866 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400867 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500868 return stid;
869out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800870 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500871 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400872}
873
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400874/*
875 * Create a unique stateid_t to represent each COPY.
876 */
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400877static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
878 unsigned char sc_type)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400879{
880 int new_id;
881
Arnd Bergmann9cc76802019-10-31 15:53:13 +0100882 stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400883 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
884 stid->sc_type = sc_type;
885
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400886 idr_preload(GFP_KERNEL);
887 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400888 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
889 stid->stid.si_opaque.so_id = new_id;
Dai Ngoca9364d2020-11-30 16:24:49 -0500890 stid->stid.si_generation = 1;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400891 spin_unlock(&nn->s2s_cp_lock);
892 idr_preload_end();
893 if (new_id < 0)
894 return 0;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400895 return 1;
896}
897
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400898int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
899{
900 return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
901}
902
903struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
904 struct nfs4_stid *p_stid)
905{
906 struct nfs4_cpntf_state *cps;
907
908 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
909 if (!cps)
910 return NULL;
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100911 cps->cpntf_time = ktime_get_boottime_seconds();
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400912 refcount_set(&cps->cp_stateid.sc_count, 1);
913 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
914 goto out_free;
915 spin_lock(&nn->s2s_cp_lock);
916 list_add(&cps->cp_list, &p_stid->sc_cp_list);
917 spin_unlock(&nn->s2s_cp_lock);
918 return cps;
919out_free:
920 kfree(cps);
921 return NULL;
922}
923
924void nfs4_free_copy_state(struct nfsd4_copy *copy)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400925{
926 struct nfsd_net *nn;
927
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400928 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400929 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
930 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400931 idr_remove(&nn->s2s_cp_stateids,
932 copy->cp_stateid.stid.si_opaque.so_id);
933 spin_unlock(&nn->s2s_cp_lock);
934}
935
936static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
937{
938 struct nfs4_cpntf_state *cps;
939 struct nfsd_net *nn;
940
941 nn = net_generic(net, nfsd_net_id);
942 spin_lock(&nn->s2s_cp_lock);
943 while (!list_empty(&stid->sc_cp_list)) {
944 cps = list_first_entry(&stid->sc_cp_list,
945 struct nfs4_cpntf_state, cp_list);
946 _free_cpntf_state_locked(nn, cps);
947 }
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400948 spin_unlock(&nn->s2s_cp_lock);
949}
950
Jeff Laytonb49e0842014-07-29 21:34:11 -0400951static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400952{
Trond Myklebust60116952014-07-29 21:34:06 -0400953 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400954
Kinglong Meed19fb702017-01-18 19:04:42 +0800955 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400956 if (!stid)
957 return NULL;
958
Kinglong Meed19fb702017-01-18 19:04:42 +0800959 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400960}
961
962static void nfs4_free_deleg(struct nfs4_stid *stid)
963{
Trond Myklebust60116952014-07-29 21:34:06 -0400964 kmem_cache_free(deleg_slab, stid);
965 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400966}
967
NeilBrown6282cd52014-06-04 17:39:26 +1000968/*
969 * When we recall a delegation, we should be careful not to hand it
970 * out again straight away.
971 * To ensure this we keep a pair of bloom filters ('new' and 'old')
972 * in which the filehandles of recalled delegations are "stored".
973 * If a filehandle appear in either filter, a delegation is blocked.
974 * When a delegation is recalled, the filehandle is stored in the "new"
975 * filter.
976 * Every 30 seconds we swap the filters and clear the "new" one,
977 * unless both are empty of course.
978 *
979 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
980 * low 3 bytes as hash-table indices.
981 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400982 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000983 * is used to manage concurrent access. Testing does not need the lock
984 * except when swapping the two filters.
985 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400986static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000987static struct bloom_pair {
988 int entries, old_entries;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200989 time64_t swap_time;
NeilBrown6282cd52014-06-04 17:39:26 +1000990 int new; /* index into 'set' */
991 DECLARE_BITMAP(set[2], 256);
992} blocked_delegations;
993
994static int delegation_blocked(struct knfsd_fh *fh)
995{
996 u32 hash;
997 struct bloom_pair *bd = &blocked_delegations;
998
999 if (bd->entries == 0)
1000 return 0;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001001 if (ktime_get_seconds() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -04001002 spin_lock(&blocked_delegations_lock);
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001003 if (ktime_get_seconds() - bd->swap_time > 30) {
NeilBrown6282cd52014-06-04 17:39:26 +10001004 bd->entries -= bd->old_entries;
1005 bd->old_entries = bd->entries;
1006 memset(bd->set[bd->new], 0,
1007 sizeof(bd->set[0]));
1008 bd->new = 1-bd->new;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001009 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +10001010 }
Jeff Laytonf54fe962014-07-25 07:34:26 -04001011 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001012 }
NeilBrownd8b26072021-09-02 11:16:32 +10001013 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +10001014 if (test_bit(hash&255, bd->set[0]) &&
1015 test_bit((hash>>8)&255, bd->set[0]) &&
1016 test_bit((hash>>16)&255, bd->set[0]))
1017 return 1;
1018
1019 if (test_bit(hash&255, bd->set[1]) &&
1020 test_bit((hash>>8)&255, bd->set[1]) &&
1021 test_bit((hash>>16)&255, bd->set[1]))
1022 return 1;
1023
1024 return 0;
1025}
1026
1027static void block_delegations(struct knfsd_fh *fh)
1028{
1029 u32 hash;
1030 struct bloom_pair *bd = &blocked_delegations;
1031
NeilBrownd8b26072021-09-02 11:16:32 +10001032 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +10001033
Jeff Laytonf54fe962014-07-25 07:34:26 -04001034 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001035 __set_bit(hash&255, bd->set[bd->new]);
1036 __set_bit((hash>>8)&255, bd->set[bd->new]);
1037 __set_bit((hash>>16)&255, bd->set[bd->new]);
1038 if (bd->entries == 0)
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001039 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +10001040 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -04001041 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001042}
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -05001045alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
1046 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02001047 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -04001050 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -04001053 n = atomic_long_inc_return(&num_delegations);
1054 if (n < 0 || n > max_delegations)
1055 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +10001056 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -04001057 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +08001058 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -07001059 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -04001060 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -04001061
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04001062 /*
1063 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001064 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1065 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04001066 */
1067 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -07001068 INIT_LIST_HEAD(&dp->dl_perfile);
1069 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02001071 dp->dl_clnt_odstate = odstate;
1072 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -04001073 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02001074 dp->dl_retries = 1;
1075 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001076 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -05001077 get_nfs4_file(fp);
1078 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -04001080out_dec:
1081 atomic_long_dec(&num_delegations);
1082 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
1085void
Trond Myklebust60116952014-07-29 21:34:06 -04001086nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Trond Myklebust11b91642014-07-29 21:34:08 -04001088 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -04001089 struct nfs4_client *clp = s->sc_client;
1090
Jeff Layton4770d722014-07-29 21:34:10 -04001091 might_lock(&clp->cl_lock);
1092
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001093 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -04001094 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -04001095 return;
Jeff Laytonb401be222014-07-29 21:34:33 -04001096 }
Trond Myklebust60116952014-07-29 21:34:06 -04001097 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001098 nfs4_free_cpntf_statelist(clp->net, s);
Jeff Layton4770d722014-07-29 21:34:10 -04001099 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001100 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -04001101 if (fp)
1102 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Jeff Layton9767feb2015-10-01 09:05:50 -04001105void
1106nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1107{
1108 stateid_t *src = &stid->sc_stateid;
1109
1110 spin_lock(&stid->sc_lock);
1111 if (unlikely(++src->si_generation == 0))
1112 src->si_generation = 1;
1113 memcpy(dst, src, sizeof(*dst));
1114 spin_unlock(&stid->sc_lock);
1115}
1116
J. Bruce Fields353601e2018-02-16 14:29:42 -05001117static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001119 struct nfsd_file *nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05001120
1121 spin_lock(&fp->fi_lock);
1122 if (--fp->fi_delegees == 0)
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001123 swap(nf, fp->fi_deleg_file);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001124 spin_unlock(&fp->fi_lock);
1125
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001126 if (nf)
1127 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001128}
1129
1130static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1131{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -05001132 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001133 struct nfsd_file *nf = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -04001134
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -05001135 WARN_ON_ONCE(!fp->fi_delegees);
1136
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001137 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001138 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001141static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1142{
1143 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001144 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001145 nfs4_put_stid(&dp->dl_stid);
1146}
1147
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001148void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001149{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001150 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001151}
1152
Andrew Elble34ed9872015-10-15 12:07:28 -04001153/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001154 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -04001155 * @clp: a pointer to the nfs4_client we're granting a delegation to
1156 * @fp: a pointer to the nfs4_file we're granting a delegation on
1157 *
1158 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001159 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -04001160 */
1161
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001162static bool
1163nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -04001164{
Andrew Elble34ed9872015-10-15 12:07:28 -04001165 struct nfs4_delegation *searchdp = NULL;
1166 struct nfs4_client *searchclp = NULL;
1167
Benny Halevycdc975052014-05-30 09:09:30 -04001168 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04001169 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001170
Andrew Elble34ed9872015-10-15 12:07:28 -04001171 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1172 searchclp = searchdp->dl_stid.sc_client;
1173 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001174 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -04001175 }
1176 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001177 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -04001178}
1179
1180/**
1181 * hash_delegation_locked - Add a delegation to the appropriate lists
1182 * @dp: a pointer to the nfs4_delegation we are adding.
1183 * @fp: a pointer to the nfs4_file we're granting a delegation on
1184 *
1185 * Return:
1186 * On success: NULL if the delegation was successfully hashed.
1187 *
1188 * On error: -EAGAIN if one was previously granted to this
1189 * nfs4_client for this nfs4_file. Delegation is not hashed.
1190 *
1191 */
1192
1193static int
1194hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1195{
Andrew Elble34ed9872015-10-15 12:07:28 -04001196 struct nfs4_client *clp = dp->dl_stid.sc_client;
1197
1198 lockdep_assert_held(&state_lock);
1199 lockdep_assert_held(&fp->fi_lock);
1200
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001201 if (nfs4_delegation_exists(clp, fp))
1202 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001203 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001204 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001205 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001206 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1207 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001208}
1209
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001210static bool
Jeff Layton42690672014-07-25 07:34:20 -04001211unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Trond Myklebust11b91642014-07-29 21:34:08 -04001213 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001214
Jeff Layton42690672014-07-25 07:34:20 -04001215 lockdep_assert_held(&state_lock);
1216
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001217 if (list_empty(&dp->dl_perfile))
1218 return false;
1219
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001220 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001221 /* Ensure that deleg break won't try to requeue it */
1222 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001223 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001224 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001226 list_del_init(&dp->dl_perfile);
1227 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001228 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001229}
1230
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001231static void destroy_delegation(struct nfs4_delegation *dp)
1232{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001233 bool unhashed;
1234
Jeff Layton42690672014-07-25 07:34:20 -04001235 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001236 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001237 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001238 if (unhashed)
1239 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001240}
1241
1242static void revoke_delegation(struct nfs4_delegation *dp)
1243{
1244 struct nfs4_client *clp = dp->dl_stid.sc_client;
1245
Jeff Layton2d4a5322014-07-25 07:34:21 -04001246 WARN_ON(!list_empty(&dp->dl_recall_lru));
1247
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001248 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001249 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001250 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001251 spin_lock(&clp->cl_lock);
1252 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1253 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001254 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001255 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001256}
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258/*
1259 * SETCLIENTID state
1260 */
1261
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001262static unsigned int clientid_hashval(u32 id)
1263{
1264 return id & CLIENT_HASH_MASK;
1265}
1266
Scott Mayhew6b189102019-03-26 18:06:26 -04001267static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001268{
Scott Mayhew6b189102019-03-26 18:06:26 -04001269 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272/*
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001273 * A stateid that had a deny mode associated with it is being released
1274 * or downgraded. Recalculate the deny mode on the file.
1275 */
1276static void
1277recalculate_deny_mode(struct nfs4_file *fp)
1278{
1279 struct nfs4_ol_stateid *stp;
1280
1281 spin_lock(&fp->fi_lock);
1282 fp->fi_share_deny = 0;
1283 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1284 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1285 spin_unlock(&fp->fi_lock);
1286}
1287
1288static void
1289reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1290{
1291 int i;
1292 bool change = false;
1293
1294 for (i = 1; i < 4; i++) {
1295 if ((i & deny) != i) {
1296 change = true;
1297 clear_deny(i, stp);
1298 }
1299 }
1300
1301 /* Recalculate per-file deny mode if there was a change */
1302 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001303 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001304}
1305
Jeff Layton82c5ff12012-05-11 09:45:13 -04001306/* release all access and file references for a given stateid */
1307static void
1308release_all_access(struct nfs4_ol_stateid *stp)
1309{
1310 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001311 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001312
1313 if (fp && stp->st_deny_bmap != 0)
1314 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001315
1316 for (i = 1; i < 4; i++) {
1317 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001318 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001319 clear_access(i, stp);
1320 }
1321}
1322
Kinglong Meed50ffde2015-07-16 12:05:07 +08001323static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1324{
1325 kfree(sop->so_owner.data);
1326 sop->so_ops->so_free(sop);
1327}
1328
Jeff Layton6b180f02014-07-29 21:34:26 -04001329static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1330{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001331 struct nfs4_client *clp = sop->so_client;
1332
1333 might_lock(&clp->cl_lock);
1334
1335 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001336 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001337 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001338 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001339 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001340}
1341
Trond Myklebusta451b122020-03-01 18:21:38 -05001342static bool
1343nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1344{
1345 return list_empty(&stp->st_perfile);
1346}
1347
Jeff Laytone8568732015-08-24 12:41:47 -04001348static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001349{
Trond Myklebust11b91642014-07-29 21:34:08 -04001350 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001351
Jeff Layton1c755dc2014-07-29 21:34:12 -04001352 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1353
Jeff Laytone8568732015-08-24 12:41:47 -04001354 if (list_empty(&stp->st_perfile))
1355 return false;
1356
Trond Myklebust1d31a252014-07-10 14:07:25 -04001357 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001358 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001359 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001360 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001361 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001362}
1363
Trond Myklebust60116952014-07-29 21:34:06 -04001364static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001365{
Trond Myklebust60116952014-07-29 21:34:06 -04001366 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001367
Sachin Bhamare8287f002015-04-27 14:50:14 +02001368 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001369 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001370 if (stp->st_stateowner)
1371 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001372 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001373}
1374
Jeff Laytonb49e0842014-07-29 21:34:11 -04001375static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001376{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001377 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1378 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001379 struct nfsd_file *nf;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001380
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001381 nf = find_any_file(stp->st_stid.sc_file);
1382 if (nf) {
1383 get_file(nf->nf_file);
1384 filp_close(nf->nf_file, (fl_owner_t)lo);
1385 nfsd_file_put(nf);
1386 }
Jeff Laytonb49e0842014-07-29 21:34:11 -04001387 nfs4_free_ol_stateid(stid);
1388}
1389
Jeff Layton2c41beb2014-07-29 21:34:41 -04001390/*
1391 * Put the persistent reference to an already unhashed generic stateid, while
1392 * holding the cl_lock. If it's the last reference, then put it onto the
1393 * reaplist for later destruction.
1394 */
1395static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1396 struct list_head *reaplist)
1397{
1398 struct nfs4_stid *s = &stp->st_stid;
1399 struct nfs4_client *clp = s->sc_client;
1400
1401 lockdep_assert_held(&clp->cl_lock);
1402
1403 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1404
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001405 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001406 wake_up_all(&close_wq);
1407 return;
1408 }
1409
1410 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1411 list_add(&stp->st_locks, reaplist);
1412}
1413
Jeff Laytone8568732015-08-24 12:41:47 -04001414static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001415{
Chuck Leverf46c4452016-10-29 18:19:03 -04001416 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001417
Trond Myklebusta451b122020-03-01 18:21:38 -05001418 if (!unhash_ol_stateid(stp))
1419 return false;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001420 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001421 nfs4_unhash_stid(&stp->st_stid);
Trond Myklebusta451b122020-03-01 18:21:38 -05001422 return true;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001423}
1424
Jeff Layton5adfd882014-07-29 21:34:31 -04001425static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001426{
Chuck Leverf46c4452016-10-29 18:19:03 -04001427 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001428 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001429
Chuck Leverf46c4452016-10-29 18:19:03 -04001430 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001431 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001432 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001433 if (unhashed)
1434 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001435}
1436
Trond Myklebustc58c6612014-07-29 21:34:35 -04001437static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001438{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001439 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001440
Trond Myklebustd4f04892014-07-29 21:34:36 -04001441 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001442
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001443 list_del_init(&lo->lo_owner.so_strhash);
1444}
1445
Jeff Layton2c41beb2014-07-29 21:34:41 -04001446/*
1447 * Free a list of generic stateids that were collected earlier after being
1448 * fully unhashed.
1449 */
1450static void
1451free_ol_stateid_reaplist(struct list_head *reaplist)
1452{
1453 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001454 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001455
1456 might_sleep();
1457
1458 while (!list_empty(reaplist)) {
1459 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1460 st_locks);
1461 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001462 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001463 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001464 if (fp)
1465 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001466 }
1467}
1468
Jeff Laytond83017f2014-07-29 21:34:42 -04001469static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1470 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001471{
1472 struct nfs4_ol_stateid *stp;
1473
Jeff Laytone8568732015-08-24 12:41:47 -04001474 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1475
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001476 while (!list_empty(&open_stp->st_locks)) {
1477 stp = list_entry(open_stp->st_locks.next,
1478 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001479 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001480 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001481 }
1482}
1483
Jeff Laytone8568732015-08-24 12:41:47 -04001484static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001485 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001486{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001487 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1488
Trond Myklebusta451b122020-03-01 18:21:38 -05001489 if (!unhash_ol_stateid(stp))
1490 return false;
Jeff Laytond83017f2014-07-29 21:34:42 -04001491 release_open_stateid_locks(stp, reaplist);
Trond Myklebusta451b122020-03-01 18:21:38 -05001492 return true;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001493}
1494
1495static void release_open_stateid(struct nfs4_ol_stateid *stp)
1496{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001497 LIST_HEAD(reaplist);
1498
1499 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001500 if (unhash_open_stateid(stp, &reaplist))
1501 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001502 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1503 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001504}
1505
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001506static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001507{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001508 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001509
Trond Myklebustd4f04892014-07-29 21:34:36 -04001510 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001511
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001512 list_del_init(&oo->oo_owner.so_strhash);
1513 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001514}
1515
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001516static void release_last_closed_stateid(struct nfs4_openowner *oo)
1517{
Jeff Layton217526e2014-07-30 08:27:11 -04001518 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1519 nfsd_net_id);
1520 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001521
Jeff Layton217526e2014-07-30 08:27:11 -04001522 spin_lock(&nn->client_lock);
1523 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001524 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001525 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001526 oo->oo_last_closed_stid = NULL;
1527 }
Jeff Layton217526e2014-07-30 08:27:11 -04001528 spin_unlock(&nn->client_lock);
1529 if (s)
1530 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001531}
1532
Jeff Layton2c41beb2014-07-29 21:34:41 -04001533static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001534{
1535 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001536 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001537 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001538
Jeff Layton2c41beb2014-07-29 21:34:41 -04001539 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001540
Trond Myklebustd4f04892014-07-29 21:34:36 -04001541 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001542 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001543 while (!list_empty(&oo->oo_owner.so_stateids)) {
1544 stp = list_first_entry(&oo->oo_owner.so_stateids,
1545 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001546 if (unhash_open_stateid(stp, &reaplist))
1547 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001548 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001549 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001550 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001551 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001552 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001553}
1554
Marc Eshel5282fd72009-04-03 08:27:52 +03001555static inline int
1556hash_sessionid(struct nfs4_sessionid *sessionid)
1557{
1558 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1559
1560 return sid->sequence % SESSION_HASH_SIZE;
1561}
1562
Mark Salter135dd002015-04-06 09:46:00 -04001563#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001564static inline void
1565dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1566{
1567 u32 *ptr = (u32 *)(&sessionid->data[0]);
1568 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1569}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001570#else
1571static inline void
1572dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1573{
1574}
1575#endif
1576
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001577/*
1578 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1579 * won't be used for replay.
1580 */
1581void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1582{
1583 struct nfs4_stateowner *so = cstate->replay_owner;
1584
1585 if (nfserr == nfserr_replay_me)
1586 return;
1587
1588 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001589 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001590 return;
1591 }
1592 if (!so)
1593 return;
1594 if (so->so_is_open_owner)
1595 release_last_closed_stateid(openowner(so));
1596 so->so_seqid++;
1597 return;
1598}
Marc Eshel5282fd72009-04-03 08:27:52 +03001599
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001600static void
1601gen_sessionid(struct nfsd4_session *ses)
1602{
1603 struct nfs4_client *clp = ses->se_client;
1604 struct nfsd4_sessionid *sid;
1605
1606 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1607 sid->clientid = clp->cl_clientid;
1608 sid->sequence = current_sessionid++;
1609 sid->reserved = 0;
1610}
1611
1612/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001613 * The protocol defines ca_maxresponssize_cached to include the size of
1614 * the rpc header, but all we need to cache is the data starting after
1615 * the end of the initial SEQUENCE operation--the rest we regenerate
1616 * each time. Therefore we can advertise a ca_maxresponssize_cached
1617 * value that is the number of bytes in our cache plus a few additional
1618 * bytes. In order to stay on the safe side, and not promise more than
1619 * we can cache, those additional bytes must be the minimum possible: 24
1620 * bytes of rpc header (xid through accept state, with AUTH_NULL
1621 * verifier), 12 for the compound header (with zero-length tag), and 44
1622 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001623 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001624#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1625
Andy Adamson557ce262009-08-28 08:45:04 -04001626static void
1627free_session_slots(struct nfsd4_session *ses)
1628{
1629 int i;
1630
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001631 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1632 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001633 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001634 }
Andy Adamson557ce262009-08-28 08:45:04 -04001635}
1636
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001637/*
1638 * We don't actually need to cache the rpc and session headers, so we
1639 * can allocate a little less for each slot:
1640 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001641static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001642{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001643 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001644
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001645 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1646 size = 0;
1647 else
1648 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1649 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001650}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001651
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001652/*
1653 * XXX: If we run out of reserved DRC memory we could (up to a point)
1654 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001655 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001656 */
NeilBrown2030ca52019-09-20 16:36:45 +10001657static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001658{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001659 u32 slotsize = slot_bytes(ca);
1660 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001661 unsigned long avail, total_avail;
NeilBrown2030ca52019-09-20 16:36:45 +10001662 unsigned int scale_factor;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001663
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001664 spin_lock(&nfsd_drc_lock);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001665 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1666 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1667 else
1668 /* We have handed out more space than we chose in
1669 * set_max_drc() to allow. That isn't really a
1670 * problem as long as that doesn't make us think we
1671 * have lots more due to integer overflow.
1672 */
1673 total_avail = 0;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001674 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001675 /*
NeilBrown2030ca52019-09-20 16:36:45 +10001676 * Never use more than a fraction of the remaining memory,
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001677 * unless it's the only way to give this client a slot.
NeilBrown2030ca52019-09-20 16:36:45 +10001678 * The chosen fraction is either 1/8 or 1/number of threads,
1679 * whichever is smaller. This ensures there are adequate
1680 * slots to support multiple clients per thread.
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001681 * Give the client one slot even if that would require
1682 * over-allocation--it is better than failure.
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001683 */
NeilBrown2030ca52019-09-20 16:36:45 +10001684 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1685
1686 avail = clamp_t(unsigned long, avail, slotsize,
1687 total_avail/scale_factor);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001688 num = min_t(int, num, avail / slotsize);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001689 num = max_t(int, num, 1);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001690 nfsd_drc_mem_used += num * slotsize;
1691 spin_unlock(&nfsd_drc_lock);
1692
1693 return num;
1694}
1695
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001696static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001697{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001698 int slotsize = slot_bytes(ca);
1699
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001700 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001701 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001702 spin_unlock(&nfsd_drc_lock);
1703}
1704
Kinglong Mee60810e52014-01-01 00:35:47 +08001705static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1706 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001707{
Kinglong Mee60810e52014-01-01 00:35:47 +08001708 int numslots = fattrs->maxreqs;
1709 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001710 struct nfsd4_session *new;
1711 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001712
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001713 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001714 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1715 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001716
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001717 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001718 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001719 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001720 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001721 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001722 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001723 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001724 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001725 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001726
1727 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1728 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1729
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001730 return new;
1731out_free:
1732 while (i--)
1733 kfree(new->se_slots[i]);
1734 kfree(new);
1735 return NULL;
1736}
1737
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001738static void free_conn(struct nfsd4_conn *c)
1739{
1740 svc_xprt_put(c->cn_xprt);
1741 kfree(c);
1742}
1743
1744static void nfsd4_conn_lost(struct svc_xpt_user *u)
1745{
1746 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1747 struct nfs4_client *clp = c->cn_session->se_client;
1748
Chuck Lever806d65b2021-05-14 15:56:43 -04001749 trace_nfsd_cb_lost(clp);
1750
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001751 spin_lock(&clp->cl_lock);
1752 if (!list_empty(&c->cn_persession)) {
1753 list_del(&c->cn_persession);
1754 free_conn(c);
1755 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001756 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001757 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001758}
1759
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001760static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001761{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001762 struct nfsd4_conn *conn;
1763
1764 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1765 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001766 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001767 svc_xprt_get(rqstp->rq_xprt);
1768 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001769 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001770 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1771 return conn;
1772}
1773
J. Bruce Fields328ead22010-09-29 16:11:06 -04001774static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1775{
1776 conn->cn_session = ses;
1777 list_add(&conn->cn_persession, &ses->se_conns);
1778}
1779
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001780static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1781{
1782 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001783
1784 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001785 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001786 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001787}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001788
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001789static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001790{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001791 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001792 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001793}
1794
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001795static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001796{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001797 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001798
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001799 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001800 ret = nfsd4_register_conn(conn);
1801 if (ret)
1802 /* oops; xprt is already down: */
1803 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001804 /* We may have gained or lost a callback channel: */
1805 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001806}
1807
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001808static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001809{
1810 u32 dir = NFS4_CDFC4_FORE;
1811
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001812 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001813 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001814 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001815}
1816
1817/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001818static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001819{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001820 struct nfs4_client *clp = s->se_client;
1821 struct nfsd4_conn *c;
1822
1823 spin_lock(&clp->cl_lock);
1824 while (!list_empty(&s->se_conns)) {
1825 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1826 list_del_init(&c->cn_persession);
1827 spin_unlock(&clp->cl_lock);
1828
1829 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1830 free_conn(c);
1831
1832 spin_lock(&clp->cl_lock);
1833 }
1834 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001835}
1836
J. Bruce Fields1377b692012-09-11 21:42:40 -04001837static void __free_session(struct nfsd4_session *ses)
1838{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001839 free_session_slots(ses);
1840 kfree(ses);
1841}
1842
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001843static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001844{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001845 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001846 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001847 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001848}
Andy Adamson557ce262009-08-28 08:45:04 -04001849
Fengguang Wu135ae822012-11-10 07:20:25 -05001850static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001851{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001852 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001853 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001854
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001855 new->se_client = clp;
1856 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001857
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001858 INIT_LIST_HEAD(&new->se_conns);
1859
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001860 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001861 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001862 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001863 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001864 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001865 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001866 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001867 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001868 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001869 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001870
Chuck Leverb0d2e422014-08-22 15:10:59 -04001871 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001872 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001873 /*
1874 * This is a little silly; with sessions there's no real
1875 * use for the callback address. Use the peer address
1876 * as a reasonable default for now, but consider fixing
1877 * the rpc client not to require an address in the
1878 * future:
1879 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001880 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1881 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001882 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001883}
1884
Benny Halevy9089f1b2010-05-12 00:12:26 +03001885/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001886static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001887__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001888{
1889 struct nfsd4_session *elem;
1890 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001891 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001892
Trond Myklebust0a880a22014-07-30 08:27:10 -04001893 lockdep_assert_held(&nn->client_lock);
1894
Marc Eshel5282fd72009-04-03 08:27:52 +03001895 dump_sessionid(__func__, sessionid);
1896 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001897 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001898 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001899 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1900 NFS4_MAX_SESSIONID_LEN)) {
1901 return elem;
1902 }
1903 }
1904
1905 dprintk("%s: session not found\n", __func__);
1906 return NULL;
1907}
1908
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001909static struct nfsd4_session *
1910find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1911 __be32 *ret)
1912{
1913 struct nfsd4_session *session;
1914 __be32 status = nfserr_badsession;
1915
1916 session = __find_in_sessionid_hashtbl(sessionid, net);
1917 if (!session)
1918 goto out;
1919 status = nfsd4_get_session_locked(session);
1920 if (status)
1921 session = NULL;
1922out:
1923 *ret = status;
1924 return session;
1925}
1926
Benny Halevy9089f1b2010-05-12 00:12:26 +03001927/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001928static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001929unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001930{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001931 struct nfs4_client *clp = ses->se_client;
1932 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1933
1934 lockdep_assert_held(&nn->client_lock);
1935
Andy Adamson7116ed62009-04-03 08:27:43 +03001936 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001937 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001938 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001939 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001940}
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1943static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001944STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001946 /*
1947 * We're assuming the clid was not given out from a boot
1948 * precisely 2^32 (about 136 years) before this one. That seems
1949 * a safe assumption:
1950 */
1951 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return 0;
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04001953 trace_nfsd_clid_stale(clid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return 1;
1955}
1956
1957/*
1958 * XXX Should we use a slab cache ?
1959 * This type of memory management is somewhat inefficient, but we use it
1960 * anyway since SETCLIENTID is not a common operation.
1961 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001962static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001965 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Jeff Layton9258a2d2018-03-16 09:47:22 -04001967 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001968 if (clp == NULL)
1969 return NULL;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04001970 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001971 if (clp->cl_name.data == NULL)
1972 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001973 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1974 sizeof(struct list_head),
1975 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001976 if (!clp->cl_ownerstr_hashtbl)
1977 goto err_no_hashtbl;
1978 for (i = 0; i < OWNER_HASH_SIZE; i++)
1979 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
Trond Myklebust5694c932014-04-18 14:43:56 -04001980 INIT_LIST_HEAD(&clp->cl_sessions);
1981 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001982 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04001983 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1984 INIT_LIST_HEAD(&clp->cl_idhash);
1985 INIT_LIST_HEAD(&clp->cl_openowners);
1986 INIT_LIST_HEAD(&clp->cl_delegations);
1987 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001988 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001989#ifdef CONFIG_NFSD_PNFS
1990 INIT_LIST_HEAD(&clp->cl_lo_states);
1991#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001992 INIT_LIST_HEAD(&clp->async_copies);
1993 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001994 spin_lock_init(&clp->cl_lock);
1995 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001997err_no_hashtbl:
1998 kfree(clp->cl_name.data);
1999err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04002000 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04002001 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002004static void __free_client(struct kref *k)
2005{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002006 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2007 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002008
2009 free_svc_cred(&clp->cl_cred);
2010 kfree(clp->cl_ownerstr_hashtbl);
2011 kfree(clp->cl_name.data);
J. Bruce Fields79123442019-06-05 12:42:05 -04002012 kfree(clp->cl_nii_domain.data);
2013 kfree(clp->cl_nii_name.data);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002014 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002015 kmem_cache_free(client_slab, clp);
2016}
2017
YueHaibing297e57a2019-07-08 15:29:33 +08002018static void drop_client(struct nfs4_client *clp)
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002019{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002020 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002021}
2022
Trond Myklebust4dd86e152014-04-18 14:43:58 -04002023static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024free_client(struct nfs4_client *clp)
2025{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002026 while (!list_empty(&clp->cl_sessions)) {
2027 struct nfsd4_session *ses;
2028 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2029 se_perclnt);
2030 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002031 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2032 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002033 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04002034 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002035 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002036 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002037 clp->cl_nfsd_dentry = NULL;
2038 wake_up_all(&expiry_wq);
2039 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002040 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
Benny Halevy84d38ac2010-05-12 00:13:16 +03002043/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04002044static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03002045unhash_client_locked(struct nfs4_client *clp)
2046{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002047 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002048 struct nfsd4_session *ses;
2049
Trond Myklebust0a880a22014-07-30 08:27:10 -04002050 lockdep_assert_held(&nn->client_lock);
2051
Trond Myklebust4beb3452014-07-30 08:27:02 -04002052 /* Mark the client as expired! */
2053 clp->cl_time = 0;
2054 /* Make it invisible */
2055 if (!list_empty(&clp->cl_idhash)) {
2056 list_del_init(&clp->cl_idhash);
2057 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2058 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2059 else
2060 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2061 }
2062 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002063 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002064 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2065 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002066 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03002067}
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04002070unhash_client(struct nfs4_client *clp)
2071{
2072 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2073
2074 spin_lock(&nn->client_lock);
2075 unhash_client_locked(clp);
2076 spin_unlock(&nn->client_lock);
2077}
2078
Jeff Layton97403d92014-07-30 08:27:12 -04002079static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2080{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04002081 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04002082 return nfserr_jukebox;
2083 unhash_client_locked(clp);
2084 return nfs_ok;
2085}
2086
Trond Myklebust4beb3452014-07-30 08:27:02 -04002087static void
2088__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002090 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002091 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 struct list_head reaplist;
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04002096 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07002097 while (!list_empty(&clp->cl_delegations)) {
2098 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04002099 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04002100 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
Benny Halevycdc975052014-05-30 09:09:30 -04002102 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 while (!list_empty(&reaplist)) {
2104 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04002105 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05002106 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04002108 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05002109 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04002110 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04002111 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002112 }
NeilBrownea1da632005-06-23 22:04:17 -07002113 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002114 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002115 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002116 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002118 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2119 struct nfs4_stateowner *so, *tmp;
2120
2121 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2122 so_strhash) {
2123 /* Should be no openowners at this point */
2124 WARN_ON_ONCE(so->so_is_open_owner);
2125 remove_blocked_locks(lockowner(so));
2126 }
2127 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002128 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002129 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002130 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002131 if (clp->cl_cb_conn.cb_xprt)
2132 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002133 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002134 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
Trond Myklebust4beb3452014-07-30 08:27:02 -04002137static void
2138destroy_client(struct nfs4_client *clp)
2139{
2140 unhash_client(clp);
2141 __destroy_client(clp);
2142}
2143
Scott Mayhew362063a2019-03-26 18:06:28 -04002144static void inc_reclaim_complete(struct nfs4_client *clp)
2145{
2146 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2147
2148 if (!nn->track_reclaim_completes)
2149 return;
2150 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2151 return;
2152 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2153 nn->reclaim_str_hashtbl_size) {
2154 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2155 clp->net->ns.inum);
2156 nfsd4_end_grace(nn);
2157 }
2158}
2159
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002160static void expire_client(struct nfs4_client *clp)
2161{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002162 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002163 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002164 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002165}
2166
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002167static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2168{
2169 memcpy(target->cl_verifier.data, source->data,
2170 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002173static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2174{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2176 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2177}
2178
J. Bruce Fields50043852015-11-20 15:58:37 -05002179static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2180{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002181 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2182 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2183 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002184 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2185 if ((source->cr_principal && !target->cr_principal) ||
2186 (source->cr_raw_principal && !target->cr_raw_principal) ||
2187 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002188 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002189
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002190 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 target->cr_uid = source->cr_uid;
2192 target->cr_gid = source->cr_gid;
2193 target->cr_group_info = source->cr_group_info;
2194 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002195 target->cr_gss_mech = source->cr_gss_mech;
2196 if (source->cr_gss_mech)
2197 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
2200
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002201static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002202compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2203{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002204 if (o1->len < o2->len)
2205 return -1;
2206 if (o1->len > o2->len)
2207 return 1;
2208 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002209}
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002212same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2213{
2214 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
2217static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002218same_clid(clientid_t *cl1, clientid_t *cl2)
2219{
2220 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221}
2222
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002223static bool groups_equal(struct group_info *g1, struct group_info *g2)
2224{
2225 int i;
2226
2227 if (g1->ngroups != g2->ngroups)
2228 return false;
2229 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002230 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002231 return false;
2232 return true;
2233}
2234
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002235/*
2236 * RFC 3530 language requires clid_inuse be returned when the
2237 * "principal" associated with a requests differs from that previously
2238 * used. We use uid, gid's, and gss principal string as our best
2239 * approximation. We also don't want to allow non-gss use of a client
2240 * established using gss: in theory cr_principal should catch that
2241 * change, but in practice cr_principal can be null even in the gss case
2242 * since gssd doesn't always pass down a principal string.
2243 */
2244static bool is_gss_cred(struct svc_cred *cr)
2245{
2246 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2247 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2248}
2249
2250
Vivek Trivedi5559b502012-07-24 21:18:20 +05302251static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002252same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2253{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002254 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002255 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2256 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002257 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2258 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002259 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002260 if (cr1->cr_principal == cr2->cr_principal)
2261 return true;
2262 if (!cr1->cr_principal || !cr2->cr_principal)
2263 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302264 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
J. Bruce Fields57266a62013-04-13 14:27:29 -04002267static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2268{
2269 struct svc_cred *cr = &rqstp->rq_cred;
2270 u32 service;
2271
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002272 if (!cr->cr_gss_mech)
2273 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002274 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2275 return service == RPC_GSS_SVC_INTEGRITY ||
2276 service == RPC_GSS_SVC_PRIVACY;
2277}
2278
Andrew Elblededeb132016-06-15 12:52:08 -04002279bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002280{
2281 struct svc_cred *cr = &rqstp->rq_cred;
2282
2283 if (!cl->cl_mach_cred)
2284 return true;
2285 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2286 return false;
2287 if (!svc_rqst_integrity_protected(rqstp))
2288 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002289 if (cl->cl_cred.cr_raw_principal)
2290 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2291 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002292 if (!cr->cr_principal)
2293 return false;
2294 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2295}
2296
Jeff Layton294ac322014-07-30 08:27:15 -04002297static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002298{
Chuck Leverab4684d2012-03-02 17:13:50 -05002299 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Jeff Laytonf4199922014-06-17 07:44:11 -04002301 /*
2302 * This is opaque to client, so no need to byte-swap. Use
2303 * __force to keep sparse happy
2304 */
Arnd Bergmann9104ae42019-11-04 16:45:30 +01002305 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002306 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002307 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
Jeff Layton294ac322014-07-30 08:27:15 -04002310static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2311{
Arnd Bergmann9cc76802019-10-31 15:53:13 +01002312 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
Jeff Layton294ac322014-07-30 08:27:15 -04002313 clp->cl_clientid.cl_id = nn->clientid_counter++;
2314 gen_confirm(clp, nn);
2315}
2316
Jeff Layton4770d722014-07-29 21:34:10 -04002317static struct nfs4_stid *
2318find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002319{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002320 struct nfs4_stid *ret;
2321
2322 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2323 if (!ret || !ret->sc_type)
2324 return NULL;
2325 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002326}
2327
Jeff Layton4770d722014-07-29 21:34:10 -04002328static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002329find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002330{
2331 struct nfs4_stid *s;
2332
Jeff Layton4770d722014-07-29 21:34:10 -04002333 spin_lock(&cl->cl_lock);
2334 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002335 if (s != NULL) {
2336 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002337 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002338 else
2339 s = NULL;
2340 }
Jeff Layton4770d722014-07-29 21:34:10 -04002341 spin_unlock(&cl->cl_lock);
2342 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002343}
2344
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002345static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2346{
2347 struct nfsdfs_client *nc;
2348 nc = get_nfsdfs_client(inode);
2349 if (!nc)
2350 return NULL;
2351 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2352}
2353
J. Bruce Fields169319f2019-06-19 12:39:46 -04002354static void seq_quote_mem(struct seq_file *m, char *data, int len)
2355{
2356 seq_printf(m, "\"");
Andy Shevchenkoc0546392021-06-30 18:55:43 -07002357 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
J. Bruce Fields169319f2019-06-19 12:39:46 -04002358 seq_printf(m, "\"");
2359}
2360
Dave Wysochanski3518c862021-06-02 13:51:39 -04002361static const char *cb_state2str(int state)
2362{
2363 switch (state) {
2364 case NFSD4_CB_UP:
2365 return "UP";
2366 case NFSD4_CB_UNKNOWN:
2367 return "UNKNOWN";
2368 case NFSD4_CB_DOWN:
2369 return "DOWN";
2370 case NFSD4_CB_FAULT:
2371 return "FAULT";
2372 }
2373 return "UNDEFINED";
2374}
2375
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002376static int client_info_show(struct seq_file *m, void *v)
2377{
2378 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002379 struct nfs4_client *clp;
2380 u64 clid;
2381
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002382 clp = get_nfsdfs_clp(inode);
2383 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002384 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002385 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2386 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002387 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
NeilBrown472d1552021-03-20 09:38:04 +11002388 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2389 seq_puts(m, "status: confirmed\n");
2390 else
2391 seq_puts(m, "status: unconfirmed\n");
J. Bruce Fields169319f2019-06-19 12:39:46 -04002392 seq_printf(m, "name: ");
2393 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2394 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields79123442019-06-05 12:42:05 -04002395 if (clp->cl_nii_domain.data) {
2396 seq_printf(m, "Implementation domain: ");
2397 seq_quote_mem(m, clp->cl_nii_domain.data,
2398 clp->cl_nii_domain.len);
2399 seq_printf(m, "\nImplementation name: ");
2400 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
Arnd Bergmanne29f4702019-10-31 14:52:43 +01002401 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
J. Bruce Fields79123442019-06-05 12:42:05 -04002402 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2403 }
Dave Wysochanski3518c862021-06-02 13:51:39 -04002404 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2405 seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002406 drop_client(clp);
2407
2408 return 0;
2409}
2410
2411static int client_info_open(struct inode *inode, struct file *file)
2412{
2413 return single_open(file, client_info_show, inode);
2414}
2415
2416static const struct file_operations client_info_fops = {
2417 .open = client_info_open,
2418 .read = seq_read,
2419 .llseek = seq_lseek,
2420 .release = single_release,
2421};
2422
J. Bruce Fields78599c42019-04-22 15:26:23 -04002423static void *states_start(struct seq_file *s, loff_t *pos)
2424 __acquires(&clp->cl_lock)
2425{
2426 struct nfs4_client *clp = s->private;
2427 unsigned long id = *pos;
2428 void *ret;
2429
2430 spin_lock(&clp->cl_lock);
2431 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2432 *pos = id;
2433 return ret;
2434}
2435
2436static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2437{
2438 struct nfs4_client *clp = s->private;
2439 unsigned long id = *pos;
2440 void *ret;
2441
2442 id = *pos;
2443 id++;
2444 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2445 *pos = id;
2446 return ret;
2447}
2448
2449static void states_stop(struct seq_file *s, void *v)
2450 __releases(&clp->cl_lock)
2451{
2452 struct nfs4_client *clp = s->private;
2453
2454 spin_unlock(&clp->cl_lock);
2455}
2456
Achilles Gaikwad580da462020-04-20 18:20:31 +05302457static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2458{
2459 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2460}
2461
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002462static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002463{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002464 struct inode *inode = f->nf_inode;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002465
2466 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2467 MAJOR(inode->i_sb->s_dev),
2468 MINOR(inode->i_sb->s_dev),
2469 inode->i_ino);
2470}
2471
2472static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2473{
2474 seq_printf(s, "owner: ");
2475 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2476}
2477
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002478static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2479{
J. Bruce Fieldsee590d22020-04-13 22:03:06 -04002480 seq_printf(s, "0x%.8x", stid->si_generation);
2481 seq_printf(s, "%12phN", &stid->si_opaque);
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002482}
2483
J. Bruce Fields78599c42019-04-22 15:26:23 -04002484static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2485{
2486 struct nfs4_ol_stateid *ols;
2487 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002488 struct nfsd_file *file;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002489 struct nfs4_stateowner *oo;
2490 unsigned int access, deny;
2491
2492 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2493 return 0; /* XXX: or SEQ_SKIP? */
2494 ols = openlockstateid(st);
2495 oo = ols->st_stateowner;
2496 nf = st->sc_file;
2497 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002498 if (!file)
2499 return 0;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002500
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002501 seq_printf(s, "- ");
2502 nfs4_show_stateid(s, &st->sc_stateid);
2503 seq_printf(s, ": { type: open, ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002504
2505 access = bmap_to_share_mode(ols->st_access_bmap);
2506 deny = bmap_to_share_mode(ols->st_deny_bmap);
2507
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002508 seq_printf(s, "access: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002509 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2510 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002511 seq_printf(s, "deny: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002512 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2513 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2514
2515 nfs4_show_superblock(s, file);
2516 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302517 nfs4_show_fname(s, file);
2518 seq_printf(s, ", ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002519 nfs4_show_owner(s, oo);
2520 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002521 nfsd_file_put(file);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002522
2523 return 0;
2524}
2525
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002526static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2527{
2528 struct nfs4_ol_stateid *ols;
2529 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002530 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002531 struct nfs4_stateowner *oo;
2532
2533 ols = openlockstateid(st);
2534 oo = ols->st_stateowner;
2535 nf = st->sc_file;
2536 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002537 if (!file)
2538 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002539
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002540 seq_printf(s, "- ");
2541 nfs4_show_stateid(s, &st->sc_stateid);
2542 seq_printf(s, ": { type: lock, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002543
2544 /*
2545 * Note: a lock stateid isn't really the same thing as a lock,
2546 * it's the locking state held by one owner on a file, and there
2547 * may be multiple (or no) lock ranges associated with it.
2548 * (Same for the matter is true of open stateids.)
2549 */
2550
2551 nfs4_show_superblock(s, file);
2552 /* XXX: open stateid? */
2553 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302554 nfs4_show_fname(s, file);
2555 seq_printf(s, ", ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002556 nfs4_show_owner(s, oo);
2557 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002558 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002559
2560 return 0;
2561}
2562
2563static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2564{
2565 struct nfs4_delegation *ds;
2566 struct nfs4_file *nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002567 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002568
2569 ds = delegstateid(st);
2570 nf = st->sc_file;
J. Bruce Fields9affa432020-07-15 13:31:36 -04002571 file = find_deleg_file(nf);
2572 if (!file)
2573 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002574
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002575 seq_printf(s, "- ");
2576 nfs4_show_stateid(s, &st->sc_stateid);
2577 seq_printf(s, ": { type: deleg, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002578
2579 /* Kinda dead code as long as we only support read delegs: */
2580 seq_printf(s, "access: %s, ",
2581 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2582
2583 /* XXX: lease time, whether it's being recalled. */
2584
2585 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302586 seq_printf(s, ", ");
2587 nfs4_show_fname(s, file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002588 seq_printf(s, " }\n");
J. Bruce Fields9affa432020-07-15 13:31:36 -04002589 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002590
2591 return 0;
2592}
2593
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002594static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2595{
2596 struct nfs4_layout_stateid *ls;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002597 struct nfsd_file *file;
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002598
2599 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2600 file = ls->ls_file;
2601
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002602 seq_printf(s, "- ");
2603 nfs4_show_stateid(s, &st->sc_stateid);
2604 seq_printf(s, ": { type: layout, ");
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002605
2606 /* XXX: What else would be useful? */
2607
2608 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302609 seq_printf(s, ", ");
2610 nfs4_show_fname(s, file);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002611 seq_printf(s, " }\n");
2612
2613 return 0;
2614}
2615
J. Bruce Fields78599c42019-04-22 15:26:23 -04002616static int states_show(struct seq_file *s, void *v)
2617{
2618 struct nfs4_stid *st = v;
2619
2620 switch (st->sc_type) {
2621 case NFS4_OPEN_STID:
2622 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002623 case NFS4_LOCK_STID:
2624 return nfs4_show_lock(s, st);
2625 case NFS4_DELEG_STID:
2626 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002627 case NFS4_LAYOUT_STID:
2628 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002629 default:
2630 return 0; /* XXX: or SEQ_SKIP? */
2631 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002632 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002633}
2634
2635static struct seq_operations states_seq_ops = {
2636 .start = states_start,
2637 .next = states_next,
2638 .stop = states_stop,
2639 .show = states_show
2640};
2641
2642static int client_states_open(struct inode *inode, struct file *file)
2643{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002644 struct seq_file *s;
2645 struct nfs4_client *clp;
2646 int ret;
2647
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002648 clp = get_nfsdfs_clp(inode);
2649 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002650 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002651
2652 ret = seq_open(file, &states_seq_ops);
2653 if (ret)
2654 return ret;
2655 s = file->private_data;
2656 s->private = clp;
2657 return 0;
2658}
2659
2660static int client_opens_release(struct inode *inode, struct file *file)
2661{
2662 struct seq_file *m = file->private_data;
2663 struct nfs4_client *clp = m->private;
2664
2665 /* XXX: alternatively, we could get/drop in seq start/stop */
2666 drop_client(clp);
2667 return 0;
2668}
2669
2670static const struct file_operations client_states_fops = {
2671 .open = client_states_open,
2672 .read = seq_read,
2673 .llseek = seq_lseek,
2674 .release = client_opens_release,
2675};
2676
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002677/*
2678 * Normally we refuse to destroy clients that are in use, but here the
2679 * administrator is telling us to just do it. We also want to wait
2680 * so the caller has a guarantee that the client's locks are gone by
2681 * the time the write returns:
2682 */
YueHaibing297e57a2019-07-08 15:29:33 +08002683static void force_expire_client(struct nfs4_client *clp)
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002684{
2685 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2686 bool already_expired;
2687
Chuck Lever2958d2e2021-05-14 15:56:06 -04002688 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
2689
J. Bruce Fieldsf7104cc2021-08-12 16:41:43 -04002690 spin_lock(&nn->client_lock);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002691 clp->cl_time = 0;
J. Bruce Fieldsf7104cc2021-08-12 16:41:43 -04002692 spin_unlock(&nn->client_lock);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002693
2694 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2695 spin_lock(&nn->client_lock);
2696 already_expired = list_empty(&clp->cl_lru);
2697 if (!already_expired)
2698 unhash_client_locked(clp);
2699 spin_unlock(&nn->client_lock);
2700
2701 if (!already_expired)
2702 expire_client(clp);
2703 else
2704 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2705}
2706
2707static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2708 size_t size, loff_t *pos)
2709{
2710 char *data;
2711 struct nfs4_client *clp;
2712
2713 data = simple_transaction_get(file, buf, size);
2714 if (IS_ERR(data))
2715 return PTR_ERR(data);
2716 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2717 return -EINVAL;
2718 clp = get_nfsdfs_clp(file_inode(file));
2719 if (!clp)
2720 return -ENXIO;
2721 force_expire_client(clp);
2722 drop_client(clp);
2723 return 7;
2724}
2725
2726static const struct file_operations client_ctl_fops = {
2727 .write = client_ctl_write,
2728 .release = simple_transaction_release,
2729};
2730
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002731static const struct tree_descr client_files[] = {
2732 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002733 [1] = {"states", &client_states_fops, S_IRUSR},
Petr Vorel6cbfad52020-03-13 13:39:57 +01002734 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002735 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002736};
2737
Jeff Layton2216d442012-11-12 15:00:57 -05002738static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002739 struct svc_rqst *rqstp, nfs4_verifier *verf)
2740{
2741 struct nfs4_client *clp;
2742 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002743 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002744 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002745 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
NeilBrown472d1552021-03-20 09:38:04 +11002746 struct dentry *dentries[ARRAY_SIZE(client_files)];
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002747
2748 clp = alloc_client(name);
2749 if (clp == NULL)
2750 return NULL;
2751
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002752 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2753 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002754 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002755 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002756 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002757 gen_clid(clp, nn);
2758 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002759 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01002760 clp->cl_time = ktime_get_boottime_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002761 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002762 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002763 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002764 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002765 clp->net = net;
NeilBrown472d1552021-03-20 09:38:04 +11002766 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2767 nn, &clp->cl_nfsdfs,
2768 clp->cl_clientid.cl_id - nn->clientid_base,
2769 client_files, dentries);
2770 clp->cl_nfsd_info_dentry = dentries[0];
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002771 if (!clp->cl_nfsd_dentry) {
2772 free_client(clp);
2773 return NULL;
2774 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002775 return clp;
2776}
2777
NeilBrownfd39ca92005-06-23 22:04:03 -07002778static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002779add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2780{
2781 struct rb_node **new = &(root->rb_node), *parent = NULL;
2782 struct nfs4_client *clp;
2783
2784 while (*new) {
2785 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2786 parent = *new;
2787
2788 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2789 new = &((*new)->rb_left);
2790 else
2791 new = &((*new)->rb_right);
2792 }
2793
2794 rb_link_node(&new_clp->cl_namenode, parent, new);
2795 rb_insert_color(&new_clp->cl_namenode, root);
2796}
2797
2798static struct nfs4_client *
2799find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2800{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002801 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002802 struct rb_node *node = root->rb_node;
2803 struct nfs4_client *clp;
2804
2805 while (node) {
2806 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2807 cmp = compare_blob(&clp->cl_name, name);
2808 if (cmp > 0)
2809 node = node->rb_left;
2810 else if (cmp < 0)
2811 node = node->rb_right;
2812 else
2813 return clp;
2814 }
2815 return NULL;
2816}
2817
2818static void
2819add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
2821 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002822 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Trond Myklebust0a880a22014-07-30 08:27:10 -04002824 lockdep_assert_held(&nn->client_lock);
2825
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002826 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002827 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002829 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002830 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831}
2832
NeilBrownfd39ca92005-06-23 22:04:03 -07002833static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834move_to_confirmed(struct nfs4_client *clp)
2835{
2836 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002837 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Trond Myklebust0a880a22014-07-30 08:27:10 -04002839 lockdep_assert_held(&nn->client_lock);
2840
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002841 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002842 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002843 add_clp_to_name_tree(clp, &nn->conf_name_tree);
J. Bruce Fields934bd072021-05-25 14:53:44 -04002844 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2845 trace_nfsd_clid_confirmed(&clp->cl_clientid);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002846 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848
2849static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002850find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
2852 struct nfs4_client *clp;
2853 unsigned int idhashval = clientid_hashval(clid->cl_id);
2854
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002855 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002856 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002857 if ((bool)clp->cl_minorversion != sessions)
2858 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002859 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 }
2863 return NULL;
2864}
2865
2866static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002867find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2868{
2869 struct list_head *tbl = nn->conf_id_hashtbl;
2870
Trond Myklebust0a880a22014-07-30 08:27:10 -04002871 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002872 return find_client_in_id_table(tbl, clid, sessions);
2873}
2874
2875static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002876find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002878 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Trond Myklebust0a880a22014-07-30 08:27:10 -04002880 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002881 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882}
2883
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002884static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002885{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002886 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002887}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002888
NeilBrown28ce6052005-06-23 22:03:56 -07002889static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002890find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002891{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002892 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002893 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002894}
2895
2896static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002897find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002898{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002899 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002900 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002901}
2902
NeilBrownfd39ca92005-06-23 22:04:03 -07002903static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002904gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002906 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002907 struct sockaddr *sa = svc_addr(rqstp);
2908 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002909 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Jeff Layton7077ecb2009-08-14 12:57:58 -04002911 /* Currently, we only support tcp and tcp6 for the callback channel */
2912 if (se->se_callback_netid_len == 3 &&
2913 !memcmp(se->se_callback_netid_val, "tcp", 3))
2914 expected_family = AF_INET;
2915 else if (se->se_callback_netid_len == 4 &&
2916 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2917 expected_family = AF_INET6;
2918 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 goto out_err;
2920
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002921 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002922 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002923 (struct sockaddr *)&conn->cb_addr,
2924 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002925
J. Bruce Fields07263f12010-05-31 19:09:40 -04002926 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002928
J. Bruce Fields07263f12010-05-31 19:09:40 -04002929 if (conn->cb_addr.ss_family == AF_INET6)
2930 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002931
J. Bruce Fields07263f12010-05-31 19:09:40 -04002932 conn->cb_prog = se->se_callback_prog;
2933 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002934 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Chuck Lever1eace0d2020-04-05 14:15:29 -04002935 trace_nfsd_cb_args(clp, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return;
2937out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002938 conn->cb_addr.ss_family = AF_UNSPEC;
2939 conn->cb_addrlen = 0;
Chuck Lever1eace0d2020-04-05 14:15:29 -04002940 trace_nfsd_cb_nodelegs(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return;
2942}
2943
Andy Adamson074fe892009-04-03 08:28:15 +03002944/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002945 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002946 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002947static void
Andy Adamson074fe892009-04-03 08:28:15 +03002948nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2949{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04002950 struct xdr_buf *buf = resp->xdr->buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002951 struct nfsd4_slot *slot = resp->cstate.slot;
2952 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002953
Andy Adamson557ce262009-08-28 08:45:04 -04002954 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002955
J. Bruce Fields085def32017-10-18 16:17:18 -04002956 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002957 slot->sl_opcnt = resp->opcnt;
2958 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002959 free_svc_cred(&slot->sl_cred);
2960 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002961
J. Bruce Fields085def32017-10-18 16:17:18 -04002962 if (!nfsd4_cache_this(resp)) {
2963 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002964 return;
2965 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002966 slot->sl_flags |= NFSD4_SLOT_CACHED;
2967
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002968 base = resp->cstate.data_offset;
2969 slot->sl_datalen = buf->len - base;
2970 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002971 WARN(1, "%s: sessions DRC could not cache compound\n",
2972 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002973 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002974}
2975
2976/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002977 * Encode the replay sequence operation from the slot values.
2978 * If cachethis is FALSE encode the uncached rep error on the next
2979 * operation which sets resp->p and increments resp->opcnt for
2980 * nfs4svc_encode_compoundres.
2981 *
Andy Adamson074fe892009-04-03 08:28:15 +03002982 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002983static __be32
2984nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2985 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002986{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002987 struct nfsd4_op *op;
2988 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002989
Andy Adamsonabfabf82009-07-23 19:02:18 -04002990 /* Encode the replayed sequence operation */
2991 op = &args->ops[resp->opcnt - 1];
2992 nfsd4_encode_operation(resp, op);
2993
J. Bruce Fields085def32017-10-18 16:17:18 -04002994 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2995 return op->status;
2996 if (args->opcnt == 1) {
2997 /*
2998 * The original operation wasn't a solo sequence--we
2999 * always cache those--so this retry must not match the
3000 * original:
3001 */
3002 op->status = nfserr_seq_false_retry;
3003 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04003004 op = &args->ops[resp->opcnt++];
3005 op->status = nfserr_retry_uncached_rep;
3006 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03003007 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04003008 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03003009}
3010
3011/*
Andy Adamson557ce262009-08-28 08:45:04 -04003012 * The sequence operation is not cached because we can use the slot and
3013 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03003014 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04003015static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03003016nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3017 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03003018{
Andy Adamson557ce262009-08-28 08:45:04 -04003019 struct nfsd4_slot *slot = resp->cstate.slot;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003020 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003021 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03003022 __be32 status;
3023
Andy Adamson557ce262009-08-28 08:45:04 -04003024 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03003025
Andy Adamsonabfabf82009-07-23 19:02:18 -04003026 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04003027 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04003028 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03003029
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003030 p = xdr_reserve_space(xdr, slot->sl_datalen);
3031 if (!p) {
3032 WARN_ON_ONCE(1);
3033 return nfserr_serverfault;
3034 }
3035 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3036 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03003037
Andy Adamson557ce262009-08-28 08:45:04 -04003038 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003039 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03003040}
3041
Andy Adamson0733d212009-04-03 08:28:01 +03003042/*
3043 * Set the exchange_id flags returned by the server.
3044 */
3045static void
3046nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3047{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003048#ifdef CONFIG_NFSD_PNFS
3049 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3050#else
Andy Adamson0733d212009-04-03 08:28:01 +03003051 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003052#endif
Andy Adamson0733d212009-04-03 08:28:01 +03003053
3054 /* Referrals are supported, Migration is not. */
3055 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3056
3057 /* set the wire flags to return to client. */
3058 clid->flags = new->cl_exchange_flags;
3059}
3060
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003061static bool client_has_openowners(struct nfs4_client *clp)
3062{
3063 struct nfs4_openowner *oo;
3064
3065 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3066 if (!list_empty(&oo->oo_owner.so_stateids))
3067 return true;
3068 }
3069 return false;
3070}
3071
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003072static bool client_has_state(struct nfs4_client *clp)
3073{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003074 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08003075#ifdef CONFIG_NFSD_PNFS
3076 || !list_empty(&clp->cl_lo_states)
3077#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04003078 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04003079 || !list_empty(&clp->cl_sessions)
3080 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003081}
3082
J. Bruce Fields79123442019-06-05 12:42:05 -04003083static __be32 copy_impl_id(struct nfs4_client *clp,
3084 struct nfsd4_exchange_id *exid)
3085{
3086 if (!exid->nii_domain.data)
3087 return 0;
3088 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3089 if (!clp->cl_nii_domain.data)
3090 return nfserr_jukebox;
3091 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3092 if (!clp->cl_nii_name.data)
3093 return nfserr_jukebox;
Arnd Bergmanne29f4702019-10-31 14:52:43 +01003094 clp->cl_nii_time = exid->nii_time;
J. Bruce Fields79123442019-06-05 12:42:05 -04003095 return 0;
3096}
3097
Al Virob37ad282006-10-19 23:28:59 -07003098__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003099nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3100 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003101{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003102 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003103 struct nfs4_client *conf, *new;
3104 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003105 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04003106 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03003107 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04003108 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003109 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003110 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03003111
Jeff Layton363168b2009-08-14 12:57:56 -04003112 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03003113 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Chuck Lever523ec6e2020-11-02 15:19:12 -05003114 "ip_addr=%s flags %x, spa_how %u\n",
Andy Adamson0733d212009-04-03 08:28:01 +03003115 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04003116 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03003117
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003118 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03003119 return nfserr_inval;
3120
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003121 new = create_client(exid->clname, rqstp, &verf);
3122 if (new == NULL)
3123 return nfserr_jukebox;
J. Bruce Fields79123442019-06-05 12:42:05 -04003124 status = copy_impl_id(new, exid);
3125 if (status)
3126 goto out_nolock;
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003127
Andy Adamson0733d212009-04-03 08:28:01 +03003128 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003129 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04003130 exid->spo_must_enforce[0] = 0;
3131 exid->spo_must_enforce[1] = (
3132 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3133 1 << (OP_EXCHANGE_ID - 32) |
3134 1 << (OP_CREATE_SESSION - 32) |
3135 1 << (OP_DESTROY_SESSION - 32) |
3136 1 << (OP_DESTROY_CLIENTID - 32));
3137
3138 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3139 1 << (OP_OPEN_DOWNGRADE) |
3140 1 << (OP_LOCKU) |
3141 1 << (OP_DELEGRETURN));
3142
3143 exid->spo_must_allow[1] &= (
3144 1 << (OP_TEST_STATEID - 32) |
3145 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003146 if (!svc_rqst_integrity_protected(rqstp)) {
3147 status = nfserr_inval;
3148 goto out_nolock;
3149 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003150 /*
3151 * Sometimes userspace doesn't give us a principal.
3152 * Which is a bug, really. Anyway, we can't enforce
3153 * MACH_CRED in that case, better to give up now:
3154 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05003155 if (!new->cl_cred.cr_principal &&
3156 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003157 status = nfserr_serverfault;
3158 goto out_nolock;
3159 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003160 new->cl_mach_cred = true;
Gustavo A. R. Silva76c50eb2020-11-20 12:26:40 -06003161 break;
Andy Adamson0733d212009-04-03 08:28:01 +03003162 case SP4_NONE:
3163 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003164 default: /* checked by xdr code */
3165 WARN_ON_ONCE(1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003166 fallthrough;
Andy Adamson0733d212009-04-03 08:28:01 +03003167 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08003168 status = nfserr_encr_alg_unsupp;
3169 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03003170 }
3171
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003172 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003173 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003174 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03003175 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003176 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3177 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3178
J. Bruce Fields136e6582012-05-12 20:37:23 -04003179 if (update) {
3180 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003181 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03003182 goto out;
3183 }
Andrew Elblededeb132016-06-15 12:52:08 -04003184 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003185 status = nfserr_wrong_cred;
3186 goto out;
3187 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003188 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03003189 status = nfserr_perm;
3190 goto out;
3191 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003192 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03003193 status = nfserr_not_same;
3194 goto out;
3195 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003196 /* case 6 */
3197 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
Chuck Levere8f80c52021-05-14 15:56:19 -04003198 trace_nfsd_clid_confirmed_r(conf);
J. Bruce Fields136e6582012-05-12 20:37:23 -04003199 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003200 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003201 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003202 if (client_has_state(conf)) {
3203 status = nfserr_clid_inuse;
Chuck Lever27787732021-05-14 15:55:29 -04003204 trace_nfsd_clid_cred_mismatch(conf, rqstp);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003205 goto out;
3206 }
Andy Adamson0733d212009-04-03 08:28:01 +03003207 goto out_new;
3208 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003209 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003210 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
Chuck Levere8f80c52021-05-14 15:56:19 -04003211 trace_nfsd_clid_confirmed_r(conf);
J. Bruce Fields136e6582012-05-12 20:37:23 -04003212 goto out_copy;
3213 }
3214 /* case 5, client reboot */
Chuck Lever744ea542021-05-14 15:55:36 -04003215 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003216 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003217 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003218 }
3219
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003220 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003221 status = nfserr_noent;
3222 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003223 }
3224
Chuck Levere8f80c52021-05-14 15:56:19 -04003225 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003226 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003227 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003228
Chuck Levere8f80c52021-05-14 15:56:19 -04003229 /* case 1, new owner ID */
3230 trace_nfsd_clid_fresh(new);
3231
Andy Adamson0733d212009-04-03 08:28:01 +03003232out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003233 if (conf) {
3234 status = mark_client_expired_locked(conf);
3235 if (status)
3236 goto out;
Chuck Lever2958d2e2021-05-14 15:56:06 -04003237 trace_nfsd_clid_replaced(&conf->cl_clientid);
Jeff Laytonfd699b82014-07-30 08:27:14 -04003238 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003239 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003240 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3241 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003242
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003243 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003244 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003245out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003246 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3247 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003248
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003249 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3250 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003251
3252 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003253 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003254 status = nfs_ok;
3255
3256out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003257 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003258out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003259 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003260 expire_client(new);
Chuck Levere8f80c52021-05-14 15:56:19 -04003261 if (unconf) {
3262 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003263 expire_client(unconf);
Chuck Levere8f80c52021-05-14 15:56:19 -04003264 }
Andy Adamson0733d212009-04-03 08:28:01 +03003265 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003266}
3267
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003268static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003269check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003270{
Andy Adamson88e588d2009-07-23 19:02:15 -04003271 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3272 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003273
3274 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003275 if (slot_inuse) {
3276 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003277 return nfserr_jukebox;
3278 else
3279 return nfserr_seq_misordered;
3280 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003281 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003282 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003283 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003284 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003285 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003286 return nfserr_seq_misordered;
3287}
3288
Andy Adamson49557cc2009-07-23 19:02:16 -04003289/*
3290 * Cache the create session result into the create session single DRC
3291 * slot cache by saving the xdr structure. sl_seqid has been set.
3292 * Do this for solo or embedded create session operations.
3293 */
3294static void
3295nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003296 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003297{
3298 slot->sl_status = nfserr;
3299 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3300}
3301
3302static __be32
3303nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3304 struct nfsd4_clid_slot *slot)
3305{
3306 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3307 return slot->sl_status;
3308}
3309
Mi Jinlong1b74c252011-07-14 14:50:17 +08003310#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3311 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3312 1 + /* MIN tag is length with zero, only length */ \
3313 3 + /* version, opcount, opcode */ \
3314 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3315 /* seqid, slotID, slotID, cache */ \
3316 4 ) * sizeof(__be32))
3317
3318#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3319 2 + /* verifier: AUTH_NULL, length 0 */\
3320 1 + /* status */ \
3321 1 + /* MIN tag is length with zero, only length */ \
3322 3 + /* opcount, opcode, opstatus*/ \
3323 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3324 /* seqid, slotID, slotID, slotID, status */ \
3325 5 ) * sizeof(__be32))
3326
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003327static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003328{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003329 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3330
J. Bruce Fields373cd402013-04-08 15:42:12 -04003331 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3332 return nfserr_toosmall;
3333 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3334 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003335 ca->headerpadsz = 0;
3336 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3337 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3338 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3339 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3340 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3341 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3342 /*
3343 * Note decreasing slot size below client's request may make it
3344 * difficult for client to function correctly, whereas
3345 * decreasing the number of slots will (just?) affect
3346 * performance. When short on memory we therefore prefer to
3347 * decrease number of slots instead of their size. Clients that
3348 * request larger slots than they need will get poor results:
NeilBrown7f49fd5d2019-09-20 16:33:16 +10003349 * Note that we always allow at least one slot, because our
3350 * accounting is soft and provides no guarantees either way.
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003351 */
NeilBrown2030ca52019-09-20 16:36:45 +10003352 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003353
J. Bruce Fields373cd402013-04-08 15:42:12 -04003354 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003355}
3356
Chuck Lever45006322016-03-01 13:06:02 -05003357/*
3358 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3359 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3360 */
3361#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3362 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3363
3364#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3365 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3366
Kinglong Mee8a891632013-12-23 18:11:02 +08003367#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003368 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003369#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003370 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3371 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003372
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003373static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3374{
3375 ca->headerpadsz = 0;
3376
Kinglong Mee8a891632013-12-23 18:11:02 +08003377 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003378 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003379 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003380 return nfserr_toosmall;
3381 ca->maxresp_cached = 0;
3382 if (ca->maxops < 2)
3383 return nfserr_toosmall;
3384
3385 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003386}
3387
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003388static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3389{
3390 switch (cbs->flavor) {
3391 case RPC_AUTH_NULL:
3392 case RPC_AUTH_UNIX:
3393 return nfs_ok;
3394 default:
3395 /*
3396 * GSS case: the spec doesn't allow us to return this
3397 * error. But it also doesn't allow us not to support
3398 * GSS.
3399 * I'd rather this fail hard than return some error the
3400 * client might think it can already handle:
3401 */
3402 return nfserr_encr_alg_unsupp;
3403 }
3404}
3405
Andy Adamson069b6ad2009-04-03 08:27:58 +03003406__be32
3407nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003408 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003409{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003410 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003411 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003412 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003413 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003414 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003415 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003416 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003417 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003418 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003419
Mi Jinlonga62573d2011-03-23 17:57:07 +08003420 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3421 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003422 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3423 if (status)
3424 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003425 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003426 if (status)
3427 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003428 status = check_backchannel_attrs(&cr_ses->back_channel);
3429 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003430 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003431 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003432 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003433 if (!new)
3434 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003435 conn = alloc_conn_from_crses(rqstp, cr_ses);
3436 if (!conn)
3437 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003438
Jeff Laytond20c11d2014-07-30 08:27:07 -04003439 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003440 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003441 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003442 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003443
3444 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003445 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003446 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003447 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003448 cs_slot = &conf->cl_cs_slot;
3449 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003450 if (status) {
3451 if (status == nfserr_replay_cache)
3452 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003453 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003454 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003455 } else if (unconf) {
Chuck Lever27787732021-05-14 15:55:29 -04003456 status = nfserr_clid_inuse;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003457 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003458 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Chuck Lever27787732021-05-14 15:55:29 -04003459 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003460 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003461 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003462 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003463 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003464 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003465 cs_slot = &unconf->cl_cs_slot;
3466 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003467 if (status) {
3468 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003469 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003470 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003471 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003472 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003473 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003474 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003475 if (status) {
3476 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003477 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003478 }
Chuck Lever2958d2e2021-05-14 15:56:06 -04003479 trace_nfsd_clid_replaced(&old->cl_clientid);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003480 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003481 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003482 conf = unconf;
3483 } else {
3484 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003485 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003486 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003487 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003488 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003489 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003490 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003491 cr_ses->flags &= ~SESSION4_RDMA;
3492
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003493 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003494 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003495
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003496 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003497 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003498 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003499 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003500
Jeff Laytond20c11d2014-07-30 08:27:07 -04003501 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003502 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003503 spin_unlock(&nn->client_lock);
J. Bruce Fields934bd072021-05-25 14:53:44 -04003504 if (conf == unconf)
3505 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003506 /* init connection and backchannel */
3507 nfsd4_init_conn(rqstp, conn, new);
3508 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003509 if (old)
3510 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003511 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003512out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003513 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003514 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003515 if (old)
3516 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003517out_free_session:
3518 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003519out_release_drc_mem:
3520 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003521 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003522}
3523
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003524static __be32 nfsd4_map_bcts_dir(u32 *dir)
3525{
3526 switch (*dir) {
3527 case NFS4_CDFC4_FORE:
3528 case NFS4_CDFC4_BACK:
3529 return nfs_ok;
3530 case NFS4_CDFC4_FORE_OR_BOTH:
3531 case NFS4_CDFC4_BACK_OR_BOTH:
3532 *dir = NFS4_CDFC4_BOTH;
3533 return nfs_ok;
zhengbinfc5fc5d2019-12-19 17:29:20 +08003534 }
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003535 return nfserr_inval;
3536}
3537
Christoph Hellwigeb698532017-05-08 20:58:35 +02003538__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3539 struct nfsd4_compound_state *cstate,
3540 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003541{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003542 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003543 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003544 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003545 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003546
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003547 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3548 if (status)
3549 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003550 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003551 session->se_cb_prog = bc->bc_cb_program;
3552 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003553 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003554
3555 nfsd4_probe_callback(session->se_client);
3556
3557 return nfs_ok;
3558}
3559
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003560static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3561{
3562 struct nfsd4_conn *c;
3563
3564 list_for_each_entry(c, &s->se_conns, cn_persession) {
3565 if (c->cn_xprt == xpt) {
3566 return c;
3567 }
3568 }
3569 return NULL;
3570}
3571
3572static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3573 struct nfsd4_session *session, u32 req)
3574{
3575 struct nfs4_client *clp = session->se_client;
3576 struct svc_xprt *xpt = rqst->rq_xprt;
3577 struct nfsd4_conn *c;
3578 __be32 status;
3579
3580 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3581 spin_lock(&clp->cl_lock);
3582 c = __nfsd4_find_conn(xpt, session);
3583 if (!c)
3584 status = nfserr_noent;
3585 else if (req == c->cn_flags)
3586 status = nfs_ok;
3587 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3588 c->cn_flags != NFS4_CDFC4_BACK)
3589 status = nfs_ok;
3590 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3591 c->cn_flags != NFS4_CDFC4_FORE)
3592 status = nfs_ok;
3593 else
3594 status = nfserr_inval;
3595 spin_unlock(&clp->cl_lock);
3596 return status;
3597}
3598
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003599__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3600 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003601 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003602{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003603 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003604 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003605 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003606 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003607 struct net *net = SVC_NET(rqstp);
3608 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003609
3610 if (!nfsd4_last_compound_op(rqstp))
3611 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003612 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003613 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003614 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003615 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003616 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003617 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003618 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003619 goto out;
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003620 status = nfsd4_match_existing_connection(rqstp, session, bcts->dir);
3621 if (status == nfs_ok || status == nfserr_inval)
3622 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003623 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003624 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003625 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003626 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003627 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003628 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003629 goto out;
3630 nfsd4_init_conn(rqstp, conn, session);
3631 status = nfs_ok;
3632out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003633 nfsd4_put_session(session);
3634out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003635 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003636}
3637
J. Bruce Fields665d5072018-04-11 16:53:36 -04003638static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003639{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003640 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003641 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003642 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003643}
3644
Andy Adamson069b6ad2009-04-03 08:27:58 +03003645__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003646nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3647 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003648{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003649 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003650 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003651 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003652 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003653 struct net *net = SVC_NET(r);
3654 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003655
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003656 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003657 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003658 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003659 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003660 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003661 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003662 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003663 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003664 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003665 if (!ses)
3666 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003667 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003668 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003669 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003670 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003671 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003672 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003673 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003674 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003675
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003676 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003677
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003678 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003679 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003680out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003681 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003682out_client_lock:
3683 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003684out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003685 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003686}
3687
J. Bruce Fields57266a62013-04-13 14:27:29 -04003688static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003689{
3690 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003691 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003692 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003693 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003694
3695 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003696 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003697 if (c)
3698 goto out_free;
3699 status = nfserr_conn_not_bound_to_session;
3700 if (clp->cl_mach_cred)
3701 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003702 __nfsd4_hash_conn(new, ses);
3703 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003704 ret = nfsd4_register_conn(new);
3705 if (ret)
3706 /* oops; xprt is already down: */
3707 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003708 return nfs_ok;
3709out_free:
3710 spin_unlock(&clp->cl_lock);
3711 free_conn(new);
3712 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003713}
3714
Mi Jinlong868b89c2011-04-27 09:09:58 +08003715static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3716{
3717 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3718
3719 return args->opcnt > session->se_fchannel.maxops;
3720}
3721
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003722static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3723 struct nfsd4_session *session)
3724{
3725 struct xdr_buf *xb = &rqstp->rq_arg;
3726
3727 return xb->len > session->se_fchannel.maxreq_sz;
3728}
3729
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003730static bool replay_matches_cache(struct svc_rqst *rqstp,
3731 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3732{
3733 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3734
3735 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3736 (bool)seq->cachethis)
3737 return false;
3738 /*
Scott Mayhew6e73e922019-10-09 15:11:37 -04003739 * If there's an error then the reply can have fewer ops than
3740 * the call.
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003741 */
Scott Mayhew6e73e922019-10-09 15:11:37 -04003742 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3743 return false;
3744 /*
3745 * But if we cached a reply with *more* ops than the call you're
3746 * sending us now, then this new call is clearly not really a
3747 * replay of the old one:
3748 */
3749 if (slot->sl_opcnt > argp->opcnt)
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003750 return false;
3751 /* This is the only check explicitly called by spec: */
3752 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3753 return false;
3754 /*
3755 * There may be more comparisons we could actually do, but the
3756 * spec doesn't require us to catch every case where the calls
3757 * don't match (that would require caching the call as well as
3758 * the reply), so we don't bother.
3759 */
3760 return true;
3761}
3762
Andy Adamson069b6ad2009-04-03 08:27:58 +03003763__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003764nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3765 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003766{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003767 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003768 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003769 struct xdr_stream *xdr = resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003770 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003771 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003772 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003773 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003774 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003775 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003776 struct net *net = SVC_NET(rqstp);
3777 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003778
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003779 if (resp->opcnt != 1)
3780 return nfserr_sequence_pos;
3781
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003782 /*
3783 * Will be either used or freed by nfsd4_sequence_check_conn
3784 * below.
3785 */
3786 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3787 if (!conn)
3788 return nfserr_jukebox;
3789
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003790 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003791 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003792 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003793 goto out_no_session;
3794 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003795
Mi Jinlong868b89c2011-04-27 09:09:58 +08003796 status = nfserr_too_many_ops;
3797 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003798 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003799
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003800 status = nfserr_req_too_big;
3801 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003802 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003803
Benny Halevyb85d4c02009-04-03 08:28:08 +03003804 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003805 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003806 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003807
Andy Adamson557ce262009-08-28 08:45:04 -04003808 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003809 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3810
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003811 /* We do not negotiate the number of slots yet, so set the
3812 * maxslots to the session maxreqs which is used to encode
3813 * sr_highest_slotid and the sr_target_slot id to maxslots */
3814 seq->maxslots = session->se_fchannel.maxreqs;
3815
J. Bruce Fields73e79482012-02-13 16:39:00 -05003816 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3817 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003818 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003819 status = nfserr_seq_misordered;
3820 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003821 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003822 status = nfserr_seq_false_retry;
3823 if (!replay_matches_cache(rqstp, seq, slot))
3824 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003825 cstate->slot = slot;
3826 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003827 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003828 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003829 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003830 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003831 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003832 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003833 }
3834 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003835 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003836
J. Bruce Fields57266a62013-04-13 14:27:29 -04003837 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003838 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003839 if (status)
3840 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003841
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003842 buflen = (seq->cachethis) ?
3843 session->se_fchannel.maxresp_cached :
3844 session->se_fchannel.maxresp_sz;
3845 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3846 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003847 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003848 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003849 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003850
3851 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003852 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003853 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003854 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003855 if (seq->cachethis)
3856 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003857 else
3858 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003859
3860 cstate->slot = slot;
3861 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003862 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003863
Benny Halevyb85d4c02009-04-03 08:28:08 +03003864out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003865 switch (clp->cl_cb_state) {
3866 case NFSD4_CB_DOWN:
3867 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3868 break;
3869 case NFSD4_CB_FAULT:
3870 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3871 break;
3872 default:
3873 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003874 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003875 if (!list_empty(&clp->cl_revoked))
3876 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003877out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003878 if (conn)
3879 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003880 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003881 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003882out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003883 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003884 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003885}
3886
Trond Myklebustb6076642014-06-30 11:48:35 -04003887void
3888nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3889{
3890 struct nfsd4_compound_state *cs = &resp->cstate;
3891
3892 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003893 if (cs->status != nfserr_replay_cache) {
3894 nfsd4_store_cache_entry(resp);
3895 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3896 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003897 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003898 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003899 } else if (cs->clp)
3900 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003901}
3902
Mi Jinlong345c2842011-10-20 17:51:39 +08003903__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003904nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3905 struct nfsd4_compound_state *cstate,
3906 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003907{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003908 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003909 struct nfs4_client *conf, *unconf;
3910 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003911 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003912 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003913
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003914 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003915 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003916 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003917 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003918
3919 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003920 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003921 status = nfserr_clientid_busy;
3922 goto out;
3923 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003924 status = mark_client_expired_locked(conf);
3925 if (status)
3926 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003927 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003928 } else if (unconf)
3929 clp = unconf;
3930 else {
3931 status = nfserr_stale_clientid;
3932 goto out;
3933 }
Andrew Elblededeb132016-06-15 12:52:08 -04003934 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003935 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003936 status = nfserr_wrong_cred;
3937 goto out;
3938 }
Chuck Leverc41a9b72021-05-14 15:56:00 -04003939 trace_nfsd_clid_destroyed(&clp->cl_clientid);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003940 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003941out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003942 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003943 if (clp)
3944 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003945 return status;
3946}
3947
Andy Adamson069b6ad2009-04-03 08:27:58 +03003948__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003949nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3950 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003951{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003952 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003953 struct nfs4_client *clp = cstate->clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003954 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003955
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003956 if (rc->rca_one_fs) {
3957 if (!cstate->current_fh.fh_dentry)
3958 return nfserr_nofilehandle;
3959 /*
3960 * We don't take advantage of the rca_one_fs case.
3961 * That's OK, it's optional, we can safely ignore it.
3962 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003963 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003964 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003965
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003966 status = nfserr_complete_already;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003967 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003968 goto out;
3969
3970 status = nfserr_stale_clientid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003971 if (is_client_expired(clp))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003972 /*
3973 * The following error isn't really legal.
3974 * But we only get here if the client just explicitly
3975 * destroyed the client. Surely it no longer cares what
3976 * error it gets back on an operation for the dead
3977 * client.
3978 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003979 goto out;
3980
3981 status = nfs_ok;
Chuck Levercee8aa02021-05-14 15:55:54 -04003982 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003983 nfsd4_client_record_create(clp);
3984 inc_reclaim_complete(clp);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003985out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003986 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003987}
3988
3989__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003990nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003991 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003993 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003994 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003996 struct nfs4_client *conf, *new;
3997 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003998 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003999 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4000
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04004001 new = create_client(clname, rqstp, &clverifier);
4002 if (new == NULL)
4003 return nfserr_jukebox;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004004 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004005 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04004006 if (conf && client_has_state(conf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05004008 if (clp_used_exchangeid(conf))
4009 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04004010 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Chuck Lever27787732021-05-14 15:55:29 -04004011 trace_nfsd_clid_cred_mismatch(conf, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 goto out;
4013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004015 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04004016 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004017 unhash_client_locked(unconf);
Chuck Lever744ea542021-05-14 15:55:36 -04004018 if (conf) {
4019 if (same_verf(&conf->cl_verifier, &clverifier)) {
4020 copy_clid(new, conf);
4021 gen_confirm(new, nn);
4022 } else
4023 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4024 &clverifier);
Chuck Lever237f91c2021-05-14 15:56:13 -04004025 } else
4026 trace_nfsd_clid_fresh(new);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04004027 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09004028 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004029 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4031 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4032 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04004033 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 status = nfs_ok;
4035out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004036 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04004037 if (new)
4038 free_client(new);
Chuck Lever237f91c2021-05-14 15:56:13 -04004039 if (unconf) {
4040 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004041 expire_client(unconf);
Chuck Lever237f91c2021-05-14 15:56:13 -04004042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 return status;
4044}
4045
Al Virob37ad282006-10-19 23:28:59 -07004046__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004047nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02004048 struct nfsd4_compound_state *cstate,
4049 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050{
Christoph Hellwigeb698532017-05-08 20:58:35 +02004051 struct nfsd4_setclientid_confirm *setclientid_confirm =
4052 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07004053 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04004054 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4056 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07004057 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004058 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004060 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07004062
Jeff Laytond20c11d2014-07-30 08:27:07 -04004063 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004064 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004065 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004066 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04004067 * We try hard to give out unique clientid's, so if we get an
4068 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004069 * the client may be buggy; this should never happen.
4070 *
4071 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004072 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004073 status = nfserr_clid_inuse;
Chuck Lever27787732021-05-14 15:55:29 -04004074 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4075 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004076 goto out;
Chuck Lever27787732021-05-14 15:55:29 -04004077 }
4078 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4079 trace_nfsd_clid_cred_mismatch(conf, rqstp);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004080 goto out;
Chuck Lever27787732021-05-14 15:55:29 -04004081 }
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004082 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04004083 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 status = nfs_ok;
Chuck Lever237f91c2021-05-14 15:56:13 -04004085 } else
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004086 status = nfserr_stale_clientid;
4087 goto out;
4088 }
4089 status = nfs_ok;
Chuck Lever237f91c2021-05-14 15:56:13 -04004090 if (conf) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04004091 old = unconf;
4092 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004093 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
Chuck Lever237f91c2021-05-14 15:56:13 -04004094 } else {
Jeff Laytond20c11d2014-07-30 08:27:07 -04004095 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4096 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04004097 status = nfserr_clid_inuse;
4098 if (client_has_state(old)
4099 && !same_creds(&unconf->cl_cred,
4100 &old->cl_cred))
4101 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04004102 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04004103 if (status) {
4104 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04004105 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04004106 }
Chuck Lever2958d2e2021-05-14 15:56:06 -04004107 trace_nfsd_clid_replaced(&old->cl_clientid);
J. Bruce Fields221a6872013-04-01 22:23:49 -04004108 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04004109 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04004110 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07004111 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04004112 get_client_locked(conf);
4113 spin_unlock(&nn->client_lock);
J. Bruce Fields934bd072021-05-25 14:53:44 -04004114 if (conf == unconf)
4115 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
Jeff Laytond20c11d2014-07-30 08:27:07 -04004116 nfsd4_probe_callback(conf);
4117 spin_lock(&nn->client_lock);
4118 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04004120 spin_unlock(&nn->client_lock);
4121 if (old)
4122 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 return status;
4124}
4125
J. Bruce Fields32513b42011-10-13 16:00:16 -04004126static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127{
J. Bruce Fields32513b42011-10-13 16:00:16 -04004128 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4129}
4130
4131/* OPEN Share state helper functions */
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004132static void nfsd4_init_file(struct svc_fh *fh, unsigned int hashval,
Jeff Layton5b095e92014-10-23 08:01:02 -04004133 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04004134{
Trond Myklebust950e0112014-06-30 11:48:31 -04004135 lockdep_assert_held(&state_lock);
4136
Elena Reshetova818a34e2017-10-20 12:53:30 +03004137 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004138 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04004139 INIT_LIST_HEAD(&fp->fi_stateids);
4140 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004141 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004142 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
Jeff Layton0c637be2014-08-22 12:05:43 -04004143 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004144 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004145 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004146 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4147 memset(fp->fi_access, 0, sizeof(fp->fi_access));
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004148 fp->fi_aliased = false;
4149 fp->fi_inode = d_inode(fh->fh_dentry);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004150#ifdef CONFIG_NFSD_PNFS
4151 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02004152 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004153#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04004154 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155}
4156
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004157void
NeilBrowne60d4392005-06-23 22:03:01 -07004158nfsd4_free_slabs(void)
4159{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004160 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004161 kmem_cache_destroy(openowner_slab);
4162 kmem_cache_destroy(lockowner_slab);
4163 kmem_cache_destroy(file_slab);
4164 kmem_cache_destroy(stateid_slab);
4165 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004166 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07004167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
Bryan Schumaker72083392011-11-01 15:24:59 -04004169int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170nfsd4_init_slabs(void)
4171{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004172 client_slab = kmem_cache_create("nfsd4_clients",
4173 sizeof(struct nfs4_client), 0, 0, NULL);
4174 if (client_slab == NULL)
4175 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004176 openowner_slab = kmem_cache_create("nfsd4_openowners",
4177 sizeof(struct nfs4_openowner), 0, 0, NULL);
4178 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04004179 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004180 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08004181 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004182 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004183 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07004184 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09004185 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07004186 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004187 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07004188 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004189 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07004190 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004191 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07004192 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09004193 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07004194 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004195 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004196 odstate_slab = kmem_cache_create("nfsd4_odstate",
4197 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4198 if (odstate_slab == NULL)
4199 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07004201
Sachin Bhamare8287f002015-04-27 14:50:14 +02004202out_free_deleg_slab:
4203 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004204out_free_stateid_slab:
4205 kmem_cache_destroy(stateid_slab);
4206out_free_file_slab:
4207 kmem_cache_destroy(file_slab);
4208out_free_lockowner_slab:
4209 kmem_cache_destroy(lockowner_slab);
4210out_free_openowner_slab:
4211 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004212out_free_client_slab:
4213 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004214out:
NeilBrowne60d4392005-06-23 22:03:01 -07004215 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216}
4217
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004218static void init_nfs4_replay(struct nfs4_replay *rp)
4219{
4220 rp->rp_status = nfserr_serverfault;
4221 rp->rp_buflen = 0;
4222 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04004223 mutex_init(&rp->rp_mutex);
4224}
4225
4226static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4227 struct nfs4_stateowner *so)
4228{
4229 if (!nfsd4_has_session(cstate)) {
4230 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004231 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004232 }
4233}
4234
4235void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4236{
4237 struct nfs4_stateowner *so = cstate->replay_owner;
4238
4239 if (so != NULL) {
4240 cstate->replay_owner = NULL;
4241 mutex_unlock(&so->so_replay.rp_mutex);
4242 nfs4_put_stateowner(so);
4243 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004244}
4245
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004246static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247{
4248 struct nfs4_stateowner *sop;
4249
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004250 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004251 if (!sop)
4252 return NULL;
4253
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04004254 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004255 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004256 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004257 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004259
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004260 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004261 sop->so_client = clp;
4262 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04004263 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004264 return sop;
4265}
4266
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004267static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004268{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004269 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004270
Trond Myklebustd4f04892014-07-29 21:34:36 -04004271 list_add(&oo->oo_owner.so_strhash,
4272 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004273 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274}
4275
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004276static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4277{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004278 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004279}
4280
Jeff Layton6b180f02014-07-29 21:34:26 -04004281static void nfs4_free_openowner(struct nfs4_stateowner *so)
4282{
4283 struct nfs4_openowner *oo = openowner(so);
4284
4285 kmem_cache_free(openowner_slab, oo);
4286}
4287
4288static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004289 .so_unhash = nfs4_unhash_openowner,
4290 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004291};
4292
Andrew Elble7fc05642015-11-05 20:42:43 -05004293static struct nfs4_ol_stateid *
4294nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4295{
4296 struct nfs4_ol_stateid *local, *ret = NULL;
4297 struct nfs4_openowner *oo = open->op_openowner;
4298
4299 lockdep_assert_held(&fp->fi_lock);
4300
4301 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4302 /* ignore lock owners */
4303 if (local->st_stateowner->so_is_open_owner == 0)
4304 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004305 if (local->st_stateowner != &oo->oo_owner)
4306 continue;
4307 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004308 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004309 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05004310 break;
4311 }
4312 }
4313 return ret;
4314}
4315
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004316static __be32
4317nfsd4_verify_open_stid(struct nfs4_stid *s)
4318{
4319 __be32 ret = nfs_ok;
4320
4321 switch (s->sc_type) {
4322 default:
4323 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004324 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004325 case NFS4_CLOSED_STID:
4326 case NFS4_CLOSED_DELEG_STID:
4327 ret = nfserr_bad_stateid;
4328 break;
4329 case NFS4_REVOKED_DELEG_STID:
4330 ret = nfserr_deleg_revoked;
4331 }
4332 return ret;
4333}
4334
4335/* Lock the stateid st_mutex, and deal with races with CLOSE */
4336static __be32
4337nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4338{
4339 __be32 ret;
4340
Andrew Elble4f34bd02017-11-08 17:29:51 -05004341 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004342 ret = nfsd4_verify_open_stid(&stp->st_stid);
4343 if (ret != nfs_ok)
4344 mutex_unlock(&stp->st_mutex);
4345 return ret;
4346}
4347
4348static struct nfs4_ol_stateid *
4349nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4350{
4351 struct nfs4_ol_stateid *stp;
4352 for (;;) {
4353 spin_lock(&fp->fi_lock);
4354 stp = nfsd4_find_existing_open(fp, open);
4355 spin_unlock(&fp->fi_lock);
4356 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4357 break;
4358 nfs4_put_stid(&stp->st_stid);
4359 }
4360 return stp;
4361}
4362
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004363static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004364alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004365 struct nfsd4_compound_state *cstate)
4366{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004367 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004368 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004370 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4371 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004373 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004374 oo->oo_owner.so_is_open_owner = 1;
4375 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004376 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004377 if (nfsd4_has_session(cstate))
4378 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004379 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004380 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004381 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004382 spin_lock(&clp->cl_lock);
4383 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004384 if (ret == NULL) {
4385 hash_openowner(oo, clp, strhashval);
4386 ret = oo;
4387 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004388 nfs4_free_stateowner(&oo->oo_owner);
4389
Trond Myklebustd4f04892014-07-29 21:34:36 -04004390 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004391 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392}
4393
Andrew Elble7fc05642015-11-05 20:42:43 -05004394static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004395init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004396{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
Andrew Elble7fc05642015-11-05 20:42:43 -05004398 struct nfs4_openowner *oo = open->op_openowner;
4399 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004400 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004401
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004402 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004403 /* We are moving these outside of the spinlocks to avoid the warnings */
4404 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004405 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004406
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004407retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004408 spin_lock(&oo->oo_owner.so_client->cl_lock);
4409 spin_lock(&fp->fi_lock);
4410
4411 retstp = nfsd4_find_existing_open(fp, open);
4412 if (retstp)
4413 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004414
4415 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004416 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004417 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004418 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004419 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004420 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004421 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 stp->st_access_bmap = 0;
4423 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004424 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004425 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004426 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004427
4428out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004429 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004430 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004431 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004432 /* Handle races with CLOSE */
4433 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4434 nfs4_put_stid(&retstp->st_stid);
4435 goto retry;
4436 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004437 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004438 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004439 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004440 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004441 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
Jeff Laytond3134b12014-07-29 21:34:32 -04004444/*
4445 * In the 4.0 case we need to keep the owners around a little while to handle
4446 * CLOSE replay. We still do need to release any file access that is held by
4447 * them before returning however.
4448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004450move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
Jeff Layton217526e2014-07-30 08:27:11 -04004452 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004453 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4454 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4455 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004456
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004457 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458
Jeff Laytonb401be222014-07-29 21:34:33 -04004459 /*
4460 * We know that we hold one reference via nfsd4_close, and another
4461 * "persistent" reference for the client. If the refcount is higher
4462 * than 2, then there are still calls in progress that are using this
4463 * stateid. We can't put the sc_file reference until they are finished.
4464 * Wait for the refcount to drop to 2. Since it has been unhashed,
4465 * there should be no danger of the refcount going back up again at
4466 * this point.
4467 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004468 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004469
Jeff Laytond3134b12014-07-29 21:34:32 -04004470 release_all_access(s);
4471 if (s->st_stid.sc_file) {
4472 put_nfs4_file(s->st_stid.sc_file);
4473 s->st_stid.sc_file = NULL;
4474 }
Jeff Layton217526e2014-07-30 08:27:11 -04004475
4476 spin_lock(&nn->client_lock);
4477 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004478 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004479 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004480 oo->oo_time = ktime_get_boottime_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004481 spin_unlock(&nn->client_lock);
4482 if (last)
4483 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484}
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486/* search file_hashtbl[] for file */
4487static struct nfs4_file *
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004488find_file_locked(struct svc_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 struct nfs4_file *fp;
4491
Madhuparna Bhowmik36a80492020-02-13 20:33:31 +05304492 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4493 lockdep_is_held(&state_lock)) {
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004494 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004495 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004496 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 }
4499 return NULL;
4500}
4501
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004502static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh,
4503 unsigned int hashval)
4504{
4505 struct nfs4_file *fp;
4506 struct nfs4_file *ret = NULL;
4507 bool alias_found = false;
4508
4509 spin_lock(&state_lock);
4510 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4511 lockdep_is_held(&state_lock)) {
4512 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4513 if (refcount_inc_not_zero(&fp->fi_ref))
4514 ret = fp;
4515 } else if (d_inode(fh->fh_dentry) == fp->fi_inode)
4516 fp->fi_aliased = alias_found = true;
4517 }
4518 if (likely(ret == NULL)) {
4519 nfsd4_init_file(fh, hashval, new);
4520 new->fi_aliased = alias_found;
4521 ret = new;
4522 }
4523 spin_unlock(&state_lock);
4524 return ret;
4525}
4526
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004527static struct nfs4_file * find_file(struct svc_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004528{
4529 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004530 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004531
Jeff Layton5b095e92014-10-23 08:01:02 -04004532 rcu_read_lock();
4533 fp = find_file_locked(fh, hashval);
4534 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004535 return fp;
4536}
4537
4538static struct nfs4_file *
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004539find_or_add_file(struct nfs4_file *new, struct svc_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004540{
4541 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004542 unsigned int hashval = file_hashval(fh);
4543
4544 rcu_read_lock();
4545 fp = find_file_locked(fh, hashval);
4546 rcu_read_unlock();
4547 if (fp)
4548 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004549
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004550 return insert_file(new, fh, hashval);
Trond Myklebust950e0112014-06-30 11:48:31 -04004551}
4552
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004553/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 * Called to check deny when READ with all zero stateid or
4555 * WRITE with all zero or all one stateid
4556 */
Al Virob37ad282006-10-19 23:28:59 -07004557static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4559{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004561 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004563 fp = find_file(current_fh);
NeilBrown13cd2182005-06-23 22:03:10 -07004564 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004565 return ret;
4566 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004567 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004568 if (fp->fi_share_deny & deny_type)
4569 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004570 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004571 put_nfs4_file(fp);
4572 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573}
4574
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004575static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004577 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004578 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4579 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004580
Trond Myklebust11b91642014-07-29 21:34:08 -04004581 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004582
Jeff Layton02e12152014-07-16 10:31:57 -04004583 /*
4584 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004585 * already holding inode->i_lock.
4586 *
Jeff Laytondff13992014-07-08 14:02:49 -04004587 * If the dl_time != 0, then we know that it has already been
4588 * queued for a lease break. Don't queue it again.
4589 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004590 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004591 if (dp->dl_time == 0) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004592 dp->dl_time = ktime_get_boottime_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004593 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004594 }
Jeff Layton02e12152014-07-16 10:31:57 -04004595 spin_unlock(&state_lock);
4596}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004598static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4599 struct rpc_task *task)
4600{
4601 struct nfs4_delegation *dp = cb_to_delegation(cb);
4602
J. Bruce Fields12ed22f2020-07-31 16:20:05 -04004603 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4604 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
Andrew Elblea4579742015-08-31 12:06:41 -04004605 return 1;
4606
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004607 switch (task->tk_status) {
4608 case 0:
4609 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004610 case -NFS4ERR_DELAY:
4611 rpc_delay(task, 2 * HZ);
4612 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004613 case -EBADHANDLE:
4614 case -NFS4ERR_BAD_STATEID:
4615 /*
4616 * Race: client probably got cb_recall before open reply
4617 * granting delegation.
4618 */
4619 if (dp->dl_retries--) {
4620 rpc_delay(task, 2 * HZ);
4621 return 0;
4622 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004623 fallthrough;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004624 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004625 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004626 }
4627}
4628
4629static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4630{
4631 struct nfs4_delegation *dp = cb_to_delegation(cb);
4632
4633 nfs4_put_stid(&dp->dl_stid);
4634}
4635
Julia Lawallc4cb8972015-11-21 22:57:39 +01004636static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004637 .prepare = nfsd4_cb_recall_prepare,
4638 .done = nfsd4_cb_recall_done,
4639 .release = nfsd4_cb_recall_release,
4640};
4641
Jeff Layton02e12152014-07-16 10:31:57 -04004642static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4643{
4644 /*
4645 * We're assuming the state code never drops its reference
4646 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004647 * callback (and since the lease code is serialized by the
4648 * i_lock) we know the server hasn't removed the lease yet, and
4649 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004650 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004651 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004652 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004653}
4654
Jeff Layton1c8c6012013-06-21 08:58:15 -04004655/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004656static bool
4657nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004658{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004659 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004660 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4661 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004662
Chuck Lever17d76dd2021-05-14 15:57:20 -04004663 trace_nfsd_cb_recall(&dp->dl_stid);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04004664
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004665 /*
4666 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004667 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004668 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004669 */
4670 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Jeff Layton02e12152014-07-16 10:31:57 -04004672 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004673 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004674 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004675 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677}
4678
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004679static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4680{
4681 struct nfs4_delegation *dl = fl->fl_owner;
4682 struct svc_rqst *rqst;
4683 struct nfs4_client *clp;
4684
4685 if (!i_am_nfsd())
4686 return NULL;
4687 rqst = kthread_data(current);
J. Bruce Fields13956162020-09-25 10:12:39 -04004688 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4689 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
J. Bruce Fields34b09af2020-08-05 15:10:11 -04004690 return NULL;
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004691 clp = *(rqst->rq_lease_breaker);
4692 return dl->dl_stid.sc_client == clp;
4693}
4694
Jeff Laytonc45198e2014-09-01 07:12:07 -04004695static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004696nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4697 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698{
4699 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004700 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 else
4702 return -EAGAIN;
4703}
4704
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004705static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004706 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004707 .lm_break = nfsd_break_deleg_cb,
4708 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709};
4710
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004711static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4712{
4713 if (nfsd4_has_session(cstate))
4714 return nfs_ok;
4715 if (seqid == so->so_seqid - 1)
4716 return nfserr_replay_me;
4717 if (seqid == so->so_seqid)
4718 return nfs_ok;
4719 return nfserr_bad_seqid;
4720}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
J. Bruce Fields7950b532021-01-21 17:57:41 -05004722static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
4723 struct nfsd_net *nn)
4724{
4725 struct nfs4_client *found;
4726
4727 spin_lock(&nn->client_lock);
4728 found = find_confirmed_client(clid, sessions, nn);
4729 if (found)
4730 atomic_inc(&found->cl_rpc_users);
4731 spin_unlock(&nn->client_lock);
4732 return found;
4733}
4734
J. Bruce Fields460d2702021-01-21 17:57:40 -05004735static __be32 set_client(clientid_t *clid,
Jeff Layton4b24ca72014-06-30 11:48:44 -04004736 struct nfsd4_compound_state *cstate,
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004737 struct nfsd_net *nn)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004738{
Jeff Layton4b24ca72014-06-30 11:48:44 -04004739 if (cstate->clp) {
J. Bruce Fields7950b532021-01-21 17:57:41 -05004740 if (!same_clid(&cstate->clp->cl_clientid, clid))
Jeff Layton4b24ca72014-06-30 11:48:44 -04004741 return nfserr_stale_clientid;
4742 return nfs_ok;
4743 }
Jeff Layton4b24ca72014-06-30 11:48:44 -04004744 if (STALE_CLIENTID(clid, nn))
4745 return nfserr_stale_clientid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004746 /*
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004747 * We're in the 4.0 case (otherwise the SEQUENCE op would have
4748 * set cstate->clp), so session = false:
Jeff Layton4b24ca72014-06-30 11:48:44 -04004749 */
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004750 cstate->clp = lookup_clientid(clid, false, nn);
J. Bruce Fields7950b532021-01-21 17:57:41 -05004751 if (!cstate->clp)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004752 return nfserr_expired;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004753 return nfs_ok;
4754}
4755
Al Virob37ad282006-10-19 23:28:59 -07004756__be32
Andy Adamson66689582009-04-03 08:28:45 +03004757nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004758 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 clientid_t *clientid = &open->op_clientid;
4761 struct nfs4_client *clp = NULL;
4762 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004763 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004764 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765
J. Bruce Fields32513b42011-10-13 16:00:16 -04004766 /*
4767 * In case we need it later, after we've already created the
4768 * file and don't want to risk a further failure:
4769 */
4770 open->op_file = nfsd4_alloc_file();
4771 if (open->op_file == NULL)
4772 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004774 status = set_client(clientid, cstate, nn);
Trond Myklebust2d91e892014-06-30 11:48:46 -04004775 if (status)
4776 return status;
4777 clp = cstate->clp;
4778
Trond Myklebustd4f04892014-07-29 21:34:36 -04004779 strhashval = ownerstr_hashval(&open->op_owner);
4780 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004781 open->op_openowner = oo;
4782 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004783 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004785 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004786 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004787 release_openowner(oo);
4788 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004789 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004790 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004791 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4792 if (status)
4793 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004794 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004795new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004796 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004797 if (oo == NULL)
4798 return nfserr_jukebox;
4799 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004800alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004801 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004802 if (!open->op_stp)
4803 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004804
4805 if (nfsd4_has_session(cstate) &&
4806 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4807 open->op_odstate = alloc_clnt_odstate(clp);
4808 if (!open->op_odstate)
4809 return nfserr_jukebox;
4810 }
4811
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004812 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
Al Virob37ad282006-10-19 23:28:59 -07004815static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004816nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4817{
4818 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4819 return nfserr_openmode;
4820 else
4821 return nfs_ok;
4822}
4823
Daniel Mackc47d8322011-05-16 16:38:14 +02004824static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004825{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004826 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4827}
4828
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004829static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004830{
4831 struct nfs4_stid *ret;
4832
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004833 ret = find_stateid_by_type(cl, s,
4834 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004835 if (!ret)
4836 return NULL;
4837 return delegstateid(ret);
4838}
4839
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004840static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4841{
4842 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4843 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4844}
4845
Al Virob37ad282006-10-19 23:28:59 -07004846static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004847nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004848 struct nfs4_delegation **dp)
4849{
4850 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004851 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004852 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004853
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004854 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4855 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004856 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004857 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4858 nfs4_put_stid(&deleg->dl_stid);
4859 if (cl->cl_minorversion)
4860 status = nfserr_deleg_revoked;
4861 goto out;
4862 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004863 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004864 status = nfs4_check_delegmode(deleg, flags);
4865 if (status) {
4866 nfs4_put_stid(&deleg->dl_stid);
4867 goto out;
4868 }
4869 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004870out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004871 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004872 return nfs_ok;
4873 if (status)
4874 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004875 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004876 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004877}
4878
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004879static inline int nfs4_access_to_access(u32 nfs4_access)
4880{
4881 int flags = 0;
4882
4883 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4884 flags |= NFSD_MAY_READ;
4885 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4886 flags |= NFSD_MAY_WRITE;
4887 return flags;
4888}
4889
Al Virob37ad282006-10-19 23:28:59 -07004890static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4892 struct nfsd4_open *open)
4893{
4894 struct iattr iattr = {
4895 .ia_valid = ATTR_SIZE,
4896 .ia_size = 0,
4897 };
4898 if (!open->op_truncate)
4899 return 0;
4900 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004901 return nfserr_inval;
Arnd Bergmann2a1aa482019-11-03 17:50:18 +01004902 return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903}
4904
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004905static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004906 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4907 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004908{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004909 struct nfsd_file *nf = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004910 __be32 status;
4911 int oflag = nfs4_access_to_omode(open->op_share_access);
4912 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004913 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004914
Trond Myklebustde186432014-07-10 14:07:26 -04004915 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004916
4917 /*
4918 * Are we trying to set a deny mode that would conflict with
4919 * current access?
4920 */
4921 status = nfs4_file_check_deny(fp, open->op_share_deny);
4922 if (status != nfs_ok) {
4923 spin_unlock(&fp->fi_lock);
4924 goto out;
4925 }
4926
4927 /* set access to the file */
4928 status = nfs4_file_get_access(fp, open->op_share_access);
4929 if (status != nfs_ok) {
4930 spin_unlock(&fp->fi_lock);
4931 goto out;
4932 }
4933
4934 /* Set access bits in stateid */
4935 old_access_bmap = stp->st_access_bmap;
4936 set_access(open->op_share_access, stp);
4937
4938 /* Set new deny mask */
4939 old_deny_bmap = stp->st_deny_bmap;
4940 set_deny(open->op_share_deny, stp);
4941 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4942
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004943 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004944 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004945 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004946 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004947 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004948 spin_lock(&fp->fi_lock);
4949 if (!fp->fi_fds[oflag]) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004950 fp->fi_fds[oflag] = nf;
4951 nf = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -04004952 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004953 }
Trond Myklebustde186432014-07-10 14:07:26 -04004954 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004955 if (nf)
4956 nfsd_file_put(nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004957
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04004958 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
4959 access));
4960 if (status)
4961 goto out_put_access;
4962
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004963 status = nfsd4_truncate(rqstp, cur_fh, open);
4964 if (status)
4965 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004966out:
4967 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004968out_put_access:
4969 stp->st_access_bmap = old_access_bmap;
4970 nfs4_file_put_access(fp, open->op_share_access);
4971 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4972 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004973}
4974
Al Virob37ad282006-10-19 23:28:59 -07004975static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004976nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977{
Al Virob37ad282006-10-19 23:28:59 -07004978 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004979 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004981 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004982 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004983
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004984 /* test and set deny mode */
4985 spin_lock(&fp->fi_lock);
4986 status = nfs4_file_check_deny(fp, open->op_share_deny);
4987 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004988 set_deny(open->op_share_deny, stp);
4989 fp->fi_share_deny |=
4990 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4991 }
4992 spin_unlock(&fp->fi_lock);
4993
4994 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004997 status = nfsd4_truncate(rqstp, cur_fh, open);
4998 if (status != nfs_ok)
4999 reset_union_bmap_deny(old_deny_bmap, stp);
5000 return status;
5001}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
J. Bruce Fields14a24e92010-12-10 19:02:49 -05005003/* Should we give out recallable state?: */
5004static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5005{
5006 if (clp->cl_cb_state == NFSD4_CB_UP)
5007 return true;
5008 /*
5009 * In the sessions case, since we don't have to establish a
5010 * separate connection for callbacks, we assume it's OK
5011 * until we hear otherwise:
5012 */
5013 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5014}
5015
J. Bruce Fields653e5142018-02-15 22:08:45 -05005016static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5017 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005018{
5019 struct file_lock *fl;
5020
5021 fl = locks_alloc_lock();
5022 if (!fl)
5023 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005024 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04005025 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005026 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5027 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05005028 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005029 fl->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005030 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005031 return fl;
5032}
5033
J. Bruce Fieldsaba20722021-04-16 14:00:18 -04005034static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5035 struct nfs4_file *fp)
5036{
5037 struct nfs4_ol_stateid *st;
5038 struct file *f = fp->fi_deleg_file->nf_file;
5039 struct inode *ino = locks_inode(f);
5040 int writes;
5041
5042 writes = atomic_read(&ino->i_writecount);
5043 if (!writes)
5044 return 0;
5045 /*
5046 * There could be multiple filehandles (hence multiple
5047 * nfs4_files) referencing this file, but that's not too
5048 * common; let's just give up in that case rather than
5049 * trying to go look up all the clients using that other
5050 * nfs4_file as well:
5051 */
5052 if (fp->fi_aliased)
5053 return -EAGAIN;
5054 /*
5055 * If there's a close in progress, make sure that we see it
5056 * clear any fi_fds[] entries before we see it decrement
5057 * i_writecount:
5058 */
5059 smp_mb__after_atomic();
5060
5061 if (fp->fi_fds[O_WRONLY])
5062 writes--;
5063 if (fp->fi_fds[O_RDWR])
5064 writes--;
5065 if (writes > 0)
5066 return -EAGAIN; /* There may be non-NFSv4 writers */
5067 /*
5068 * It's possible there are non-NFSv4 write opens in progress,
5069 * but if they haven't incremented i_writecount yet then they
5070 * also haven't called break lease yet; so, they'll break this
5071 * lease soon enough. So, all that's left to check for is NFSv4
5072 * opens:
5073 */
5074 spin_lock(&fp->fi_lock);
5075 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5076 if (st->st_openstp == NULL /* it's an open */ &&
5077 access_permit_write(st) &&
5078 st->st_stid.sc_client != clp) {
5079 spin_unlock(&fp->fi_lock);
5080 return -EAGAIN;
5081 }
5082 }
5083 spin_unlock(&fp->fi_lock);
5084 /*
5085 * There's a small chance that we could be racing with another
5086 * NFSv4 open. However, any open that hasn't added itself to
5087 * the fi_stateids list also hasn't called break_lease yet; so,
5088 * they'll break this lease soon enough.
5089 */
5090 return 0;
5091}
5092
Jeff Layton0b266932014-07-25 07:34:25 -04005093static struct nfs4_delegation *
5094nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02005095 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05005096{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05005097 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04005098 struct nfs4_delegation *dp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005099 struct nfsd_file *nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005100 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04005101
J. Bruce Fields353601e2018-02-16 14:29:42 -05005102 /*
5103 * The fi_had_conflict and nfs_get_existing_delegation checks
5104 * here are just optimizations; we'll need to recheck them at
5105 * the end:
5106 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04005107 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04005108 return ERR_PTR(-EAGAIN);
5109
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005110 nf = find_readable_file(fp);
5111 if (!nf) {
J. Bruce Fieldsaba20722021-04-16 14:00:18 -04005112 /*
5113 * We probably could attempt another open and get a read
5114 * delegation, but for now, don't bother until the
5115 * client actually sends us one.
5116 */
5117 return ERR_PTR(-EAGAIN);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005118 }
Andrew Elble34ed9872015-10-15 12:07:28 -04005119 spin_lock(&state_lock);
5120 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05005121 if (nfs4_delegation_exists(clp, fp))
5122 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005123 else if (!fp->fi_deleg_file) {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005124 fp->fi_deleg_file = nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005125 /* increment early to prevent fi_deleg_file from being
5126 * cleared */
5127 fp->fi_delegees = 1;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005128 nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005129 } else
5130 fp->fi_delegees++;
5131 spin_unlock(&fp->fi_lock);
5132 spin_unlock(&state_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005133 if (nf)
5134 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005135 if (status)
5136 return ERR_PTR(status);
5137
5138 status = -ENOMEM;
5139 dp = alloc_init_deleg(clp, fp, fh, odstate);
5140 if (!dp)
5141 goto out_delegees;
5142
5143 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5144 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04005145 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005146
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005147 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005148 if (fl)
5149 locks_free_lock(fl);
5150 if (status)
5151 goto out_clnt_odstate;
J. Bruce Fieldsaba20722021-04-16 14:00:18 -04005152 status = nfsd4_check_conflicting_opens(clp, fp);
5153 if (status)
5154 goto out_unlock;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005155
5156 spin_lock(&state_lock);
5157 spin_lock(&fp->fi_lock);
5158 if (fp->fi_had_conflict)
5159 status = -EAGAIN;
5160 else
5161 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04005162 spin_unlock(&fp->fi_lock);
5163 spin_unlock(&state_lock);
5164
5165 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04005166 goto out_unlock;
5167
Jeff Layton0b266932014-07-25 07:34:25 -04005168 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04005169out_unlock:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005170 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005171out_clnt_odstate:
5172 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005173 nfs4_put_stid(&dp->dl_stid);
5174out_delegees:
5175 put_deleg_file(fp);
5176 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05005177}
5178
Benny Halevy4aa89132012-02-21 14:16:44 -08005179static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5180{
5181 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5182 if (status == -EAGAIN)
5183 open->op_why_no_deleg = WND4_CONTENTION;
5184 else {
5185 open->op_why_no_deleg = WND4_RESOURCE;
5186 switch (open->op_deleg_want) {
5187 case NFS4_SHARE_WANT_READ_DELEG:
5188 case NFS4_SHARE_WANT_WRITE_DELEG:
5189 case NFS4_SHARE_WANT_ANY_DELEG:
5190 break;
5191 case NFS4_SHARE_WANT_CANCEL:
5192 open->op_why_no_deleg = WND4_CANCELLED;
5193 break;
5194 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005195 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08005196 }
5197 }
5198}
5199
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200/*
5201 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04005202 *
5203 * Note we don't support write delegations, and won't until the vfs has
5204 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 */
5206static void
Jeff Layton4cf59222014-07-25 07:34:24 -04005207nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
5208 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209{
5210 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04005211 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5212 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05005213 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005214 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005216 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07005217 open->op_recall = 0;
5218 switch (open->op_claim_type) {
5219 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05005220 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07005221 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005222 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5223 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005224 break;
5225 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00005226 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005227 /*
5228 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005229 * had the chance to reclaim theirs, *and* until
5230 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005231 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005232 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005233 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005234 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005235 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005236 break;
5237 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005238 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005239 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005240 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04005241 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005242 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005244 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245
Hou Tao3caf9172020-08-28 15:02:55 +08005246 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
J. Bruce Fields99c41512013-05-21 16:21:25 -04005247 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04005248 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005249 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005250out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005251 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5252 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005253 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04005254 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005255 open->op_recall = 1;
5256 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04005257
5258 /* 4.1 client asking for a delegation? */
5259 if (open->op_deleg_want)
5260 nfsd4_open_deleg_none_ext(open, status);
5261 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262}
5263
Benny Halevye27f49c2012-02-21 14:16:54 -08005264static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5265 struct nfs4_delegation *dp)
5266{
5267 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5268 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5269 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5270 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5271 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5272 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5273 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5274 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5275 }
5276 /* Otherwise the client must be confused wanting a delegation
5277 * it already has, therefore we don't return
5278 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5279 */
5280}
5281
Al Virob37ad282006-10-19 23:28:59 -07005282__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5284{
Andy Adamson66689582009-04-03 08:28:45 +03005285 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005286 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005288 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07005289 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005290 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005291 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 /*
5294 * Lookup file; if found, lookup stateid and check open request,
5295 * and check for delegations in the process of being recalled.
5296 * If not found, create the nfs4_file struct
5297 */
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04005298 fp = find_or_add_file(open->op_file, current_fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04005299 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04005300 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07005301 if (status)
5302 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005303 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04005305 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07005306 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04005307 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07005308 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 }
5310
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005311 if (!stp) {
5312 stp = init_open_stateid(fp, open);
5313 if (!open->op_stp)
5314 new_stp = true;
5315 }
5316
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 /*
5318 * OPEN the file, or upgrade an existing OPEN.
5319 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005320 *
5321 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005323 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005325 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005326 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005327 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005331 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5332 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005333 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005334 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005335 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005336 goto out;
5337 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005338
5339 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5340 open->op_odstate);
5341 if (stp->st_clnt_odstate == open->op_odstate)
5342 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005344
Jeff Layton9767feb2015-10-01 09:05:50 -04005345 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005346 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347
Benny Halevyd24433c2012-02-16 20:57:17 +02005348 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005349 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5350 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5351 open->op_why_no_deleg = WND4_NOT_WANTED;
5352 goto nodeleg;
5353 }
5354 }
5355
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 /*
5357 * Attempt to hand out a delegation. No error return, because the
5358 * OPEN succeeds even if we fail.
5359 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005360 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005361nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 status = nfs_ok;
Hou Tao3caf9172020-08-28 15:02:55 +08005363 trace_nfsd_open(&stp->st_stid.sc_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005365 /* 4.1 client trying to upgrade/downgrade delegation? */
5366 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005367 open->op_deleg_want)
5368 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005369
NeilBrown13cd2182005-06-23 22:03:10 -07005370 if (fp)
5371 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005372 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005373 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 /*
5375 * To finish the open response, we just need to set the rflags.
5376 */
5377 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005378 if (nfsd4_has_session(&resp->cstate))
5379 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5380 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005382
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005383 if (dp)
5384 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005385 if (stp)
5386 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
5388 return status;
5389}
5390
Jeff Layton58fb12e2014-07-29 21:34:27 -04005391void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005392 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005393{
5394 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005395 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005396
Jeff Laytond3134b12014-07-29 21:34:32 -04005397 nfsd4_cstate_assign_replay(cstate, so);
5398 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005399 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005400 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005401 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005402 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005403 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005404 if (open->op_odstate)
5405 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005406}
5407
Al Virob37ad282006-10-19 23:28:59 -07005408__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005409nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005410 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005412 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005414 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005415 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005417 trace_nfsd_clid_renew(clid);
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005418 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005419 if (status)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005420 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005421 clp = cstate->clp;
NeilBrownea1da632005-06-23 22:04:17 -07005422 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005423 && clp->cl_cb_state != NFSD4_CB_UP)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005424 return nfserr_cb_path_down;
5425 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426}
5427
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005428void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005429nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005430{
Jeff Layton33dcc482012-04-10 11:08:48 -04005431 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005432 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005433 return;
5434
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005435 trace_nfsd_grace_complete(nn);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005436 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005437 /*
5438 * If the server goes down again right now, an NFSv4
5439 * client will still be allowed to reclaim after it comes back up,
5440 * even if it hasn't yet had a chance to reclaim state this time.
5441 *
5442 */
Jeff Layton919b8042014-09-12 16:40:20 -04005443 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005444 /*
5445 * At this point, NFSv4 clients can still reclaim. But if the
5446 * server crashes, any that have not yet reclaimed will be out
5447 * of luck on the next boot.
5448 *
5449 * (NFSv4.1+ clients are considered to have reclaimed once they
5450 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5451 * have reclaimed after their first OPEN.)
5452 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005453 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005454 /*
5455 * At this point, and once lockd and/or any other containers
5456 * exit their grace period, further reclaims will fail and
5457 * regular locking can resume.
5458 */
NeilBrowna76b4312005-06-23 22:04:01 -07005459}
5460
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005461/*
5462 * If we've waited a lease period but there are still clients trying to
5463 * reclaim, wait a little longer to give them a chance to finish.
5464 */
5465static bool clients_still_reclaiming(struct nfsd_net *nn)
5466{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005467 time64_t double_grace_period_end = nn->boot_time +
5468 2 * nn->nfsd4_lease;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005469
Scott Mayhew362063a2019-03-26 18:06:28 -04005470 if (nn->track_reclaim_completes &&
5471 atomic_read(&nn->nr_reclaim_complete) ==
5472 nn->reclaim_str_hashtbl_size)
5473 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005474 if (!nn->somebody_reclaimed)
5475 return false;
5476 nn->somebody_reclaimed = false;
5477 /*
5478 * If we've given them *two* lease times to reclaim, and they're
5479 * still not done, give up:
5480 */
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005481 if (ktime_get_boottime_seconds() > double_grace_period_end)
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005482 return false;
5483 return true;
5484}
5485
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005486struct laundry_time {
5487 time64_t cutoff;
5488 time64_t new_timeo;
5489};
5490
5491static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5492{
5493 time64_t time_remaining;
5494
5495 if (last_refresh < lt->cutoff)
5496 return true;
5497 time_remaining = last_refresh - lt->cutoff;
5498 lt->new_timeo = min(lt->new_timeo, time_remaining);
5499 return false;
5500}
5501
Dai Ngof4e44b32021-05-21 15:09:37 -04005502#ifdef CONFIG_NFSD_V4_2_INTER_SSC
5503void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
5504{
5505 spin_lock_init(&nn->nfsd_ssc_lock);
5506 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
5507 init_waitqueue_head(&nn->nfsd_ssc_waitq);
5508}
5509EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
5510
5511/*
5512 * This is called when nfsd is being shutdown, after all inter_ssc
5513 * cleanup were done, to destroy the ssc delayed unmount list.
5514 */
5515static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
5516{
Dai Ngof47dc2d2021-06-03 20:02:26 -04005517 struct nfsd4_ssc_umount_item *ni = NULL;
Dai Ngof4e44b32021-05-21 15:09:37 -04005518 struct nfsd4_ssc_umount_item *tmp;
5519
5520 spin_lock(&nn->nfsd_ssc_lock);
5521 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5522 list_del(&ni->nsui_list);
5523 spin_unlock(&nn->nfsd_ssc_lock);
5524 mntput(ni->nsui_vfsmount);
5525 kfree(ni);
5526 spin_lock(&nn->nfsd_ssc_lock);
5527 }
5528 spin_unlock(&nn->nfsd_ssc_lock);
5529}
5530
5531static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
5532{
5533 bool do_wakeup = false;
5534 struct nfsd4_ssc_umount_item *ni = 0;
5535 struct nfsd4_ssc_umount_item *tmp;
5536
5537 spin_lock(&nn->nfsd_ssc_lock);
5538 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5539 if (time_after(jiffies, ni->nsui_expire)) {
5540 if (refcount_read(&ni->nsui_refcnt) > 1)
5541 continue;
5542
5543 /* mark being unmount */
5544 ni->nsui_busy = true;
5545 spin_unlock(&nn->nfsd_ssc_lock);
5546 mntput(ni->nsui_vfsmount);
5547 spin_lock(&nn->nfsd_ssc_lock);
5548
5549 /* waiters need to start from begin of list */
5550 list_del(&ni->nsui_list);
5551 kfree(ni);
5552
5553 /* wakeup ssc_connect waiters */
5554 do_wakeup = true;
5555 continue;
5556 }
5557 break;
5558 }
5559 if (do_wakeup)
5560 wake_up_all(&nn->nfsd_ssc_waitq);
5561 spin_unlock(&nn->nfsd_ssc_lock);
5562}
5563#endif
5564
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005565static time64_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005566nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567{
5568 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005569 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005571 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005572 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 struct list_head *pos, *next, reaplist;
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005574 struct laundry_time lt = {
5575 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
5576 .new_timeo = nn->nfsd4_lease
5577 };
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005578 struct nfs4_cpntf_state *cps;
5579 copy_stateid_t *cps_t;
5580 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005582 if (clients_still_reclaiming(nn)) {
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005583 lt.new_timeo = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005584 goto out;
5585 }
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005586 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005587 INIT_LIST_HEAD(&reaplist);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005588
5589 spin_lock(&nn->s2s_cp_lock);
5590 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5591 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5592 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005593 state_expired(&lt, cps->cpntf_time))
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005594 _free_cpntf_state_locked(nn, cps);
5595 }
5596 spin_unlock(&nn->s2s_cp_lock);
5597
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005598 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005599 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 clp = list_entry(pos, struct nfs4_client, cl_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005601 if (!state_expired(&lt, clp->cl_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 break;
Chuck Lever2958d2e2021-05-14 15:56:06 -04005603 if (mark_client_expired_locked(clp))
Benny Halevyd7682982010-05-12 00:13:54 +03005604 continue;
Trond Myklebust4864af92014-07-30 08:27:03 -04005605 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005606 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005607 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005608 list_for_each_safe(pos, next, &reaplist) {
5609 clp = list_entry(pos, struct nfs4_client, cl_lru);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005610 trace_nfsd_clid_purged(&clp->cl_clientid);
Trond Myklebust4864af92014-07-30 08:27:03 -04005611 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 expire_client(clp);
5613 }
Benny Halevycdc975052014-05-30 09:09:30 -04005614 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005615 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005617 if (!state_expired(&lt, dp->dl_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 break;
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005619 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005620 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 }
Benny Halevycdc975052014-05-30 09:09:30 -04005622 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005623 while (!list_empty(&reaplist)) {
5624 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5625 dl_recall_lru);
5626 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005627 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 }
Jeff Layton217526e2014-07-30 08:27:11 -04005629
5630 spin_lock(&nn->client_lock);
5631 while (!list_empty(&nn->close_lru)) {
5632 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5633 oo_close_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005634 if (!state_expired(&lt, oo->oo_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 break;
Jeff Layton217526e2014-07-30 08:27:11 -04005636 list_del_init(&oo->oo_close_lru);
5637 stp = oo->oo_last_closed_stid;
5638 oo->oo_last_closed_stid = NULL;
5639 spin_unlock(&nn->client_lock);
5640 nfs4_put_stid(&stp->st_stid);
5641 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 }
Jeff Layton217526e2014-07-30 08:27:11 -04005643 spin_unlock(&nn->client_lock);
5644
Jeff Layton7919d0a2016-09-16 16:28:25 -04005645 /*
5646 * It's possible for a client to try and acquire an already held lock
5647 * that is being held for a long time, and then lose interest in it.
5648 * So, we clean out any un-revisited request after a lease period
5649 * under the assumption that the client is no longer interested.
5650 *
5651 * RFC5661, sec. 9.6 states that the client must not rely on getting
5652 * notifications and must continue to poll for locks, even when the
5653 * server supports them. Thus this shouldn't lead to clients blocking
5654 * indefinitely once the lock does become free.
5655 */
5656 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005657 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005658 while (!list_empty(&nn->blocked_locks_lru)) {
5659 nbl = list_first_entry(&nn->blocked_locks_lru,
5660 struct nfsd4_blocked_lock, nbl_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005661 if (!state_expired(&lt, nbl->nbl_time))
Jeff Layton7919d0a2016-09-16 16:28:25 -04005662 break;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005663 list_move(&nbl->nbl_lru, &reaplist);
5664 list_del_init(&nbl->nbl_list);
5665 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005666 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005667
5668 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005669 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005670 struct nfsd4_blocked_lock, nbl_lru);
5671 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005672 free_blocked_lock(nbl);
5673 }
Dai Ngof4e44b32021-05-21 15:09:37 -04005674#ifdef CONFIG_NFSD_V4_2_INTER_SSC
5675 /* service the server-to-server copy delayed unmount list */
5676 nfsd4_ssc_expire_umount(nn);
5677#endif
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005678out:
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005679 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680}
5681
Harvey Harrisona254b242008-02-20 12:49:00 -08005682static struct workqueue_struct *laundry_wq;
5683static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005684
5685static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005686laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005688 time64_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005689 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005690 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5691 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005693 t = nfs4_laundromat(nn);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005694 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695}
5696
Jeff Layton8fcd4612015-07-30 06:57:46 -04005697static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005698{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005699 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005700 return nfserr_bad_stateid;
5701 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702}
5703
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005705__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706{
Al Virob37ad282006-10-19 23:28:59 -07005707 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
J. Bruce Fields02921912010-07-29 15:16:59 -04005709 /* For lock stateid's, we test the parent open, not the lock: */
5710 if (stp->st_openstp)
5711 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005712 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005714 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 goto out;
5716 status = nfs_ok;
5717out:
5718 return status;
5719}
5720
Al Virob37ad282006-10-19 23:28:59 -07005721static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005722check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005724 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005726 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005727 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 * conflicting state; so we must wait out the grace period. */
5729 return nfserr_grace;
5730 } else if (flags & WR_STATE)
5731 return nfs4_share_conflict(current_fh,
5732 NFS4_SHARE_DENY_WRITE);
5733 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5734 return nfs4_share_conflict(current_fh,
5735 NFS4_SHARE_DENY_READ);
5736}
5737
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005738static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005739{
Andy Adamson66689582009-04-03 08:28:45 +03005740 /*
5741 * When sessions are used the stateid generation number is ignored
5742 * when it is zero.
5743 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005744 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005745 return nfs_ok;
5746
5747 if (in->si_generation == ref->si_generation)
5748 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005749
J. Bruce Fields0836f582008-01-26 19:08:12 -05005750 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005751 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005752 return nfserr_bad_stateid;
5753 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005754 * However, we could see a stateid from the past, even from a
5755 * non-buggy client. For example, if the client sends a lock
5756 * while some IO is outstanding, the lock may bump si_generation
5757 * while the IO is still in flight. The client could avoid that
5758 * situation by waiting for responses on all the IO requests,
5759 * but better performance may result in retrying IO that
5760 * receives an old_stateid error if requests are rarely
5761 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005762 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005763 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005764}
5765
Trond Myklebust03da3162017-11-03 08:00:16 -04005766static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5767{
5768 __be32 ret;
5769
5770 spin_lock(&s->sc_lock);
5771 ret = nfsd4_verify_open_stid(s);
5772 if (ret == nfs_ok)
5773 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5774 spin_unlock(&s->sc_lock);
5775 return ret;
5776}
5777
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005778static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5779{
5780 if (ols->st_stateowner->so_is_open_owner &&
5781 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5782 return nfserr_bad_stateid;
5783 return nfs_ok;
5784}
5785
Chuck Lever7df302f2012-05-29 13:56:37 -04005786static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005787{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005788 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005789 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005790
Andrew Elbleae254dac2017-11-09 13:41:10 -05005791 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5792 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005793 return status;
J. Bruce Fields663e36f2020-03-19 10:18:49 -04005794 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005795 return status;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005796 spin_lock(&cl->cl_lock);
5797 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005798 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005799 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005800 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005801 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005802 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005803 switch (s->sc_type) {
5804 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005805 status = nfs_ok;
5806 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005807 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005808 status = nfserr_deleg_revoked;
5809 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005810 case NFS4_OPEN_STID:
5811 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005812 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005813 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005814 default:
5815 printk("unknown stateid type %x\n", s->sc_type);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05005816 fallthrough;
J. Bruce Fields23340032013-04-09 17:42:28 -04005817 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005818 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005819 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005820 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005821out_unlock:
5822 spin_unlock(&cl->cl_lock);
5823 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005824}
5825
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005826__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005827nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5828 stateid_t *stateid, unsigned char typemask,
5829 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005830{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005831 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005832 bool return_revoked = false;
5833
5834 /*
5835 * only return revoked delegations if explicitly asked.
5836 * otherwise we report revoked or bad_stateid status.
5837 */
5838 if (typemask & NFS4_REVOKED_DELEG_STID)
5839 return_revoked = true;
5840 else if (typemask & NFS4_DELEG_STID)
5841 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005842
Andrew Elbleae254dac2017-11-09 13:41:10 -05005843 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5844 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005845 return nfserr_bad_stateid;
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005846 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005847 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005848 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005849 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005850 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005851 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005852 if (status)
5853 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005854 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005855 if (!*s)
5856 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005857 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5858 nfs4_put_stid(*s);
5859 if (cstate->minorversion)
5860 return nfserr_deleg_revoked;
5861 return nfserr_bad_stateid;
5862 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005863 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005864}
5865
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005866static struct nfsd_file *
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005867nfs4_find_file(struct nfs4_stid *s, int flags)
5868{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005869 if (!s)
5870 return NULL;
5871
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005872 switch (s->sc_type) {
5873 case NFS4_DELEG_STID:
5874 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5875 return NULL;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005876 return nfsd_file_get(s->sc_file->fi_deleg_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005877 case NFS4_OPEN_STID:
5878 case NFS4_LOCK_STID:
5879 if (flags & RD_STATE)
5880 return find_readable_file(s->sc_file);
5881 else
5882 return find_writeable_file(s->sc_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005883 }
5884
5885 return NULL;
5886}
5887
5888static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005889nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005890{
5891 __be32 status;
5892
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005893 status = nfsd4_check_openowner_confirmed(ols);
5894 if (status)
5895 return status;
5896 return nfs4_check_openmode(ols, flags);
5897}
5898
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005899static __be32
5900nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
Jeff Layton5c4583b2019-08-18 14:18:54 -04005901 struct nfsd_file **nfp, int flags)
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005902{
5903 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005904 struct nfsd_file *nf;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005905 __be32 status;
5906
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005907 nf = nfs4_find_file(s, flags);
5908 if (nf) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005909 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5910 acc | NFSD_MAY_OWNER_OVERRIDE);
Jeff Layton5c4583b2019-08-18 14:18:54 -04005911 if (status) {
5912 nfsd_file_put(nf);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005913 goto out;
Jeff Layton5c4583b2019-08-18 14:18:54 -04005914 }
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005915 } else {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005916 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005917 if (status)
5918 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005919 }
Jeff Layton5c4583b2019-08-18 14:18:54 -04005920 *nfp = nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005921out:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005922 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005923}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005924static void
5925_free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5926{
5927 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5928 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5929 return;
5930 list_del(&cps->cp_list);
5931 idr_remove(&nn->s2s_cp_stateids,
5932 cps->cp_stateid.stid.si_opaque.so_id);
5933 kfree(cps);
5934}
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005935/*
5936 * A READ from an inter server to server COPY will have a
5937 * copy stateid. Look up the copy notify stateid from the
5938 * idr structure and take a reference on it.
5939 */
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005940__be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5941 struct nfs4_client *clp,
5942 struct nfs4_cpntf_state **cps)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005943{
5944 copy_stateid_t *cps_t;
5945 struct nfs4_cpntf_state *state = NULL;
5946
5947 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5948 return nfserr_bad_stateid;
5949 spin_lock(&nn->s2s_cp_lock);
5950 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5951 if (cps_t) {
5952 state = container_of(cps_t, struct nfs4_cpntf_state,
5953 cp_stateid);
Dan Carpenter5277a792019-12-04 10:59:36 +03005954 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5955 state = NULL;
5956 goto unlock;
5957 }
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005958 if (!clp)
5959 refcount_inc(&state->cp_stateid.sc_count);
5960 else
5961 _free_cpntf_state_locked(nn, state);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005962 }
Dan Carpenter5277a792019-12-04 10:59:36 +03005963unlock:
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005964 spin_unlock(&nn->s2s_cp_lock);
5965 if (!state)
5966 return nfserr_bad_stateid;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005967 if (!clp && state)
5968 *cps = state;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005969 return 0;
5970}
5971
5972static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5973 struct nfs4_stid **stid)
5974{
5975 __be32 status;
5976 struct nfs4_cpntf_state *cps = NULL;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005977 struct nfs4_client *found;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005978
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005979 status = manage_cpntf_state(nn, st, NULL, &cps);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005980 if (status)
5981 return status;
5982
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005983 cps->cpntf_time = ktime_get_boottime_seconds();
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005984
5985 status = nfserr_expired;
5986 found = lookup_clientid(&cps->cp_p_clid, true, nn);
5987 if (!found)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005988 goto out;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005989
5990 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
5991 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
5992 if (*stid)
5993 status = nfs_ok;
5994 else
5995 status = nfserr_bad_stateid;
5996
5997 put_client_renew(found);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005998out:
5999 nfs4_put_cpntf_state(nn, cps);
6000 return status;
6001}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006002
6003void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6004{
6005 spin_lock(&nn->s2s_cp_lock);
6006 _free_cpntf_state_locked(nn, cps);
6007 spin_unlock(&nn->s2s_cp_lock);
6008}
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006011 * Checks for stateid operations
6012 */
Al Virob37ad282006-10-19 23:28:59 -07006013__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006014nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01006015 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006016 stateid_t *stateid, int flags, struct nfsd_file **nfp,
6017 struct nfs4_stid **cstid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006019 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006020 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006021 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006022 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023
Jeff Layton5c4583b2019-08-18 14:18:54 -04006024 if (nfp)
6025 *nfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006027 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
6028 status = check_special_stateids(net, fhp, stateid, flags);
6029 goto done;
6030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006032 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04006033 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006034 &s, nn);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006035 if (status == nfserr_bad_stateid)
6036 status = find_cpntf_state(nn, stateid, &s);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006037 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04006038 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04006039 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006040 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04006041 if (status)
6042 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006043
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006044 switch (s->sc_type) {
6045 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006046 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006047 break;
6048 case NFS4_OPEN_STID:
6049 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05006050 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006051 break;
6052 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04006053 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006054 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04006056 if (status)
6057 goto out;
6058 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006059
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006060done:
Jeff Layton5c4583b2019-08-18 14:18:54 -04006061 if (status == nfs_ok && nfp)
6062 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063out:
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006064 if (s) {
6065 if (!status && cstid)
6066 *cstid = s;
6067 else
6068 nfs4_put_stid(s);
6069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 return status;
6071}
6072
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006073/*
Bryan Schumaker17456802011-07-13 10:50:48 -04006074 * Test if the stateid is valid
6075 */
6076__be32
6077nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006078 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04006079{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006080 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05006081 struct nfsd4_test_stateid_id *stateid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006082 struct nfs4_client *cl = cstate->clp;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05006083
Bryan Schumaker03cfb422012-01-27 10:22:49 -05006084 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04006085 stateid->ts_id_status =
6086 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05006087
Bryan Schumaker17456802011-07-13 10:50:48 -04006088 return nfs_ok;
6089}
6090
Chuck Lever42691392016-08-11 10:37:30 -04006091static __be32
6092nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
6093{
6094 struct nfs4_ol_stateid *stp = openlockstateid(s);
6095 __be32 ret;
6096
Trond Myklebust659aefb2017-11-03 08:00:13 -04006097 ret = nfsd4_lock_ol_stateid(stp);
6098 if (ret)
6099 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04006100
6101 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6102 if (ret)
6103 goto out;
6104
6105 ret = nfserr_locks_held;
6106 if (check_for_locks(stp->st_stid.sc_file,
6107 lockowner(stp->st_stateowner)))
6108 goto out;
6109
6110 release_lock_stateid(stp);
6111 ret = nfs_ok;
6112
6113out:
6114 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04006115out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04006116 nfs4_put_stid(s);
6117 return ret;
6118}
6119
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006120__be32
6121nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006122 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006123{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006124 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006125 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006126 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006127 struct nfs4_delegation *dp;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006128 struct nfs4_client *cl = cstate->clp;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006129 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006130
Jeff Layton1af71cc2014-07-29 21:34:14 -04006131 spin_lock(&cl->cl_lock);
6132 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006133 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04006134 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04006135 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006136 switch (s->sc_type) {
6137 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006138 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04006139 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006140 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04006141 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6142 if (ret)
6143 break;
6144 ret = nfserr_locks_held;
6145 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006146 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04006147 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006148 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006149 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04006150 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006151 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006152 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04006153 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006154 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04006155 list_del_init(&dp->dl_recall_lru);
6156 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04006157 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006158 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04006159 goto out;
6160 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006161 }
Trond Myklebust03da3162017-11-03 08:00:16 -04006162 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006163out_unlock:
6164 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006165out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006166 return ret;
6167}
6168
NeilBrown4c4cd222005-07-07 17:59:27 -07006169static inline int
6170setlkflg (int type)
6171{
6172 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6173 RD_STATE : WR_STATE;
6174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006176static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006177{
6178 struct svc_fh *current_fh = &cstate->current_fh;
6179 struct nfs4_stateowner *sop = stp->st_stateowner;
6180 __be32 status;
6181
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006182 status = nfsd4_check_seqid(cstate, sop, seqid);
6183 if (status)
6184 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04006185 status = nfsd4_lock_ol_stateid(stp);
6186 if (status != nfs_ok)
6187 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006188 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04006189 if (status == nfs_ok)
6190 status = nfs4_check_fh(current_fh, &stp->st_stid);
6191 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006192 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04006193 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006194}
6195
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196/*
6197 * Checks for sequence id mutating operations.
6198 */
Al Virob37ad282006-10-19 23:28:59 -07006199static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03006200nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04006201 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006202 struct nfs4_ol_stateid **stpp,
6203 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204{
J. Bruce Fields0836f582008-01-26 19:08:12 -05006205 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006206 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006207 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006209 trace_nfsd_preprocess(seqid, stateid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006210
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006212 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006213 if (status)
6214 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006215 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04006216 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006218 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006219 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006220 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04006221 else
6222 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006223 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006224}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05006225
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006226static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6227 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006228{
6229 __be32 status;
6230 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006231 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006233 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006234 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04006235 if (status)
6236 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006237 oo = openowner(stp->st_stateowner);
6238 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006239 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006240 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006241 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006242 }
6243 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07006244 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245}
6246
Al Virob37ad282006-10-19 23:28:59 -07006247__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006248nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006249 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006251 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07006252 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006253 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006254 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006255 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256
Al Viroa6a9f182013-09-16 10:57:01 -04006257 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6258 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006260 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07006261 if (status)
6262 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006264 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006265 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006266 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006267 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006268 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006269 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006270 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006271 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006272 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04006273 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006274 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04006275 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04006276 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006277 mutex_unlock(&stp->st_mutex);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006278 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
Jeff Layton2a4317c2012-03-21 16:42:43 -04006279 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006280 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04006281put_stateid:
6282 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006284 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 return status;
6286}
6287
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006288static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289{
Jeff Layton82c5ff12012-05-11 09:45:13 -04006290 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006291 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04006292 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006293 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006294}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04006295
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006296static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6297{
6298 switch (to_access) {
6299 case NFS4_SHARE_ACCESS_READ:
6300 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6301 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6302 break;
6303 case NFS4_SHARE_ACCESS_WRITE:
6304 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6305 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6306 break;
6307 case NFS4_SHARE_ACCESS_BOTH:
6308 break;
6309 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006310 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 }
6312}
6313
Al Virob37ad282006-10-19 23:28:59 -07006314__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006315nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006316 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006318 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07006319 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006320 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006321 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322
Al Viroa6a9f182013-09-16 10:57:01 -04006323 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6324 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04006326 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02006327 if (od->od_deleg_want)
6328 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6329 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006331 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006332 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006333 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04006336 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006337 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006339 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04006341 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006342 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006344 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006346 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04006347 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04006348 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006350put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006351 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006352 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006354 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 return status;
6356}
6357
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006358static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6359{
Trond Myklebustacf92952014-06-30 11:48:37 -04006360 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04006361 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04006362 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006363
Jeff Layton2c41beb2014-07-29 21:34:41 -04006364 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04006365 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006366
Jeff Laytond83017f2014-07-29 21:34:42 -04006367 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04006368 if (unhashed)
6369 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04006370 spin_unlock(&clp->cl_lock);
6371 free_ol_stateid_reaplist(&reaplist);
6372 } else {
6373 spin_unlock(&clp->cl_lock);
6374 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04006375 if (unhashed)
6376 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04006377 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04006378}
6379
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380/*
6381 * nfs4_unlock_state() called after encode
6382 */
Al Virob37ad282006-10-19 23:28:59 -07006383__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006384nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006385 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006387 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07006388 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006389 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006390 struct net *net = SVC_NET(rqstp);
6391 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
Al Viroa6a9f182013-09-16 10:57:01 -04006393 dprintk("NFSD: nfsd4_close on file %pd\n",
6394 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006396 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6397 &close->cl_stateid,
6398 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006399 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006400 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006401 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006403
6404 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006405
6406 /*
6407 * Technically we don't _really_ have to increment or copy it, since
6408 * it should just be gone after this operation and we clobber the
6409 * copied value below, but we continue to do so here just to ensure
6410 * that racing ops see that there was a state change.
6411 */
Jeff Layton9767feb2015-10-01 09:05:50 -04006412 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006414 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006415 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006416
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006417 /* v4.1+ suggests that we send a special stateid in here, since the
6418 * clients should just ignore this anyway. Since this is not useful
6419 * for v4.0 clients either, we set it to the special close_stateid
6420 * universally.
6421 *
6422 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6423 */
6424 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04006425
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006426 /* put reference from nfs4_preprocess_seqid_op */
6427 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 return status;
6430}
6431
Al Virob37ad282006-10-19 23:28:59 -07006432__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006433nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006434 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006436 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006437 struct nfs4_delegation *dp;
6438 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006439 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07006440 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006441 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006443 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006444 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006446 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006447 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006448 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006449 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04006450 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006451 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04006452 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006453
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006454 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006455put_stateid:
6456 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457out:
6458 return status;
6459}
6460
Benny Halevy87df4de2008-12-15 19:42:03 +02006461/* last octet in a range */
6462static inline u64
6463last_byte_offset(u64 start, u64 len)
6464{
6465 u64 end;
6466
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006467 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006468 end = start + len;
6469 return end > start ? end - 1: NFS4_MAX_UINT64;
6470}
6471
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472/*
6473 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6474 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6475 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6476 * locking, this prevents us from being completely protocol-compliant. The
6477 * real solution to this problem is to start using unsigned file offsets in
6478 * the VFS, but this is a very deep change!
6479 */
6480static inline void
6481nfs4_transform_lock_offset(struct file_lock *lock)
6482{
6483 if (lock->fl_start < 0)
6484 lock->fl_start = OFFSET_MAX;
6485 if (lock->fl_end < 0)
6486 lock->fl_end = OFFSET_MAX;
6487}
6488
Jeff Laytoncae80b32015-04-03 09:04:04 -04006489static fl_owner_t
6490nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006491{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006492 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6493
6494 nfs4_get_stateowner(&lo->lo_owner);
6495 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006496}
6497
Jeff Laytoncae80b32015-04-03 09:04:04 -04006498static void
6499nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006500{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006501 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006502
Jeff Laytoncae80b32015-04-03 09:04:04 -04006503 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006504 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006505}
6506
Jeff Layton76d348f2016-09-16 16:28:24 -04006507static void
6508nfsd4_lm_notify(struct file_lock *fl)
6509{
6510 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6511 struct net *net = lo->lo_owner.so_client->net;
6512 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6513 struct nfsd4_blocked_lock *nbl = container_of(fl,
6514 struct nfsd4_blocked_lock, nbl_lock);
6515 bool queue = false;
6516
Jeff Layton7919d0a2016-09-16 16:28:25 -04006517 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006518 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006519 if (!list_empty(&nbl->nbl_list)) {
6520 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006521 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006522 queue = true;
6523 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006524 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006525
Chuck Lever2cde7f82021-05-14 15:57:08 -04006526 if (queue) {
6527 trace_nfsd_cb_notify_lock(lo, nbl);
Jeff Layton76d348f2016-09-16 16:28:24 -04006528 nfsd4_run_cb(&nbl->nbl_cb);
Chuck Lever2cde7f82021-05-14 15:57:08 -04006529 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006530}
6531
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006532static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006533 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006534 .lm_get_owner = nfsd4_fl_get_owner,
6535 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006536};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537
6538static inline void
6539nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6540{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006541 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542
NeilBrownd5b90262006-04-10 22:55:22 -07006543 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006544 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04006545 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6546 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006547 if (!deny->ld_owner.data)
6548 /* We just don't care that much */
6549 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006550 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006551 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006552nevermind:
6553 deny->ld_owner.len = 0;
6554 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006555 deny->ld_clientid.cl_boot = 0;
6556 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 }
6558 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006559 deny->ld_length = NFS4_MAX_UINT64;
6560 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6562 deny->ld_type = NFS4_READ_LT;
6563 if (fl->fl_type != F_RDLCK)
6564 deny->ld_type = NFS4_WRITE_LT;
6565}
6566
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006567static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006568find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006570 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006571 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572
Trond Myklebust0a880a22014-07-30 08:27:10 -04006573 lockdep_assert_held(&clp->cl_lock);
6574
Trond Myklebustd4f04892014-07-29 21:34:36 -04006575 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6576 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006577 if (so->so_is_open_owner)
6578 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006579 if (same_owner_str(so, owner))
6580 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 }
6582 return NULL;
6583}
6584
Trond Myklebustc58c6612014-07-29 21:34:35 -04006585static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006586find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006587{
6588 struct nfs4_lockowner *lo;
6589
Trond Myklebustd4f04892014-07-29 21:34:36 -04006590 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006591 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006592 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006593 return lo;
6594}
6595
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006596static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6597{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006598 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006599}
6600
Jeff Layton6b180f02014-07-29 21:34:26 -04006601static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6602{
6603 struct nfs4_lockowner *lo = lockowner(sop);
6604
6605 kmem_cache_free(lockowner_slab, lo);
6606}
6607
6608static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006609 .so_unhash = nfs4_unhash_lockowner,
6610 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006611};
6612
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613/*
6614 * Alloc a lock owner structure.
6615 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006616 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006618 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006620static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006621alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6622 struct nfs4_ol_stateid *open_stp,
6623 struct nfsd4_lock *lock)
6624{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006625 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006627 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6628 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006630 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006631 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6632 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006633 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006634 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006635 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006636 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006637 if (ret == NULL) {
6638 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006639 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006640 ret = lo;
6641 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006642 nfs4_free_stateowner(&lo->lo_owner);
6643
Trond Myklebustd4f04892014-07-29 21:34:36 -04006644 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006645 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646}
6647
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006648static struct nfs4_ol_stateid *
Trond Myklebusta451b122020-03-01 18:21:38 -05006649find_lock_stateid(const struct nfs4_lockowner *lo,
6650 const struct nfs4_ol_stateid *ost)
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006651{
6652 struct nfs4_ol_stateid *lst;
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006653
Trond Myklebusta451b122020-03-01 18:21:38 -05006654 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006655
Trond Myklebusta451b122020-03-01 18:21:38 -05006656 /* If ost is not hashed, ost->st_locks will not be valid */
6657 if (!nfs4_ol_stateid_unhashed(ost))
6658 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6659 if (lst->st_stateowner == &lo->lo_owner) {
6660 refcount_inc(&lst->st_stid.sc_count);
6661 return lst;
6662 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006663 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006664 return NULL;
6665}
6666
Trond Myklebustbeeca192017-11-03 08:00:14 -04006667static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006668init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6669 struct nfs4_file *fp, struct inode *inode,
6670 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006672 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006673 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674
Trond Myklebustbeeca192017-11-03 08:00:14 -04006675 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006676 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006677retry:
6678 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006679 if (nfs4_ol_stateid_unhashed(open_stp))
6680 goto out_close;
6681 retstp = find_lock_stateid(lo, open_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006682 if (retstp)
Trond Myklebusta451b122020-03-01 18:21:38 -05006683 goto out_found;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006684 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006685 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006686 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006687 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006688 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006689 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006691 stp->st_openstp = open_stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006692 spin_lock(&fp->fi_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006693 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006694 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006695 list_add(&stp->st_perfile, &fp->fi_stateids);
6696 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006697 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006698 return stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006699out_found:
6700 spin_unlock(&clp->cl_lock);
6701 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6702 nfs4_put_stid(&retstp->st_stid);
6703 goto retry;
6704 }
6705 /* To keep mutex tracking happy */
6706 mutex_unlock(&stp->st_mutex);
6707 return retstp;
6708out_close:
6709 spin_unlock(&clp->cl_lock);
6710 mutex_unlock(&stp->st_mutex);
6711 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712}
6713
Jeff Laytonc53530d2014-06-30 11:48:39 -04006714static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006715find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6716 struct inode *inode, struct nfs4_ol_stateid *ost,
6717 bool *new)
6718{
6719 struct nfs4_stid *ns = NULL;
6720 struct nfs4_ol_stateid *lst;
6721 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6722 struct nfs4_client *clp = oo->oo_owner.so_client;
6723
Trond Myklebustbeeca192017-11-03 08:00:14 -04006724 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006725 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006726 lst = find_lock_stateid(lo, ost);
Jeff Layton356a95e2014-07-29 21:34:13 -04006727 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006728 if (lst != NULL) {
6729 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6730 goto out;
6731 nfs4_put_stid(&lst->st_stid);
6732 }
6733 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6734 if (ns == NULL)
6735 return NULL;
6736
6737 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6738 if (lst == openlockstateid(ns))
6739 *new = true;
6740 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006741 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006742out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006743 return lst;
6744}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006745
NeilBrownfd39ca92005-06-23 22:04:03 -07006746static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747check_lock_length(u64 offset, u64 length)
6748{
Kinglong Meee7969312015-07-13 17:34:19 +08006749 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6750 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751}
6752
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006753static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006754{
Trond Myklebust11b91642014-07-29 21:34:08 -04006755 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006756
Jeff Layton7214e862014-07-10 14:07:33 -04006757 lockdep_assert_held(&fp->fi_lock);
6758
Jeff Layton82c5ff12012-05-11 09:45:13 -04006759 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006760 return;
Jeff Layton12659652014-07-10 14:07:28 -04006761 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006762 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006763}
6764
Jeff Layton356a95e2014-07-29 21:34:13 -04006765static __be32
6766lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6767 struct nfs4_ol_stateid *ost,
6768 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006769 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006770{
Jeff Layton5db1c032014-07-29 21:34:28 -04006771 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006772 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006773 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6774 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006775 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006776 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006777 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006778 unsigned int strhashval;
6779
Kinglong Meec8623992015-07-13 17:35:37 +08006780 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006781 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006782 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006783 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6784 if (lo == NULL)
6785 return nfserr_jukebox;
6786 } else {
6787 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006788 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006789 if (!cstate->minorversion &&
6790 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006791 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006792 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006793
Jeff Laytondd257932016-08-11 10:37:39 -04006794 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6795 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006796 status = nfserr_jukebox;
6797 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006798 }
Jeff Laytondd257932016-08-11 10:37:39 -04006799
Jeff Layton5db1c032014-07-29 21:34:28 -04006800 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006801 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006802out:
6803 nfs4_put_stateowner(&lo->lo_owner);
6804 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006805}
6806
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807/*
6808 * LOCK operation
6809 */
Al Virob37ad282006-10-19 23:28:59 -07006810__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006811nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006812 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006814 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006815 struct nfs4_openowner *open_sop = NULL;
6816 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006817 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006818 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006819 struct nfs4_file *fp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006820 struct nfsd_file *nf = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006821 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006822 struct file_lock *file_lock = NULL;
6823 struct file_lock *conflock = NULL;
J. Bruce Fieldsf657f8e2021-08-20 17:02:04 -04006824 struct super_block *sb;
Al Virob37ad282006-10-19 23:28:59 -07006825 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006826 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006827 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006828 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006829 unsigned char fl_type;
6830 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006831 struct net *net = SVC_NET(rqstp);
6832 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
6834 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6835 (long long) lock->lk_offset,
6836 (long long) lock->lk_length);
6837
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 if (check_lock_length(lock->lk_offset, lock->lk_length))
6839 return nfserr_inval;
6840
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006841 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006842 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006843 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6844 return status;
6845 }
J. Bruce Fieldsf657f8e2021-08-20 17:02:04 -04006846 sb = cstate->current_fh.fh_dentry->d_sb;
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006847
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006849 if (nfsd4_has_session(cstate))
6850 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006851 memcpy(&lock->lk_new_clientid,
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006852 &cstate->clp->cl_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006853 sizeof(clientid_t));
6854
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006856 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 lock->lk_new_open_seqid,
6858 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006859 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006860 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006862 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006863 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006864 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006865 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006866 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006867 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006868 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006869 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006870 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006871 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006872 lock->lk_old_lock_seqid,
6873 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006874 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006875 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006876 if (status)
6877 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006878 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006880 lkflg = setlkflg(lock->lk_type);
6881 status = nfs4_check_openmode(lock_stp, lkflg);
6882 if (status)
6883 goto out;
6884
NeilBrown0dd395d2005-07-07 17:59:15 -07006885 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006886 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006887 goto out;
6888 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006889 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006890 goto out;
6891
J. Bruce Fieldsbb0a55b2021-08-20 17:02:06 -04006892 if (lock->lk_reclaim)
6893 fl_flags |= FL_RECLAIM;
6894
Trond Myklebust11b91642014-07-29 21:34:08 -04006895 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 case NFS4_READW_LT:
J. Bruce Fieldsf657f8e2021-08-20 17:02:04 -04006898 if (nfsd4_has_session(cstate) &&
6899 !(sb->s_export_op->flags & EXPORT_OP_SYNC_LOCKS))
Jeff Layton76d348f2016-09-16 16:28:24 -04006900 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006901 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006902 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006903 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006904 nf = find_readable_file_locked(fp);
6905 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006906 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006907 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006908 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006909 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 case NFS4_WRITEW_LT:
J. Bruce Fieldsf657f8e2021-08-20 17:02:04 -04006911 if (nfsd4_has_session(cstate) &&
6912 !(sb->s_export_op->flags & EXPORT_OP_SYNC_LOCKS))
Jeff Layton76d348f2016-09-16 16:28:24 -04006913 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006914 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006915 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006916 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006917 nf = find_writeable_file_locked(fp);
6918 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006919 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006920 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006921 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006922 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 default:
6924 status = nfserr_inval;
6925 goto out;
6926 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006927
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006928 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006929 status = nfserr_openmode;
6930 goto out;
6931 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006932
Jeff Layton76d348f2016-09-16 16:28:24 -04006933 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6934 if (!nbl) {
6935 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6936 status = nfserr_jukebox;
6937 goto out;
6938 }
6939
6940 file_lock = &nbl->nbl_lock;
6941 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006942 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006943 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006944 file_lock->fl_file = nf->nf_file;
Jeff Layton76d348f2016-09-16 16:28:24 -04006945 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006946 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6947 file_lock->fl_start = lock->lk_offset;
6948 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6949 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950
Jeff Layton21179d82012-08-21 08:03:32 -04006951 conflock = locks_alloc_lock();
6952 if (!conflock) {
6953 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6954 status = nfserr_jukebox;
6955 goto out;
6956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957
Jeff Layton76d348f2016-09-16 16:28:24 -04006958 if (fl_flags & FL_SLEEP) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01006959 nbl->nbl_time = ktime_get_boottime_seconds();
Jeff Layton0cc11a62016-10-20 09:34:31 -04006960 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006961 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006962 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006963 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006964 }
6965
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006966 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006967 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006969 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006970 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006971 if (lock->lk_reclaim)
6972 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006973 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006974 case FILE_LOCK_DEFERRED:
6975 nbl = NULL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006976 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006977 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006978 status = nfserr_denied;
6979 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006980 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006981 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006982 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006984 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006985 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006986 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006987 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006991 if (nbl) {
6992 /* dequeue it if we queued it before */
6993 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006994 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006995 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006996 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006997 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006998 }
6999 free_blocked_lock(nbl);
7000 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007001 if (nf)
7002 nfsd_file_put(nf);
Jeff Layton5db1c032014-07-29 21:34:28 -04007003 if (lock_stp) {
7004 /* Bump seqid manually if the 4.0 replay owner is openowner */
7005 if (cstate->replay_owner &&
7006 cstate->replay_owner != &lock_sop->lo_owner &&
7007 seqid_mutating_err(ntohl(status)))
7008 lock_sop->lo_owner.so_seqid++;
7009
7010 /*
7011 * If this is a new, never-before-used stateid, and we are
7012 * returning an error, then just go ahead and release it.
7013 */
J. Bruce Fields25020722018-01-17 16:25:59 -05007014 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04007015 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04007016
7017 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04007018
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04007019 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04007020 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04007021 if (open_stp)
7022 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04007023 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04007024 if (conflock)
7025 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 return status;
7027}
7028
7029/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08007030 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
7031 * so we do a temporary open here just to get an open file to pass to
J. Bruce Fields0bcc7ca2021-08-24 22:36:03 -04007032 * vfs_test_lock.
J. Bruce Fields55ef1272008-12-20 11:58:38 -08007033 */
Al Viro04da6e92012-04-13 00:00:04 -04007034static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08007035{
Jeff Layton6b556ca2019-08-18 14:18:55 -04007036 struct nfsd_file *nf;
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04007037 __be32 err;
7038
7039 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
7040 if (err)
7041 return err;
7042 fh_lock(fhp); /* to block new leases till after test_lock: */
7043 err = nfserrno(nfsd_open_break_lease(fhp->fh_dentry->d_inode,
7044 NFSD_MAY_READ));
7045 if (err)
7046 goto out;
J. Bruce Fields0bcc7ca2021-08-24 22:36:03 -04007047 lock->fl_file = nf->nf_file;
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04007048 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
J. Bruce Fields0bcc7ca2021-08-24 22:36:03 -04007049 lock->fl_file = NULL;
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04007050out:
7051 fh_unlock(fhp);
7052 nfsd_file_put(nf);
J. Bruce Fields55ef1272008-12-20 11:58:38 -08007053 return err;
7054}
7055
7056/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 * LOCKT operation
7058 */
Al Virob37ad282006-10-19 23:28:59 -07007059__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08007060nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007061 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007063 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04007064 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04007065 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07007066 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03007067 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04007069 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 return nfserr_grace;
7071
7072 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
7073 return nfserr_inval;
7074
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007075 if (!nfsd4_has_session(cstate)) {
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05007076 status = set_client(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007077 if (status)
7078 goto out;
7079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080
J. Bruce Fields75c096f2011-08-15 18:39:32 -04007081 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
Jeff Layton21179d82012-08-21 08:03:32 -04007084 file_lock = locks_alloc_lock();
7085 if (!file_lock) {
7086 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7087 status = nfserr_jukebox;
7088 goto out;
7089 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08007090
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091 switch (lockt->lt_type) {
7092 case NFS4_READ_LT:
7093 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04007094 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00007095 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 case NFS4_WRITE_LT:
7097 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04007098 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00007099 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04007101 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00007103 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 }
7105
Kinglong Meec8623992015-07-13 17:35:37 +08007106 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007107 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04007108 file_lock->fl_owner = (fl_owner_t)lo;
7109 file_lock->fl_pid = current->tgid;
7110 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111
Jeff Layton21179d82012-08-21 08:03:32 -04007112 file_lock->fl_start = lockt->lt_offset;
7113 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114
Jeff Layton21179d82012-08-21 08:03:32 -04007115 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116
Jeff Layton21179d82012-08-21 08:03:32 -04007117 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04007118 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05007119 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04007120
Jeff Layton21179d82012-08-21 08:03:32 -04007121 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04007123 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 }
7125out:
Jeff Layton5db1c032014-07-29 21:34:28 -04007126 if (lo)
7127 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04007128 if (file_lock)
7129 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130 return status;
7131}
7132
Al Virob37ad282006-10-19 23:28:59 -07007133__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08007134nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007135 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007137 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04007138 struct nfs4_ol_stateid *stp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007139 struct nfsd_file *nf = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04007140 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07007141 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07007142 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007143 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7144
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7146 (long long) locku->lu_offset,
7147 (long long) locku->lu_length);
7148
7149 if (check_lock_length(locku->lu_offset, locku->lu_length))
7150 return nfserr_inval;
7151
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04007152 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007153 &locku->lu_stateid, NFS4_LOCK_STID,
7154 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04007155 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 goto out;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007157 nf = find_any_file(stp->st_stid.sc_file);
7158 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04007159 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04007160 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04007161 }
Jeff Layton21179d82012-08-21 08:03:32 -04007162 file_lock = locks_alloc_lock();
7163 if (!file_lock) {
7164 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7165 status = nfserr_jukebox;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007166 goto put_file;
Jeff Layton21179d82012-08-21 08:03:32 -04007167 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08007168
Jeff Layton21179d82012-08-21 08:03:32 -04007169 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04007170 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04007171 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007172 file_lock->fl_file = nf->nf_file;
Jeff Layton21179d82012-08-21 08:03:32 -04007173 file_lock->fl_flags = FL_POSIX;
7174 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7175 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176
Jeff Layton21179d82012-08-21 08:03:32 -04007177 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7178 locku->lu_length);
7179 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007181 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07007182 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05007183 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184 goto out_nfserr;
7185 }
Jeff Layton9767feb2015-10-01 09:05:50 -04007186 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007187put_file:
7188 nfsd_file_put(nf);
Trond Myklebust858cc572014-07-29 21:34:16 -04007189put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04007190 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04007191 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04007193 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04007194 if (file_lock)
7195 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 return status;
7197
7198out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07007199 status = nfserrno(err);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007200 goto put_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201}
7202
7203/*
7204 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007205 * true: locks held by lockowner
7206 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007208static bool
7209check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007211 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007212 int status = false;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007213 struct nfsd_file *nf = find_any_file(fp);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007214 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007215 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007216
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007217 if (!nf) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007218 /* Any valid lock stateid should have some sort of access */
7219 WARN_ON_ONCE(1);
7220 return status;
7221 }
7222
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007223 inode = locks_inode(nf->nf_file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007224 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007226 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05007227 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007228 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7229 if (fl->fl_owner == (fl_owner_t)lowner) {
7230 status = true;
7231 break;
7232 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08007233 }
Jeff Layton6109c852015-01-16 15:05:57 -05007234 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007236 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 return status;
7238}
7239
Al Virob37ad282006-10-19 23:28:59 -07007240__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08007241nfsd4_release_lockowner(struct svc_rqst *rqstp,
7242 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007243 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007245 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04007247 struct nfs4_stateowner *sop;
7248 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04007249 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04007251 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07007252 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03007253 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04007254 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04007255 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256
7257 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7258 clid->cl_boot, clid->cl_id);
7259
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05007260 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007261 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04007262 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007263
Trond Myklebustd4f04892014-07-29 21:34:36 -04007264 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04007265 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04007266 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04007267 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04007268 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04007269
7270 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05007271 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04007272
Jeff Layton882e9d22014-07-29 21:34:37 -04007273 /* see if there are still any locks associated with it */
7274 lo = lockowner(sop);
7275 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
7276 if (check_for_locks(stp->st_stid.sc_file, lo)) {
7277 status = nfserr_locks_held;
7278 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04007279 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04007280 }
Jeff Layton5adfd882014-07-29 21:34:31 -04007281 }
Jeff Layton882e9d22014-07-29 21:34:37 -04007282
Kinglong Meeb5971af2014-08-22 10:18:43 -04007283 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04007284 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04007285 }
Chuck Lever88584812016-07-13 16:40:14 -04007286 if (!lo) {
7287 spin_unlock(&clp->cl_lock);
7288 return status;
7289 }
7290
7291 unhash_lockowner_locked(lo);
7292 while (!list_empty(&lo->lo_owner.so_stateids)) {
7293 stp = list_first_entry(&lo->lo_owner.so_stateids,
7294 struct nfs4_ol_stateid,
7295 st_perstateowner);
7296 WARN_ON(!unhash_lock_stateid(stp));
7297 put_ol_stateid_locked(stp, &reaplist);
7298 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04007299 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04007300 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007301 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04007302 nfs4_put_stateowner(&lo->lo_owner);
7303
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 return status;
7305}
7306
7307static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07007308alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309{
NeilBrowna55370a2005-06-23 22:03:52 -07007310 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311}
7312
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007313bool
Scott Mayhew6b189102019-03-26 18:06:26 -04007314nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07007315{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007316 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07007317
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007318 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007319 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07007320}
7321
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322/*
7323 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04007324 *
7325 * The caller is responsible for freeing name.data if NULL is returned (it
7326 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05007328struct nfs4_client_reclaim *
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007329nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7330 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331{
7332 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007333 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334
NeilBrowna55370a2005-06-23 22:03:52 -07007335 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05007336 if (crp) {
7337 strhashval = clientstr_hashval(name);
7338 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007339 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04007340 crp->cr_name.data = name.data;
7341 crp->cr_name.len = name.len;
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007342 crp->cr_princhash.data = princhash.data;
7343 crp->cr_princhash.len = princhash.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007344 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007345 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007346 }
7347 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348}
7349
Jeff Layton2a4317c2012-03-21 16:42:43 -04007350void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007351nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05007352{
7353 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04007354 kfree(crp->cr_name.data);
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007355 kfree(crp->cr_princhash.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05007356 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007357 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05007358}
7359
7360void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007361nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362{
7363 struct nfs4_client_reclaim *crp = NULL;
7364 int i;
7365
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007367 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7368 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007370 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371 }
7372 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05007373 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374}
7375
7376/*
7377 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04007378struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04007379nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380{
7381 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 struct nfs4_client_reclaim *crp = NULL;
7383
Scott Mayhew6b189102019-03-26 18:06:26 -04007384 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007385 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04007386 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387 return crp;
7388 }
7389 }
7390 return NULL;
7391}
7392
Al Virob37ad282006-10-19 23:28:59 -07007393__be32
J. Bruce Fields1722b042021-01-21 17:57:44 -05007394nfs4_check_open_reclaim(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395{
J. Bruce Fields1722b042021-01-21 17:57:44 -05007396 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Jeff Layton3b3e7b72014-09-12 16:40:22 -04007397 return nfserr_no_grace;
7398
J. Bruce Fields1722b042021-01-21 17:57:44 -05007399 if (nfsd4_client_record_check(clp))
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007400 return nfserr_reclaim_bad;
7401
7402 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403}
7404
Meelap Shahc2f1a552007-07-17 04:04:39 -07007405/*
7406 * Since the lifetime of a delegation isn't limited to that of an open, a
7407 * client may quite reasonably hang on to a delegation as long as it has
7408 * the inode cached. This becomes an obvious problem the first time a
7409 * client's inode cache approaches the size of the server's total memory.
7410 *
7411 * For now we avoid this problem by imposing a hard limit on the number
7412 * of delegations, which varies according to the server's memory size.
7413 */
7414static void
7415set_max_delegations(void)
7416{
7417 /*
7418 * Allow at most 4 delegations per megabyte of RAM. Quick
7419 * estimates suggest that in the worst case (where every delegation
7420 * is for a different inode), a delegation could take about 1.5K,
7421 * giving a worst case usage of about 6% of memory.
7422 */
7423 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7424}
7425
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007426static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007427{
7428 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7429 int i;
7430
Kees Cook6da2ec52018-06-12 13:55:00 -07007431 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7432 sizeof(struct list_head),
7433 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007434 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007435 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007436 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7437 sizeof(struct list_head),
7438 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007439 if (!nn->unconf_id_hashtbl)
7440 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007441 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7442 sizeof(struct list_head),
7443 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007444 if (!nn->sessionid_hashtbl)
7445 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007446
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007447 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007448 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007449 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007450 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007451 for (i = 0; i < SESSION_HASH_SIZE; i++)
7452 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007453 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007454 nn->unconf_name_tree = RB_ROOT;
Arnd Bergmann9cc76802019-10-31 15:53:13 +01007455 nn->boot_time = ktime_get_real_seconds();
Vasily Averin81833de2017-11-13 07:25:40 +03007456 nn->grace_ended = false;
7457 nn->nfsd4_manager.block_opens = true;
7458 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007459 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007460 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007461 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007462 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007463 spin_lock_init(&nn->s2s_cp_lock);
7464 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007465
Jeff Layton0cc11a62016-10-20 09:34:31 -04007466 spin_lock_init(&nn->blocked_locks_lock);
7467 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7468
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007469 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007470 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007471
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007472 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007473
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007474err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007475 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007476err_unconf_id:
7477 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007478err:
7479 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007480}
7481
7482static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007483nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007484{
7485 int i;
7486 struct nfs4_client *clp = NULL;
7487 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7488
7489 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7490 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7491 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7492 destroy_client(clp);
7493 }
7494 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007495
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007496 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7497
Kinglong Mee2b905632014-03-26 22:09:30 +08007498 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7499 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7500 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7501 destroy_client(clp);
7502 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007503 }
7504
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007505 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007506 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007507 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007508 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007509}
7510
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007511int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007512nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007513{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007514 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007515 int ret;
7516
Trond Myklebustc6c7f2a2021-03-13 16:08:47 -05007517 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007518 if (ret)
7519 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007520 locks_start_grace(net, &nn->nfsd4_manager);
7521 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007522 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7523 goto skip_grace;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01007524 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
Vasily Averin7e981a82017-11-06 16:46:04 +03007525 nn->nfsd4_grace, net->ns.inum);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007526 trace_nfsd_grace_start(nn);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007527 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007528 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007529
7530skip_grace:
7531 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7532 net->ns.inum);
7533 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7534 nfsd4_end_grace(nn);
7535 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007536}
7537
7538/* initialization to perform when the nfsd service is started: */
7539
7540int
7541nfs4_state_start(void)
7542{
7543 int ret;
7544
Jeff Layton51a54562015-08-20 07:17:01 -04007545 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007546 if (laundry_wq == NULL) {
7547 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007548 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007549 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007550 ret = nfsd4_create_callback_queue();
7551 if (ret)
7552 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007553
Meelap Shahc2f1a552007-07-17 04:04:39 -07007554 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007555 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007556
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007557out_free_laundry:
7558 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007559out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007560 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561}
7562
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007563void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007564nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007568 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007570 cancel_delayed_work_sync(&nn->laundromat_work);
7571 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007572
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007574 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007575 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007577 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007578 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 }
Benny Halevycdc975052014-05-30 09:09:30 -04007580 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 list_for_each_safe(pos, next, &reaplist) {
7582 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007583 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007584 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585 }
7586
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007587 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007588 nfs4_state_destroy_net(net);
Dai Ngof4e44b32021-05-21 15:09:37 -04007589#ifdef CONFIG_NFSD_V4_2_INTER_SSC
7590 nfsd4_ssc_shutdown_umount(nn);
7591#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592}
7593
7594void
7595nfs4_state_shutdown(void)
7596{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007597 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007598 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007600
7601static void
7602get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7603{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007604 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7605 CURRENT_STATEID(stateid))
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007606 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007607}
7608
7609static void
7610put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7611{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007612 if (cstate->minorversion) {
7613 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007614 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007615 }
7616}
7617
7618void
7619clear_current_stateid(struct nfsd4_compound_state *cstate)
7620{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007621 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007622}
7623
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007624/*
7625 * functions to set current state id
7626 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007627void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007628nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7629 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007630{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007631 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007632}
7633
7634void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007635nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7636 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007637{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007638 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007639}
7640
7641void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007642nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7643 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007644{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007645 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007646}
7647
7648void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007649nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7650 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007651{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007652 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007653}
7654
7655/*
7656 * functions to consume current state id
7657 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007658
7659void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007660nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7661 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007662{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007663 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007664}
7665
7666void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007667nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7668 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007669{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007670 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007671}
7672
7673void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007674nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7675 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007676{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007677 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007678}
7679
7680void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007681nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7682 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007683{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007684 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007685}
7686
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007687void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007688nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7689 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007690{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007691 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007692}
7693
7694void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007695nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7696 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007697{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007698 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007699}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007700
7701void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007702nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7703 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007704{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007705 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007706}
7707
7708void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007709nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7710 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007711{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007712 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007713}