blob: 836bd825ca4ad66a9d70de578e523d2bcbfba12a [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
Dai Ngo66af2572022-05-02 14:19:21 -0700128static struct workqueue_struct *laundry_wq;
129
Dai Ngod76cc462022-05-02 14:19:23 -0700130int nfsd4_create_laundry_wq(void)
131{
132 int rc = 0;
133
134 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
135 if (laundry_wq == NULL)
136 rc = -ENOMEM;
137 return rc;
138}
139
140void nfsd4_destroy_laundry_wq(void)
141{
142 destroy_workqueue(laundry_wq);
143}
144
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400145static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800146{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400147 return ses->se_flags & NFS4_SESSION_DEAD;
148}
149
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400150static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
151{
152 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400153 return nfserr_jukebox;
154 ses->se_flags |= NFS4_SESSION_DEAD;
155 return nfs_ok;
156}
157
J. Bruce Fields221a6872013-04-01 22:23:49 -0400158static bool is_client_expired(struct nfs4_client *clp)
159{
160 return clp->cl_time == 0;
161}
162
Dai Ngo3a4ea23d2022-09-14 08:54:25 -0700163static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn,
164 struct nfs4_client *clp)
165{
166 if (clp->cl_state != NFSD4_ACTIVE)
167 atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0);
168}
169
J. Bruce Fields221a6872013-04-01 22:23:49 -0400170static __be32 get_client_locked(struct nfs4_client *clp)
171{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400172 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
173
174 lockdep_assert_held(&nn->client_lock);
175
J. Bruce Fields221a6872013-04-01 22:23:49 -0400176 if (is_client_expired(clp))
177 return nfserr_expired;
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400178 atomic_inc(&clp->cl_rpc_users);
Dai Ngo3a4ea23d2022-09-14 08:54:25 -0700179 nfsd4_dec_courtesy_client_count(nn, clp);
Dai Ngo66af2572022-05-02 14:19:21 -0700180 clp->cl_state = NFSD4_ACTIVE;
J. Bruce Fields221a6872013-04-01 22:23:49 -0400181 return nfs_ok;
182}
183
184/* must be called under the client_lock */
185static inline void
186renew_client_locked(struct nfs4_client *clp)
187{
188 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
189
190 if (is_client_expired(clp)) {
191 WARN_ON(1);
192 printk("%s: client (clientid %08x/%08x) already expired\n",
193 __func__,
194 clp->cl_clientid.cl_boot,
195 clp->cl_clientid.cl_id);
196 return;
197 }
198
J. Bruce Fields221a6872013-04-01 22:23:49 -0400199 list_move_tail(&clp->cl_lru, &nn->client_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100200 clp->cl_time = ktime_get_boottime_seconds();
Dai Ngo3a4ea23d2022-09-14 08:54:25 -0700201 nfsd4_dec_courtesy_client_count(nn, clp);
Dai Ngo66af2572022-05-02 14:19:21 -0700202 clp->cl_state = NFSD4_ACTIVE;
J. Bruce Fields221a6872013-04-01 22:23:49 -0400203}
204
Fengguang Wuba138432013-04-16 22:14:15 -0400205static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400206{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400207 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
208
209 lockdep_assert_held(&nn->client_lock);
210
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400211 if (!atomic_dec_and_test(&clp->cl_rpc_users))
J. Bruce Fields221a6872013-04-01 22:23:49 -0400212 return;
213 if (!is_client_expired(clp))
214 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400215 else
216 wake_up_all(&expiry_wq);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400217}
218
Jeff Layton4b24ca72014-06-30 11:48:44 -0400219static void put_client_renew(struct nfs4_client *clp)
220{
221 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
222
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400223 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
Jeff Laytond6c249b2014-07-08 14:02:50 -0400224 return;
225 if (!is_client_expired(clp))
226 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400227 else
228 wake_up_all(&expiry_wq);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400229 spin_unlock(&nn->client_lock);
230}
231
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400232static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
233{
234 __be32 status;
235
236 if (is_session_dead(ses))
237 return nfserr_badsession;
238 status = get_client_locked(ses->se_client);
239 if (status)
240 return status;
241 atomic_inc(&ses->se_ref);
242 return nfs_ok;
243}
244
245static void nfsd4_put_session_locked(struct nfsd4_session *ses)
246{
247 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400248 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
249
250 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400251
252 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
253 free_session(ses);
254 put_client_renew_locked(clp);
255}
256
257static void nfsd4_put_session(struct nfsd4_session *ses)
258{
259 struct nfs4_client *clp = ses->se_client;
260 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
261
262 spin_lock(&nn->client_lock);
263 nfsd4_put_session_locked(ses);
264 spin_unlock(&nn->client_lock);
265}
266
Jeff Layton76d348f2016-09-16 16:28:24 -0400267static struct nfsd4_blocked_lock *
268find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
269 struct nfsd_net *nn)
270{
271 struct nfsd4_blocked_lock *cur, *found = NULL;
272
Jeff Layton0cc11a62016-10-20 09:34:31 -0400273 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400274 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
275 if (fh_match(fh, &cur->nbl_fh)) {
276 list_del_init(&cur->nbl_list);
Vasily Averin47446d742021-12-17 09:49:39 +0300277 WARN_ON(list_empty(&cur->nbl_lru));
Jeff Layton7919d0a2016-09-16 16:28:25 -0400278 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400279 found = cur;
280 break;
281 }
282 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400283 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400284 if (found)
NeilBrowncb03f942018-11-30 10:04:08 +1100285 locks_delete_block(&found->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400286 return found;
287}
288
289static struct nfsd4_blocked_lock *
290find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
291 struct nfsd_net *nn)
292{
293 struct nfsd4_blocked_lock *nbl;
294
295 nbl = find_blocked_lock(lo, fh, nn);
296 if (!nbl) {
297 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
298 if (nbl) {
Vasily Averine1e83992020-03-27 07:50:40 +0300299 INIT_LIST_HEAD(&nbl->nbl_list);
300 INIT_LIST_HEAD(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400301 fh_copy_shallow(&nbl->nbl_fh, fh);
302 locks_init_lock(&nbl->nbl_lock);
Vasily Averin47446d742021-12-17 09:49:39 +0300303 kref_init(&nbl->nbl_kref);
Jeff Layton76d348f2016-09-16 16:28:24 -0400304 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
305 &nfsd4_cb_notify_lock_ops,
306 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
307 }
308 }
309 return nbl;
310}
311
312static void
Vasily Averin47446d742021-12-17 09:49:39 +0300313free_nbl(struct kref *kref)
314{
315 struct nfsd4_blocked_lock *nbl;
316
317 nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref);
318 kfree(nbl);
319}
320
321static void
Jeff Layton76d348f2016-09-16 16:28:24 -0400322free_blocked_lock(struct nfsd4_blocked_lock *nbl)
323{
Jeff Layton6aaafc42019-04-22 12:34:23 -0400324 locks_delete_block(&nbl->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400325 locks_release_private(&nbl->nbl_lock);
Vasily Averin47446d742021-12-17 09:49:39 +0300326 kref_put(&nbl->nbl_kref, free_nbl);
Jeff Layton76d348f2016-09-16 16:28:24 -0400327}
328
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400329static void
330remove_blocked_locks(struct nfs4_lockowner *lo)
331{
332 struct nfs4_client *clp = lo->lo_owner.so_client;
333 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
334 struct nfsd4_blocked_lock *nbl;
335 LIST_HEAD(reaplist);
336
337 /* Dequeue all blocked locks */
338 spin_lock(&nn->blocked_locks_lock);
339 while (!list_empty(&lo->lo_blocked)) {
340 nbl = list_first_entry(&lo->lo_blocked,
341 struct nfsd4_blocked_lock,
342 nbl_list);
343 list_del_init(&nbl->nbl_list);
Vasily Averin47446d742021-12-17 09:49:39 +0300344 WARN_ON(list_empty(&nbl->nbl_lru));
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400345 list_move(&nbl->nbl_lru, &reaplist);
346 }
347 spin_unlock(&nn->blocked_locks_lock);
348
349 /* Now free them */
350 while (!list_empty(&reaplist)) {
351 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
352 nbl_lru);
353 list_del_init(&nbl->nbl_lru);
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400354 free_blocked_lock(nbl);
355 }
356}
357
Jeff Laytonf4564582019-04-22 12:34:24 -0400358static void
359nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
360{
361 struct nfsd4_blocked_lock *nbl = container_of(cb,
362 struct nfsd4_blocked_lock, nbl_cb);
363 locks_delete_block(&nbl->nbl_lock);
364}
365
Jeff Layton76d348f2016-09-16 16:28:24 -0400366static int
367nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
368{
Chuck Lever1035d652022-09-08 18:13:54 -0400369 trace_nfsd_cb_notify_lock_done(&zero_stateid, task);
370
Jeff Layton76d348f2016-09-16 16:28:24 -0400371 /*
372 * Since this is just an optimization, we don't try very hard if it
373 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
374 * just quit trying on anything else.
375 */
376 switch (task->tk_status) {
377 case -NFS4ERR_DELAY:
378 rpc_delay(task, 1 * HZ);
379 return 0;
380 default:
381 return 1;
382 }
383}
384
385static void
386nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
387{
388 struct nfsd4_blocked_lock *nbl = container_of(cb,
389 struct nfsd4_blocked_lock, nbl_cb);
390
391 free_blocked_lock(nbl);
392}
393
394static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
Jeff Laytonf4564582019-04-22 12:34:24 -0400395 .prepare = nfsd4_cb_notify_lock_prepare,
Jeff Layton76d348f2016-09-16 16:28:24 -0400396 .done = nfsd4_cb_notify_lock_done,
397 .release = nfsd4_cb_notify_lock_release,
398};
399
J. Bruce Fieldsebd9d2c2021-04-16 14:00:17 -0400400/*
401 * We store the NONE, READ, WRITE, and BOTH bits separately in the
402 * st_{access,deny}_bmap field of the stateid, in order to track not
403 * only what share bits are currently in force, but also what
404 * combinations of share bits previous opens have used. This allows us
J. Bruce Fields3dcd1d82021-12-07 17:32:21 -0500405 * to enforce the recommendation in
406 * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that
407 * the server return an error if the client attempt to downgrade to a
408 * combination of share bits not explicable by closing some of its
409 * previous opens.
J. Bruce Fieldsebd9d2c2021-04-16 14:00:17 -0400410 *
J. Bruce Fields3dcd1d82021-12-07 17:32:21 -0500411 * This enforcement is arguably incomplete, since we don't keep
J. Bruce Fieldsebd9d2c2021-04-16 14:00:17 -0400412 * track of access/deny bit combinations; so, e.g., we allow:
413 *
414 * OPEN allow read, deny write
415 * OPEN allow both, deny none
416 * DOWNGRADE allow read, deny none
417 *
418 * which we should reject.
J. Bruce Fields3dcd1d82021-12-07 17:32:21 -0500419 *
420 * But you could also argue that our current code is already overkill,
421 * since it only exists to return NFS4ERR_INVAL on incorrect client
422 * behavior.
J. Bruce Fieldsebd9d2c2021-04-16 14:00:17 -0400423 */
424static unsigned int
425bmap_to_share_mode(unsigned long bmap)
426{
427 int i;
428 unsigned int access = 0;
429
430 for (i = 1; i < 4; i++) {
431 if (test_bit(i, &bmap))
432 access |= i;
433 }
434 return access;
435}
436
437/* set share access for a given stateid */
438static inline void
439set_access(u32 access, struct nfs4_ol_stateid *stp)
440{
441 unsigned char mask = 1 << access;
442
443 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
444 stp->st_access_bmap |= mask;
445}
446
447/* clear share access for a given stateid */
448static inline void
449clear_access(u32 access, struct nfs4_ol_stateid *stp)
450{
451 unsigned char mask = 1 << access;
452
453 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
454 stp->st_access_bmap &= ~mask;
455}
456
457/* test whether a given stateid has access */
458static inline bool
459test_access(u32 access, struct nfs4_ol_stateid *stp)
460{
461 unsigned char mask = 1 << access;
462
463 return (bool)(stp->st_access_bmap & mask);
464}
465
466/* set share deny for a given stateid */
467static inline void
468set_deny(u32 deny, struct nfs4_ol_stateid *stp)
469{
470 unsigned char mask = 1 << deny;
471
472 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
473 stp->st_deny_bmap |= mask;
474}
475
476/* clear share deny for a given stateid */
477static inline void
478clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
479{
480 unsigned char mask = 1 << deny;
481
482 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
483 stp->st_deny_bmap &= ~mask;
484}
485
486/* test whether a given stateid is denying specific access */
487static inline bool
488test_deny(u32 deny, struct nfs4_ol_stateid *stp)
489{
490 unsigned char mask = 1 << deny;
491
492 return (bool)(stp->st_deny_bmap & mask);
493}
494
495static int nfs4_access_to_omode(u32 access)
496{
497 switch (access & NFS4_SHARE_ACCESS_BOTH) {
498 case NFS4_SHARE_ACCESS_READ:
499 return O_RDONLY;
500 case NFS4_SHARE_ACCESS_WRITE:
501 return O_WRONLY;
502 case NFS4_SHARE_ACCESS_BOTH:
503 return O_RDWR;
504 }
505 WARN_ON_ONCE(1);
506 return O_RDONLY;
507}
508
509static inline int
510access_permit_read(struct nfs4_ol_stateid *stp)
511{
512 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
513 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
514 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
515}
516
517static inline int
518access_permit_write(struct nfs4_ol_stateid *stp)
519{
520 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
521 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
522}
523
Kinglong Meeb5971af2014-08-22 10:18:43 -0400524static inline struct nfs4_stateowner *
525nfs4_get_stateowner(struct nfs4_stateowner *sop)
526{
527 atomic_inc(&sop->so_count);
528 return sop;
529}
530
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400531static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400532same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400533{
534 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400535 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400536}
537
538static struct nfs4_openowner *
539find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400540 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400541{
542 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400543
Trond Myklebustd4f04892014-07-29 21:34:36 -0400544 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400545
Trond Myklebustd4f04892014-07-29 21:34:36 -0400546 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
547 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400548 if (!so->so_is_open_owner)
549 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400550 if (same_owner_str(so, &open->op_owner))
551 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400552 }
553 return NULL;
554}
555
556static struct nfs4_openowner *
557find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400558 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400559{
560 struct nfs4_openowner *oo;
561
Trond Myklebustd4f04892014-07-29 21:34:36 -0400562 spin_lock(&clp->cl_lock);
563 oo = find_openstateowner_str_locked(hashval, open, clp);
564 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400565 return oo;
566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568static inline u32
569opaque_hashval(const void *ptr, int nbytes)
570{
571 unsigned char *cptr = (unsigned char *) ptr;
572
573 u32 x = 0;
574 while (nbytes--) {
575 x *= 37;
576 x += *cptr++;
577 }
578 return x;
579}
580
Jeff Layton5b095e92014-10-23 08:01:02 -0400581static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400582{
Jeff Layton5b095e92014-10-23 08:01:02 -0400583 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
584
585 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400586}
587
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200588void
NeilBrown13cd2182005-06-23 22:03:10 -0700589put_nfs4_file(struct nfs4_file *fi)
590{
Jeff Layton02e12152014-07-16 10:31:57 -0400591 might_lock(&state_lock);
592
Elena Reshetova818a34e2017-10-20 12:53:30 +0300593 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400594 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400595 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200596 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400597 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
598 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800599 }
NeilBrown13cd2182005-06-23 22:03:10 -0700600}
601
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400602static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400603__nfs4_get_fd(struct nfs4_file *f, int oflag)
604{
605 if (f->fi_fds[oflag])
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400606 return nfsd_file_get(f->fi_fds[oflag]);
Trond Myklebustde186432014-07-10 14:07:26 -0400607 return NULL;
608}
609
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400610static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400611find_writeable_file_locked(struct nfs4_file *f)
612{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400613 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400614
615 lockdep_assert_held(&f->fi_lock);
616
617 ret = __nfs4_get_fd(f, O_WRONLY);
618 if (!ret)
619 ret = __nfs4_get_fd(f, O_RDWR);
620 return ret;
621}
622
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400623static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400624find_writeable_file(struct nfs4_file *f)
625{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400626 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400627
628 spin_lock(&f->fi_lock);
629 ret = find_writeable_file_locked(f);
630 spin_unlock(&f->fi_lock);
631
632 return ret;
633}
634
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400635static struct nfsd_file *
636find_readable_file_locked(struct nfs4_file *f)
Trond Myklebustde186432014-07-10 14:07:26 -0400637{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400638 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400639
640 lockdep_assert_held(&f->fi_lock);
641
642 ret = __nfs4_get_fd(f, O_RDONLY);
643 if (!ret)
644 ret = __nfs4_get_fd(f, O_RDWR);
645 return ret;
646}
647
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400648static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400649find_readable_file(struct nfs4_file *f)
650{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400651 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400652
653 spin_lock(&f->fi_lock);
654 ret = find_readable_file_locked(f);
655 spin_unlock(&f->fi_lock);
656
657 return ret;
658}
659
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400660struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400661find_any_file(struct nfs4_file *f)
662{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400663 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400664
Trond Myklebusta451b122020-03-01 18:21:38 -0500665 if (!f)
666 return NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400667 spin_lock(&f->fi_lock);
668 ret = __nfs4_get_fd(f, O_RDWR);
669 if (!ret) {
670 ret = __nfs4_get_fd(f, O_WRONLY);
671 if (!ret)
672 ret = __nfs4_get_fd(f, O_RDONLY);
673 }
674 spin_unlock(&f->fi_lock);
675 return ret;
676}
677
J. Bruce Fields9affa432020-07-15 13:31:36 -0400678static struct nfsd_file *find_deleg_file(struct nfs4_file *f)
679{
680 struct nfsd_file *ret = NULL;
681
682 spin_lock(&f->fi_lock);
683 if (f->fi_deleg_file)
684 ret = nfsd_file_get(f->fi_deleg_file);
685 spin_unlock(&f->fi_lock);
686 return ret;
687}
688
Trond Myklebust02a35082014-07-25 07:34:22 -0400689static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800690unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700691
692/*
693 * Open owner state (share locks)
694 */
695
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500696/* hash tables for lock and open owners */
697#define OWNER_HASH_BITS 8
698#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
699#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700700
Trond Myklebustd4f04892014-07-29 21:34:36 -0400701static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400702{
703 unsigned int ret;
704
705 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500706 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400707}
NeilBrownef0f3392006-04-10 22:55:41 -0700708
NeilBrownef0f3392006-04-10 22:55:41 -0700709/* hash table for nfs4_file */
710#define FILE_HASH_BITS 8
711#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800712
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400713static unsigned int file_hashval(struct svc_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400714{
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400715 struct inode *inode = d_inode(fh->fh_dentry);
Trond Myklebustca943212014-07-23 16:17:39 -0400716
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400717 /* XXX: why not (here & in file cache) use inode? */
718 return (unsigned int)hash_long(inode->i_ino, FILE_HASH_BITS);
Trond Myklebustca943212014-07-23 16:17:39 -0400719}
720
Jeff Layton89876f82013-04-02 09:01:59 -0400721static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700722
Dai Ngo3d694272022-05-02 14:19:22 -0700723/*
724 * Check if courtesy clients have conflicting access and resolve it if possible
725 *
726 * access: is op_share_access if share_access is true.
727 * Check if access mode, op_share_access, would conflict with
728 * the current deny mode of the file 'fp'.
729 * access: is op_share_deny if share_access is false.
730 * Check if the deny mode, op_share_deny, would conflict with
731 * current access of the file 'fp'.
732 * stp: skip checking this entry.
733 * new_stp: normal open, not open upgrade.
734 *
735 * Function returns:
736 * false - access/deny mode conflict with normal client.
737 * true - no conflict or conflict with courtesy client(s) is resolved.
738 */
739static bool
740nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
741 struct nfs4_ol_stateid *stp, u32 access, bool share_access)
742{
743 struct nfs4_ol_stateid *st;
744 bool resolvable = true;
745 unsigned char bmap;
746 struct nfsd_net *nn;
747 struct nfs4_client *clp;
748
749 lockdep_assert_held(&fp->fi_lock);
750 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
751 /* ignore lock stateid */
752 if (st->st_openstp)
753 continue;
754 if (st == stp && new_stp)
755 continue;
756 /* check file access against deny mode or vice versa */
757 bmap = share_access ? st->st_deny_bmap : st->st_access_bmap;
758 if (!(access & bmap_to_share_mode(bmap)))
759 continue;
760 clp = st->st_stid.sc_client;
761 if (try_to_expire_client(clp))
762 continue;
763 resolvable = false;
764 break;
765 }
766 if (resolvable) {
767 clp = stp->st_stid.sc_client;
768 nn = net_generic(clp->net, nfsd_net_id);
769 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
770 }
771 return resolvable;
772}
773
Jeff Layton12659652014-07-10 14:07:28 -0400774static void
775__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400776{
Jeff Layton7214e862014-07-10 14:07:33 -0400777 lockdep_assert_held(&fp->fi_lock);
778
Jeff Layton12659652014-07-10 14:07:28 -0400779 if (access & NFS4_SHARE_ACCESS_WRITE)
780 atomic_inc(&fp->fi_access[O_WRONLY]);
781 if (access & NFS4_SHARE_ACCESS_READ)
782 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400783}
784
Jeff Layton12659652014-07-10 14:07:28 -0400785static __be32
786nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400787{
Jeff Layton7214e862014-07-10 14:07:33 -0400788 lockdep_assert_held(&fp->fi_lock);
789
Jeff Layton12659652014-07-10 14:07:28 -0400790 /* Does this access mode make sense? */
791 if (access & ~NFS4_SHARE_ACCESS_BOTH)
792 return nfserr_inval;
793
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400794 /* Does it conflict with a deny mode already set? */
795 if ((access & fp->fi_share_deny) != 0)
796 return nfserr_share_denied;
797
Jeff Layton12659652014-07-10 14:07:28 -0400798 __nfs4_file_get_access(fp, access);
799 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400800}
801
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400802static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
803{
804 /* Common case is that there is no deny mode. */
805 if (deny) {
806 /* Does this deny mode make sense? */
807 if (deny & ~NFS4_SHARE_DENY_BOTH)
808 return nfserr_inval;
809
810 if ((deny & NFS4_SHARE_DENY_READ) &&
811 atomic_read(&fp->fi_access[O_RDONLY]))
812 return nfserr_share_denied;
813
814 if ((deny & NFS4_SHARE_DENY_WRITE) &&
815 atomic_read(&fp->fi_access[O_WRONLY]))
816 return nfserr_share_denied;
817 }
818 return nfs_ok;
819}
820
J. Bruce Fields998db522010-08-07 09:21:41 -0400821static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400822{
Trond Myklebustde186432014-07-10 14:07:26 -0400823 might_lock(&fp->fi_lock);
824
825 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400826 struct nfsd_file *f1 = NULL;
827 struct nfsd_file *f2 = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400828
Jeff Layton6d338b52014-07-10 14:07:29 -0400829 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400830 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400831 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400832 spin_unlock(&fp->fi_lock);
833 if (f1)
Chuck Lever5e138c42022-07-08 14:27:02 -0400834 nfsd_file_close(f1);
Trond Myklebustde186432014-07-10 14:07:26 -0400835 if (f2)
Chuck Lever5e138c42022-07-08 14:27:02 -0400836 nfsd_file_close(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400837 }
838}
839
Jeff Layton12659652014-07-10 14:07:28 -0400840static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400841{
Jeff Layton12659652014-07-10 14:07:28 -0400842 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
843
844 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400845 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400846 if (access & NFS4_SHARE_ACCESS_READ)
847 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400848}
849
Sachin Bhamare8287f002015-04-27 14:50:14 +0200850/*
851 * Allocate a new open/delegation state counter. This is needed for
852 * pNFS for proper return on close semantics.
853 *
854 * Note that we only allocate it for pNFS-enabled exports, otherwise
855 * all pointers to struct nfs4_clnt_odstate are always NULL.
856 */
857static struct nfs4_clnt_odstate *
858alloc_clnt_odstate(struct nfs4_client *clp)
859{
860 struct nfs4_clnt_odstate *co;
861
862 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
863 if (co) {
864 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300865 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200866 }
867 return co;
868}
869
870static void
871hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
872{
873 struct nfs4_file *fp = co->co_file;
874
875 lockdep_assert_held(&fp->fi_lock);
876 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
877}
878
879static inline void
880get_clnt_odstate(struct nfs4_clnt_odstate *co)
881{
882 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300883 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200884}
885
886static void
887put_clnt_odstate(struct nfs4_clnt_odstate *co)
888{
889 struct nfs4_file *fp;
890
891 if (!co)
892 return;
893
894 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300895 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200896 list_del(&co->co_perfile);
897 spin_unlock(&fp->fi_lock);
898
899 nfsd4_return_all_file_layouts(co->co_client, fp);
900 kmem_cache_free(odstate_slab, co);
901 }
902}
903
904static struct nfs4_clnt_odstate *
905find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
906{
907 struct nfs4_clnt_odstate *co;
908 struct nfs4_client *cl;
909
910 if (!new)
911 return NULL;
912
913 cl = new->co_client;
914
915 spin_lock(&fp->fi_lock);
916 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
917 if (co->co_client == cl) {
918 get_clnt_odstate(co);
919 goto out;
920 }
921 }
922 co = new;
923 co->co_file = fp;
924 hash_clnt_odstate_locked(new);
925out:
926 spin_unlock(&fp->fi_lock);
927 return co;
928}
929
Kinglong Meed19fb702017-01-18 19:04:42 +0800930struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
931 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400932{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500933 struct nfs4_stid *stid;
934 int new_id;
935
Trond Myklebustf8338832014-07-25 07:34:19 -0400936 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500937 if (!stid)
938 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400939
Jeff Layton4770d722014-07-29 21:34:10 -0400940 idr_preload(GFP_KERNEL);
941 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400942 /* Reserving 0 for start of file in nfsdfs "states" file: */
943 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400944 spin_unlock(&cl->cl_lock);
945 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700946 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500947 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800948
949 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500950 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500951 stid->sc_stateid.si_opaque.so_id = new_id;
952 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
953 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300954 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400955 spin_lock_init(&stid->sc_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400956 INIT_LIST_HEAD(&stid->sc_cp_list);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500957
J. Bruce Fields996e0932011-10-17 11:14:48 -0400958 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500959 * It shouldn't be a problem to reuse an opaque stateid value.
960 * I don't think it is for 4.1. But with 4.0 I worry that, for
961 * example, a stray write retransmission could be accepted by
962 * the server when it should have been rejected. Therefore,
963 * adopt a trick from the sctp code to attempt to maximize the
964 * amount of time until an id is reused, by ensuring they always
965 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400966 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500967 return stid;
968out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800969 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500970 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400971}
972
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400973/*
974 * Create a unique stateid_t to represent each COPY.
975 */
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400976static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
Chuck Lever781fde12022-09-22 13:10:35 -0400977 unsigned char cs_type)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400978{
979 int new_id;
980
Chuck Lever781fde12022-09-22 13:10:35 -0400981 stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
982 stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
983 stid->cs_type = cs_type;
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400984
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400985 idr_preload(GFP_KERNEL);
986 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400987 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
Chuck Lever781fde12022-09-22 13:10:35 -0400988 stid->cs_stid.si_opaque.so_id = new_id;
989 stid->cs_stid.si_generation = 1;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400990 spin_unlock(&nn->s2s_cp_lock);
991 idr_preload_end();
992 if (new_id < 0)
993 return 0;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400994 return 1;
995}
996
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400997int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
998{
999 return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
1000}
1001
1002struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
1003 struct nfs4_stid *p_stid)
1004{
1005 struct nfs4_cpntf_state *cps;
1006
1007 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
1008 if (!cps)
1009 return NULL;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01001010 cps->cpntf_time = ktime_get_boottime_seconds();
Chuck Lever781fde12022-09-22 13:10:35 -04001011 refcount_set(&cps->cp_stateid.cs_count, 1);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001012 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
1013 goto out_free;
1014 spin_lock(&nn->s2s_cp_lock);
1015 list_add(&cps->cp_list, &p_stid->sc_cp_list);
1016 spin_unlock(&nn->s2s_cp_lock);
1017 return cps;
1018out_free:
1019 kfree(cps);
1020 return NULL;
1021}
1022
1023void nfs4_free_copy_state(struct nfsd4_copy *copy)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001024{
1025 struct nfsd_net *nn;
1026
Chuck Lever781fde12022-09-22 13:10:35 -04001027 WARN_ON_ONCE(copy->cp_stateid.cs_type != NFS4_COPY_STID);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001028 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
1029 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001030 idr_remove(&nn->s2s_cp_stateids,
Chuck Lever781fde12022-09-22 13:10:35 -04001031 copy->cp_stateid.cs_stid.si_opaque.so_id);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001032 spin_unlock(&nn->s2s_cp_lock);
1033}
1034
1035static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
1036{
1037 struct nfs4_cpntf_state *cps;
1038 struct nfsd_net *nn;
1039
1040 nn = net_generic(net, nfsd_net_id);
1041 spin_lock(&nn->s2s_cp_lock);
1042 while (!list_empty(&stid->sc_cp_list)) {
1043 cps = list_first_entry(&stid->sc_cp_list,
1044 struct nfs4_cpntf_state, cp_list);
1045 _free_cpntf_state_locked(nn, cps);
1046 }
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001047 spin_unlock(&nn->s2s_cp_lock);
1048}
1049
Jeff Laytonb49e0842014-07-29 21:34:11 -04001050static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04001051{
Trond Myklebust60116952014-07-29 21:34:06 -04001052 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -04001053
Kinglong Meed19fb702017-01-18 19:04:42 +08001054 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -04001055 if (!stid)
1056 return NULL;
1057
Kinglong Meed19fb702017-01-18 19:04:42 +08001058 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -04001059}
1060
1061static void nfs4_free_deleg(struct nfs4_stid *stid)
1062{
Jeff Layton895ddf52022-09-26 14:41:02 -04001063 struct nfs4_delegation *dp = delegstateid(stid);
1064
1065 WARN_ON_ONCE(!list_empty(&stid->sc_cp_list));
1066 WARN_ON_ONCE(!list_empty(&dp->dl_perfile));
1067 WARN_ON_ONCE(!list_empty(&dp->dl_perclnt));
1068 WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru));
Trond Myklebust60116952014-07-29 21:34:06 -04001069 kmem_cache_free(deleg_slab, stid);
1070 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04001071}
1072
NeilBrown6282cd52014-06-04 17:39:26 +10001073/*
1074 * When we recall a delegation, we should be careful not to hand it
1075 * out again straight away.
1076 * To ensure this we keep a pair of bloom filters ('new' and 'old')
1077 * in which the filehandles of recalled delegations are "stored".
1078 * If a filehandle appear in either filter, a delegation is blocked.
1079 * When a delegation is recalled, the filehandle is stored in the "new"
1080 * filter.
1081 * Every 30 seconds we swap the filters and clear the "new" one,
1082 * unless both are empty of course.
1083 *
1084 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
1085 * low 3 bytes as hash-table indices.
1086 *
Jeff Laytonf54fe962014-07-25 07:34:26 -04001087 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +10001088 * is used to manage concurrent access. Testing does not need the lock
1089 * except when swapping the two filters.
1090 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04001091static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001092static struct bloom_pair {
1093 int entries, old_entries;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001094 time64_t swap_time;
NeilBrown6282cd52014-06-04 17:39:26 +10001095 int new; /* index into 'set' */
1096 DECLARE_BITMAP(set[2], 256);
1097} blocked_delegations;
1098
1099static int delegation_blocked(struct knfsd_fh *fh)
1100{
1101 u32 hash;
1102 struct bloom_pair *bd = &blocked_delegations;
1103
1104 if (bd->entries == 0)
1105 return 0;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001106 if (ktime_get_seconds() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -04001107 spin_lock(&blocked_delegations_lock);
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001108 if (ktime_get_seconds() - bd->swap_time > 30) {
NeilBrown6282cd52014-06-04 17:39:26 +10001109 bd->entries -= bd->old_entries;
1110 bd->old_entries = bd->entries;
1111 memset(bd->set[bd->new], 0,
1112 sizeof(bd->set[0]));
1113 bd->new = 1-bd->new;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001114 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +10001115 }
Jeff Laytonf54fe962014-07-25 07:34:26 -04001116 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001117 }
NeilBrownd8b26072021-09-02 11:16:32 +10001118 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +10001119 if (test_bit(hash&255, bd->set[0]) &&
1120 test_bit((hash>>8)&255, bd->set[0]) &&
1121 test_bit((hash>>16)&255, bd->set[0]))
1122 return 1;
1123
1124 if (test_bit(hash&255, bd->set[1]) &&
1125 test_bit((hash>>8)&255, bd->set[1]) &&
1126 test_bit((hash>>16)&255, bd->set[1]))
1127 return 1;
1128
1129 return 0;
1130}
1131
1132static void block_delegations(struct knfsd_fh *fh)
1133{
1134 u32 hash;
1135 struct bloom_pair *bd = &blocked_delegations;
1136
NeilBrownd8b26072021-09-02 11:16:32 +10001137 hash = jhash(&fh->fh_raw, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +10001138
Jeff Laytonf54fe962014-07-25 07:34:26 -04001139 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001140 __set_bit(hash&255, bd->set[bd->new]);
1141 __set_bit((hash>>8)&255, bd->set[bd->new]);
1142 __set_bit((hash>>16)&255, bd->set[bd->new]);
1143 if (bd->entries == 0)
Arnd Bergmannb3f255e2017-10-20 11:25:34 +02001144 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +10001145 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -04001146 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +10001147}
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -05001150alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
Sachin Bhamare8287f002015-04-27 14:50:14 +02001151 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -04001154 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -04001157 n = atomic_long_inc_return(&num_delegations);
1158 if (n < 0 || n > max_delegations)
1159 goto out_dec;
Jeff Laytonbbf936e2022-07-26 16:45:30 +10001160 if (delegation_blocked(&fp->fi_fhandle))
Trond Myklebust02a35082014-07-25 07:34:22 -04001161 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +08001162 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -07001163 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -04001164 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -04001165
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04001166 /*
1167 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001168 * meaning of seqid 0 isn't meaningful, really, but let's avoid
1169 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04001170 */
1171 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -07001172 INIT_LIST_HEAD(&dp->dl_perfile);
1173 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +02001175 dp->dl_clnt_odstate = odstate;
1176 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -04001177 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02001178 dp->dl_retries = 1;
Dai Ngo66af2572022-05-02 14:19:21 -07001179 dp->dl_recalled = false;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02001180 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +02001181 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -05001182 get_nfs4_file(fp);
1183 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -04001185out_dec:
1186 atomic_long_dec(&num_delegations);
1187 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190void
Trond Myklebust60116952014-07-29 21:34:06 -04001191nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Trond Myklebust11b91642014-07-29 21:34:08 -04001193 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -04001194 struct nfs4_client *clp = s->sc_client;
1195
Jeff Layton4770d722014-07-29 21:34:10 -04001196 might_lock(&clp->cl_lock);
1197
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001198 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -04001199 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -04001200 return;
Jeff Laytonb401be222014-07-29 21:34:33 -04001201 }
Trond Myklebust60116952014-07-29 21:34:06 -04001202 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04001203 nfs4_free_cpntf_statelist(clp->net, s);
Jeff Layton4770d722014-07-29 21:34:10 -04001204 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04001205 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -04001206 if (fp)
1207 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Jeff Layton9767feb2015-10-01 09:05:50 -04001210void
1211nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
1212{
1213 stateid_t *src = &stid->sc_stateid;
1214
1215 spin_lock(&stid->sc_lock);
1216 if (unlikely(++src->si_generation == 0))
1217 src->si_generation = 1;
1218 memcpy(dst, src, sizeof(*dst));
1219 spin_unlock(&stid->sc_lock);
1220}
1221
J. Bruce Fields353601e2018-02-16 14:29:42 -05001222static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001224 struct nfsd_file *nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05001225
1226 spin_lock(&fp->fi_lock);
1227 if (--fp->fi_delegees == 0)
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001228 swap(nf, fp->fi_deleg_file);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001229 spin_unlock(&fp->fi_lock);
1230
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001231 if (nf)
1232 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001233}
1234
1235static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1236{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -05001237 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001238 struct nfsd_file *nf = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -04001239
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -05001240 WARN_ON_ONCE(!fp->fi_delegees);
1241
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001242 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001243 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001246static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1247{
1248 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001249 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001250 nfs4_put_stid(&dp->dl_stid);
1251}
1252
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001253void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001254{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001255 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001256}
1257
Andrew Elble34ed9872015-10-15 12:07:28 -04001258/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001259 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -04001260 * @clp: a pointer to the nfs4_client we're granting a delegation to
1261 * @fp: a pointer to the nfs4_file we're granting a delegation on
1262 *
1263 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001264 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -04001265 */
1266
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001267static bool
1268nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -04001269{
Andrew Elble34ed9872015-10-15 12:07:28 -04001270 struct nfs4_delegation *searchdp = NULL;
1271 struct nfs4_client *searchclp = NULL;
1272
Benny Halevycdc975052014-05-30 09:09:30 -04001273 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04001274 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001275
Andrew Elble34ed9872015-10-15 12:07:28 -04001276 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1277 searchclp = searchdp->dl_stid.sc_client;
1278 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001279 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -04001280 }
1281 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001282 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -04001283}
1284
1285/**
1286 * hash_delegation_locked - Add a delegation to the appropriate lists
1287 * @dp: a pointer to the nfs4_delegation we are adding.
1288 * @fp: a pointer to the nfs4_file we're granting a delegation on
1289 *
1290 * Return:
1291 * On success: NULL if the delegation was successfully hashed.
1292 *
1293 * On error: -EAGAIN if one was previously granted to this
1294 * nfs4_client for this nfs4_file. Delegation is not hashed.
1295 *
1296 */
1297
1298static int
1299hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1300{
Andrew Elble34ed9872015-10-15 12:07:28 -04001301 struct nfs4_client *clp = dp->dl_stid.sc_client;
1302
1303 lockdep_assert_held(&state_lock);
1304 lockdep_assert_held(&fp->fi_lock);
1305
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001306 if (nfs4_delegation_exists(clp, fp))
1307 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001308 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001309 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001310 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001311 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1312 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001313}
1314
J. Bruce Fields548ec082021-11-29 15:08:00 -05001315static bool delegation_hashed(struct nfs4_delegation *dp)
1316{
1317 return !(list_empty(&dp->dl_perfile));
1318}
1319
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001320static bool
Jeff Layton42690672014-07-25 07:34:20 -04001321unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Trond Myklebust11b91642014-07-29 21:34:08 -04001323 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001324
Jeff Layton42690672014-07-25 07:34:20 -04001325 lockdep_assert_held(&state_lock);
1326
J. Bruce Fields548ec082021-11-29 15:08:00 -05001327 if (!delegation_hashed(dp))
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001328 return false;
1329
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001330 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001331 /* Ensure that deleg break won't try to requeue it */
1332 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001333 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001334 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001336 list_del_init(&dp->dl_perfile);
1337 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001338 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001339}
1340
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001341static void destroy_delegation(struct nfs4_delegation *dp)
1342{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001343 bool unhashed;
1344
Jeff Layton42690672014-07-25 07:34:20 -04001345 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001346 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001347 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001348 if (unhashed)
1349 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001350}
1351
1352static void revoke_delegation(struct nfs4_delegation *dp)
1353{
1354 struct nfs4_client *clp = dp->dl_stid.sc_client;
1355
Jeff Layton2d4a5322014-07-25 07:34:21 -04001356 WARN_ON(!list_empty(&dp->dl_recall_lru));
1357
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001358 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001359 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001360 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001361 spin_lock(&clp->cl_lock);
1362 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1363 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001364 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001365 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001366}
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368/*
1369 * SETCLIENTID state
1370 */
1371
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001372static unsigned int clientid_hashval(u32 id)
1373{
1374 return id & CLIENT_HASH_MASK;
1375}
1376
Scott Mayhew6b189102019-03-26 18:06:26 -04001377static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001378{
Scott Mayhew6b189102019-03-26 18:06:26 -04001379 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001380}
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382/*
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001383 * A stateid that had a deny mode associated with it is being released
1384 * or downgraded. Recalculate the deny mode on the file.
1385 */
1386static void
1387recalculate_deny_mode(struct nfs4_file *fp)
1388{
1389 struct nfs4_ol_stateid *stp;
1390
1391 spin_lock(&fp->fi_lock);
1392 fp->fi_share_deny = 0;
1393 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1394 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1395 spin_unlock(&fp->fi_lock);
1396}
1397
1398static void
1399reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1400{
1401 int i;
1402 bool change = false;
1403
1404 for (i = 1; i < 4; i++) {
1405 if ((i & deny) != i) {
1406 change = true;
1407 clear_deny(i, stp);
1408 }
1409 }
1410
1411 /* Recalculate per-file deny mode if there was a change */
1412 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001413 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001414}
1415
Jeff Layton82c5ff12012-05-11 09:45:13 -04001416/* release all access and file references for a given stateid */
1417static void
1418release_all_access(struct nfs4_ol_stateid *stp)
1419{
1420 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001421 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001422
1423 if (fp && stp->st_deny_bmap != 0)
1424 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001425
1426 for (i = 1; i < 4; i++) {
1427 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001428 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001429 clear_access(i, stp);
1430 }
1431}
1432
Kinglong Meed50ffde2015-07-16 12:05:07 +08001433static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1434{
1435 kfree(sop->so_owner.data);
1436 sop->so_ops->so_free(sop);
1437}
1438
Jeff Layton6b180f02014-07-29 21:34:26 -04001439static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1440{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001441 struct nfs4_client *clp = sop->so_client;
1442
1443 might_lock(&clp->cl_lock);
1444
1445 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001446 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001447 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001448 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001449 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001450}
1451
Trond Myklebusta451b122020-03-01 18:21:38 -05001452static bool
1453nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1454{
1455 return list_empty(&stp->st_perfile);
1456}
1457
Jeff Laytone8568732015-08-24 12:41:47 -04001458static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001459{
Trond Myklebust11b91642014-07-29 21:34:08 -04001460 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001461
Jeff Layton1c755dc2014-07-29 21:34:12 -04001462 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1463
Jeff Laytone8568732015-08-24 12:41:47 -04001464 if (list_empty(&stp->st_perfile))
1465 return false;
1466
Trond Myklebust1d31a252014-07-10 14:07:25 -04001467 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001468 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001469 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001470 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001471 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001472}
1473
Trond Myklebust60116952014-07-29 21:34:06 -04001474static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001475{
Trond Myklebust60116952014-07-29 21:34:06 -04001476 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001477
Sachin Bhamare8287f002015-04-27 14:50:14 +02001478 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001479 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001480 if (stp->st_stateowner)
1481 nfs4_put_stateowner(stp->st_stateowner);
Dai Ngo019805f2022-09-26 10:59:16 -07001482 WARN_ON(!list_empty(&stid->sc_cp_list));
Trond Myklebust60116952014-07-29 21:34:06 -04001483 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001484}
1485
Jeff Laytonb49e0842014-07-29 21:34:11 -04001486static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001487{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001488 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1489 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001490 struct nfsd_file *nf;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001491
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001492 nf = find_any_file(stp->st_stid.sc_file);
1493 if (nf) {
1494 get_file(nf->nf_file);
1495 filp_close(nf->nf_file, (fl_owner_t)lo);
1496 nfsd_file_put(nf);
1497 }
Jeff Laytonb49e0842014-07-29 21:34:11 -04001498 nfs4_free_ol_stateid(stid);
1499}
1500
Jeff Layton2c41beb2014-07-29 21:34:41 -04001501/*
1502 * Put the persistent reference to an already unhashed generic stateid, while
1503 * holding the cl_lock. If it's the last reference, then put it onto the
1504 * reaplist for later destruction.
1505 */
1506static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1507 struct list_head *reaplist)
1508{
1509 struct nfs4_stid *s = &stp->st_stid;
1510 struct nfs4_client *clp = s->sc_client;
1511
1512 lockdep_assert_held(&clp->cl_lock);
1513
1514 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1515
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001516 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001517 wake_up_all(&close_wq);
1518 return;
1519 }
1520
1521 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1522 list_add(&stp->st_locks, reaplist);
1523}
1524
Jeff Laytone8568732015-08-24 12:41:47 -04001525static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001526{
Chuck Leverf46c4452016-10-29 18:19:03 -04001527 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001528
Trond Myklebusta451b122020-03-01 18:21:38 -05001529 if (!unhash_ol_stateid(stp))
1530 return false;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001531 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001532 nfs4_unhash_stid(&stp->st_stid);
Trond Myklebusta451b122020-03-01 18:21:38 -05001533 return true;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001534}
1535
Jeff Layton5adfd882014-07-29 21:34:31 -04001536static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001537{
Chuck Leverf46c4452016-10-29 18:19:03 -04001538 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001539 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001540
Chuck Leverf46c4452016-10-29 18:19:03 -04001541 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001542 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001543 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001544 if (unhashed)
1545 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001546}
1547
Trond Myklebustc58c6612014-07-29 21:34:35 -04001548static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001549{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001550 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001551
Trond Myklebustd4f04892014-07-29 21:34:36 -04001552 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001553
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001554 list_del_init(&lo->lo_owner.so_strhash);
1555}
1556
Jeff Layton2c41beb2014-07-29 21:34:41 -04001557/*
1558 * Free a list of generic stateids that were collected earlier after being
1559 * fully unhashed.
1560 */
1561static void
1562free_ol_stateid_reaplist(struct list_head *reaplist)
1563{
1564 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001565 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001566
1567 might_sleep();
1568
1569 while (!list_empty(reaplist)) {
1570 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1571 st_locks);
1572 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001573 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001574 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001575 if (fp)
1576 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001577 }
1578}
1579
Jeff Laytond83017f2014-07-29 21:34:42 -04001580static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1581 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001582{
1583 struct nfs4_ol_stateid *stp;
1584
Jeff Laytone8568732015-08-24 12:41:47 -04001585 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1586
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001587 while (!list_empty(&open_stp->st_locks)) {
1588 stp = list_entry(open_stp->st_locks.next,
1589 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001590 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001591 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001592 }
1593}
1594
Jeff Laytone8568732015-08-24 12:41:47 -04001595static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001596 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001597{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001598 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1599
Trond Myklebusta451b122020-03-01 18:21:38 -05001600 if (!unhash_ol_stateid(stp))
1601 return false;
Jeff Laytond83017f2014-07-29 21:34:42 -04001602 release_open_stateid_locks(stp, reaplist);
Trond Myklebusta451b122020-03-01 18:21:38 -05001603 return true;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001604}
1605
1606static void release_open_stateid(struct nfs4_ol_stateid *stp)
1607{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001608 LIST_HEAD(reaplist);
1609
1610 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001611 if (unhash_open_stateid(stp, &reaplist))
1612 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001613 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1614 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001615}
1616
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001617static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001618{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001619 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001620
Trond Myklebustd4f04892014-07-29 21:34:36 -04001621 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001622
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001623 list_del_init(&oo->oo_owner.so_strhash);
1624 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001625}
1626
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001627static void release_last_closed_stateid(struct nfs4_openowner *oo)
1628{
Jeff Layton217526e2014-07-30 08:27:11 -04001629 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1630 nfsd_net_id);
1631 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001632
Jeff Layton217526e2014-07-30 08:27:11 -04001633 spin_lock(&nn->client_lock);
1634 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001635 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001636 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001637 oo->oo_last_closed_stid = NULL;
1638 }
Jeff Layton217526e2014-07-30 08:27:11 -04001639 spin_unlock(&nn->client_lock);
1640 if (s)
1641 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001642}
1643
Jeff Layton2c41beb2014-07-29 21:34:41 -04001644static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001645{
1646 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001647 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001648 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001649
Jeff Layton2c41beb2014-07-29 21:34:41 -04001650 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001651
Trond Myklebustd4f04892014-07-29 21:34:36 -04001652 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001653 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001654 while (!list_empty(&oo->oo_owner.so_stateids)) {
1655 stp = list_first_entry(&oo->oo_owner.so_stateids,
1656 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001657 if (unhash_open_stateid(stp, &reaplist))
1658 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001659 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001660 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001661 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001662 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001663 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001664}
1665
Marc Eshel5282fd72009-04-03 08:27:52 +03001666static inline int
1667hash_sessionid(struct nfs4_sessionid *sessionid)
1668{
1669 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1670
1671 return sid->sequence % SESSION_HASH_SIZE;
1672}
1673
Mark Salter135dd002015-04-06 09:46:00 -04001674#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001675static inline void
1676dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1677{
1678 u32 *ptr = (u32 *)(&sessionid->data[0]);
1679 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1680}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001681#else
1682static inline void
1683dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1684{
1685}
1686#endif
1687
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001688/*
1689 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1690 * won't be used for replay.
1691 */
1692void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1693{
1694 struct nfs4_stateowner *so = cstate->replay_owner;
1695
1696 if (nfserr == nfserr_replay_me)
1697 return;
1698
1699 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001700 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001701 return;
1702 }
1703 if (!so)
1704 return;
1705 if (so->so_is_open_owner)
1706 release_last_closed_stateid(openowner(so));
1707 so->so_seqid++;
1708 return;
1709}
Marc Eshel5282fd72009-04-03 08:27:52 +03001710
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001711static void
1712gen_sessionid(struct nfsd4_session *ses)
1713{
1714 struct nfs4_client *clp = ses->se_client;
1715 struct nfsd4_sessionid *sid;
1716
1717 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1718 sid->clientid = clp->cl_clientid;
1719 sid->sequence = current_sessionid++;
1720 sid->reserved = 0;
1721}
1722
1723/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001724 * The protocol defines ca_maxresponssize_cached to include the size of
1725 * the rpc header, but all we need to cache is the data starting after
1726 * the end of the initial SEQUENCE operation--the rest we regenerate
1727 * each time. Therefore we can advertise a ca_maxresponssize_cached
1728 * value that is the number of bytes in our cache plus a few additional
1729 * bytes. In order to stay on the safe side, and not promise more than
1730 * we can cache, those additional bytes must be the minimum possible: 24
1731 * bytes of rpc header (xid through accept state, with AUTH_NULL
1732 * verifier), 12 for the compound header (with zero-length tag), and 44
1733 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001734 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001735#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1736
Andy Adamson557ce262009-08-28 08:45:04 -04001737static void
1738free_session_slots(struct nfsd4_session *ses)
1739{
1740 int i;
1741
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001742 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1743 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001744 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001745 }
Andy Adamson557ce262009-08-28 08:45:04 -04001746}
1747
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001748/*
1749 * We don't actually need to cache the rpc and session headers, so we
1750 * can allocate a little less for each slot:
1751 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001752static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001753{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001754 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001755
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001756 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1757 size = 0;
1758 else
1759 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1760 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001761}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001762
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001763/*
1764 * XXX: If we run out of reserved DRC memory we could (up to a point)
1765 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001766 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001767 */
NeilBrown2030ca52019-09-20 16:36:45 +10001768static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001769{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001770 u32 slotsize = slot_bytes(ca);
1771 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001772 unsigned long avail, total_avail;
NeilBrown2030ca52019-09-20 16:36:45 +10001773 unsigned int scale_factor;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001774
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001775 spin_lock(&nfsd_drc_lock);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001776 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1777 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1778 else
1779 /* We have handed out more space than we chose in
1780 * set_max_drc() to allow. That isn't really a
1781 * problem as long as that doesn't make us think we
1782 * have lots more due to integer overflow.
1783 */
1784 total_avail = 0;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001785 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001786 /*
NeilBrown2030ca52019-09-20 16:36:45 +10001787 * Never use more than a fraction of the remaining memory,
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001788 * unless it's the only way to give this client a slot.
NeilBrown2030ca52019-09-20 16:36:45 +10001789 * The chosen fraction is either 1/8 or 1/number of threads,
1790 * whichever is smaller. This ensures there are adequate
1791 * slots to support multiple clients per thread.
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001792 * Give the client one slot even if that would require
1793 * over-allocation--it is better than failure.
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001794 */
NeilBrown2030ca52019-09-20 16:36:45 +10001795 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1796
1797 avail = clamp_t(unsigned long, avail, slotsize,
1798 total_avail/scale_factor);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001799 num = min_t(int, num, avail / slotsize);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001800 num = max_t(int, num, 1);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001801 nfsd_drc_mem_used += num * slotsize;
1802 spin_unlock(&nfsd_drc_lock);
1803
1804 return num;
1805}
1806
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001807static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001808{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001809 int slotsize = slot_bytes(ca);
1810
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001811 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001812 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001813 spin_unlock(&nfsd_drc_lock);
1814}
1815
Kinglong Mee60810e52014-01-01 00:35:47 +08001816static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1817 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001818{
Kinglong Mee60810e52014-01-01 00:35:47 +08001819 int numslots = fattrs->maxreqs;
1820 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001821 struct nfsd4_session *new;
1822 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001823
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001824 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001825 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1826 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001827
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001828 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001829 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001830 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001831 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001832 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001833 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001834 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001835 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001836 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001837
1838 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1839 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1840
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001841 return new;
1842out_free:
1843 while (i--)
1844 kfree(new->se_slots[i]);
1845 kfree(new);
1846 return NULL;
1847}
1848
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001849static void free_conn(struct nfsd4_conn *c)
1850{
1851 svc_xprt_put(c->cn_xprt);
1852 kfree(c);
1853}
1854
1855static void nfsd4_conn_lost(struct svc_xpt_user *u)
1856{
1857 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1858 struct nfs4_client *clp = c->cn_session->se_client;
1859
Chuck Lever806d65b2021-05-14 15:56:43 -04001860 trace_nfsd_cb_lost(clp);
1861
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001862 spin_lock(&clp->cl_lock);
1863 if (!list_empty(&c->cn_persession)) {
1864 list_del(&c->cn_persession);
1865 free_conn(c);
1866 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001867 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001868 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001869}
1870
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001871static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001872{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001873 struct nfsd4_conn *conn;
1874
1875 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1876 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001877 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001878 svc_xprt_get(rqstp->rq_xprt);
1879 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001880 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001881 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1882 return conn;
1883}
1884
J. Bruce Fields328ead22010-09-29 16:11:06 -04001885static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1886{
1887 conn->cn_session = ses;
1888 list_add(&conn->cn_persession, &ses->se_conns);
1889}
1890
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001891static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1892{
1893 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001894
1895 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001896 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001897 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001898}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001899
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001900static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001901{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001902 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001903 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001904}
1905
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001906static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001907{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001908 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001909
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001910 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001911 ret = nfsd4_register_conn(conn);
1912 if (ret)
1913 /* oops; xprt is already down: */
1914 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001915 /* We may have gained or lost a callback channel: */
1916 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001917}
1918
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001919static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001920{
1921 u32 dir = NFS4_CDFC4_FORE;
1922
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001923 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001924 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001925 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001926}
1927
1928/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001929static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001930{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001931 struct nfs4_client *clp = s->se_client;
1932 struct nfsd4_conn *c;
1933
1934 spin_lock(&clp->cl_lock);
1935 while (!list_empty(&s->se_conns)) {
1936 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1937 list_del_init(&c->cn_persession);
1938 spin_unlock(&clp->cl_lock);
1939
1940 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1941 free_conn(c);
1942
1943 spin_lock(&clp->cl_lock);
1944 }
1945 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001946}
1947
J. Bruce Fields1377b692012-09-11 21:42:40 -04001948static void __free_session(struct nfsd4_session *ses)
1949{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001950 free_session_slots(ses);
1951 kfree(ses);
1952}
1953
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001954static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001955{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001956 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001957 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001958 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001959}
Andy Adamson557ce262009-08-28 08:45:04 -04001960
Fengguang Wu135ae822012-11-10 07:20:25 -05001961static 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 -04001962{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001963 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001964 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001965
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001966 new->se_client = clp;
1967 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001968
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001969 INIT_LIST_HEAD(&new->se_conns);
1970
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001971 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001972 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001973 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001974 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001975 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001976 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001977 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001978 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001979 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001980 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001981
Chuck Leverb0d2e422014-08-22 15:10:59 -04001982 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001983 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001984 /*
1985 * This is a little silly; with sessions there's no real
1986 * use for the callback address. Use the peer address
1987 * as a reasonable default for now, but consider fixing
1988 * the rpc client not to require an address in the
1989 * future:
1990 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001991 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1992 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001993 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001994}
1995
Benny Halevy9089f1b2010-05-12 00:12:26 +03001996/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001997static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001998__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001999{
2000 struct nfsd4_session *elem;
2001 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002002 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03002003
Trond Myklebust0a880a22014-07-30 08:27:10 -04002004 lockdep_assert_held(&nn->client_lock);
2005
Marc Eshel5282fd72009-04-03 08:27:52 +03002006 dump_sessionid(__func__, sessionid);
2007 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03002008 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03002009 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03002010 if (!memcmp(elem->se_sessionid.data, sessionid->data,
2011 NFS4_MAX_SESSIONID_LEN)) {
2012 return elem;
2013 }
2014 }
2015
2016 dprintk("%s: session not found\n", __func__);
2017 return NULL;
2018}
2019
Trond Myklebustd4e19e702014-06-30 11:48:42 -04002020static struct nfsd4_session *
2021find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2022 __be32 *ret)
2023{
2024 struct nfsd4_session *session;
2025 __be32 status = nfserr_badsession;
2026
2027 session = __find_in_sessionid_hashtbl(sessionid, net);
2028 if (!session)
2029 goto out;
2030 status = nfsd4_get_session_locked(session);
2031 if (status)
2032 session = NULL;
2033out:
2034 *ret = status;
2035 return session;
2036}
2037
Benny Halevy9089f1b2010-05-12 00:12:26 +03002038/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03002039static void
Marc Eshel5282fd72009-04-03 08:27:52 +03002040unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03002041{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002042 struct nfs4_client *clp = ses->se_client;
2043 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2044
2045 lockdep_assert_held(&nn->client_lock);
2046
Andy Adamson7116ed62009-04-03 08:27:43 +03002047 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002048 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03002049 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002050 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03002051}
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
2054static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04002055STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05002057 /*
2058 * We're assuming the clid was not given out from a boot
2059 * precisely 2^32 (about 136 years) before this one. That seems
2060 * a safe assumption:
2061 */
2062 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return 0;
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04002064 trace_nfsd_clid_stale(clid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return 1;
2066}
2067
2068/*
2069 * XXX Should we use a slab cache ?
2070 * This type of memory management is somewhat inefficient, but we use it
2071 * anyway since SETCLIENTID is not a common operation.
2072 */
Dai Ngo0926c3952022-07-15 16:54:52 -07002073static struct nfs4_client *alloc_client(struct xdr_netobj name,
2074 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
2076 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04002077 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Dai Ngo4271c2c2022-07-15 16:54:53 -07002079 if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) {
2080 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
2081 return NULL;
2082 }
Jeff Layton9258a2d2018-03-16 09:47:22 -04002083 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002084 if (clp == NULL)
2085 return NULL;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04002086 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04002087 if (clp->cl_name.data == NULL)
2088 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07002089 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
2090 sizeof(struct list_head),
2091 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04002092 if (!clp->cl_ownerstr_hashtbl)
2093 goto err_no_hashtbl;
2094 for (i = 0; i < OWNER_HASH_SIZE; i++)
2095 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
Trond Myklebust5694c932014-04-18 14:43:56 -04002096 INIT_LIST_HEAD(&clp->cl_sessions);
2097 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04002098 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04002099 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Dai Ngo66af2572022-05-02 14:19:21 -07002100 clp->cl_state = NFSD4_ACTIVE;
Dai Ngo0926c3952022-07-15 16:54:52 -07002101 atomic_inc(&nn->nfs4_client_count);
Dai Ngo66af2572022-05-02 14:19:21 -07002102 atomic_set(&clp->cl_delegs_in_recall, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04002103 INIT_LIST_HEAD(&clp->cl_idhash);
2104 INIT_LIST_HEAD(&clp->cl_openowners);
2105 INIT_LIST_HEAD(&clp->cl_delegations);
2106 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04002107 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002108#ifdef CONFIG_NFSD_PNFS
2109 INIT_LIST_HEAD(&clp->cl_lo_states);
2110#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002111 INIT_LIST_HEAD(&clp->async_copies);
2112 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04002113 spin_lock_init(&clp->cl_lock);
2114 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04002116err_no_hashtbl:
2117 kfree(clp->cl_name.data);
2118err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04002119 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04002120 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002123static void __free_client(struct kref *k)
2124{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002125 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2126 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002127
2128 free_svc_cred(&clp->cl_cred);
2129 kfree(clp->cl_ownerstr_hashtbl);
2130 kfree(clp->cl_name.data);
J. Bruce Fields79123442019-06-05 12:42:05 -04002131 kfree(clp->cl_nii_domain.data);
2132 kfree(clp->cl_nii_name.data);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002133 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002134 kmem_cache_free(client_slab, clp);
2135}
2136
YueHaibing297e57a2019-07-08 15:29:33 +08002137static void drop_client(struct nfs4_client *clp)
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002138{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002139 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002140}
2141
Trond Myklebust4dd86e152014-04-18 14:43:58 -04002142static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143free_client(struct nfs4_client *clp)
2144{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002145 while (!list_empty(&clp->cl_sessions)) {
2146 struct nfsd4_session *ses;
2147 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2148 se_perclnt);
2149 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002150 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2151 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002152 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04002153 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002154 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002155 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002156 clp->cl_nfsd_dentry = NULL;
2157 wake_up_all(&expiry_wq);
2158 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002159 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
Benny Halevy84d38ac2010-05-12 00:13:16 +03002162/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04002163static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03002164unhash_client_locked(struct nfs4_client *clp)
2165{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002166 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002167 struct nfsd4_session *ses;
2168
Trond Myklebust0a880a22014-07-30 08:27:10 -04002169 lockdep_assert_held(&nn->client_lock);
2170
Trond Myklebust4beb3452014-07-30 08:27:02 -04002171 /* Mark the client as expired! */
2172 clp->cl_time = 0;
2173 /* Make it invisible */
2174 if (!list_empty(&clp->cl_idhash)) {
2175 list_del_init(&clp->cl_idhash);
2176 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2177 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2178 else
2179 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2180 }
2181 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002182 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002183 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2184 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002185 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03002186}
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04002189unhash_client(struct nfs4_client *clp)
2190{
2191 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2192
2193 spin_lock(&nn->client_lock);
2194 unhash_client_locked(clp);
2195 spin_unlock(&nn->client_lock);
2196}
2197
Jeff Layton97403d92014-07-30 08:27:12 -04002198static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2199{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04002200 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04002201 return nfserr_jukebox;
2202 unhash_client_locked(clp);
2203 return nfs_ok;
2204}
2205
Trond Myklebust4beb3452014-07-30 08:27:02 -04002206static void
2207__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Dai Ngo0926c3952022-07-15 16:54:52 -07002209 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002210 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002211 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 struct list_head reaplist;
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04002216 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07002217 while (!list_empty(&clp->cl_delegations)) {
2218 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04002219 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04002220 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
Benny Halevycdc975052014-05-30 09:09:30 -04002222 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 while (!list_empty(&reaplist)) {
2224 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04002225 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05002226 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04002228 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05002229 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04002230 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04002231 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002232 }
NeilBrownea1da632005-06-23 22:04:17 -07002233 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002234 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002235 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002236 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002238 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2239 struct nfs4_stateowner *so, *tmp;
2240
2241 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2242 so_strhash) {
2243 /* Should be no openowners at this point */
2244 WARN_ON_ONCE(so->so_is_open_owner);
2245 remove_blocked_locks(lockowner(so));
2246 }
2247 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002248 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002249 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002250 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002251 if (clp->cl_cb_conn.cb_xprt)
2252 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
Dai Ngo0926c3952022-07-15 16:54:52 -07002253 atomic_add_unless(&nn->nfs4_client_count, -1, 0);
Dai Ngo3a4ea23d2022-09-14 08:54:25 -07002254 nfsd4_dec_courtesy_client_count(nn, clp);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002255 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002256 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
2258
Trond Myklebust4beb3452014-07-30 08:27:02 -04002259static void
2260destroy_client(struct nfs4_client *clp)
2261{
2262 unhash_client(clp);
2263 __destroy_client(clp);
2264}
2265
Scott Mayhew362063a2019-03-26 18:06:28 -04002266static void inc_reclaim_complete(struct nfs4_client *clp)
2267{
2268 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2269
2270 if (!nn->track_reclaim_completes)
2271 return;
2272 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2273 return;
2274 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2275 nn->reclaim_str_hashtbl_size) {
2276 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2277 clp->net->ns.inum);
2278 nfsd4_end_grace(nn);
2279 }
2280}
2281
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002282static void expire_client(struct nfs4_client *clp)
2283{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002284 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002285 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002286 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002287}
2288
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002289static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2290{
2291 memcpy(target->cl_verifier.data, source->data,
2292 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002295static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2296{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2298 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2299}
2300
J. Bruce Fields50043852015-11-20 15:58:37 -05002301static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2302{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002303 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2304 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2305 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002306 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2307 if ((source->cr_principal && !target->cr_principal) ||
2308 (source->cr_raw_principal && !target->cr_raw_principal) ||
2309 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002310 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002311
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002312 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 target->cr_uid = source->cr_uid;
2314 target->cr_gid = source->cr_gid;
2315 target->cr_group_info = source->cr_group_info;
2316 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002317 target->cr_gss_mech = source->cr_gss_mech;
2318 if (source->cr_gss_mech)
2319 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002323static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002324compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2325{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002326 if (o1->len < o2->len)
2327 return -1;
2328 if (o1->len > o2->len)
2329 return 1;
2330 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002331}
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002334same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2335{
2336 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
2339static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002340same_clid(clientid_t *cl1, clientid_t *cl2)
2341{
2342 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002345static bool groups_equal(struct group_info *g1, struct group_info *g2)
2346{
2347 int i;
2348
2349 if (g1->ngroups != g2->ngroups)
2350 return false;
2351 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002352 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002353 return false;
2354 return true;
2355}
2356
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002357/*
2358 * RFC 3530 language requires clid_inuse be returned when the
2359 * "principal" associated with a requests differs from that previously
2360 * used. We use uid, gid's, and gss principal string as our best
2361 * approximation. We also don't want to allow non-gss use of a client
2362 * established using gss: in theory cr_principal should catch that
2363 * change, but in practice cr_principal can be null even in the gss case
2364 * since gssd doesn't always pass down a principal string.
2365 */
2366static bool is_gss_cred(struct svc_cred *cr)
2367{
2368 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2369 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2370}
2371
2372
Vivek Trivedi5559b502012-07-24 21:18:20 +05302373static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002374same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2375{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002376 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002377 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2378 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002379 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2380 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002381 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002382 if (cr1->cr_principal == cr2->cr_principal)
2383 return true;
2384 if (!cr1->cr_principal || !cr2->cr_principal)
2385 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302386 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388
J. Bruce Fields57266a62013-04-13 14:27:29 -04002389static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2390{
2391 struct svc_cred *cr = &rqstp->rq_cred;
2392 u32 service;
2393
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002394 if (!cr->cr_gss_mech)
2395 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002396 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2397 return service == RPC_GSS_SVC_INTEGRITY ||
2398 service == RPC_GSS_SVC_PRIVACY;
2399}
2400
Andrew Elblededeb132016-06-15 12:52:08 -04002401bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002402{
2403 struct svc_cred *cr = &rqstp->rq_cred;
2404
2405 if (!cl->cl_mach_cred)
2406 return true;
2407 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2408 return false;
2409 if (!svc_rqst_integrity_protected(rqstp))
2410 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002411 if (cl->cl_cred.cr_raw_principal)
2412 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2413 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002414 if (!cr->cr_principal)
2415 return false;
2416 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2417}
2418
Jeff Layton294ac322014-07-30 08:27:15 -04002419static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002420{
Chuck Leverab4684d2012-03-02 17:13:50 -05002421 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Jeff Laytonf4199922014-06-17 07:44:11 -04002423 /*
2424 * This is opaque to client, so no need to byte-swap. Use
2425 * __force to keep sparse happy
2426 */
Arnd Bergmann9104ae42019-11-04 16:45:30 +01002427 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002428 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002429 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
Jeff Layton294ac322014-07-30 08:27:15 -04002432static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2433{
Arnd Bergmann9cc76802019-10-31 15:53:13 +01002434 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
Jeff Layton294ac322014-07-30 08:27:15 -04002435 clp->cl_clientid.cl_id = nn->clientid_counter++;
2436 gen_confirm(clp, nn);
2437}
2438
Jeff Layton4770d722014-07-29 21:34:10 -04002439static struct nfs4_stid *
2440find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002441{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002442 struct nfs4_stid *ret;
2443
2444 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2445 if (!ret || !ret->sc_type)
2446 return NULL;
2447 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002448}
2449
Jeff Layton4770d722014-07-29 21:34:10 -04002450static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002451find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002452{
2453 struct nfs4_stid *s;
2454
Jeff Layton4770d722014-07-29 21:34:10 -04002455 spin_lock(&cl->cl_lock);
2456 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002457 if (s != NULL) {
2458 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002459 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002460 else
2461 s = NULL;
2462 }
Jeff Layton4770d722014-07-29 21:34:10 -04002463 spin_unlock(&cl->cl_lock);
2464 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002465}
2466
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002467static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2468{
2469 struct nfsdfs_client *nc;
2470 nc = get_nfsdfs_client(inode);
2471 if (!nc)
2472 return NULL;
2473 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2474}
2475
J. Bruce Fields169319f2019-06-19 12:39:46 -04002476static void seq_quote_mem(struct seq_file *m, char *data, int len)
2477{
2478 seq_printf(m, "\"");
Andy Shevchenkoc0546392021-06-30 18:55:43 -07002479 seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
J. Bruce Fields169319f2019-06-19 12:39:46 -04002480 seq_printf(m, "\"");
2481}
2482
Dave Wysochanski3518c862021-06-02 13:51:39 -04002483static const char *cb_state2str(int state)
2484{
2485 switch (state) {
2486 case NFSD4_CB_UP:
2487 return "UP";
2488 case NFSD4_CB_UNKNOWN:
2489 return "UNKNOWN";
2490 case NFSD4_CB_DOWN:
2491 return "DOWN";
2492 case NFSD4_CB_FAULT:
2493 return "FAULT";
2494 }
2495 return "UNDEFINED";
2496}
2497
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002498static int client_info_show(struct seq_file *m, void *v)
2499{
ChenXiaoSong1d7f6b32022-09-23 00:31:54 +08002500 struct inode *inode = file_inode(m->file);
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002501 struct nfs4_client *clp;
2502 u64 clid;
2503
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002504 clp = get_nfsdfs_clp(inode);
2505 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002506 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002507 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2508 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002509 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
Dai Ngoe9488d52022-05-02 14:19:27 -07002510
2511 if (clp->cl_state == NFSD4_COURTESY)
2512 seq_puts(m, "status: courtesy\n");
2513 else if (clp->cl_state == NFSD4_EXPIRABLE)
2514 seq_puts(m, "status: expirable\n");
2515 else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
NeilBrown472d1552021-03-20 09:38:04 +11002516 seq_puts(m, "status: confirmed\n");
2517 else
2518 seq_puts(m, "status: unconfirmed\n");
Dai Ngoe9488d52022-05-02 14:19:27 -07002519 seq_printf(m, "seconds from last renew: %lld\n",
2520 ktime_get_boottime_seconds() - clp->cl_time);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002521 seq_printf(m, "name: ");
2522 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2523 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields79123442019-06-05 12:42:05 -04002524 if (clp->cl_nii_domain.data) {
2525 seq_printf(m, "Implementation domain: ");
2526 seq_quote_mem(m, clp->cl_nii_domain.data,
2527 clp->cl_nii_domain.len);
2528 seq_printf(m, "\nImplementation name: ");
2529 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
Arnd Bergmanne29f4702019-10-31 14:52:43 +01002530 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
J. Bruce Fields79123442019-06-05 12:42:05 -04002531 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2532 }
Dave Wysochanski3518c862021-06-02 13:51:39 -04002533 seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
2534 seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002535 drop_client(clp);
2536
2537 return 0;
2538}
2539
ChenXiaoSong1d7f6b32022-09-23 00:31:54 +08002540DEFINE_SHOW_ATTRIBUTE(client_info);
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002541
J. Bruce Fields78599c42019-04-22 15:26:23 -04002542static void *states_start(struct seq_file *s, loff_t *pos)
2543 __acquires(&clp->cl_lock)
2544{
2545 struct nfs4_client *clp = s->private;
2546 unsigned long id = *pos;
2547 void *ret;
2548
2549 spin_lock(&clp->cl_lock);
2550 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2551 *pos = id;
2552 return ret;
2553}
2554
2555static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2556{
2557 struct nfs4_client *clp = s->private;
2558 unsigned long id = *pos;
2559 void *ret;
2560
2561 id = *pos;
2562 id++;
2563 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2564 *pos = id;
2565 return ret;
2566}
2567
2568static void states_stop(struct seq_file *s, void *v)
2569 __releases(&clp->cl_lock)
2570{
2571 struct nfs4_client *clp = s->private;
2572
2573 spin_unlock(&clp->cl_lock);
2574}
2575
Achilles Gaikwad580da462020-04-20 18:20:31 +05302576static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2577{
2578 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2579}
2580
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002581static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002582{
Chuck Lever427f5f82022-07-08 14:27:09 -04002583 struct inode *inode = file_inode(f->nf_file);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002584
2585 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2586 MAJOR(inode->i_sb->s_dev),
2587 MINOR(inode->i_sb->s_dev),
2588 inode->i_ino);
2589}
2590
2591static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2592{
2593 seq_printf(s, "owner: ");
2594 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2595}
2596
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002597static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2598{
J. Bruce Fieldsee590d22020-04-13 22:03:06 -04002599 seq_printf(s, "0x%.8x", stid->si_generation);
2600 seq_printf(s, "%12phN", &stid->si_opaque);
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002601}
2602
J. Bruce Fields78599c42019-04-22 15:26:23 -04002603static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2604{
2605 struct nfs4_ol_stateid *ols;
2606 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002607 struct nfsd_file *file;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002608 struct nfs4_stateowner *oo;
2609 unsigned int access, deny;
2610
2611 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2612 return 0; /* XXX: or SEQ_SKIP? */
2613 ols = openlockstateid(st);
2614 oo = ols->st_stateowner;
2615 nf = st->sc_file;
2616 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002617 if (!file)
2618 return 0;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002619
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002620 seq_printf(s, "- ");
2621 nfs4_show_stateid(s, &st->sc_stateid);
2622 seq_printf(s, ": { type: open, ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002623
2624 access = bmap_to_share_mode(ols->st_access_bmap);
2625 deny = bmap_to_share_mode(ols->st_deny_bmap);
2626
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002627 seq_printf(s, "access: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002628 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2629 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002630 seq_printf(s, "deny: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002631 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2632 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2633
2634 nfs4_show_superblock(s, file);
2635 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302636 nfs4_show_fname(s, file);
2637 seq_printf(s, ", ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002638 nfs4_show_owner(s, oo);
2639 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002640 nfsd_file_put(file);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002641
2642 return 0;
2643}
2644
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002645static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2646{
2647 struct nfs4_ol_stateid *ols;
2648 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002649 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002650 struct nfs4_stateowner *oo;
2651
2652 ols = openlockstateid(st);
2653 oo = ols->st_stateowner;
2654 nf = st->sc_file;
2655 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002656 if (!file)
2657 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002658
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002659 seq_printf(s, "- ");
2660 nfs4_show_stateid(s, &st->sc_stateid);
2661 seq_printf(s, ": { type: lock, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002662
2663 /*
2664 * Note: a lock stateid isn't really the same thing as a lock,
2665 * it's the locking state held by one owner on a file, and there
2666 * may be multiple (or no) lock ranges associated with it.
2667 * (Same for the matter is true of open stateids.)
2668 */
2669
2670 nfs4_show_superblock(s, file);
2671 /* XXX: open stateid? */
2672 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302673 nfs4_show_fname(s, file);
2674 seq_printf(s, ", ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002675 nfs4_show_owner(s, oo);
2676 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002677 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002678
2679 return 0;
2680}
2681
2682static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2683{
2684 struct nfs4_delegation *ds;
2685 struct nfs4_file *nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002686 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002687
2688 ds = delegstateid(st);
2689 nf = st->sc_file;
J. Bruce Fields9affa432020-07-15 13:31:36 -04002690 file = find_deleg_file(nf);
2691 if (!file)
2692 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002693
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002694 seq_printf(s, "- ");
2695 nfs4_show_stateid(s, &st->sc_stateid);
2696 seq_printf(s, ": { type: deleg, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002697
2698 /* Kinda dead code as long as we only support read delegs: */
2699 seq_printf(s, "access: %s, ",
2700 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2701
2702 /* XXX: lease time, whether it's being recalled. */
2703
2704 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302705 seq_printf(s, ", ");
2706 nfs4_show_fname(s, file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002707 seq_printf(s, " }\n");
J. Bruce Fields9affa432020-07-15 13:31:36 -04002708 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002709
2710 return 0;
2711}
2712
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002713static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2714{
2715 struct nfs4_layout_stateid *ls;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002716 struct nfsd_file *file;
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002717
2718 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2719 file = ls->ls_file;
2720
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002721 seq_printf(s, "- ");
2722 nfs4_show_stateid(s, &st->sc_stateid);
2723 seq_printf(s, ": { type: layout, ");
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002724
2725 /* XXX: What else would be useful? */
2726
2727 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302728 seq_printf(s, ", ");
2729 nfs4_show_fname(s, file);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002730 seq_printf(s, " }\n");
2731
2732 return 0;
2733}
2734
J. Bruce Fields78599c42019-04-22 15:26:23 -04002735static int states_show(struct seq_file *s, void *v)
2736{
2737 struct nfs4_stid *st = v;
2738
2739 switch (st->sc_type) {
2740 case NFS4_OPEN_STID:
2741 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002742 case NFS4_LOCK_STID:
2743 return nfs4_show_lock(s, st);
2744 case NFS4_DELEG_STID:
2745 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002746 case NFS4_LAYOUT_STID:
2747 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002748 default:
2749 return 0; /* XXX: or SEQ_SKIP? */
2750 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002751 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002752}
2753
2754static struct seq_operations states_seq_ops = {
2755 .start = states_start,
2756 .next = states_next,
2757 .stop = states_stop,
2758 .show = states_show
2759};
2760
2761static int client_states_open(struct inode *inode, struct file *file)
2762{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002763 struct seq_file *s;
2764 struct nfs4_client *clp;
2765 int ret;
2766
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002767 clp = get_nfsdfs_clp(inode);
2768 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002769 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002770
2771 ret = seq_open(file, &states_seq_ops);
2772 if (ret)
2773 return ret;
2774 s = file->private_data;
2775 s->private = clp;
2776 return 0;
2777}
2778
2779static int client_opens_release(struct inode *inode, struct file *file)
2780{
2781 struct seq_file *m = file->private_data;
2782 struct nfs4_client *clp = m->private;
2783
2784 /* XXX: alternatively, we could get/drop in seq start/stop */
2785 drop_client(clp);
2786 return 0;
2787}
2788
2789static const struct file_operations client_states_fops = {
2790 .open = client_states_open,
2791 .read = seq_read,
2792 .llseek = seq_lseek,
2793 .release = client_opens_release,
2794};
2795
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002796/*
2797 * Normally we refuse to destroy clients that are in use, but here the
2798 * administrator is telling us to just do it. We also want to wait
2799 * so the caller has a guarantee that the client's locks are gone by
2800 * the time the write returns:
2801 */
YueHaibing297e57a2019-07-08 15:29:33 +08002802static void force_expire_client(struct nfs4_client *clp)
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002803{
2804 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2805 bool already_expired;
2806
Chuck Lever2958d2e2021-05-14 15:56:06 -04002807 trace_nfsd_clid_admin_expired(&clp->cl_clientid);
2808
J. Bruce Fieldsf7104cc2021-08-12 16:41:43 -04002809 spin_lock(&nn->client_lock);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002810 clp->cl_time = 0;
J. Bruce Fieldsf7104cc2021-08-12 16:41:43 -04002811 spin_unlock(&nn->client_lock);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002812
2813 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2814 spin_lock(&nn->client_lock);
2815 already_expired = list_empty(&clp->cl_lru);
2816 if (!already_expired)
2817 unhash_client_locked(clp);
2818 spin_unlock(&nn->client_lock);
2819
2820 if (!already_expired)
2821 expire_client(clp);
2822 else
2823 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2824}
2825
2826static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2827 size_t size, loff_t *pos)
2828{
2829 char *data;
2830 struct nfs4_client *clp;
2831
2832 data = simple_transaction_get(file, buf, size);
2833 if (IS_ERR(data))
2834 return PTR_ERR(data);
2835 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2836 return -EINVAL;
2837 clp = get_nfsdfs_clp(file_inode(file));
2838 if (!clp)
2839 return -ENXIO;
2840 force_expire_client(clp);
2841 drop_client(clp);
2842 return 7;
2843}
2844
2845static const struct file_operations client_ctl_fops = {
2846 .write = client_ctl_write,
2847 .release = simple_transaction_release,
2848};
2849
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002850static const struct tree_descr client_files[] = {
2851 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002852 [1] = {"states", &client_states_fops, S_IRUSR},
Petr Vorel6cbfad52020-03-13 13:39:57 +01002853 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002854 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002855};
2856
Jeff Layton2216d442012-11-12 15:00:57 -05002857static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002858 struct svc_rqst *rqstp, nfs4_verifier *verf)
2859{
2860 struct nfs4_client *clp;
2861 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002862 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002863 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002864 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
NeilBrown472d1552021-03-20 09:38:04 +11002865 struct dentry *dentries[ARRAY_SIZE(client_files)];
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002866
Dai Ngo0926c3952022-07-15 16:54:52 -07002867 clp = alloc_client(name, nn);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002868 if (clp == NULL)
2869 return NULL;
2870
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002871 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2872 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002873 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002874 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002875 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002876 gen_clid(clp, nn);
2877 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002878 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01002879 clp->cl_time = ktime_get_boottime_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002880 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002881 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002882 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002883 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002884 clp->net = net;
NeilBrown472d1552021-03-20 09:38:04 +11002885 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2886 nn, &clp->cl_nfsdfs,
2887 clp->cl_clientid.cl_id - nn->clientid_base,
2888 client_files, dentries);
2889 clp->cl_nfsd_info_dentry = dentries[0];
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002890 if (!clp->cl_nfsd_dentry) {
2891 free_client(clp);
2892 return NULL;
2893 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002894 return clp;
2895}
2896
NeilBrownfd39ca92005-06-23 22:04:03 -07002897static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002898add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2899{
2900 struct rb_node **new = &(root->rb_node), *parent = NULL;
2901 struct nfs4_client *clp;
2902
2903 while (*new) {
2904 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2905 parent = *new;
2906
2907 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2908 new = &((*new)->rb_left);
2909 else
2910 new = &((*new)->rb_right);
2911 }
2912
2913 rb_link_node(&new_clp->cl_namenode, parent, new);
2914 rb_insert_color(&new_clp->cl_namenode, root);
2915}
2916
2917static struct nfs4_client *
2918find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2919{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002920 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002921 struct rb_node *node = root->rb_node;
2922 struct nfs4_client *clp;
2923
2924 while (node) {
2925 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2926 cmp = compare_blob(&clp->cl_name, name);
2927 if (cmp > 0)
2928 node = node->rb_left;
2929 else if (cmp < 0)
2930 node = node->rb_right;
2931 else
2932 return clp;
2933 }
2934 return NULL;
2935}
2936
2937static void
2938add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
2940 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002941 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Trond Myklebust0a880a22014-07-30 08:27:10 -04002943 lockdep_assert_held(&nn->client_lock);
2944
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002945 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002946 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002948 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002949 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
2951
NeilBrownfd39ca92005-06-23 22:04:03 -07002952static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953move_to_confirmed(struct nfs4_client *clp)
2954{
2955 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002956 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Trond Myklebust0a880a22014-07-30 08:27:10 -04002958 lockdep_assert_held(&nn->client_lock);
2959
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002960 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002961 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002962 add_clp_to_name_tree(clp, &nn->conf_name_tree);
J. Bruce Fields934bd072021-05-25 14:53:44 -04002963 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2964 trace_nfsd_clid_confirmed(&clp->cl_clientid);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002965 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966}
2967
2968static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002969find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
2971 struct nfs4_client *clp;
2972 unsigned int idhashval = clientid_hashval(clid->cl_id);
2973
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002974 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002975 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002976 if ((bool)clp->cl_minorversion != sessions)
2977 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002978 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
2982 return NULL;
2983}
2984
2985static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002986find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2987{
2988 struct list_head *tbl = nn->conf_id_hashtbl;
2989
Trond Myklebust0a880a22014-07-30 08:27:10 -04002990 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002991 return find_client_in_id_table(tbl, clid, sessions);
2992}
2993
2994static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002995find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002997 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Trond Myklebust0a880a22014-07-30 08:27:10 -04002999 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04003000 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05003003static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03003004{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05003005 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003006}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03003007
NeilBrown28ce6052005-06-23 22:03:56 -07003008static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003009find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07003010{
Trond Myklebust0a880a22014-07-30 08:27:10 -04003011 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003012 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07003013}
3014
3015static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003016find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07003017{
Trond Myklebust0a880a22014-07-30 08:27:10 -04003018 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003019 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07003020}
3021
NeilBrownfd39ca92005-06-23 22:04:03 -07003022static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003023gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
J. Bruce Fields07263f12010-05-31 19:09:40 -04003025 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003026 struct sockaddr *sa = svc_addr(rqstp);
3027 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04003028 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Jeff Layton7077ecb2009-08-14 12:57:58 -04003030 /* Currently, we only support tcp and tcp6 for the callback channel */
3031 if (se->se_callback_netid_len == 3 &&
3032 !memcmp(se->se_callback_netid_val, "tcp", 3))
3033 expected_family = AF_INET;
3034 else if (se->se_callback_netid_len == 4 &&
3035 !memcmp(se->se_callback_netid_val, "tcp6", 4))
3036 expected_family = AF_INET6;
3037 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 goto out_err;
3039
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003040 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04003041 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04003042 (struct sockaddr *)&conn->cb_addr,
3043 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04003044
J. Bruce Fields07263f12010-05-31 19:09:40 -04003045 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04003047
J. Bruce Fields07263f12010-05-31 19:09:40 -04003048 if (conn->cb_addr.ss_family == AF_INET6)
3049 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04003050
J. Bruce Fields07263f12010-05-31 19:09:40 -04003051 conn->cb_prog = se->se_callback_prog;
3052 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08003053 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Chuck Lever1eace0d2020-04-05 14:15:29 -04003054 trace_nfsd_cb_args(clp, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 return;
3056out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04003057 conn->cb_addr.ss_family = AF_UNSPEC;
3058 conn->cb_addrlen = 0;
Chuck Lever1eace0d2020-04-05 14:15:29 -04003059 trace_nfsd_cb_nodelegs(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 return;
3061}
3062
Andy Adamson074fe892009-04-03 08:28:15 +03003063/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04003064 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03003065 */
Trond Myklebustb6076642014-06-30 11:48:35 -04003066static void
Andy Adamson074fe892009-04-03 08:28:15 +03003067nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3068{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003069 struct xdr_buf *buf = resp->xdr->buf;
Andy Adamson557ce262009-08-28 08:45:04 -04003070 struct nfsd4_slot *slot = resp->cstate.slot;
3071 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03003072
Andy Adamson557ce262009-08-28 08:45:04 -04003073 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03003074
J. Bruce Fields085def32017-10-18 16:17:18 -04003075 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04003076 slot->sl_opcnt = resp->opcnt;
3077 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003078 free_svc_cred(&slot->sl_cred);
3079 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03003080
J. Bruce Fields085def32017-10-18 16:17:18 -04003081 if (!nfsd4_cache_this(resp)) {
3082 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03003083 return;
3084 }
J. Bruce Fields085def32017-10-18 16:17:18 -04003085 slot->sl_flags |= NFSD4_SLOT_CACHED;
3086
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003087 base = resp->cstate.data_offset;
3088 slot->sl_datalen = buf->len - base;
3089 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03003090 WARN(1, "%s: sessions DRC could not cache compound\n",
3091 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04003092 return;
Andy Adamson074fe892009-04-03 08:28:15 +03003093}
3094
3095/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04003096 * Encode the replay sequence operation from the slot values.
3097 * If cachethis is FALSE encode the uncached rep error on the next
3098 * operation which sets resp->p and increments resp->opcnt for
3099 * nfs4svc_encode_compoundres.
3100 *
Andy Adamson074fe892009-04-03 08:28:15 +03003101 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04003102static __be32
3103nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3104 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03003105{
Andy Adamsonabfabf82009-07-23 19:02:18 -04003106 struct nfsd4_op *op;
3107 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03003108
Andy Adamsonabfabf82009-07-23 19:02:18 -04003109 /* Encode the replayed sequence operation */
3110 op = &args->ops[resp->opcnt - 1];
3111 nfsd4_encode_operation(resp, op);
3112
J. Bruce Fields085def32017-10-18 16:17:18 -04003113 if (slot->sl_flags & NFSD4_SLOT_CACHED)
3114 return op->status;
3115 if (args->opcnt == 1) {
3116 /*
3117 * The original operation wasn't a solo sequence--we
3118 * always cache those--so this retry must not match the
3119 * original:
3120 */
3121 op->status = nfserr_seq_false_retry;
3122 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04003123 op = &args->ops[resp->opcnt++];
3124 op->status = nfserr_retry_uncached_rep;
3125 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03003126 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04003127 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03003128}
3129
3130/*
Andy Adamson557ce262009-08-28 08:45:04 -04003131 * The sequence operation is not cached because we can use the slot and
3132 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03003133 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04003134static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03003135nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3136 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03003137{
Andy Adamson557ce262009-08-28 08:45:04 -04003138 struct nfsd4_slot *slot = resp->cstate.slot;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003139 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003140 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03003141 __be32 status;
3142
Andy Adamson557ce262009-08-28 08:45:04 -04003143 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03003144
Andy Adamsonabfabf82009-07-23 19:02:18 -04003145 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04003146 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04003147 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03003148
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003149 p = xdr_reserve_space(xdr, slot->sl_datalen);
3150 if (!p) {
3151 WARN_ON_ONCE(1);
3152 return nfserr_serverfault;
3153 }
3154 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3155 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03003156
Andy Adamson557ce262009-08-28 08:45:04 -04003157 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003158 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03003159}
3160
Andy Adamson0733d212009-04-03 08:28:01 +03003161/*
3162 * Set the exchange_id flags returned by the server.
3163 */
3164static void
3165nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3166{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003167#ifdef CONFIG_NFSD_PNFS
3168 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3169#else
Andy Adamson0733d212009-04-03 08:28:01 +03003170 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003171#endif
Andy Adamson0733d212009-04-03 08:28:01 +03003172
3173 /* Referrals are supported, Migration is not. */
3174 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3175
3176 /* set the wire flags to return to client. */
3177 clid->flags = new->cl_exchange_flags;
3178}
3179
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003180static bool client_has_openowners(struct nfs4_client *clp)
3181{
3182 struct nfs4_openowner *oo;
3183
3184 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3185 if (!list_empty(&oo->oo_owner.so_stateids))
3186 return true;
3187 }
3188 return false;
3189}
3190
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003191static bool client_has_state(struct nfs4_client *clp)
3192{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003193 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08003194#ifdef CONFIG_NFSD_PNFS
3195 || !list_empty(&clp->cl_lo_states)
3196#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04003197 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04003198 || !list_empty(&clp->cl_sessions)
3199 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003200}
3201
J. Bruce Fields79123442019-06-05 12:42:05 -04003202static __be32 copy_impl_id(struct nfs4_client *clp,
3203 struct nfsd4_exchange_id *exid)
3204{
3205 if (!exid->nii_domain.data)
3206 return 0;
3207 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3208 if (!clp->cl_nii_domain.data)
3209 return nfserr_jukebox;
3210 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3211 if (!clp->cl_nii_name.data)
3212 return nfserr_jukebox;
Arnd Bergmanne29f4702019-10-31 14:52:43 +01003213 clp->cl_nii_time = exid->nii_time;
J. Bruce Fields79123442019-06-05 12:42:05 -04003214 return 0;
3215}
3216
Al Virob37ad282006-10-19 23:28:59 -07003217__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003218nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3219 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003220{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003221 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003222 struct nfs4_client *conf, *new;
3223 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003224 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04003225 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03003226 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04003227 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003228 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003229 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03003230
Jeff Layton363168b2009-08-14 12:57:56 -04003231 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03003232 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Chuck Lever523ec6e2020-11-02 15:19:12 -05003233 "ip_addr=%s flags %x, spa_how %u\n",
Andy Adamson0733d212009-04-03 08:28:01 +03003234 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04003235 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03003236
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003237 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03003238 return nfserr_inval;
3239
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003240 new = create_client(exid->clname, rqstp, &verf);
3241 if (new == NULL)
3242 return nfserr_jukebox;
J. Bruce Fields79123442019-06-05 12:42:05 -04003243 status = copy_impl_id(new, exid);
3244 if (status)
3245 goto out_nolock;
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003246
Andy Adamson0733d212009-04-03 08:28:01 +03003247 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003248 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04003249 exid->spo_must_enforce[0] = 0;
3250 exid->spo_must_enforce[1] = (
3251 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3252 1 << (OP_EXCHANGE_ID - 32) |
3253 1 << (OP_CREATE_SESSION - 32) |
3254 1 << (OP_DESTROY_SESSION - 32) |
3255 1 << (OP_DESTROY_CLIENTID - 32));
3256
3257 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3258 1 << (OP_OPEN_DOWNGRADE) |
3259 1 << (OP_LOCKU) |
3260 1 << (OP_DELEGRETURN));
3261
3262 exid->spo_must_allow[1] &= (
3263 1 << (OP_TEST_STATEID - 32) |
3264 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003265 if (!svc_rqst_integrity_protected(rqstp)) {
3266 status = nfserr_inval;
3267 goto out_nolock;
3268 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003269 /*
3270 * Sometimes userspace doesn't give us a principal.
3271 * Which is a bug, really. Anyway, we can't enforce
3272 * MACH_CRED in that case, better to give up now:
3273 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05003274 if (!new->cl_cred.cr_principal &&
3275 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003276 status = nfserr_serverfault;
3277 goto out_nolock;
3278 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003279 new->cl_mach_cred = true;
Gustavo A. R. Silva76c50eb2020-11-20 12:26:40 -06003280 break;
Andy Adamson0733d212009-04-03 08:28:01 +03003281 case SP4_NONE:
3282 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003283 default: /* checked by xdr code */
3284 WARN_ON_ONCE(1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003285 fallthrough;
Andy Adamson0733d212009-04-03 08:28:01 +03003286 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08003287 status = nfserr_encr_alg_unsupp;
3288 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03003289 }
3290
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003291 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003292 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003293 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03003294 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003295 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3296 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3297
J. Bruce Fields136e6582012-05-12 20:37:23 -04003298 if (update) {
3299 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003300 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03003301 goto out;
3302 }
Andrew Elblededeb132016-06-15 12:52:08 -04003303 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003304 status = nfserr_wrong_cred;
3305 goto out;
3306 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003307 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03003308 status = nfserr_perm;
3309 goto out;
3310 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003311 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03003312 status = nfserr_not_same;
3313 goto out;
3314 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003315 /* case 6 */
3316 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
Chuck Levere8f80c52021-05-14 15:56:19 -04003317 trace_nfsd_clid_confirmed_r(conf);
J. Bruce Fields136e6582012-05-12 20:37:23 -04003318 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003319 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003320 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003321 if (client_has_state(conf)) {
3322 status = nfserr_clid_inuse;
Chuck Lever27787732021-05-14 15:55:29 -04003323 trace_nfsd_clid_cred_mismatch(conf, rqstp);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003324 goto out;
3325 }
Andy Adamson0733d212009-04-03 08:28:01 +03003326 goto out_new;
3327 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003328 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003329 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
Chuck Levere8f80c52021-05-14 15:56:19 -04003330 trace_nfsd_clid_confirmed_r(conf);
J. Bruce Fields136e6582012-05-12 20:37:23 -04003331 goto out_copy;
3332 }
3333 /* case 5, client reboot */
Chuck Lever744ea542021-05-14 15:55:36 -04003334 trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003335 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003336 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003337 }
3338
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003339 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003340 status = nfserr_noent;
3341 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003342 }
3343
Chuck Levere8f80c52021-05-14 15:56:19 -04003344 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003345 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003346 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003347
Chuck Levere8f80c52021-05-14 15:56:19 -04003348 /* case 1, new owner ID */
3349 trace_nfsd_clid_fresh(new);
3350
Andy Adamson0733d212009-04-03 08:28:01 +03003351out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003352 if (conf) {
3353 status = mark_client_expired_locked(conf);
3354 if (status)
3355 goto out;
Chuck Lever2958d2e2021-05-14 15:56:06 -04003356 trace_nfsd_clid_replaced(&conf->cl_clientid);
Jeff Laytonfd699b82014-07-30 08:27:14 -04003357 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003358 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003359 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3360 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003361
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003362 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003363 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003364out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003365 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3366 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003367
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003368 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3369 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003370
3371 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003372 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003373 status = nfs_ok;
3374
3375out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003376 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003377out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003378 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003379 expire_client(new);
Chuck Levere8f80c52021-05-14 15:56:19 -04003380 if (unconf) {
3381 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003382 expire_client(unconf);
Chuck Levere8f80c52021-05-14 15:56:19 -04003383 }
Andy Adamson0733d212009-04-03 08:28:01 +03003384 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003385}
3386
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003387static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003388check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003389{
Andy Adamson88e588d2009-07-23 19:02:15 -04003390 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3391 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003392
3393 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003394 if (slot_inuse) {
3395 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003396 return nfserr_jukebox;
3397 else
3398 return nfserr_seq_misordered;
3399 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003400 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003401 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003402 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003403 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003404 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003405 return nfserr_seq_misordered;
3406}
3407
Andy Adamson49557cc2009-07-23 19:02:16 -04003408/*
3409 * Cache the create session result into the create session single DRC
3410 * slot cache by saving the xdr structure. sl_seqid has been set.
3411 * Do this for solo or embedded create session operations.
3412 */
3413static void
3414nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003415 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003416{
3417 slot->sl_status = nfserr;
3418 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3419}
3420
3421static __be32
3422nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3423 struct nfsd4_clid_slot *slot)
3424{
3425 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3426 return slot->sl_status;
3427}
3428
Mi Jinlong1b74c252011-07-14 14:50:17 +08003429#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3430 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3431 1 + /* MIN tag is length with zero, only length */ \
3432 3 + /* version, opcount, opcode */ \
3433 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3434 /* seqid, slotID, slotID, cache */ \
3435 4 ) * sizeof(__be32))
3436
3437#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3438 2 + /* verifier: AUTH_NULL, length 0 */\
3439 1 + /* status */ \
3440 1 + /* MIN tag is length with zero, only length */ \
3441 3 + /* opcount, opcode, opstatus*/ \
3442 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3443 /* seqid, slotID, slotID, slotID, status */ \
3444 5 ) * sizeof(__be32))
3445
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003446static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003447{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003448 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3449
J. Bruce Fields373cd402013-04-08 15:42:12 -04003450 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3451 return nfserr_toosmall;
3452 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3453 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003454 ca->headerpadsz = 0;
3455 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3456 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3457 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3458 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3459 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3460 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3461 /*
3462 * Note decreasing slot size below client's request may make it
3463 * difficult for client to function correctly, whereas
3464 * decreasing the number of slots will (just?) affect
3465 * performance. When short on memory we therefore prefer to
3466 * decrease number of slots instead of their size. Clients that
3467 * request larger slots than they need will get poor results:
NeilBrown7f49fd5d2019-09-20 16:33:16 +10003468 * Note that we always allow at least one slot, because our
3469 * accounting is soft and provides no guarantees either way.
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003470 */
NeilBrown2030ca52019-09-20 16:36:45 +10003471 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003472
J. Bruce Fields373cd402013-04-08 15:42:12 -04003473 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003474}
3475
Chuck Lever45006322016-03-01 13:06:02 -05003476/*
3477 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3478 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3479 */
3480#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3481 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3482
3483#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3484 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3485
Kinglong Mee8a891632013-12-23 18:11:02 +08003486#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003487 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003488#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003489 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3490 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003491
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003492static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3493{
3494 ca->headerpadsz = 0;
3495
Kinglong Mee8a891632013-12-23 18:11:02 +08003496 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003497 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003498 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003499 return nfserr_toosmall;
3500 ca->maxresp_cached = 0;
3501 if (ca->maxops < 2)
3502 return nfserr_toosmall;
3503
3504 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003505}
3506
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003507static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3508{
3509 switch (cbs->flavor) {
3510 case RPC_AUTH_NULL:
3511 case RPC_AUTH_UNIX:
3512 return nfs_ok;
3513 default:
3514 /*
3515 * GSS case: the spec doesn't allow us to return this
3516 * error. But it also doesn't allow us not to support
3517 * GSS.
3518 * I'd rather this fail hard than return some error the
3519 * client might think it can already handle:
3520 */
3521 return nfserr_encr_alg_unsupp;
3522 }
3523}
3524
Andy Adamson069b6ad2009-04-03 08:27:58 +03003525__be32
3526nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003527 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003528{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003529 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003530 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003531 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003532 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003533 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003534 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003535 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003536 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003537 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003538
Mi Jinlonga62573d2011-03-23 17:57:07 +08003539 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3540 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003541 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3542 if (status)
3543 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003544 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003545 if (status)
3546 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003547 status = check_backchannel_attrs(&cr_ses->back_channel);
3548 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003549 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003550 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003551 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003552 if (!new)
3553 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003554 conn = alloc_conn_from_crses(rqstp, cr_ses);
3555 if (!conn)
3556 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003557
Jeff Laytond20c11d2014-07-30 08:27:07 -04003558 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003559 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003560 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003561 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003562
3563 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003564 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003565 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003566 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003567 cs_slot = &conf->cl_cs_slot;
3568 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003569 if (status) {
3570 if (status == nfserr_replay_cache)
3571 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003572 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003573 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003574 } else if (unconf) {
Chuck Lever27787732021-05-14 15:55:29 -04003575 status = nfserr_clid_inuse;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003576 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003577 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Chuck Lever27787732021-05-14 15:55:29 -04003578 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003579 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003580 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003581 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003582 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003583 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003584 cs_slot = &unconf->cl_cs_slot;
3585 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003586 if (status) {
3587 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003588 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003589 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003590 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003591 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003592 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003593 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003594 if (status) {
3595 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003596 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003597 }
Chuck Lever2958d2e2021-05-14 15:56:06 -04003598 trace_nfsd_clid_replaced(&old->cl_clientid);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003599 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003600 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003601 conf = unconf;
3602 } else {
3603 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003604 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003605 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003606 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003607 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003608 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003609 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003610 cr_ses->flags &= ~SESSION4_RDMA;
3611
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003612 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003613 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003614
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003615 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003616 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003617 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003618 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003619
Jeff Laytond20c11d2014-07-30 08:27:07 -04003620 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003621 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003622 spin_unlock(&nn->client_lock);
J. Bruce Fields934bd072021-05-25 14:53:44 -04003623 if (conf == unconf)
3624 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003625 /* init connection and backchannel */
3626 nfsd4_init_conn(rqstp, conn, new);
3627 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003628 if (old)
3629 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003630 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003631out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003632 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003633 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003634 if (old)
3635 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003636out_free_session:
3637 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003638out_release_drc_mem:
3639 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003640 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003641}
3642
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003643static __be32 nfsd4_map_bcts_dir(u32 *dir)
3644{
3645 switch (*dir) {
3646 case NFS4_CDFC4_FORE:
3647 case NFS4_CDFC4_BACK:
3648 return nfs_ok;
3649 case NFS4_CDFC4_FORE_OR_BOTH:
3650 case NFS4_CDFC4_BACK_OR_BOTH:
3651 *dir = NFS4_CDFC4_BOTH;
3652 return nfs_ok;
zhengbinfc5fc5d2019-12-19 17:29:20 +08003653 }
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003654 return nfserr_inval;
3655}
3656
Christoph Hellwigeb698532017-05-08 20:58:35 +02003657__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3658 struct nfsd4_compound_state *cstate,
3659 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003660{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003661 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003662 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003663 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003664 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003665
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003666 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3667 if (status)
3668 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003669 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003670 session->se_cb_prog = bc->bc_cb_program;
3671 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003672 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003673
3674 nfsd4_probe_callback(session->se_client);
3675
3676 return nfs_ok;
3677}
3678
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003679static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3680{
3681 struct nfsd4_conn *c;
3682
3683 list_for_each_entry(c, &s->se_conns, cn_persession) {
3684 if (c->cn_xprt == xpt) {
3685 return c;
3686 }
3687 }
3688 return NULL;
3689}
3690
3691static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
Dai Ngo02579b22021-09-16 14:22:12 -04003692 struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003693{
3694 struct nfs4_client *clp = session->se_client;
3695 struct svc_xprt *xpt = rqst->rq_xprt;
3696 struct nfsd4_conn *c;
3697 __be32 status;
3698
3699 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3700 spin_lock(&clp->cl_lock);
3701 c = __nfsd4_find_conn(xpt, session);
3702 if (!c)
3703 status = nfserr_noent;
3704 else if (req == c->cn_flags)
3705 status = nfs_ok;
3706 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3707 c->cn_flags != NFS4_CDFC4_BACK)
3708 status = nfs_ok;
3709 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3710 c->cn_flags != NFS4_CDFC4_FORE)
3711 status = nfs_ok;
3712 else
3713 status = nfserr_inval;
3714 spin_unlock(&clp->cl_lock);
Dai Ngo02579b22021-09-16 14:22:12 -04003715 if (status == nfs_ok && conn)
3716 *conn = c;
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003717 return status;
3718}
3719
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003720__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3721 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003722 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003723{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003724 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003725 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003726 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003727 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003728 struct net *net = SVC_NET(rqstp);
3729 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003730
3731 if (!nfsd4_last_compound_op(rqstp))
3732 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003733 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003734 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003735 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003736 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003737 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003738 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003739 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003740 goto out;
Dai Ngo02579b22021-09-16 14:22:12 -04003741 status = nfsd4_match_existing_connection(rqstp, session,
3742 bcts->dir, &conn);
3743 if (status == nfs_ok) {
3744 if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
3745 bcts->dir == NFS4_CDFC4_BACK)
3746 conn->cn_flags |= NFS4_CDFC4_BACK;
3747 nfsd4_probe_callback(session->se_client);
3748 goto out;
3749 }
3750 if (status == nfserr_inval)
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003751 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003752 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003753 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003754 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003755 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003756 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003757 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003758 goto out;
3759 nfsd4_init_conn(rqstp, conn, session);
3760 status = nfs_ok;
3761out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003762 nfsd4_put_session(session);
3763out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003764 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003765}
3766
J. Bruce Fields665d5072018-04-11 16:53:36 -04003767static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003768{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003769 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003770 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003771 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003772}
3773
Andy Adamson069b6ad2009-04-03 08:27:58 +03003774__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003775nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3776 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003777{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003778 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003779 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003780 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003781 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003782 struct net *net = SVC_NET(r);
3783 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003784
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003785 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003786 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003787 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003788 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003789 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003790 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003791 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003792 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003793 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003794 if (!ses)
3795 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003796 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003797 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003798 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003799 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003800 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003801 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003802 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003803 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003804
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003805 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003806
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003807 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003808 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003809out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003810 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003811out_client_lock:
3812 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003813out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003814 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003815}
3816
J. Bruce Fields57266a62013-04-13 14:27:29 -04003817static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003818{
3819 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003820 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003821 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003822 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003823
3824 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003825 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003826 if (c)
3827 goto out_free;
3828 status = nfserr_conn_not_bound_to_session;
3829 if (clp->cl_mach_cred)
3830 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003831 __nfsd4_hash_conn(new, ses);
3832 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003833 ret = nfsd4_register_conn(new);
3834 if (ret)
3835 /* oops; xprt is already down: */
3836 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003837 return nfs_ok;
3838out_free:
3839 spin_unlock(&clp->cl_lock);
3840 free_conn(new);
3841 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003842}
3843
Mi Jinlong868b89c2011-04-27 09:09:58 +08003844static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3845{
3846 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3847
3848 return args->opcnt > session->se_fchannel.maxops;
3849}
3850
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003851static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3852 struct nfsd4_session *session)
3853{
3854 struct xdr_buf *xb = &rqstp->rq_arg;
3855
3856 return xb->len > session->se_fchannel.maxreq_sz;
3857}
3858
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003859static bool replay_matches_cache(struct svc_rqst *rqstp,
3860 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3861{
3862 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3863
3864 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3865 (bool)seq->cachethis)
3866 return false;
3867 /*
Scott Mayhew6e73e922019-10-09 15:11:37 -04003868 * If there's an error then the reply can have fewer ops than
3869 * the call.
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003870 */
Scott Mayhew6e73e922019-10-09 15:11:37 -04003871 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3872 return false;
3873 /*
3874 * But if we cached a reply with *more* ops than the call you're
3875 * sending us now, then this new call is clearly not really a
3876 * replay of the old one:
3877 */
3878 if (slot->sl_opcnt > argp->opcnt)
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003879 return false;
3880 /* This is the only check explicitly called by spec: */
3881 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3882 return false;
3883 /*
3884 * There may be more comparisons we could actually do, but the
3885 * spec doesn't require us to catch every case where the calls
3886 * don't match (that would require caching the call as well as
3887 * the reply), so we don't bother.
3888 */
3889 return true;
3890}
3891
Andy Adamson069b6ad2009-04-03 08:27:58 +03003892__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003893nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3894 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003895{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003896 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003897 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003898 struct xdr_stream *xdr = resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003899 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003900 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003901 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003902 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003903 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003904 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003905 struct net *net = SVC_NET(rqstp);
3906 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003907
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003908 if (resp->opcnt != 1)
3909 return nfserr_sequence_pos;
3910
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003911 /*
3912 * Will be either used or freed by nfsd4_sequence_check_conn
3913 * below.
3914 */
3915 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3916 if (!conn)
3917 return nfserr_jukebox;
3918
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003919 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003920 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003921 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003922 goto out_no_session;
3923 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003924
Mi Jinlong868b89c2011-04-27 09:09:58 +08003925 status = nfserr_too_many_ops;
3926 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003927 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003928
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003929 status = nfserr_req_too_big;
3930 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003931 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003932
Benny Halevyb85d4c02009-04-03 08:28:08 +03003933 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003934 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003935 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003936
Andy Adamson557ce262009-08-28 08:45:04 -04003937 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003938 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3939
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003940 /* We do not negotiate the number of slots yet, so set the
3941 * maxslots to the session maxreqs which is used to encode
3942 * sr_highest_slotid and the sr_target_slot id to maxslots */
3943 seq->maxslots = session->se_fchannel.maxreqs;
3944
J. Bruce Fields73e79482012-02-13 16:39:00 -05003945 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3946 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003947 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003948 status = nfserr_seq_misordered;
3949 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003950 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003951 status = nfserr_seq_false_retry;
3952 if (!replay_matches_cache(rqstp, seq, slot))
3953 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003954 cstate->slot = slot;
3955 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003956 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003957 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003958 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003959 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003960 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003961 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003962 }
3963 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003964 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003965
J. Bruce Fields57266a62013-04-13 14:27:29 -04003966 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003967 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003968 if (status)
3969 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003970
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003971 buflen = (seq->cachethis) ?
3972 session->se_fchannel.maxresp_cached :
3973 session->se_fchannel.maxresp_sz;
3974 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3975 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003976 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003977 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003978 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003979
3980 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003981 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003982 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003983 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003984 if (seq->cachethis)
3985 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003986 else
3987 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003988
3989 cstate->slot = slot;
3990 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003991 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003992
Benny Halevyb85d4c02009-04-03 08:28:08 +03003993out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003994 switch (clp->cl_cb_state) {
3995 case NFSD4_CB_DOWN:
3996 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3997 break;
3998 case NFSD4_CB_FAULT:
3999 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
4000 break;
4001 default:
4002 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03004003 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04004004 if (!list_empty(&clp->cl_revoked))
4005 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04004006out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08004007 if (conn)
4008 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03004009 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03004010 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04004011out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04004012 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04004013 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03004014}
4015
Trond Myklebustb6076642014-06-30 11:48:35 -04004016void
4017nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4018{
4019 struct nfsd4_compound_state *cs = &resp->cstate;
4020
4021 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04004022 if (cs->status != nfserr_replay_cache) {
4023 nfsd4_store_cache_entry(resp);
4024 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4025 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04004026 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04004027 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004028 } else if (cs->clp)
4029 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04004030}
4031
Mi Jinlong345c2842011-10-20 17:51:39 +08004032__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02004033nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4034 struct nfsd4_compound_state *cstate,
4035 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08004036{
Christoph Hellwigeb698532017-05-08 20:58:35 +02004037 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04004038 struct nfs4_client *conf, *unconf;
4039 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004040 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004041 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08004042
Trond Myklebust6b10ad12014-07-30 08:27:08 -04004043 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004044 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004045 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04004046 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08004047
4048 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04004049 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08004050 status = nfserr_clientid_busy;
4051 goto out;
4052 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04004053 status = mark_client_expired_locked(conf);
4054 if (status)
4055 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04004056 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08004057 } else if (unconf)
4058 clp = unconf;
4059 else {
4060 status = nfserr_stale_clientid;
4061 goto out;
4062 }
Andrew Elblededeb132016-06-15 12:52:08 -04004063 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04004064 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04004065 status = nfserr_wrong_cred;
4066 goto out;
4067 }
Chuck Leverc41a9b72021-05-14 15:56:00 -04004068 trace_nfsd_clid_destroyed(&clp->cl_clientid);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04004069 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08004070out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04004071 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04004072 if (clp)
4073 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08004074 return status;
4075}
4076
Andy Adamson069b6ad2009-04-03 08:27:58 +03004077__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02004078nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4079 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04004080{
Christoph Hellwigeb698532017-05-08 20:58:35 +02004081 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05004082 struct nfs4_client *clp = cstate->clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004083 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08004084
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04004085 if (rc->rca_one_fs) {
4086 if (!cstate->current_fh.fh_dentry)
4087 return nfserr_nofilehandle;
4088 /*
4089 * We don't take advantage of the rca_one_fs case.
4090 * That's OK, it's optional, we can safely ignore it.
4091 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02004092 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04004093 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08004094
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08004095 status = nfserr_complete_already;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05004096 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08004097 goto out;
4098
4099 status = nfserr_stale_clientid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05004100 if (is_client_expired(clp))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04004101 /*
4102 * The following error isn't really legal.
4103 * But we only get here if the client just explicitly
4104 * destroyed the client. Surely it no longer cares what
4105 * error it gets back on an operation for the dead
4106 * client.
4107 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08004108 goto out;
4109
4110 status = nfs_ok;
Chuck Levercee8aa02021-05-14 15:55:54 -04004111 trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
J. Bruce Fieldsec596592021-01-21 17:57:45 -05004112 nfsd4_client_record_create(clp);
4113 inc_reclaim_complete(clp);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08004114out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08004115 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04004116}
4117
4118__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004119nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02004120 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121{
Christoph Hellwigeb698532017-05-08 20:58:35 +02004122 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04004123 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004125 struct nfs4_client *conf, *new;
4126 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004127 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03004128 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4129
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04004130 new = create_client(clname, rqstp, &clverifier);
4131 if (new == NULL)
4132 return nfserr_jukebox;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004133 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03004134 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04004135 if (conf && client_has_state(conf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05004137 if (clp_used_exchangeid(conf))
4138 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04004139 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Chuck Lever27787732021-05-14 15:55:29 -04004140 trace_nfsd_clid_cred_mismatch(conf, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 goto out;
4142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03004144 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04004145 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004146 unhash_client_locked(unconf);
Chuck Lever744ea542021-05-14 15:55:36 -04004147 if (conf) {
4148 if (same_verf(&conf->cl_verifier, &clverifier)) {
4149 copy_clid(new, conf);
4150 gen_confirm(new, nn);
4151 } else
4152 trace_nfsd_clid_verf_mismatch(conf, rqstp,
4153 &clverifier);
Chuck Lever237f91c2021-05-14 15:56:13 -04004154 } else
4155 trace_nfsd_clid_fresh(new);
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04004156 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09004157 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05004158 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
4160 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
4161 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04004162 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 status = nfs_ok;
4164out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004165 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04004166 if (new)
4167 free_client(new);
Chuck Lever237f91c2021-05-14 15:56:13 -04004168 if (unconf) {
4169 trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04004170 expire_client(unconf);
Chuck Lever237f91c2021-05-14 15:56:13 -04004171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 return status;
4173}
4174
Al Virob37ad282006-10-19 23:28:59 -07004175__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08004176nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02004177 struct nfsd4_compound_state *cstate,
4178 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179{
Christoph Hellwigeb698532017-05-08 20:58:35 +02004180 struct nfsd4_setclientid_confirm *setclientid_confirm =
4181 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07004182 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04004183 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4185 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07004186 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004187 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004189 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07004191
Jeff Laytond20c11d2014-07-30 08:27:07 -04004192 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004193 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004194 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004195 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04004196 * We try hard to give out unique clientid's, so if we get an
4197 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004198 * the client may be buggy; this should never happen.
4199 *
4200 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004201 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004202 status = nfserr_clid_inuse;
Chuck Lever27787732021-05-14 15:55:29 -04004203 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
4204 trace_nfsd_clid_cred_mismatch(unconf, rqstp);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004205 goto out;
Chuck Lever27787732021-05-14 15:55:29 -04004206 }
4207 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
4208 trace_nfsd_clid_cred_mismatch(conf, rqstp);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004209 goto out;
Chuck Lever27787732021-05-14 15:55:29 -04004210 }
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004211 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04004212 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 status = nfs_ok;
Chuck Lever237f91c2021-05-14 15:56:13 -04004214 } else
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004215 status = nfserr_stale_clientid;
4216 goto out;
4217 }
4218 status = nfs_ok;
Chuck Lever237f91c2021-05-14 15:56:13 -04004219 if (conf) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04004220 old = unconf;
4221 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004222 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
Chuck Lever237f91c2021-05-14 15:56:13 -04004223 } else {
Jeff Laytond20c11d2014-07-30 08:27:07 -04004224 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4225 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04004226 status = nfserr_clid_inuse;
4227 if (client_has_state(old)
4228 && !same_creds(&unconf->cl_cred,
Dai Ngoab451ea2022-01-26 13:13:38 -08004229 &old->cl_cred)) {
4230 old = NULL;
J. Bruce Fields2b634822015-10-15 15:33:23 -04004231 goto out;
Dai Ngoab451ea2022-01-26 13:13:38 -08004232 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04004233 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04004234 if (status) {
4235 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04004236 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04004237 }
Chuck Lever2958d2e2021-05-14 15:56:06 -04004238 trace_nfsd_clid_replaced(&old->cl_clientid);
J. Bruce Fields221a6872013-04-01 22:23:49 -04004239 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04004240 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04004241 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07004242 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04004243 get_client_locked(conf);
4244 spin_unlock(&nn->client_lock);
J. Bruce Fields934bd072021-05-25 14:53:44 -04004245 if (conf == unconf)
4246 fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
Jeff Laytond20c11d2014-07-30 08:27:07 -04004247 nfsd4_probe_callback(conf);
4248 spin_lock(&nn->client_lock);
4249 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04004251 spin_unlock(&nn->client_lock);
4252 if (old)
4253 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 return status;
4255}
4256
J. Bruce Fields32513b42011-10-13 16:00:16 -04004257static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
J. Bruce Fields32513b42011-10-13 16:00:16 -04004259 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4260}
4261
4262/* OPEN Share state helper functions */
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004263static void nfsd4_init_file(struct svc_fh *fh, unsigned int hashval,
Jeff Layton5b095e92014-10-23 08:01:02 -04004264 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04004265{
Trond Myklebust950e0112014-06-30 11:48:31 -04004266 lockdep_assert_held(&state_lock);
4267
Elena Reshetova818a34e2017-10-20 12:53:30 +03004268 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004269 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04004270 INIT_LIST_HEAD(&fp->fi_stateids);
4271 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004272 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004273 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
Jeff Layton0c637be2014-08-22 12:05:43 -04004274 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004275 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004276 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004277 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4278 memset(fp->fi_access, 0, sizeof(fp->fi_access));
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004279 fp->fi_aliased = false;
4280 fp->fi_inode = d_inode(fh->fh_dentry);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004281#ifdef CONFIG_NFSD_PNFS
4282 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02004283 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004284#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04004285 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286}
4287
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004288void
NeilBrowne60d4392005-06-23 22:03:01 -07004289nfsd4_free_slabs(void)
4290{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004291 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004292 kmem_cache_destroy(openowner_slab);
4293 kmem_cache_destroy(lockowner_slab);
4294 kmem_cache_destroy(file_slab);
4295 kmem_cache_destroy(stateid_slab);
4296 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004297 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07004298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Bryan Schumaker72083392011-11-01 15:24:59 -04004300int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301nfsd4_init_slabs(void)
4302{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004303 client_slab = kmem_cache_create("nfsd4_clients",
4304 sizeof(struct nfs4_client), 0, 0, NULL);
4305 if (client_slab == NULL)
4306 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004307 openowner_slab = kmem_cache_create("nfsd4_openowners",
4308 sizeof(struct nfs4_openowner), 0, 0, NULL);
4309 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04004310 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004311 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08004312 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004313 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004314 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07004315 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09004316 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07004317 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004318 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07004319 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004320 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07004321 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004322 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07004323 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09004324 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07004325 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004326 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004327 odstate_slab = kmem_cache_create("nfsd4_odstate",
4328 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4329 if (odstate_slab == NULL)
4330 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07004332
Sachin Bhamare8287f002015-04-27 14:50:14 +02004333out_free_deleg_slab:
4334 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004335out_free_stateid_slab:
4336 kmem_cache_destroy(stateid_slab);
4337out_free_file_slab:
4338 kmem_cache_destroy(file_slab);
4339out_free_lockowner_slab:
4340 kmem_cache_destroy(lockowner_slab);
4341out_free_openowner_slab:
4342 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004343out_free_client_slab:
4344 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004345out:
NeilBrowne60d4392005-06-23 22:03:01 -07004346 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347}
4348
Dai Ngo7746b322022-09-14 08:54:26 -07004349static unsigned long
4350nfsd_courtesy_client_count(struct shrinker *shrink, struct shrink_control *sc)
4351{
4352 int cnt;
4353 struct nfsd_net *nn = container_of(shrink,
4354 struct nfsd_net, nfsd_client_shrinker);
4355
4356 cnt = atomic_read(&nn->nfsd_courtesy_clients);
4357 if (cnt > 0)
4358 mod_delayed_work(laundry_wq, &nn->nfsd_shrinker_work, 0);
4359 return (unsigned long)cnt;
4360}
4361
4362static unsigned long
4363nfsd_courtesy_client_scan(struct shrinker *shrink, struct shrink_control *sc)
4364{
4365 return SHRINK_STOP;
4366}
4367
4368int
4369nfsd4_init_leases_net(struct nfsd_net *nn)
Dai Ngo68671372022-07-15 16:54:51 -07004370{
Dai Ngo4271c2c2022-07-15 16:54:53 -07004371 struct sysinfo si;
4372 u64 max_clients;
4373
Dai Ngo68671372022-07-15 16:54:51 -07004374 nn->nfsd4_lease = 90; /* default lease time */
4375 nn->nfsd4_grace = 90;
4376 nn->somebody_reclaimed = false;
4377 nn->track_reclaim_completes = false;
Jason A. Donenfelda251c172022-10-05 17:43:22 +02004378 nn->clverifier_counter = get_random_u32();
4379 nn->clientid_base = get_random_u32();
Dai Ngo68671372022-07-15 16:54:51 -07004380 nn->clientid_counter = nn->clientid_base + 1;
4381 nn->s2s_cp_cl_id = nn->clientid_counter++;
Dai Ngo0926c3952022-07-15 16:54:52 -07004382
4383 atomic_set(&nn->nfs4_client_count, 0);
Dai Ngo4271c2c2022-07-15 16:54:53 -07004384 si_meminfo(&si);
4385 max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
4386 max_clients *= NFS4_CLIENTS_PER_GB;
4387 nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
Dai Ngo3a4ea23d2022-09-14 08:54:25 -07004388
4389 atomic_set(&nn->nfsd_courtesy_clients, 0);
Dai Ngo7746b322022-09-14 08:54:26 -07004390 nn->nfsd_client_shrinker.scan_objects = nfsd_courtesy_client_scan;
4391 nn->nfsd_client_shrinker.count_objects = nfsd_courtesy_client_count;
4392 nn->nfsd_client_shrinker.seeks = DEFAULT_SEEKS;
4393 return register_shrinker(&nn->nfsd_client_shrinker, "nfsd-client");
4394}
4395
4396void
4397nfsd4_leases_net_shutdown(struct nfsd_net *nn)
4398{
4399 unregister_shrinker(&nn->nfsd_client_shrinker);
Dai Ngo68671372022-07-15 16:54:51 -07004400}
4401
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004402static void init_nfs4_replay(struct nfs4_replay *rp)
4403{
4404 rp->rp_status = nfserr_serverfault;
4405 rp->rp_buflen = 0;
4406 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04004407 mutex_init(&rp->rp_mutex);
4408}
4409
4410static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4411 struct nfs4_stateowner *so)
4412{
4413 if (!nfsd4_has_session(cstate)) {
4414 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004415 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004416 }
4417}
4418
4419void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4420{
4421 struct nfs4_stateowner *so = cstate->replay_owner;
4422
4423 if (so != NULL) {
4424 cstate->replay_owner = NULL;
4425 mutex_unlock(&so->so_replay.rp_mutex);
4426 nfs4_put_stateowner(so);
4427 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004428}
4429
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004430static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431{
4432 struct nfs4_stateowner *sop;
4433
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004434 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004435 if (!sop)
4436 return NULL;
4437
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04004438 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004439 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004440 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004441 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004443
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004444 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004445 sop->so_client = clp;
4446 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04004447 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004448 return sop;
4449}
4450
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004451static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004452{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004453 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004454
Trond Myklebustd4f04892014-07-29 21:34:36 -04004455 list_add(&oo->oo_owner.so_strhash,
4456 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004457 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458}
4459
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004460static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4461{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004462 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004463}
4464
Jeff Layton6b180f02014-07-29 21:34:26 -04004465static void nfs4_free_openowner(struct nfs4_stateowner *so)
4466{
4467 struct nfs4_openowner *oo = openowner(so);
4468
4469 kmem_cache_free(openowner_slab, oo);
4470}
4471
4472static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004473 .so_unhash = nfs4_unhash_openowner,
4474 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004475};
4476
Andrew Elble7fc05642015-11-05 20:42:43 -05004477static struct nfs4_ol_stateid *
4478nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4479{
4480 struct nfs4_ol_stateid *local, *ret = NULL;
4481 struct nfs4_openowner *oo = open->op_openowner;
4482
4483 lockdep_assert_held(&fp->fi_lock);
4484
4485 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4486 /* ignore lock owners */
4487 if (local->st_stateowner->so_is_open_owner == 0)
4488 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004489 if (local->st_stateowner != &oo->oo_owner)
4490 continue;
4491 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004492 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004493 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05004494 break;
4495 }
4496 }
4497 return ret;
4498}
4499
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004500static __be32
4501nfsd4_verify_open_stid(struct nfs4_stid *s)
4502{
4503 __be32 ret = nfs_ok;
4504
4505 switch (s->sc_type) {
4506 default:
4507 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004508 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004509 case NFS4_CLOSED_STID:
4510 case NFS4_CLOSED_DELEG_STID:
4511 ret = nfserr_bad_stateid;
4512 break;
4513 case NFS4_REVOKED_DELEG_STID:
4514 ret = nfserr_deleg_revoked;
4515 }
4516 return ret;
4517}
4518
4519/* Lock the stateid st_mutex, and deal with races with CLOSE */
4520static __be32
4521nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4522{
4523 __be32 ret;
4524
Andrew Elble4f34bd02017-11-08 17:29:51 -05004525 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004526 ret = nfsd4_verify_open_stid(&stp->st_stid);
4527 if (ret != nfs_ok)
4528 mutex_unlock(&stp->st_mutex);
4529 return ret;
4530}
4531
4532static struct nfs4_ol_stateid *
4533nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4534{
4535 struct nfs4_ol_stateid *stp;
4536 for (;;) {
4537 spin_lock(&fp->fi_lock);
4538 stp = nfsd4_find_existing_open(fp, open);
4539 spin_unlock(&fp->fi_lock);
4540 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4541 break;
4542 nfs4_put_stid(&stp->st_stid);
4543 }
4544 return stp;
4545}
4546
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004547static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004548alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004549 struct nfsd4_compound_state *cstate)
4550{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004551 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004552 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004554 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4555 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004557 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004558 oo->oo_owner.so_is_open_owner = 1;
4559 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004560 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004561 if (nfsd4_has_session(cstate))
4562 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004563 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004564 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004565 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004566 spin_lock(&clp->cl_lock);
4567 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004568 if (ret == NULL) {
4569 hash_openowner(oo, clp, strhashval);
4570 ret = oo;
4571 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004572 nfs4_free_stateowner(&oo->oo_owner);
4573
Trond Myklebustd4f04892014-07-29 21:34:36 -04004574 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004575 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576}
4577
Andrew Elble7fc05642015-11-05 20:42:43 -05004578static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004579init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004580{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Andrew Elble7fc05642015-11-05 20:42:43 -05004582 struct nfs4_openowner *oo = open->op_openowner;
4583 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004584 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004585
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004586 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004587 /* We are moving these outside of the spinlocks to avoid the warnings */
4588 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004589 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004590
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004591retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004592 spin_lock(&oo->oo_owner.so_client->cl_lock);
4593 spin_lock(&fp->fi_lock);
4594
4595 retstp = nfsd4_find_existing_open(fp, open);
4596 if (retstp)
4597 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004598
4599 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004600 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004601 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004602 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004603 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004604 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004605 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 stp->st_access_bmap = 0;
4607 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004608 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004609 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004610 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004611
4612out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004613 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004614 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004615 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004616 /* Handle races with CLOSE */
4617 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4618 nfs4_put_stid(&retstp->st_stid);
4619 goto retry;
4620 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004621 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004622 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004623 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004624 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004625 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626}
4627
Jeff Laytond3134b12014-07-29 21:34:32 -04004628/*
4629 * In the 4.0 case we need to keep the owners around a little while to handle
4630 * CLOSE replay. We still do need to release any file access that is held by
4631 * them before returning however.
4632 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004634move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
Jeff Layton217526e2014-07-30 08:27:11 -04004636 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004637 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4638 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4639 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004640
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004641 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Jeff Laytonb401be222014-07-29 21:34:33 -04004643 /*
4644 * We know that we hold one reference via nfsd4_close, and another
4645 * "persistent" reference for the client. If the refcount is higher
4646 * than 2, then there are still calls in progress that are using this
4647 * stateid. We can't put the sc_file reference until they are finished.
4648 * Wait for the refcount to drop to 2. Since it has been unhashed,
4649 * there should be no danger of the refcount going back up again at
4650 * this point.
4651 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004652 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004653
Jeff Laytond3134b12014-07-29 21:34:32 -04004654 release_all_access(s);
4655 if (s->st_stid.sc_file) {
4656 put_nfs4_file(s->st_stid.sc_file);
4657 s->st_stid.sc_file = NULL;
4658 }
Jeff Layton217526e2014-07-30 08:27:11 -04004659
4660 spin_lock(&nn->client_lock);
4661 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004662 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004663 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004664 oo->oo_time = ktime_get_boottime_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004665 spin_unlock(&nn->client_lock);
4666 if (last)
4667 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668}
4669
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670/* search file_hashtbl[] for file */
4671static struct nfs4_file *
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004672find_file_locked(struct svc_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 struct nfs4_file *fp;
4675
Madhuparna Bhowmik36a80492020-02-13 20:33:31 +05304676 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4677 lockdep_is_held(&state_lock)) {
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004678 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004679 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004680 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 }
4683 return NULL;
4684}
4685
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004686static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh,
4687 unsigned int hashval)
4688{
4689 struct nfs4_file *fp;
4690 struct nfs4_file *ret = NULL;
4691 bool alias_found = false;
4692
4693 spin_lock(&state_lock);
4694 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4695 lockdep_is_held(&state_lock)) {
4696 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4697 if (refcount_inc_not_zero(&fp->fi_ref))
4698 ret = fp;
4699 } else if (d_inode(fh->fh_dentry) == fp->fi_inode)
4700 fp->fi_aliased = alias_found = true;
4701 }
4702 if (likely(ret == NULL)) {
4703 nfsd4_init_file(fh, hashval, new);
4704 new->fi_aliased = alias_found;
4705 ret = new;
4706 }
4707 spin_unlock(&state_lock);
4708 return ret;
4709}
4710
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004711static struct nfs4_file * find_file(struct svc_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004712{
4713 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004714 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004715
Jeff Layton5b095e92014-10-23 08:01:02 -04004716 rcu_read_lock();
4717 fp = find_file_locked(fh, hashval);
4718 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004719 return fp;
4720}
4721
4722static struct nfs4_file *
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004723find_or_add_file(struct nfs4_file *new, struct svc_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004724{
4725 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004726 unsigned int hashval = file_hashval(fh);
4727
4728 rcu_read_lock();
4729 fp = find_file_locked(fh, hashval);
4730 rcu_read_unlock();
4731 if (fp)
4732 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004733
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004734 return insert_file(new, fh, hashval);
Trond Myklebust950e0112014-06-30 11:48:31 -04004735}
4736
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004737/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 * Called to check deny when READ with all zero stateid or
4739 * WRITE with all zero or all one stateid
4740 */
Al Virob37ad282006-10-19 23:28:59 -07004741static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4743{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004745 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004747 fp = find_file(current_fh);
NeilBrown13cd2182005-06-23 22:03:10 -07004748 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004749 return ret;
4750 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004751 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004752 if (fp->fi_share_deny & deny_type)
4753 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004754 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004755 put_nfs4_file(fp);
4756 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757}
4758
Chuck Leverc0353622022-09-08 18:14:00 -04004759static bool nfsd4_deleg_present(const struct inode *inode)
4760{
4761 struct file_lock_context *ctx = smp_load_acquire(&inode->i_flctx);
4762
4763 return ctx && !list_empty_careful(&ctx->flc_lease);
4764}
4765
4766/**
4767 * nfsd_wait_for_delegreturn - wait for delegations to be returned
4768 * @rqstp: the RPC transaction being executed
4769 * @inode: in-core inode of the file being waited for
4770 *
4771 * The timeout prevents deadlock if all nfsd threads happen to be
4772 * tied up waiting for returning delegations.
4773 *
4774 * Return values:
4775 * %true: delegation was returned
4776 * %false: timed out waiting for delegreturn
4777 */
4778bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode)
4779{
4780 long __maybe_unused timeo;
4781
4782 timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode),
4783 NFSD_DELEGRETURN_TIMEOUT);
4784 trace_nfsd_delegret_wakeup(rqstp, inode, timeo);
4785 return timeo > 0;
4786}
4787
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004788static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004790 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004791 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4792 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004793
Trond Myklebust11b91642014-07-29 21:34:08 -04004794 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004795
Jeff Layton02e12152014-07-16 10:31:57 -04004796 /*
4797 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004798 * already holding inode->i_lock.
4799 *
Jeff Laytondff13992014-07-08 14:02:49 -04004800 * If the dl_time != 0, then we know that it has already been
4801 * queued for a lease break. Don't queue it again.
4802 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004803 spin_lock(&state_lock);
J. Bruce Fields548ec082021-11-29 15:08:00 -05004804 if (delegation_hashed(dp) && dp->dl_time == 0) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004805 dp->dl_time = ktime_get_boottime_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004806 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004807 }
Jeff Layton02e12152014-07-16 10:31:57 -04004808 spin_unlock(&state_lock);
4809}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004811static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4812 struct rpc_task *task)
4813{
4814 struct nfs4_delegation *dp = cb_to_delegation(cb);
4815
Chuck Lever1035d652022-09-08 18:13:54 -04004816 trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task);
4817
J. Bruce Fields12ed22f2020-07-31 16:20:05 -04004818 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4819 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
Andrew Elblea4579742015-08-31 12:06:41 -04004820 return 1;
4821
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004822 switch (task->tk_status) {
4823 case 0:
4824 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004825 case -NFS4ERR_DELAY:
4826 rpc_delay(task, 2 * HZ);
4827 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004828 case -EBADHANDLE:
4829 case -NFS4ERR_BAD_STATEID:
4830 /*
4831 * Race: client probably got cb_recall before open reply
4832 * granting delegation.
4833 */
4834 if (dp->dl_retries--) {
4835 rpc_delay(task, 2 * HZ);
4836 return 0;
4837 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004838 fallthrough;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004839 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004840 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004841 }
4842}
4843
4844static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4845{
4846 struct nfs4_delegation *dp = cb_to_delegation(cb);
4847
4848 nfs4_put_stid(&dp->dl_stid);
4849}
4850
Julia Lawallc4cb8972015-11-21 22:57:39 +01004851static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004852 .prepare = nfsd4_cb_recall_prepare,
4853 .done = nfsd4_cb_recall_done,
4854 .release = nfsd4_cb_recall_release,
4855};
4856
Jeff Layton02e12152014-07-16 10:31:57 -04004857static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4858{
4859 /*
4860 * We're assuming the state code never drops its reference
4861 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004862 * callback (and since the lease code is serialized by the
Jeff Layton25fbe1f2022-09-26 12:38:45 -04004863 * flc_lock) we know the server hasn't removed the lease yet, and
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004864 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004865 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004866 refcount_inc(&dp->dl_stid.sc_count);
Jeff Laytonb95239c2022-09-26 14:41:01 -04004867 WARN_ON_ONCE(!nfsd4_run_cb(&dp->dl_recall));
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004868}
4869
Jeff Layton25fbe1f2022-09-26 12:38:45 -04004870/* Called from break_lease() with flc_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004871static bool
4872nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004873{
J. Bruce Fields653e5142018-02-15 22:08:45 -05004874 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4875 struct nfs4_file *fp = dp->dl_stid.sc_file;
Dai Ngo66af2572022-05-02 14:19:21 -07004876 struct nfs4_client *clp = dp->dl_stid.sc_client;
4877 struct nfsd_net *nn;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004878
Chuck Lever17d76dd2021-05-14 15:57:20 -04004879 trace_nfsd_cb_recall(&dp->dl_stid);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04004880
Dai Ngo66af2572022-05-02 14:19:21 -07004881 dp->dl_recalled = true;
4882 atomic_inc(&clp->cl_delegs_in_recall);
4883 if (try_to_expire_client(clp)) {
4884 nn = net_generic(clp->net, nfsd_net_id);
4885 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
4886 }
4887
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004888 /*
4889 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004890 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004891 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004892 */
4893 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
Jeff Layton02e12152014-07-16 10:31:57 -04004895 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004896 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004897 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004898 spin_unlock(&fp->fi_lock);
Jeff Laytonb95239c2022-09-26 14:41:01 -04004899 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900}
4901
Chuck Lever50719bf2022-02-14 15:30:13 -05004902/**
4903 * nfsd_breaker_owns_lease - Check if lease conflict was resolved
4904 * @fl: Lock state to check
4905 *
4906 * Return values:
4907 * %true: Lease conflict was resolved
4908 * %false: Lease conflict was not resolved.
4909 */
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004910static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4911{
4912 struct nfs4_delegation *dl = fl->fl_owner;
4913 struct svc_rqst *rqst;
4914 struct nfs4_client *clp;
4915
4916 if (!i_am_nfsd())
Chuck Lever50719bf2022-02-14 15:30:13 -05004917 return false;
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004918 rqst = kthread_data(current);
J. Bruce Fields13956162020-09-25 10:12:39 -04004919 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4920 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
Chuck Lever50719bf2022-02-14 15:30:13 -05004921 return false;
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004922 clp = *(rqst->rq_lease_breaker);
4923 return dl->dl_stid.sc_client == clp;
4924}
4925
Jeff Laytonc45198e2014-09-01 07:12:07 -04004926static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004927nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4928 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929{
Dai Ngo66af2572022-05-02 14:19:21 -07004930 struct nfs4_delegation *dp = (struct nfs4_delegation *)onlist->fl_owner;
4931 struct nfs4_client *clp = dp->dl_stid.sc_client;
4932
4933 if (arg & F_UNLCK) {
4934 if (dp->dl_recalled)
4935 atomic_dec(&clp->cl_delegs_in_recall);
Jeff Laytonc45198e2014-09-01 07:12:07 -04004936 return lease_modify(onlist, arg, dispose);
Dai Ngo66af2572022-05-02 14:19:21 -07004937 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 return -EAGAIN;
4939}
4940
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004941static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004942 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004943 .lm_break = nfsd_break_deleg_cb,
4944 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945};
4946
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004947static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4948{
4949 if (nfsd4_has_session(cstate))
4950 return nfs_ok;
4951 if (seqid == so->so_seqid - 1)
4952 return nfserr_replay_me;
4953 if (seqid == so->so_seqid)
4954 return nfs_ok;
4955 return nfserr_bad_seqid;
4956}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
J. Bruce Fields7950b532021-01-21 17:57:41 -05004958static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
4959 struct nfsd_net *nn)
4960{
4961 struct nfs4_client *found;
4962
4963 spin_lock(&nn->client_lock);
4964 found = find_confirmed_client(clid, sessions, nn);
4965 if (found)
4966 atomic_inc(&found->cl_rpc_users);
4967 spin_unlock(&nn->client_lock);
4968 return found;
4969}
4970
J. Bruce Fields460d2702021-01-21 17:57:40 -05004971static __be32 set_client(clientid_t *clid,
Jeff Layton4b24ca72014-06-30 11:48:44 -04004972 struct nfsd4_compound_state *cstate,
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004973 struct nfsd_net *nn)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004974{
Jeff Layton4b24ca72014-06-30 11:48:44 -04004975 if (cstate->clp) {
J. Bruce Fields7950b532021-01-21 17:57:41 -05004976 if (!same_clid(&cstate->clp->cl_clientid, clid))
Jeff Layton4b24ca72014-06-30 11:48:44 -04004977 return nfserr_stale_clientid;
4978 return nfs_ok;
4979 }
Jeff Layton4b24ca72014-06-30 11:48:44 -04004980 if (STALE_CLIENTID(clid, nn))
4981 return nfserr_stale_clientid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004982 /*
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004983 * We're in the 4.0 case (otherwise the SEQUENCE op would have
4984 * set cstate->clp), so session = false:
Jeff Layton4b24ca72014-06-30 11:48:44 -04004985 */
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004986 cstate->clp = lookup_clientid(clid, false, nn);
J. Bruce Fields7950b532021-01-21 17:57:41 -05004987 if (!cstate->clp)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004988 return nfserr_expired;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004989 return nfs_ok;
4990}
4991
Al Virob37ad282006-10-19 23:28:59 -07004992__be32
Andy Adamson66689582009-04-03 08:28:45 +03004993nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004994 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 clientid_t *clientid = &open->op_clientid;
4997 struct nfs4_client *clp = NULL;
4998 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004999 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005000 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
J. Bruce Fields32513b42011-10-13 16:00:16 -04005002 /*
5003 * In case we need it later, after we've already created the
5004 * file and don't want to risk a further failure:
5005 */
5006 open->op_file = nfsd4_alloc_file();
5007 if (open->op_file == NULL)
5008 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005010 status = set_client(clientid, cstate, nn);
Trond Myklebust2d91e892014-06-30 11:48:46 -04005011 if (status)
5012 return status;
5013 clp = cstate->clp;
5014
Trond Myklebustd4f04892014-07-29 21:34:36 -04005015 strhashval = ownerstr_hashval(&open->op_owner);
5016 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005017 open->op_openowner = oo;
5018 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04005019 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005021 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08005022 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005023 release_openowner(oo);
5024 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04005025 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08005026 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005027 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
5028 if (status)
5029 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005030 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04005031new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04005032 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04005033 if (oo == NULL)
5034 return nfserr_jukebox;
5035 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005036alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04005037 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005038 if (!open->op_stp)
5039 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02005040
5041 if (nfsd4_has_session(cstate) &&
5042 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
5043 open->op_odstate = alloc_clnt_odstate(clp);
5044 if (!open->op_odstate)
5045 return nfserr_jukebox;
5046 }
5047
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08005048 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049}
5050
Al Virob37ad282006-10-19 23:28:59 -07005051static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07005052nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
5053{
5054 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
5055 return nfserr_openmode;
5056 else
5057 return nfs_ok;
5058}
5059
Daniel Mackc47d8322011-05-16 16:38:14 +02005060static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04005061{
J. Bruce Fields24a01112010-05-18 20:01:35 -04005062 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
5063}
5064
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005065static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04005066{
5067 struct nfs4_stid *ret;
5068
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005069 ret = find_stateid_by_type(cl, s,
5070 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04005071 if (!ret)
5072 return NULL;
5073 return delegstateid(ret);
5074}
5075
J. Bruce Fields8b289b22011-10-19 11:52:12 -04005076static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
5077{
5078 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
5079 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
5080}
5081
Al Virob37ad282006-10-19 23:28:59 -07005082static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04005083nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07005084 struct nfs4_delegation **dp)
5085{
5086 int flags;
Al Virob37ad282006-10-19 23:28:59 -07005087 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005088 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07005089
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005090 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5091 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07005092 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005093 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
5094 nfs4_put_stid(&deleg->dl_stid);
5095 if (cl->cl_minorversion)
5096 status = nfserr_deleg_revoked;
5097 goto out;
5098 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04005099 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005100 status = nfs4_check_delegmode(deleg, flags);
5101 if (status) {
5102 nfs4_put_stid(&deleg->dl_stid);
5103 goto out;
5104 }
5105 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07005106out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04005107 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07005108 return nfs_ok;
5109 if (status)
5110 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005111 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07005112 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07005113}
5114
J. Bruce Fields21fb4012010-07-28 12:21:23 -04005115static inline int nfs4_access_to_access(u32 nfs4_access)
5116{
5117 int flags = 0;
5118
5119 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
5120 flags |= NFSD_MAY_READ;
5121 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
5122 flags |= NFSD_MAY_WRITE;
5123 return flags;
5124}
5125
Al Virob37ad282006-10-19 23:28:59 -07005126static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
5128 struct nfsd4_open *open)
5129{
5130 struct iattr iattr = {
5131 .ia_valid = ATTR_SIZE,
5132 .ia_size = 0,
5133 };
NeilBrown7fe2a712022-07-26 16:45:30 +10005134 struct nfsd_attrs attrs = {
5135 .na_iattr = &iattr,
5136 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 if (!open->op_truncate)
5138 return 0;
5139 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08005140 return nfserr_inval;
NeilBrown7fe2a712022-07-26 16:45:30 +10005141 return nfsd_setattr(rqstp, fh, &attrs, 0, (time64_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142}
5143
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005144static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005145 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
Dai Ngo3d694272022-05-02 14:19:22 -07005146 struct nfsd4_open *open, bool new_stp)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005147{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04005148 struct nfsd_file *nf = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005149 __be32 status;
5150 int oflag = nfs4_access_to_omode(open->op_share_access);
5151 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005152 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005153
Trond Myklebustde186432014-07-10 14:07:26 -04005154 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005155
5156 /*
5157 * Are we trying to set a deny mode that would conflict with
5158 * current access?
5159 */
5160 status = nfs4_file_check_deny(fp, open->op_share_deny);
5161 if (status != nfs_ok) {
Dai Ngo3d694272022-05-02 14:19:22 -07005162 if (status != nfserr_share_denied) {
5163 spin_unlock(&fp->fi_lock);
5164 goto out;
5165 }
5166 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5167 stp, open->op_share_deny, false))
5168 status = nfserr_jukebox;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005169 spin_unlock(&fp->fi_lock);
5170 goto out;
5171 }
5172
5173 /* set access to the file */
5174 status = nfs4_file_get_access(fp, open->op_share_access);
5175 if (status != nfs_ok) {
Dai Ngo3d694272022-05-02 14:19:22 -07005176 if (status != nfserr_share_denied) {
5177 spin_unlock(&fp->fi_lock);
5178 goto out;
5179 }
5180 if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
5181 stp, open->op_share_access, true))
5182 status = nfserr_jukebox;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005183 spin_unlock(&fp->fi_lock);
5184 goto out;
5185 }
5186
5187 /* Set access bits in stateid */
5188 old_access_bmap = stp->st_access_bmap;
5189 set_access(open->op_share_access, stp);
5190
5191 /* Set new deny mask */
5192 old_deny_bmap = stp->st_deny_bmap;
5193 set_deny(open->op_share_deny, stp);
5194 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5195
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005196 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04005197 spin_unlock(&fp->fi_lock);
Chuck Leverfb70bf12022-03-30 10:30:54 -04005198
5199 if (!open->op_filp) {
5200 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
5201 if (status != nfs_ok)
5202 goto out_put_access;
5203 } else {
5204 status = nfsd_file_create(rqstp, cur_fh, access, &nf);
5205 if (status != nfs_ok)
5206 goto out_put_access;
5207 nf->nf_file = open->op_filp;
5208 open->op_filp = NULL;
Chuck Leverbe023002022-07-08 14:26:43 -04005209 trace_nfsd_file_create(rqstp, access, nf);
Chuck Leverfb70bf12022-03-30 10:30:54 -04005210 }
5211
Trond Myklebustde186432014-07-10 14:07:26 -04005212 spin_lock(&fp->fi_lock);
5213 if (!fp->fi_fds[oflag]) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04005214 fp->fi_fds[oflag] = nf;
5215 nf = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -04005216 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005217 }
Trond Myklebustde186432014-07-10 14:07:26 -04005218 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04005219 if (nf)
5220 nfsd_file_put(nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005221
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04005222 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5223 access));
5224 if (status)
5225 goto out_put_access;
5226
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005227 status = nfsd4_truncate(rqstp, cur_fh, open);
5228 if (status)
5229 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005230out:
5231 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005232out_put_access:
5233 stp->st_access_bmap = old_access_bmap;
5234 nfs4_file_put_access(fp, open->op_share_access);
5235 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5236 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005237}
5238
Al Virob37ad282006-10-19 23:28:59 -07005239static __be32
Dai Ngo3d694272022-05-02 14:19:22 -07005240nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
5241 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
5242 struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243{
Al Virob37ad282006-10-19 23:28:59 -07005244 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02005245 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005247 if (!test_access(open->op_share_access, stp))
Dai Ngo3d694272022-05-02 14:19:22 -07005248 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04005249
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005250 /* test and set deny mode */
5251 spin_lock(&fp->fi_lock);
5252 status = nfs4_file_check_deny(fp, open->op_share_deny);
5253 if (status == nfs_ok) {
Dai Ngo3d694272022-05-02 14:19:22 -07005254 if (status != nfserr_share_denied) {
5255 set_deny(open->op_share_deny, stp);
5256 fp->fi_share_deny |=
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005257 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
Dai Ngo3d694272022-05-02 14:19:22 -07005258 } else {
5259 if (nfs4_resolve_deny_conflicts_locked(fp, false,
5260 stp, open->op_share_deny, false))
5261 status = nfserr_jukebox;
5262 }
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005263 }
5264 spin_unlock(&fp->fi_lock);
5265
5266 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04005269 status = nfsd4_truncate(rqstp, cur_fh, open);
5270 if (status != nfs_ok)
5271 reset_union_bmap_deny(old_deny_bmap, stp);
5272 return status;
5273}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
J. Bruce Fields14a24e92010-12-10 19:02:49 -05005275/* Should we give out recallable state?: */
5276static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
5277{
5278 if (clp->cl_cb_state == NFSD4_CB_UP)
5279 return true;
5280 /*
5281 * In the sessions case, since we don't have to establish a
5282 * separate connection for callbacks, we assume it's OK
5283 * until we hear otherwise:
5284 */
5285 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
5286}
5287
J. Bruce Fields653e5142018-02-15 22:08:45 -05005288static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5289 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005290{
5291 struct file_lock *fl;
5292
5293 fl = locks_alloc_lock();
5294 if (!fl)
5295 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005296 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04005297 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005298 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
5299 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05005300 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005301 fl->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005302 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05005303 return fl;
5304}
5305
J. Bruce Fieldsaba20722021-04-16 14:00:18 -04005306static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5307 struct nfs4_file *fp)
5308{
5309 struct nfs4_ol_stateid *st;
5310 struct file *f = fp->fi_deleg_file->nf_file;
5311 struct inode *ino = locks_inode(f);
5312 int writes;
5313
5314 writes = atomic_read(&ino->i_writecount);
5315 if (!writes)
5316 return 0;
5317 /*
5318 * There could be multiple filehandles (hence multiple
5319 * nfs4_files) referencing this file, but that's not too
5320 * common; let's just give up in that case rather than
5321 * trying to go look up all the clients using that other
5322 * nfs4_file as well:
5323 */
5324 if (fp->fi_aliased)
5325 return -EAGAIN;
5326 /*
5327 * If there's a close in progress, make sure that we see it
5328 * clear any fi_fds[] entries before we see it decrement
5329 * i_writecount:
5330 */
5331 smp_mb__after_atomic();
5332
5333 if (fp->fi_fds[O_WRONLY])
5334 writes--;
5335 if (fp->fi_fds[O_RDWR])
5336 writes--;
5337 if (writes > 0)
5338 return -EAGAIN; /* There may be non-NFSv4 writers */
5339 /*
5340 * It's possible there are non-NFSv4 write opens in progress,
5341 * but if they haven't incremented i_writecount yet then they
5342 * also haven't called break lease yet; so, they'll break this
5343 * lease soon enough. So, all that's left to check for is NFSv4
5344 * opens:
5345 */
5346 spin_lock(&fp->fi_lock);
5347 list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5348 if (st->st_openstp == NULL /* it's an open */ &&
5349 access_permit_write(st) &&
5350 st->st_stid.sc_client != clp) {
5351 spin_unlock(&fp->fi_lock);
5352 return -EAGAIN;
5353 }
5354 }
5355 spin_unlock(&fp->fi_lock);
5356 /*
5357 * There's a small chance that we could be racing with another
5358 * NFSv4 open. However, any open that hasn't added itself to
5359 * the fi_stateids list also hasn't called break_lease yet; so,
5360 * they'll break this lease soon enough.
5361 */
5362 return 0;
5363}
5364
Jeff Layton876c5532022-07-26 16:45:30 +10005365/*
5366 * It's possible that between opening the dentry and setting the delegation,
5367 * that it has been renamed or unlinked. Redo the lookup to verify that this
5368 * hasn't happened.
5369 */
5370static int
5371nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
5372 struct svc_fh *parent)
5373{
5374 struct svc_export *exp;
5375 struct dentry *child;
5376 __be32 err;
5377
Jeff Layton876c5532022-07-26 16:45:30 +10005378 err = nfsd_lookup_dentry(open->op_rqstp, parent,
5379 open->op_fname, open->op_fnamelen,
5380 &exp, &child);
5381
5382 if (err)
5383 return -EAGAIN;
5384
Jeff Layton50256e42022-11-08 11:23:11 -05005385 exp_put(exp);
Jeff Layton876c5532022-07-26 16:45:30 +10005386 dput(child);
5387 if (child != file_dentry(fp->fi_deleg_file->nf_file))
5388 return -EAGAIN;
5389
5390 return 0;
5391}
5392
Jeff Layton0b266932014-07-25 07:34:25 -04005393static struct nfs4_delegation *
Jeff Layton876c5532022-07-26 16:45:30 +10005394nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5395 struct svc_fh *parent)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05005396{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05005397 int status = 0;
Jeff Layton876c5532022-07-26 16:45:30 +10005398 struct nfs4_client *clp = stp->st_stid.sc_client;
5399 struct nfs4_file *fp = stp->st_stid.sc_file;
5400 struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
Jeff Layton0b266932014-07-25 07:34:25 -04005401 struct nfs4_delegation *dp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005402 struct nfsd_file *nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005403 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04005404
J. Bruce Fields353601e2018-02-16 14:29:42 -05005405 /*
5406 * The fi_had_conflict and nfs_get_existing_delegation checks
5407 * here are just optimizations; we'll need to recheck them at
5408 * the end:
5409 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04005410 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04005411 return ERR_PTR(-EAGAIN);
5412
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005413 nf = find_readable_file(fp);
5414 if (!nf) {
J. Bruce Fieldsaba20722021-04-16 14:00:18 -04005415 /*
5416 * We probably could attempt another open and get a read
5417 * delegation, but for now, don't bother until the
5418 * client actually sends us one.
5419 */
5420 return ERR_PTR(-EAGAIN);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005421 }
Andrew Elble34ed9872015-10-15 12:07:28 -04005422 spin_lock(&state_lock);
5423 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05005424 if (nfs4_delegation_exists(clp, fp))
5425 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005426 else if (!fp->fi_deleg_file) {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005427 fp->fi_deleg_file = nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005428 /* increment early to prevent fi_deleg_file from being
5429 * cleared */
5430 fp->fi_delegees = 1;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005431 nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005432 } else
5433 fp->fi_delegees++;
5434 spin_unlock(&fp->fi_lock);
5435 spin_unlock(&state_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005436 if (nf)
5437 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005438 if (status)
5439 return ERR_PTR(status);
5440
5441 status = -ENOMEM;
Jeff Laytonbbf936e2022-07-26 16:45:30 +10005442 dp = alloc_init_deleg(clp, fp, odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005443 if (!dp)
5444 goto out_delegees;
5445
5446 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5447 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04005448 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005449
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005450 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005451 if (fl)
5452 locks_free_lock(fl);
5453 if (status)
5454 goto out_clnt_odstate;
Jeff Layton876c5532022-07-26 16:45:30 +10005455
5456 if (parent) {
5457 status = nfsd4_verify_deleg_dentry(open, fp, parent);
5458 if (status)
5459 goto out_unlock;
5460 }
5461
J. Bruce Fieldsaba20722021-04-16 14:00:18 -04005462 status = nfsd4_check_conflicting_opens(clp, fp);
5463 if (status)
5464 goto out_unlock;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005465
5466 spin_lock(&state_lock);
5467 spin_lock(&fp->fi_lock);
5468 if (fp->fi_had_conflict)
5469 status = -EAGAIN;
5470 else
5471 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04005472 spin_unlock(&fp->fi_lock);
5473 spin_unlock(&state_lock);
5474
5475 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04005476 goto out_unlock;
5477
Jeff Layton0b266932014-07-25 07:34:25 -04005478 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04005479out_unlock:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005480 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005481out_clnt_odstate:
5482 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005483 nfs4_put_stid(&dp->dl_stid);
5484out_delegees:
5485 put_deleg_file(fp);
5486 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05005487}
5488
Benny Halevy4aa89132012-02-21 14:16:44 -08005489static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5490{
5491 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5492 if (status == -EAGAIN)
5493 open->op_why_no_deleg = WND4_CONTENTION;
5494 else {
5495 open->op_why_no_deleg = WND4_RESOURCE;
5496 switch (open->op_deleg_want) {
5497 case NFS4_SHARE_WANT_READ_DELEG:
5498 case NFS4_SHARE_WANT_WRITE_DELEG:
5499 case NFS4_SHARE_WANT_ANY_DELEG:
5500 break;
5501 case NFS4_SHARE_WANT_CANCEL:
5502 open->op_why_no_deleg = WND4_CANCELLED;
5503 break;
5504 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005505 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08005506 }
5507 }
5508}
5509
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510/*
5511 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04005512 *
5513 * Note we don't support write delegations, and won't until the vfs has
5514 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 */
5516static void
Jeff Layton876c5532022-07-26 16:45:30 +10005517nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5518 struct svc_fh *currentfh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519{
5520 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04005521 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5522 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Layton876c5532022-07-26 16:45:30 +10005523 struct svc_fh *parent = NULL;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05005524 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005525 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005527 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07005528 open->op_recall = 0;
5529 switch (open->op_claim_type) {
5530 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05005531 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07005532 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005533 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5534 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005535 break;
5536 case NFS4_OPEN_CLAIM_NULL:
Jeff Layton876c5532022-07-26 16:45:30 +10005537 parent = currentfh;
5538 fallthrough;
Ming Chened47b062014-01-09 21:26:10 +00005539 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005540 /*
5541 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005542 * had the chance to reclaim theirs, *and* until
5543 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005544 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005545 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005546 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005547 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005548 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005549 break;
5550 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005551 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005552 }
Jeff Layton876c5532022-07-26 16:45:30 +10005553 dp = nfs4_set_delegation(open, stp, parent);
Jeff Layton0b266932014-07-25 07:34:25 -04005554 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005555 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005557 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
Hou Tao3caf9172020-08-28 15:02:55 +08005559 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
J. Bruce Fields99c41512013-05-21 16:21:25 -04005560 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04005561 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005562 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005563out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005564 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5565 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005566 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04005567 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005568 open->op_recall = 1;
5569 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04005570
5571 /* 4.1 client asking for a delegation? */
5572 if (open->op_deleg_want)
5573 nfsd4_open_deleg_none_ext(open, status);
5574 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575}
5576
Benny Halevye27f49c2012-02-21 14:16:54 -08005577static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5578 struct nfs4_delegation *dp)
5579{
5580 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5581 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5582 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5583 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5584 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5585 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5586 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5587 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5588 }
5589 /* Otherwise the client must be confused wanting a delegation
5590 * it already has, therefore we don't return
5591 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5592 */
5593}
5594
Chuck Lever7e2ce0c2022-03-23 13:55:37 -04005595/**
5596 * nfsd4_process_open2 - finish open processing
5597 * @rqstp: the RPC transaction being executed
5598 * @current_fh: NFSv4 COMPOUND's current filehandle
5599 * @open: OPEN arguments
5600 *
5601 * If successful, (1) truncate the file if open->op_truncate was
5602 * set, (2) set open->op_stateid, (3) set open->op_delegation.
5603 *
5604 * Returns %nfs_ok on success; otherwise an nfs4stat value in
5605 * network byte order is returned.
5606 */
Al Virob37ad282006-10-19 23:28:59 -07005607__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5609{
Andy Adamson66689582009-04-03 08:28:45 +03005610 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005611 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005613 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07005614 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005615 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005616 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 /*
5619 * Lookup file; if found, lookup stateid and check open request,
5620 * and check for delegations in the process of being recalled.
5621 * If not found, create the nfs4_file struct
5622 */
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04005623 fp = find_or_add_file(open->op_file, current_fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04005624 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04005625 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07005626 if (status)
5627 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005628 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04005630 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07005631 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04005632 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07005633 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 }
5635
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005636 if (!stp) {
5637 stp = init_open_stateid(fp, open);
5638 if (!open->op_stp)
5639 new_stp = true;
5640 }
5641
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 /*
5643 * OPEN the file, or upgrade an existing OPEN.
5644 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005645 *
5646 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005648 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005650 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005651 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005652 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 } else {
Dai Ngo3d694272022-05-02 14:19:22 -07005656 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005657 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005658 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005659 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005660 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005661 goto out;
5662 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005663
5664 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5665 open->op_odstate);
5666 if (stp->st_clnt_odstate == open->op_odstate)
5667 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005669
Jeff Layton9767feb2015-10-01 09:05:50 -04005670 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005671 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
Benny Halevyd24433c2012-02-16 20:57:17 +02005673 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005674 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5675 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5676 open->op_why_no_deleg = WND4_NOT_WANTED;
5677 goto nodeleg;
5678 }
5679 }
5680
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 /*
5682 * Attempt to hand out a delegation. No error return, because the
5683 * OPEN succeeds even if we fail.
5684 */
Jeff Layton876c5532022-07-26 16:45:30 +10005685 nfs4_open_delegation(open, stp, &resp->cstate.current_fh);
Benny Halevyd24433c2012-02-16 20:57:17 +02005686nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 status = nfs_ok;
Hou Tao3caf9172020-08-28 15:02:55 +08005688 trace_nfsd_open(&stp->st_stid.sc_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005690 /* 4.1 client trying to upgrade/downgrade delegation? */
5691 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005692 open->op_deleg_want)
5693 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005694
NeilBrown13cd2182005-06-23 22:03:10 -07005695 if (fp)
5696 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005697 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005698 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 /*
5700 * To finish the open response, we just need to set the rflags.
5701 */
5702 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005703 if (nfsd4_has_session(&resp->cstate))
5704 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5705 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005707
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005708 if (dp)
5709 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005710 if (stp)
5711 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712
5713 return status;
5714}
5715
Jeff Layton58fb12e2014-07-29 21:34:27 -04005716void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005717 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005718{
5719 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005720 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005721
Jeff Laytond3134b12014-07-29 21:34:32 -04005722 nfsd4_cstate_assign_replay(cstate, so);
5723 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005724 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005725 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005726 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005727 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005728 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005729 if (open->op_odstate)
5730 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005731}
5732
Al Virob37ad282006-10-19 23:28:59 -07005733__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005734nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005735 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005737 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005739 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005740 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005742 trace_nfsd_clid_renew(clid);
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005743 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005744 if (status)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005745 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005746 clp = cstate->clp;
NeilBrownea1da632005-06-23 22:04:17 -07005747 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005748 && clp->cl_cb_state != NFSD4_CB_UP)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005749 return nfserr_cb_path_down;
5750 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751}
5752
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005753void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005754nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005755{
Jeff Layton33dcc482012-04-10 11:08:48 -04005756 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005757 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005758 return;
5759
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005760 trace_nfsd_grace_complete(nn);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005761 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005762 /*
5763 * If the server goes down again right now, an NFSv4
5764 * client will still be allowed to reclaim after it comes back up,
5765 * even if it hasn't yet had a chance to reclaim state this time.
5766 *
5767 */
Jeff Layton919b8042014-09-12 16:40:20 -04005768 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005769 /*
5770 * At this point, NFSv4 clients can still reclaim. But if the
5771 * server crashes, any that have not yet reclaimed will be out
5772 * of luck on the next boot.
5773 *
5774 * (NFSv4.1+ clients are considered to have reclaimed once they
5775 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5776 * have reclaimed after their first OPEN.)
5777 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005778 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005779 /*
5780 * At this point, and once lockd and/or any other containers
5781 * exit their grace period, further reclaims will fail and
5782 * regular locking can resume.
5783 */
NeilBrowna76b4312005-06-23 22:04:01 -07005784}
5785
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005786/*
5787 * If we've waited a lease period but there are still clients trying to
5788 * reclaim, wait a little longer to give them a chance to finish.
5789 */
5790static bool clients_still_reclaiming(struct nfsd_net *nn)
5791{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005792 time64_t double_grace_period_end = nn->boot_time +
5793 2 * nn->nfsd4_lease;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005794
Scott Mayhew362063a2019-03-26 18:06:28 -04005795 if (nn->track_reclaim_completes &&
5796 atomic_read(&nn->nr_reclaim_complete) ==
5797 nn->reclaim_str_hashtbl_size)
5798 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005799 if (!nn->somebody_reclaimed)
5800 return false;
5801 nn->somebody_reclaimed = false;
5802 /*
5803 * If we've given them *two* lease times to reclaim, and they're
5804 * still not done, give up:
5805 */
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005806 if (ktime_get_boottime_seconds() > double_grace_period_end)
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005807 return false;
5808 return true;
5809}
5810
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005811struct laundry_time {
5812 time64_t cutoff;
5813 time64_t new_timeo;
5814};
5815
5816static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5817{
5818 time64_t time_remaining;
5819
5820 if (last_refresh < lt->cutoff)
5821 return true;
5822 time_remaining = last_refresh - lt->cutoff;
5823 lt->new_timeo = min(lt->new_timeo, time_remaining);
5824 return false;
5825}
5826
Dai Ngof4e44b32021-05-21 15:09:37 -04005827#ifdef CONFIG_NFSD_V4_2_INTER_SSC
5828void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
5829{
5830 spin_lock_init(&nn->nfsd_ssc_lock);
5831 INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
5832 init_waitqueue_head(&nn->nfsd_ssc_waitq);
5833}
5834EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
5835
5836/*
5837 * This is called when nfsd is being shutdown, after all inter_ssc
5838 * cleanup were done, to destroy the ssc delayed unmount list.
5839 */
5840static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
5841{
Dai Ngof47dc2d2021-06-03 20:02:26 -04005842 struct nfsd4_ssc_umount_item *ni = NULL;
Dai Ngof4e44b32021-05-21 15:09:37 -04005843 struct nfsd4_ssc_umount_item *tmp;
5844
5845 spin_lock(&nn->nfsd_ssc_lock);
5846 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5847 list_del(&ni->nsui_list);
5848 spin_unlock(&nn->nfsd_ssc_lock);
5849 mntput(ni->nsui_vfsmount);
5850 kfree(ni);
5851 spin_lock(&nn->nfsd_ssc_lock);
5852 }
5853 spin_unlock(&nn->nfsd_ssc_lock);
5854}
5855
5856static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
5857{
5858 bool do_wakeup = false;
Colin Ian King8e70bf22021-09-25 23:58:41 +01005859 struct nfsd4_ssc_umount_item *ni = NULL;
Dai Ngof4e44b32021-05-21 15:09:37 -04005860 struct nfsd4_ssc_umount_item *tmp;
5861
5862 spin_lock(&nn->nfsd_ssc_lock);
5863 list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5864 if (time_after(jiffies, ni->nsui_expire)) {
5865 if (refcount_read(&ni->nsui_refcnt) > 1)
5866 continue;
5867
5868 /* mark being unmount */
5869 ni->nsui_busy = true;
5870 spin_unlock(&nn->nfsd_ssc_lock);
5871 mntput(ni->nsui_vfsmount);
5872 spin_lock(&nn->nfsd_ssc_lock);
5873
5874 /* waiters need to start from begin of list */
5875 list_del(&ni->nsui_list);
5876 kfree(ni);
5877
5878 /* wakeup ssc_connect waiters */
5879 do_wakeup = true;
5880 continue;
5881 }
5882 break;
5883 }
5884 if (do_wakeup)
5885 wake_up_all(&nn->nfsd_ssc_waitq);
5886 spin_unlock(&nn->nfsd_ssc_lock);
5887}
5888#endif
5889
Dai Ngo27431af2022-05-02 14:19:26 -07005890/* Check if any lock belonging to this lockowner has any blockers */
Dai Ngo3d694272022-05-02 14:19:22 -07005891static bool
Dai Ngo27431af2022-05-02 14:19:26 -07005892nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
5893{
5894 struct file_lock_context *ctx;
5895 struct nfs4_ol_stateid *stp;
5896 struct nfs4_file *nf;
5897
5898 list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
5899 nf = stp->st_stid.sc_file;
5900 ctx = nf->fi_inode->i_flctx;
5901 if (!ctx)
5902 continue;
5903 if (locks_owner_has_blockers(ctx, lo))
5904 return true;
5905 }
5906 return false;
5907}
5908
5909static bool
5910nfs4_anylock_blockers(struct nfs4_client *clp)
Dai Ngo3d694272022-05-02 14:19:22 -07005911{
5912 int i;
5913 struct nfs4_stateowner *so;
Dai Ngo27431af2022-05-02 14:19:26 -07005914 struct nfs4_lockowner *lo;
Dai Ngo3d694272022-05-02 14:19:22 -07005915
Dai Ngo27431af2022-05-02 14:19:26 -07005916 if (atomic_read(&clp->cl_delegs_in_recall))
5917 return true;
Dai Ngo3d694272022-05-02 14:19:22 -07005918 spin_lock(&clp->cl_lock);
5919 for (i = 0; i < OWNER_HASH_SIZE; i++) {
5920 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
5921 so_strhash) {
5922 if (so->so_is_open_owner)
5923 continue;
Dai Ngo27431af2022-05-02 14:19:26 -07005924 lo = lockowner(so);
5925 if (nfs4_lockowner_has_blockers(lo)) {
5926 spin_unlock(&clp->cl_lock);
5927 return true;
5928 }
Dai Ngo3d694272022-05-02 14:19:22 -07005929 }
5930 }
5931 spin_unlock(&clp->cl_lock);
5932 return false;
5933}
5934
Dai Ngo66af2572022-05-02 14:19:21 -07005935static void
5936nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
5937 struct laundry_time *lt)
5938{
Dai Ngo4271c2c2022-07-15 16:54:53 -07005939 unsigned int maxreap, reapcnt = 0;
Dai Ngo66af2572022-05-02 14:19:21 -07005940 struct list_head *pos, *next;
5941 struct nfs4_client *clp;
5942
Dai Ngo4271c2c2022-07-15 16:54:53 -07005943 maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
5944 NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
Dai Ngo66af2572022-05-02 14:19:21 -07005945 INIT_LIST_HEAD(reaplist);
5946 spin_lock(&nn->client_lock);
5947 list_for_each_safe(pos, next, &nn->client_lru) {
5948 clp = list_entry(pos, struct nfs4_client, cl_lru);
5949 if (clp->cl_state == NFSD4_EXPIRABLE)
5950 goto exp_client;
5951 if (!state_expired(lt, clp->cl_time))
5952 break;
Dai Ngo3a4ea23d2022-09-14 08:54:25 -07005953 if (!atomic_read(&clp->cl_rpc_users)) {
5954 if (clp->cl_state == NFSD4_ACTIVE)
5955 atomic_inc(&nn->nfsd_courtesy_clients);
Dai Ngo66af2572022-05-02 14:19:21 -07005956 clp->cl_state = NFSD4_COURTESY;
Dai Ngo3a4ea23d2022-09-14 08:54:25 -07005957 }
Dai Ngo4271c2c2022-07-15 16:54:53 -07005958 if (!client_has_state(clp))
Dai Ngo66af2572022-05-02 14:19:21 -07005959 goto exp_client;
Dai Ngo4271c2c2022-07-15 16:54:53 -07005960 if (!nfs4_anylock_blockers(clp))
5961 if (reapcnt >= maxreap)
5962 continue;
Dai Ngo66af2572022-05-02 14:19:21 -07005963exp_client:
Dai Ngo4271c2c2022-07-15 16:54:53 -07005964 if (!mark_client_expired_locked(clp)) {
5965 list_add(&clp->cl_lru, reaplist);
5966 reapcnt++;
Dai Ngo66af2572022-05-02 14:19:21 -07005967 }
5968 }
5969 spin_unlock(&nn->client_lock);
5970}
5971
Dai Ngo7746b322022-09-14 08:54:26 -07005972static void
5973nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn,
5974 struct list_head *reaplist)
5975{
5976 unsigned int maxreap = 0, reapcnt = 0;
5977 struct list_head *pos, *next;
5978 struct nfs4_client *clp;
5979
5980 maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN;
5981 INIT_LIST_HEAD(reaplist);
5982
5983 spin_lock(&nn->client_lock);
5984 list_for_each_safe(pos, next, &nn->client_lru) {
5985 clp = list_entry(pos, struct nfs4_client, cl_lru);
5986 if (clp->cl_state == NFSD4_ACTIVE)
5987 break;
5988 if (reapcnt >= maxreap)
5989 break;
5990 if (!mark_client_expired_locked(clp)) {
5991 list_add(&clp->cl_lru, reaplist);
5992 reapcnt++;
5993 }
5994 }
5995 spin_unlock(&nn->client_lock);
5996}
5997
5998static void
5999nfs4_process_client_reaplist(struct list_head *reaplist)
6000{
6001 struct list_head *pos, *next;
6002 struct nfs4_client *clp;
6003
6004 list_for_each_safe(pos, next, reaplist) {
6005 clp = list_entry(pos, struct nfs4_client, cl_lru);
6006 trace_nfsd_clid_purged(&clp->cl_clientid);
6007 list_del_init(&clp->cl_lru);
6008 expire_client(clp);
6009 }
6010}
6011
Arnd Bergmann20b7d862019-11-04 16:31:52 +01006012static time64_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006013nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006015 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04006017 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04006018 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 struct list_head *pos, *next, reaplist;
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05006020 struct laundry_time lt = {
6021 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
6022 .new_timeo = nn->nfsd4_lease
6023 };
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006024 struct nfs4_cpntf_state *cps;
6025 copy_stateid_t *cps_t;
6026 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006028 if (clients_still_reclaiming(nn)) {
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05006029 lt.new_timeo = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006030 goto out;
6031 }
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03006032 nfsd4_end_grace(nn);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006033
6034 spin_lock(&nn->s2s_cp_lock);
6035 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
6036 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
Chuck Lever781fde12022-09-22 13:10:35 -04006037 if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID &&
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05006038 state_expired(&lt, cps->cpntf_time))
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006039 _free_cpntf_state_locked(nn, cps);
6040 }
6041 spin_unlock(&nn->s2s_cp_lock);
Dai Ngo66af2572022-05-02 14:19:21 -07006042 nfs4_get_client_reaplist(nn, &reaplist, &lt);
Dai Ngo7746b322022-09-14 08:54:26 -07006043 nfs4_process_client_reaplist(&reaplist);
6044
Benny Halevycdc975052014-05-30 09:09:30 -04006045 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04006046 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05006048 if (!state_expired(&lt, dp->dl_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 break;
Jeff Layton3fcbbd22015-08-24 12:41:48 -04006050 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04006051 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 }
Benny Halevycdc975052014-05-30 09:09:30 -04006053 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04006054 while (!list_empty(&reaplist)) {
6055 dp = list_first_entry(&reaplist, struct nfs4_delegation,
6056 dl_recall_lru);
6057 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006058 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 }
Jeff Layton217526e2014-07-30 08:27:11 -04006060
6061 spin_lock(&nn->client_lock);
6062 while (!list_empty(&nn->close_lru)) {
6063 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
6064 oo_close_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05006065 if (!state_expired(&lt, oo->oo_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 break;
Jeff Layton217526e2014-07-30 08:27:11 -04006067 list_del_init(&oo->oo_close_lru);
6068 stp = oo->oo_last_closed_stid;
6069 oo->oo_last_closed_stid = NULL;
6070 spin_unlock(&nn->client_lock);
6071 nfs4_put_stid(&stp->st_stid);
6072 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 }
Jeff Layton217526e2014-07-30 08:27:11 -04006074 spin_unlock(&nn->client_lock);
6075
Jeff Layton7919d0a2016-09-16 16:28:25 -04006076 /*
6077 * It's possible for a client to try and acquire an already held lock
6078 * that is being held for a long time, and then lose interest in it.
6079 * So, we clean out any un-revisited request after a lease period
6080 * under the assumption that the client is no longer interested.
6081 *
6082 * RFC5661, sec. 9.6 states that the client must not rely on getting
6083 * notifications and must continue to poll for locks, even when the
6084 * server supports them. Thus this shouldn't lead to clients blocking
6085 * indefinitely once the lock does become free.
6086 */
6087 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04006088 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006089 while (!list_empty(&nn->blocked_locks_lru)) {
6090 nbl = list_first_entry(&nn->blocked_locks_lru,
6091 struct nfsd4_blocked_lock, nbl_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05006092 if (!state_expired(&lt, nbl->nbl_time))
Jeff Layton7919d0a2016-09-16 16:28:25 -04006093 break;
Jeff Layton7919d0a2016-09-16 16:28:25 -04006094 list_move(&nbl->nbl_lru, &reaplist);
6095 list_del_init(&nbl->nbl_list);
6096 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006097 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006098
6099 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05006100 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04006101 struct nfsd4_blocked_lock, nbl_lru);
6102 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006103 free_blocked_lock(nbl);
6104 }
Dai Ngof4e44b32021-05-21 15:09:37 -04006105#ifdef CONFIG_NFSD_V4_2_INTER_SSC
6106 /* service the server-to-server copy delayed unmount list */
6107 nfsd4_ssc_expire_umount(nn);
6108#endif
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006109out:
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05006110 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111}
6112
Harvey Harrisona254b242008-02-20 12:49:00 -08006113static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08006114
6115static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006116laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01006118 time64_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08006119 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006120 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6121 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006123 t = nfs4_laundromat(nn);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03006124 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125}
6126
Dai Ngo7746b322022-09-14 08:54:26 -07006127static void
6128courtesy_client_reaper(struct work_struct *reaper)
6129{
6130 struct list_head reaplist;
6131 struct delayed_work *dwork = to_delayed_work(reaper);
6132 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
6133 nfsd_shrinker_work);
6134
6135 nfs4_get_courtesy_client_reaplist(nn, &reaplist);
6136 nfs4_process_client_reaplist(&reaplist);
6137}
6138
Jeff Layton8fcd4612015-07-30 06:57:46 -04006139static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07006140{
Jeff Layton8fcd4612015-07-30 06:57:46 -04006141 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006142 return nfserr_bad_stateid;
6143 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144}
6145
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006147__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148{
Al Virob37ad282006-10-19 23:28:59 -07006149 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150
J. Bruce Fields02921912010-07-29 15:16:59 -04006151 /* For lock stateid's, we test the parent open, not the lock: */
6152 if (stp->st_openstp)
6153 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04006154 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04006156 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 goto out;
6158 status = nfs_ok;
6159out:
6160 return status;
6161}
6162
Al Virob37ad282006-10-19 23:28:59 -07006163static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006164check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006166 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04006168 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006169 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 * conflicting state; so we must wait out the grace period. */
6171 return nfserr_grace;
6172 } else if (flags & WR_STATE)
6173 return nfs4_share_conflict(current_fh,
6174 NFS4_SHARE_DENY_WRITE);
6175 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
6176 return nfs4_share_conflict(current_fh,
6177 NFS4_SHARE_DENY_READ);
6178}
6179
J. Bruce Fields57b7b432012-04-25 17:58:50 -04006180static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05006181{
Andy Adamson66689582009-04-03 08:28:45 +03006182 /*
6183 * When sessions are used the stateid generation number is ignored
6184 * when it is zero.
6185 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04006186 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04006187 return nfs_ok;
6188
6189 if (in->si_generation == ref->si_generation)
6190 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03006191
J. Bruce Fields0836f582008-01-26 19:08:12 -05006192 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04006193 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05006194 return nfserr_bad_stateid;
6195 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04006196 * However, we could see a stateid from the past, even from a
6197 * non-buggy client. For example, if the client sends a lock
6198 * while some IO is outstanding, the lock may bump si_generation
6199 * while the IO is still in flight. The client could avoid that
6200 * situation by waiting for responses on all the IO requests,
6201 * but better performance may result in retrying IO that
6202 * receives an old_stateid error if requests are rarely
6203 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05006204 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04006205 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05006206}
6207
Trond Myklebust03da3162017-11-03 08:00:16 -04006208static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
6209{
6210 __be32 ret;
6211
6212 spin_lock(&s->sc_lock);
6213 ret = nfsd4_verify_open_stid(s);
6214 if (ret == nfs_ok)
6215 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
6216 spin_unlock(&s->sc_lock);
6217 return ret;
6218}
6219
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02006220static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
6221{
6222 if (ols->st_stateowner->so_is_open_owner &&
6223 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
6224 return nfserr_bad_stateid;
6225 return nfs_ok;
6226}
6227
Chuck Lever7df302f2012-05-29 13:56:37 -04006228static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04006229{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04006230 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04006231 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04006232
Andrew Elbleae254dac2017-11-09 13:41:10 -05006233 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6234 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04006235 return status;
J. Bruce Fields663e36f2020-03-19 10:18:49 -04006236 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04006237 return status;
Jeff Layton1af71cc2014-07-29 21:34:14 -04006238 spin_lock(&cl->cl_lock);
6239 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04006240 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04006241 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04006242 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04006243 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04006244 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04006245 switch (s->sc_type) {
6246 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04006247 status = nfs_ok;
6248 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006249 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04006250 status = nfserr_deleg_revoked;
6251 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04006252 case NFS4_OPEN_STID:
6253 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02006254 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04006255 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04006256 default:
6257 printk("unknown stateid type %x\n", s->sc_type);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006258 fallthrough;
J. Bruce Fields23340032013-04-09 17:42:28 -04006259 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04006260 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04006261 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04006262 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04006263out_unlock:
6264 spin_unlock(&cl->cl_lock);
6265 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04006266}
6267
Christoph Hellwigcd61c522014-08-14 08:44:57 +02006268__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006269nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
6270 stateid_t *stateid, unsigned char typemask,
6271 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006272{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04006273 __be32 status;
Jeff Layton4d014162022-09-26 12:38:44 -04006274 struct nfs4_stid *stid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04006275 bool return_revoked = false;
6276
6277 /*
6278 * only return revoked delegations if explicitly asked.
6279 * otherwise we report revoked or bad_stateid status.
6280 */
6281 if (typemask & NFS4_REVOKED_DELEG_STID)
6282 return_revoked = true;
6283 else if (typemask & NFS4_DELEG_STID)
6284 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006285
Andrew Elbleae254dac2017-11-09 13:41:10 -05006286 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6287 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006288 return nfserr_bad_stateid;
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05006289 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04006290 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04006291 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04006292 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006293 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04006294 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04006295 if (status)
6296 return status;
Jeff Layton4d014162022-09-26 12:38:44 -04006297 stid = find_stateid_by_type(cstate->clp, stateid, typemask);
6298 if (!stid)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006299 return nfserr_bad_stateid;
Jeff Layton4d014162022-09-26 12:38:44 -04006300 if ((stid->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
6301 nfs4_put_stid(stid);
Andrew Elble95da1b3a2017-11-03 14:06:31 -04006302 if (cstate->minorversion)
6303 return nfserr_deleg_revoked;
6304 return nfserr_bad_stateid;
6305 }
Jeff Layton4d014162022-09-26 12:38:44 -04006306 *s = stid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006307 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006308}
6309
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006310static struct nfsd_file *
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006311nfs4_find_file(struct nfs4_stid *s, int flags)
6312{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006313 if (!s)
6314 return NULL;
6315
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006316 switch (s->sc_type) {
6317 case NFS4_DELEG_STID:
6318 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
6319 return NULL;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006320 return nfsd_file_get(s->sc_file->fi_deleg_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006321 case NFS4_OPEN_STID:
6322 case NFS4_LOCK_STID:
6323 if (flags & RD_STATE)
6324 return find_readable_file(s->sc_file);
6325 else
6326 return find_writeable_file(s->sc_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006327 }
6328
6329 return NULL;
6330}
6331
6332static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05006333nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006334{
6335 __be32 status;
6336
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006337 status = nfsd4_check_openowner_confirmed(ols);
6338 if (status)
6339 return status;
6340 return nfs4_check_openmode(ols, flags);
6341}
6342
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006343static __be32
6344nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
Jeff Layton5c4583b2019-08-18 14:18:54 -04006345 struct nfsd_file **nfp, int flags)
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006346{
6347 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006348 struct nfsd_file *nf;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006349 __be32 status;
6350
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006351 nf = nfs4_find_file(s, flags);
6352 if (nf) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006353 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
6354 acc | NFSD_MAY_OWNER_OVERRIDE);
Jeff Layton5c4583b2019-08-18 14:18:54 -04006355 if (status) {
6356 nfsd_file_put(nf);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006357 goto out;
Jeff Layton5c4583b2019-08-18 14:18:54 -04006358 }
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006359 } else {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006360 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006361 if (status)
6362 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006363 }
Jeff Layton5c4583b2019-08-18 14:18:54 -04006364 *nfp = nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006365out:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006366 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006367}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006368static void
6369_free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6370{
Chuck Lever781fde12022-09-22 13:10:35 -04006371 WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID);
6372 if (!refcount_dec_and_test(&cps->cp_stateid.cs_count))
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006373 return;
6374 list_del(&cps->cp_list);
6375 idr_remove(&nn->s2s_cp_stateids,
Chuck Lever781fde12022-09-22 13:10:35 -04006376 cps->cp_stateid.cs_stid.si_opaque.so_id);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006377 kfree(cps);
6378}
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006379/*
6380 * A READ from an inter server to server COPY will have a
6381 * copy stateid. Look up the copy notify stateid from the
6382 * idr structure and take a reference on it.
6383 */
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04006384__be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6385 struct nfs4_client *clp,
6386 struct nfs4_cpntf_state **cps)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006387{
6388 copy_stateid_t *cps_t;
6389 struct nfs4_cpntf_state *state = NULL;
6390
6391 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
6392 return nfserr_bad_stateid;
6393 spin_lock(&nn->s2s_cp_lock);
6394 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
6395 if (cps_t) {
6396 state = container_of(cps_t, struct nfs4_cpntf_state,
6397 cp_stateid);
Chuck Lever781fde12022-09-22 13:10:35 -04006398 if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) {
Dan Carpenter5277a792019-12-04 10:59:36 +03006399 state = NULL;
6400 goto unlock;
6401 }
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04006402 if (!clp)
Chuck Lever781fde12022-09-22 13:10:35 -04006403 refcount_inc(&state->cp_stateid.cs_count);
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04006404 else
6405 _free_cpntf_state_locked(nn, state);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006406 }
Dan Carpenter5277a792019-12-04 10:59:36 +03006407unlock:
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006408 spin_unlock(&nn->s2s_cp_lock);
6409 if (!state)
6410 return nfserr_bad_stateid;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04006411 if (!clp && state)
6412 *cps = state;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006413 return 0;
6414}
6415
6416static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6417 struct nfs4_stid **stid)
6418{
6419 __be32 status;
6420 struct nfs4_cpntf_state *cps = NULL;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05006421 struct nfs4_client *found;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006422
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04006423 status = manage_cpntf_state(nn, st, NULL, &cps);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006424 if (status)
6425 return status;
6426
Arnd Bergmann20b7d862019-11-04 16:31:52 +01006427 cps->cpntf_time = ktime_get_boottime_seconds();
J. Bruce Fields47fdb222021-01-21 17:57:42 -05006428
6429 status = nfserr_expired;
6430 found = lookup_clientid(&cps->cp_p_clid, true, nn);
6431 if (!found)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006432 goto out;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05006433
6434 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
6435 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
6436 if (*stid)
6437 status = nfs_ok;
6438 else
6439 status = nfserr_bad_stateid;
6440
6441 put_client_renew(found);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006442out:
6443 nfs4_put_cpntf_state(nn, cps);
6444 return status;
6445}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006446
6447void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6448{
6449 spin_lock(&nn->s2s_cp_lock);
6450 _free_cpntf_state_locked(nn, cps);
6451 spin_unlock(&nn->s2s_cp_lock);
6452}
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006453
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006455 * Checks for stateid operations
6456 */
Al Virob37ad282006-10-19 23:28:59 -07006457__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006458nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01006459 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006460 stateid_t *stateid, int flags, struct nfsd_file **nfp,
6461 struct nfs4_stid **cstid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006463 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006464 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006465 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006466 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467
Jeff Layton5c4583b2019-08-18 14:18:54 -04006468 if (nfp)
6469 *nfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006471 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
J. Bruce Fields074b07d2022-01-05 14:15:03 -05006472 if (cstid)
6473 status = nfserr_bad_stateid;
6474 else
6475 status = check_special_stateids(net, fhp, stateid,
6476 flags);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006477 goto done;
6478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006480 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04006481 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006482 &s, nn);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006483 if (status == nfserr_bad_stateid)
6484 status = find_cpntf_state(nn, stateid, &s);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006485 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04006486 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04006487 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006488 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04006489 if (status)
6490 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006491
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006492 switch (s->sc_type) {
6493 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006494 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006495 break;
6496 case NFS4_OPEN_STID:
6497 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05006498 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006499 break;
6500 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04006501 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006502 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04006504 if (status)
6505 goto out;
6506 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02006507
Christoph Hellwigaf90f702015-06-18 16:45:00 +02006508done:
Jeff Layton5c4583b2019-08-18 14:18:54 -04006509 if (status == nfs_ok && nfp)
6510 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511out:
Olga Kornievskaia624322f2019-10-04 16:34:26 -04006512 if (s) {
6513 if (!status && cstid)
6514 *cstid = s;
6515 else
6516 nfs4_put_stid(s);
6517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 return status;
6519}
6520
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006521/*
Bryan Schumaker17456802011-07-13 10:50:48 -04006522 * Test if the stateid is valid
6523 */
6524__be32
6525nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006526 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04006527{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006528 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb4202012-01-27 10:22:49 -05006529 struct nfsd4_test_stateid_id *stateid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006530 struct nfs4_client *cl = cstate->clp;
Bryan Schumaker03cfb4202012-01-27 10:22:49 -05006531
Bryan Schumaker03cfb4202012-01-27 10:22:49 -05006532 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04006533 stateid->ts_id_status =
6534 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb4202012-01-27 10:22:49 -05006535
Bryan Schumaker17456802011-07-13 10:50:48 -04006536 return nfs_ok;
6537}
6538
Chuck Lever42691392016-08-11 10:37:30 -04006539static __be32
6540nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
6541{
6542 struct nfs4_ol_stateid *stp = openlockstateid(s);
6543 __be32 ret;
6544
Trond Myklebust659aefb2017-11-03 08:00:13 -04006545 ret = nfsd4_lock_ol_stateid(stp);
6546 if (ret)
6547 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04006548
6549 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6550 if (ret)
6551 goto out;
6552
6553 ret = nfserr_locks_held;
6554 if (check_for_locks(stp->st_stid.sc_file,
6555 lockowner(stp->st_stateowner)))
6556 goto out;
6557
6558 release_lock_stateid(stp);
6559 ret = nfs_ok;
6560
6561out:
6562 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04006563out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04006564 nfs4_put_stid(s);
6565 return ret;
6566}
6567
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006568__be32
6569nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006570 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006571{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006572 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006573 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006574 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006575 struct nfs4_delegation *dp;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006576 struct nfs4_client *cl = cstate->clp;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006577 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006578
Jeff Layton1af71cc2014-07-29 21:34:14 -04006579 spin_lock(&cl->cl_lock);
6580 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006581 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04006582 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04006583 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006584 switch (s->sc_type) {
6585 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006586 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04006587 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006588 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04006589 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
6590 if (ret)
6591 break;
6592 ret = nfserr_locks_held;
6593 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04006594 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04006595 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006596 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006597 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04006598 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006599 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006600 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04006601 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006602 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04006603 list_del_init(&dp->dl_recall_lru);
6604 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04006605 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006606 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04006607 goto out;
6608 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006609 }
Trond Myklebust03da3162017-11-03 08:00:16 -04006610 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006611out_unlock:
6612 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006613out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006614 return ret;
6615}
6616
NeilBrown4c4cd222005-07-07 17:59:27 -07006617static inline int
6618setlkflg (int type)
6619{
6620 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6621 RD_STATE : WR_STATE;
6622}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006624static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
J. Bruce Fieldsc0a5d93e2011-09-06 15:19:46 -04006625{
6626 struct svc_fh *current_fh = &cstate->current_fh;
6627 struct nfs4_stateowner *sop = stp->st_stateowner;
6628 __be32 status;
6629
J. Bruce Fieldsc0a5d93e2011-09-06 15:19:46 -04006630 status = nfsd4_check_seqid(cstate, sop, seqid);
6631 if (status)
6632 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04006633 status = nfsd4_lock_ol_stateid(stp);
6634 if (status != nfs_ok)
6635 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006636 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04006637 if (status == nfs_ok)
6638 status = nfs4_check_fh(current_fh, &stp->st_stid);
6639 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006640 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04006641 return status;
J. Bruce Fieldsc0a5d93e2011-09-06 15:19:46 -04006642}
6643
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644/*
6645 * Checks for sequence id mutating operations.
6646 */
Al Virob37ad282006-10-19 23:28:59 -07006647static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03006648nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04006649 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006650 struct nfs4_ol_stateid **stpp,
6651 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652{
J. Bruce Fields0836f582008-01-26 19:08:12 -05006653 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006654 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006655 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006657 trace_nfsd_preprocess(seqid, stateid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006658
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006660 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d93e2011-09-06 15:19:46 -04006661 if (status)
6662 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006663 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04006664 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006666 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006667 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006668 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04006669 else
6670 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006671 return status;
J. Bruce Fieldsc0a5d93e2011-09-06 15:19:46 -04006672}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05006673
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006674static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6675 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d93e2011-09-06 15:19:46 -04006676{
6677 __be32 status;
6678 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006679 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680
J. Bruce Fieldsc0a5d93e2011-09-06 15:19:46 -04006681 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006682 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04006683 if (status)
6684 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006685 oo = openowner(stp->st_stateowner);
6686 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006687 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006688 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006689 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006690 }
6691 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07006692 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693}
6694
Al Virob37ad282006-10-19 23:28:59 -07006695__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006696nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006697 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006699 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07006700 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006701 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006702 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006703 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704
Al Viroa6a9f182013-09-16 10:57:01 -04006705 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6706 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006708 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07006709 if (status)
6710 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006712 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006713 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006714 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006715 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006716 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006717 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006718 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006719 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006720 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04006721 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006722 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04006723 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04006724 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006725 mutex_unlock(&stp->st_mutex);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006726 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
Jeff Layton2a4317c2012-03-21 16:42:43 -04006727 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006728 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04006729put_stateid:
6730 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006732 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 return status;
6734}
6735
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006736static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737{
Jeff Layton82c5ff12012-05-11 09:45:13 -04006738 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006739 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04006740 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006741 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006742}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04006743
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006744static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6745{
6746 switch (to_access) {
6747 case NFS4_SHARE_ACCESS_READ:
6748 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6749 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6750 break;
6751 case NFS4_SHARE_ACCESS_WRITE:
6752 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6753 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6754 break;
6755 case NFS4_SHARE_ACCESS_BOTH:
6756 break;
6757 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006758 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 }
6760}
6761
Al Virob37ad282006-10-19 23:28:59 -07006762__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006763nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006764 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006766 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07006767 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006768 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006769 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770
Al Viroa6a9f182013-09-16 10:57:01 -04006771 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6772 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04006774 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02006775 if (od->od_deleg_want)
6776 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6777 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778
J. Bruce Fieldsc0a5d93e2011-09-06 15:19:46 -04006779 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006780 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006781 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04006784 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006785 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006787 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04006789 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006790 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006792 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006794 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04006795 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04006796 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006798put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006799 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006800 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006802 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803 return status;
6804}
6805
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006806static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6807{
Trond Myklebustacf92952014-06-30 11:48:37 -04006808 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04006809 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04006810 LIST_HEAD(reaplist);
Dai Ngo019805f2022-09-26 10:59:16 -07006811 struct nfs4_ol_stateid *stp;
Trond Myklebustacf92952014-06-30 11:48:37 -04006812
Jeff Layton2c41beb2014-07-29 21:34:41 -04006813 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04006814 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006815
Jeff Laytond83017f2014-07-29 21:34:42 -04006816 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04006817 if (unhashed)
6818 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04006819 spin_unlock(&clp->cl_lock);
Dai Ngo019805f2022-09-26 10:59:16 -07006820 list_for_each_entry(stp, &reaplist, st_locks)
6821 nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
Jeff Laytond83017f2014-07-29 21:34:42 -04006822 free_ol_stateid_reaplist(&reaplist);
6823 } else {
6824 spin_unlock(&clp->cl_lock);
6825 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04006826 if (unhashed)
6827 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04006828 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04006829}
6830
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831/*
6832 * nfs4_unlock_state() called after encode
6833 */
Al Virob37ad282006-10-19 23:28:59 -07006834__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006835nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006836 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006838 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07006839 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006840 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006841 struct net *net = SVC_NET(rqstp);
6842 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843
Al Viroa6a9f182013-09-16 10:57:01 -04006844 dprintk("NFSD: nfsd4_close on file %pd\n",
6845 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006847 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6848 &close->cl_stateid,
6849 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006850 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006851 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006852 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006854
6855 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2decac52018-02-15 15:16:18 -05006856
6857 /*
6858 * Technically we don't _really_ have to increment or copy it, since
6859 * it should just be gone after this operation and we clobber the
6860 * copied value below, but we continue to do so here just to ensure
6861 * that racing ops see that there was a state change.
6862 */
Jeff Layton9767feb2015-10-01 09:05:50 -04006863 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006865 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006866 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006867
Jeff Laytonbd2decac52018-02-15 15:16:18 -05006868 /* v4.1+ suggests that we send a special stateid in here, since the
6869 * clients should just ignore this anyway. Since this is not useful
6870 * for v4.0 clients either, we set it to the special close_stateid
6871 * universally.
6872 *
6873 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6874 */
6875 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04006876
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006877 /* put reference from nfs4_preprocess_seqid_op */
6878 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 return status;
6881}
6882
Al Virob37ad282006-10-19 23:28:59 -07006883__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006884nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006885 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006887 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006888 struct nfs4_delegation *dp;
6889 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006890 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07006891 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006892 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006894 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006895 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006897 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006898 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006899 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006900 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04006901 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006902 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04006903 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006904
Chuck Leverc0353622022-09-08 18:14:00 -04006905 wake_up_var(d_inode(cstate->current_fh.fh_dentry));
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006906 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006907put_stateid:
6908 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909out:
6910 return status;
6911}
6912
Benny Halevy87df4de2008-12-15 19:42:03 +02006913/* last octet in a range */
6914static inline u64
6915last_byte_offset(u64 start, u64 len)
6916{
6917 u64 end;
6918
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006919 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006920 end = start + len;
6921 return end > start ? end - 1: NFS4_MAX_UINT64;
6922}
6923
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924/*
6925 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6926 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6927 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6928 * locking, this prevents us from being completely protocol-compliant. The
6929 * real solution to this problem is to start using unsigned file offsets in
6930 * the VFS, but this is a very deep change!
6931 */
6932static inline void
6933nfs4_transform_lock_offset(struct file_lock *lock)
6934{
6935 if (lock->fl_start < 0)
6936 lock->fl_start = OFFSET_MAX;
6937 if (lock->fl_end < 0)
6938 lock->fl_end = OFFSET_MAX;
6939}
6940
Jeff Laytoncae80b32015-04-03 09:04:04 -04006941static fl_owner_t
Chuck Lever35aff062022-02-16 11:26:06 -05006942nfsd4_lm_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006943{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006944 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6945
6946 nfs4_get_stateowner(&lo->lo_owner);
6947 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006948}
6949
Jeff Laytoncae80b32015-04-03 09:04:04 -04006950static void
Chuck Lever35aff062022-02-16 11:26:06 -05006951nfsd4_lm_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006952{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006953 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006954
Jeff Laytoncae80b32015-04-03 09:04:04 -04006955 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006956 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006957}
6958
Dai Ngo27431af2022-05-02 14:19:26 -07006959/* return pointer to struct nfs4_client if client is expirable */
6960static bool
6961nfsd4_lm_lock_expirable(struct file_lock *cfl)
6962{
6963 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)cfl->fl_owner;
6964 struct nfs4_client *clp = lo->lo_owner.so_client;
6965 struct nfsd_net *nn;
6966
6967 if (try_to_expire_client(clp)) {
6968 nn = net_generic(clp->net, nfsd_net_id);
6969 mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
6970 return true;
6971 }
6972 return false;
6973}
6974
6975/* schedule laundromat to run immediately and wait for it to complete */
6976static void
6977nfsd4_lm_expire_lock(void)
6978{
6979 flush_workqueue(laundry_wq);
6980}
6981
Jeff Layton76d348f2016-09-16 16:28:24 -04006982static void
6983nfsd4_lm_notify(struct file_lock *fl)
6984{
6985 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6986 struct net *net = lo->lo_owner.so_client->net;
6987 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6988 struct nfsd4_blocked_lock *nbl = container_of(fl,
6989 struct nfsd4_blocked_lock, nbl_lock);
6990 bool queue = false;
6991
Jeff Layton7919d0a2016-09-16 16:28:25 -04006992 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006993 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006994 if (!list_empty(&nbl->nbl_list)) {
6995 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006996 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006997 queue = true;
6998 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006999 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04007000
Chuck Lever2cde7f82021-05-14 15:57:08 -04007001 if (queue) {
7002 trace_nfsd_cb_notify_lock(lo, nbl);
Jeff Layton76d348f2016-09-16 16:28:24 -04007003 nfsd4_run_cb(&nbl->nbl_cb);
Chuck Lever2cde7f82021-05-14 15:57:08 -04007004 }
Jeff Layton76d348f2016-09-16 16:28:24 -04007005}
7006
Alexey Dobriyan7b021962009-09-21 17:01:12 -07007007static const struct lock_manager_operations nfsd_posix_mng_ops = {
Dai Ngo27431af2022-05-02 14:19:26 -07007008 .lm_mod_owner = THIS_MODULE,
Jeff Layton76d348f2016-09-16 16:28:24 -04007009 .lm_notify = nfsd4_lm_notify,
Chuck Lever35aff062022-02-16 11:26:06 -05007010 .lm_get_owner = nfsd4_lm_get_owner,
7011 .lm_put_owner = nfsd4_lm_put_owner,
Dai Ngo27431af2022-05-02 14:19:26 -07007012 .lm_lock_expirable = nfsd4_lm_lock_expirable,
7013 .lm_expire_lock = nfsd4_lm_expire_lock,
NeilBrownd5b90262006-04-10 22:55:22 -07007014};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015
7016static inline void
7017nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
7018{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007019 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020
NeilBrownd5b90262006-04-10 22:55:22 -07007021 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007022 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04007023 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
7024 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04007025 if (!deny->ld_owner.data)
7026 /* We just don't care that much */
7027 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007028 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07007029 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04007030nevermind:
7031 deny->ld_owner.len = 0;
7032 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07007033 deny->ld_clientid.cl_boot = 0;
7034 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 }
7036 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02007037 deny->ld_length = NFS4_MAX_UINT64;
7038 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 deny->ld_length = fl->fl_end - fl->fl_start + 1;
7040 deny->ld_type = NFS4_READ_LT;
7041 if (fl->fl_type != F_RDLCK)
7042 deny->ld_type = NFS4_WRITE_LT;
7043}
7044
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007045static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08007046find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047{
Trond Myklebustd4f04892014-07-29 21:34:36 -04007048 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04007049 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050
Trond Myklebust0a880a22014-07-30 08:27:10 -04007051 lockdep_assert_held(&clp->cl_lock);
7052
Trond Myklebustd4f04892014-07-29 21:34:36 -04007053 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
7054 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04007055 if (so->so_is_open_owner)
7056 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04007057 if (same_owner_str(so, owner))
7058 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 }
7060 return NULL;
7061}
7062
Trond Myklebustc58c6612014-07-29 21:34:35 -04007063static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08007064find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04007065{
7066 struct nfs4_lockowner *lo;
7067
Trond Myklebustd4f04892014-07-29 21:34:36 -04007068 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08007069 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04007070 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04007071 return lo;
7072}
7073
Jeff Layton8f4b54c2014-07-29 21:34:29 -04007074static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
7075{
Trond Myklebustc58c6612014-07-29 21:34:35 -04007076 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04007077}
7078
Jeff Layton6b180f02014-07-29 21:34:26 -04007079static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
7080{
7081 struct nfs4_lockowner *lo = lockowner(sop);
7082
7083 kmem_cache_free(lockowner_slab, lo);
7084}
7085
7086static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04007087 .so_unhash = nfs4_unhash_lockowner,
7088 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04007089};
7090
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091/*
7092 * Alloc a lock owner structure.
7093 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007094 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05007096 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007098static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04007099alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
7100 struct nfs4_ol_stateid *open_stp,
7101 struct nfsd4_lock *lock)
7102{
Trond Myklebustc58c6612014-07-29 21:34:35 -04007103 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007105 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
7106 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04007108 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007109 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
7110 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04007111 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04007112 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04007113 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08007114 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04007115 if (ret == NULL) {
7116 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04007117 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04007118 ret = lo;
7119 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08007120 nfs4_free_stateowner(&lo->lo_owner);
7121
Trond Myklebustd4f04892014-07-29 21:34:36 -04007122 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04007123 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124}
7125
Trond Myklebustfd1fd682017-11-03 08:00:14 -04007126static struct nfs4_ol_stateid *
Trond Myklebusta451b122020-03-01 18:21:38 -05007127find_lock_stateid(const struct nfs4_lockowner *lo,
7128 const struct nfs4_ol_stateid *ost)
Trond Myklebustfd1fd682017-11-03 08:00:14 -04007129{
7130 struct nfs4_ol_stateid *lst;
Trond Myklebustfd1fd682017-11-03 08:00:14 -04007131
Trond Myklebusta451b122020-03-01 18:21:38 -05007132 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
Trond Myklebustfd1fd682017-11-03 08:00:14 -04007133
Trond Myklebusta451b122020-03-01 18:21:38 -05007134 /* If ost is not hashed, ost->st_locks will not be valid */
7135 if (!nfs4_ol_stateid_unhashed(ost))
7136 list_for_each_entry(lst, &ost->st_locks, st_locks) {
7137 if (lst->st_stateowner == &lo->lo_owner) {
7138 refcount_inc(&lst->st_stid.sc_count);
7139 return lst;
7140 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04007141 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04007142 return NULL;
7143}
7144
Trond Myklebustbeeca192017-11-03 08:00:14 -04007145static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04007146init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
7147 struct nfs4_file *fp, struct inode *inode,
7148 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04007150 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04007151 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152
Trond Myklebustbeeca192017-11-03 08:00:14 -04007153 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05007154 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04007155retry:
7156 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05007157 if (nfs4_ol_stateid_unhashed(open_stp))
7158 goto out_close;
7159 retstp = find_lock_stateid(lo, open_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04007160 if (retstp)
Trond Myklebusta451b122020-03-01 18:21:38 -05007161 goto out_found;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03007162 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05007163 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04007164 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07007165 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04007166 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05007167 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07007169 stp->st_openstp = open_stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05007170 spin_lock(&fp->fi_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007171 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04007172 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04007173 list_add(&stp->st_perfile, &fp->fi_stateids);
7174 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04007175 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04007176 return stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05007177out_found:
7178 spin_unlock(&clp->cl_lock);
7179 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
7180 nfs4_put_stid(&retstp->st_stid);
7181 goto retry;
7182 }
7183 /* To keep mutex tracking happy */
7184 mutex_unlock(&stp->st_mutex);
7185 return retstp;
7186out_close:
7187 spin_unlock(&clp->cl_lock);
7188 mutex_unlock(&stp->st_mutex);
7189 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190}
7191
Jeff Laytonc53530d2014-06-30 11:48:39 -04007192static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04007193find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
7194 struct inode *inode, struct nfs4_ol_stateid *ost,
7195 bool *new)
7196{
7197 struct nfs4_stid *ns = NULL;
7198 struct nfs4_ol_stateid *lst;
7199 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7200 struct nfs4_client *clp = oo->oo_owner.so_client;
7201
Trond Myklebustbeeca192017-11-03 08:00:14 -04007202 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04007203 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05007204 lst = find_lock_stateid(lo, ost);
Jeff Layton356a95e2014-07-29 21:34:13 -04007205 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04007206 if (lst != NULL) {
7207 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
7208 goto out;
7209 nfs4_put_stid(&lst->st_stid);
7210 }
7211 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
7212 if (ns == NULL)
7213 return NULL;
7214
7215 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
7216 if (lst == openlockstateid(ns))
7217 *new = true;
7218 else
Jeff Layton356a95e2014-07-29 21:34:13 -04007219 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04007220out:
Jeff Layton356a95e2014-07-29 21:34:13 -04007221 return lst;
7222}
Jeff Laytonc53530d2014-06-30 11:48:39 -04007223
NeilBrownfd39ca92005-06-23 22:04:03 -07007224static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225check_lock_length(u64 offset, u64 length)
7226{
Kinglong Meee7969312015-07-13 17:34:19 +08007227 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
7228 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229}
7230
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04007231static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05007232{
Trond Myklebust11b91642014-07-29 21:34:08 -04007233 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05007234
Jeff Layton7214e862014-07-10 14:07:33 -04007235 lockdep_assert_held(&fp->fi_lock);
7236
Jeff Layton82c5ff12012-05-11 09:45:13 -04007237 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05007238 return;
Jeff Layton12659652014-07-10 14:07:28 -04007239 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04007240 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05007241}
7242
Jeff Layton356a95e2014-07-29 21:34:13 -04007243static __be32
7244lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
7245 struct nfs4_ol_stateid *ost,
7246 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04007247 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04007248{
Jeff Layton5db1c032014-07-29 21:34:28 -04007249 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04007250 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04007251 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7252 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00007253 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04007254 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04007255 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04007256 unsigned int strhashval;
7257
Kinglong Meec8623992015-07-13 17:35:37 +08007258 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04007259 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08007260 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04007261 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
7262 if (lo == NULL)
7263 return nfserr_jukebox;
7264 } else {
7265 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04007266 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04007267 if (!cstate->minorversion &&
7268 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04007269 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04007270 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04007271
Jeff Laytondd257932016-08-11 10:37:39 -04007272 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
7273 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04007274 status = nfserr_jukebox;
7275 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04007276 }
Jeff Laytondd257932016-08-11 10:37:39 -04007277
Jeff Layton5db1c032014-07-29 21:34:28 -04007278 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04007279 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04007280out:
7281 nfs4_put_stateowner(&lo->lo_owner);
7282 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04007283}
7284
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285/*
7286 * LOCK operation
7287 */
Al Virob37ad282006-10-19 23:28:59 -07007288__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08007289nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007290 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007292 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007293 struct nfs4_openowner *open_sop = NULL;
7294 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04007295 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04007296 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04007297 struct nfs4_file *fp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007298 struct nfsd_file *nf = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04007299 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04007300 struct file_lock *file_lock = NULL;
7301 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07007302 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04007303 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07007304 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04007305 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04007306 unsigned char fl_type;
7307 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007308 struct net *net = SVC_NET(rqstp);
7309 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310
7311 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
7312 (long long) lock->lk_offset,
7313 (long long) lock->lk_length);
7314
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315 if (check_lock_length(lock->lk_offset, lock->lk_length))
7316 return nfserr_inval;
7317
J.Bruce Fieldsca364312006-12-13 00:35:27 -08007318 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02007319 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08007320 dprintk("NFSD: nfsd4_lock: permission denied!\n");
7321 return status;
7322 }
7323
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04007325 if (nfsd4_has_session(cstate))
7326 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08007327 memcpy(&lock->lk_new_clientid,
J. Bruce Fieldsec596592021-01-21 17:57:45 -05007328 &cstate->clp->cl_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04007329 sizeof(clientid_t));
7330
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d93e2011-09-06 15:19:46 -04007332 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 lock->lk_new_open_seqid,
7334 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007335 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07007336 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04007338 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007339 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04007340 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04007341 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08007342 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04007343 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04007344 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04007345 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04007346 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03007347 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007348 lock->lk_old_lock_seqid,
7349 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007350 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04007351 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04007352 if (status)
7353 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04007354 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04007356 lkflg = setlkflg(lock->lk_type);
7357 status = nfs4_check_openmode(lock_stp, lkflg);
7358 if (status)
7359 goto out;
7360
NeilBrown0dd395d2005-07-07 17:59:15 -07007361 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007362 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07007363 goto out;
7364 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007365 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07007366 goto out;
7367
J. Bruce Fieldsbb0a55b2021-08-20 17:02:06 -04007368 if (lock->lk_reclaim)
7369 fl_flags |= FL_RECLAIM;
7370
Trond Myklebust11b91642014-07-29 21:34:08 -04007371 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 case NFS4_READW_LT:
J. Bruce Fields40595cd2021-12-16 12:20:13 -05007374 if (nfsd4_has_session(cstate))
Jeff Layton76d348f2016-09-16 16:28:24 -04007375 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05007376 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04007377 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04007378 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007379 nf = find_readable_file_locked(fp);
7380 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05007381 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04007382 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04007383 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05007384 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 case NFS4_WRITEW_LT:
J. Bruce Fields40595cd2021-12-16 12:20:13 -05007386 if (nfsd4_has_session(cstate))
Jeff Layton76d348f2016-09-16 16:28:24 -04007387 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05007388 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04007389 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04007390 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007391 nf = find_writeable_file_locked(fp);
7392 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05007393 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04007394 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04007395 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05007396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 default:
7398 status = nfserr_inval;
7399 goto out;
7400 }
Jeff Layton76d348f2016-09-16 16:28:24 -04007401
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007402 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04007403 status = nfserr_openmode;
7404 goto out;
7405 }
Kinglong Meeaef95832014-08-22 10:18:44 -04007406
J. Bruce Fields40595cd2021-12-16 12:20:13 -05007407 /*
7408 * Most filesystems with their own ->lock operations will block
7409 * the nfsd thread waiting to acquire the lock. That leads to
7410 * deadlocks (we don't want every nfsd thread tied up waiting
7411 * for file locks), so don't attempt blocking lock notifications
7412 * on those filesystems:
7413 */
7414 if (nf->nf_file->f_op->lock)
7415 fl_flags &= ~FL_SLEEP;
7416
Jeff Layton76d348f2016-09-16 16:28:24 -04007417 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
7418 if (!nbl) {
7419 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
7420 status = nfserr_jukebox;
7421 goto out;
7422 }
7423
7424 file_lock = &nbl->nbl_lock;
7425 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04007426 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04007427 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007428 file_lock->fl_file = nf->nf_file;
Jeff Layton76d348f2016-09-16 16:28:24 -04007429 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04007430 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7431 file_lock->fl_start = lock->lk_offset;
7432 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
7433 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434
Jeff Layton21179d82012-08-21 08:03:32 -04007435 conflock = locks_alloc_lock();
7436 if (!conflock) {
7437 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7438 status = nfserr_jukebox;
7439 goto out;
7440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441
Jeff Layton76d348f2016-09-16 16:28:24 -04007442 if (fl_flags & FL_SLEEP) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01007443 nbl->nbl_time = ktime_get_boottime_seconds();
Jeff Layton0cc11a62016-10-20 09:34:31 -04007444 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04007445 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04007446 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Vasily Averin47446d742021-12-17 09:49:39 +03007447 kref_get(&nbl->nbl_kref);
Jeff Layton0cc11a62016-10-20 09:34:31 -04007448 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04007449 }
7450
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007451 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04007452 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04007454 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07007455 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04007456 if (lock->lk_reclaim)
7457 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08007458 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04007459 case FILE_LOCK_DEFERRED:
Vasily Averin47446d742021-12-17 09:49:39 +03007460 kref_put(&nbl->nbl_kref, free_nbl);
Jeff Layton76d348f2016-09-16 16:28:24 -04007461 nbl = NULL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05007462 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04007463 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08007464 status = nfserr_denied;
7465 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04007466 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08007467 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04007468 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08007470 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04007471 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05007472 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04007473 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08007474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476out:
Jeff Layton76d348f2016-09-16 16:28:24 -04007477 if (nbl) {
7478 /* dequeue it if we queued it before */
7479 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04007480 spin_lock(&nn->blocked_locks_lock);
Vasily Averin47446d742021-12-17 09:49:39 +03007481 if (!list_empty(&nbl->nbl_list) &&
7482 !list_empty(&nbl->nbl_lru)) {
7483 list_del_init(&nbl->nbl_list);
7484 list_del_init(&nbl->nbl_lru);
7485 kref_put(&nbl->nbl_kref, free_nbl);
7486 }
7487 /* nbl can use one of lists to be linked to reaplist */
Jeff Layton0cc11a62016-10-20 09:34:31 -04007488 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04007489 }
7490 free_blocked_lock(nbl);
7491 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007492 if (nf)
7493 nfsd_file_put(nf);
Jeff Layton5db1c032014-07-29 21:34:28 -04007494 if (lock_stp) {
7495 /* Bump seqid manually if the 4.0 replay owner is openowner */
7496 if (cstate->replay_owner &&
7497 cstate->replay_owner != &lock_sop->lo_owner &&
7498 seqid_mutating_err(ntohl(status)))
7499 lock_sop->lo_owner.so_seqid++;
7500
7501 /*
7502 * If this is a new, never-before-used stateid, and we are
7503 * returning an error, then just go ahead and release it.
7504 */
J. Bruce Fields25020722018-01-17 16:25:59 -05007505 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04007506 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04007507
7508 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04007509
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04007510 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04007511 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04007512 if (open_stp)
7513 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04007514 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04007515 if (conflock)
7516 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517 return status;
7518}
7519
7520/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08007521 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
7522 * so we do a temporary open here just to get an open file to pass to
J. Bruce Fields0bcc7ca2021-08-24 22:36:03 -04007523 * vfs_test_lock.
J. Bruce Fields55ef1272008-12-20 11:58:38 -08007524 */
Al Viro04da6e92012-04-13 00:00:04 -04007525static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08007526{
Jeff Layton6b556ca2019-08-18 14:18:55 -04007527 struct nfsd_file *nf;
NeilBrownbb4d53d2022-07-26 16:45:30 +10007528 struct inode *inode;
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04007529 __be32 err;
7530
7531 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
7532 if (err)
7533 return err;
NeilBrownbb4d53d2022-07-26 16:45:30 +10007534 inode = fhp->fh_dentry->d_inode;
7535 inode_lock(inode); /* to block new leases till after test_lock: */
7536 err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04007537 if (err)
7538 goto out;
J. Bruce Fields0bcc7ca2021-08-24 22:36:03 -04007539 lock->fl_file = nf->nf_file;
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04007540 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
J. Bruce Fields0bcc7ca2021-08-24 22:36:03 -04007541 lock->fl_file = NULL;
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04007542out:
NeilBrownbb4d53d2022-07-26 16:45:30 +10007543 inode_unlock(inode);
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04007544 nfsd_file_put(nf);
J. Bruce Fields55ef1272008-12-20 11:58:38 -08007545 return err;
7546}
7547
7548/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549 * LOCKT operation
7550 */
Al Virob37ad282006-10-19 23:28:59 -07007551__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08007552nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007553 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007555 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04007556 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04007557 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07007558 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03007559 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04007561 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 return nfserr_grace;
7563
7564 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
7565 return nfserr_inval;
7566
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007567 if (!nfsd4_has_session(cstate)) {
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05007568 status = set_client(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007569 if (status)
7570 goto out;
7571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572
J. Bruce Fields75c096f2011-08-15 18:39:32 -04007573 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575
Jeff Layton21179d82012-08-21 08:03:32 -04007576 file_lock = locks_alloc_lock();
7577 if (!file_lock) {
7578 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7579 status = nfserr_jukebox;
7580 goto out;
7581 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08007582
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 switch (lockt->lt_type) {
7584 case NFS4_READ_LT:
7585 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04007586 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00007587 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588 case NFS4_WRITE_LT:
7589 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04007590 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00007591 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04007593 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00007595 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 }
7597
Kinglong Meec8623992015-07-13 17:35:37 +08007598 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04007599 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04007600 file_lock->fl_owner = (fl_owner_t)lo;
7601 file_lock->fl_pid = current->tgid;
7602 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603
Jeff Layton21179d82012-08-21 08:03:32 -04007604 file_lock->fl_start = lockt->lt_offset;
7605 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007606
Jeff Layton21179d82012-08-21 08:03:32 -04007607 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608
Jeff Layton21179d82012-08-21 08:03:32 -04007609 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04007610 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05007611 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04007612
Jeff Layton21179d82012-08-21 08:03:32 -04007613 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04007615 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616 }
7617out:
Jeff Layton5db1c032014-07-29 21:34:28 -04007618 if (lo)
7619 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04007620 if (file_lock)
7621 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 return status;
7623}
7624
Al Virob37ad282006-10-19 23:28:59 -07007625__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08007626nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007627 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007629 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04007630 struct nfs4_ol_stateid *stp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007631 struct nfsd_file *nf = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04007632 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07007633 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07007634 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007635 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7636
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7638 (long long) locku->lu_offset,
7639 (long long) locku->lu_length);
7640
7641 if (check_lock_length(locku->lu_offset, locku->lu_length))
7642 return nfserr_inval;
7643
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04007644 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007645 &locku->lu_stateid, NFS4_LOCK_STID,
7646 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04007647 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648 goto out;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007649 nf = find_any_file(stp->st_stid.sc_file);
7650 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04007651 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04007652 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04007653 }
Jeff Layton21179d82012-08-21 08:03:32 -04007654 file_lock = locks_alloc_lock();
7655 if (!file_lock) {
7656 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7657 status = nfserr_jukebox;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007658 goto put_file;
Jeff Layton21179d82012-08-21 08:03:32 -04007659 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08007660
Jeff Layton21179d82012-08-21 08:03:32 -04007661 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04007662 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04007663 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007664 file_lock->fl_file = nf->nf_file;
Jeff Layton21179d82012-08-21 08:03:32 -04007665 file_lock->fl_flags = FL_POSIX;
7666 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7667 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668
Jeff Layton21179d82012-08-21 08:03:32 -04007669 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7670 locku->lu_length);
7671 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007673 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07007674 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05007675 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 goto out_nfserr;
7677 }
Jeff Layton9767feb2015-10-01 09:05:50 -04007678 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007679put_file:
7680 nfsd_file_put(nf);
Trond Myklebust858cc572014-07-29 21:34:16 -04007681put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04007682 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04007683 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04007685 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04007686 if (file_lock)
7687 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688 return status;
7689
7690out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07007691 status = nfserrno(err);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007692 goto put_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693}
7694
7695/*
7696 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007697 * true: locks held by lockowner
7698 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007700static bool
7701check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007702{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007703 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007704 int status = false;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007705 struct nfsd_file *nf = find_any_file(fp);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007706 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007707 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007708
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007709 if (!nf) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007710 /* Any valid lock stateid should have some sort of access */
7711 WARN_ON_ONCE(1);
7712 return status;
7713 }
7714
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007715 inode = locks_inode(nf->nf_file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007716 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007718 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05007719 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007720 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7721 if (fl->fl_owner == (fl_owner_t)lowner) {
7722 status = true;
7723 break;
7724 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08007725 }
Jeff Layton6109c852015-01-16 15:05:57 -05007726 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007728 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729 return status;
7730}
7731
Chuck Lever043862b2022-05-22 12:34:38 -04007732/**
7733 * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
7734 * @rqstp: RPC transaction
7735 * @cstate: NFSv4 COMPOUND state
7736 * @u: RELEASE_LOCKOWNER arguments
7737 *
7738 * The lockowner's so_count is bumped when a lock record is added
7739 * or when copying a conflicting lock. The latter case is brief,
7740 * but can lead to fleeting false positives when looking for
7741 * locks-in-use.
7742 *
7743 * Return values:
7744 * %nfs_ok: lockowner released or not found
7745 * %nfserr_locks_held: lockowner still in use
7746 * %nfserr_stale_clientid: clientid no longer active
7747 * %nfserr_expired: clientid not recognized
7748 */
Al Virob37ad282006-10-19 23:28:59 -07007749__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08007750nfsd4_release_lockowner(struct svc_rqst *rqstp,
7751 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007752 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007753{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007754 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03007755 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Chuck Leverbd8fdb62022-05-22 12:07:18 -04007756 clientid_t *clid = &rlockowner->rl_clientid;
7757 struct nfs4_ol_stateid *stp;
7758 struct nfs4_lockowner *lo;
Trond Myklebustc58c6612014-07-29 21:34:35 -04007759 struct nfs4_client *clp;
Chuck Leverbd8fdb62022-05-22 12:07:18 -04007760 LIST_HEAD(reaplist);
7761 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762
7763 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7764 clid->cl_boot, clid->cl_id);
7765
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05007766 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007767 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04007768 return status;
Trond Myklebustd4f04892014-07-29 21:34:36 -04007769 clp = cstate->clp;
Chuck Leverbd8fdb62022-05-22 12:07:18 -04007770
Trond Myklebustd4f04892014-07-29 21:34:36 -04007771 spin_lock(&clp->cl_lock);
Chuck Leverbd8fdb62022-05-22 12:07:18 -04007772 lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
Chuck Lever88584812016-07-13 16:40:14 -04007773 if (!lo) {
7774 spin_unlock(&clp->cl_lock);
Chuck Lever043862b2022-05-22 12:34:38 -04007775 return nfs_ok;
Chuck Lever88584812016-07-13 16:40:14 -04007776 }
Chuck Leverbd8fdb62022-05-22 12:07:18 -04007777 if (atomic_read(&lo->lo_owner.so_count) != 2) {
7778 spin_unlock(&clp->cl_lock);
7779 nfs4_put_stateowner(&lo->lo_owner);
7780 return nfserr_locks_held;
7781 }
Chuck Lever88584812016-07-13 16:40:14 -04007782 unhash_lockowner_locked(lo);
7783 while (!list_empty(&lo->lo_owner.so_stateids)) {
7784 stp = list_first_entry(&lo->lo_owner.so_stateids,
7785 struct nfs4_ol_stateid,
7786 st_perstateowner);
7787 WARN_ON(!unhash_lock_stateid(stp));
7788 put_ol_stateid_locked(stp, &reaplist);
7789 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04007790 spin_unlock(&clp->cl_lock);
Chuck Lever043862b2022-05-22 12:34:38 -04007791
Chuck Lever88584812016-07-13 16:40:14 -04007792 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007793 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04007794 nfs4_put_stateowner(&lo->lo_owner);
Chuck Lever043862b2022-05-22 12:34:38 -04007795 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796}
7797
7798static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07007799alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800{
NeilBrowna55370a2005-06-23 22:03:52 -07007801 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007802}
7803
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007804bool
Scott Mayhew6b189102019-03-26 18:06:26 -04007805nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07007806{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007807 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07007808
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007809 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007810 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07007811}
7812
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813/*
7814 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04007815 *
7816 * The caller is responsible for freeing name.data if NULL is returned (it
7817 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05007819struct nfs4_client_reclaim *
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007820nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7821 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822{
7823 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007824 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825
NeilBrowna55370a2005-06-23 22:03:52 -07007826 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05007827 if (crp) {
7828 strhashval = clientstr_hashval(name);
7829 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007830 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04007831 crp->cr_name.data = name.data;
7832 crp->cr_name.len = name.len;
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007833 crp->cr_princhash.data = princhash.data;
7834 crp->cr_princhash.len = princhash.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007835 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007836 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007837 }
7838 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839}
7840
Jeff Layton2a4317c2012-03-21 16:42:43 -04007841void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007842nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05007843{
7844 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04007845 kfree(crp->cr_name.data);
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007846 kfree(crp->cr_princhash.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05007847 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007848 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05007849}
7850
7851void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007852nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853{
7854 struct nfs4_client_reclaim *crp = NULL;
7855 int i;
7856
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007858 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7859 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007861 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862 }
7863 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05007864 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865}
7866
7867/*
7868 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04007869struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04007870nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007871{
7872 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 struct nfs4_client_reclaim *crp = NULL;
7874
Scott Mayhew6b189102019-03-26 18:06:26 -04007875 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007876 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04007877 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 return crp;
7879 }
7880 }
7881 return NULL;
7882}
7883
Al Virob37ad282006-10-19 23:28:59 -07007884__be32
J. Bruce Fields1722b042021-01-21 17:57:44 -05007885nfs4_check_open_reclaim(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886{
J. Bruce Fields1722b042021-01-21 17:57:44 -05007887 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Jeff Layton3b3e7b72014-09-12 16:40:22 -04007888 return nfserr_no_grace;
7889
J. Bruce Fields1722b042021-01-21 17:57:44 -05007890 if (nfsd4_client_record_check(clp))
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007891 return nfserr_reclaim_bad;
7892
7893 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894}
7895
Meelap Shahc2f1a552007-07-17 04:04:39 -07007896/*
7897 * Since the lifetime of a delegation isn't limited to that of an open, a
7898 * client may quite reasonably hang on to a delegation as long as it has
7899 * the inode cached. This becomes an obvious problem the first time a
7900 * client's inode cache approaches the size of the server's total memory.
7901 *
7902 * For now we avoid this problem by imposing a hard limit on the number
7903 * of delegations, which varies according to the server's memory size.
7904 */
7905static void
7906set_max_delegations(void)
7907{
7908 /*
7909 * Allow at most 4 delegations per megabyte of RAM. Quick
7910 * estimates suggest that in the worst case (where every delegation
7911 * is for a different inode), a delegation could take about 1.5K,
7912 * giving a worst case usage of about 6% of memory.
7913 */
7914 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7915}
7916
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007917static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007918{
7919 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7920 int i;
7921
Kees Cook6da2ec52018-06-12 13:55:00 -07007922 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7923 sizeof(struct list_head),
7924 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007925 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007926 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007927 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7928 sizeof(struct list_head),
7929 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007930 if (!nn->unconf_id_hashtbl)
7931 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007932 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7933 sizeof(struct list_head),
7934 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007935 if (!nn->sessionid_hashtbl)
7936 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007937
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007938 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007939 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007940 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007941 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007942 for (i = 0; i < SESSION_HASH_SIZE; i++)
7943 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007944 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007945 nn->unconf_name_tree = RB_ROOT;
Arnd Bergmann9cc76802019-10-31 15:53:13 +01007946 nn->boot_time = ktime_get_real_seconds();
Vasily Averin81833de2017-11-13 07:25:40 +03007947 nn->grace_ended = false;
7948 nn->nfsd4_manager.block_opens = true;
7949 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007950 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007951 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007952 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007953 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007954 spin_lock_init(&nn->s2s_cp_lock);
7955 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007956
Jeff Layton0cc11a62016-10-20 09:34:31 -04007957 spin_lock_init(&nn->blocked_locks_lock);
7958 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7959
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007960 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Dai Ngo7746b322022-09-14 08:54:26 -07007961 INIT_DELAYED_WORK(&nn->nfsd_shrinker_work, courtesy_client_reaper);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007962 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007963
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007964 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007965
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007966err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007967 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007968err_unconf_id:
7969 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007970err:
7971 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007972}
7973
7974static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007975nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007976{
7977 int i;
7978 struct nfs4_client *clp = NULL;
7979 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7980
7981 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7982 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7983 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7984 destroy_client(clp);
7985 }
7986 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007987
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007988 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7989
Kinglong Mee2b905632014-03-26 22:09:30 +08007990 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7991 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7992 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7993 destroy_client(clp);
7994 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007995 }
7996
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007997 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007998 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007999 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03008000 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03008001}
8002
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03008003int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03008004nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07008005{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04008006 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05008007 int ret;
8008
Trond Myklebustc6c7f2a2021-03-13 16:08:47 -05008009 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03008010 if (ret)
8011 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04008012 locks_start_grace(net, &nn->nfsd4_manager);
8013 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04008014 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
8015 goto skip_grace;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01008016 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
Vasily Averin7e981a82017-11-06 16:46:04 +03008017 nn->nfsd4_grace, net->ns.inum);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04008018 trace_nfsd_grace_start(nn);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03008019 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03008020 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04008021
8022skip_grace:
8023 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
8024 net->ns.inum);
8025 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
8026 nfsd4_end_grace(nn);
8027 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03008028}
8029
8030/* initialization to perform when the nfsd service is started: */
8031
8032int
8033nfs4_state_start(void)
8034{
8035 int ret;
8036
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05008037 ret = nfsd4_create_callback_queue();
8038 if (ret)
Dai Ngod76cc462022-05-02 14:19:23 -07008039 return ret;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03008040
Meelap Shahc2f1a552007-07-17 04:04:39 -07008041 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05008042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043}
8044
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03008045void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03008046nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008048 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03008050 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03008052 cancel_delayed_work_sync(&nn->laundromat_work);
8053 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05008054
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04008056 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04008057 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008058 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04008059 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04008060 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008061 }
Benny Halevycdc975052014-05-30 09:09:30 -04008062 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063 list_for_each_safe(pos, next, &reaplist) {
8064 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04008065 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05008066 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067 }
8068
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03008069 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03008070 nfs4_state_destroy_net(net);
Dai Ngof4e44b32021-05-21 15:09:37 -04008071#ifdef CONFIG_NFSD_V4_2_INTER_SSC
8072 nfsd4_ssc_shutdown_umount(nn);
8073#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008074}
8075
8076void
8077nfs4_state_shutdown(void)
8078{
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04008079 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008080}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008081
8082static void
8083get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8084{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04008085 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
8086 CURRENT_STATEID(stateid))
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01008087 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008088}
8089
8090static void
8091put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
8092{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01008093 if (cstate->minorversion) {
8094 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -04008095 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01008096 }
8097}
8098
8099void
8100clear_current_stateid(struct nfsd4_compound_state *cstate)
8101{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04008102 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008103}
8104
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01008105/*
8106 * functions to set current state id
8107 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008108void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02008109nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
8110 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01008111{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02008112 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01008113}
8114
8115void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02008116nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
8117 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008118{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02008119 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008120}
8121
8122void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02008123nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
8124 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01008125{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02008126 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01008127}
8128
8129void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02008130nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
8131 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01008132{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02008133 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01008134}
8135
8136/*
8137 * functions to consume current state id
8138 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008139
8140void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008141nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
8142 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01008143{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008144 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01008145}
8146
8147void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008148nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
8149 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01008150{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008151 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01008152}
8153
8154void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008155nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
8156 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008157{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008158 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008159}
8160
8161void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008162nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
8163 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008164{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008165 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008166}
8167
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01008168void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008169nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
8170 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008171{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008172 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008173}
8174
8175void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008176nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
8177 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008178{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008179 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008180}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008181
8182void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008183nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
8184 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008185{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008186 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008187}
8188
8189void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008190nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
8191 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008192{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008193 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008194}