blob: b12f530f6449a43afce4d7f2614890cc7d93c011 [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>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020047#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040048#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050049#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040050#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040052#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020053#include "pnfs.h"
Jeff Laytonfd4f83f2019-08-18 14:18:52 -040054#include "filecache.h"
Chuck Leverdd5e3fb2020-04-05 11:42:29 -040055#include "trace.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define NFSDDBG_FACILITY NFSDDBG_PROC
58
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050059#define all_ones {{~0,~0},~0}
60static const stateid_t one_stateid = {
61 .si_generation = ~0,
62 .si_opaque = all_ones,
63};
64static const stateid_t zero_stateid = {
65 /* all fields zero */
66};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010067static const stateid_t currentstateid = {
68 .si_generation = 1,
69};
Trond Myklebustfb500a72017-11-03 08:00:12 -040070static const stateid_t close_stateid = {
71 .si_generation = 0xffffffffU,
72};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050073
Andy Adamsonec6b5d72009-04-03 08:28:28 +030074static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070075
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050076#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
77#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010078#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elbleae254dac2017-11-09 13:41:10 -050079#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040082static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040083static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Scott Mayhew362063a2019-03-26 18:06:28 -040084void nfsd4_end_grace(struct nfsd_net *nn);
Olga Kornievskaia624322f2019-10-04 16:34:26 -040085static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
J. Bruce Fields8b671b82009-02-22 14:51:34 -080087/* Locking: */
88
J. Bruce Fields8b671b82009-02-22 14:51:34 -080089/*
90 * Currently used for the del_recall_lru and file hash table. In an
91 * effort to decrease the scope of the client_mutex, this spinlock may
92 * eventually cover more:
93 */
Benny Halevycdc975052014-05-30 09:09:30 -040094static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080095
Andrew Elble4f34bd02017-11-08 17:29:51 -050096enum nfsd4_st_mutex_lock_subclass {
97 OPEN_STATEID_MUTEX = 0,
98 LOCK_STATEID_MUTEX = 1,
99};
100
Jeff Laytonb401be222014-07-29 21:34:33 -0400101/*
102 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
103 * the refcount on the open stateid to drop.
104 */
105static DECLARE_WAIT_QUEUE_HEAD(close_wq);
106
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400107/*
108 * A waitqueue where a writer to clients/#/ctl destroying a client can
109 * wait for cl_rpc_users to drop to 0 and then for the client to be
110 * unhashed.
111 */
112static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
113
Jeff Layton9258a2d2018-03-16 09:47:22 -0400114static struct kmem_cache *client_slab;
Christoph Hellwigabf11352014-05-21 07:43:03 -0700115static struct kmem_cache *openowner_slab;
116static struct kmem_cache *lockowner_slab;
117static struct kmem_cache *file_slab;
118static struct kmem_cache *stateid_slab;
119static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200120static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700121
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400122static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800123
Julia Lawallc4cb8972015-11-21 22:57:39 +0100124static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400125static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200126
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400127static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800128{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400129 return ses->se_flags & NFS4_SESSION_DEAD;
130}
131
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400132static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
133{
134 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400135 return nfserr_jukebox;
136 ses->se_flags |= NFS4_SESSION_DEAD;
137 return nfs_ok;
138}
139
J. Bruce Fields221a6872013-04-01 22:23:49 -0400140static bool is_client_expired(struct nfs4_client *clp)
141{
142 return clp->cl_time == 0;
143}
144
J. Bruce Fields221a6872013-04-01 22:23:49 -0400145static __be32 get_client_locked(struct nfs4_client *clp)
146{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400147 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
148
149 lockdep_assert_held(&nn->client_lock);
150
J. Bruce Fields221a6872013-04-01 22:23:49 -0400151 if (is_client_expired(clp))
152 return nfserr_expired;
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400153 atomic_inc(&clp->cl_rpc_users);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400154 return nfs_ok;
155}
156
157/* must be called under the client_lock */
158static inline void
159renew_client_locked(struct nfs4_client *clp)
160{
161 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
162
163 if (is_client_expired(clp)) {
164 WARN_ON(1);
165 printk("%s: client (clientid %08x/%08x) already expired\n",
166 __func__,
167 clp->cl_clientid.cl_boot,
168 clp->cl_clientid.cl_id);
169 return;
170 }
171
J. Bruce Fields221a6872013-04-01 22:23:49 -0400172 list_move_tail(&clp->cl_lru, &nn->client_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100173 clp->cl_time = ktime_get_boottime_seconds();
J. Bruce Fields221a6872013-04-01 22:23:49 -0400174}
175
Fengguang Wuba138432013-04-16 22:14:15 -0400176static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400177{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400178 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
179
180 lockdep_assert_held(&nn->client_lock);
181
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400182 if (!atomic_dec_and_test(&clp->cl_rpc_users))
J. Bruce Fields221a6872013-04-01 22:23:49 -0400183 return;
184 if (!is_client_expired(clp))
185 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400186 else
187 wake_up_all(&expiry_wq);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400188}
189
Jeff Layton4b24ca72014-06-30 11:48:44 -0400190static void put_client_renew(struct nfs4_client *clp)
191{
192 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
193
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400194 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
Jeff Laytond6c249b2014-07-08 14:02:50 -0400195 return;
196 if (!is_client_expired(clp))
197 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400198 else
199 wake_up_all(&expiry_wq);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400200 spin_unlock(&nn->client_lock);
201}
202
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400203static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
204{
205 __be32 status;
206
207 if (is_session_dead(ses))
208 return nfserr_badsession;
209 status = get_client_locked(ses->se_client);
210 if (status)
211 return status;
212 atomic_inc(&ses->se_ref);
213 return nfs_ok;
214}
215
216static void nfsd4_put_session_locked(struct nfsd4_session *ses)
217{
218 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400219 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
220
221 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400222
223 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
224 free_session(ses);
225 put_client_renew_locked(clp);
226}
227
228static void nfsd4_put_session(struct nfsd4_session *ses)
229{
230 struct nfs4_client *clp = ses->se_client;
231 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
232
233 spin_lock(&nn->client_lock);
234 nfsd4_put_session_locked(ses);
235 spin_unlock(&nn->client_lock);
236}
237
Jeff Layton76d348f2016-09-16 16:28:24 -0400238static struct nfsd4_blocked_lock *
239find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
240 struct nfsd_net *nn)
241{
242 struct nfsd4_blocked_lock *cur, *found = NULL;
243
Jeff Layton0cc11a62016-10-20 09:34:31 -0400244 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400245 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
246 if (fh_match(fh, &cur->nbl_fh)) {
247 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400248 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400249 found = cur;
250 break;
251 }
252 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400253 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400254 if (found)
NeilBrowncb03f942018-11-30 10:04:08 +1100255 locks_delete_block(&found->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400256 return found;
257}
258
259static struct nfsd4_blocked_lock *
260find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
261 struct nfsd_net *nn)
262{
263 struct nfsd4_blocked_lock *nbl;
264
265 nbl = find_blocked_lock(lo, fh, nn);
266 if (!nbl) {
267 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
268 if (nbl) {
Vasily Averine1e83992020-03-27 07:50:40 +0300269 INIT_LIST_HEAD(&nbl->nbl_list);
270 INIT_LIST_HEAD(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400271 fh_copy_shallow(&nbl->nbl_fh, fh);
272 locks_init_lock(&nbl->nbl_lock);
273 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
274 &nfsd4_cb_notify_lock_ops,
275 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
276 }
277 }
278 return nbl;
279}
280
281static void
282free_blocked_lock(struct nfsd4_blocked_lock *nbl)
283{
Jeff Layton6aaafc42019-04-22 12:34:23 -0400284 locks_delete_block(&nbl->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400285 locks_release_private(&nbl->nbl_lock);
286 kfree(nbl);
287}
288
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400289static void
290remove_blocked_locks(struct nfs4_lockowner *lo)
291{
292 struct nfs4_client *clp = lo->lo_owner.so_client;
293 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
294 struct nfsd4_blocked_lock *nbl;
295 LIST_HEAD(reaplist);
296
297 /* Dequeue all blocked locks */
298 spin_lock(&nn->blocked_locks_lock);
299 while (!list_empty(&lo->lo_blocked)) {
300 nbl = list_first_entry(&lo->lo_blocked,
301 struct nfsd4_blocked_lock,
302 nbl_list);
303 list_del_init(&nbl->nbl_list);
304 list_move(&nbl->nbl_lru, &reaplist);
305 }
306 spin_unlock(&nn->blocked_locks_lock);
307
308 /* Now free them */
309 while (!list_empty(&reaplist)) {
310 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
311 nbl_lru);
312 list_del_init(&nbl->nbl_lru);
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400313 free_blocked_lock(nbl);
314 }
315}
316
Jeff Laytonf4564582019-04-22 12:34:24 -0400317static void
318nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
319{
320 struct nfsd4_blocked_lock *nbl = container_of(cb,
321 struct nfsd4_blocked_lock, nbl_cb);
322 locks_delete_block(&nbl->nbl_lock);
323}
324
Jeff Layton76d348f2016-09-16 16:28:24 -0400325static int
326nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
327{
328 /*
329 * Since this is just an optimization, we don't try very hard if it
330 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
331 * just quit trying on anything else.
332 */
333 switch (task->tk_status) {
334 case -NFS4ERR_DELAY:
335 rpc_delay(task, 1 * HZ);
336 return 0;
337 default:
338 return 1;
339 }
340}
341
342static void
343nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
344{
345 struct nfsd4_blocked_lock *nbl = container_of(cb,
346 struct nfsd4_blocked_lock, nbl_cb);
347
348 free_blocked_lock(nbl);
349}
350
351static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
Jeff Laytonf4564582019-04-22 12:34:24 -0400352 .prepare = nfsd4_cb_notify_lock_prepare,
Jeff Layton76d348f2016-09-16 16:28:24 -0400353 .done = nfsd4_cb_notify_lock_done,
354 .release = nfsd4_cb_notify_lock_release,
355};
356
Kinglong Meeb5971af2014-08-22 10:18:43 -0400357static inline struct nfs4_stateowner *
358nfs4_get_stateowner(struct nfs4_stateowner *sop)
359{
360 atomic_inc(&sop->so_count);
361 return sop;
362}
363
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400364static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400365same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400366{
367 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400368 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400369}
370
371static struct nfs4_openowner *
372find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400373 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400374{
375 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400376
Trond Myklebustd4f04892014-07-29 21:34:36 -0400377 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400378
Trond Myklebustd4f04892014-07-29 21:34:36 -0400379 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
380 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400381 if (!so->so_is_open_owner)
382 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400383 if (same_owner_str(so, &open->op_owner))
384 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400385 }
386 return NULL;
387}
388
389static struct nfs4_openowner *
390find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400391 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400392{
393 struct nfs4_openowner *oo;
394
Trond Myklebustd4f04892014-07-29 21:34:36 -0400395 spin_lock(&clp->cl_lock);
396 oo = find_openstateowner_str_locked(hashval, open, clp);
397 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400398 return oo;
399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401static inline u32
402opaque_hashval(const void *ptr, int nbytes)
403{
404 unsigned char *cptr = (unsigned char *) ptr;
405
406 u32 x = 0;
407 while (nbytes--) {
408 x *= 37;
409 x += *cptr++;
410 }
411 return x;
412}
413
Jeff Layton5b095e92014-10-23 08:01:02 -0400414static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400415{
Jeff Layton5b095e92014-10-23 08:01:02 -0400416 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
417
418 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400419}
420
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200421void
NeilBrown13cd2182005-06-23 22:03:10 -0700422put_nfs4_file(struct nfs4_file *fi)
423{
Jeff Layton02e12152014-07-16 10:31:57 -0400424 might_lock(&state_lock);
425
Elena Reshetova818a34e2017-10-20 12:53:30 +0300426 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400427 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400428 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200429 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400430 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
431 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800432 }
NeilBrown13cd2182005-06-23 22:03:10 -0700433}
434
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400435static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400436__nfs4_get_fd(struct nfs4_file *f, int oflag)
437{
438 if (f->fi_fds[oflag])
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400439 return nfsd_file_get(f->fi_fds[oflag]);
Trond Myklebustde186432014-07-10 14:07:26 -0400440 return NULL;
441}
442
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400443static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400444find_writeable_file_locked(struct nfs4_file *f)
445{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400446 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400447
448 lockdep_assert_held(&f->fi_lock);
449
450 ret = __nfs4_get_fd(f, O_WRONLY);
451 if (!ret)
452 ret = __nfs4_get_fd(f, O_RDWR);
453 return ret;
454}
455
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400456static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400457find_writeable_file(struct nfs4_file *f)
458{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400459 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400460
461 spin_lock(&f->fi_lock);
462 ret = find_writeable_file_locked(f);
463 spin_unlock(&f->fi_lock);
464
465 return ret;
466}
467
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400468static struct nfsd_file *
469find_readable_file_locked(struct nfs4_file *f)
Trond Myklebustde186432014-07-10 14:07:26 -0400470{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400471 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400472
473 lockdep_assert_held(&f->fi_lock);
474
475 ret = __nfs4_get_fd(f, O_RDONLY);
476 if (!ret)
477 ret = __nfs4_get_fd(f, O_RDWR);
478 return ret;
479}
480
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400481static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400482find_readable_file(struct nfs4_file *f)
483{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400484 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400485
486 spin_lock(&f->fi_lock);
487 ret = find_readable_file_locked(f);
488 spin_unlock(&f->fi_lock);
489
490 return ret;
491}
492
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400493struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400494find_any_file(struct nfs4_file *f)
495{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400496 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400497
Trond Myklebusta451b122020-03-01 18:21:38 -0500498 if (!f)
499 return NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400500 spin_lock(&f->fi_lock);
501 ret = __nfs4_get_fd(f, O_RDWR);
502 if (!ret) {
503 ret = __nfs4_get_fd(f, O_WRONLY);
504 if (!ret)
505 ret = __nfs4_get_fd(f, O_RDONLY);
506 }
507 spin_unlock(&f->fi_lock);
508 return ret;
509}
510
J. Bruce Fields9affa432020-07-15 13:31:36 -0400511static struct nfsd_file *find_deleg_file(struct nfs4_file *f)
512{
513 struct nfsd_file *ret = NULL;
514
515 spin_lock(&f->fi_lock);
516 if (f->fi_deleg_file)
517 ret = nfsd_file_get(f->fi_deleg_file);
518 spin_unlock(&f->fi_lock);
519 return ret;
520}
521
Trond Myklebust02a35082014-07-25 07:34:22 -0400522static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800523unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700524
525/*
526 * Open owner state (share locks)
527 */
528
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500529/* hash tables for lock and open owners */
530#define OWNER_HASH_BITS 8
531#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
532#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700533
Trond Myklebustd4f04892014-07-29 21:34:36 -0400534static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400535{
536 unsigned int ret;
537
538 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500539 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400540}
NeilBrownef0f3392006-04-10 22:55:41 -0700541
NeilBrownef0f3392006-04-10 22:55:41 -0700542/* hash table for nfs4_file */
543#define FILE_HASH_BITS 8
544#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800545
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400546static unsigned int file_hashval(struct svc_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400547{
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400548 struct inode *inode = d_inode(fh->fh_dentry);
Trond Myklebustca943212014-07-23 16:17:39 -0400549
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -0400550 /* XXX: why not (here & in file cache) use inode? */
551 return (unsigned int)hash_long(inode->i_ino, FILE_HASH_BITS);
Trond Myklebustca943212014-07-23 16:17:39 -0400552}
553
Jeff Layton89876f82013-04-02 09:01:59 -0400554static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700555
Jeff Layton12659652014-07-10 14:07:28 -0400556static void
557__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400558{
Jeff Layton7214e862014-07-10 14:07:33 -0400559 lockdep_assert_held(&fp->fi_lock);
560
Jeff Layton12659652014-07-10 14:07:28 -0400561 if (access & NFS4_SHARE_ACCESS_WRITE)
562 atomic_inc(&fp->fi_access[O_WRONLY]);
563 if (access & NFS4_SHARE_ACCESS_READ)
564 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400565}
566
Jeff Layton12659652014-07-10 14:07:28 -0400567static __be32
568nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400569{
Jeff Layton7214e862014-07-10 14:07:33 -0400570 lockdep_assert_held(&fp->fi_lock);
571
Jeff Layton12659652014-07-10 14:07:28 -0400572 /* Does this access mode make sense? */
573 if (access & ~NFS4_SHARE_ACCESS_BOTH)
574 return nfserr_inval;
575
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400576 /* Does it conflict with a deny mode already set? */
577 if ((access & fp->fi_share_deny) != 0)
578 return nfserr_share_denied;
579
Jeff Layton12659652014-07-10 14:07:28 -0400580 __nfs4_file_get_access(fp, access);
581 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400582}
583
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400584static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
585{
586 /* Common case is that there is no deny mode. */
587 if (deny) {
588 /* Does this deny mode make sense? */
589 if (deny & ~NFS4_SHARE_DENY_BOTH)
590 return nfserr_inval;
591
592 if ((deny & NFS4_SHARE_DENY_READ) &&
593 atomic_read(&fp->fi_access[O_RDONLY]))
594 return nfserr_share_denied;
595
596 if ((deny & NFS4_SHARE_DENY_WRITE) &&
597 atomic_read(&fp->fi_access[O_WRONLY]))
598 return nfserr_share_denied;
599 }
600 return nfs_ok;
601}
602
J. Bruce Fields998db522010-08-07 09:21:41 -0400603static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400604{
Trond Myklebustde186432014-07-10 14:07:26 -0400605 might_lock(&fp->fi_lock);
606
607 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400608 struct nfsd_file *f1 = NULL;
609 struct nfsd_file *f2 = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400610
Jeff Layton6d338b52014-07-10 14:07:29 -0400611 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400612 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400613 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400614 spin_unlock(&fp->fi_lock);
615 if (f1)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400616 nfsd_file_put(f1);
Trond Myklebustde186432014-07-10 14:07:26 -0400617 if (f2)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400618 nfsd_file_put(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400619 }
620}
621
Jeff Layton12659652014-07-10 14:07:28 -0400622static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400623{
Jeff Layton12659652014-07-10 14:07:28 -0400624 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
625
626 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400627 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400628 if (access & NFS4_SHARE_ACCESS_READ)
629 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400630}
631
Sachin Bhamare8287f002015-04-27 14:50:14 +0200632/*
633 * Allocate a new open/delegation state counter. This is needed for
634 * pNFS for proper return on close semantics.
635 *
636 * Note that we only allocate it for pNFS-enabled exports, otherwise
637 * all pointers to struct nfs4_clnt_odstate are always NULL.
638 */
639static struct nfs4_clnt_odstate *
640alloc_clnt_odstate(struct nfs4_client *clp)
641{
642 struct nfs4_clnt_odstate *co;
643
644 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
645 if (co) {
646 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300647 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200648 }
649 return co;
650}
651
652static void
653hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
654{
655 struct nfs4_file *fp = co->co_file;
656
657 lockdep_assert_held(&fp->fi_lock);
658 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
659}
660
661static inline void
662get_clnt_odstate(struct nfs4_clnt_odstate *co)
663{
664 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300665 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200666}
667
668static void
669put_clnt_odstate(struct nfs4_clnt_odstate *co)
670{
671 struct nfs4_file *fp;
672
673 if (!co)
674 return;
675
676 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300677 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200678 list_del(&co->co_perfile);
679 spin_unlock(&fp->fi_lock);
680
681 nfsd4_return_all_file_layouts(co->co_client, fp);
682 kmem_cache_free(odstate_slab, co);
683 }
684}
685
686static struct nfs4_clnt_odstate *
687find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
688{
689 struct nfs4_clnt_odstate *co;
690 struct nfs4_client *cl;
691
692 if (!new)
693 return NULL;
694
695 cl = new->co_client;
696
697 spin_lock(&fp->fi_lock);
698 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
699 if (co->co_client == cl) {
700 get_clnt_odstate(co);
701 goto out;
702 }
703 }
704 co = new;
705 co->co_file = fp;
706 hash_clnt_odstate_locked(new);
707out:
708 spin_unlock(&fp->fi_lock);
709 return co;
710}
711
Kinglong Meed19fb702017-01-18 19:04:42 +0800712struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
713 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400714{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500715 struct nfs4_stid *stid;
716 int new_id;
717
Trond Myklebustf8338832014-07-25 07:34:19 -0400718 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500719 if (!stid)
720 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400721
Jeff Layton4770d722014-07-29 21:34:10 -0400722 idr_preload(GFP_KERNEL);
723 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400724 /* Reserving 0 for start of file in nfsdfs "states" file: */
725 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400726 spin_unlock(&cl->cl_lock);
727 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700728 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500729 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800730
731 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500732 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500733 stid->sc_stateid.si_opaque.so_id = new_id;
734 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
735 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300736 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400737 spin_lock_init(&stid->sc_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400738 INIT_LIST_HEAD(&stid->sc_cp_list);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500739
J. Bruce Fields996e0932011-10-17 11:14:48 -0400740 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500741 * It shouldn't be a problem to reuse an opaque stateid value.
742 * I don't think it is for 4.1. But with 4.0 I worry that, for
743 * example, a stray write retransmission could be accepted by
744 * the server when it should have been rejected. Therefore,
745 * adopt a trick from the sctp code to attempt to maximize the
746 * amount of time until an id is reused, by ensuring they always
747 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400748 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500749 return stid;
750out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800751 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500752 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400753}
754
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400755/*
756 * Create a unique stateid_t to represent each COPY.
757 */
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400758static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
759 unsigned char sc_type)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400760{
761 int new_id;
762
Arnd Bergmann9cc76802019-10-31 15:53:13 +0100763 stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400764 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
765 stid->sc_type = sc_type;
766
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400767 idr_preload(GFP_KERNEL);
768 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400769 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
770 stid->stid.si_opaque.so_id = new_id;
Dai Ngoca9364d2020-11-30 16:24:49 -0500771 stid->stid.si_generation = 1;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400772 spin_unlock(&nn->s2s_cp_lock);
773 idr_preload_end();
774 if (new_id < 0)
775 return 0;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400776 return 1;
777}
778
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400779int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
780{
781 return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
782}
783
784struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
785 struct nfs4_stid *p_stid)
786{
787 struct nfs4_cpntf_state *cps;
788
789 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
790 if (!cps)
791 return NULL;
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100792 cps->cpntf_time = ktime_get_boottime_seconds();
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400793 refcount_set(&cps->cp_stateid.sc_count, 1);
794 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
795 goto out_free;
796 spin_lock(&nn->s2s_cp_lock);
797 list_add(&cps->cp_list, &p_stid->sc_cp_list);
798 spin_unlock(&nn->s2s_cp_lock);
799 return cps;
800out_free:
801 kfree(cps);
802 return NULL;
803}
804
805void nfs4_free_copy_state(struct nfsd4_copy *copy)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400806{
807 struct nfsd_net *nn;
808
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400809 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400810 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
811 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400812 idr_remove(&nn->s2s_cp_stateids,
813 copy->cp_stateid.stid.si_opaque.so_id);
814 spin_unlock(&nn->s2s_cp_lock);
815}
816
817static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
818{
819 struct nfs4_cpntf_state *cps;
820 struct nfsd_net *nn;
821
822 nn = net_generic(net, nfsd_net_id);
823 spin_lock(&nn->s2s_cp_lock);
824 while (!list_empty(&stid->sc_cp_list)) {
825 cps = list_first_entry(&stid->sc_cp_list,
826 struct nfs4_cpntf_state, cp_list);
827 _free_cpntf_state_locked(nn, cps);
828 }
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400829 spin_unlock(&nn->s2s_cp_lock);
830}
831
Jeff Laytonb49e0842014-07-29 21:34:11 -0400832static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400833{
Trond Myklebust60116952014-07-29 21:34:06 -0400834 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400835
Kinglong Meed19fb702017-01-18 19:04:42 +0800836 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400837 if (!stid)
838 return NULL;
839
Kinglong Meed19fb702017-01-18 19:04:42 +0800840 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400841}
842
843static void nfs4_free_deleg(struct nfs4_stid *stid)
844{
Trond Myklebust60116952014-07-29 21:34:06 -0400845 kmem_cache_free(deleg_slab, stid);
846 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400847}
848
NeilBrown6282cd52014-06-04 17:39:26 +1000849/*
850 * When we recall a delegation, we should be careful not to hand it
851 * out again straight away.
852 * To ensure this we keep a pair of bloom filters ('new' and 'old')
853 * in which the filehandles of recalled delegations are "stored".
854 * If a filehandle appear in either filter, a delegation is blocked.
855 * When a delegation is recalled, the filehandle is stored in the "new"
856 * filter.
857 * Every 30 seconds we swap the filters and clear the "new" one,
858 * unless both are empty of course.
859 *
860 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
861 * low 3 bytes as hash-table indices.
862 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400863 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000864 * is used to manage concurrent access. Testing does not need the lock
865 * except when swapping the two filters.
866 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400867static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000868static struct bloom_pair {
869 int entries, old_entries;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200870 time64_t swap_time;
NeilBrown6282cd52014-06-04 17:39:26 +1000871 int new; /* index into 'set' */
872 DECLARE_BITMAP(set[2], 256);
873} blocked_delegations;
874
875static int delegation_blocked(struct knfsd_fh *fh)
876{
877 u32 hash;
878 struct bloom_pair *bd = &blocked_delegations;
879
880 if (bd->entries == 0)
881 return 0;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200882 if (ktime_get_seconds() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400883 spin_lock(&blocked_delegations_lock);
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200884 if (ktime_get_seconds() - bd->swap_time > 30) {
NeilBrown6282cd52014-06-04 17:39:26 +1000885 bd->entries -= bd->old_entries;
886 bd->old_entries = bd->entries;
887 memset(bd->set[bd->new], 0,
888 sizeof(bd->set[0]));
889 bd->new = 1-bd->new;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200890 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +1000891 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400892 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000893 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100894 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000895 if (test_bit(hash&255, bd->set[0]) &&
896 test_bit((hash>>8)&255, bd->set[0]) &&
897 test_bit((hash>>16)&255, bd->set[0]))
898 return 1;
899
900 if (test_bit(hash&255, bd->set[1]) &&
901 test_bit((hash>>8)&255, bd->set[1]) &&
902 test_bit((hash>>16)&255, bd->set[1]))
903 return 1;
904
905 return 0;
906}
907
908static void block_delegations(struct knfsd_fh *fh)
909{
910 u32 hash;
911 struct bloom_pair *bd = &blocked_delegations;
912
Daniel Borkmann87545892014-12-10 16:33:11 +0100913 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000914
Jeff Laytonf54fe962014-07-25 07:34:26 -0400915 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000916 __set_bit(hash&255, bd->set[bd->new]);
917 __set_bit((hash>>8)&255, bd->set[bd->new]);
918 __set_bit((hash>>16)&255, bd->set[bd->new]);
919 if (bd->entries == 0)
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200920 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +1000921 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400922 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500926alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
927 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200928 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400931 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400934 n = atomic_long_inc_return(&num_delegations);
935 if (n < 0 || n > max_delegations)
936 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000937 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400938 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800939 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700940 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400941 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400942
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400943 /*
944 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400945 * meaning of seqid 0 isn't meaningful, really, but let's avoid
946 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400947 */
948 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700949 INIT_LIST_HEAD(&dp->dl_perfile);
950 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200952 dp->dl_clnt_odstate = odstate;
953 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400954 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200955 dp->dl_retries = 1;
956 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200957 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500958 get_nfs4_file(fp);
959 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400961out_dec:
962 atomic_long_dec(&num_delegations);
963 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966void
Trond Myklebust60116952014-07-29 21:34:06 -0400967nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Trond Myklebust11b91642014-07-29 21:34:08 -0400969 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400970 struct nfs4_client *clp = s->sc_client;
971
Jeff Layton4770d722014-07-29 21:34:10 -0400972 might_lock(&clp->cl_lock);
973
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300974 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400975 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400976 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400977 }
Trond Myklebust60116952014-07-29 21:34:06 -0400978 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400979 nfs4_free_cpntf_statelist(clp->net, s);
Jeff Layton4770d722014-07-29 21:34:10 -0400980 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400981 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400982 if (fp)
983 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Jeff Layton9767feb2015-10-01 09:05:50 -0400986void
987nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
988{
989 stateid_t *src = &stid->sc_stateid;
990
991 spin_lock(&stid->sc_lock);
992 if (unlikely(++src->si_generation == 0))
993 src->si_generation = 1;
994 memcpy(dst, src, sizeof(*dst));
995 spin_unlock(&stid->sc_lock);
996}
997
J. Bruce Fields353601e2018-02-16 14:29:42 -0500998static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001000 struct nfsd_file *nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05001001
1002 spin_lock(&fp->fi_lock);
1003 if (--fp->fi_delegees == 0)
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001004 swap(nf, fp->fi_deleg_file);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001005 spin_unlock(&fp->fi_lock);
1006
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001007 if (nf)
1008 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001009}
1010
1011static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1012{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -05001013 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001014 struct nfsd_file *nf = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -04001015
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -05001016 WARN_ON_ONCE(!fp->fi_delegees);
1017
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001018 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001019 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001022static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1023{
1024 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001025 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001026 nfs4_put_stid(&dp->dl_stid);
1027}
1028
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001029void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001030{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001031 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001032}
1033
Andrew Elble34ed9872015-10-15 12:07:28 -04001034/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001035 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -04001036 * @clp: a pointer to the nfs4_client we're granting a delegation to
1037 * @fp: a pointer to the nfs4_file we're granting a delegation on
1038 *
1039 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001040 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -04001041 */
1042
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001043static bool
1044nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -04001045{
Andrew Elble34ed9872015-10-15 12:07:28 -04001046 struct nfs4_delegation *searchdp = NULL;
1047 struct nfs4_client *searchclp = NULL;
1048
Benny Halevycdc975052014-05-30 09:09:30 -04001049 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04001050 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001051
Andrew Elble34ed9872015-10-15 12:07:28 -04001052 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1053 searchclp = searchdp->dl_stid.sc_client;
1054 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001055 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -04001056 }
1057 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001058 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -04001059}
1060
1061/**
1062 * hash_delegation_locked - Add a delegation to the appropriate lists
1063 * @dp: a pointer to the nfs4_delegation we are adding.
1064 * @fp: a pointer to the nfs4_file we're granting a delegation on
1065 *
1066 * Return:
1067 * On success: NULL if the delegation was successfully hashed.
1068 *
1069 * On error: -EAGAIN if one was previously granted to this
1070 * nfs4_client for this nfs4_file. Delegation is not hashed.
1071 *
1072 */
1073
1074static int
1075hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1076{
Andrew Elble34ed9872015-10-15 12:07:28 -04001077 struct nfs4_client *clp = dp->dl_stid.sc_client;
1078
1079 lockdep_assert_held(&state_lock);
1080 lockdep_assert_held(&fp->fi_lock);
1081
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001082 if (nfs4_delegation_exists(clp, fp))
1083 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001084 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001085 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001086 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001087 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1088 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001089}
1090
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001091static bool
Jeff Layton42690672014-07-25 07:34:20 -04001092unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Trond Myklebust11b91642014-07-29 21:34:08 -04001094 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001095
Jeff Layton42690672014-07-25 07:34:20 -04001096 lockdep_assert_held(&state_lock);
1097
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001098 if (list_empty(&dp->dl_perfile))
1099 return false;
1100
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001101 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001102 /* Ensure that deleg break won't try to requeue it */
1103 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001104 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001105 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001107 list_del_init(&dp->dl_perfile);
1108 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001109 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001110}
1111
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001112static void destroy_delegation(struct nfs4_delegation *dp)
1113{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001114 bool unhashed;
1115
Jeff Layton42690672014-07-25 07:34:20 -04001116 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001117 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001118 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001119 if (unhashed)
1120 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001121}
1122
1123static void revoke_delegation(struct nfs4_delegation *dp)
1124{
1125 struct nfs4_client *clp = dp->dl_stid.sc_client;
1126
Jeff Layton2d4a5322014-07-25 07:34:21 -04001127 WARN_ON(!list_empty(&dp->dl_recall_lru));
1128
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001129 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001130 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001131 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001132 spin_lock(&clp->cl_lock);
1133 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1134 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001135 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001136 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139/*
1140 * SETCLIENTID state
1141 */
1142
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001143static unsigned int clientid_hashval(u32 id)
1144{
1145 return id & CLIENT_HASH_MASK;
1146}
1147
Scott Mayhew6b189102019-03-26 18:06:26 -04001148static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001149{
Scott Mayhew6b189102019-03-26 18:06:26 -04001150 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001154 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1155 * st_{access,deny}_bmap field of the stateid, in order to track not
1156 * only what share bits are currently in force, but also what
1157 * combinations of share bits previous opens have used. This allows us
1158 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1159 * return an error if the client attempt to downgrade to a combination
1160 * of share bits not explicable by closing some of its previous opens.
1161 *
1162 * XXX: This enforcement is actually incomplete, since we don't keep
1163 * track of access/deny bit combinations; so, e.g., we allow:
1164 *
1165 * OPEN allow read, deny write
1166 * OPEN allow both, deny none
1167 * DOWNGRADE allow read, deny none
1168 *
1169 * which we should reject.
1170 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001171static unsigned int
1172bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001173 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001174 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001175
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001176 for (i = 1; i < 4; i++) {
1177 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001178 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001179 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001180 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001181}
1182
Jeff Layton82c5ff12012-05-11 09:45:13 -04001183/* set share access for a given stateid */
1184static inline void
1185set_access(u32 access, struct nfs4_ol_stateid *stp)
1186{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001187 unsigned char mask = 1 << access;
1188
1189 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1190 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001191}
1192
1193/* clear share access for a given stateid */
1194static inline void
1195clear_access(u32 access, struct nfs4_ol_stateid *stp)
1196{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001197 unsigned char mask = 1 << access;
1198
1199 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1200 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001201}
1202
1203/* test whether a given stateid has access */
1204static inline bool
1205test_access(u32 access, struct nfs4_ol_stateid *stp)
1206{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001207 unsigned char mask = 1 << access;
1208
1209 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001210}
1211
Jeff Laytonce0fc432012-05-11 09:45:14 -04001212/* set share deny for a given stateid */
1213static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001214set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001215{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001216 unsigned char mask = 1 << deny;
1217
1218 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1219 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001220}
1221
1222/* clear share deny for a given stateid */
1223static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001224clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001225{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001226 unsigned char mask = 1 << deny;
1227
1228 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1229 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001230}
1231
1232/* test whether a given stateid is denying specific access */
1233static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001234test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001235{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001236 unsigned char mask = 1 << deny;
1237
1238 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001239}
1240
1241static int nfs4_access_to_omode(u32 access)
1242{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001243 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001244 case NFS4_SHARE_ACCESS_READ:
1245 return O_RDONLY;
1246 case NFS4_SHARE_ACCESS_WRITE:
1247 return O_WRONLY;
1248 case NFS4_SHARE_ACCESS_BOTH:
1249 return O_RDWR;
1250 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001251 WARN_ON_ONCE(1);
1252 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001253}
1254
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001255/*
1256 * A stateid that had a deny mode associated with it is being released
1257 * or downgraded. Recalculate the deny mode on the file.
1258 */
1259static void
1260recalculate_deny_mode(struct nfs4_file *fp)
1261{
1262 struct nfs4_ol_stateid *stp;
1263
1264 spin_lock(&fp->fi_lock);
1265 fp->fi_share_deny = 0;
1266 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1267 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1268 spin_unlock(&fp->fi_lock);
1269}
1270
1271static void
1272reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1273{
1274 int i;
1275 bool change = false;
1276
1277 for (i = 1; i < 4; i++) {
1278 if ((i & deny) != i) {
1279 change = true;
1280 clear_deny(i, stp);
1281 }
1282 }
1283
1284 /* Recalculate per-file deny mode if there was a change */
1285 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001286 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001287}
1288
Jeff Layton82c5ff12012-05-11 09:45:13 -04001289/* release all access and file references for a given stateid */
1290static void
1291release_all_access(struct nfs4_ol_stateid *stp)
1292{
1293 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001294 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001295
1296 if (fp && stp->st_deny_bmap != 0)
1297 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001298
1299 for (i = 1; i < 4; i++) {
1300 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001301 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001302 clear_access(i, stp);
1303 }
1304}
1305
Kinglong Meed50ffde2015-07-16 12:05:07 +08001306static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1307{
1308 kfree(sop->so_owner.data);
1309 sop->so_ops->so_free(sop);
1310}
1311
Jeff Layton6b180f02014-07-29 21:34:26 -04001312static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1313{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001314 struct nfs4_client *clp = sop->so_client;
1315
1316 might_lock(&clp->cl_lock);
1317
1318 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001319 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001320 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001321 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001322 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001323}
1324
Trond Myklebusta451b122020-03-01 18:21:38 -05001325static bool
1326nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1327{
1328 return list_empty(&stp->st_perfile);
1329}
1330
Jeff Laytone8568732015-08-24 12:41:47 -04001331static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001332{
Trond Myklebust11b91642014-07-29 21:34:08 -04001333 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001334
Jeff Layton1c755dc2014-07-29 21:34:12 -04001335 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1336
Jeff Laytone8568732015-08-24 12:41:47 -04001337 if (list_empty(&stp->st_perfile))
1338 return false;
1339
Trond Myklebust1d31a252014-07-10 14:07:25 -04001340 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001341 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001342 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001343 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001344 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001345}
1346
Trond Myklebust60116952014-07-29 21:34:06 -04001347static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001348{
Trond Myklebust60116952014-07-29 21:34:06 -04001349 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001350
Sachin Bhamare8287f002015-04-27 14:50:14 +02001351 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001352 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001353 if (stp->st_stateowner)
1354 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001355 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001356}
1357
Jeff Laytonb49e0842014-07-29 21:34:11 -04001358static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001359{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001360 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1361 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001362 struct nfsd_file *nf;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001363
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001364 nf = find_any_file(stp->st_stid.sc_file);
1365 if (nf) {
1366 get_file(nf->nf_file);
1367 filp_close(nf->nf_file, (fl_owner_t)lo);
1368 nfsd_file_put(nf);
1369 }
Jeff Laytonb49e0842014-07-29 21:34:11 -04001370 nfs4_free_ol_stateid(stid);
1371}
1372
Jeff Layton2c41beb2014-07-29 21:34:41 -04001373/*
1374 * Put the persistent reference to an already unhashed generic stateid, while
1375 * holding the cl_lock. If it's the last reference, then put it onto the
1376 * reaplist for later destruction.
1377 */
1378static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1379 struct list_head *reaplist)
1380{
1381 struct nfs4_stid *s = &stp->st_stid;
1382 struct nfs4_client *clp = s->sc_client;
1383
1384 lockdep_assert_held(&clp->cl_lock);
1385
1386 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1387
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001388 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001389 wake_up_all(&close_wq);
1390 return;
1391 }
1392
1393 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1394 list_add(&stp->st_locks, reaplist);
1395}
1396
Jeff Laytone8568732015-08-24 12:41:47 -04001397static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001398{
Chuck Leverf46c4452016-10-29 18:19:03 -04001399 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001400
Trond Myklebusta451b122020-03-01 18:21:38 -05001401 if (!unhash_ol_stateid(stp))
1402 return false;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001403 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001404 nfs4_unhash_stid(&stp->st_stid);
Trond Myklebusta451b122020-03-01 18:21:38 -05001405 return true;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001406}
1407
Jeff Layton5adfd882014-07-29 21:34:31 -04001408static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001409{
Chuck Leverf46c4452016-10-29 18:19:03 -04001410 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001411 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001412
Chuck Leverf46c4452016-10-29 18:19:03 -04001413 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001414 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001415 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001416 if (unhashed)
1417 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001418}
1419
Trond Myklebustc58c6612014-07-29 21:34:35 -04001420static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001421{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001422 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001423
Trond Myklebustd4f04892014-07-29 21:34:36 -04001424 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001425
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001426 list_del_init(&lo->lo_owner.so_strhash);
1427}
1428
Jeff Layton2c41beb2014-07-29 21:34:41 -04001429/*
1430 * Free a list of generic stateids that were collected earlier after being
1431 * fully unhashed.
1432 */
1433static void
1434free_ol_stateid_reaplist(struct list_head *reaplist)
1435{
1436 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001437 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001438
1439 might_sleep();
1440
1441 while (!list_empty(reaplist)) {
1442 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1443 st_locks);
1444 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001445 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001446 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001447 if (fp)
1448 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001449 }
1450}
1451
Jeff Laytond83017f2014-07-29 21:34:42 -04001452static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1453 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001454{
1455 struct nfs4_ol_stateid *stp;
1456
Jeff Laytone8568732015-08-24 12:41:47 -04001457 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1458
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001459 while (!list_empty(&open_stp->st_locks)) {
1460 stp = list_entry(open_stp->st_locks.next,
1461 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001462 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001463 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001464 }
1465}
1466
Jeff Laytone8568732015-08-24 12:41:47 -04001467static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001468 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001469{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001470 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1471
Trond Myklebusta451b122020-03-01 18:21:38 -05001472 if (!unhash_ol_stateid(stp))
1473 return false;
Jeff Laytond83017f2014-07-29 21:34:42 -04001474 release_open_stateid_locks(stp, reaplist);
Trond Myklebusta451b122020-03-01 18:21:38 -05001475 return true;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001476}
1477
1478static void release_open_stateid(struct nfs4_ol_stateid *stp)
1479{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001480 LIST_HEAD(reaplist);
1481
1482 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001483 if (unhash_open_stateid(stp, &reaplist))
1484 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001485 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1486 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001487}
1488
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001489static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001490{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001491 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001492
Trond Myklebustd4f04892014-07-29 21:34:36 -04001493 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001494
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001495 list_del_init(&oo->oo_owner.so_strhash);
1496 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001497}
1498
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001499static void release_last_closed_stateid(struct nfs4_openowner *oo)
1500{
Jeff Layton217526e2014-07-30 08:27:11 -04001501 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1502 nfsd_net_id);
1503 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001504
Jeff Layton217526e2014-07-30 08:27:11 -04001505 spin_lock(&nn->client_lock);
1506 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001507 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001508 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001509 oo->oo_last_closed_stid = NULL;
1510 }
Jeff Layton217526e2014-07-30 08:27:11 -04001511 spin_unlock(&nn->client_lock);
1512 if (s)
1513 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001514}
1515
Jeff Layton2c41beb2014-07-29 21:34:41 -04001516static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001517{
1518 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001519 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001520 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001521
Jeff Layton2c41beb2014-07-29 21:34:41 -04001522 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001523
Trond Myklebustd4f04892014-07-29 21:34:36 -04001524 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001525 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001526 while (!list_empty(&oo->oo_owner.so_stateids)) {
1527 stp = list_first_entry(&oo->oo_owner.so_stateids,
1528 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001529 if (unhash_open_stateid(stp, &reaplist))
1530 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001531 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001532 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001533 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001534 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001535 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001536}
1537
Marc Eshel5282fd72009-04-03 08:27:52 +03001538static inline int
1539hash_sessionid(struct nfs4_sessionid *sessionid)
1540{
1541 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1542
1543 return sid->sequence % SESSION_HASH_SIZE;
1544}
1545
Mark Salter135dd002015-04-06 09:46:00 -04001546#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001547static inline void
1548dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1549{
1550 u32 *ptr = (u32 *)(&sessionid->data[0]);
1551 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1552}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001553#else
1554static inline void
1555dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1556{
1557}
1558#endif
1559
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001560/*
1561 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1562 * won't be used for replay.
1563 */
1564void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1565{
1566 struct nfs4_stateowner *so = cstate->replay_owner;
1567
1568 if (nfserr == nfserr_replay_me)
1569 return;
1570
1571 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001572 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001573 return;
1574 }
1575 if (!so)
1576 return;
1577 if (so->so_is_open_owner)
1578 release_last_closed_stateid(openowner(so));
1579 so->so_seqid++;
1580 return;
1581}
Marc Eshel5282fd72009-04-03 08:27:52 +03001582
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001583static void
1584gen_sessionid(struct nfsd4_session *ses)
1585{
1586 struct nfs4_client *clp = ses->se_client;
1587 struct nfsd4_sessionid *sid;
1588
1589 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1590 sid->clientid = clp->cl_clientid;
1591 sid->sequence = current_sessionid++;
1592 sid->reserved = 0;
1593}
1594
1595/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001596 * The protocol defines ca_maxresponssize_cached to include the size of
1597 * the rpc header, but all we need to cache is the data starting after
1598 * the end of the initial SEQUENCE operation--the rest we regenerate
1599 * each time. Therefore we can advertise a ca_maxresponssize_cached
1600 * value that is the number of bytes in our cache plus a few additional
1601 * bytes. In order to stay on the safe side, and not promise more than
1602 * we can cache, those additional bytes must be the minimum possible: 24
1603 * bytes of rpc header (xid through accept state, with AUTH_NULL
1604 * verifier), 12 for the compound header (with zero-length tag), and 44
1605 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001606 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001607#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1608
Andy Adamson557ce262009-08-28 08:45:04 -04001609static void
1610free_session_slots(struct nfsd4_session *ses)
1611{
1612 int i;
1613
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001614 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1615 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001616 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001617 }
Andy Adamson557ce262009-08-28 08:45:04 -04001618}
1619
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001620/*
1621 * We don't actually need to cache the rpc and session headers, so we
1622 * can allocate a little less for each slot:
1623 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001624static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001625{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001626 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001627
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001628 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1629 size = 0;
1630 else
1631 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1632 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001633}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001634
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001635/*
1636 * XXX: If we run out of reserved DRC memory we could (up to a point)
1637 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001638 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001639 */
NeilBrown2030ca52019-09-20 16:36:45 +10001640static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001641{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001642 u32 slotsize = slot_bytes(ca);
1643 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001644 unsigned long avail, total_avail;
NeilBrown2030ca52019-09-20 16:36:45 +10001645 unsigned int scale_factor;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001646
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001647 spin_lock(&nfsd_drc_lock);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001648 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1649 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1650 else
1651 /* We have handed out more space than we chose in
1652 * set_max_drc() to allow. That isn't really a
1653 * problem as long as that doesn't make us think we
1654 * have lots more due to integer overflow.
1655 */
1656 total_avail = 0;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001657 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001658 /*
NeilBrown2030ca52019-09-20 16:36:45 +10001659 * Never use more than a fraction of the remaining memory,
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001660 * unless it's the only way to give this client a slot.
NeilBrown2030ca52019-09-20 16:36:45 +10001661 * The chosen fraction is either 1/8 or 1/number of threads,
1662 * whichever is smaller. This ensures there are adequate
1663 * slots to support multiple clients per thread.
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001664 * Give the client one slot even if that would require
1665 * over-allocation--it is better than failure.
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001666 */
NeilBrown2030ca52019-09-20 16:36:45 +10001667 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1668
1669 avail = clamp_t(unsigned long, avail, slotsize,
1670 total_avail/scale_factor);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001671 num = min_t(int, num, avail / slotsize);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001672 num = max_t(int, num, 1);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001673 nfsd_drc_mem_used += num * slotsize;
1674 spin_unlock(&nfsd_drc_lock);
1675
1676 return num;
1677}
1678
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001679static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001680{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001681 int slotsize = slot_bytes(ca);
1682
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001683 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001684 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001685 spin_unlock(&nfsd_drc_lock);
1686}
1687
Kinglong Mee60810e52014-01-01 00:35:47 +08001688static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1689 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001690{
Kinglong Mee60810e52014-01-01 00:35:47 +08001691 int numslots = fattrs->maxreqs;
1692 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001693 struct nfsd4_session *new;
1694 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001695
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001696 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001697 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1698 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001699
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001700 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001701 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001702 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001703 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001704 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001705 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001706 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001707 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001708 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001709
1710 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1711 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1712
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001713 return new;
1714out_free:
1715 while (i--)
1716 kfree(new->se_slots[i]);
1717 kfree(new);
1718 return NULL;
1719}
1720
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001721static void free_conn(struct nfsd4_conn *c)
1722{
1723 svc_xprt_put(c->cn_xprt);
1724 kfree(c);
1725}
1726
1727static void nfsd4_conn_lost(struct svc_xpt_user *u)
1728{
1729 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1730 struct nfs4_client *clp = c->cn_session->se_client;
1731
1732 spin_lock(&clp->cl_lock);
1733 if (!list_empty(&c->cn_persession)) {
1734 list_del(&c->cn_persession);
1735 free_conn(c);
1736 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001737 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001738 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001739}
1740
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001741static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001742{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001743 struct nfsd4_conn *conn;
1744
1745 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1746 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001747 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001748 svc_xprt_get(rqstp->rq_xprt);
1749 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001750 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001751 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1752 return conn;
1753}
1754
J. Bruce Fields328ead22010-09-29 16:11:06 -04001755static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1756{
1757 conn->cn_session = ses;
1758 list_add(&conn->cn_persession, &ses->se_conns);
1759}
1760
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001761static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1762{
1763 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001764
1765 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001766 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001767 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001768}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001769
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001770static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001771{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001772 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001773 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001774}
1775
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001776static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001777{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001778 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001779
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001780 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001781 ret = nfsd4_register_conn(conn);
1782 if (ret)
1783 /* oops; xprt is already down: */
1784 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001785 /* We may have gained or lost a callback channel: */
1786 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001787}
1788
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001789static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001790{
1791 u32 dir = NFS4_CDFC4_FORE;
1792
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001793 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001794 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001795 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001796}
1797
1798/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001799static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001800{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001801 struct nfs4_client *clp = s->se_client;
1802 struct nfsd4_conn *c;
1803
1804 spin_lock(&clp->cl_lock);
1805 while (!list_empty(&s->se_conns)) {
1806 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1807 list_del_init(&c->cn_persession);
1808 spin_unlock(&clp->cl_lock);
1809
1810 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1811 free_conn(c);
1812
1813 spin_lock(&clp->cl_lock);
1814 }
1815 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001816}
1817
J. Bruce Fields1377b692012-09-11 21:42:40 -04001818static void __free_session(struct nfsd4_session *ses)
1819{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001820 free_session_slots(ses);
1821 kfree(ses);
1822}
1823
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001824static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001825{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001826 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001827 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001828 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001829}
Andy Adamson557ce262009-08-28 08:45:04 -04001830
Fengguang Wu135ae822012-11-10 07:20:25 -05001831static 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 -04001832{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001833 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001834 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001835
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001836 new->se_client = clp;
1837 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001838
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001839 INIT_LIST_HEAD(&new->se_conns);
1840
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001841 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001842 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001843 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001844 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001845 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001846 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001847 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001848 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001849 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001850 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001851
Chuck Leverb0d2e422014-08-22 15:10:59 -04001852 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001853 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001854 /*
1855 * This is a little silly; with sessions there's no real
1856 * use for the callback address. Use the peer address
1857 * as a reasonable default for now, but consider fixing
1858 * the rpc client not to require an address in the
1859 * future:
1860 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001861 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1862 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001863 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001864}
1865
Benny Halevy9089f1b2010-05-12 00:12:26 +03001866/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001867static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001868__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001869{
1870 struct nfsd4_session *elem;
1871 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001872 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001873
Trond Myklebust0a880a22014-07-30 08:27:10 -04001874 lockdep_assert_held(&nn->client_lock);
1875
Marc Eshel5282fd72009-04-03 08:27:52 +03001876 dump_sessionid(__func__, sessionid);
1877 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001878 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001879 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001880 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1881 NFS4_MAX_SESSIONID_LEN)) {
1882 return elem;
1883 }
1884 }
1885
1886 dprintk("%s: session not found\n", __func__);
1887 return NULL;
1888}
1889
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001890static struct nfsd4_session *
1891find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1892 __be32 *ret)
1893{
1894 struct nfsd4_session *session;
1895 __be32 status = nfserr_badsession;
1896
1897 session = __find_in_sessionid_hashtbl(sessionid, net);
1898 if (!session)
1899 goto out;
1900 status = nfsd4_get_session_locked(session);
1901 if (status)
1902 session = NULL;
1903out:
1904 *ret = status;
1905 return session;
1906}
1907
Benny Halevy9089f1b2010-05-12 00:12:26 +03001908/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001909static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001910unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001911{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001912 struct nfs4_client *clp = ses->se_client;
1913 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1914
1915 lockdep_assert_held(&nn->client_lock);
1916
Andy Adamson7116ed62009-04-03 08:27:43 +03001917 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001918 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001919 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001920 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001921}
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1924static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001925STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001927 /*
1928 * We're assuming the clid was not given out from a boot
1929 * precisely 2^32 (about 136 years) before this one. That seems
1930 * a safe assumption:
1931 */
1932 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return 0;
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04001934 trace_nfsd_clid_stale(clid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return 1;
1936}
1937
1938/*
1939 * XXX Should we use a slab cache ?
1940 * This type of memory management is somewhat inefficient, but we use it
1941 * anyway since SETCLIENTID is not a common operation.
1942 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001943static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
1945 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001946 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Jeff Layton9258a2d2018-03-16 09:47:22 -04001948 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001949 if (clp == NULL)
1950 return NULL;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04001951 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001952 if (clp->cl_name.data == NULL)
1953 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001954 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1955 sizeof(struct list_head),
1956 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001957 if (!clp->cl_ownerstr_hashtbl)
1958 goto err_no_hashtbl;
1959 for (i = 0; i < OWNER_HASH_SIZE; i++)
1960 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
Trond Myklebust5694c932014-04-18 14:43:56 -04001961 INIT_LIST_HEAD(&clp->cl_sessions);
1962 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001963 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04001964 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1965 INIT_LIST_HEAD(&clp->cl_idhash);
1966 INIT_LIST_HEAD(&clp->cl_openowners);
1967 INIT_LIST_HEAD(&clp->cl_delegations);
1968 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001969 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001970#ifdef CONFIG_NFSD_PNFS
1971 INIT_LIST_HEAD(&clp->cl_lo_states);
1972#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001973 INIT_LIST_HEAD(&clp->async_copies);
1974 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001975 spin_lock_init(&clp->cl_lock);
1976 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001978err_no_hashtbl:
1979 kfree(clp->cl_name.data);
1980err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001981 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001982 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001985static void __free_client(struct kref *k)
1986{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001987 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1988 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001989
1990 free_svc_cred(&clp->cl_cred);
1991 kfree(clp->cl_ownerstr_hashtbl);
1992 kfree(clp->cl_name.data);
J. Bruce Fields79123442019-06-05 12:42:05 -04001993 kfree(clp->cl_nii_domain.data);
1994 kfree(clp->cl_nii_name.data);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001995 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001996 kmem_cache_free(client_slab, clp);
1997}
1998
YueHaibing297e57a2019-07-08 15:29:33 +08001999static void drop_client(struct nfs4_client *clp)
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002000{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002001 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002002}
2003
Trond Myklebust4dd86e152014-04-18 14:43:58 -04002004static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005free_client(struct nfs4_client *clp)
2006{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002007 while (!list_empty(&clp->cl_sessions)) {
2008 struct nfsd4_session *ses;
2009 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2010 se_perclnt);
2011 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002012 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2013 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002014 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04002015 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002016 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002017 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002018 clp->cl_nfsd_dentry = NULL;
2019 wake_up_all(&expiry_wq);
2020 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002021 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Benny Halevy84d38ac2010-05-12 00:13:16 +03002024/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04002025static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03002026unhash_client_locked(struct nfs4_client *clp)
2027{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002028 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002029 struct nfsd4_session *ses;
2030
Trond Myklebust0a880a22014-07-30 08:27:10 -04002031 lockdep_assert_held(&nn->client_lock);
2032
Trond Myklebust4beb3452014-07-30 08:27:02 -04002033 /* Mark the client as expired! */
2034 clp->cl_time = 0;
2035 /* Make it invisible */
2036 if (!list_empty(&clp->cl_idhash)) {
2037 list_del_init(&clp->cl_idhash);
2038 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2039 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2040 else
2041 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2042 }
2043 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002044 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002045 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2046 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002047 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03002048}
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04002051unhash_client(struct nfs4_client *clp)
2052{
2053 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2054
2055 spin_lock(&nn->client_lock);
2056 unhash_client_locked(clp);
2057 spin_unlock(&nn->client_lock);
2058}
2059
Jeff Layton97403d92014-07-30 08:27:12 -04002060static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2061{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04002062 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04002063 return nfserr_jukebox;
2064 unhash_client_locked(clp);
2065 return nfs_ok;
2066}
2067
Trond Myklebust4beb3452014-07-30 08:27:02 -04002068static void
2069__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002071 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002072 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 struct list_head reaplist;
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04002077 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07002078 while (!list_empty(&clp->cl_delegations)) {
2079 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04002080 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04002081 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Benny Halevycdc975052014-05-30 09:09:30 -04002083 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 while (!list_empty(&reaplist)) {
2085 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04002086 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05002087 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04002089 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05002090 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04002091 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04002092 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002093 }
NeilBrownea1da632005-06-23 22:04:17 -07002094 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002095 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002096 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002097 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002099 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2100 struct nfs4_stateowner *so, *tmp;
2101
2102 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2103 so_strhash) {
2104 /* Should be no openowners at this point */
2105 WARN_ON_ONCE(so->so_is_open_owner);
2106 remove_blocked_locks(lockowner(so));
2107 }
2108 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002109 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002110 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002111 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002112 if (clp->cl_cb_conn.cb_xprt)
2113 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002114 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002115 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
Trond Myklebust4beb3452014-07-30 08:27:02 -04002118static void
2119destroy_client(struct nfs4_client *clp)
2120{
2121 unhash_client(clp);
2122 __destroy_client(clp);
2123}
2124
Scott Mayhew362063a2019-03-26 18:06:28 -04002125static void inc_reclaim_complete(struct nfs4_client *clp)
2126{
2127 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2128
2129 if (!nn->track_reclaim_completes)
2130 return;
2131 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2132 return;
2133 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2134 nn->reclaim_str_hashtbl_size) {
2135 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2136 clp->net->ns.inum);
2137 nfsd4_end_grace(nn);
2138 }
2139}
2140
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002141static void expire_client(struct nfs4_client *clp)
2142{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002143 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002144 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002145 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002146}
2147
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002148static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2149{
2150 memcpy(target->cl_verifier.data, source->data,
2151 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002154static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2155{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2157 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2158}
2159
J. Bruce Fields50043852015-11-20 15:58:37 -05002160static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2161{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002162 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2163 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2164 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002165 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2166 if ((source->cr_principal && !target->cr_principal) ||
2167 (source->cr_raw_principal && !target->cr_raw_principal) ||
2168 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002169 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002170
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002171 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 target->cr_uid = source->cr_uid;
2173 target->cr_gid = source->cr_gid;
2174 target->cr_group_info = source->cr_group_info;
2175 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002176 target->cr_gss_mech = source->cr_gss_mech;
2177 if (source->cr_gss_mech)
2178 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002182static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002183compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2184{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002185 if (o1->len < o2->len)
2186 return -1;
2187 if (o1->len > o2->len)
2188 return 1;
2189 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002190}
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002193same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2194{
2195 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
2198static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002199same_clid(clientid_t *cl1, clientid_t *cl2)
2200{
2201 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002204static bool groups_equal(struct group_info *g1, struct group_info *g2)
2205{
2206 int i;
2207
2208 if (g1->ngroups != g2->ngroups)
2209 return false;
2210 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002211 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002212 return false;
2213 return true;
2214}
2215
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002216/*
2217 * RFC 3530 language requires clid_inuse be returned when the
2218 * "principal" associated with a requests differs from that previously
2219 * used. We use uid, gid's, and gss principal string as our best
2220 * approximation. We also don't want to allow non-gss use of a client
2221 * established using gss: in theory cr_principal should catch that
2222 * change, but in practice cr_principal can be null even in the gss case
2223 * since gssd doesn't always pass down a principal string.
2224 */
2225static bool is_gss_cred(struct svc_cred *cr)
2226{
2227 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2228 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2229}
2230
2231
Vivek Trivedi5559b502012-07-24 21:18:20 +05302232static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002233same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2234{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002235 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002236 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2237 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002238 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2239 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002240 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002241 if (cr1->cr_principal == cr2->cr_principal)
2242 return true;
2243 if (!cr1->cr_principal || !cr2->cr_principal)
2244 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302245 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
J. Bruce Fields57266a62013-04-13 14:27:29 -04002248static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2249{
2250 struct svc_cred *cr = &rqstp->rq_cred;
2251 u32 service;
2252
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002253 if (!cr->cr_gss_mech)
2254 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002255 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2256 return service == RPC_GSS_SVC_INTEGRITY ||
2257 service == RPC_GSS_SVC_PRIVACY;
2258}
2259
Andrew Elblededeb132016-06-15 12:52:08 -04002260bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002261{
2262 struct svc_cred *cr = &rqstp->rq_cred;
2263
2264 if (!cl->cl_mach_cred)
2265 return true;
2266 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2267 return false;
2268 if (!svc_rqst_integrity_protected(rqstp))
2269 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002270 if (cl->cl_cred.cr_raw_principal)
2271 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2272 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002273 if (!cr->cr_principal)
2274 return false;
2275 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2276}
2277
Jeff Layton294ac322014-07-30 08:27:15 -04002278static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002279{
Chuck Leverab4684d2012-03-02 17:13:50 -05002280 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Jeff Laytonf4199922014-06-17 07:44:11 -04002282 /*
2283 * This is opaque to client, so no need to byte-swap. Use
2284 * __force to keep sparse happy
2285 */
Arnd Bergmann9104ae42019-11-04 16:45:30 +01002286 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002287 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002288 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
Jeff Layton294ac322014-07-30 08:27:15 -04002291static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2292{
Arnd Bergmann9cc76802019-10-31 15:53:13 +01002293 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
Jeff Layton294ac322014-07-30 08:27:15 -04002294 clp->cl_clientid.cl_id = nn->clientid_counter++;
2295 gen_confirm(clp, nn);
2296}
2297
Jeff Layton4770d722014-07-29 21:34:10 -04002298static struct nfs4_stid *
2299find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002300{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002301 struct nfs4_stid *ret;
2302
2303 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2304 if (!ret || !ret->sc_type)
2305 return NULL;
2306 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002307}
2308
Jeff Layton4770d722014-07-29 21:34:10 -04002309static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002310find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002311{
2312 struct nfs4_stid *s;
2313
Jeff Layton4770d722014-07-29 21:34:10 -04002314 spin_lock(&cl->cl_lock);
2315 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002316 if (s != NULL) {
2317 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002318 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002319 else
2320 s = NULL;
2321 }
Jeff Layton4770d722014-07-29 21:34:10 -04002322 spin_unlock(&cl->cl_lock);
2323 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002324}
2325
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002326static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2327{
2328 struct nfsdfs_client *nc;
2329 nc = get_nfsdfs_client(inode);
2330 if (!nc)
2331 return NULL;
2332 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2333}
2334
J. Bruce Fields169319f2019-06-19 12:39:46 -04002335static void seq_quote_mem(struct seq_file *m, char *data, int len)
2336{
2337 seq_printf(m, "\"");
2338 seq_escape_mem_ascii(m, data, len);
2339 seq_printf(m, "\"");
2340}
2341
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002342static int client_info_show(struct seq_file *m, void *v)
2343{
2344 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002345 struct nfs4_client *clp;
2346 u64 clid;
2347
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002348 clp = get_nfsdfs_clp(inode);
2349 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002350 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002351 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2352 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002353 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
NeilBrown472d1552021-03-20 09:38:04 +11002354 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2355 seq_puts(m, "status: confirmed\n");
2356 else
2357 seq_puts(m, "status: unconfirmed\n");
J. Bruce Fields169319f2019-06-19 12:39:46 -04002358 seq_printf(m, "name: ");
2359 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2360 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields79123442019-06-05 12:42:05 -04002361 if (clp->cl_nii_domain.data) {
2362 seq_printf(m, "Implementation domain: ");
2363 seq_quote_mem(m, clp->cl_nii_domain.data,
2364 clp->cl_nii_domain.len);
2365 seq_printf(m, "\nImplementation name: ");
2366 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
Arnd Bergmanne29f4702019-10-31 14:52:43 +01002367 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
J. Bruce Fields79123442019-06-05 12:42:05 -04002368 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2369 }
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002370 drop_client(clp);
2371
2372 return 0;
2373}
2374
2375static int client_info_open(struct inode *inode, struct file *file)
2376{
2377 return single_open(file, client_info_show, inode);
2378}
2379
2380static const struct file_operations client_info_fops = {
2381 .open = client_info_open,
2382 .read = seq_read,
2383 .llseek = seq_lseek,
2384 .release = single_release,
2385};
2386
J. Bruce Fields78599c42019-04-22 15:26:23 -04002387static void *states_start(struct seq_file *s, loff_t *pos)
2388 __acquires(&clp->cl_lock)
2389{
2390 struct nfs4_client *clp = s->private;
2391 unsigned long id = *pos;
2392 void *ret;
2393
2394 spin_lock(&clp->cl_lock);
2395 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2396 *pos = id;
2397 return ret;
2398}
2399
2400static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2401{
2402 struct nfs4_client *clp = s->private;
2403 unsigned long id = *pos;
2404 void *ret;
2405
2406 id = *pos;
2407 id++;
2408 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2409 *pos = id;
2410 return ret;
2411}
2412
2413static void states_stop(struct seq_file *s, void *v)
2414 __releases(&clp->cl_lock)
2415{
2416 struct nfs4_client *clp = s->private;
2417
2418 spin_unlock(&clp->cl_lock);
2419}
2420
Achilles Gaikwad580da462020-04-20 18:20:31 +05302421static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2422{
2423 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2424}
2425
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002426static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002427{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002428 struct inode *inode = f->nf_inode;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002429
2430 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2431 MAJOR(inode->i_sb->s_dev),
2432 MINOR(inode->i_sb->s_dev),
2433 inode->i_ino);
2434}
2435
2436static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2437{
2438 seq_printf(s, "owner: ");
2439 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2440}
2441
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002442static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2443{
J. Bruce Fieldsee590d22020-04-13 22:03:06 -04002444 seq_printf(s, "0x%.8x", stid->si_generation);
2445 seq_printf(s, "%12phN", &stid->si_opaque);
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002446}
2447
J. Bruce Fields78599c42019-04-22 15:26:23 -04002448static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2449{
2450 struct nfs4_ol_stateid *ols;
2451 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002452 struct nfsd_file *file;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002453 struct nfs4_stateowner *oo;
2454 unsigned int access, deny;
2455
2456 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2457 return 0; /* XXX: or SEQ_SKIP? */
2458 ols = openlockstateid(st);
2459 oo = ols->st_stateowner;
2460 nf = st->sc_file;
2461 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002462 if (!file)
2463 return 0;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002464
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002465 seq_printf(s, "- ");
2466 nfs4_show_stateid(s, &st->sc_stateid);
2467 seq_printf(s, ": { type: open, ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002468
2469 access = bmap_to_share_mode(ols->st_access_bmap);
2470 deny = bmap_to_share_mode(ols->st_deny_bmap);
2471
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002472 seq_printf(s, "access: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002473 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2474 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002475 seq_printf(s, "deny: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002476 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2477 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2478
2479 nfs4_show_superblock(s, file);
2480 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302481 nfs4_show_fname(s, file);
2482 seq_printf(s, ", ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002483 nfs4_show_owner(s, oo);
2484 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002485 nfsd_file_put(file);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002486
2487 return 0;
2488}
2489
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002490static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2491{
2492 struct nfs4_ol_stateid *ols;
2493 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002494 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002495 struct nfs4_stateowner *oo;
2496
2497 ols = openlockstateid(st);
2498 oo = ols->st_stateowner;
2499 nf = st->sc_file;
2500 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002501 if (!file)
2502 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002503
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002504 seq_printf(s, "- ");
2505 nfs4_show_stateid(s, &st->sc_stateid);
2506 seq_printf(s, ": { type: lock, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002507
2508 /*
2509 * Note: a lock stateid isn't really the same thing as a lock,
2510 * it's the locking state held by one owner on a file, and there
2511 * may be multiple (or no) lock ranges associated with it.
2512 * (Same for the matter is true of open stateids.)
2513 */
2514
2515 nfs4_show_superblock(s, file);
2516 /* XXX: open stateid? */
2517 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302518 nfs4_show_fname(s, file);
2519 seq_printf(s, ", ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002520 nfs4_show_owner(s, oo);
2521 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002522 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002523
2524 return 0;
2525}
2526
2527static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2528{
2529 struct nfs4_delegation *ds;
2530 struct nfs4_file *nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002531 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002532
2533 ds = delegstateid(st);
2534 nf = st->sc_file;
J. Bruce Fields9affa432020-07-15 13:31:36 -04002535 file = find_deleg_file(nf);
2536 if (!file)
2537 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002538
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002539 seq_printf(s, "- ");
2540 nfs4_show_stateid(s, &st->sc_stateid);
2541 seq_printf(s, ": { type: deleg, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002542
2543 /* Kinda dead code as long as we only support read delegs: */
2544 seq_printf(s, "access: %s, ",
2545 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2546
2547 /* XXX: lease time, whether it's being recalled. */
2548
2549 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302550 seq_printf(s, ", ");
2551 nfs4_show_fname(s, file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002552 seq_printf(s, " }\n");
J. Bruce Fields9affa432020-07-15 13:31:36 -04002553 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002554
2555 return 0;
2556}
2557
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002558static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2559{
2560 struct nfs4_layout_stateid *ls;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002561 struct nfsd_file *file;
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002562
2563 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2564 file = ls->ls_file;
2565
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002566 seq_printf(s, "- ");
2567 nfs4_show_stateid(s, &st->sc_stateid);
2568 seq_printf(s, ": { type: layout, ");
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002569
2570 /* XXX: What else would be useful? */
2571
2572 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302573 seq_printf(s, ", ");
2574 nfs4_show_fname(s, file);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002575 seq_printf(s, " }\n");
2576
2577 return 0;
2578}
2579
J. Bruce Fields78599c42019-04-22 15:26:23 -04002580static int states_show(struct seq_file *s, void *v)
2581{
2582 struct nfs4_stid *st = v;
2583
2584 switch (st->sc_type) {
2585 case NFS4_OPEN_STID:
2586 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002587 case NFS4_LOCK_STID:
2588 return nfs4_show_lock(s, st);
2589 case NFS4_DELEG_STID:
2590 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002591 case NFS4_LAYOUT_STID:
2592 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002593 default:
2594 return 0; /* XXX: or SEQ_SKIP? */
2595 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002596 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002597}
2598
2599static struct seq_operations states_seq_ops = {
2600 .start = states_start,
2601 .next = states_next,
2602 .stop = states_stop,
2603 .show = states_show
2604};
2605
2606static int client_states_open(struct inode *inode, struct file *file)
2607{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002608 struct seq_file *s;
2609 struct nfs4_client *clp;
2610 int ret;
2611
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002612 clp = get_nfsdfs_clp(inode);
2613 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002614 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002615
2616 ret = seq_open(file, &states_seq_ops);
2617 if (ret)
2618 return ret;
2619 s = file->private_data;
2620 s->private = clp;
2621 return 0;
2622}
2623
2624static int client_opens_release(struct inode *inode, struct file *file)
2625{
2626 struct seq_file *m = file->private_data;
2627 struct nfs4_client *clp = m->private;
2628
2629 /* XXX: alternatively, we could get/drop in seq start/stop */
2630 drop_client(clp);
2631 return 0;
2632}
2633
2634static const struct file_operations client_states_fops = {
2635 .open = client_states_open,
2636 .read = seq_read,
2637 .llseek = seq_lseek,
2638 .release = client_opens_release,
2639};
2640
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002641/*
2642 * Normally we refuse to destroy clients that are in use, but here the
2643 * administrator is telling us to just do it. We also want to wait
2644 * so the caller has a guarantee that the client's locks are gone by
2645 * the time the write returns:
2646 */
YueHaibing297e57a2019-07-08 15:29:33 +08002647static void force_expire_client(struct nfs4_client *clp)
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002648{
2649 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2650 bool already_expired;
2651
2652 spin_lock(&clp->cl_lock);
2653 clp->cl_time = 0;
2654 spin_unlock(&clp->cl_lock);
2655
2656 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2657 spin_lock(&nn->client_lock);
2658 already_expired = list_empty(&clp->cl_lru);
2659 if (!already_expired)
2660 unhash_client_locked(clp);
2661 spin_unlock(&nn->client_lock);
2662
2663 if (!already_expired)
2664 expire_client(clp);
2665 else
2666 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2667}
2668
2669static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2670 size_t size, loff_t *pos)
2671{
2672 char *data;
2673 struct nfs4_client *clp;
2674
2675 data = simple_transaction_get(file, buf, size);
2676 if (IS_ERR(data))
2677 return PTR_ERR(data);
2678 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2679 return -EINVAL;
2680 clp = get_nfsdfs_clp(file_inode(file));
2681 if (!clp)
2682 return -ENXIO;
2683 force_expire_client(clp);
2684 drop_client(clp);
2685 return 7;
2686}
2687
2688static const struct file_operations client_ctl_fops = {
2689 .write = client_ctl_write,
2690 .release = simple_transaction_release,
2691};
2692
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002693static const struct tree_descr client_files[] = {
2694 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002695 [1] = {"states", &client_states_fops, S_IRUSR},
Petr Vorel6cbfad52020-03-13 13:39:57 +01002696 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002697 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002698};
2699
Jeff Layton2216d442012-11-12 15:00:57 -05002700static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002701 struct svc_rqst *rqstp, nfs4_verifier *verf)
2702{
2703 struct nfs4_client *clp;
2704 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002705 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002706 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002707 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
NeilBrown472d1552021-03-20 09:38:04 +11002708 struct dentry *dentries[ARRAY_SIZE(client_files)];
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002709
2710 clp = alloc_client(name);
2711 if (clp == NULL)
2712 return NULL;
2713
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002714 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2715 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002716 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002717 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002718 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002719 gen_clid(clp, nn);
2720 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002721 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01002722 clp->cl_time = ktime_get_boottime_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002723 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002724 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002725 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002726 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002727 clp->net = net;
NeilBrown472d1552021-03-20 09:38:04 +11002728 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2729 nn, &clp->cl_nfsdfs,
2730 clp->cl_clientid.cl_id - nn->clientid_base,
2731 client_files, dentries);
2732 clp->cl_nfsd_info_dentry = dentries[0];
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002733 if (!clp->cl_nfsd_dentry) {
2734 free_client(clp);
2735 return NULL;
2736 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002737 return clp;
2738}
2739
NeilBrownfd39ca92005-06-23 22:04:03 -07002740static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002741add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2742{
2743 struct rb_node **new = &(root->rb_node), *parent = NULL;
2744 struct nfs4_client *clp;
2745
2746 while (*new) {
2747 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2748 parent = *new;
2749
2750 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2751 new = &((*new)->rb_left);
2752 else
2753 new = &((*new)->rb_right);
2754 }
2755
2756 rb_link_node(&new_clp->cl_namenode, parent, new);
2757 rb_insert_color(&new_clp->cl_namenode, root);
2758}
2759
2760static struct nfs4_client *
2761find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2762{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002763 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002764 struct rb_node *node = root->rb_node;
2765 struct nfs4_client *clp;
2766
2767 while (node) {
2768 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2769 cmp = compare_blob(&clp->cl_name, name);
2770 if (cmp > 0)
2771 node = node->rb_left;
2772 else if (cmp < 0)
2773 node = node->rb_right;
2774 else
2775 return clp;
2776 }
2777 return NULL;
2778}
2779
2780static void
2781add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
2783 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002784 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Trond Myklebust0a880a22014-07-30 08:27:10 -04002786 lockdep_assert_held(&nn->client_lock);
2787
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002788 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002789 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002791 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002792 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
2794
NeilBrownfd39ca92005-06-23 22:04:03 -07002795static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796move_to_confirmed(struct nfs4_client *clp)
2797{
2798 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002799 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Trond Myklebust0a880a22014-07-30 08:27:10 -04002801 lockdep_assert_held(&nn->client_lock);
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002804 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002805 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002806 add_clp_to_name_tree(clp, &nn->conf_name_tree);
NeilBrown472d1552021-03-20 09:38:04 +11002807 if (!test_and_set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags) &&
2808 clp->cl_nfsd_dentry &&
2809 clp->cl_nfsd_info_dentry)
2810 fsnotify_dentry(clp->cl_nfsd_info_dentry, FS_MODIFY);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002811 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
2814static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002815find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
2817 struct nfs4_client *clp;
2818 unsigned int idhashval = clientid_hashval(clid->cl_id);
2819
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002820 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002821 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002822 if ((bool)clp->cl_minorversion != sessions)
2823 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002824 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
2828 return NULL;
2829}
2830
2831static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002832find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2833{
2834 struct list_head *tbl = nn->conf_id_hashtbl;
2835
Trond Myklebust0a880a22014-07-30 08:27:10 -04002836 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002837 return find_client_in_id_table(tbl, clid, sessions);
2838}
2839
2840static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002841find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002843 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Trond Myklebust0a880a22014-07-30 08:27:10 -04002845 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002846 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002849static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002850{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002851 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002852}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002853
NeilBrown28ce6052005-06-23 22:03:56 -07002854static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002855find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002856{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002857 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002858 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002859}
2860
2861static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002862find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002863{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002864 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002865 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002866}
2867
NeilBrownfd39ca92005-06-23 22:04:03 -07002868static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002869gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002871 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002872 struct sockaddr *sa = svc_addr(rqstp);
2873 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002874 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Jeff Layton7077ecb2009-08-14 12:57:58 -04002876 /* Currently, we only support tcp and tcp6 for the callback channel */
2877 if (se->se_callback_netid_len == 3 &&
2878 !memcmp(se->se_callback_netid_val, "tcp", 3))
2879 expected_family = AF_INET;
2880 else if (se->se_callback_netid_len == 4 &&
2881 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2882 expected_family = AF_INET6;
2883 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 goto out_err;
2885
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002886 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002887 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002888 (struct sockaddr *)&conn->cb_addr,
2889 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002890
J. Bruce Fields07263f12010-05-31 19:09:40 -04002891 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002893
J. Bruce Fields07263f12010-05-31 19:09:40 -04002894 if (conn->cb_addr.ss_family == AF_INET6)
2895 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002896
J. Bruce Fields07263f12010-05-31 19:09:40 -04002897 conn->cb_prog = se->se_callback_prog;
2898 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002899 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Chuck Lever1eace0d2020-04-05 14:15:29 -04002900 trace_nfsd_cb_args(clp, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return;
2902out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002903 conn->cb_addr.ss_family = AF_UNSPEC;
2904 conn->cb_addrlen = 0;
Chuck Lever1eace0d2020-04-05 14:15:29 -04002905 trace_nfsd_cb_nodelegs(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 return;
2907}
2908
Andy Adamson074fe892009-04-03 08:28:15 +03002909/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002910 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002911 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002912static void
Andy Adamson074fe892009-04-03 08:28:15 +03002913nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2914{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04002915 struct xdr_buf *buf = resp->xdr->buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002916 struct nfsd4_slot *slot = resp->cstate.slot;
2917 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002918
Andy Adamson557ce262009-08-28 08:45:04 -04002919 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002920
J. Bruce Fields085def32017-10-18 16:17:18 -04002921 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002922 slot->sl_opcnt = resp->opcnt;
2923 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002924 free_svc_cred(&slot->sl_cred);
2925 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002926
J. Bruce Fields085def32017-10-18 16:17:18 -04002927 if (!nfsd4_cache_this(resp)) {
2928 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002929 return;
2930 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002931 slot->sl_flags |= NFSD4_SLOT_CACHED;
2932
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002933 base = resp->cstate.data_offset;
2934 slot->sl_datalen = buf->len - base;
2935 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002936 WARN(1, "%s: sessions DRC could not cache compound\n",
2937 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002938 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002939}
2940
2941/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002942 * Encode the replay sequence operation from the slot values.
2943 * If cachethis is FALSE encode the uncached rep error on the next
2944 * operation which sets resp->p and increments resp->opcnt for
2945 * nfs4svc_encode_compoundres.
2946 *
Andy Adamson074fe892009-04-03 08:28:15 +03002947 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002948static __be32
2949nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2950 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002951{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002952 struct nfsd4_op *op;
2953 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002954
Andy Adamsonabfabf82009-07-23 19:02:18 -04002955 /* Encode the replayed sequence operation */
2956 op = &args->ops[resp->opcnt - 1];
2957 nfsd4_encode_operation(resp, op);
2958
J. Bruce Fields085def32017-10-18 16:17:18 -04002959 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2960 return op->status;
2961 if (args->opcnt == 1) {
2962 /*
2963 * The original operation wasn't a solo sequence--we
2964 * always cache those--so this retry must not match the
2965 * original:
2966 */
2967 op->status = nfserr_seq_false_retry;
2968 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002969 op = &args->ops[resp->opcnt++];
2970 op->status = nfserr_retry_uncached_rep;
2971 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002972 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002973 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002974}
2975
2976/*
Andy Adamson557ce262009-08-28 08:45:04 -04002977 * The sequence operation is not cached because we can use the slot and
2978 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002979 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002980static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002981nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2982 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002983{
Andy Adamson557ce262009-08-28 08:45:04 -04002984 struct nfsd4_slot *slot = resp->cstate.slot;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04002985 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002986 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002987 __be32 status;
2988
Andy Adamson557ce262009-08-28 08:45:04 -04002989 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002990
Andy Adamsonabfabf82009-07-23 19:02:18 -04002991 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002992 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002993 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002994
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002995 p = xdr_reserve_space(xdr, slot->sl_datalen);
2996 if (!p) {
2997 WARN_ON_ONCE(1);
2998 return nfserr_serverfault;
2999 }
3000 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3001 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03003002
Andy Adamson557ce262009-08-28 08:45:04 -04003003 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003004 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03003005}
3006
Andy Adamson0733d212009-04-03 08:28:01 +03003007/*
3008 * Set the exchange_id flags returned by the server.
3009 */
3010static void
3011nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3012{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003013#ifdef CONFIG_NFSD_PNFS
3014 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3015#else
Andy Adamson0733d212009-04-03 08:28:01 +03003016 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003017#endif
Andy Adamson0733d212009-04-03 08:28:01 +03003018
3019 /* Referrals are supported, Migration is not. */
3020 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3021
3022 /* set the wire flags to return to client. */
3023 clid->flags = new->cl_exchange_flags;
3024}
3025
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003026static bool client_has_openowners(struct nfs4_client *clp)
3027{
3028 struct nfs4_openowner *oo;
3029
3030 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3031 if (!list_empty(&oo->oo_owner.so_stateids))
3032 return true;
3033 }
3034 return false;
3035}
3036
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003037static bool client_has_state(struct nfs4_client *clp)
3038{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003039 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08003040#ifdef CONFIG_NFSD_PNFS
3041 || !list_empty(&clp->cl_lo_states)
3042#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04003043 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04003044 || !list_empty(&clp->cl_sessions)
3045 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003046}
3047
J. Bruce Fields79123442019-06-05 12:42:05 -04003048static __be32 copy_impl_id(struct nfs4_client *clp,
3049 struct nfsd4_exchange_id *exid)
3050{
3051 if (!exid->nii_domain.data)
3052 return 0;
3053 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3054 if (!clp->cl_nii_domain.data)
3055 return nfserr_jukebox;
3056 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3057 if (!clp->cl_nii_name.data)
3058 return nfserr_jukebox;
Arnd Bergmanne29f4702019-10-31 14:52:43 +01003059 clp->cl_nii_time = exid->nii_time;
J. Bruce Fields79123442019-06-05 12:42:05 -04003060 return 0;
3061}
3062
Al Virob37ad282006-10-19 23:28:59 -07003063__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003064nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3065 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003066{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003067 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003068 struct nfs4_client *conf, *new;
3069 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003070 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04003071 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03003072 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04003073 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003074 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003075 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03003076
Jeff Layton363168b2009-08-14 12:57:56 -04003077 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03003078 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Chuck Lever523ec6e2020-11-02 15:19:12 -05003079 "ip_addr=%s flags %x, spa_how %u\n",
Andy Adamson0733d212009-04-03 08:28:01 +03003080 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04003081 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03003082
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003083 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03003084 return nfserr_inval;
3085
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003086 new = create_client(exid->clname, rqstp, &verf);
3087 if (new == NULL)
3088 return nfserr_jukebox;
J. Bruce Fields79123442019-06-05 12:42:05 -04003089 status = copy_impl_id(new, exid);
3090 if (status)
3091 goto out_nolock;
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003092
Andy Adamson0733d212009-04-03 08:28:01 +03003093 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003094 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04003095 exid->spo_must_enforce[0] = 0;
3096 exid->spo_must_enforce[1] = (
3097 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3098 1 << (OP_EXCHANGE_ID - 32) |
3099 1 << (OP_CREATE_SESSION - 32) |
3100 1 << (OP_DESTROY_SESSION - 32) |
3101 1 << (OP_DESTROY_CLIENTID - 32));
3102
3103 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3104 1 << (OP_OPEN_DOWNGRADE) |
3105 1 << (OP_LOCKU) |
3106 1 << (OP_DELEGRETURN));
3107
3108 exid->spo_must_allow[1] &= (
3109 1 << (OP_TEST_STATEID - 32) |
3110 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003111 if (!svc_rqst_integrity_protected(rqstp)) {
3112 status = nfserr_inval;
3113 goto out_nolock;
3114 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003115 /*
3116 * Sometimes userspace doesn't give us a principal.
3117 * Which is a bug, really. Anyway, we can't enforce
3118 * MACH_CRED in that case, better to give up now:
3119 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05003120 if (!new->cl_cred.cr_principal &&
3121 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003122 status = nfserr_serverfault;
3123 goto out_nolock;
3124 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003125 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03003126 case SP4_NONE:
3127 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003128 default: /* checked by xdr code */
3129 WARN_ON_ONCE(1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003130 fallthrough;
Andy Adamson0733d212009-04-03 08:28:01 +03003131 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08003132 status = nfserr_encr_alg_unsupp;
3133 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03003134 }
3135
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003136 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003137 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003138 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03003139 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003140 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3141 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3142
J. Bruce Fields136e6582012-05-12 20:37:23 -04003143 if (update) {
3144 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003145 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03003146 goto out;
3147 }
Andrew Elblededeb132016-06-15 12:52:08 -04003148 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003149 status = nfserr_wrong_cred;
3150 goto out;
3151 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003152 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03003153 status = nfserr_perm;
3154 goto out;
3155 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003156 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03003157 status = nfserr_not_same;
3158 goto out;
3159 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003160 /* case 6 */
3161 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003162 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003163 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003164 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003165 if (client_has_state(conf)) {
3166 status = nfserr_clid_inuse;
3167 goto out;
3168 }
Andy Adamson0733d212009-04-03 08:28:01 +03003169 goto out_new;
3170 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003171 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003172 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003173 goto out_copy;
3174 }
3175 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003176 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003177 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003178 }
3179
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003180 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003181 status = nfserr_noent;
3182 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003183 }
3184
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003185 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003186 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003187 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003188
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003189 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03003190out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003191 if (conf) {
3192 status = mark_client_expired_locked(conf);
3193 if (status)
3194 goto out;
3195 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003196 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003197 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3198 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003199
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003200 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003201 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003202out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003203 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3204 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003205
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003206 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3207 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003208
3209 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003210 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003211 status = nfs_ok;
3212
3213out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003214 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003215out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003216 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003217 expire_client(new);
3218 if (unconf)
3219 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003220 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003221}
3222
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003223static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003224check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003225{
Andy Adamson88e588d2009-07-23 19:02:15 -04003226 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3227 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003228
3229 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003230 if (slot_inuse) {
3231 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003232 return nfserr_jukebox;
3233 else
3234 return nfserr_seq_misordered;
3235 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003236 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003237 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003238 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003239 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003240 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003241 return nfserr_seq_misordered;
3242}
3243
Andy Adamson49557cc2009-07-23 19:02:16 -04003244/*
3245 * Cache the create session result into the create session single DRC
3246 * slot cache by saving the xdr structure. sl_seqid has been set.
3247 * Do this for solo or embedded create session operations.
3248 */
3249static void
3250nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003251 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003252{
3253 slot->sl_status = nfserr;
3254 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3255}
3256
3257static __be32
3258nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3259 struct nfsd4_clid_slot *slot)
3260{
3261 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3262 return slot->sl_status;
3263}
3264
Mi Jinlong1b74c252011-07-14 14:50:17 +08003265#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3266 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3267 1 + /* MIN tag is length with zero, only length */ \
3268 3 + /* version, opcount, opcode */ \
3269 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3270 /* seqid, slotID, slotID, cache */ \
3271 4 ) * sizeof(__be32))
3272
3273#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3274 2 + /* verifier: AUTH_NULL, length 0 */\
3275 1 + /* status */ \
3276 1 + /* MIN tag is length with zero, only length */ \
3277 3 + /* opcount, opcode, opstatus*/ \
3278 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3279 /* seqid, slotID, slotID, slotID, status */ \
3280 5 ) * sizeof(__be32))
3281
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003282static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003283{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003284 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3285
J. Bruce Fields373cd402013-04-08 15:42:12 -04003286 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3287 return nfserr_toosmall;
3288 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3289 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003290 ca->headerpadsz = 0;
3291 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3292 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3293 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3294 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3295 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3296 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3297 /*
3298 * Note decreasing slot size below client's request may make it
3299 * difficult for client to function correctly, whereas
3300 * decreasing the number of slots will (just?) affect
3301 * performance. When short on memory we therefore prefer to
3302 * decrease number of slots instead of their size. Clients that
3303 * request larger slots than they need will get poor results:
NeilBrown7f49fd5d2019-09-20 16:33:16 +10003304 * Note that we always allow at least one slot, because our
3305 * accounting is soft and provides no guarantees either way.
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003306 */
NeilBrown2030ca52019-09-20 16:36:45 +10003307 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003308
J. Bruce Fields373cd402013-04-08 15:42:12 -04003309 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003310}
3311
Chuck Lever45006322016-03-01 13:06:02 -05003312/*
3313 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3314 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3315 */
3316#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3317 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3318
3319#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3320 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3321
Kinglong Mee8a891632013-12-23 18:11:02 +08003322#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003323 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003324#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003325 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3326 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003327
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003328static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3329{
3330 ca->headerpadsz = 0;
3331
Kinglong Mee8a891632013-12-23 18:11:02 +08003332 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003333 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003334 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003335 return nfserr_toosmall;
3336 ca->maxresp_cached = 0;
3337 if (ca->maxops < 2)
3338 return nfserr_toosmall;
3339
3340 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003341}
3342
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003343static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3344{
3345 switch (cbs->flavor) {
3346 case RPC_AUTH_NULL:
3347 case RPC_AUTH_UNIX:
3348 return nfs_ok;
3349 default:
3350 /*
3351 * GSS case: the spec doesn't allow us to return this
3352 * error. But it also doesn't allow us not to support
3353 * GSS.
3354 * I'd rather this fail hard than return some error the
3355 * client might think it can already handle:
3356 */
3357 return nfserr_encr_alg_unsupp;
3358 }
3359}
3360
Andy Adamson069b6ad2009-04-03 08:27:58 +03003361__be32
3362nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003363 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003364{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003365 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003366 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003367 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003368 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003369 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003370 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003371 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003372 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003373 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003374
Mi Jinlonga62573d2011-03-23 17:57:07 +08003375 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3376 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003377 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3378 if (status)
3379 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003380 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003381 if (status)
3382 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003383 status = check_backchannel_attrs(&cr_ses->back_channel);
3384 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003385 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003386 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003387 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003388 if (!new)
3389 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003390 conn = alloc_conn_from_crses(rqstp, cr_ses);
3391 if (!conn)
3392 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003393
Jeff Laytond20c11d2014-07-30 08:27:07 -04003394 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003395 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003396 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003397 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003398
3399 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003400 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003401 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003402 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003403 cs_slot = &conf->cl_cs_slot;
3404 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003405 if (status) {
3406 if (status == nfserr_replay_cache)
3407 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003408 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003409 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003410 } else if (unconf) {
3411 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003412 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003413 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003414 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003415 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003416 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003417 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003418 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003419 cs_slot = &unconf->cl_cs_slot;
3420 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003421 if (status) {
3422 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003423 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003424 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003425 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003426 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003427 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003428 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003429 if (status) {
3430 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003431 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003432 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003433 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003434 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003435 conf = unconf;
3436 } else {
3437 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003438 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003439 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003440 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003441 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003442 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003443 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003444 cr_ses->flags &= ~SESSION4_RDMA;
3445
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003446 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003447 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003448
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003449 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003450 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003451 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003452 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003453
Jeff Laytond20c11d2014-07-30 08:27:07 -04003454 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003455 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003456 spin_unlock(&nn->client_lock);
3457 /* init connection and backchannel */
3458 nfsd4_init_conn(rqstp, conn, new);
3459 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003460 if (old)
3461 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003462 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003463out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003464 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003465 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003466 if (old)
3467 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003468out_free_session:
3469 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003470out_release_drc_mem:
3471 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003472 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003473}
3474
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003475static __be32 nfsd4_map_bcts_dir(u32 *dir)
3476{
3477 switch (*dir) {
3478 case NFS4_CDFC4_FORE:
3479 case NFS4_CDFC4_BACK:
3480 return nfs_ok;
3481 case NFS4_CDFC4_FORE_OR_BOTH:
3482 case NFS4_CDFC4_BACK_OR_BOTH:
3483 *dir = NFS4_CDFC4_BOTH;
3484 return nfs_ok;
zhengbinfc5fc5d2019-12-19 17:29:20 +08003485 }
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003486 return nfserr_inval;
3487}
3488
Christoph Hellwigeb698532017-05-08 20:58:35 +02003489__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3490 struct nfsd4_compound_state *cstate,
3491 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003492{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003493 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003494 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003495 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003496 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003497
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003498 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3499 if (status)
3500 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003501 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003502 session->se_cb_prog = bc->bc_cb_program;
3503 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003504 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003505
3506 nfsd4_probe_callback(session->se_client);
3507
3508 return nfs_ok;
3509}
3510
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003511static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3512{
3513 struct nfsd4_conn *c;
3514
3515 list_for_each_entry(c, &s->se_conns, cn_persession) {
3516 if (c->cn_xprt == xpt) {
3517 return c;
3518 }
3519 }
3520 return NULL;
3521}
3522
3523static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3524 struct nfsd4_session *session, u32 req)
3525{
3526 struct nfs4_client *clp = session->se_client;
3527 struct svc_xprt *xpt = rqst->rq_xprt;
3528 struct nfsd4_conn *c;
3529 __be32 status;
3530
3531 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3532 spin_lock(&clp->cl_lock);
3533 c = __nfsd4_find_conn(xpt, session);
3534 if (!c)
3535 status = nfserr_noent;
3536 else if (req == c->cn_flags)
3537 status = nfs_ok;
3538 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3539 c->cn_flags != NFS4_CDFC4_BACK)
3540 status = nfs_ok;
3541 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3542 c->cn_flags != NFS4_CDFC4_FORE)
3543 status = nfs_ok;
3544 else
3545 status = nfserr_inval;
3546 spin_unlock(&clp->cl_lock);
3547 return status;
3548}
3549
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003550__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3551 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003552 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003553{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003554 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003555 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003556 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003557 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003558 struct net *net = SVC_NET(rqstp);
3559 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003560
3561 if (!nfsd4_last_compound_op(rqstp))
3562 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003563 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003564 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003565 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003566 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003567 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003568 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003569 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003570 goto out;
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003571 status = nfsd4_match_existing_connection(rqstp, session, bcts->dir);
3572 if (status == nfs_ok || status == nfserr_inval)
3573 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003574 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003575 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003576 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003577 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003578 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003579 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003580 goto out;
3581 nfsd4_init_conn(rqstp, conn, session);
3582 status = nfs_ok;
3583out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003584 nfsd4_put_session(session);
3585out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003586 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003587}
3588
J. Bruce Fields665d5072018-04-11 16:53:36 -04003589static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003590{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003591 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003592 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003593 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003594}
3595
Andy Adamson069b6ad2009-04-03 08:27:58 +03003596__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003597nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3598 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003599{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003600 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003601 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003602 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003603 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003604 struct net *net = SVC_NET(r);
3605 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003606
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003607 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003608 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003609 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003610 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003611 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003612 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003613 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003614 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003615 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003616 if (!ses)
3617 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003618 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003619 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003620 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003621 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003622 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003623 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003624 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003625 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003626
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003627 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003628
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003629 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003630 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003631out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003632 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003633out_client_lock:
3634 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003635out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003636 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003637}
3638
J. Bruce Fields57266a62013-04-13 14:27:29 -04003639static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003640{
3641 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003642 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003643 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003644 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003645
3646 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003647 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003648 if (c)
3649 goto out_free;
3650 status = nfserr_conn_not_bound_to_session;
3651 if (clp->cl_mach_cred)
3652 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003653 __nfsd4_hash_conn(new, ses);
3654 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003655 ret = nfsd4_register_conn(new);
3656 if (ret)
3657 /* oops; xprt is already down: */
3658 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003659 return nfs_ok;
3660out_free:
3661 spin_unlock(&clp->cl_lock);
3662 free_conn(new);
3663 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003664}
3665
Mi Jinlong868b89c2011-04-27 09:09:58 +08003666static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3667{
3668 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3669
3670 return args->opcnt > session->se_fchannel.maxops;
3671}
3672
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003673static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3674 struct nfsd4_session *session)
3675{
3676 struct xdr_buf *xb = &rqstp->rq_arg;
3677
3678 return xb->len > session->se_fchannel.maxreq_sz;
3679}
3680
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003681static bool replay_matches_cache(struct svc_rqst *rqstp,
3682 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3683{
3684 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3685
3686 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3687 (bool)seq->cachethis)
3688 return false;
3689 /*
Scott Mayhew6e73e922019-10-09 15:11:37 -04003690 * If there's an error then the reply can have fewer ops than
3691 * the call.
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003692 */
Scott Mayhew6e73e922019-10-09 15:11:37 -04003693 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3694 return false;
3695 /*
3696 * But if we cached a reply with *more* ops than the call you're
3697 * sending us now, then this new call is clearly not really a
3698 * replay of the old one:
3699 */
3700 if (slot->sl_opcnt > argp->opcnt)
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003701 return false;
3702 /* This is the only check explicitly called by spec: */
3703 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3704 return false;
3705 /*
3706 * There may be more comparisons we could actually do, but the
3707 * spec doesn't require us to catch every case where the calls
3708 * don't match (that would require caching the call as well as
3709 * the reply), so we don't bother.
3710 */
3711 return true;
3712}
3713
Andy Adamson069b6ad2009-04-03 08:27:58 +03003714__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003715nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3716 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003717{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003718 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003719 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003720 struct xdr_stream *xdr = resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003721 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003722 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003723 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003724 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003725 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003726 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003727 struct net *net = SVC_NET(rqstp);
3728 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003729
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003730 if (resp->opcnt != 1)
3731 return nfserr_sequence_pos;
3732
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003733 /*
3734 * Will be either used or freed by nfsd4_sequence_check_conn
3735 * below.
3736 */
3737 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3738 if (!conn)
3739 return nfserr_jukebox;
3740
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003741 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003742 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003743 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003744 goto out_no_session;
3745 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003746
Mi Jinlong868b89c2011-04-27 09:09:58 +08003747 status = nfserr_too_many_ops;
3748 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003749 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003750
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003751 status = nfserr_req_too_big;
3752 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003753 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003754
Benny Halevyb85d4c02009-04-03 08:28:08 +03003755 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003756 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003757 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003758
Andy Adamson557ce262009-08-28 08:45:04 -04003759 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003760 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3761
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003762 /* We do not negotiate the number of slots yet, so set the
3763 * maxslots to the session maxreqs which is used to encode
3764 * sr_highest_slotid and the sr_target_slot id to maxslots */
3765 seq->maxslots = session->se_fchannel.maxreqs;
3766
J. Bruce Fields73e79482012-02-13 16:39:00 -05003767 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3768 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003769 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003770 status = nfserr_seq_misordered;
3771 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003772 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003773 status = nfserr_seq_false_retry;
3774 if (!replay_matches_cache(rqstp, seq, slot))
3775 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003776 cstate->slot = slot;
3777 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003778 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003779 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003780 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003781 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003782 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003783 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003784 }
3785 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003786 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003787
J. Bruce Fields57266a62013-04-13 14:27:29 -04003788 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003789 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003790 if (status)
3791 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003792
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003793 buflen = (seq->cachethis) ?
3794 session->se_fchannel.maxresp_cached :
3795 session->se_fchannel.maxresp_sz;
3796 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3797 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003798 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003799 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003800 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003801
3802 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003803 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003804 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003805 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003806 if (seq->cachethis)
3807 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003808 else
3809 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003810
3811 cstate->slot = slot;
3812 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003813 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003814
Benny Halevyb85d4c02009-04-03 08:28:08 +03003815out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003816 switch (clp->cl_cb_state) {
3817 case NFSD4_CB_DOWN:
3818 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3819 break;
3820 case NFSD4_CB_FAULT:
3821 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3822 break;
3823 default:
3824 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003825 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003826 if (!list_empty(&clp->cl_revoked))
3827 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003828out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003829 if (conn)
3830 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003831 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003832 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003833out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003834 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003835 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003836}
3837
Trond Myklebustb6076642014-06-30 11:48:35 -04003838void
3839nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3840{
3841 struct nfsd4_compound_state *cs = &resp->cstate;
3842
3843 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003844 if (cs->status != nfserr_replay_cache) {
3845 nfsd4_store_cache_entry(resp);
3846 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3847 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003848 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003849 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003850 } else if (cs->clp)
3851 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003852}
3853
Mi Jinlong345c2842011-10-20 17:51:39 +08003854__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003855nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3856 struct nfsd4_compound_state *cstate,
3857 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003858{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003859 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003860 struct nfs4_client *conf, *unconf;
3861 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003862 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003863 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003864
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003865 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003866 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003867 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003868 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003869
3870 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003871 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003872 status = nfserr_clientid_busy;
3873 goto out;
3874 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003875 status = mark_client_expired_locked(conf);
3876 if (status)
3877 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003878 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003879 } else if (unconf)
3880 clp = unconf;
3881 else {
3882 status = nfserr_stale_clientid;
3883 goto out;
3884 }
Andrew Elblededeb132016-06-15 12:52:08 -04003885 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003886 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003887 status = nfserr_wrong_cred;
3888 goto out;
3889 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003890 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003891out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003892 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003893 if (clp)
3894 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003895 return status;
3896}
3897
Andy Adamson069b6ad2009-04-03 08:27:58 +03003898__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003899nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3900 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003901{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003902 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003903 struct nfs4_client *clp = cstate->clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003904 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003905
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003906 if (rc->rca_one_fs) {
3907 if (!cstate->current_fh.fh_dentry)
3908 return nfserr_nofilehandle;
3909 /*
3910 * We don't take advantage of the rca_one_fs case.
3911 * That's OK, it's optional, we can safely ignore it.
3912 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003913 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003914 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003915
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003916 status = nfserr_complete_already;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003917 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003918 goto out;
3919
3920 status = nfserr_stale_clientid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003921 if (is_client_expired(clp))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003922 /*
3923 * The following error isn't really legal.
3924 * But we only get here if the client just explicitly
3925 * destroyed the client. Surely it no longer cares what
3926 * error it gets back on an operation for the dead
3927 * client.
3928 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003929 goto out;
3930
3931 status = nfs_ok;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003932 nfsd4_client_record_create(clp);
3933 inc_reclaim_complete(clp);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003934out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003935 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003936}
3937
3938__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003939nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003940 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003942 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003943 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003945 struct nfs4_client *conf, *new;
3946 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003947 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003948 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3949
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003950 new = create_client(clname, rqstp, &clverifier);
3951 if (new == NULL)
3952 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003953 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003954 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003955 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003956 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003957 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003959 if (clp_used_exchangeid(conf))
3960 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003961 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04003962 trace_nfsd_clid_inuse_err(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 goto out;
3964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003966 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003967 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003968 unhash_client_locked(unconf);
Chuck Lever45f56da2020-04-05 14:40:25 -04003969 /* We need to handle only case 1: probable callback update */
Kinglong Mee41eb1672015-07-13 17:29:41 +08003970 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003972 gen_confirm(new, nn);
Chuck Lever45f56da2020-04-05 14:40:25 -04003973 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003974 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003975 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003976 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3978 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3979 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003980 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 status = nfs_ok;
3982out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003983 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003984 if (new)
3985 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003986 if (unconf)
3987 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 return status;
3989}
3990
3991
Al Virob37ad282006-10-19 23:28:59 -07003992__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003993nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003994 struct nfsd4_compound_state *cstate,
3995 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003997 struct nfsd4_setclientid_confirm *setclientid_confirm =
3998 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003999 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04004000 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4002 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07004003 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004004 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004006 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07004008
Jeff Laytond20c11d2014-07-30 08:27:07 -04004009 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004010 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004011 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004012 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04004013 * We try hard to give out unique clientid's, so if we get an
4014 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004015 * the client may be buggy; this should never happen.
4016 *
4017 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004018 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004019 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04004020 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
4021 goto out;
4022 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
4023 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04004024 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004025 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04004026 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4027 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04004029 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004030 status = nfserr_stale_clientid;
4031 goto out;
4032 }
4033 status = nfs_ok;
4034 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04004035 old = unconf;
4036 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004037 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004038 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04004039 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4040 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04004041 status = nfserr_clid_inuse;
4042 if (client_has_state(old)
4043 && !same_creds(&unconf->cl_cred,
4044 &old->cl_cred))
4045 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04004046 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04004047 if (status) {
4048 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04004049 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04004050 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004051 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04004052 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04004053 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07004054 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04004055 get_client_locked(conf);
4056 spin_unlock(&nn->client_lock);
4057 nfsd4_probe_callback(conf);
4058 spin_lock(&nn->client_lock);
4059 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04004061 spin_unlock(&nn->client_lock);
4062 if (old)
4063 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 return status;
4065}
4066
J. Bruce Fields32513b42011-10-13 16:00:16 -04004067static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068{
J. Bruce Fields32513b42011-10-13 16:00:16 -04004069 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4070}
4071
4072/* OPEN Share state helper functions */
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004073static void nfsd4_init_file(struct svc_fh *fh, unsigned int hashval,
Jeff Layton5b095e92014-10-23 08:01:02 -04004074 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04004075{
Trond Myklebust950e0112014-06-30 11:48:31 -04004076 lockdep_assert_held(&state_lock);
4077
Elena Reshetova818a34e2017-10-20 12:53:30 +03004078 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004079 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04004080 INIT_LIST_HEAD(&fp->fi_stateids);
4081 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004082 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004083 fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
Jeff Layton0c637be2014-08-22 12:05:43 -04004084 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004085 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004086 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004087 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4088 memset(fp->fi_access, 0, sizeof(fp->fi_access));
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004089 fp->fi_aliased = false;
4090 fp->fi_inode = d_inode(fh->fh_dentry);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004091#ifdef CONFIG_NFSD_PNFS
4092 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02004093 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004094#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04004095 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096}
4097
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004098void
NeilBrowne60d4392005-06-23 22:03:01 -07004099nfsd4_free_slabs(void)
4100{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004101 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004102 kmem_cache_destroy(openowner_slab);
4103 kmem_cache_destroy(lockowner_slab);
4104 kmem_cache_destroy(file_slab);
4105 kmem_cache_destroy(stateid_slab);
4106 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004107 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07004108}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Bryan Schumaker72083392011-11-01 15:24:59 -04004110int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111nfsd4_init_slabs(void)
4112{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004113 client_slab = kmem_cache_create("nfsd4_clients",
4114 sizeof(struct nfs4_client), 0, 0, NULL);
4115 if (client_slab == NULL)
4116 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004117 openowner_slab = kmem_cache_create("nfsd4_openowners",
4118 sizeof(struct nfs4_openowner), 0, 0, NULL);
4119 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04004120 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004121 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08004122 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004123 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004124 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07004125 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09004126 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07004127 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004128 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07004129 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004130 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07004131 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004132 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07004133 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09004134 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07004135 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004136 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004137 odstate_slab = kmem_cache_create("nfsd4_odstate",
4138 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4139 if (odstate_slab == NULL)
4140 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07004142
Sachin Bhamare8287f002015-04-27 14:50:14 +02004143out_free_deleg_slab:
4144 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004145out_free_stateid_slab:
4146 kmem_cache_destroy(stateid_slab);
4147out_free_file_slab:
4148 kmem_cache_destroy(file_slab);
4149out_free_lockowner_slab:
4150 kmem_cache_destroy(lockowner_slab);
4151out_free_openowner_slab:
4152 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004153out_free_client_slab:
4154 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004155out:
NeilBrowne60d4392005-06-23 22:03:01 -07004156 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157}
4158
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004159static void init_nfs4_replay(struct nfs4_replay *rp)
4160{
4161 rp->rp_status = nfserr_serverfault;
4162 rp->rp_buflen = 0;
4163 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04004164 mutex_init(&rp->rp_mutex);
4165}
4166
4167static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4168 struct nfs4_stateowner *so)
4169{
4170 if (!nfsd4_has_session(cstate)) {
4171 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004172 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004173 }
4174}
4175
4176void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4177{
4178 struct nfs4_stateowner *so = cstate->replay_owner;
4179
4180 if (so != NULL) {
4181 cstate->replay_owner = NULL;
4182 mutex_unlock(&so->so_replay.rp_mutex);
4183 nfs4_put_stateowner(so);
4184 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004185}
4186
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004187static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188{
4189 struct nfs4_stateowner *sop;
4190
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004191 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004192 if (!sop)
4193 return NULL;
4194
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04004195 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004196 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004197 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004198 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004200
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004201 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004202 sop->so_client = clp;
4203 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04004204 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004205 return sop;
4206}
4207
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004208static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004209{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004210 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004211
Trond Myklebustd4f04892014-07-29 21:34:36 -04004212 list_add(&oo->oo_owner.so_strhash,
4213 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004214 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215}
4216
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004217static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4218{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004219 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004220}
4221
Jeff Layton6b180f02014-07-29 21:34:26 -04004222static void nfs4_free_openowner(struct nfs4_stateowner *so)
4223{
4224 struct nfs4_openowner *oo = openowner(so);
4225
4226 kmem_cache_free(openowner_slab, oo);
4227}
4228
4229static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004230 .so_unhash = nfs4_unhash_openowner,
4231 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004232};
4233
Andrew Elble7fc05642015-11-05 20:42:43 -05004234static struct nfs4_ol_stateid *
4235nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4236{
4237 struct nfs4_ol_stateid *local, *ret = NULL;
4238 struct nfs4_openowner *oo = open->op_openowner;
4239
4240 lockdep_assert_held(&fp->fi_lock);
4241
4242 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4243 /* ignore lock owners */
4244 if (local->st_stateowner->so_is_open_owner == 0)
4245 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004246 if (local->st_stateowner != &oo->oo_owner)
4247 continue;
4248 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004249 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004250 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05004251 break;
4252 }
4253 }
4254 return ret;
4255}
4256
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004257static __be32
4258nfsd4_verify_open_stid(struct nfs4_stid *s)
4259{
4260 __be32 ret = nfs_ok;
4261
4262 switch (s->sc_type) {
4263 default:
4264 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004265 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004266 case NFS4_CLOSED_STID:
4267 case NFS4_CLOSED_DELEG_STID:
4268 ret = nfserr_bad_stateid;
4269 break;
4270 case NFS4_REVOKED_DELEG_STID:
4271 ret = nfserr_deleg_revoked;
4272 }
4273 return ret;
4274}
4275
4276/* Lock the stateid st_mutex, and deal with races with CLOSE */
4277static __be32
4278nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4279{
4280 __be32 ret;
4281
Andrew Elble4f34bd02017-11-08 17:29:51 -05004282 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004283 ret = nfsd4_verify_open_stid(&stp->st_stid);
4284 if (ret != nfs_ok)
4285 mutex_unlock(&stp->st_mutex);
4286 return ret;
4287}
4288
4289static struct nfs4_ol_stateid *
4290nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4291{
4292 struct nfs4_ol_stateid *stp;
4293 for (;;) {
4294 spin_lock(&fp->fi_lock);
4295 stp = nfsd4_find_existing_open(fp, open);
4296 spin_unlock(&fp->fi_lock);
4297 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4298 break;
4299 nfs4_put_stid(&stp->st_stid);
4300 }
4301 return stp;
4302}
4303
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004304static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004305alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004306 struct nfsd4_compound_state *cstate)
4307{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004308 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004309 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004311 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4312 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004314 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004315 oo->oo_owner.so_is_open_owner = 1;
4316 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004317 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004318 if (nfsd4_has_session(cstate))
4319 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004320 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004321 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004322 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004323 spin_lock(&clp->cl_lock);
4324 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004325 if (ret == NULL) {
4326 hash_openowner(oo, clp, strhashval);
4327 ret = oo;
4328 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004329 nfs4_free_stateowner(&oo->oo_owner);
4330
Trond Myklebustd4f04892014-07-29 21:34:36 -04004331 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004332 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333}
4334
Andrew Elble7fc05642015-11-05 20:42:43 -05004335static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004336init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004337{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
Andrew Elble7fc05642015-11-05 20:42:43 -05004339 struct nfs4_openowner *oo = open->op_openowner;
4340 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004341 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004342
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004343 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004344 /* We are moving these outside of the spinlocks to avoid the warnings */
4345 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004346 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004347
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004348retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004349 spin_lock(&oo->oo_owner.so_client->cl_lock);
4350 spin_lock(&fp->fi_lock);
4351
4352 retstp = nfsd4_find_existing_open(fp, open);
4353 if (retstp)
4354 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004355
4356 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004357 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004358 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004359 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004360 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004361 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004362 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 stp->st_access_bmap = 0;
4364 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004365 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004366 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004367 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004368
4369out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004370 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004371 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004372 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004373 /* Handle races with CLOSE */
4374 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4375 nfs4_put_stid(&retstp->st_stid);
4376 goto retry;
4377 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004378 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004379 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004380 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004381 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004382 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383}
4384
Jeff Laytond3134b12014-07-29 21:34:32 -04004385/*
4386 * In the 4.0 case we need to keep the owners around a little while to handle
4387 * CLOSE replay. We still do need to release any file access that is held by
4388 * them before returning however.
4389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004391move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392{
Jeff Layton217526e2014-07-30 08:27:11 -04004393 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004394 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4395 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4396 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004397
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004398 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
Jeff Laytonb401be222014-07-29 21:34:33 -04004400 /*
4401 * We know that we hold one reference via nfsd4_close, and another
4402 * "persistent" reference for the client. If the refcount is higher
4403 * than 2, then there are still calls in progress that are using this
4404 * stateid. We can't put the sc_file reference until they are finished.
4405 * Wait for the refcount to drop to 2. Since it has been unhashed,
4406 * there should be no danger of the refcount going back up again at
4407 * this point.
4408 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004409 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004410
Jeff Laytond3134b12014-07-29 21:34:32 -04004411 release_all_access(s);
4412 if (s->st_stid.sc_file) {
4413 put_nfs4_file(s->st_stid.sc_file);
4414 s->st_stid.sc_file = NULL;
4415 }
Jeff Layton217526e2014-07-30 08:27:11 -04004416
4417 spin_lock(&nn->client_lock);
4418 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004419 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004420 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004421 oo->oo_time = ktime_get_boottime_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004422 spin_unlock(&nn->client_lock);
4423 if (last)
4424 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
4426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427/* search file_hashtbl[] for file */
4428static struct nfs4_file *
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004429find_file_locked(struct svc_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 struct nfs4_file *fp;
4432
Madhuparna Bhowmik36a80492020-02-13 20:33:31 +05304433 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4434 lockdep_is_held(&state_lock)) {
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004435 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004436 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004437 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 }
4440 return NULL;
4441}
4442
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004443static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh,
4444 unsigned int hashval)
4445{
4446 struct nfs4_file *fp;
4447 struct nfs4_file *ret = NULL;
4448 bool alias_found = false;
4449
4450 spin_lock(&state_lock);
4451 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4452 lockdep_is_held(&state_lock)) {
4453 if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) {
4454 if (refcount_inc_not_zero(&fp->fi_ref))
4455 ret = fp;
4456 } else if (d_inode(fh->fh_dentry) == fp->fi_inode)
4457 fp->fi_aliased = alias_found = true;
4458 }
4459 if (likely(ret == NULL)) {
4460 nfsd4_init_file(fh, hashval, new);
4461 new->fi_aliased = alias_found;
4462 ret = new;
4463 }
4464 spin_unlock(&state_lock);
4465 return ret;
4466}
4467
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004468static struct nfs4_file * find_file(struct svc_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004469{
4470 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004471 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004472
Jeff Layton5b095e92014-10-23 08:01:02 -04004473 rcu_read_lock();
4474 fp = find_file_locked(fh, hashval);
4475 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004476 return fp;
4477}
4478
4479static struct nfs4_file *
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004480find_or_add_file(struct nfs4_file *new, struct svc_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004481{
4482 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004483 unsigned int hashval = file_hashval(fh);
4484
4485 rcu_read_lock();
4486 fp = find_file_locked(fh, hashval);
4487 rcu_read_unlock();
4488 if (fp)
4489 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004490
J. Bruce Fieldsa0ce4832021-04-16 14:00:16 -04004491 return insert_file(new, fh, hashval);
Trond Myklebust950e0112014-06-30 11:48:31 -04004492}
4493
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004494/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 * Called to check deny when READ with all zero stateid or
4496 * WRITE with all zero or all one stateid
4497 */
Al Virob37ad282006-10-19 23:28:59 -07004498static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004502 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04004504 fp = find_file(current_fh);
NeilBrown13cd2182005-06-23 22:03:10 -07004505 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004506 return ret;
4507 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004508 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004509 if (fp->fi_share_deny & deny_type)
4510 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004511 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004512 put_nfs4_file(fp);
4513 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514}
4515
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004516static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004518 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004519 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4520 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004521
Trond Myklebust11b91642014-07-29 21:34:08 -04004522 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004523
Jeff Layton02e12152014-07-16 10:31:57 -04004524 /*
4525 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004526 * already holding inode->i_lock.
4527 *
Jeff Laytondff13992014-07-08 14:02:49 -04004528 * If the dl_time != 0, then we know that it has already been
4529 * queued for a lease break. Don't queue it again.
4530 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004531 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004532 if (dp->dl_time == 0) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004533 dp->dl_time = ktime_get_boottime_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004534 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004535 }
Jeff Layton02e12152014-07-16 10:31:57 -04004536 spin_unlock(&state_lock);
4537}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004539static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4540 struct rpc_task *task)
4541{
4542 struct nfs4_delegation *dp = cb_to_delegation(cb);
4543
J. Bruce Fields12ed22f2020-07-31 16:20:05 -04004544 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4545 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
Andrew Elblea4579742015-08-31 12:06:41 -04004546 return 1;
4547
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004548 switch (task->tk_status) {
4549 case 0:
4550 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004551 case -NFS4ERR_DELAY:
4552 rpc_delay(task, 2 * HZ);
4553 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004554 case -EBADHANDLE:
4555 case -NFS4ERR_BAD_STATEID:
4556 /*
4557 * Race: client probably got cb_recall before open reply
4558 * granting delegation.
4559 */
4560 if (dp->dl_retries--) {
4561 rpc_delay(task, 2 * HZ);
4562 return 0;
4563 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004564 fallthrough;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004565 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004566 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004567 }
4568}
4569
4570static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4571{
4572 struct nfs4_delegation *dp = cb_to_delegation(cb);
4573
4574 nfs4_put_stid(&dp->dl_stid);
4575}
4576
Julia Lawallc4cb8972015-11-21 22:57:39 +01004577static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004578 .prepare = nfsd4_cb_recall_prepare,
4579 .done = nfsd4_cb_recall_done,
4580 .release = nfsd4_cb_recall_release,
4581};
4582
Jeff Layton02e12152014-07-16 10:31:57 -04004583static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4584{
4585 /*
4586 * We're assuming the state code never drops its reference
4587 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004588 * callback (and since the lease code is serialized by the
4589 * i_lock) we know the server hasn't removed the lease yet, and
4590 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004591 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004592 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004593 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004594}
4595
Jeff Layton1c8c6012013-06-21 08:58:15 -04004596/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004597static bool
4598nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004599{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004600 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004601 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4602 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004603
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04004604 trace_nfsd_deleg_break(&dp->dl_stid.sc_stateid);
4605
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004606 /*
4607 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004608 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004609 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004610 */
4611 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Jeff Layton02e12152014-07-16 10:31:57 -04004613 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004614 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004615 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004616 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004617 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618}
4619
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004620static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4621{
4622 struct nfs4_delegation *dl = fl->fl_owner;
4623 struct svc_rqst *rqst;
4624 struct nfs4_client *clp;
4625
4626 if (!i_am_nfsd())
4627 return NULL;
4628 rqst = kthread_data(current);
J. Bruce Fields13956162020-09-25 10:12:39 -04004629 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4630 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
J. Bruce Fields34b09af2020-08-05 15:10:11 -04004631 return NULL;
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004632 clp = *(rqst->rq_lease_breaker);
4633 return dl->dl_stid.sc_client == clp;
4634}
4635
Jeff Laytonc45198e2014-09-01 07:12:07 -04004636static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004637nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4638 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639{
4640 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004641 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 else
4643 return -EAGAIN;
4644}
4645
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004646static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004647 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004648 .lm_break = nfsd_break_deleg_cb,
4649 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650};
4651
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004652static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4653{
4654 if (nfsd4_has_session(cstate))
4655 return nfs_ok;
4656 if (seqid == so->so_seqid - 1)
4657 return nfserr_replay_me;
4658 if (seqid == so->so_seqid)
4659 return nfs_ok;
4660 return nfserr_bad_seqid;
4661}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
J. Bruce Fields7950b532021-01-21 17:57:41 -05004663static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
4664 struct nfsd_net *nn)
4665{
4666 struct nfs4_client *found;
4667
4668 spin_lock(&nn->client_lock);
4669 found = find_confirmed_client(clid, sessions, nn);
4670 if (found)
4671 atomic_inc(&found->cl_rpc_users);
4672 spin_unlock(&nn->client_lock);
4673 return found;
4674}
4675
J. Bruce Fields460d2702021-01-21 17:57:40 -05004676static __be32 set_client(clientid_t *clid,
Jeff Layton4b24ca72014-06-30 11:48:44 -04004677 struct nfsd4_compound_state *cstate,
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004678 struct nfsd_net *nn)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004679{
Jeff Layton4b24ca72014-06-30 11:48:44 -04004680 if (cstate->clp) {
J. Bruce Fields7950b532021-01-21 17:57:41 -05004681 if (!same_clid(&cstate->clp->cl_clientid, clid))
Jeff Layton4b24ca72014-06-30 11:48:44 -04004682 return nfserr_stale_clientid;
4683 return nfs_ok;
4684 }
Jeff Layton4b24ca72014-06-30 11:48:44 -04004685 if (STALE_CLIENTID(clid, nn))
4686 return nfserr_stale_clientid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004687 /*
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004688 * We're in the 4.0 case (otherwise the SEQUENCE op would have
4689 * set cstate->clp), so session = false:
Jeff Layton4b24ca72014-06-30 11:48:44 -04004690 */
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004691 cstate->clp = lookup_clientid(clid, false, nn);
J. Bruce Fields7950b532021-01-21 17:57:41 -05004692 if (!cstate->clp)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004693 return nfserr_expired;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004694 return nfs_ok;
4695}
4696
Al Virob37ad282006-10-19 23:28:59 -07004697__be32
Andy Adamson66689582009-04-03 08:28:45 +03004698nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004699 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 clientid_t *clientid = &open->op_clientid;
4702 struct nfs4_client *clp = NULL;
4703 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004704 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004705 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
J. Bruce Fields32513b42011-10-13 16:00:16 -04004707 /*
4708 * In case we need it later, after we've already created the
4709 * file and don't want to risk a further failure:
4710 */
4711 open->op_file = nfsd4_alloc_file();
4712 if (open->op_file == NULL)
4713 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004715 status = set_client(clientid, cstate, nn);
Trond Myklebust2d91e892014-06-30 11:48:46 -04004716 if (status)
4717 return status;
4718 clp = cstate->clp;
4719
Trond Myklebustd4f04892014-07-29 21:34:36 -04004720 strhashval = ownerstr_hashval(&open->op_owner);
4721 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004722 open->op_openowner = oo;
4723 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004724 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004726 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004727 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004728 release_openowner(oo);
4729 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004730 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004731 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004732 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4733 if (status)
4734 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004735 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004736new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004737 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004738 if (oo == NULL)
4739 return nfserr_jukebox;
4740 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004741alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004742 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004743 if (!open->op_stp)
4744 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004745
4746 if (nfsd4_has_session(cstate) &&
4747 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4748 open->op_odstate = alloc_clnt_odstate(clp);
4749 if (!open->op_odstate)
4750 return nfserr_jukebox;
4751 }
4752
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004753 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754}
4755
Al Virob37ad282006-10-19 23:28:59 -07004756static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004757nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4758{
4759 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4760 return nfserr_openmode;
4761 else
4762 return nfs_ok;
4763}
4764
Daniel Mackc47d8322011-05-16 16:38:14 +02004765static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004766{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004767 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4768}
4769
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004770static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004771{
4772 struct nfs4_stid *ret;
4773
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004774 ret = find_stateid_by_type(cl, s,
4775 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004776 if (!ret)
4777 return NULL;
4778 return delegstateid(ret);
4779}
4780
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004781static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4782{
4783 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4784 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4785}
4786
Al Virob37ad282006-10-19 23:28:59 -07004787static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004788nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004789 struct nfs4_delegation **dp)
4790{
4791 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004792 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004793 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004794
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004795 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4796 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004797 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004798 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4799 nfs4_put_stid(&deleg->dl_stid);
4800 if (cl->cl_minorversion)
4801 status = nfserr_deleg_revoked;
4802 goto out;
4803 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004804 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004805 status = nfs4_check_delegmode(deleg, flags);
4806 if (status) {
4807 nfs4_put_stid(&deleg->dl_stid);
4808 goto out;
4809 }
4810 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004811out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004812 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004813 return nfs_ok;
4814 if (status)
4815 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004816 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004817 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004818}
4819
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004820static inline int nfs4_access_to_access(u32 nfs4_access)
4821{
4822 int flags = 0;
4823
4824 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4825 flags |= NFSD_MAY_READ;
4826 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4827 flags |= NFSD_MAY_WRITE;
4828 return flags;
4829}
4830
Al Virob37ad282006-10-19 23:28:59 -07004831static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4833 struct nfsd4_open *open)
4834{
4835 struct iattr iattr = {
4836 .ia_valid = ATTR_SIZE,
4837 .ia_size = 0,
4838 };
4839 if (!open->op_truncate)
4840 return 0;
4841 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004842 return nfserr_inval;
Arnd Bergmann2a1aa482019-11-03 17:50:18 +01004843 return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844}
4845
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004846static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004847 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4848 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004849{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004850 struct nfsd_file *nf = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004851 __be32 status;
4852 int oflag = nfs4_access_to_omode(open->op_share_access);
4853 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004854 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004855
Trond Myklebustde186432014-07-10 14:07:26 -04004856 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004857
4858 /*
4859 * Are we trying to set a deny mode that would conflict with
4860 * current access?
4861 */
4862 status = nfs4_file_check_deny(fp, open->op_share_deny);
4863 if (status != nfs_ok) {
4864 spin_unlock(&fp->fi_lock);
4865 goto out;
4866 }
4867
4868 /* set access to the file */
4869 status = nfs4_file_get_access(fp, open->op_share_access);
4870 if (status != nfs_ok) {
4871 spin_unlock(&fp->fi_lock);
4872 goto out;
4873 }
4874
4875 /* Set access bits in stateid */
4876 old_access_bmap = stp->st_access_bmap;
4877 set_access(open->op_share_access, stp);
4878
4879 /* Set new deny mask */
4880 old_deny_bmap = stp->st_deny_bmap;
4881 set_deny(open->op_share_deny, stp);
4882 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4883
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004884 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004885 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004886 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004887 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004888 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004889 spin_lock(&fp->fi_lock);
4890 if (!fp->fi_fds[oflag]) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004891 fp->fi_fds[oflag] = nf;
4892 nf = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -04004893 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004894 }
Trond Myklebustde186432014-07-10 14:07:26 -04004895 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004896 if (nf)
4897 nfsd_file_put(nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004898
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04004899 status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
4900 access));
4901 if (status)
4902 goto out_put_access;
4903
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004904 status = nfsd4_truncate(rqstp, cur_fh, open);
4905 if (status)
4906 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004907out:
4908 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004909out_put_access:
4910 stp->st_access_bmap = old_access_bmap;
4911 nfs4_file_put_access(fp, open->op_share_access);
4912 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4913 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004914}
4915
Al Virob37ad282006-10-19 23:28:59 -07004916static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004917nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918{
Al Virob37ad282006-10-19 23:28:59 -07004919 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004920 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004922 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004923 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004924
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004925 /* test and set deny mode */
4926 spin_lock(&fp->fi_lock);
4927 status = nfs4_file_check_deny(fp, open->op_share_deny);
4928 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004929 set_deny(open->op_share_deny, stp);
4930 fp->fi_share_deny |=
4931 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4932 }
4933 spin_unlock(&fp->fi_lock);
4934
4935 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004938 status = nfsd4_truncate(rqstp, cur_fh, open);
4939 if (status != nfs_ok)
4940 reset_union_bmap_deny(old_deny_bmap, stp);
4941 return status;
4942}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004944/* Should we give out recallable state?: */
4945static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4946{
4947 if (clp->cl_cb_state == NFSD4_CB_UP)
4948 return true;
4949 /*
4950 * In the sessions case, since we don't have to establish a
4951 * separate connection for callbacks, we assume it's OK
4952 * until we hear otherwise:
4953 */
4954 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4955}
4956
J. Bruce Fields653e5142018-02-15 22:08:45 -05004957static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4958 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004959{
4960 struct file_lock *fl;
4961
4962 fl = locks_alloc_lock();
4963 if (!fl)
4964 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004965 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004966 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004967 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4968 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004969 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004970 fl->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004971 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004972 return fl;
4973}
4974
Jeff Layton0b266932014-07-25 07:34:25 -04004975static struct nfs4_delegation *
4976nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004977 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004978{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004979 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004980 struct nfs4_delegation *dp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004981 struct nfsd_file *nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004982 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004983
J. Bruce Fields353601e2018-02-16 14:29:42 -05004984 /*
4985 * The fi_had_conflict and nfs_get_existing_delegation checks
4986 * here are just optimizations; we'll need to recheck them at
4987 * the end:
4988 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004989 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004990 return ERR_PTR(-EAGAIN);
4991
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004992 nf = find_readable_file(fp);
4993 if (!nf) {
J. Bruce Fields6ee65a772021-03-08 10:52:29 -05004994 /* We should always have a readable file here */
4995 WARN_ON_ONCE(1);
4996 return ERR_PTR(-EBADF);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004997 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004998 spin_lock(&state_lock);
4999 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05005000 if (nfs4_delegation_exists(clp, fp))
5001 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005002 else if (!fp->fi_deleg_file) {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005003 fp->fi_deleg_file = nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005004 /* increment early to prevent fi_deleg_file from being
5005 * cleared */
5006 fp->fi_delegees = 1;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005007 nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005008 } else
5009 fp->fi_delegees++;
5010 spin_unlock(&fp->fi_lock);
5011 spin_unlock(&state_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005012 if (nf)
5013 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005014 if (status)
5015 return ERR_PTR(status);
5016
5017 status = -ENOMEM;
5018 dp = alloc_init_deleg(clp, fp, fh, odstate);
5019 if (!dp)
5020 goto out_delegees;
5021
5022 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5023 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04005024 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005025
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005026 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005027 if (fl)
5028 locks_free_lock(fl);
5029 if (status)
5030 goto out_clnt_odstate;
5031
5032 spin_lock(&state_lock);
5033 spin_lock(&fp->fi_lock);
5034 if (fp->fi_had_conflict)
5035 status = -EAGAIN;
5036 else
5037 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04005038 spin_unlock(&fp->fi_lock);
5039 spin_unlock(&state_lock);
5040
5041 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04005042 goto out_unlock;
5043
Jeff Layton0b266932014-07-25 07:34:25 -04005044 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04005045out_unlock:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005046 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005047out_clnt_odstate:
5048 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005049 nfs4_put_stid(&dp->dl_stid);
5050out_delegees:
5051 put_deleg_file(fp);
5052 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05005053}
5054
Benny Halevy4aa89132012-02-21 14:16:44 -08005055static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5056{
5057 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5058 if (status == -EAGAIN)
5059 open->op_why_no_deleg = WND4_CONTENTION;
5060 else {
5061 open->op_why_no_deleg = WND4_RESOURCE;
5062 switch (open->op_deleg_want) {
5063 case NFS4_SHARE_WANT_READ_DELEG:
5064 case NFS4_SHARE_WANT_WRITE_DELEG:
5065 case NFS4_SHARE_WANT_ANY_DELEG:
5066 break;
5067 case NFS4_SHARE_WANT_CANCEL:
5068 open->op_why_no_deleg = WND4_CANCELLED;
5069 break;
5070 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005071 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08005072 }
5073 }
5074}
5075
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076/*
5077 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04005078 *
5079 * Note we don't support write delegations, and won't until the vfs has
5080 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 */
5082static void
Jeff Layton4cf59222014-07-25 07:34:24 -04005083nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
5084 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085{
5086 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04005087 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5088 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05005089 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005090 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005092 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07005093 open->op_recall = 0;
5094 switch (open->op_claim_type) {
5095 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05005096 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07005097 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005098 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5099 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005100 break;
5101 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00005102 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005103 /*
5104 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005105 * had the chance to reclaim theirs, *and* until
5106 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005107 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005108 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005109 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005110 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005111 goto out_no_deleg;
J. Bruce Fields6ee65a772021-03-08 10:52:29 -05005112 /*
5113 * Also, if the file was opened for write or
5114 * create, there's a good chance the client's
5115 * about to write to it, resulting in an
5116 * immediate recall (since we don't support
5117 * write delegations):
5118 */
5119 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
5120 goto out_no_deleg;
5121 if (open->op_create == NFS4_OPEN_CREATE)
5122 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005123 break;
5124 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005125 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005126 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005127 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04005128 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005129 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005131 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
Hou Tao3caf9172020-08-28 15:02:55 +08005133 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
J. Bruce Fields99c41512013-05-21 16:21:25 -04005134 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04005135 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005136 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005137out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005138 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5139 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005140 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04005141 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005142 open->op_recall = 1;
5143 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04005144
5145 /* 4.1 client asking for a delegation? */
5146 if (open->op_deleg_want)
5147 nfsd4_open_deleg_none_ext(open, status);
5148 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149}
5150
Benny Halevye27f49c2012-02-21 14:16:54 -08005151static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5152 struct nfs4_delegation *dp)
5153{
5154 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5155 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5156 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5157 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5158 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5159 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5160 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5161 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5162 }
5163 /* Otherwise the client must be confused wanting a delegation
5164 * it already has, therefore we don't return
5165 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5166 */
5167}
5168
Al Virob37ad282006-10-19 23:28:59 -07005169__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5171{
Andy Adamson66689582009-04-03 08:28:45 +03005172 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005173 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005175 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07005176 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005177 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005178 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 /*
5181 * Lookup file; if found, lookup stateid and check open request,
5182 * and check for delegations in the process of being recalled.
5183 * If not found, create the nfs4_file struct
5184 */
J. Bruce Fieldsf9b60e22021-04-16 14:00:15 -04005185 fp = find_or_add_file(open->op_file, current_fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04005186 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04005187 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07005188 if (status)
5189 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005190 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04005192 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07005193 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04005194 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07005195 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 }
5197
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005198 if (!stp) {
5199 stp = init_open_stateid(fp, open);
5200 if (!open->op_stp)
5201 new_stp = true;
5202 }
5203
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 /*
5205 * OPEN the file, or upgrade an existing OPEN.
5206 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005207 *
5208 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005210 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005212 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005213 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005214 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005218 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5219 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005220 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005221 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005222 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005223 goto out;
5224 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005225
5226 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5227 open->op_odstate);
5228 if (stp->st_clnt_odstate == open->op_odstate)
5229 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005231
Jeff Layton9767feb2015-10-01 09:05:50 -04005232 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005233 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234
Benny Halevyd24433c2012-02-16 20:57:17 +02005235 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005236 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5237 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5238 open->op_why_no_deleg = WND4_NOT_WANTED;
5239 goto nodeleg;
5240 }
5241 }
5242
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 /*
5244 * Attempt to hand out a delegation. No error return, because the
5245 * OPEN succeeds even if we fail.
5246 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005247 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005248nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 status = nfs_ok;
Hou Tao3caf9172020-08-28 15:02:55 +08005250 trace_nfsd_open(&stp->st_stid.sc_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005252 /* 4.1 client trying to upgrade/downgrade delegation? */
5253 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005254 open->op_deleg_want)
5255 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005256
NeilBrown13cd2182005-06-23 22:03:10 -07005257 if (fp)
5258 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005259 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005260 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 /*
5262 * To finish the open response, we just need to set the rflags.
5263 */
5264 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005265 if (nfsd4_has_session(&resp->cstate))
5266 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5267 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005269
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005270 if (dp)
5271 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005272 if (stp)
5273 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
5275 return status;
5276}
5277
Jeff Layton58fb12e2014-07-29 21:34:27 -04005278void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005279 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005280{
5281 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005282 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005283
Jeff Laytond3134b12014-07-29 21:34:32 -04005284 nfsd4_cstate_assign_replay(cstate, so);
5285 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005286 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005287 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005288 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005289 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005290 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005291 if (open->op_odstate)
5292 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005293}
5294
Al Virob37ad282006-10-19 23:28:59 -07005295__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005296nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005297 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005299 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005301 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005302 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005304 trace_nfsd_clid_renew(clid);
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005305 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005306 if (status)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005307 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005308 clp = cstate->clp;
NeilBrownea1da632005-06-23 22:04:17 -07005309 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005310 && clp->cl_cb_state != NFSD4_CB_UP)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005311 return nfserr_cb_path_down;
5312 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313}
5314
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005315void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005316nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005317{
Jeff Layton33dcc482012-04-10 11:08:48 -04005318 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005319 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005320 return;
5321
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005322 trace_nfsd_grace_complete(nn);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005323 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005324 /*
5325 * If the server goes down again right now, an NFSv4
5326 * client will still be allowed to reclaim after it comes back up,
5327 * even if it hasn't yet had a chance to reclaim state this time.
5328 *
5329 */
Jeff Layton919b8042014-09-12 16:40:20 -04005330 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005331 /*
5332 * At this point, NFSv4 clients can still reclaim. But if the
5333 * server crashes, any that have not yet reclaimed will be out
5334 * of luck on the next boot.
5335 *
5336 * (NFSv4.1+ clients are considered to have reclaimed once they
5337 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5338 * have reclaimed after their first OPEN.)
5339 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005340 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005341 /*
5342 * At this point, and once lockd and/or any other containers
5343 * exit their grace period, further reclaims will fail and
5344 * regular locking can resume.
5345 */
NeilBrowna76b4312005-06-23 22:04:01 -07005346}
5347
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005348/*
5349 * If we've waited a lease period but there are still clients trying to
5350 * reclaim, wait a little longer to give them a chance to finish.
5351 */
5352static bool clients_still_reclaiming(struct nfsd_net *nn)
5353{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005354 time64_t double_grace_period_end = nn->boot_time +
5355 2 * nn->nfsd4_lease;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005356
Scott Mayhew362063a2019-03-26 18:06:28 -04005357 if (nn->track_reclaim_completes &&
5358 atomic_read(&nn->nr_reclaim_complete) ==
5359 nn->reclaim_str_hashtbl_size)
5360 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005361 if (!nn->somebody_reclaimed)
5362 return false;
5363 nn->somebody_reclaimed = false;
5364 /*
5365 * If we've given them *two* lease times to reclaim, and they're
5366 * still not done, give up:
5367 */
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005368 if (ktime_get_boottime_seconds() > double_grace_period_end)
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005369 return false;
5370 return true;
5371}
5372
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005373struct laundry_time {
5374 time64_t cutoff;
5375 time64_t new_timeo;
5376};
5377
5378static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5379{
5380 time64_t time_remaining;
5381
5382 if (last_refresh < lt->cutoff)
5383 return true;
5384 time_remaining = last_refresh - lt->cutoff;
5385 lt->new_timeo = min(lt->new_timeo, time_remaining);
5386 return false;
5387}
5388
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005389static time64_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005390nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391{
5392 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005393 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005395 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005396 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 struct list_head *pos, *next, reaplist;
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005398 struct laundry_time lt = {
5399 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
5400 .new_timeo = nn->nfsd4_lease
5401 };
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005402 struct nfs4_cpntf_state *cps;
5403 copy_stateid_t *cps_t;
5404 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005406 if (clients_still_reclaiming(nn)) {
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005407 lt.new_timeo = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005408 goto out;
5409 }
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005410 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005411 INIT_LIST_HEAD(&reaplist);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005412
5413 spin_lock(&nn->s2s_cp_lock);
5414 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5415 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5416 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005417 state_expired(&lt, cps->cpntf_time))
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005418 _free_cpntf_state_locked(nn, cps);
5419 }
5420 spin_unlock(&nn->s2s_cp_lock);
5421
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005422 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005423 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 clp = list_entry(pos, struct nfs4_client, cl_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005425 if (!state_expired(&lt, clp->cl_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 break;
J. Bruce Fields221a6872013-04-01 22:23:49 -04005427 if (mark_client_expired_locked(clp)) {
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005428 trace_nfsd_clid_expired(&clp->cl_clientid);
Benny Halevyd7682982010-05-12 00:13:54 +03005429 continue;
5430 }
Trond Myklebust4864af92014-07-30 08:27:03 -04005431 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005432 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005433 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005434 list_for_each_safe(pos, next, &reaplist) {
5435 clp = list_entry(pos, struct nfs4_client, cl_lru);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005436 trace_nfsd_clid_purged(&clp->cl_clientid);
Trond Myklebust4864af92014-07-30 08:27:03 -04005437 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 expire_client(clp);
5439 }
Benny Halevycdc975052014-05-30 09:09:30 -04005440 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005441 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005443 if (!state_expired(&lt, dp->dl_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 break;
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005445 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005446 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 }
Benny Halevycdc975052014-05-30 09:09:30 -04005448 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005449 while (!list_empty(&reaplist)) {
5450 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5451 dl_recall_lru);
5452 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005453 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 }
Jeff Layton217526e2014-07-30 08:27:11 -04005455
5456 spin_lock(&nn->client_lock);
5457 while (!list_empty(&nn->close_lru)) {
5458 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5459 oo_close_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005460 if (!state_expired(&lt, oo->oo_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 break;
Jeff Layton217526e2014-07-30 08:27:11 -04005462 list_del_init(&oo->oo_close_lru);
5463 stp = oo->oo_last_closed_stid;
5464 oo->oo_last_closed_stid = NULL;
5465 spin_unlock(&nn->client_lock);
5466 nfs4_put_stid(&stp->st_stid);
5467 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 }
Jeff Layton217526e2014-07-30 08:27:11 -04005469 spin_unlock(&nn->client_lock);
5470
Jeff Layton7919d0a2016-09-16 16:28:25 -04005471 /*
5472 * It's possible for a client to try and acquire an already held lock
5473 * that is being held for a long time, and then lose interest in it.
5474 * So, we clean out any un-revisited request after a lease period
5475 * under the assumption that the client is no longer interested.
5476 *
5477 * RFC5661, sec. 9.6 states that the client must not rely on getting
5478 * notifications and must continue to poll for locks, even when the
5479 * server supports them. Thus this shouldn't lead to clients blocking
5480 * indefinitely once the lock does become free.
5481 */
5482 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005483 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005484 while (!list_empty(&nn->blocked_locks_lru)) {
5485 nbl = list_first_entry(&nn->blocked_locks_lru,
5486 struct nfsd4_blocked_lock, nbl_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005487 if (!state_expired(&lt, nbl->nbl_time))
Jeff Layton7919d0a2016-09-16 16:28:25 -04005488 break;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005489 list_move(&nbl->nbl_lru, &reaplist);
5490 list_del_init(&nbl->nbl_list);
5491 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005492 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005493
5494 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005495 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005496 struct nfsd4_blocked_lock, nbl_lru);
5497 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005498 free_blocked_lock(nbl);
5499 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005500out:
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005501 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502}
5503
Harvey Harrisona254b242008-02-20 12:49:00 -08005504static struct workqueue_struct *laundry_wq;
5505static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005506
5507static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005508laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005510 time64_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005511 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005512 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5513 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005515 t = nfs4_laundromat(nn);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005516 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517}
5518
Jeff Layton8fcd4612015-07-30 06:57:46 -04005519static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005520{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005521 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005522 return nfserr_bad_stateid;
5523 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524}
5525
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005527access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005529 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5530 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5531 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532}
5533
5534static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005535access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005537 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5538 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539}
5540
5541static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005542__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543{
Al Virob37ad282006-10-19 23:28:59 -07005544 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545
J. Bruce Fields02921912010-07-29 15:16:59 -04005546 /* For lock stateid's, we test the parent open, not the lock: */
5547 if (stp->st_openstp)
5548 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005549 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005551 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 goto out;
5553 status = nfs_ok;
5554out:
5555 return status;
5556}
5557
Al Virob37ad282006-10-19 23:28:59 -07005558static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005559check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005561 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005563 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005564 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 * conflicting state; so we must wait out the grace period. */
5566 return nfserr_grace;
5567 } else if (flags & WR_STATE)
5568 return nfs4_share_conflict(current_fh,
5569 NFS4_SHARE_DENY_WRITE);
5570 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5571 return nfs4_share_conflict(current_fh,
5572 NFS4_SHARE_DENY_READ);
5573}
5574
5575/*
5576 * Allow READ/WRITE during grace period on recovered state only for files
5577 * that are not able to provide mandatory locking.
5578 */
5579static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005580grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005582 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583}
5584
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005585static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005586{
Andy Adamson66689582009-04-03 08:28:45 +03005587 /*
5588 * When sessions are used the stateid generation number is ignored
5589 * when it is zero.
5590 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005591 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005592 return nfs_ok;
5593
5594 if (in->si_generation == ref->si_generation)
5595 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005596
J. Bruce Fields0836f582008-01-26 19:08:12 -05005597 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005598 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005599 return nfserr_bad_stateid;
5600 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005601 * However, we could see a stateid from the past, even from a
5602 * non-buggy client. For example, if the client sends a lock
5603 * while some IO is outstanding, the lock may bump si_generation
5604 * while the IO is still in flight. The client could avoid that
5605 * situation by waiting for responses on all the IO requests,
5606 * but better performance may result in retrying IO that
5607 * receives an old_stateid error if requests are rarely
5608 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005609 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005610 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005611}
5612
Trond Myklebust03da3162017-11-03 08:00:16 -04005613static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5614{
5615 __be32 ret;
5616
5617 spin_lock(&s->sc_lock);
5618 ret = nfsd4_verify_open_stid(s);
5619 if (ret == nfs_ok)
5620 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5621 spin_unlock(&s->sc_lock);
5622 return ret;
5623}
5624
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005625static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5626{
5627 if (ols->st_stateowner->so_is_open_owner &&
5628 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5629 return nfserr_bad_stateid;
5630 return nfs_ok;
5631}
5632
Chuck Lever7df302f2012-05-29 13:56:37 -04005633static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005634{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005635 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005636 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005637
Andrew Elbleae254dac2017-11-09 13:41:10 -05005638 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5639 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005640 return status;
J. Bruce Fields663e36f2020-03-19 10:18:49 -04005641 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005642 return status;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005643 spin_lock(&cl->cl_lock);
5644 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005645 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005646 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005647 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005648 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005649 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005650 switch (s->sc_type) {
5651 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005652 status = nfs_ok;
5653 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005654 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005655 status = nfserr_deleg_revoked;
5656 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005657 case NFS4_OPEN_STID:
5658 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005659 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005660 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005661 default:
5662 printk("unknown stateid type %x\n", s->sc_type);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05005663 fallthrough;
J. Bruce Fields23340032013-04-09 17:42:28 -04005664 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005665 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005666 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005667 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005668out_unlock:
5669 spin_unlock(&cl->cl_lock);
5670 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005671}
5672
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005673__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005674nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5675 stateid_t *stateid, unsigned char typemask,
5676 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005677{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005678 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005679 bool return_revoked = false;
5680
5681 /*
5682 * only return revoked delegations if explicitly asked.
5683 * otherwise we report revoked or bad_stateid status.
5684 */
5685 if (typemask & NFS4_REVOKED_DELEG_STID)
5686 return_revoked = true;
5687 else if (typemask & NFS4_DELEG_STID)
5688 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005689
Andrew Elbleae254dac2017-11-09 13:41:10 -05005690 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5691 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005692 return nfserr_bad_stateid;
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005693 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005694 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005695 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005696 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005697 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005698 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005699 if (status)
5700 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005701 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005702 if (!*s)
5703 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005704 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5705 nfs4_put_stid(*s);
5706 if (cstate->minorversion)
5707 return nfserr_deleg_revoked;
5708 return nfserr_bad_stateid;
5709 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005710 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005711}
5712
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005713static struct nfsd_file *
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005714nfs4_find_file(struct nfs4_stid *s, int flags)
5715{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005716 if (!s)
5717 return NULL;
5718
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005719 switch (s->sc_type) {
5720 case NFS4_DELEG_STID:
5721 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5722 return NULL;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005723 return nfsd_file_get(s->sc_file->fi_deleg_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005724 case NFS4_OPEN_STID:
5725 case NFS4_LOCK_STID:
5726 if (flags & RD_STATE)
5727 return find_readable_file(s->sc_file);
5728 else
5729 return find_writeable_file(s->sc_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005730 }
5731
5732 return NULL;
5733}
5734
5735static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005736nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005737{
5738 __be32 status;
5739
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005740 status = nfsd4_check_openowner_confirmed(ols);
5741 if (status)
5742 return status;
5743 return nfs4_check_openmode(ols, flags);
5744}
5745
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005746static __be32
5747nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
Jeff Layton5c4583b2019-08-18 14:18:54 -04005748 struct nfsd_file **nfp, int flags)
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005749{
5750 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005751 struct nfsd_file *nf;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005752 __be32 status;
5753
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005754 nf = nfs4_find_file(s, flags);
5755 if (nf) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005756 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5757 acc | NFSD_MAY_OWNER_OVERRIDE);
Jeff Layton5c4583b2019-08-18 14:18:54 -04005758 if (status) {
5759 nfsd_file_put(nf);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005760 goto out;
Jeff Layton5c4583b2019-08-18 14:18:54 -04005761 }
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005762 } else {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005763 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005764 if (status)
5765 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005766 }
Jeff Layton5c4583b2019-08-18 14:18:54 -04005767 *nfp = nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005768out:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005769 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005770}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005771static void
5772_free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5773{
5774 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5775 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5776 return;
5777 list_del(&cps->cp_list);
5778 idr_remove(&nn->s2s_cp_stateids,
5779 cps->cp_stateid.stid.si_opaque.so_id);
5780 kfree(cps);
5781}
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005782/*
5783 * A READ from an inter server to server COPY will have a
5784 * copy stateid. Look up the copy notify stateid from the
5785 * idr structure and take a reference on it.
5786 */
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005787__be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5788 struct nfs4_client *clp,
5789 struct nfs4_cpntf_state **cps)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005790{
5791 copy_stateid_t *cps_t;
5792 struct nfs4_cpntf_state *state = NULL;
5793
5794 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5795 return nfserr_bad_stateid;
5796 spin_lock(&nn->s2s_cp_lock);
5797 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5798 if (cps_t) {
5799 state = container_of(cps_t, struct nfs4_cpntf_state,
5800 cp_stateid);
Dan Carpenter5277a792019-12-04 10:59:36 +03005801 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5802 state = NULL;
5803 goto unlock;
5804 }
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005805 if (!clp)
5806 refcount_inc(&state->cp_stateid.sc_count);
5807 else
5808 _free_cpntf_state_locked(nn, state);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005809 }
Dan Carpenter5277a792019-12-04 10:59:36 +03005810unlock:
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005811 spin_unlock(&nn->s2s_cp_lock);
5812 if (!state)
5813 return nfserr_bad_stateid;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005814 if (!clp && state)
5815 *cps = state;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005816 return 0;
5817}
5818
5819static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5820 struct nfs4_stid **stid)
5821{
5822 __be32 status;
5823 struct nfs4_cpntf_state *cps = NULL;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005824 struct nfs4_client *found;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005825
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005826 status = manage_cpntf_state(nn, st, NULL, &cps);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005827 if (status)
5828 return status;
5829
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005830 cps->cpntf_time = ktime_get_boottime_seconds();
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005831
5832 status = nfserr_expired;
5833 found = lookup_clientid(&cps->cp_p_clid, true, nn);
5834 if (!found)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005835 goto out;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005836
5837 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
5838 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
5839 if (*stid)
5840 status = nfs_ok;
5841 else
5842 status = nfserr_bad_stateid;
5843
5844 put_client_renew(found);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005845out:
5846 nfs4_put_cpntf_state(nn, cps);
5847 return status;
5848}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005849
5850void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5851{
5852 spin_lock(&nn->s2s_cp_lock);
5853 _free_cpntf_state_locked(nn, cps);
5854 spin_unlock(&nn->s2s_cp_lock);
5855}
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005856
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005858 * Checks for stateid operations
5859 */
Al Virob37ad282006-10-19 23:28:59 -07005860__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005861nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005862 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005863 stateid_t *stateid, int flags, struct nfsd_file **nfp,
5864 struct nfs4_stid **cstid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005866 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005867 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005868 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005869 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005870 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
Jeff Layton5c4583b2019-08-18 14:18:54 -04005872 if (nfp)
5873 *nfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005875 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 return nfserr_grace;
5877
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005878 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5879 status = check_special_stateids(net, fhp, stateid, flags);
5880 goto done;
5881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005883 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005884 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005885 &s, nn);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005886 if (status == nfserr_bad_stateid)
5887 status = find_cpntf_state(nn, stateid, &s);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005888 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005889 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005890 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005891 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005892 if (status)
5893 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005894
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005895 switch (s->sc_type) {
5896 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005897 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005898 break;
5899 case NFS4_OPEN_STID:
5900 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005901 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005902 break;
5903 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005904 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005905 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005907 if (status)
5908 goto out;
5909 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005910
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005911done:
Jeff Layton5c4583b2019-08-18 14:18:54 -04005912 if (status == nfs_ok && nfp)
5913 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914out:
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005915 if (s) {
5916 if (!status && cstid)
5917 *cstid = s;
5918 else
5919 nfs4_put_stid(s);
5920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 return status;
5922}
5923
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005924/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005925 * Test if the stateid is valid
5926 */
5927__be32
5928nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005929 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005930{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005931 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005932 struct nfsd4_test_stateid_id *stateid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05005933 struct nfs4_client *cl = cstate->clp;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005934
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005935 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005936 stateid->ts_id_status =
5937 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005938
Bryan Schumaker17456802011-07-13 10:50:48 -04005939 return nfs_ok;
5940}
5941
Chuck Lever42691392016-08-11 10:37:30 -04005942static __be32
5943nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5944{
5945 struct nfs4_ol_stateid *stp = openlockstateid(s);
5946 __be32 ret;
5947
Trond Myklebust659aefb2017-11-03 08:00:13 -04005948 ret = nfsd4_lock_ol_stateid(stp);
5949 if (ret)
5950 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005951
5952 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5953 if (ret)
5954 goto out;
5955
5956 ret = nfserr_locks_held;
5957 if (check_for_locks(stp->st_stid.sc_file,
5958 lockowner(stp->st_stateowner)))
5959 goto out;
5960
5961 release_lock_stateid(stp);
5962 ret = nfs_ok;
5963
5964out:
5965 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005966out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005967 nfs4_put_stid(s);
5968 return ret;
5969}
5970
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005971__be32
5972nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005973 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005974{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005975 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005976 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005977 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005978 struct nfs4_delegation *dp;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05005979 struct nfs4_client *cl = cstate->clp;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005980 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005981
Jeff Layton1af71cc2014-07-29 21:34:14 -04005982 spin_lock(&cl->cl_lock);
5983 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005984 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005985 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005986 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005987 switch (s->sc_type) {
5988 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005989 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005990 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005991 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005992 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5993 if (ret)
5994 break;
5995 ret = nfserr_locks_held;
5996 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005997 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005998 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005999 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006000 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04006001 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006002 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006003 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04006004 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006005 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04006006 list_del_init(&dp->dl_recall_lru);
6007 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04006008 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006009 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04006010 goto out;
6011 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006012 }
Trond Myklebust03da3162017-11-03 08:00:16 -04006013 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04006014out_unlock:
6015 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006016out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04006017 return ret;
6018}
6019
NeilBrown4c4cd222005-07-07 17:59:27 -07006020static inline int
6021setlkflg (int type)
6022{
6023 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6024 RD_STATE : WR_STATE;
6025}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006027static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006028{
6029 struct svc_fh *current_fh = &cstate->current_fh;
6030 struct nfs4_stateowner *sop = stp->st_stateowner;
6031 __be32 status;
6032
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006033 status = nfsd4_check_seqid(cstate, sop, seqid);
6034 if (status)
6035 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04006036 status = nfsd4_lock_ol_stateid(stp);
6037 if (status != nfs_ok)
6038 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006039 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04006040 if (status == nfs_ok)
6041 status = nfs4_check_fh(current_fh, &stp->st_stid);
6042 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006043 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04006044 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006045}
6046
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047/*
6048 * Checks for sequence id mutating operations.
6049 */
Al Virob37ad282006-10-19 23:28:59 -07006050static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03006051nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04006052 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006053 struct nfs4_ol_stateid **stpp,
6054 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055{
J. Bruce Fields0836f582008-01-26 19:08:12 -05006056 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006057 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006058 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006060 trace_nfsd_preprocess(seqid, stateid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006061
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006063 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006064 if (status)
6065 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006066 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04006067 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006069 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006070 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006071 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04006072 else
6073 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006074 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006075}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05006076
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006077static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6078 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006079{
6080 __be32 status;
6081 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006082 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006084 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006085 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04006086 if (status)
6087 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006088 oo = openowner(stp->st_stateowner);
6089 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006090 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006091 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006092 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006093 }
6094 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07006095 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096}
6097
Al Virob37ad282006-10-19 23:28:59 -07006098__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006099nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006100 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006102 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07006103 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006104 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006105 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006106 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
Al Viroa6a9f182013-09-16 10:57:01 -04006108 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6109 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006111 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07006112 if (status)
6113 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006115 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006116 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006117 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006118 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006119 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006120 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006121 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006122 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006123 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04006124 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006125 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04006126 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04006127 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006128 mutex_unlock(&stp->st_mutex);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006129 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
Jeff Layton2a4317c2012-03-21 16:42:43 -04006130 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006131 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04006132put_stateid:
6133 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006135 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 return status;
6137}
6138
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006139static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140{
Jeff Layton82c5ff12012-05-11 09:45:13 -04006141 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006142 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04006143 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006144 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006145}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04006146
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006147static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6148{
6149 switch (to_access) {
6150 case NFS4_SHARE_ACCESS_READ:
6151 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6152 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6153 break;
6154 case NFS4_SHARE_ACCESS_WRITE:
6155 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6156 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6157 break;
6158 case NFS4_SHARE_ACCESS_BOTH:
6159 break;
6160 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006161 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 }
6163}
6164
Al Virob37ad282006-10-19 23:28:59 -07006165__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006166nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006167 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006169 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07006170 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006171 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006172 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173
Al Viroa6a9f182013-09-16 10:57:01 -04006174 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6175 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04006177 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02006178 if (od->od_deleg_want)
6179 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6180 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006182 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006183 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006184 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04006187 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006188 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006190 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04006192 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006193 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006195 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006197 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04006198 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04006199 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006201put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006202 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006203 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006205 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 return status;
6207}
6208
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006209static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6210{
Trond Myklebustacf92952014-06-30 11:48:37 -04006211 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04006212 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04006213 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006214
Jeff Layton2c41beb2014-07-29 21:34:41 -04006215 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04006216 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006217
Jeff Laytond83017f2014-07-29 21:34:42 -04006218 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04006219 if (unhashed)
6220 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04006221 spin_unlock(&clp->cl_lock);
6222 free_ol_stateid_reaplist(&reaplist);
6223 } else {
6224 spin_unlock(&clp->cl_lock);
6225 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04006226 if (unhashed)
6227 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04006228 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04006229}
6230
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231/*
6232 * nfs4_unlock_state() called after encode
6233 */
Al Virob37ad282006-10-19 23:28:59 -07006234__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006235nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006236 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006238 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07006239 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006240 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006241 struct net *net = SVC_NET(rqstp);
6242 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
Al Viroa6a9f182013-09-16 10:57:01 -04006244 dprintk("NFSD: nfsd4_close on file %pd\n",
6245 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006247 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6248 &close->cl_stateid,
6249 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006250 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006251 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006252 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006254
6255 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006256
6257 /*
6258 * Technically we don't _really_ have to increment or copy it, since
6259 * it should just be gone after this operation and we clobber the
6260 * copied value below, but we continue to do so here just to ensure
6261 * that racing ops see that there was a state change.
6262 */
Jeff Layton9767feb2015-10-01 09:05:50 -04006263 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006265 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006266 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006267
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006268 /* v4.1+ suggests that we send a special stateid in here, since the
6269 * clients should just ignore this anyway. Since this is not useful
6270 * for v4.0 clients either, we set it to the special close_stateid
6271 * universally.
6272 *
6273 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6274 */
6275 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04006276
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006277 /* put reference from nfs4_preprocess_seqid_op */
6278 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 return status;
6281}
6282
Al Virob37ad282006-10-19 23:28:59 -07006283__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006284nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006285 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006287 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006288 struct nfs4_delegation *dp;
6289 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006290 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07006291 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006292 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006294 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006295 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006297 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006298 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006299 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006300 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04006301 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006302 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04006303 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006304
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006305 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006306put_stateid:
6307 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308out:
6309 return status;
6310}
6311
Benny Halevy87df4de2008-12-15 19:42:03 +02006312/* last octet in a range */
6313static inline u64
6314last_byte_offset(u64 start, u64 len)
6315{
6316 u64 end;
6317
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006318 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006319 end = start + len;
6320 return end > start ? end - 1: NFS4_MAX_UINT64;
6321}
6322
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323/*
6324 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6325 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6326 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6327 * locking, this prevents us from being completely protocol-compliant. The
6328 * real solution to this problem is to start using unsigned file offsets in
6329 * the VFS, but this is a very deep change!
6330 */
6331static inline void
6332nfs4_transform_lock_offset(struct file_lock *lock)
6333{
6334 if (lock->fl_start < 0)
6335 lock->fl_start = OFFSET_MAX;
6336 if (lock->fl_end < 0)
6337 lock->fl_end = OFFSET_MAX;
6338}
6339
Jeff Laytoncae80b32015-04-03 09:04:04 -04006340static fl_owner_t
6341nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006342{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006343 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6344
6345 nfs4_get_stateowner(&lo->lo_owner);
6346 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006347}
6348
Jeff Laytoncae80b32015-04-03 09:04:04 -04006349static void
6350nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006351{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006352 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006353
Jeff Laytoncae80b32015-04-03 09:04:04 -04006354 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006355 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006356}
6357
Jeff Layton76d348f2016-09-16 16:28:24 -04006358static void
6359nfsd4_lm_notify(struct file_lock *fl)
6360{
6361 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6362 struct net *net = lo->lo_owner.so_client->net;
6363 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6364 struct nfsd4_blocked_lock *nbl = container_of(fl,
6365 struct nfsd4_blocked_lock, nbl_lock);
6366 bool queue = false;
6367
Jeff Layton7919d0a2016-09-16 16:28:25 -04006368 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006369 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006370 if (!list_empty(&nbl->nbl_list)) {
6371 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006372 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006373 queue = true;
6374 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006375 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006376
6377 if (queue)
6378 nfsd4_run_cb(&nbl->nbl_cb);
6379}
6380
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006381static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006382 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006383 .lm_get_owner = nfsd4_fl_get_owner,
6384 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006385};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386
6387static inline void
6388nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6389{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006390 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
NeilBrownd5b90262006-04-10 22:55:22 -07006392 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006393 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04006394 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6395 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006396 if (!deny->ld_owner.data)
6397 /* We just don't care that much */
6398 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006399 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006400 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006401nevermind:
6402 deny->ld_owner.len = 0;
6403 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006404 deny->ld_clientid.cl_boot = 0;
6405 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 }
6407 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006408 deny->ld_length = NFS4_MAX_UINT64;
6409 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6411 deny->ld_type = NFS4_READ_LT;
6412 if (fl->fl_type != F_RDLCK)
6413 deny->ld_type = NFS4_WRITE_LT;
6414}
6415
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006416static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006417find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006419 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006420 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421
Trond Myklebust0a880a22014-07-30 08:27:10 -04006422 lockdep_assert_held(&clp->cl_lock);
6423
Trond Myklebustd4f04892014-07-29 21:34:36 -04006424 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6425 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006426 if (so->so_is_open_owner)
6427 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006428 if (same_owner_str(so, owner))
6429 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 }
6431 return NULL;
6432}
6433
Trond Myklebustc58c6612014-07-29 21:34:35 -04006434static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006435find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006436{
6437 struct nfs4_lockowner *lo;
6438
Trond Myklebustd4f04892014-07-29 21:34:36 -04006439 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006440 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006441 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006442 return lo;
6443}
6444
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006445static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6446{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006447 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006448}
6449
Jeff Layton6b180f02014-07-29 21:34:26 -04006450static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6451{
6452 struct nfs4_lockowner *lo = lockowner(sop);
6453
6454 kmem_cache_free(lockowner_slab, lo);
6455}
6456
6457static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006458 .so_unhash = nfs4_unhash_lockowner,
6459 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006460};
6461
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462/*
6463 * Alloc a lock owner structure.
6464 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006465 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006467 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006469static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006470alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6471 struct nfs4_ol_stateid *open_stp,
6472 struct nfsd4_lock *lock)
6473{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006474 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006476 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6477 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006479 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006480 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6481 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006482 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006483 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006484 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006485 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006486 if (ret == NULL) {
6487 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006488 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006489 ret = lo;
6490 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006491 nfs4_free_stateowner(&lo->lo_owner);
6492
Trond Myklebustd4f04892014-07-29 21:34:36 -04006493 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006494 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495}
6496
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006497static struct nfs4_ol_stateid *
Trond Myklebusta451b122020-03-01 18:21:38 -05006498find_lock_stateid(const struct nfs4_lockowner *lo,
6499 const struct nfs4_ol_stateid *ost)
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006500{
6501 struct nfs4_ol_stateid *lst;
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006502
Trond Myklebusta451b122020-03-01 18:21:38 -05006503 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006504
Trond Myklebusta451b122020-03-01 18:21:38 -05006505 /* If ost is not hashed, ost->st_locks will not be valid */
6506 if (!nfs4_ol_stateid_unhashed(ost))
6507 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6508 if (lst->st_stateowner == &lo->lo_owner) {
6509 refcount_inc(&lst->st_stid.sc_count);
6510 return lst;
6511 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006512 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006513 return NULL;
6514}
6515
Trond Myklebustbeeca192017-11-03 08:00:14 -04006516static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006517init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6518 struct nfs4_file *fp, struct inode *inode,
6519 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006521 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006522 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523
Trond Myklebustbeeca192017-11-03 08:00:14 -04006524 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006525 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006526retry:
6527 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006528 if (nfs4_ol_stateid_unhashed(open_stp))
6529 goto out_close;
6530 retstp = find_lock_stateid(lo, open_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006531 if (retstp)
Trond Myklebusta451b122020-03-01 18:21:38 -05006532 goto out_found;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006533 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006534 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006535 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006536 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006537 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006538 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006540 stp->st_openstp = open_stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006541 spin_lock(&fp->fi_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006542 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006543 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006544 list_add(&stp->st_perfile, &fp->fi_stateids);
6545 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006546 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006547 return stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006548out_found:
6549 spin_unlock(&clp->cl_lock);
6550 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6551 nfs4_put_stid(&retstp->st_stid);
6552 goto retry;
6553 }
6554 /* To keep mutex tracking happy */
6555 mutex_unlock(&stp->st_mutex);
6556 return retstp;
6557out_close:
6558 spin_unlock(&clp->cl_lock);
6559 mutex_unlock(&stp->st_mutex);
6560 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561}
6562
Jeff Laytonc53530d2014-06-30 11:48:39 -04006563static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006564find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6565 struct inode *inode, struct nfs4_ol_stateid *ost,
6566 bool *new)
6567{
6568 struct nfs4_stid *ns = NULL;
6569 struct nfs4_ol_stateid *lst;
6570 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6571 struct nfs4_client *clp = oo->oo_owner.so_client;
6572
Trond Myklebustbeeca192017-11-03 08:00:14 -04006573 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006574 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006575 lst = find_lock_stateid(lo, ost);
Jeff Layton356a95e2014-07-29 21:34:13 -04006576 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006577 if (lst != NULL) {
6578 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6579 goto out;
6580 nfs4_put_stid(&lst->st_stid);
6581 }
6582 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6583 if (ns == NULL)
6584 return NULL;
6585
6586 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6587 if (lst == openlockstateid(ns))
6588 *new = true;
6589 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006590 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006591out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006592 return lst;
6593}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006594
NeilBrownfd39ca92005-06-23 22:04:03 -07006595static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596check_lock_length(u64 offset, u64 length)
6597{
Kinglong Meee7969312015-07-13 17:34:19 +08006598 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6599 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600}
6601
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006602static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006603{
Trond Myklebust11b91642014-07-29 21:34:08 -04006604 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006605
Jeff Layton7214e862014-07-10 14:07:33 -04006606 lockdep_assert_held(&fp->fi_lock);
6607
Jeff Layton82c5ff12012-05-11 09:45:13 -04006608 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006609 return;
Jeff Layton12659652014-07-10 14:07:28 -04006610 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006611 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006612}
6613
Jeff Layton356a95e2014-07-29 21:34:13 -04006614static __be32
6615lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6616 struct nfs4_ol_stateid *ost,
6617 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006618 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006619{
Jeff Layton5db1c032014-07-29 21:34:28 -04006620 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006621 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006622 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6623 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006624 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006625 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006626 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006627 unsigned int strhashval;
6628
Kinglong Meec8623992015-07-13 17:35:37 +08006629 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006630 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006631 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006632 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6633 if (lo == NULL)
6634 return nfserr_jukebox;
6635 } else {
6636 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006637 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006638 if (!cstate->minorversion &&
6639 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006640 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006641 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006642
Jeff Laytondd257932016-08-11 10:37:39 -04006643 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6644 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006645 status = nfserr_jukebox;
6646 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006647 }
Jeff Laytondd257932016-08-11 10:37:39 -04006648
Jeff Layton5db1c032014-07-29 21:34:28 -04006649 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006650 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006651out:
6652 nfs4_put_stateowner(&lo->lo_owner);
6653 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006654}
6655
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656/*
6657 * LOCK operation
6658 */
Al Virob37ad282006-10-19 23:28:59 -07006659__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006660nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006661 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006663 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006664 struct nfs4_openowner *open_sop = NULL;
6665 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006666 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006667 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006668 struct nfs4_file *fp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006669 struct nfsd_file *nf = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006670 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006671 struct file_lock *file_lock = NULL;
6672 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006673 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006674 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006675 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006676 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006677 unsigned char fl_type;
6678 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006679 struct net *net = SVC_NET(rqstp);
6680 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681
6682 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6683 (long long) lock->lk_offset,
6684 (long long) lock->lk_length);
6685
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 if (check_lock_length(lock->lk_offset, lock->lk_length))
6687 return nfserr_inval;
6688
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006689 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006690 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006691 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6692 return status;
6693 }
6694
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006696 if (nfsd4_has_session(cstate))
6697 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006698 memcpy(&lock->lk_new_clientid,
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006699 &cstate->clp->cl_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006700 sizeof(clientid_t));
6701
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006703 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 lock->lk_new_open_seqid,
6705 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006706 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006707 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006709 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006710 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006711 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006712 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006713 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006714 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006715 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006716 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006717 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006718 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006719 lock->lk_old_lock_seqid,
6720 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006721 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006722 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006723 if (status)
6724 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006725 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006727 lkflg = setlkflg(lock->lk_type);
6728 status = nfs4_check_openmode(lock_stp, lkflg);
6729 if (status)
6730 goto out;
6731
NeilBrown0dd395d2005-07-07 17:59:15 -07006732 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006733 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006734 goto out;
6735 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006736 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006737 goto out;
6738
Trond Myklebust11b91642014-07-29 21:34:08 -04006739 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006742 if (nfsd4_has_session(cstate))
6743 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006744 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006745 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006746 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006747 nf = find_readable_file_locked(fp);
6748 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006749 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006750 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006751 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006752 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006754 if (nfsd4_has_session(cstate))
6755 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006756 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006757 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006758 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006759 nf = find_writeable_file_locked(fp);
6760 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006761 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006762 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006763 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006764 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 default:
6766 status = nfserr_inval;
6767 goto out;
6768 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006769
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006770 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006771 status = nfserr_openmode;
6772 goto out;
6773 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006774
Jeff Layton76d348f2016-09-16 16:28:24 -04006775 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6776 if (!nbl) {
6777 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6778 status = nfserr_jukebox;
6779 goto out;
6780 }
6781
6782 file_lock = &nbl->nbl_lock;
6783 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006784 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006785 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006786 file_lock->fl_file = nf->nf_file;
Jeff Layton76d348f2016-09-16 16:28:24 -04006787 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006788 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6789 file_lock->fl_start = lock->lk_offset;
6790 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6791 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792
Jeff Layton21179d82012-08-21 08:03:32 -04006793 conflock = locks_alloc_lock();
6794 if (!conflock) {
6795 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6796 status = nfserr_jukebox;
6797 goto out;
6798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799
Jeff Layton76d348f2016-09-16 16:28:24 -04006800 if (fl_flags & FL_SLEEP) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01006801 nbl->nbl_time = ktime_get_boottime_seconds();
Jeff Layton0cc11a62016-10-20 09:34:31 -04006802 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006803 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006804 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006805 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006806 }
6807
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006808 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006809 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006811 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006812 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006813 if (lock->lk_reclaim)
6814 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006815 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006816 case FILE_LOCK_DEFERRED:
6817 nbl = NULL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006818 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006819 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006820 status = nfserr_denied;
6821 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006822 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006823 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006824 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006826 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006827 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006828 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006829 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006830 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006833 if (nbl) {
6834 /* dequeue it if we queued it before */
6835 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006836 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006837 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006838 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006839 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006840 }
6841 free_blocked_lock(nbl);
6842 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006843 if (nf)
6844 nfsd_file_put(nf);
Jeff Layton5db1c032014-07-29 21:34:28 -04006845 if (lock_stp) {
6846 /* Bump seqid manually if the 4.0 replay owner is openowner */
6847 if (cstate->replay_owner &&
6848 cstate->replay_owner != &lock_sop->lo_owner &&
6849 seqid_mutating_err(ntohl(status)))
6850 lock_sop->lo_owner.so_seqid++;
6851
6852 /*
6853 * If this is a new, never-before-used stateid, and we are
6854 * returning an error, then just go ahead and release it.
6855 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006856 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006857 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006858
6859 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006860
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006861 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006862 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006863 if (open_stp)
6864 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006865 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006866 if (conflock)
6867 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 return status;
6869}
6870
6871/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006872 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6873 * so we do a temporary open here just to get an open file to pass to
6874 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6875 * inode operation.)
6876 */
Al Viro04da6e92012-04-13 00:00:04 -04006877static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006878{
Jeff Layton6b556ca2019-08-18 14:18:55 -04006879 struct nfsd_file *nf;
J. Bruce Fields217fd6f2021-04-16 14:00:14 -04006880 __be32 err;
6881
6882 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
6883 if (err)
6884 return err;
6885 fh_lock(fhp); /* to block new leases till after test_lock: */
6886 err = nfserrno(nfsd_open_break_lease(fhp->fh_dentry->d_inode,
6887 NFSD_MAY_READ));
6888 if (err)
6889 goto out;
6890 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6891out:
6892 fh_unlock(fhp);
6893 nfsd_file_put(nf);
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006894 return err;
6895}
6896
6897/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 * LOCKT operation
6899 */
Al Virob37ad282006-10-19 23:28:59 -07006900__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006901nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006902 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006904 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006905 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006906 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006907 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006908 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006910 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 return nfserr_grace;
6912
6913 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6914 return nfserr_inval;
6915
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006916 if (!nfsd4_has_session(cstate)) {
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05006917 status = set_client(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006918 if (status)
6919 goto out;
6920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006922 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924
Jeff Layton21179d82012-08-21 08:03:32 -04006925 file_lock = locks_alloc_lock();
6926 if (!file_lock) {
6927 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6928 status = nfserr_jukebox;
6929 goto out;
6930 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006931
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932 switch (lockt->lt_type) {
6933 case NFS4_READ_LT:
6934 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006935 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006936 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 case NFS4_WRITE_LT:
6938 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006939 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006940 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006942 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006944 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945 }
6946
Kinglong Meec8623992015-07-13 17:35:37 +08006947 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006948 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006949 file_lock->fl_owner = (fl_owner_t)lo;
6950 file_lock->fl_pid = current->tgid;
6951 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952
Jeff Layton21179d82012-08-21 08:03:32 -04006953 file_lock->fl_start = lockt->lt_offset;
6954 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955
Jeff Layton21179d82012-08-21 08:03:32 -04006956 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957
Jeff Layton21179d82012-08-21 08:03:32 -04006958 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006959 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006960 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006961
Jeff Layton21179d82012-08-21 08:03:32 -04006962 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006964 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 }
6966out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006967 if (lo)
6968 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006969 if (file_lock)
6970 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 return status;
6972}
6973
Al Virob37ad282006-10-19 23:28:59 -07006974__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006975nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006976 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006978 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006979 struct nfs4_ol_stateid *stp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006980 struct nfsd_file *nf = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006981 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006982 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006983 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006984 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6985
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6987 (long long) locku->lu_offset,
6988 (long long) locku->lu_length);
6989
6990 if (check_lock_length(locku->lu_offset, locku->lu_length))
6991 return nfserr_inval;
6992
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006993 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006994 &locku->lu_stateid, NFS4_LOCK_STID,
6995 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006996 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 goto out;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006998 nf = find_any_file(stp->st_stid.sc_file);
6999 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04007000 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04007001 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04007002 }
Jeff Layton21179d82012-08-21 08:03:32 -04007003 file_lock = locks_alloc_lock();
7004 if (!file_lock) {
7005 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7006 status = nfserr_jukebox;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007007 goto put_file;
Jeff Layton21179d82012-08-21 08:03:32 -04007008 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08007009
Jeff Layton21179d82012-08-21 08:03:32 -04007010 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04007011 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04007012 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007013 file_lock->fl_file = nf->nf_file;
Jeff Layton21179d82012-08-21 08:03:32 -04007014 file_lock->fl_flags = FL_POSIX;
7015 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7016 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017
Jeff Layton21179d82012-08-21 08:03:32 -04007018 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7019 locku->lu_length);
7020 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007022 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07007023 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05007024 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025 goto out_nfserr;
7026 }
Jeff Layton9767feb2015-10-01 09:05:50 -04007027 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007028put_file:
7029 nfsd_file_put(nf);
Trond Myklebust858cc572014-07-29 21:34:16 -04007030put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04007031 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04007032 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04007034 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04007035 if (file_lock)
7036 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 return status;
7038
7039out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07007040 status = nfserrno(err);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007041 goto put_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042}
7043
7044/*
7045 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007046 * true: locks held by lockowner
7047 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007049static bool
7050check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007052 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007053 int status = false;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007054 struct nfsd_file *nf = find_any_file(fp);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007055 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007056 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007057
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007058 if (!nf) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007059 /* Any valid lock stateid should have some sort of access */
7060 WARN_ON_ONCE(1);
7061 return status;
7062 }
7063
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007064 inode = locks_inode(nf->nf_file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007065 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007067 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05007068 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007069 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7070 if (fl->fl_owner == (fl_owner_t)lowner) {
7071 status = true;
7072 break;
7073 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08007074 }
Jeff Layton6109c852015-01-16 15:05:57 -05007075 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007077 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078 return status;
7079}
7080
Al Virob37ad282006-10-19 23:28:59 -07007081__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08007082nfsd4_release_lockowner(struct svc_rqst *rqstp,
7083 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007084 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007086 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04007088 struct nfs4_stateowner *sop;
7089 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04007090 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04007092 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07007093 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03007094 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04007095 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04007096 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097
7098 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7099 clid->cl_boot, clid->cl_id);
7100
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05007101 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007102 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04007103 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007104
Trond Myklebustd4f04892014-07-29 21:34:36 -04007105 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04007106 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04007107 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04007108 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04007109 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04007110
7111 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05007112 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04007113
Jeff Layton882e9d22014-07-29 21:34:37 -04007114 /* see if there are still any locks associated with it */
7115 lo = lockowner(sop);
7116 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
7117 if (check_for_locks(stp->st_stid.sc_file, lo)) {
7118 status = nfserr_locks_held;
7119 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04007120 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04007121 }
Jeff Layton5adfd882014-07-29 21:34:31 -04007122 }
Jeff Layton882e9d22014-07-29 21:34:37 -04007123
Kinglong Meeb5971af2014-08-22 10:18:43 -04007124 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04007125 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04007126 }
Chuck Lever88584812016-07-13 16:40:14 -04007127 if (!lo) {
7128 spin_unlock(&clp->cl_lock);
7129 return status;
7130 }
7131
7132 unhash_lockowner_locked(lo);
7133 while (!list_empty(&lo->lo_owner.so_stateids)) {
7134 stp = list_first_entry(&lo->lo_owner.so_stateids,
7135 struct nfs4_ol_stateid,
7136 st_perstateowner);
7137 WARN_ON(!unhash_lock_stateid(stp));
7138 put_ol_stateid_locked(stp, &reaplist);
7139 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04007140 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04007141 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007142 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04007143 nfs4_put_stateowner(&lo->lo_owner);
7144
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 return status;
7146}
7147
7148static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07007149alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150{
NeilBrowna55370a2005-06-23 22:03:52 -07007151 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152}
7153
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007154bool
Scott Mayhew6b189102019-03-26 18:06:26 -04007155nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07007156{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007157 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07007158
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007159 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007160 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07007161}
7162
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163/*
7164 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04007165 *
7166 * The caller is responsible for freeing name.data if NULL is returned (it
7167 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05007169struct nfs4_client_reclaim *
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007170nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7171 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172{
7173 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007174 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007176 trace_nfsd_clid_reclaim(nn, name.len, name.data);
NeilBrowna55370a2005-06-23 22:03:52 -07007177 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05007178 if (crp) {
7179 strhashval = clientstr_hashval(name);
7180 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007181 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04007182 crp->cr_name.data = name.data;
7183 crp->cr_name.len = name.len;
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007184 crp->cr_princhash.data = princhash.data;
7185 crp->cr_princhash.len = princhash.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007186 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007187 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007188 }
7189 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190}
7191
Jeff Layton2a4317c2012-03-21 16:42:43 -04007192void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007193nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05007194{
7195 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04007196 kfree(crp->cr_name.data);
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007197 kfree(crp->cr_princhash.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05007198 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007199 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05007200}
7201
7202void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007203nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204{
7205 struct nfs4_client_reclaim *crp = NULL;
7206 int i;
7207
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007209 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7210 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007212 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 }
7214 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05007215 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216}
7217
7218/*
7219 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04007220struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04007221nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222{
7223 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 struct nfs4_client_reclaim *crp = NULL;
7225
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007226 trace_nfsd_clid_find(nn, name.len, name.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227
Scott Mayhew6b189102019-03-26 18:06:26 -04007228 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007229 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04007230 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 return crp;
7232 }
7233 }
7234 return NULL;
7235}
7236
Al Virob37ad282006-10-19 23:28:59 -07007237__be32
J. Bruce Fields1722b042021-01-21 17:57:44 -05007238nfs4_check_open_reclaim(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239{
J. Bruce Fields1722b042021-01-21 17:57:44 -05007240 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Jeff Layton3b3e7b72014-09-12 16:40:22 -04007241 return nfserr_no_grace;
7242
J. Bruce Fields1722b042021-01-21 17:57:44 -05007243 if (nfsd4_client_record_check(clp))
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007244 return nfserr_reclaim_bad;
7245
7246 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247}
7248
Meelap Shahc2f1a552007-07-17 04:04:39 -07007249/*
7250 * Since the lifetime of a delegation isn't limited to that of an open, a
7251 * client may quite reasonably hang on to a delegation as long as it has
7252 * the inode cached. This becomes an obvious problem the first time a
7253 * client's inode cache approaches the size of the server's total memory.
7254 *
7255 * For now we avoid this problem by imposing a hard limit on the number
7256 * of delegations, which varies according to the server's memory size.
7257 */
7258static void
7259set_max_delegations(void)
7260{
7261 /*
7262 * Allow at most 4 delegations per megabyte of RAM. Quick
7263 * estimates suggest that in the worst case (where every delegation
7264 * is for a different inode), a delegation could take about 1.5K,
7265 * giving a worst case usage of about 6% of memory.
7266 */
7267 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7268}
7269
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007270static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007271{
7272 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7273 int i;
7274
Kees Cook6da2ec52018-06-12 13:55:00 -07007275 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7276 sizeof(struct list_head),
7277 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007278 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007279 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007280 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7281 sizeof(struct list_head),
7282 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007283 if (!nn->unconf_id_hashtbl)
7284 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007285 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7286 sizeof(struct list_head),
7287 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007288 if (!nn->sessionid_hashtbl)
7289 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007290
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007291 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007292 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007293 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007294 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007295 for (i = 0; i < SESSION_HASH_SIZE; i++)
7296 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007297 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007298 nn->unconf_name_tree = RB_ROOT;
Arnd Bergmann9cc76802019-10-31 15:53:13 +01007299 nn->boot_time = ktime_get_real_seconds();
Vasily Averin81833de2017-11-13 07:25:40 +03007300 nn->grace_ended = false;
7301 nn->nfsd4_manager.block_opens = true;
7302 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007303 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007304 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007305 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007306 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007307 spin_lock_init(&nn->s2s_cp_lock);
7308 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007309
Jeff Layton0cc11a62016-10-20 09:34:31 -04007310 spin_lock_init(&nn->blocked_locks_lock);
7311 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7312
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007313 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007314 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007315
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007316 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007317
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007318err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007319 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007320err_unconf_id:
7321 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007322err:
7323 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007324}
7325
7326static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007327nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007328{
7329 int i;
7330 struct nfs4_client *clp = NULL;
7331 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7332
7333 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7334 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7335 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7336 destroy_client(clp);
7337 }
7338 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007339
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007340 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7341
Kinglong Mee2b905632014-03-26 22:09:30 +08007342 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7343 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7344 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7345 destroy_client(clp);
7346 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007347 }
7348
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007349 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007350 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007351 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007352 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007353}
7354
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007355int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007356nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007357{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007358 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007359 int ret;
7360
Trond Myklebustc6c7f2a2021-03-13 16:08:47 -05007361 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007362 if (ret)
7363 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007364 locks_start_grace(net, &nn->nfsd4_manager);
7365 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007366 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7367 goto skip_grace;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01007368 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
Vasily Averin7e981a82017-11-06 16:46:04 +03007369 nn->nfsd4_grace, net->ns.inum);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007370 trace_nfsd_grace_start(nn);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007371 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007372 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007373
7374skip_grace:
7375 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7376 net->ns.inum);
7377 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7378 nfsd4_end_grace(nn);
7379 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007380}
7381
7382/* initialization to perform when the nfsd service is started: */
7383
7384int
7385nfs4_state_start(void)
7386{
7387 int ret;
7388
Jeff Layton51a54562015-08-20 07:17:01 -04007389 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007390 if (laundry_wq == NULL) {
7391 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007392 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007393 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007394 ret = nfsd4_create_callback_queue();
7395 if (ret)
7396 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007397
Meelap Shahc2f1a552007-07-17 04:04:39 -07007398 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007399 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007400
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007401out_free_laundry:
7402 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007403out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007404 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405}
7406
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007407void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007408nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007412 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007414 cancel_delayed_work_sync(&nn->laundromat_work);
7415 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007416
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007418 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007419 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007421 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007422 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 }
Benny Halevycdc975052014-05-30 09:09:30 -04007424 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425 list_for_each_safe(pos, next, &reaplist) {
7426 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007427 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007428 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429 }
7430
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007431 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007432 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433}
7434
7435void
7436nfs4_state_shutdown(void)
7437{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007438 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007439 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007441
7442static void
7443get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7444{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007445 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7446 CURRENT_STATEID(stateid))
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007447 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007448}
7449
7450static void
7451put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7452{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007453 if (cstate->minorversion) {
7454 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007455 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007456 }
7457}
7458
7459void
7460clear_current_stateid(struct nfsd4_compound_state *cstate)
7461{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007462 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007463}
7464
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007465/*
7466 * functions to set current state id
7467 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007468void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007469nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7470 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007471{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007472 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007473}
7474
7475void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007476nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7477 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007478{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007479 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007480}
7481
7482void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007483nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7484 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007485{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007486 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007487}
7488
7489void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007490nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7491 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007492{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007493 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007494}
7495
7496/*
7497 * functions to consume current state id
7498 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007499
7500void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007501nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7502 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007503{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007504 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007505}
7506
7507void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007508nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7509 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007510{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007511 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007512}
7513
7514void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007515nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7516 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007517{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007518 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007519}
7520
7521void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007522nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7523 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007524{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007525 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007526}
7527
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007528void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007529nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7530 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007531{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007532 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007533}
7534
7535void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007536nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7537 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007538{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007539 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007540}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007541
7542void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007543nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7544 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007545{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007546 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007547}
7548
7549void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007550nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7551 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007552{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007553 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007554}