blob: 7698172ac0c7619319393ed03c56b511f453d14e [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
Trond Myklebustca943212014-07-23 16:17:39 -0400546static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400547{
Trond Myklebustca943212014-07-23 16:17:39 -0400548 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
549}
550
551static unsigned int file_hashval(struct knfsd_fh *fh)
552{
553 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
554}
555
Jeff Layton89876f82013-04-02 09:01:59 -0400556static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700557
Jeff Layton12659652014-07-10 14:07:28 -0400558static void
559__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400560{
Jeff Layton7214e862014-07-10 14:07:33 -0400561 lockdep_assert_held(&fp->fi_lock);
562
Jeff Layton12659652014-07-10 14:07:28 -0400563 if (access & NFS4_SHARE_ACCESS_WRITE)
564 atomic_inc(&fp->fi_access[O_WRONLY]);
565 if (access & NFS4_SHARE_ACCESS_READ)
566 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400567}
568
Jeff Layton12659652014-07-10 14:07:28 -0400569static __be32
570nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400571{
Jeff Layton7214e862014-07-10 14:07:33 -0400572 lockdep_assert_held(&fp->fi_lock);
573
Jeff Layton12659652014-07-10 14:07:28 -0400574 /* Does this access mode make sense? */
575 if (access & ~NFS4_SHARE_ACCESS_BOTH)
576 return nfserr_inval;
577
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400578 /* Does it conflict with a deny mode already set? */
579 if ((access & fp->fi_share_deny) != 0)
580 return nfserr_share_denied;
581
Jeff Layton12659652014-07-10 14:07:28 -0400582 __nfs4_file_get_access(fp, access);
583 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400584}
585
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400586static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
587{
588 /* Common case is that there is no deny mode. */
589 if (deny) {
590 /* Does this deny mode make sense? */
591 if (deny & ~NFS4_SHARE_DENY_BOTH)
592 return nfserr_inval;
593
594 if ((deny & NFS4_SHARE_DENY_READ) &&
595 atomic_read(&fp->fi_access[O_RDONLY]))
596 return nfserr_share_denied;
597
598 if ((deny & NFS4_SHARE_DENY_WRITE) &&
599 atomic_read(&fp->fi_access[O_WRONLY]))
600 return nfserr_share_denied;
601 }
602 return nfs_ok;
603}
604
J. Bruce Fields998db522010-08-07 09:21:41 -0400605static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400606{
Trond Myklebustde186432014-07-10 14:07:26 -0400607 might_lock(&fp->fi_lock);
608
609 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400610 struct nfsd_file *f1 = NULL;
611 struct nfsd_file *f2 = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400612
Jeff Layton6d338b52014-07-10 14:07:29 -0400613 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400614 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400615 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400616 spin_unlock(&fp->fi_lock);
617 if (f1)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400618 nfsd_file_put(f1);
Trond Myklebustde186432014-07-10 14:07:26 -0400619 if (f2)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400620 nfsd_file_put(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400621 }
622}
623
Jeff Layton12659652014-07-10 14:07:28 -0400624static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400625{
Jeff Layton12659652014-07-10 14:07:28 -0400626 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
627
628 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400629 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400630 if (access & NFS4_SHARE_ACCESS_READ)
631 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400632}
633
Sachin Bhamare8287f002015-04-27 14:50:14 +0200634/*
635 * Allocate a new open/delegation state counter. This is needed for
636 * pNFS for proper return on close semantics.
637 *
638 * Note that we only allocate it for pNFS-enabled exports, otherwise
639 * all pointers to struct nfs4_clnt_odstate are always NULL.
640 */
641static struct nfs4_clnt_odstate *
642alloc_clnt_odstate(struct nfs4_client *clp)
643{
644 struct nfs4_clnt_odstate *co;
645
646 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
647 if (co) {
648 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300649 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200650 }
651 return co;
652}
653
654static void
655hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
656{
657 struct nfs4_file *fp = co->co_file;
658
659 lockdep_assert_held(&fp->fi_lock);
660 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
661}
662
663static inline void
664get_clnt_odstate(struct nfs4_clnt_odstate *co)
665{
666 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300667 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200668}
669
670static void
671put_clnt_odstate(struct nfs4_clnt_odstate *co)
672{
673 struct nfs4_file *fp;
674
675 if (!co)
676 return;
677
678 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300679 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200680 list_del(&co->co_perfile);
681 spin_unlock(&fp->fi_lock);
682
683 nfsd4_return_all_file_layouts(co->co_client, fp);
684 kmem_cache_free(odstate_slab, co);
685 }
686}
687
688static struct nfs4_clnt_odstate *
689find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
690{
691 struct nfs4_clnt_odstate *co;
692 struct nfs4_client *cl;
693
694 if (!new)
695 return NULL;
696
697 cl = new->co_client;
698
699 spin_lock(&fp->fi_lock);
700 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
701 if (co->co_client == cl) {
702 get_clnt_odstate(co);
703 goto out;
704 }
705 }
706 co = new;
707 co->co_file = fp;
708 hash_clnt_odstate_locked(new);
709out:
710 spin_unlock(&fp->fi_lock);
711 return co;
712}
713
Kinglong Meed19fb702017-01-18 19:04:42 +0800714struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
715 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400716{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500717 struct nfs4_stid *stid;
718 int new_id;
719
Trond Myklebustf8338832014-07-25 07:34:19 -0400720 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500721 if (!stid)
722 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400723
Jeff Layton4770d722014-07-29 21:34:10 -0400724 idr_preload(GFP_KERNEL);
725 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400726 /* Reserving 0 for start of file in nfsdfs "states" file: */
727 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400728 spin_unlock(&cl->cl_lock);
729 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700730 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500731 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800732
733 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500734 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500735 stid->sc_stateid.si_opaque.so_id = new_id;
736 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
737 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300738 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400739 spin_lock_init(&stid->sc_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400740 INIT_LIST_HEAD(&stid->sc_cp_list);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500741
J. Bruce Fields996e0932011-10-17 11:14:48 -0400742 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500743 * It shouldn't be a problem to reuse an opaque stateid value.
744 * I don't think it is for 4.1. But with 4.0 I worry that, for
745 * example, a stray write retransmission could be accepted by
746 * the server when it should have been rejected. Therefore,
747 * adopt a trick from the sctp code to attempt to maximize the
748 * amount of time until an id is reused, by ensuring they always
749 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400750 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500751 return stid;
752out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800753 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500754 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400755}
756
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400757/*
758 * Create a unique stateid_t to represent each COPY.
759 */
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400760static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
761 unsigned char sc_type)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400762{
763 int new_id;
764
Arnd Bergmann9cc76802019-10-31 15:53:13 +0100765 stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400766 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
767 stid->sc_type = sc_type;
768
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400769 idr_preload(GFP_KERNEL);
770 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400771 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
772 stid->stid.si_opaque.so_id = new_id;
Dai Ngoca9364d2020-11-30 16:24:49 -0500773 stid->stid.si_generation = 1;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400774 spin_unlock(&nn->s2s_cp_lock);
775 idr_preload_end();
776 if (new_id < 0)
777 return 0;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400778 return 1;
779}
780
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400781int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
782{
783 return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
784}
785
786struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
787 struct nfs4_stid *p_stid)
788{
789 struct nfs4_cpntf_state *cps;
790
791 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
792 if (!cps)
793 return NULL;
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100794 cps->cpntf_time = ktime_get_boottime_seconds();
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400795 refcount_set(&cps->cp_stateid.sc_count, 1);
796 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
797 goto out_free;
798 spin_lock(&nn->s2s_cp_lock);
799 list_add(&cps->cp_list, &p_stid->sc_cp_list);
800 spin_unlock(&nn->s2s_cp_lock);
801 return cps;
802out_free:
803 kfree(cps);
804 return NULL;
805}
806
807void nfs4_free_copy_state(struct nfsd4_copy *copy)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400808{
809 struct nfsd_net *nn;
810
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400811 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400812 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
813 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400814 idr_remove(&nn->s2s_cp_stateids,
815 copy->cp_stateid.stid.si_opaque.so_id);
816 spin_unlock(&nn->s2s_cp_lock);
817}
818
819static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
820{
821 struct nfs4_cpntf_state *cps;
822 struct nfsd_net *nn;
823
824 nn = net_generic(net, nfsd_net_id);
825 spin_lock(&nn->s2s_cp_lock);
826 while (!list_empty(&stid->sc_cp_list)) {
827 cps = list_first_entry(&stid->sc_cp_list,
828 struct nfs4_cpntf_state, cp_list);
829 _free_cpntf_state_locked(nn, cps);
830 }
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400831 spin_unlock(&nn->s2s_cp_lock);
832}
833
Jeff Laytonb49e0842014-07-29 21:34:11 -0400834static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400835{
Trond Myklebust60116952014-07-29 21:34:06 -0400836 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400837
Kinglong Meed19fb702017-01-18 19:04:42 +0800838 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400839 if (!stid)
840 return NULL;
841
Kinglong Meed19fb702017-01-18 19:04:42 +0800842 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400843}
844
845static void nfs4_free_deleg(struct nfs4_stid *stid)
846{
Trond Myklebust60116952014-07-29 21:34:06 -0400847 kmem_cache_free(deleg_slab, stid);
848 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400849}
850
NeilBrown6282cd52014-06-04 17:39:26 +1000851/*
852 * When we recall a delegation, we should be careful not to hand it
853 * out again straight away.
854 * To ensure this we keep a pair of bloom filters ('new' and 'old')
855 * in which the filehandles of recalled delegations are "stored".
856 * If a filehandle appear in either filter, a delegation is blocked.
857 * When a delegation is recalled, the filehandle is stored in the "new"
858 * filter.
859 * Every 30 seconds we swap the filters and clear the "new" one,
860 * unless both are empty of course.
861 *
862 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
863 * low 3 bytes as hash-table indices.
864 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400865 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000866 * is used to manage concurrent access. Testing does not need the lock
867 * except when swapping the two filters.
868 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400869static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000870static struct bloom_pair {
871 int entries, old_entries;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200872 time64_t swap_time;
NeilBrown6282cd52014-06-04 17:39:26 +1000873 int new; /* index into 'set' */
874 DECLARE_BITMAP(set[2], 256);
875} blocked_delegations;
876
877static int delegation_blocked(struct knfsd_fh *fh)
878{
879 u32 hash;
880 struct bloom_pair *bd = &blocked_delegations;
881
882 if (bd->entries == 0)
883 return 0;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200884 if (ktime_get_seconds() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400885 spin_lock(&blocked_delegations_lock);
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200886 if (ktime_get_seconds() - bd->swap_time > 30) {
NeilBrown6282cd52014-06-04 17:39:26 +1000887 bd->entries -= bd->old_entries;
888 bd->old_entries = bd->entries;
889 memset(bd->set[bd->new], 0,
890 sizeof(bd->set[0]));
891 bd->new = 1-bd->new;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200892 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +1000893 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400894 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000895 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100896 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000897 if (test_bit(hash&255, bd->set[0]) &&
898 test_bit((hash>>8)&255, bd->set[0]) &&
899 test_bit((hash>>16)&255, bd->set[0]))
900 return 1;
901
902 if (test_bit(hash&255, bd->set[1]) &&
903 test_bit((hash>>8)&255, bd->set[1]) &&
904 test_bit((hash>>16)&255, bd->set[1]))
905 return 1;
906
907 return 0;
908}
909
910static void block_delegations(struct knfsd_fh *fh)
911{
912 u32 hash;
913 struct bloom_pair *bd = &blocked_delegations;
914
Daniel Borkmann87545892014-12-10 16:33:11 +0100915 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000916
Jeff Laytonf54fe962014-07-25 07:34:26 -0400917 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000918 __set_bit(hash&255, bd->set[bd->new]);
919 __set_bit((hash>>8)&255, bd->set[bd->new]);
920 __set_bit((hash>>16)&255, bd->set[bd->new]);
921 if (bd->entries == 0)
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200922 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +1000923 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400924 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500928alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
929 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200930 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400933 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400936 n = atomic_long_inc_return(&num_delegations);
937 if (n < 0 || n > max_delegations)
938 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000939 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400940 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800941 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700942 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400943 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400944
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400945 /*
946 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400947 * meaning of seqid 0 isn't meaningful, really, but let's avoid
948 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400949 */
950 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700951 INIT_LIST_HEAD(&dp->dl_perfile);
952 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200954 dp->dl_clnt_odstate = odstate;
955 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400956 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200957 dp->dl_retries = 1;
958 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200959 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500960 get_nfs4_file(fp);
961 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400963out_dec:
964 atomic_long_dec(&num_delegations);
965 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968void
Trond Myklebust60116952014-07-29 21:34:06 -0400969nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Trond Myklebust11b91642014-07-29 21:34:08 -0400971 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400972 struct nfs4_client *clp = s->sc_client;
973
Jeff Layton4770d722014-07-29 21:34:10 -0400974 might_lock(&clp->cl_lock);
975
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300976 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400977 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400978 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400979 }
Trond Myklebust60116952014-07-29 21:34:06 -0400980 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400981 nfs4_free_cpntf_statelist(clp->net, s);
Jeff Layton4770d722014-07-29 21:34:10 -0400982 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400983 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400984 if (fp)
985 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Jeff Layton9767feb2015-10-01 09:05:50 -0400988void
989nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
990{
991 stateid_t *src = &stid->sc_stateid;
992
993 spin_lock(&stid->sc_lock);
994 if (unlikely(++src->si_generation == 0))
995 src->si_generation = 1;
996 memcpy(dst, src, sizeof(*dst));
997 spin_unlock(&stid->sc_lock);
998}
999
J. Bruce Fields353601e2018-02-16 14:29:42 -05001000static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001002 struct nfsd_file *nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05001003
1004 spin_lock(&fp->fi_lock);
1005 if (--fp->fi_delegees == 0)
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001006 swap(nf, fp->fi_deleg_file);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001007 spin_unlock(&fp->fi_lock);
1008
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001009 if (nf)
1010 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001011}
1012
1013static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1014{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -05001015 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001016 struct nfsd_file *nf = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -04001017
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -05001018 WARN_ON_ONCE(!fp->fi_delegees);
1019
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001020 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001021 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001024static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1025{
1026 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001027 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001028 nfs4_put_stid(&dp->dl_stid);
1029}
1030
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001031void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001032{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001033 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001034}
1035
Andrew Elble34ed9872015-10-15 12:07:28 -04001036/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001037 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -04001038 * @clp: a pointer to the nfs4_client we're granting a delegation to
1039 * @fp: a pointer to the nfs4_file we're granting a delegation on
1040 *
1041 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001042 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -04001043 */
1044
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001045static bool
1046nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -04001047{
Andrew Elble34ed9872015-10-15 12:07:28 -04001048 struct nfs4_delegation *searchdp = NULL;
1049 struct nfs4_client *searchclp = NULL;
1050
Benny Halevycdc975052014-05-30 09:09:30 -04001051 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04001052 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001053
Andrew Elble34ed9872015-10-15 12:07:28 -04001054 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1055 searchclp = searchdp->dl_stid.sc_client;
1056 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001057 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -04001058 }
1059 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001060 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -04001061}
1062
1063/**
1064 * hash_delegation_locked - Add a delegation to the appropriate lists
1065 * @dp: a pointer to the nfs4_delegation we are adding.
1066 * @fp: a pointer to the nfs4_file we're granting a delegation on
1067 *
1068 * Return:
1069 * On success: NULL if the delegation was successfully hashed.
1070 *
1071 * On error: -EAGAIN if one was previously granted to this
1072 * nfs4_client for this nfs4_file. Delegation is not hashed.
1073 *
1074 */
1075
1076static int
1077hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1078{
Andrew Elble34ed9872015-10-15 12:07:28 -04001079 struct nfs4_client *clp = dp->dl_stid.sc_client;
1080
1081 lockdep_assert_held(&state_lock);
1082 lockdep_assert_held(&fp->fi_lock);
1083
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001084 if (nfs4_delegation_exists(clp, fp))
1085 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001086 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001087 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001088 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001089 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1090 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001091}
1092
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001093static bool
Jeff Layton42690672014-07-25 07:34:20 -04001094unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Trond Myklebust11b91642014-07-29 21:34:08 -04001096 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001097
Jeff Layton42690672014-07-25 07:34:20 -04001098 lockdep_assert_held(&state_lock);
1099
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001100 if (list_empty(&dp->dl_perfile))
1101 return false;
1102
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001103 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001104 /* Ensure that deleg break won't try to requeue it */
1105 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001106 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001107 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001109 list_del_init(&dp->dl_perfile);
1110 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001111 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001112}
1113
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001114static void destroy_delegation(struct nfs4_delegation *dp)
1115{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001116 bool unhashed;
1117
Jeff Layton42690672014-07-25 07:34:20 -04001118 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001119 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001120 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001121 if (unhashed)
1122 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001123}
1124
1125static void revoke_delegation(struct nfs4_delegation *dp)
1126{
1127 struct nfs4_client *clp = dp->dl_stid.sc_client;
1128
Jeff Layton2d4a5322014-07-25 07:34:21 -04001129 WARN_ON(!list_empty(&dp->dl_recall_lru));
1130
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001131 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001132 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001133 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001134 spin_lock(&clp->cl_lock);
1135 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1136 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001137 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001138 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141/*
1142 * SETCLIENTID state
1143 */
1144
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001145static unsigned int clientid_hashval(u32 id)
1146{
1147 return id & CLIENT_HASH_MASK;
1148}
1149
Scott Mayhew6b189102019-03-26 18:06:26 -04001150static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001151{
Scott Mayhew6b189102019-03-26 18:06:26 -04001152 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001153}
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001156 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1157 * st_{access,deny}_bmap field of the stateid, in order to track not
1158 * only what share bits are currently in force, but also what
1159 * combinations of share bits previous opens have used. This allows us
1160 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1161 * return an error if the client attempt to downgrade to a combination
1162 * of share bits not explicable by closing some of its previous opens.
1163 *
1164 * XXX: This enforcement is actually incomplete, since we don't keep
1165 * track of access/deny bit combinations; so, e.g., we allow:
1166 *
1167 * OPEN allow read, deny write
1168 * OPEN allow both, deny none
1169 * DOWNGRADE allow read, deny none
1170 *
1171 * which we should reject.
1172 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001173static unsigned int
1174bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001175 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001176 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001177
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001178 for (i = 1; i < 4; i++) {
1179 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001180 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001181 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001182 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001183}
1184
Jeff Layton82c5ff12012-05-11 09:45:13 -04001185/* set share access for a given stateid */
1186static inline void
1187set_access(u32 access, struct nfs4_ol_stateid *stp)
1188{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001189 unsigned char mask = 1 << access;
1190
1191 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1192 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001193}
1194
1195/* clear share access for a given stateid */
1196static inline void
1197clear_access(u32 access, struct nfs4_ol_stateid *stp)
1198{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001199 unsigned char mask = 1 << access;
1200
1201 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1202 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001203}
1204
1205/* test whether a given stateid has access */
1206static inline bool
1207test_access(u32 access, struct nfs4_ol_stateid *stp)
1208{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001209 unsigned char mask = 1 << access;
1210
1211 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001212}
1213
Jeff Laytonce0fc432012-05-11 09:45:14 -04001214/* set share deny for a given stateid */
1215static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001216set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001217{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001218 unsigned char mask = 1 << deny;
1219
1220 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1221 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001222}
1223
1224/* clear share deny for a given stateid */
1225static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001226clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001227{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001228 unsigned char mask = 1 << deny;
1229
1230 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1231 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001232}
1233
1234/* test whether a given stateid is denying specific access */
1235static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001236test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001237{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001238 unsigned char mask = 1 << deny;
1239
1240 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001241}
1242
1243static int nfs4_access_to_omode(u32 access)
1244{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001245 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001246 case NFS4_SHARE_ACCESS_READ:
1247 return O_RDONLY;
1248 case NFS4_SHARE_ACCESS_WRITE:
1249 return O_WRONLY;
1250 case NFS4_SHARE_ACCESS_BOTH:
1251 return O_RDWR;
1252 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001253 WARN_ON_ONCE(1);
1254 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001255}
1256
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001257/*
1258 * A stateid that had a deny mode associated with it is being released
1259 * or downgraded. Recalculate the deny mode on the file.
1260 */
1261static void
1262recalculate_deny_mode(struct nfs4_file *fp)
1263{
1264 struct nfs4_ol_stateid *stp;
1265
1266 spin_lock(&fp->fi_lock);
1267 fp->fi_share_deny = 0;
1268 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1269 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1270 spin_unlock(&fp->fi_lock);
1271}
1272
1273static void
1274reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1275{
1276 int i;
1277 bool change = false;
1278
1279 for (i = 1; i < 4; i++) {
1280 if ((i & deny) != i) {
1281 change = true;
1282 clear_deny(i, stp);
1283 }
1284 }
1285
1286 /* Recalculate per-file deny mode if there was a change */
1287 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001288 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001289}
1290
Jeff Layton82c5ff12012-05-11 09:45:13 -04001291/* release all access and file references for a given stateid */
1292static void
1293release_all_access(struct nfs4_ol_stateid *stp)
1294{
1295 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001296 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001297
1298 if (fp && stp->st_deny_bmap != 0)
1299 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001300
1301 for (i = 1; i < 4; i++) {
1302 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001303 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001304 clear_access(i, stp);
1305 }
1306}
1307
Kinglong Meed50ffde2015-07-16 12:05:07 +08001308static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1309{
1310 kfree(sop->so_owner.data);
1311 sop->so_ops->so_free(sop);
1312}
1313
Jeff Layton6b180f02014-07-29 21:34:26 -04001314static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1315{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001316 struct nfs4_client *clp = sop->so_client;
1317
1318 might_lock(&clp->cl_lock);
1319
1320 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001321 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001322 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001323 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001324 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001325}
1326
Trond Myklebusta451b122020-03-01 18:21:38 -05001327static bool
1328nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1329{
1330 return list_empty(&stp->st_perfile);
1331}
1332
Jeff Laytone8568732015-08-24 12:41:47 -04001333static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001334{
Trond Myklebust11b91642014-07-29 21:34:08 -04001335 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001336
Jeff Layton1c755dc2014-07-29 21:34:12 -04001337 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1338
Jeff Laytone8568732015-08-24 12:41:47 -04001339 if (list_empty(&stp->st_perfile))
1340 return false;
1341
Trond Myklebust1d31a252014-07-10 14:07:25 -04001342 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001343 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001344 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001345 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001346 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001347}
1348
Trond Myklebust60116952014-07-29 21:34:06 -04001349static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001350{
Trond Myklebust60116952014-07-29 21:34:06 -04001351 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001352
Sachin Bhamare8287f002015-04-27 14:50:14 +02001353 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001354 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001355 if (stp->st_stateowner)
1356 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001357 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001358}
1359
Jeff Laytonb49e0842014-07-29 21:34:11 -04001360static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001361{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001362 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1363 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001364 struct nfsd_file *nf;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001365
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001366 nf = find_any_file(stp->st_stid.sc_file);
1367 if (nf) {
1368 get_file(nf->nf_file);
1369 filp_close(nf->nf_file, (fl_owner_t)lo);
1370 nfsd_file_put(nf);
1371 }
Jeff Laytonb49e0842014-07-29 21:34:11 -04001372 nfs4_free_ol_stateid(stid);
1373}
1374
Jeff Layton2c41beb2014-07-29 21:34:41 -04001375/*
1376 * Put the persistent reference to an already unhashed generic stateid, while
1377 * holding the cl_lock. If it's the last reference, then put it onto the
1378 * reaplist for later destruction.
1379 */
1380static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1381 struct list_head *reaplist)
1382{
1383 struct nfs4_stid *s = &stp->st_stid;
1384 struct nfs4_client *clp = s->sc_client;
1385
1386 lockdep_assert_held(&clp->cl_lock);
1387
1388 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1389
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001390 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001391 wake_up_all(&close_wq);
1392 return;
1393 }
1394
1395 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1396 list_add(&stp->st_locks, reaplist);
1397}
1398
Jeff Laytone8568732015-08-24 12:41:47 -04001399static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001400{
Chuck Leverf46c4452016-10-29 18:19:03 -04001401 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001402
Trond Myklebusta451b122020-03-01 18:21:38 -05001403 if (!unhash_ol_stateid(stp))
1404 return false;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001405 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001406 nfs4_unhash_stid(&stp->st_stid);
Trond Myklebusta451b122020-03-01 18:21:38 -05001407 return true;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001408}
1409
Jeff Layton5adfd882014-07-29 21:34:31 -04001410static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001411{
Chuck Leverf46c4452016-10-29 18:19:03 -04001412 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001413 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001414
Chuck Leverf46c4452016-10-29 18:19:03 -04001415 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001416 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001417 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001418 if (unhashed)
1419 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001420}
1421
Trond Myklebustc58c6612014-07-29 21:34:35 -04001422static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001423{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001424 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001425
Trond Myklebustd4f04892014-07-29 21:34:36 -04001426 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001427
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001428 list_del_init(&lo->lo_owner.so_strhash);
1429}
1430
Jeff Layton2c41beb2014-07-29 21:34:41 -04001431/*
1432 * Free a list of generic stateids that were collected earlier after being
1433 * fully unhashed.
1434 */
1435static void
1436free_ol_stateid_reaplist(struct list_head *reaplist)
1437{
1438 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001439 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001440
1441 might_sleep();
1442
1443 while (!list_empty(reaplist)) {
1444 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1445 st_locks);
1446 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001447 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001448 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001449 if (fp)
1450 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001451 }
1452}
1453
Jeff Laytond83017f2014-07-29 21:34:42 -04001454static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1455 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001456{
1457 struct nfs4_ol_stateid *stp;
1458
Jeff Laytone8568732015-08-24 12:41:47 -04001459 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1460
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001461 while (!list_empty(&open_stp->st_locks)) {
1462 stp = list_entry(open_stp->st_locks.next,
1463 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001464 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001465 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001466 }
1467}
1468
Jeff Laytone8568732015-08-24 12:41:47 -04001469static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001470 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001471{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001472 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1473
Trond Myklebusta451b122020-03-01 18:21:38 -05001474 if (!unhash_ol_stateid(stp))
1475 return false;
Jeff Laytond83017f2014-07-29 21:34:42 -04001476 release_open_stateid_locks(stp, reaplist);
Trond Myklebusta451b122020-03-01 18:21:38 -05001477 return true;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001478}
1479
1480static void release_open_stateid(struct nfs4_ol_stateid *stp)
1481{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001482 LIST_HEAD(reaplist);
1483
1484 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001485 if (unhash_open_stateid(stp, &reaplist))
1486 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001487 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1488 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001489}
1490
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001491static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001492{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001493 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001494
Trond Myklebustd4f04892014-07-29 21:34:36 -04001495 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001496
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001497 list_del_init(&oo->oo_owner.so_strhash);
1498 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001499}
1500
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001501static void release_last_closed_stateid(struct nfs4_openowner *oo)
1502{
Jeff Layton217526e2014-07-30 08:27:11 -04001503 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1504 nfsd_net_id);
1505 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001506
Jeff Layton217526e2014-07-30 08:27:11 -04001507 spin_lock(&nn->client_lock);
1508 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001509 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001510 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001511 oo->oo_last_closed_stid = NULL;
1512 }
Jeff Layton217526e2014-07-30 08:27:11 -04001513 spin_unlock(&nn->client_lock);
1514 if (s)
1515 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001516}
1517
Jeff Layton2c41beb2014-07-29 21:34:41 -04001518static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001519{
1520 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001521 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001522 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001523
Jeff Layton2c41beb2014-07-29 21:34:41 -04001524 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001525
Trond Myklebustd4f04892014-07-29 21:34:36 -04001526 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001527 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001528 while (!list_empty(&oo->oo_owner.so_stateids)) {
1529 stp = list_first_entry(&oo->oo_owner.so_stateids,
1530 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001531 if (unhash_open_stateid(stp, &reaplist))
1532 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001533 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001534 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001535 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001536 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001537 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001538}
1539
Marc Eshel5282fd72009-04-03 08:27:52 +03001540static inline int
1541hash_sessionid(struct nfs4_sessionid *sessionid)
1542{
1543 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1544
1545 return sid->sequence % SESSION_HASH_SIZE;
1546}
1547
Mark Salter135dd002015-04-06 09:46:00 -04001548#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001549static inline void
1550dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1551{
1552 u32 *ptr = (u32 *)(&sessionid->data[0]);
1553 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1554}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001555#else
1556static inline void
1557dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1558{
1559}
1560#endif
1561
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001562/*
1563 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1564 * won't be used for replay.
1565 */
1566void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1567{
1568 struct nfs4_stateowner *so = cstate->replay_owner;
1569
1570 if (nfserr == nfserr_replay_me)
1571 return;
1572
1573 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001574 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001575 return;
1576 }
1577 if (!so)
1578 return;
1579 if (so->so_is_open_owner)
1580 release_last_closed_stateid(openowner(so));
1581 so->so_seqid++;
1582 return;
1583}
Marc Eshel5282fd72009-04-03 08:27:52 +03001584
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001585static void
1586gen_sessionid(struct nfsd4_session *ses)
1587{
1588 struct nfs4_client *clp = ses->se_client;
1589 struct nfsd4_sessionid *sid;
1590
1591 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1592 sid->clientid = clp->cl_clientid;
1593 sid->sequence = current_sessionid++;
1594 sid->reserved = 0;
1595}
1596
1597/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001598 * The protocol defines ca_maxresponssize_cached to include the size of
1599 * the rpc header, but all we need to cache is the data starting after
1600 * the end of the initial SEQUENCE operation--the rest we regenerate
1601 * each time. Therefore we can advertise a ca_maxresponssize_cached
1602 * value that is the number of bytes in our cache plus a few additional
1603 * bytes. In order to stay on the safe side, and not promise more than
1604 * we can cache, those additional bytes must be the minimum possible: 24
1605 * bytes of rpc header (xid through accept state, with AUTH_NULL
1606 * verifier), 12 for the compound header (with zero-length tag), and 44
1607 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001608 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001609#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1610
Andy Adamson557ce262009-08-28 08:45:04 -04001611static void
1612free_session_slots(struct nfsd4_session *ses)
1613{
1614 int i;
1615
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001616 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1617 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001618 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001619 }
Andy Adamson557ce262009-08-28 08:45:04 -04001620}
1621
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001622/*
1623 * We don't actually need to cache the rpc and session headers, so we
1624 * can allocate a little less for each slot:
1625 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001626static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001627{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001628 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001629
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001630 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1631 size = 0;
1632 else
1633 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1634 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001635}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001636
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001637/*
1638 * XXX: If we run out of reserved DRC memory we could (up to a point)
1639 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001640 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001641 */
NeilBrown2030ca52019-09-20 16:36:45 +10001642static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001643{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001644 u32 slotsize = slot_bytes(ca);
1645 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001646 unsigned long avail, total_avail;
NeilBrown2030ca52019-09-20 16:36:45 +10001647 unsigned int scale_factor;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001648
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001649 spin_lock(&nfsd_drc_lock);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001650 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1651 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1652 else
1653 /* We have handed out more space than we chose in
1654 * set_max_drc() to allow. That isn't really a
1655 * problem as long as that doesn't make us think we
1656 * have lots more due to integer overflow.
1657 */
1658 total_avail = 0;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001659 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001660 /*
NeilBrown2030ca52019-09-20 16:36:45 +10001661 * Never use more than a fraction of the remaining memory,
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001662 * unless it's the only way to give this client a slot.
NeilBrown2030ca52019-09-20 16:36:45 +10001663 * The chosen fraction is either 1/8 or 1/number of threads,
1664 * whichever is smaller. This ensures there are adequate
1665 * slots to support multiple clients per thread.
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001666 * Give the client one slot even if that would require
1667 * over-allocation--it is better than failure.
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001668 */
NeilBrown2030ca52019-09-20 16:36:45 +10001669 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1670
1671 avail = clamp_t(unsigned long, avail, slotsize,
1672 total_avail/scale_factor);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001673 num = min_t(int, num, avail / slotsize);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001674 num = max_t(int, num, 1);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001675 nfsd_drc_mem_used += num * slotsize;
1676 spin_unlock(&nfsd_drc_lock);
1677
1678 return num;
1679}
1680
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001681static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001682{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001683 int slotsize = slot_bytes(ca);
1684
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001685 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001686 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001687 spin_unlock(&nfsd_drc_lock);
1688}
1689
Kinglong Mee60810e52014-01-01 00:35:47 +08001690static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1691 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001692{
Kinglong Mee60810e52014-01-01 00:35:47 +08001693 int numslots = fattrs->maxreqs;
1694 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001695 struct nfsd4_session *new;
1696 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001697
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001698 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001699 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1700 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001701
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001702 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001703 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001704 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001705 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001706 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001707 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001708 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001709 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001710 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001711
1712 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1713 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1714
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001715 return new;
1716out_free:
1717 while (i--)
1718 kfree(new->se_slots[i]);
1719 kfree(new);
1720 return NULL;
1721}
1722
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001723static void free_conn(struct nfsd4_conn *c)
1724{
1725 svc_xprt_put(c->cn_xprt);
1726 kfree(c);
1727}
1728
1729static void nfsd4_conn_lost(struct svc_xpt_user *u)
1730{
1731 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1732 struct nfs4_client *clp = c->cn_session->se_client;
1733
1734 spin_lock(&clp->cl_lock);
1735 if (!list_empty(&c->cn_persession)) {
1736 list_del(&c->cn_persession);
1737 free_conn(c);
1738 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001739 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001740 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001741}
1742
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001743static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001744{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001745 struct nfsd4_conn *conn;
1746
1747 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1748 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001749 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001750 svc_xprt_get(rqstp->rq_xprt);
1751 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001752 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001753 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1754 return conn;
1755}
1756
J. Bruce Fields328ead22010-09-29 16:11:06 -04001757static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1758{
1759 conn->cn_session = ses;
1760 list_add(&conn->cn_persession, &ses->se_conns);
1761}
1762
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001763static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1764{
1765 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001766
1767 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001768 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001769 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001770}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001771
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001772static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001773{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001774 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001775 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001776}
1777
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001778static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001779{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001780 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001781
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001782 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001783 ret = nfsd4_register_conn(conn);
1784 if (ret)
1785 /* oops; xprt is already down: */
1786 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001787 /* We may have gained or lost a callback channel: */
1788 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001789}
1790
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001791static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001792{
1793 u32 dir = NFS4_CDFC4_FORE;
1794
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001795 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001796 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001797 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001798}
1799
1800/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001801static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001802{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001803 struct nfs4_client *clp = s->se_client;
1804 struct nfsd4_conn *c;
1805
1806 spin_lock(&clp->cl_lock);
1807 while (!list_empty(&s->se_conns)) {
1808 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1809 list_del_init(&c->cn_persession);
1810 spin_unlock(&clp->cl_lock);
1811
1812 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1813 free_conn(c);
1814
1815 spin_lock(&clp->cl_lock);
1816 }
1817 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001818}
1819
J. Bruce Fields1377b692012-09-11 21:42:40 -04001820static void __free_session(struct nfsd4_session *ses)
1821{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001822 free_session_slots(ses);
1823 kfree(ses);
1824}
1825
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001826static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001827{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001828 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001829 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001830 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001831}
Andy Adamson557ce262009-08-28 08:45:04 -04001832
Fengguang Wu135ae822012-11-10 07:20:25 -05001833static 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 -04001834{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001835 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001836 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001837
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001838 new->se_client = clp;
1839 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001840
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001841 INIT_LIST_HEAD(&new->se_conns);
1842
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001843 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001844 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001845 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001846 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001847 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001848 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001849 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001850 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001851 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001852 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001853
Chuck Leverb0d2e422014-08-22 15:10:59 -04001854 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001855 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001856 /*
1857 * This is a little silly; with sessions there's no real
1858 * use for the callback address. Use the peer address
1859 * as a reasonable default for now, but consider fixing
1860 * the rpc client not to require an address in the
1861 * future:
1862 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001863 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1864 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001865 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001866}
1867
Benny Halevy9089f1b2010-05-12 00:12:26 +03001868/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001869static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001870__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001871{
1872 struct nfsd4_session *elem;
1873 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001874 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001875
Trond Myklebust0a880a22014-07-30 08:27:10 -04001876 lockdep_assert_held(&nn->client_lock);
1877
Marc Eshel5282fd72009-04-03 08:27:52 +03001878 dump_sessionid(__func__, sessionid);
1879 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001880 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001881 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001882 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1883 NFS4_MAX_SESSIONID_LEN)) {
1884 return elem;
1885 }
1886 }
1887
1888 dprintk("%s: session not found\n", __func__);
1889 return NULL;
1890}
1891
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001892static struct nfsd4_session *
1893find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1894 __be32 *ret)
1895{
1896 struct nfsd4_session *session;
1897 __be32 status = nfserr_badsession;
1898
1899 session = __find_in_sessionid_hashtbl(sessionid, net);
1900 if (!session)
1901 goto out;
1902 status = nfsd4_get_session_locked(session);
1903 if (status)
1904 session = NULL;
1905out:
1906 *ret = status;
1907 return session;
1908}
1909
Benny Halevy9089f1b2010-05-12 00:12:26 +03001910/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001911static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001912unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001913{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001914 struct nfs4_client *clp = ses->se_client;
1915 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1916
1917 lockdep_assert_held(&nn->client_lock);
1918
Andy Adamson7116ed62009-04-03 08:27:43 +03001919 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001920 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001921 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001922 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001923}
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1926static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001927STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001929 /*
1930 * We're assuming the clid was not given out from a boot
1931 * precisely 2^32 (about 136 years) before this one. That seems
1932 * a safe assumption:
1933 */
1934 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return 0;
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04001936 trace_nfsd_clid_stale(clid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return 1;
1938}
1939
1940/*
1941 * XXX Should we use a slab cache ?
1942 * This type of memory management is somewhat inefficient, but we use it
1943 * anyway since SETCLIENTID is not a common operation.
1944 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001945static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001948 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Jeff Layton9258a2d2018-03-16 09:47:22 -04001950 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001951 if (clp == NULL)
1952 return NULL;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04001953 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001954 if (clp->cl_name.data == NULL)
1955 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001956 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1957 sizeof(struct list_head),
1958 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001959 if (!clp->cl_ownerstr_hashtbl)
1960 goto err_no_hashtbl;
1961 for (i = 0; i < OWNER_HASH_SIZE; i++)
1962 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
Trond Myklebust5694c932014-04-18 14:43:56 -04001963 INIT_LIST_HEAD(&clp->cl_sessions);
1964 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001965 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04001966 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1967 INIT_LIST_HEAD(&clp->cl_idhash);
1968 INIT_LIST_HEAD(&clp->cl_openowners);
1969 INIT_LIST_HEAD(&clp->cl_delegations);
1970 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001971 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001972#ifdef CONFIG_NFSD_PNFS
1973 INIT_LIST_HEAD(&clp->cl_lo_states);
1974#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001975 INIT_LIST_HEAD(&clp->async_copies);
1976 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001977 spin_lock_init(&clp->cl_lock);
1978 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001980err_no_hashtbl:
1981 kfree(clp->cl_name.data);
1982err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001983 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001984 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001987static void __free_client(struct kref *k)
1988{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001989 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1990 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001991
1992 free_svc_cred(&clp->cl_cred);
1993 kfree(clp->cl_ownerstr_hashtbl);
1994 kfree(clp->cl_name.data);
J. Bruce Fields79123442019-06-05 12:42:05 -04001995 kfree(clp->cl_nii_domain.data);
1996 kfree(clp->cl_nii_name.data);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001997 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001998 kmem_cache_free(client_slab, clp);
1999}
2000
YueHaibing297e57a2019-07-08 15:29:33 +08002001static void drop_client(struct nfs4_client *clp)
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002002{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002003 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002004}
2005
Trond Myklebust4dd86e152014-04-18 14:43:58 -04002006static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007free_client(struct nfs4_client *clp)
2008{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002009 while (!list_empty(&clp->cl_sessions)) {
2010 struct nfsd4_session *ses;
2011 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2012 se_perclnt);
2013 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002014 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2015 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002016 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04002017 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002018 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002019 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002020 clp->cl_nfsd_dentry = NULL;
2021 wake_up_all(&expiry_wq);
2022 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002023 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Benny Halevy84d38ac2010-05-12 00:13:16 +03002026/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04002027static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03002028unhash_client_locked(struct nfs4_client *clp)
2029{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002030 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002031 struct nfsd4_session *ses;
2032
Trond Myklebust0a880a22014-07-30 08:27:10 -04002033 lockdep_assert_held(&nn->client_lock);
2034
Trond Myklebust4beb3452014-07-30 08:27:02 -04002035 /* Mark the client as expired! */
2036 clp->cl_time = 0;
2037 /* Make it invisible */
2038 if (!list_empty(&clp->cl_idhash)) {
2039 list_del_init(&clp->cl_idhash);
2040 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2041 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2042 else
2043 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2044 }
2045 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002046 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002047 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2048 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002049 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03002050}
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04002053unhash_client(struct nfs4_client *clp)
2054{
2055 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2056
2057 spin_lock(&nn->client_lock);
2058 unhash_client_locked(clp);
2059 spin_unlock(&nn->client_lock);
2060}
2061
Jeff Layton97403d92014-07-30 08:27:12 -04002062static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2063{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04002064 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04002065 return nfserr_jukebox;
2066 unhash_client_locked(clp);
2067 return nfs_ok;
2068}
2069
Trond Myklebust4beb3452014-07-30 08:27:02 -04002070static void
2071__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002073 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002074 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 struct list_head reaplist;
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04002079 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07002080 while (!list_empty(&clp->cl_delegations)) {
2081 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04002082 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04002083 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
Benny Halevycdc975052014-05-30 09:09:30 -04002085 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 while (!list_empty(&reaplist)) {
2087 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04002088 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05002089 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04002091 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05002092 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04002093 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04002094 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002095 }
NeilBrownea1da632005-06-23 22:04:17 -07002096 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002097 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002098 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002099 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002101 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2102 struct nfs4_stateowner *so, *tmp;
2103
2104 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2105 so_strhash) {
2106 /* Should be no openowners at this point */
2107 WARN_ON_ONCE(so->so_is_open_owner);
2108 remove_blocked_locks(lockowner(so));
2109 }
2110 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002111 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002112 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002113 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002114 if (clp->cl_cb_conn.cb_xprt)
2115 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002116 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002117 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
Trond Myklebust4beb3452014-07-30 08:27:02 -04002120static void
2121destroy_client(struct nfs4_client *clp)
2122{
2123 unhash_client(clp);
2124 __destroy_client(clp);
2125}
2126
Scott Mayhew362063a2019-03-26 18:06:28 -04002127static void inc_reclaim_complete(struct nfs4_client *clp)
2128{
2129 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2130
2131 if (!nn->track_reclaim_completes)
2132 return;
2133 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2134 return;
2135 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2136 nn->reclaim_str_hashtbl_size) {
2137 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2138 clp->net->ns.inum);
2139 nfsd4_end_grace(nn);
2140 }
2141}
2142
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002143static void expire_client(struct nfs4_client *clp)
2144{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002145 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002146 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002147 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002148}
2149
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002150static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2151{
2152 memcpy(target->cl_verifier.data, source->data,
2153 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002156static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2157{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2159 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2160}
2161
J. Bruce Fields50043852015-11-20 15:58:37 -05002162static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2163{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002164 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2165 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2166 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002167 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2168 if ((source->cr_principal && !target->cr_principal) ||
2169 (source->cr_raw_principal && !target->cr_raw_principal) ||
2170 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002171 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002172
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002173 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 target->cr_uid = source->cr_uid;
2175 target->cr_gid = source->cr_gid;
2176 target->cr_group_info = source->cr_group_info;
2177 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002178 target->cr_gss_mech = source->cr_gss_mech;
2179 if (source->cr_gss_mech)
2180 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002184static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002185compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2186{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002187 if (o1->len < o2->len)
2188 return -1;
2189 if (o1->len > o2->len)
2190 return 1;
2191 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002192}
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002195same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2196{
2197 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
2200static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002201same_clid(clientid_t *cl1, clientid_t *cl2)
2202{
2203 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002206static bool groups_equal(struct group_info *g1, struct group_info *g2)
2207{
2208 int i;
2209
2210 if (g1->ngroups != g2->ngroups)
2211 return false;
2212 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002213 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002214 return false;
2215 return true;
2216}
2217
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002218/*
2219 * RFC 3530 language requires clid_inuse be returned when the
2220 * "principal" associated with a requests differs from that previously
2221 * used. We use uid, gid's, and gss principal string as our best
2222 * approximation. We also don't want to allow non-gss use of a client
2223 * established using gss: in theory cr_principal should catch that
2224 * change, but in practice cr_principal can be null even in the gss case
2225 * since gssd doesn't always pass down a principal string.
2226 */
2227static bool is_gss_cred(struct svc_cred *cr)
2228{
2229 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2230 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2231}
2232
2233
Vivek Trivedi5559b502012-07-24 21:18:20 +05302234static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002235same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2236{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002237 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002238 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2239 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002240 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2241 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002242 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002243 if (cr1->cr_principal == cr2->cr_principal)
2244 return true;
2245 if (!cr1->cr_principal || !cr2->cr_principal)
2246 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302247 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
J. Bruce Fields57266a62013-04-13 14:27:29 -04002250static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2251{
2252 struct svc_cred *cr = &rqstp->rq_cred;
2253 u32 service;
2254
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002255 if (!cr->cr_gss_mech)
2256 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002257 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2258 return service == RPC_GSS_SVC_INTEGRITY ||
2259 service == RPC_GSS_SVC_PRIVACY;
2260}
2261
Andrew Elblededeb132016-06-15 12:52:08 -04002262bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002263{
2264 struct svc_cred *cr = &rqstp->rq_cred;
2265
2266 if (!cl->cl_mach_cred)
2267 return true;
2268 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2269 return false;
2270 if (!svc_rqst_integrity_protected(rqstp))
2271 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002272 if (cl->cl_cred.cr_raw_principal)
2273 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2274 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002275 if (!cr->cr_principal)
2276 return false;
2277 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2278}
2279
Jeff Layton294ac322014-07-30 08:27:15 -04002280static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002281{
Chuck Leverab4684d2012-03-02 17:13:50 -05002282 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Jeff Laytonf4199922014-06-17 07:44:11 -04002284 /*
2285 * This is opaque to client, so no need to byte-swap. Use
2286 * __force to keep sparse happy
2287 */
Arnd Bergmann9104ae42019-11-04 16:45:30 +01002288 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002289 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002290 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
Jeff Layton294ac322014-07-30 08:27:15 -04002293static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2294{
Arnd Bergmann9cc76802019-10-31 15:53:13 +01002295 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
Jeff Layton294ac322014-07-30 08:27:15 -04002296 clp->cl_clientid.cl_id = nn->clientid_counter++;
2297 gen_confirm(clp, nn);
2298}
2299
Jeff Layton4770d722014-07-29 21:34:10 -04002300static struct nfs4_stid *
2301find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002302{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002303 struct nfs4_stid *ret;
2304
2305 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2306 if (!ret || !ret->sc_type)
2307 return NULL;
2308 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002309}
2310
Jeff Layton4770d722014-07-29 21:34:10 -04002311static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002312find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002313{
2314 struct nfs4_stid *s;
2315
Jeff Layton4770d722014-07-29 21:34:10 -04002316 spin_lock(&cl->cl_lock);
2317 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002318 if (s != NULL) {
2319 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002320 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002321 else
2322 s = NULL;
2323 }
Jeff Layton4770d722014-07-29 21:34:10 -04002324 spin_unlock(&cl->cl_lock);
2325 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002326}
2327
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002328static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2329{
2330 struct nfsdfs_client *nc;
2331 nc = get_nfsdfs_client(inode);
2332 if (!nc)
2333 return NULL;
2334 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2335}
2336
J. Bruce Fields169319f2019-06-19 12:39:46 -04002337static void seq_quote_mem(struct seq_file *m, char *data, int len)
2338{
2339 seq_printf(m, "\"");
2340 seq_escape_mem_ascii(m, data, len);
2341 seq_printf(m, "\"");
2342}
2343
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002344static int client_info_show(struct seq_file *m, void *v)
2345{
2346 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002347 struct nfs4_client *clp;
2348 u64 clid;
2349
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002350 clp = get_nfsdfs_clp(inode);
2351 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002352 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002353 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2354 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002355 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
NeilBrown472d1552021-03-20 09:38:04 +11002356 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2357 seq_puts(m, "status: confirmed\n");
2358 else
2359 seq_puts(m, "status: unconfirmed\n");
J. Bruce Fields169319f2019-06-19 12:39:46 -04002360 seq_printf(m, "name: ");
2361 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2362 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields79123442019-06-05 12:42:05 -04002363 if (clp->cl_nii_domain.data) {
2364 seq_printf(m, "Implementation domain: ");
2365 seq_quote_mem(m, clp->cl_nii_domain.data,
2366 clp->cl_nii_domain.len);
2367 seq_printf(m, "\nImplementation name: ");
2368 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
Arnd Bergmanne29f4702019-10-31 14:52:43 +01002369 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
J. Bruce Fields79123442019-06-05 12:42:05 -04002370 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2371 }
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002372 drop_client(clp);
2373
2374 return 0;
2375}
2376
2377static int client_info_open(struct inode *inode, struct file *file)
2378{
2379 return single_open(file, client_info_show, inode);
2380}
2381
2382static const struct file_operations client_info_fops = {
2383 .open = client_info_open,
2384 .read = seq_read,
2385 .llseek = seq_lseek,
2386 .release = single_release,
2387};
2388
J. Bruce Fields78599c42019-04-22 15:26:23 -04002389static void *states_start(struct seq_file *s, loff_t *pos)
2390 __acquires(&clp->cl_lock)
2391{
2392 struct nfs4_client *clp = s->private;
2393 unsigned long id = *pos;
2394 void *ret;
2395
2396 spin_lock(&clp->cl_lock);
2397 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2398 *pos = id;
2399 return ret;
2400}
2401
2402static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2403{
2404 struct nfs4_client *clp = s->private;
2405 unsigned long id = *pos;
2406 void *ret;
2407
2408 id = *pos;
2409 id++;
2410 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2411 *pos = id;
2412 return ret;
2413}
2414
2415static void states_stop(struct seq_file *s, void *v)
2416 __releases(&clp->cl_lock)
2417{
2418 struct nfs4_client *clp = s->private;
2419
2420 spin_unlock(&clp->cl_lock);
2421}
2422
Achilles Gaikwad580da462020-04-20 18:20:31 +05302423static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2424{
2425 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2426}
2427
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002428static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002429{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002430 struct inode *inode = f->nf_inode;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002431
2432 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2433 MAJOR(inode->i_sb->s_dev),
2434 MINOR(inode->i_sb->s_dev),
2435 inode->i_ino);
2436}
2437
2438static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2439{
2440 seq_printf(s, "owner: ");
2441 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2442}
2443
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002444static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2445{
J. Bruce Fieldsee590d22020-04-13 22:03:06 -04002446 seq_printf(s, "0x%.8x", stid->si_generation);
2447 seq_printf(s, "%12phN", &stid->si_opaque);
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002448}
2449
J. Bruce Fields78599c42019-04-22 15:26:23 -04002450static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2451{
2452 struct nfs4_ol_stateid *ols;
2453 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002454 struct nfsd_file *file;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002455 struct nfs4_stateowner *oo;
2456 unsigned int access, deny;
2457
2458 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2459 return 0; /* XXX: or SEQ_SKIP? */
2460 ols = openlockstateid(st);
2461 oo = ols->st_stateowner;
2462 nf = st->sc_file;
2463 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002464 if (!file)
2465 return 0;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002466
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002467 seq_printf(s, "- ");
2468 nfs4_show_stateid(s, &st->sc_stateid);
2469 seq_printf(s, ": { type: open, ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002470
2471 access = bmap_to_share_mode(ols->st_access_bmap);
2472 deny = bmap_to_share_mode(ols->st_deny_bmap);
2473
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002474 seq_printf(s, "access: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002475 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2476 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002477 seq_printf(s, "deny: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002478 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2479 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2480
2481 nfs4_show_superblock(s, file);
2482 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302483 nfs4_show_fname(s, file);
2484 seq_printf(s, ", ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002485 nfs4_show_owner(s, oo);
2486 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002487 nfsd_file_put(file);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002488
2489 return 0;
2490}
2491
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002492static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2493{
2494 struct nfs4_ol_stateid *ols;
2495 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002496 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002497 struct nfs4_stateowner *oo;
2498
2499 ols = openlockstateid(st);
2500 oo = ols->st_stateowner;
2501 nf = st->sc_file;
2502 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002503 if (!file)
2504 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002505
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002506 seq_printf(s, "- ");
2507 nfs4_show_stateid(s, &st->sc_stateid);
2508 seq_printf(s, ": { type: lock, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002509
2510 /*
2511 * Note: a lock stateid isn't really the same thing as a lock,
2512 * it's the locking state held by one owner on a file, and there
2513 * may be multiple (or no) lock ranges associated with it.
2514 * (Same for the matter is true of open stateids.)
2515 */
2516
2517 nfs4_show_superblock(s, file);
2518 /* XXX: open stateid? */
2519 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302520 nfs4_show_fname(s, file);
2521 seq_printf(s, ", ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002522 nfs4_show_owner(s, oo);
2523 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002524 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002525
2526 return 0;
2527}
2528
2529static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2530{
2531 struct nfs4_delegation *ds;
2532 struct nfs4_file *nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002533 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002534
2535 ds = delegstateid(st);
2536 nf = st->sc_file;
J. Bruce Fields9affa432020-07-15 13:31:36 -04002537 file = find_deleg_file(nf);
2538 if (!file)
2539 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002540
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002541 seq_printf(s, "- ");
2542 nfs4_show_stateid(s, &st->sc_stateid);
2543 seq_printf(s, ": { type: deleg, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002544
2545 /* Kinda dead code as long as we only support read delegs: */
2546 seq_printf(s, "access: %s, ",
2547 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2548
2549 /* XXX: lease time, whether it's being recalled. */
2550
2551 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302552 seq_printf(s, ", ");
2553 nfs4_show_fname(s, file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002554 seq_printf(s, " }\n");
J. Bruce Fields9affa432020-07-15 13:31:36 -04002555 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002556
2557 return 0;
2558}
2559
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002560static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2561{
2562 struct nfs4_layout_stateid *ls;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002563 struct nfsd_file *file;
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002564
2565 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2566 file = ls->ls_file;
2567
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002568 seq_printf(s, "- ");
2569 nfs4_show_stateid(s, &st->sc_stateid);
2570 seq_printf(s, ": { type: layout, ");
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002571
2572 /* XXX: What else would be useful? */
2573
2574 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302575 seq_printf(s, ", ");
2576 nfs4_show_fname(s, file);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002577 seq_printf(s, " }\n");
2578
2579 return 0;
2580}
2581
J. Bruce Fields78599c42019-04-22 15:26:23 -04002582static int states_show(struct seq_file *s, void *v)
2583{
2584 struct nfs4_stid *st = v;
2585
2586 switch (st->sc_type) {
2587 case NFS4_OPEN_STID:
2588 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002589 case NFS4_LOCK_STID:
2590 return nfs4_show_lock(s, st);
2591 case NFS4_DELEG_STID:
2592 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002593 case NFS4_LAYOUT_STID:
2594 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002595 default:
2596 return 0; /* XXX: or SEQ_SKIP? */
2597 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002598 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002599}
2600
2601static struct seq_operations states_seq_ops = {
2602 .start = states_start,
2603 .next = states_next,
2604 .stop = states_stop,
2605 .show = states_show
2606};
2607
2608static int client_states_open(struct inode *inode, struct file *file)
2609{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002610 struct seq_file *s;
2611 struct nfs4_client *clp;
2612 int ret;
2613
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002614 clp = get_nfsdfs_clp(inode);
2615 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002616 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002617
2618 ret = seq_open(file, &states_seq_ops);
2619 if (ret)
2620 return ret;
2621 s = file->private_data;
2622 s->private = clp;
2623 return 0;
2624}
2625
2626static int client_opens_release(struct inode *inode, struct file *file)
2627{
2628 struct seq_file *m = file->private_data;
2629 struct nfs4_client *clp = m->private;
2630
2631 /* XXX: alternatively, we could get/drop in seq start/stop */
2632 drop_client(clp);
2633 return 0;
2634}
2635
2636static const struct file_operations client_states_fops = {
2637 .open = client_states_open,
2638 .read = seq_read,
2639 .llseek = seq_lseek,
2640 .release = client_opens_release,
2641};
2642
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002643/*
2644 * Normally we refuse to destroy clients that are in use, but here the
2645 * administrator is telling us to just do it. We also want to wait
2646 * so the caller has a guarantee that the client's locks are gone by
2647 * the time the write returns:
2648 */
YueHaibing297e57a2019-07-08 15:29:33 +08002649static void force_expire_client(struct nfs4_client *clp)
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002650{
2651 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2652 bool already_expired;
2653
2654 spin_lock(&clp->cl_lock);
2655 clp->cl_time = 0;
2656 spin_unlock(&clp->cl_lock);
2657
2658 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2659 spin_lock(&nn->client_lock);
2660 already_expired = list_empty(&clp->cl_lru);
2661 if (!already_expired)
2662 unhash_client_locked(clp);
2663 spin_unlock(&nn->client_lock);
2664
2665 if (!already_expired)
2666 expire_client(clp);
2667 else
2668 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2669}
2670
2671static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2672 size_t size, loff_t *pos)
2673{
2674 char *data;
2675 struct nfs4_client *clp;
2676
2677 data = simple_transaction_get(file, buf, size);
2678 if (IS_ERR(data))
2679 return PTR_ERR(data);
2680 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2681 return -EINVAL;
2682 clp = get_nfsdfs_clp(file_inode(file));
2683 if (!clp)
2684 return -ENXIO;
2685 force_expire_client(clp);
2686 drop_client(clp);
2687 return 7;
2688}
2689
2690static const struct file_operations client_ctl_fops = {
2691 .write = client_ctl_write,
2692 .release = simple_transaction_release,
2693};
2694
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002695static const struct tree_descr client_files[] = {
2696 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002697 [1] = {"states", &client_states_fops, S_IRUSR},
Petr Vorel6cbfad52020-03-13 13:39:57 +01002698 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002699 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002700};
2701
Jeff Layton2216d442012-11-12 15:00:57 -05002702static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002703 struct svc_rqst *rqstp, nfs4_verifier *verf)
2704{
2705 struct nfs4_client *clp;
2706 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002707 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002708 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002709 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
NeilBrown472d1552021-03-20 09:38:04 +11002710 struct dentry *dentries[ARRAY_SIZE(client_files)];
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002711
2712 clp = alloc_client(name);
2713 if (clp == NULL)
2714 return NULL;
2715
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002716 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2717 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002718 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002719 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002720 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002721 gen_clid(clp, nn);
2722 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002723 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01002724 clp->cl_time = ktime_get_boottime_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002725 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002726 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002727 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002728 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002729 clp->net = net;
NeilBrown472d1552021-03-20 09:38:04 +11002730 clp->cl_nfsd_dentry = nfsd_client_mkdir(
2731 nn, &clp->cl_nfsdfs,
2732 clp->cl_clientid.cl_id - nn->clientid_base,
2733 client_files, dentries);
2734 clp->cl_nfsd_info_dentry = dentries[0];
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002735 if (!clp->cl_nfsd_dentry) {
2736 free_client(clp);
2737 return NULL;
2738 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002739 return clp;
2740}
2741
NeilBrownfd39ca92005-06-23 22:04:03 -07002742static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002743add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2744{
2745 struct rb_node **new = &(root->rb_node), *parent = NULL;
2746 struct nfs4_client *clp;
2747
2748 while (*new) {
2749 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2750 parent = *new;
2751
2752 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2753 new = &((*new)->rb_left);
2754 else
2755 new = &((*new)->rb_right);
2756 }
2757
2758 rb_link_node(&new_clp->cl_namenode, parent, new);
2759 rb_insert_color(&new_clp->cl_namenode, root);
2760}
2761
2762static struct nfs4_client *
2763find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2764{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002765 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002766 struct rb_node *node = root->rb_node;
2767 struct nfs4_client *clp;
2768
2769 while (node) {
2770 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2771 cmp = compare_blob(&clp->cl_name, name);
2772 if (cmp > 0)
2773 node = node->rb_left;
2774 else if (cmp < 0)
2775 node = node->rb_right;
2776 else
2777 return clp;
2778 }
2779 return NULL;
2780}
2781
2782static void
2783add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
2785 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002786 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Trond Myklebust0a880a22014-07-30 08:27:10 -04002788 lockdep_assert_held(&nn->client_lock);
2789
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002790 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002791 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002793 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002794 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795}
2796
NeilBrownfd39ca92005-06-23 22:04:03 -07002797static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798move_to_confirmed(struct nfs4_client *clp)
2799{
2800 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002801 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Trond Myklebust0a880a22014-07-30 08:27:10 -04002803 lockdep_assert_held(&nn->client_lock);
2804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002806 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002807 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002808 add_clp_to_name_tree(clp, &nn->conf_name_tree);
NeilBrown472d1552021-03-20 09:38:04 +11002809 if (!test_and_set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags) &&
2810 clp->cl_nfsd_dentry &&
2811 clp->cl_nfsd_info_dentry)
2812 fsnotify_dentry(clp->cl_nfsd_info_dentry, FS_MODIFY);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002813 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
2816static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002817find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
2819 struct nfs4_client *clp;
2820 unsigned int idhashval = clientid_hashval(clid->cl_id);
2821
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002822 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002823 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002824 if ((bool)clp->cl_minorversion != sessions)
2825 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002826 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830 return NULL;
2831}
2832
2833static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002834find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2835{
2836 struct list_head *tbl = nn->conf_id_hashtbl;
2837
Trond Myklebust0a880a22014-07-30 08:27:10 -04002838 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002839 return find_client_in_id_table(tbl, clid, sessions);
2840}
2841
2842static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002843find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002845 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Trond Myklebust0a880a22014-07-30 08:27:10 -04002847 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002848 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002851static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002852{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002853 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002854}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002855
NeilBrown28ce6052005-06-23 22:03:56 -07002856static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002857find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002858{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002859 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002860 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002861}
2862
2863static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002864find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002865{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002866 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002867 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002868}
2869
NeilBrownfd39ca92005-06-23 22:04:03 -07002870static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002871gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002873 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002874 struct sockaddr *sa = svc_addr(rqstp);
2875 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002876 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Jeff Layton7077ecb2009-08-14 12:57:58 -04002878 /* Currently, we only support tcp and tcp6 for the callback channel */
2879 if (se->se_callback_netid_len == 3 &&
2880 !memcmp(se->se_callback_netid_val, "tcp", 3))
2881 expected_family = AF_INET;
2882 else if (se->se_callback_netid_len == 4 &&
2883 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2884 expected_family = AF_INET6;
2885 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 goto out_err;
2887
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002888 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002889 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002890 (struct sockaddr *)&conn->cb_addr,
2891 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002892
J. Bruce Fields07263f12010-05-31 19:09:40 -04002893 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002895
J. Bruce Fields07263f12010-05-31 19:09:40 -04002896 if (conn->cb_addr.ss_family == AF_INET6)
2897 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002898
J. Bruce Fields07263f12010-05-31 19:09:40 -04002899 conn->cb_prog = se->se_callback_prog;
2900 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002901 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Chuck Lever1eace0d2020-04-05 14:15:29 -04002902 trace_nfsd_cb_args(clp, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return;
2904out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002905 conn->cb_addr.ss_family = AF_UNSPEC;
2906 conn->cb_addrlen = 0;
Chuck Lever1eace0d2020-04-05 14:15:29 -04002907 trace_nfsd_cb_nodelegs(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return;
2909}
2910
Andy Adamson074fe892009-04-03 08:28:15 +03002911/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002912 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002913 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002914static void
Andy Adamson074fe892009-04-03 08:28:15 +03002915nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2916{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04002917 struct xdr_buf *buf = resp->xdr->buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002918 struct nfsd4_slot *slot = resp->cstate.slot;
2919 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002920
Andy Adamson557ce262009-08-28 08:45:04 -04002921 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002922
J. Bruce Fields085def32017-10-18 16:17:18 -04002923 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002924 slot->sl_opcnt = resp->opcnt;
2925 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002926 free_svc_cred(&slot->sl_cred);
2927 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002928
J. Bruce Fields085def32017-10-18 16:17:18 -04002929 if (!nfsd4_cache_this(resp)) {
2930 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002931 return;
2932 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002933 slot->sl_flags |= NFSD4_SLOT_CACHED;
2934
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002935 base = resp->cstate.data_offset;
2936 slot->sl_datalen = buf->len - base;
2937 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002938 WARN(1, "%s: sessions DRC could not cache compound\n",
2939 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002940 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002941}
2942
2943/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002944 * Encode the replay sequence operation from the slot values.
2945 * If cachethis is FALSE encode the uncached rep error on the next
2946 * operation which sets resp->p and increments resp->opcnt for
2947 * nfs4svc_encode_compoundres.
2948 *
Andy Adamson074fe892009-04-03 08:28:15 +03002949 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002950static __be32
2951nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2952 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002953{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002954 struct nfsd4_op *op;
2955 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002956
Andy Adamsonabfabf82009-07-23 19:02:18 -04002957 /* Encode the replayed sequence operation */
2958 op = &args->ops[resp->opcnt - 1];
2959 nfsd4_encode_operation(resp, op);
2960
J. Bruce Fields085def32017-10-18 16:17:18 -04002961 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2962 return op->status;
2963 if (args->opcnt == 1) {
2964 /*
2965 * The original operation wasn't a solo sequence--we
2966 * always cache those--so this retry must not match the
2967 * original:
2968 */
2969 op->status = nfserr_seq_false_retry;
2970 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002971 op = &args->ops[resp->opcnt++];
2972 op->status = nfserr_retry_uncached_rep;
2973 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002974 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002975 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002976}
2977
2978/*
Andy Adamson557ce262009-08-28 08:45:04 -04002979 * The sequence operation is not cached because we can use the slot and
2980 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002981 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002982static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002983nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2984 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002985{
Andy Adamson557ce262009-08-28 08:45:04 -04002986 struct nfsd4_slot *slot = resp->cstate.slot;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04002987 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002988 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002989 __be32 status;
2990
Andy Adamson557ce262009-08-28 08:45:04 -04002991 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002992
Andy Adamsonabfabf82009-07-23 19:02:18 -04002993 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002994 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002995 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002996
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002997 p = xdr_reserve_space(xdr, slot->sl_datalen);
2998 if (!p) {
2999 WARN_ON_ONCE(1);
3000 return nfserr_serverfault;
3001 }
3002 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3003 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03003004
Andy Adamson557ce262009-08-28 08:45:04 -04003005 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04003006 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03003007}
3008
Andy Adamson0733d212009-04-03 08:28:01 +03003009/*
3010 * Set the exchange_id flags returned by the server.
3011 */
3012static void
3013nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3014{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003015#ifdef CONFIG_NFSD_PNFS
3016 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3017#else
Andy Adamson0733d212009-04-03 08:28:01 +03003018 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003019#endif
Andy Adamson0733d212009-04-03 08:28:01 +03003020
3021 /* Referrals are supported, Migration is not. */
3022 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3023
3024 /* set the wire flags to return to client. */
3025 clid->flags = new->cl_exchange_flags;
3026}
3027
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003028static bool client_has_openowners(struct nfs4_client *clp)
3029{
3030 struct nfs4_openowner *oo;
3031
3032 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3033 if (!list_empty(&oo->oo_owner.so_stateids))
3034 return true;
3035 }
3036 return false;
3037}
3038
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003039static bool client_has_state(struct nfs4_client *clp)
3040{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003041 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08003042#ifdef CONFIG_NFSD_PNFS
3043 || !list_empty(&clp->cl_lo_states)
3044#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04003045 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04003046 || !list_empty(&clp->cl_sessions)
3047 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003048}
3049
J. Bruce Fields79123442019-06-05 12:42:05 -04003050static __be32 copy_impl_id(struct nfs4_client *clp,
3051 struct nfsd4_exchange_id *exid)
3052{
3053 if (!exid->nii_domain.data)
3054 return 0;
3055 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3056 if (!clp->cl_nii_domain.data)
3057 return nfserr_jukebox;
3058 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3059 if (!clp->cl_nii_name.data)
3060 return nfserr_jukebox;
Arnd Bergmanne29f4702019-10-31 14:52:43 +01003061 clp->cl_nii_time = exid->nii_time;
J. Bruce Fields79123442019-06-05 12:42:05 -04003062 return 0;
3063}
3064
Al Virob37ad282006-10-19 23:28:59 -07003065__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003066nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3067 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003068{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003069 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003070 struct nfs4_client *conf, *new;
3071 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003072 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04003073 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03003074 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04003075 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003076 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003077 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03003078
Jeff Layton363168b2009-08-14 12:57:56 -04003079 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03003080 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Chuck Lever523ec6e2020-11-02 15:19:12 -05003081 "ip_addr=%s flags %x, spa_how %u\n",
Andy Adamson0733d212009-04-03 08:28:01 +03003082 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04003083 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03003084
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003085 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03003086 return nfserr_inval;
3087
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003088 new = create_client(exid->clname, rqstp, &verf);
3089 if (new == NULL)
3090 return nfserr_jukebox;
J. Bruce Fields79123442019-06-05 12:42:05 -04003091 status = copy_impl_id(new, exid);
3092 if (status)
3093 goto out_nolock;
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003094
Andy Adamson0733d212009-04-03 08:28:01 +03003095 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003096 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04003097 exid->spo_must_enforce[0] = 0;
3098 exid->spo_must_enforce[1] = (
3099 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3100 1 << (OP_EXCHANGE_ID - 32) |
3101 1 << (OP_CREATE_SESSION - 32) |
3102 1 << (OP_DESTROY_SESSION - 32) |
3103 1 << (OP_DESTROY_CLIENTID - 32));
3104
3105 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3106 1 << (OP_OPEN_DOWNGRADE) |
3107 1 << (OP_LOCKU) |
3108 1 << (OP_DELEGRETURN));
3109
3110 exid->spo_must_allow[1] &= (
3111 1 << (OP_TEST_STATEID - 32) |
3112 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003113 if (!svc_rqst_integrity_protected(rqstp)) {
3114 status = nfserr_inval;
3115 goto out_nolock;
3116 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003117 /*
3118 * Sometimes userspace doesn't give us a principal.
3119 * Which is a bug, really. Anyway, we can't enforce
3120 * MACH_CRED in that case, better to give up now:
3121 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05003122 if (!new->cl_cred.cr_principal &&
3123 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003124 status = nfserr_serverfault;
3125 goto out_nolock;
3126 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003127 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03003128 case SP4_NONE:
3129 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003130 default: /* checked by xdr code */
3131 WARN_ON_ONCE(1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003132 fallthrough;
Andy Adamson0733d212009-04-03 08:28:01 +03003133 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08003134 status = nfserr_encr_alg_unsupp;
3135 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03003136 }
3137
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003138 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003139 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003140 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03003141 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003142 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3143 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3144
J. Bruce Fields136e6582012-05-12 20:37:23 -04003145 if (update) {
3146 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003147 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03003148 goto out;
3149 }
Andrew Elblededeb132016-06-15 12:52:08 -04003150 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003151 status = nfserr_wrong_cred;
3152 goto out;
3153 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003154 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03003155 status = nfserr_perm;
3156 goto out;
3157 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003158 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03003159 status = nfserr_not_same;
3160 goto out;
3161 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003162 /* case 6 */
3163 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003164 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003165 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003166 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003167 if (client_has_state(conf)) {
3168 status = nfserr_clid_inuse;
3169 goto out;
3170 }
Andy Adamson0733d212009-04-03 08:28:01 +03003171 goto out_new;
3172 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003173 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003174 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003175 goto out_copy;
3176 }
3177 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003178 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003179 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003180 }
3181
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003182 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003183 status = nfserr_noent;
3184 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003185 }
3186
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003187 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003188 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003189 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003190
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003191 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03003192out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003193 if (conf) {
3194 status = mark_client_expired_locked(conf);
3195 if (status)
3196 goto out;
3197 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003198 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003199 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3200 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003201
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003202 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003203 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003204out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003205 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3206 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003207
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003208 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3209 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003210
3211 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003212 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003213 status = nfs_ok;
3214
3215out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003216 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003217out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003218 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003219 expire_client(new);
3220 if (unconf)
3221 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003222 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003223}
3224
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003225static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003226check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003227{
Andy Adamson88e588d2009-07-23 19:02:15 -04003228 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3229 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003230
3231 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003232 if (slot_inuse) {
3233 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003234 return nfserr_jukebox;
3235 else
3236 return nfserr_seq_misordered;
3237 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003238 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003239 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003240 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003241 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003242 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003243 return nfserr_seq_misordered;
3244}
3245
Andy Adamson49557cc2009-07-23 19:02:16 -04003246/*
3247 * Cache the create session result into the create session single DRC
3248 * slot cache by saving the xdr structure. sl_seqid has been set.
3249 * Do this for solo or embedded create session operations.
3250 */
3251static void
3252nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003253 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003254{
3255 slot->sl_status = nfserr;
3256 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3257}
3258
3259static __be32
3260nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3261 struct nfsd4_clid_slot *slot)
3262{
3263 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3264 return slot->sl_status;
3265}
3266
Mi Jinlong1b74c252011-07-14 14:50:17 +08003267#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3268 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3269 1 + /* MIN tag is length with zero, only length */ \
3270 3 + /* version, opcount, opcode */ \
3271 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3272 /* seqid, slotID, slotID, cache */ \
3273 4 ) * sizeof(__be32))
3274
3275#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3276 2 + /* verifier: AUTH_NULL, length 0 */\
3277 1 + /* status */ \
3278 1 + /* MIN tag is length with zero, only length */ \
3279 3 + /* opcount, opcode, opstatus*/ \
3280 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3281 /* seqid, slotID, slotID, slotID, status */ \
3282 5 ) * sizeof(__be32))
3283
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003284static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003285{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003286 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3287
J. Bruce Fields373cd402013-04-08 15:42:12 -04003288 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3289 return nfserr_toosmall;
3290 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3291 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003292 ca->headerpadsz = 0;
3293 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3294 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3295 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3296 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3297 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3298 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3299 /*
3300 * Note decreasing slot size below client's request may make it
3301 * difficult for client to function correctly, whereas
3302 * decreasing the number of slots will (just?) affect
3303 * performance. When short on memory we therefore prefer to
3304 * decrease number of slots instead of their size. Clients that
3305 * request larger slots than they need will get poor results:
NeilBrown7f49fd5d2019-09-20 16:33:16 +10003306 * Note that we always allow at least one slot, because our
3307 * accounting is soft and provides no guarantees either way.
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003308 */
NeilBrown2030ca52019-09-20 16:36:45 +10003309 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003310
J. Bruce Fields373cd402013-04-08 15:42:12 -04003311 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003312}
3313
Chuck Lever45006322016-03-01 13:06:02 -05003314/*
3315 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3316 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3317 */
3318#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3319 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3320
3321#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3322 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3323
Kinglong Mee8a891632013-12-23 18:11:02 +08003324#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003325 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003326#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003327 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3328 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003329
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003330static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3331{
3332 ca->headerpadsz = 0;
3333
Kinglong Mee8a891632013-12-23 18:11:02 +08003334 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003335 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003336 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003337 return nfserr_toosmall;
3338 ca->maxresp_cached = 0;
3339 if (ca->maxops < 2)
3340 return nfserr_toosmall;
3341
3342 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003343}
3344
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003345static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3346{
3347 switch (cbs->flavor) {
3348 case RPC_AUTH_NULL:
3349 case RPC_AUTH_UNIX:
3350 return nfs_ok;
3351 default:
3352 /*
3353 * GSS case: the spec doesn't allow us to return this
3354 * error. But it also doesn't allow us not to support
3355 * GSS.
3356 * I'd rather this fail hard than return some error the
3357 * client might think it can already handle:
3358 */
3359 return nfserr_encr_alg_unsupp;
3360 }
3361}
3362
Andy Adamson069b6ad2009-04-03 08:27:58 +03003363__be32
3364nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003365 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003366{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003367 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003368 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003369 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003370 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003371 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003372 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003373 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003374 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003375 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003376
Mi Jinlonga62573d2011-03-23 17:57:07 +08003377 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3378 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003379 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3380 if (status)
3381 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003382 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003383 if (status)
3384 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003385 status = check_backchannel_attrs(&cr_ses->back_channel);
3386 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003387 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003388 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003389 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003390 if (!new)
3391 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003392 conn = alloc_conn_from_crses(rqstp, cr_ses);
3393 if (!conn)
3394 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003395
Jeff Laytond20c11d2014-07-30 08:27:07 -04003396 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003397 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003398 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003399 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003400
3401 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003402 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003403 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003404 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003405 cs_slot = &conf->cl_cs_slot;
3406 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003407 if (status) {
3408 if (status == nfserr_replay_cache)
3409 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003410 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003411 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003412 } else if (unconf) {
3413 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003414 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003415 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003416 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003417 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003418 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003419 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003420 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003421 cs_slot = &unconf->cl_cs_slot;
3422 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003423 if (status) {
3424 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003425 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003426 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003427 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003428 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003429 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003430 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003431 if (status) {
3432 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003433 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003434 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003435 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003436 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003437 conf = unconf;
3438 } else {
3439 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003440 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003441 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003442 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003443 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003444 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003445 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003446 cr_ses->flags &= ~SESSION4_RDMA;
3447
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003448 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003449 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003450
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003451 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003452 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003453 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003454 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003455
Jeff Laytond20c11d2014-07-30 08:27:07 -04003456 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003457 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003458 spin_unlock(&nn->client_lock);
3459 /* init connection and backchannel */
3460 nfsd4_init_conn(rqstp, conn, new);
3461 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003462 if (old)
3463 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003464 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003465out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003466 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003467 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003468 if (old)
3469 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003470out_free_session:
3471 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003472out_release_drc_mem:
3473 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003474 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003475}
3476
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003477static __be32 nfsd4_map_bcts_dir(u32 *dir)
3478{
3479 switch (*dir) {
3480 case NFS4_CDFC4_FORE:
3481 case NFS4_CDFC4_BACK:
3482 return nfs_ok;
3483 case NFS4_CDFC4_FORE_OR_BOTH:
3484 case NFS4_CDFC4_BACK_OR_BOTH:
3485 *dir = NFS4_CDFC4_BOTH;
3486 return nfs_ok;
zhengbinfc5fc5d2019-12-19 17:29:20 +08003487 }
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003488 return nfserr_inval;
3489}
3490
Christoph Hellwigeb698532017-05-08 20:58:35 +02003491__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3492 struct nfsd4_compound_state *cstate,
3493 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003494{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003495 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003496 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003497 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003498 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003499
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003500 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3501 if (status)
3502 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003503 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003504 session->se_cb_prog = bc->bc_cb_program;
3505 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003506 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003507
3508 nfsd4_probe_callback(session->se_client);
3509
3510 return nfs_ok;
3511}
3512
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003513static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3514{
3515 struct nfsd4_conn *c;
3516
3517 list_for_each_entry(c, &s->se_conns, cn_persession) {
3518 if (c->cn_xprt == xpt) {
3519 return c;
3520 }
3521 }
3522 return NULL;
3523}
3524
3525static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3526 struct nfsd4_session *session, u32 req)
3527{
3528 struct nfs4_client *clp = session->se_client;
3529 struct svc_xprt *xpt = rqst->rq_xprt;
3530 struct nfsd4_conn *c;
3531 __be32 status;
3532
3533 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3534 spin_lock(&clp->cl_lock);
3535 c = __nfsd4_find_conn(xpt, session);
3536 if (!c)
3537 status = nfserr_noent;
3538 else if (req == c->cn_flags)
3539 status = nfs_ok;
3540 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3541 c->cn_flags != NFS4_CDFC4_BACK)
3542 status = nfs_ok;
3543 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3544 c->cn_flags != NFS4_CDFC4_FORE)
3545 status = nfs_ok;
3546 else
3547 status = nfserr_inval;
3548 spin_unlock(&clp->cl_lock);
3549 return status;
3550}
3551
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003552__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3553 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003554 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003555{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003556 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003557 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003558 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003559 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003560 struct net *net = SVC_NET(rqstp);
3561 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003562
3563 if (!nfsd4_last_compound_op(rqstp))
3564 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003565 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003566 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003567 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003568 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003569 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003570 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003571 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003572 goto out;
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003573 status = nfsd4_match_existing_connection(rqstp, session, bcts->dir);
3574 if (status == nfs_ok || status == nfserr_inval)
3575 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003576 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003577 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003578 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003579 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003580 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003581 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003582 goto out;
3583 nfsd4_init_conn(rqstp, conn, session);
3584 status = nfs_ok;
3585out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003586 nfsd4_put_session(session);
3587out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003588 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003589}
3590
J. Bruce Fields665d5072018-04-11 16:53:36 -04003591static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003592{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003593 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003594 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003595 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003596}
3597
Andy Adamson069b6ad2009-04-03 08:27:58 +03003598__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003599nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3600 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003601{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003602 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003603 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003604 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003605 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003606 struct net *net = SVC_NET(r);
3607 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003608
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003609 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003610 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003611 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003612 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003613 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003614 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003615 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003616 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003617 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003618 if (!ses)
3619 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003620 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003621 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003622 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003623 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003624 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003625 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003626 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003627 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003628
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003629 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003630
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003631 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003632 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003633out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003634 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003635out_client_lock:
3636 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003637out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003638 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003639}
3640
J. Bruce Fields57266a62013-04-13 14:27:29 -04003641static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003642{
3643 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003644 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003645 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003646 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003647
3648 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003649 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003650 if (c)
3651 goto out_free;
3652 status = nfserr_conn_not_bound_to_session;
3653 if (clp->cl_mach_cred)
3654 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003655 __nfsd4_hash_conn(new, ses);
3656 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003657 ret = nfsd4_register_conn(new);
3658 if (ret)
3659 /* oops; xprt is already down: */
3660 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003661 return nfs_ok;
3662out_free:
3663 spin_unlock(&clp->cl_lock);
3664 free_conn(new);
3665 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003666}
3667
Mi Jinlong868b89c2011-04-27 09:09:58 +08003668static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3669{
3670 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3671
3672 return args->opcnt > session->se_fchannel.maxops;
3673}
3674
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003675static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3676 struct nfsd4_session *session)
3677{
3678 struct xdr_buf *xb = &rqstp->rq_arg;
3679
3680 return xb->len > session->se_fchannel.maxreq_sz;
3681}
3682
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003683static bool replay_matches_cache(struct svc_rqst *rqstp,
3684 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3685{
3686 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3687
3688 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3689 (bool)seq->cachethis)
3690 return false;
3691 /*
Scott Mayhew6e73e922019-10-09 15:11:37 -04003692 * If there's an error then the reply can have fewer ops than
3693 * the call.
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003694 */
Scott Mayhew6e73e922019-10-09 15:11:37 -04003695 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3696 return false;
3697 /*
3698 * But if we cached a reply with *more* ops than the call you're
3699 * sending us now, then this new call is clearly not really a
3700 * replay of the old one:
3701 */
3702 if (slot->sl_opcnt > argp->opcnt)
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003703 return false;
3704 /* This is the only check explicitly called by spec: */
3705 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3706 return false;
3707 /*
3708 * There may be more comparisons we could actually do, but the
3709 * spec doesn't require us to catch every case where the calls
3710 * don't match (that would require caching the call as well as
3711 * the reply), so we don't bother.
3712 */
3713 return true;
3714}
3715
Andy Adamson069b6ad2009-04-03 08:27:58 +03003716__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003717nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3718 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003719{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003720 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003721 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003722 struct xdr_stream *xdr = resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003723 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003724 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003725 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003726 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003727 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003728 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003729 struct net *net = SVC_NET(rqstp);
3730 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003731
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003732 if (resp->opcnt != 1)
3733 return nfserr_sequence_pos;
3734
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003735 /*
3736 * Will be either used or freed by nfsd4_sequence_check_conn
3737 * below.
3738 */
3739 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3740 if (!conn)
3741 return nfserr_jukebox;
3742
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003743 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003744 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003745 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003746 goto out_no_session;
3747 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003748
Mi Jinlong868b89c2011-04-27 09:09:58 +08003749 status = nfserr_too_many_ops;
3750 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003751 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003752
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003753 status = nfserr_req_too_big;
3754 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003755 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003756
Benny Halevyb85d4c02009-04-03 08:28:08 +03003757 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003758 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003759 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003760
Andy Adamson557ce262009-08-28 08:45:04 -04003761 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003762 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3763
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003764 /* We do not negotiate the number of slots yet, so set the
3765 * maxslots to the session maxreqs which is used to encode
3766 * sr_highest_slotid and the sr_target_slot id to maxslots */
3767 seq->maxslots = session->se_fchannel.maxreqs;
3768
J. Bruce Fields73e79482012-02-13 16:39:00 -05003769 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3770 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003771 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003772 status = nfserr_seq_misordered;
3773 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003774 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003775 status = nfserr_seq_false_retry;
3776 if (!replay_matches_cache(rqstp, seq, slot))
3777 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003778 cstate->slot = slot;
3779 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003780 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003781 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003782 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003783 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003784 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003785 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003786 }
3787 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003788 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003789
J. Bruce Fields57266a62013-04-13 14:27:29 -04003790 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003791 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003792 if (status)
3793 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003794
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003795 buflen = (seq->cachethis) ?
3796 session->se_fchannel.maxresp_cached :
3797 session->se_fchannel.maxresp_sz;
3798 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3799 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003800 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003801 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003802 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003803
3804 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003805 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003806 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003807 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003808 if (seq->cachethis)
3809 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003810 else
3811 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003812
3813 cstate->slot = slot;
3814 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003815 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003816
Benny Halevyb85d4c02009-04-03 08:28:08 +03003817out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003818 switch (clp->cl_cb_state) {
3819 case NFSD4_CB_DOWN:
3820 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3821 break;
3822 case NFSD4_CB_FAULT:
3823 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3824 break;
3825 default:
3826 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003827 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003828 if (!list_empty(&clp->cl_revoked))
3829 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003830out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003831 if (conn)
3832 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003833 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003834 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003835out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003836 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003837 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003838}
3839
Trond Myklebustb6076642014-06-30 11:48:35 -04003840void
3841nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3842{
3843 struct nfsd4_compound_state *cs = &resp->cstate;
3844
3845 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003846 if (cs->status != nfserr_replay_cache) {
3847 nfsd4_store_cache_entry(resp);
3848 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3849 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003850 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003851 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003852 } else if (cs->clp)
3853 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003854}
3855
Mi Jinlong345c2842011-10-20 17:51:39 +08003856__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003857nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3858 struct nfsd4_compound_state *cstate,
3859 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003860{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003861 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003862 struct nfs4_client *conf, *unconf;
3863 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003864 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003865 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003866
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003867 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003868 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003869 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003870 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003871
3872 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003873 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003874 status = nfserr_clientid_busy;
3875 goto out;
3876 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003877 status = mark_client_expired_locked(conf);
3878 if (status)
3879 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003880 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003881 } else if (unconf)
3882 clp = unconf;
3883 else {
3884 status = nfserr_stale_clientid;
3885 goto out;
3886 }
Andrew Elblededeb132016-06-15 12:52:08 -04003887 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003888 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003889 status = nfserr_wrong_cred;
3890 goto out;
3891 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003892 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003893out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003894 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003895 if (clp)
3896 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003897 return status;
3898}
3899
Andy Adamson069b6ad2009-04-03 08:27:58 +03003900__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003901nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3902 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003903{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003904 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003905 struct nfs4_client *clp = cstate->clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003906 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003907
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003908 if (rc->rca_one_fs) {
3909 if (!cstate->current_fh.fh_dentry)
3910 return nfserr_nofilehandle;
3911 /*
3912 * We don't take advantage of the rca_one_fs case.
3913 * That's OK, it's optional, we can safely ignore it.
3914 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003915 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003916 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003917
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003918 status = nfserr_complete_already;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003919 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003920 goto out;
3921
3922 status = nfserr_stale_clientid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003923 if (is_client_expired(clp))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003924 /*
3925 * The following error isn't really legal.
3926 * But we only get here if the client just explicitly
3927 * destroyed the client. Surely it no longer cares what
3928 * error it gets back on an operation for the dead
3929 * client.
3930 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003931 goto out;
3932
3933 status = nfs_ok;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003934 nfsd4_client_record_create(clp);
3935 inc_reclaim_complete(clp);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003936out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003937 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003938}
3939
3940__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003941nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003942 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003944 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003945 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003947 struct nfs4_client *conf, *new;
3948 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003949 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003950 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3951
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003952 new = create_client(clname, rqstp, &clverifier);
3953 if (new == NULL)
3954 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003955 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003956 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003957 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003958 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003959 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003961 if (clp_used_exchangeid(conf))
3962 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003963 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04003964 trace_nfsd_clid_inuse_err(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 goto out;
3966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003968 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003969 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003970 unhash_client_locked(unconf);
Chuck Lever45f56da2020-04-05 14:40:25 -04003971 /* We need to handle only case 1: probable callback update */
Kinglong Mee41eb1672015-07-13 17:29:41 +08003972 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003974 gen_confirm(new, nn);
Chuck Lever45f56da2020-04-05 14:40:25 -04003975 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003976 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003977 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003978 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3980 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3981 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003982 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 status = nfs_ok;
3984out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003985 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003986 if (new)
3987 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003988 if (unconf)
3989 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 return status;
3991}
3992
3993
Al Virob37ad282006-10-19 23:28:59 -07003994__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003995nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003996 struct nfsd4_compound_state *cstate,
3997 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003999 struct nfsd4_setclientid_confirm *setclientid_confirm =
4000 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07004001 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04004002 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4004 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07004005 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03004006 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004008 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07004010
Jeff Laytond20c11d2014-07-30 08:27:07 -04004011 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004012 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004013 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004014 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04004015 * We try hard to give out unique clientid's, so if we get an
4016 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004017 * the client may be buggy; this should never happen.
4018 *
4019 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004020 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004021 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04004022 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
4023 goto out;
4024 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
4025 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04004026 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004027 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04004028 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4029 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04004031 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004032 status = nfserr_stale_clientid;
4033 goto out;
4034 }
4035 status = nfs_ok;
4036 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04004037 old = unconf;
4038 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004039 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004040 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04004041 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4042 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04004043 status = nfserr_clid_inuse;
4044 if (client_has_state(old)
4045 && !same_creds(&unconf->cl_cred,
4046 &old->cl_cred))
4047 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04004048 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04004049 if (status) {
4050 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04004051 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04004052 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004053 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04004054 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04004055 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07004056 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04004057 get_client_locked(conf);
4058 spin_unlock(&nn->client_lock);
4059 nfsd4_probe_callback(conf);
4060 spin_lock(&nn->client_lock);
4061 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04004063 spin_unlock(&nn->client_lock);
4064 if (old)
4065 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 return status;
4067}
4068
J. Bruce Fields32513b42011-10-13 16:00:16 -04004069static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070{
J. Bruce Fields32513b42011-10-13 16:00:16 -04004071 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4072}
4073
4074/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04004075static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
4076 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04004077{
Trond Myklebust950e0112014-06-30 11:48:31 -04004078 lockdep_assert_held(&state_lock);
4079
Elena Reshetova818a34e2017-10-20 12:53:30 +03004080 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004081 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04004082 INIT_LIST_HEAD(&fp->fi_stateids);
4083 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004084 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04004085 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04004086 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004087 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004088 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004089 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4090 memset(fp->fi_access, 0, sizeof(fp->fi_access));
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 *
Jeff Layton5b095e92014-10-23 08:01:02 -04004429find_file_locked(struct knfsd_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)) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02004435 if (fh_match(&fp->fi_fhandle, fh)) {
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
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02004443struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04004444find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004445{
4446 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004447 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004448
Jeff Layton5b095e92014-10-23 08:01:02 -04004449 rcu_read_lock();
4450 fp = find_file_locked(fh, hashval);
4451 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004452 return fp;
4453}
4454
4455static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004456find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004457{
4458 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004459 unsigned int hashval = file_hashval(fh);
4460
4461 rcu_read_lock();
4462 fp = find_file_locked(fh, hashval);
4463 rcu_read_unlock();
4464 if (fp)
4465 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004466
4467 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04004468 fp = find_file_locked(fh, hashval);
4469 if (likely(fp == NULL)) {
4470 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04004471 fp = new;
4472 }
4473 spin_unlock(&state_lock);
4474
4475 return fp;
4476}
4477
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004478/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 * Called to check deny when READ with all zero stateid or
4480 * WRITE with all zero or all one stateid
4481 */
Al Virob37ad282006-10-19 23:28:59 -07004482static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4484{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004486 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Trond Myklebustca943212014-07-23 16:17:39 -04004488 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07004489 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004490 return ret;
4491 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004492 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004493 if (fp->fi_share_deny & deny_type)
4494 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004495 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004496 put_nfs4_file(fp);
4497 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498}
4499
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004500static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004502 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004503 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4504 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004505
Trond Myklebust11b91642014-07-29 21:34:08 -04004506 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004507
Jeff Layton02e12152014-07-16 10:31:57 -04004508 /*
4509 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004510 * already holding inode->i_lock.
4511 *
Jeff Laytondff13992014-07-08 14:02:49 -04004512 * If the dl_time != 0, then we know that it has already been
4513 * queued for a lease break. Don't queue it again.
4514 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004515 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004516 if (dp->dl_time == 0) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004517 dp->dl_time = ktime_get_boottime_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004518 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004519 }
Jeff Layton02e12152014-07-16 10:31:57 -04004520 spin_unlock(&state_lock);
4521}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004523static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4524 struct rpc_task *task)
4525{
4526 struct nfs4_delegation *dp = cb_to_delegation(cb);
4527
J. Bruce Fields12ed22f2020-07-31 16:20:05 -04004528 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4529 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
Andrew Elblea4579742015-08-31 12:06:41 -04004530 return 1;
4531
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004532 switch (task->tk_status) {
4533 case 0:
4534 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004535 case -NFS4ERR_DELAY:
4536 rpc_delay(task, 2 * HZ);
4537 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004538 case -EBADHANDLE:
4539 case -NFS4ERR_BAD_STATEID:
4540 /*
4541 * Race: client probably got cb_recall before open reply
4542 * granting delegation.
4543 */
4544 if (dp->dl_retries--) {
4545 rpc_delay(task, 2 * HZ);
4546 return 0;
4547 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004548 fallthrough;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004549 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004550 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004551 }
4552}
4553
4554static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4555{
4556 struct nfs4_delegation *dp = cb_to_delegation(cb);
4557
4558 nfs4_put_stid(&dp->dl_stid);
4559}
4560
Julia Lawallc4cb8972015-11-21 22:57:39 +01004561static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004562 .prepare = nfsd4_cb_recall_prepare,
4563 .done = nfsd4_cb_recall_done,
4564 .release = nfsd4_cb_recall_release,
4565};
4566
Jeff Layton02e12152014-07-16 10:31:57 -04004567static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4568{
4569 /*
4570 * We're assuming the state code never drops its reference
4571 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004572 * callback (and since the lease code is serialized by the
4573 * i_lock) we know the server hasn't removed the lease yet, and
4574 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004575 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004576 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004577 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004578}
4579
Jeff Layton1c8c6012013-06-21 08:58:15 -04004580/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004581static bool
4582nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004583{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004584 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004585 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4586 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004587
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04004588 trace_nfsd_deleg_break(&dp->dl_stid.sc_stateid);
4589
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004590 /*
4591 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004592 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004593 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004594 */
4595 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
Jeff Layton02e12152014-07-16 10:31:57 -04004597 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004598 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004599 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004600 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004601 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602}
4603
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004604static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4605{
4606 struct nfs4_delegation *dl = fl->fl_owner;
4607 struct svc_rqst *rqst;
4608 struct nfs4_client *clp;
4609
4610 if (!i_am_nfsd())
4611 return NULL;
4612 rqst = kthread_data(current);
J. Bruce Fields13956162020-09-25 10:12:39 -04004613 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4614 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
J. Bruce Fields34b09af2020-08-05 15:10:11 -04004615 return NULL;
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004616 clp = *(rqst->rq_lease_breaker);
4617 return dl->dl_stid.sc_client == clp;
4618}
4619
Jeff Laytonc45198e2014-09-01 07:12:07 -04004620static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004621nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4622 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623{
4624 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004625 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 else
4627 return -EAGAIN;
4628}
4629
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004630static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004631 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004632 .lm_break = nfsd_break_deleg_cb,
4633 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634};
4635
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004636static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4637{
4638 if (nfsd4_has_session(cstate))
4639 return nfs_ok;
4640 if (seqid == so->so_seqid - 1)
4641 return nfserr_replay_me;
4642 if (seqid == so->so_seqid)
4643 return nfs_ok;
4644 return nfserr_bad_seqid;
4645}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
J. Bruce Fields7950b532021-01-21 17:57:41 -05004647static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
4648 struct nfsd_net *nn)
4649{
4650 struct nfs4_client *found;
4651
4652 spin_lock(&nn->client_lock);
4653 found = find_confirmed_client(clid, sessions, nn);
4654 if (found)
4655 atomic_inc(&found->cl_rpc_users);
4656 spin_unlock(&nn->client_lock);
4657 return found;
4658}
4659
J. Bruce Fields460d2702021-01-21 17:57:40 -05004660static __be32 set_client(clientid_t *clid,
Jeff Layton4b24ca72014-06-30 11:48:44 -04004661 struct nfsd4_compound_state *cstate,
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004662 struct nfsd_net *nn)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004663{
Jeff Layton4b24ca72014-06-30 11:48:44 -04004664 if (cstate->clp) {
J. Bruce Fields7950b532021-01-21 17:57:41 -05004665 if (!same_clid(&cstate->clp->cl_clientid, clid))
Jeff Layton4b24ca72014-06-30 11:48:44 -04004666 return nfserr_stale_clientid;
4667 return nfs_ok;
4668 }
Jeff Layton4b24ca72014-06-30 11:48:44 -04004669 if (STALE_CLIENTID(clid, nn))
4670 return nfserr_stale_clientid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004671 /*
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004672 * We're in the 4.0 case (otherwise the SEQUENCE op would have
4673 * set cstate->clp), so session = false:
Jeff Layton4b24ca72014-06-30 11:48:44 -04004674 */
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004675 cstate->clp = lookup_clientid(clid, false, nn);
J. Bruce Fields7950b532021-01-21 17:57:41 -05004676 if (!cstate->clp)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004677 return nfserr_expired;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004678 return nfs_ok;
4679}
4680
Al Virob37ad282006-10-19 23:28:59 -07004681__be32
Andy Adamson66689582009-04-03 08:28:45 +03004682nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004683 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 clientid_t *clientid = &open->op_clientid;
4686 struct nfs4_client *clp = NULL;
4687 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004688 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004689 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
J. Bruce Fields32513b42011-10-13 16:00:16 -04004691 /*
4692 * In case we need it later, after we've already created the
4693 * file and don't want to risk a further failure:
4694 */
4695 open->op_file = nfsd4_alloc_file();
4696 if (open->op_file == NULL)
4697 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004699 status = set_client(clientid, cstate, nn);
Trond Myklebust2d91e892014-06-30 11:48:46 -04004700 if (status)
4701 return status;
4702 clp = cstate->clp;
4703
Trond Myklebustd4f04892014-07-29 21:34:36 -04004704 strhashval = ownerstr_hashval(&open->op_owner);
4705 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004706 open->op_openowner = oo;
4707 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004708 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004710 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004711 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004712 release_openowner(oo);
4713 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004714 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004715 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004716 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4717 if (status)
4718 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004719 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004720new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004721 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004722 if (oo == NULL)
4723 return nfserr_jukebox;
4724 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004725alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004726 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004727 if (!open->op_stp)
4728 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004729
4730 if (nfsd4_has_session(cstate) &&
4731 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4732 open->op_odstate = alloc_clnt_odstate(clp);
4733 if (!open->op_odstate)
4734 return nfserr_jukebox;
4735 }
4736
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004737 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738}
4739
Al Virob37ad282006-10-19 23:28:59 -07004740static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004741nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4742{
4743 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4744 return nfserr_openmode;
4745 else
4746 return nfs_ok;
4747}
4748
Daniel Mackc47d8322011-05-16 16:38:14 +02004749static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004750{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004751 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4752}
4753
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004754static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004755{
4756 struct nfs4_stid *ret;
4757
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004758 ret = find_stateid_by_type(cl, s,
4759 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004760 if (!ret)
4761 return NULL;
4762 return delegstateid(ret);
4763}
4764
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004765static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4766{
4767 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4768 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4769}
4770
Al Virob37ad282006-10-19 23:28:59 -07004771static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004772nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004773 struct nfs4_delegation **dp)
4774{
4775 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004776 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004777 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004778
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004779 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4780 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004781 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004782 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4783 nfs4_put_stid(&deleg->dl_stid);
4784 if (cl->cl_minorversion)
4785 status = nfserr_deleg_revoked;
4786 goto out;
4787 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004788 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004789 status = nfs4_check_delegmode(deleg, flags);
4790 if (status) {
4791 nfs4_put_stid(&deleg->dl_stid);
4792 goto out;
4793 }
4794 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004795out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004796 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004797 return nfs_ok;
4798 if (status)
4799 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004800 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004801 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004802}
4803
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004804static inline int nfs4_access_to_access(u32 nfs4_access)
4805{
4806 int flags = 0;
4807
4808 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4809 flags |= NFSD_MAY_READ;
4810 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4811 flags |= NFSD_MAY_WRITE;
4812 return flags;
4813}
4814
Al Virob37ad282006-10-19 23:28:59 -07004815static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4817 struct nfsd4_open *open)
4818{
4819 struct iattr iattr = {
4820 .ia_valid = ATTR_SIZE,
4821 .ia_size = 0,
4822 };
4823 if (!open->op_truncate)
4824 return 0;
4825 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004826 return nfserr_inval;
Arnd Bergmann2a1aa482019-11-03 17:50:18 +01004827 return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828}
4829
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004830static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004831 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4832 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004833{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004834 struct nfsd_file *nf = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004835 __be32 status;
4836 int oflag = nfs4_access_to_omode(open->op_share_access);
4837 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004838 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004839
Trond Myklebustde186432014-07-10 14:07:26 -04004840 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004841
4842 /*
4843 * Are we trying to set a deny mode that would conflict with
4844 * current access?
4845 */
4846 status = nfs4_file_check_deny(fp, open->op_share_deny);
4847 if (status != nfs_ok) {
4848 spin_unlock(&fp->fi_lock);
4849 goto out;
4850 }
4851
4852 /* set access to the file */
4853 status = nfs4_file_get_access(fp, open->op_share_access);
4854 if (status != nfs_ok) {
4855 spin_unlock(&fp->fi_lock);
4856 goto out;
4857 }
4858
4859 /* Set access bits in stateid */
4860 old_access_bmap = stp->st_access_bmap;
4861 set_access(open->op_share_access, stp);
4862
4863 /* Set new deny mask */
4864 old_deny_bmap = stp->st_deny_bmap;
4865 set_deny(open->op_share_deny, stp);
4866 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4867
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004868 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004869 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004870 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004871 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004872 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004873 spin_lock(&fp->fi_lock);
4874 if (!fp->fi_fds[oflag]) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004875 fp->fi_fds[oflag] = nf;
4876 nf = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -04004877 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004878 }
Trond Myklebustde186432014-07-10 14:07:26 -04004879 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004880 if (nf)
4881 nfsd_file_put(nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004882
4883 status = nfsd4_truncate(rqstp, cur_fh, open);
4884 if (status)
4885 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004886out:
4887 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004888out_put_access:
4889 stp->st_access_bmap = old_access_bmap;
4890 nfs4_file_put_access(fp, open->op_share_access);
4891 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4892 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004893}
4894
Al Virob37ad282006-10-19 23:28:59 -07004895static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004896nfs4_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 -07004897{
Al Virob37ad282006-10-19 23:28:59 -07004898 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004899 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004901 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004902 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004903
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004904 /* test and set deny mode */
4905 spin_lock(&fp->fi_lock);
4906 status = nfs4_file_check_deny(fp, open->op_share_deny);
4907 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004908 set_deny(open->op_share_deny, stp);
4909 fp->fi_share_deny |=
4910 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4911 }
4912 spin_unlock(&fp->fi_lock);
4913
4914 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004917 status = nfsd4_truncate(rqstp, cur_fh, open);
4918 if (status != nfs_ok)
4919 reset_union_bmap_deny(old_deny_bmap, stp);
4920 return status;
4921}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004923/* Should we give out recallable state?: */
4924static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4925{
4926 if (clp->cl_cb_state == NFSD4_CB_UP)
4927 return true;
4928 /*
4929 * In the sessions case, since we don't have to establish a
4930 * separate connection for callbacks, we assume it's OK
4931 * until we hear otherwise:
4932 */
4933 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4934}
4935
J. Bruce Fields653e5142018-02-15 22:08:45 -05004936static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4937 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004938{
4939 struct file_lock *fl;
4940
4941 fl = locks_alloc_lock();
4942 if (!fl)
4943 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004944 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004945 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004946 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4947 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004948 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004949 fl->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004950 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004951 return fl;
4952}
4953
Jeff Layton0b266932014-07-25 07:34:25 -04004954static struct nfs4_delegation *
4955nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004956 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004957{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004958 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004959 struct nfs4_delegation *dp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004960 struct nfsd_file *nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004961 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004962
J. Bruce Fields353601e2018-02-16 14:29:42 -05004963 /*
4964 * The fi_had_conflict and nfs_get_existing_delegation checks
4965 * here are just optimizations; we'll need to recheck them at
4966 * the end:
4967 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004968 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004969 return ERR_PTR(-EAGAIN);
4970
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004971 nf = find_readable_file(fp);
4972 if (!nf) {
J. Bruce Fields6ee65a772021-03-08 10:52:29 -05004973 /* We should always have a readable file here */
4974 WARN_ON_ONCE(1);
4975 return ERR_PTR(-EBADF);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004976 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004977 spin_lock(&state_lock);
4978 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004979 if (nfs4_delegation_exists(clp, fp))
4980 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004981 else if (!fp->fi_deleg_file) {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004982 fp->fi_deleg_file = nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004983 /* increment early to prevent fi_deleg_file from being
4984 * cleared */
4985 fp->fi_delegees = 1;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004986 nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004987 } else
4988 fp->fi_delegees++;
4989 spin_unlock(&fp->fi_lock);
4990 spin_unlock(&state_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004991 if (nf)
4992 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004993 if (status)
4994 return ERR_PTR(status);
4995
4996 status = -ENOMEM;
4997 dp = alloc_init_deleg(clp, fp, fh, odstate);
4998 if (!dp)
4999 goto out_delegees;
5000
5001 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5002 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04005003 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05005004
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005005 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005006 if (fl)
5007 locks_free_lock(fl);
5008 if (status)
5009 goto out_clnt_odstate;
5010
5011 spin_lock(&state_lock);
5012 spin_lock(&fp->fi_lock);
5013 if (fp->fi_had_conflict)
5014 status = -EAGAIN;
5015 else
5016 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04005017 spin_unlock(&fp->fi_lock);
5018 spin_unlock(&state_lock);
5019
5020 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04005021 goto out_unlock;
5022
Jeff Layton0b266932014-07-25 07:34:25 -04005023 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04005024out_unlock:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005025 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005026out_clnt_odstate:
5027 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005028 nfs4_put_stid(&dp->dl_stid);
5029out_delegees:
5030 put_deleg_file(fp);
5031 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05005032}
5033
Benny Halevy4aa89132012-02-21 14:16:44 -08005034static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5035{
5036 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5037 if (status == -EAGAIN)
5038 open->op_why_no_deleg = WND4_CONTENTION;
5039 else {
5040 open->op_why_no_deleg = WND4_RESOURCE;
5041 switch (open->op_deleg_want) {
5042 case NFS4_SHARE_WANT_READ_DELEG:
5043 case NFS4_SHARE_WANT_WRITE_DELEG:
5044 case NFS4_SHARE_WANT_ANY_DELEG:
5045 break;
5046 case NFS4_SHARE_WANT_CANCEL:
5047 open->op_why_no_deleg = WND4_CANCELLED;
5048 break;
5049 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005050 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08005051 }
5052 }
5053}
5054
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055/*
5056 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04005057 *
5058 * Note we don't support write delegations, and won't until the vfs has
5059 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 */
5061static void
Jeff Layton4cf59222014-07-25 07:34:24 -04005062nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
5063 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064{
5065 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04005066 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5067 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05005068 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005069 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005071 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07005072 open->op_recall = 0;
5073 switch (open->op_claim_type) {
5074 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05005075 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07005076 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005077 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5078 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005079 break;
5080 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00005081 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005082 /*
5083 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005084 * had the chance to reclaim theirs, *and* until
5085 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005086 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005087 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005088 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005089 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005090 goto out_no_deleg;
J. Bruce Fields6ee65a772021-03-08 10:52:29 -05005091 /*
5092 * Also, if the file was opened for write or
5093 * create, there's a good chance the client's
5094 * about to write to it, resulting in an
5095 * immediate recall (since we don't support
5096 * write delegations):
5097 */
5098 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
5099 goto out_no_deleg;
5100 if (open->op_create == NFS4_OPEN_CREATE)
5101 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005102 break;
5103 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005104 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005105 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005106 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04005107 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005108 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005110 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
Hou Tao3caf9172020-08-28 15:02:55 +08005112 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
J. Bruce Fields99c41512013-05-21 16:21:25 -04005113 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04005114 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005115 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005116out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005117 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5118 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005119 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04005120 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005121 open->op_recall = 1;
5122 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04005123
5124 /* 4.1 client asking for a delegation? */
5125 if (open->op_deleg_want)
5126 nfsd4_open_deleg_none_ext(open, status);
5127 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128}
5129
Benny Halevye27f49c2012-02-21 14:16:54 -08005130static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5131 struct nfs4_delegation *dp)
5132{
5133 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5134 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5135 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5136 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5137 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5138 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5139 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5140 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5141 }
5142 /* Otherwise the client must be confused wanting a delegation
5143 * it already has, therefore we don't return
5144 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5145 */
5146}
5147
Al Virob37ad282006-10-19 23:28:59 -07005148__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5150{
Andy Adamson66689582009-04-03 08:28:45 +03005151 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005152 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005154 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07005155 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005156 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005157 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 /*
5160 * Lookup file; if found, lookup stateid and check open request,
5161 * and check for delegations in the process of being recalled.
5162 * If not found, create the nfs4_file struct
5163 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005164 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04005165 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04005166 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07005167 if (status)
5168 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005169 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04005171 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07005172 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04005173 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07005174 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 }
5176
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005177 if (!stp) {
5178 stp = init_open_stateid(fp, open);
5179 if (!open->op_stp)
5180 new_stp = true;
5181 }
5182
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 /*
5184 * OPEN the file, or upgrade an existing OPEN.
5185 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005186 *
5187 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005189 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005191 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005192 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005193 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005197 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5198 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005199 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005200 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005201 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005202 goto out;
5203 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005204
5205 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5206 open->op_odstate);
5207 if (stp->st_clnt_odstate == open->op_odstate)
5208 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005210
Jeff Layton9767feb2015-10-01 09:05:50 -04005211 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005212 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
Benny Halevyd24433c2012-02-16 20:57:17 +02005214 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005215 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5216 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5217 open->op_why_no_deleg = WND4_NOT_WANTED;
5218 goto nodeleg;
5219 }
5220 }
5221
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 /*
5223 * Attempt to hand out a delegation. No error return, because the
5224 * OPEN succeeds even if we fail.
5225 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005226 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005227nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 status = nfs_ok;
Hou Tao3caf9172020-08-28 15:02:55 +08005229 trace_nfsd_open(&stp->st_stid.sc_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005231 /* 4.1 client trying to upgrade/downgrade delegation? */
5232 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005233 open->op_deleg_want)
5234 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005235
NeilBrown13cd2182005-06-23 22:03:10 -07005236 if (fp)
5237 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005238 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005239 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 /*
5241 * To finish the open response, we just need to set the rflags.
5242 */
5243 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005244 if (nfsd4_has_session(&resp->cstate))
5245 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5246 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005248
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005249 if (dp)
5250 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005251 if (stp)
5252 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
5254 return status;
5255}
5256
Jeff Layton58fb12e2014-07-29 21:34:27 -04005257void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005258 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005259{
5260 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005261 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005262
Jeff Laytond3134b12014-07-29 21:34:32 -04005263 nfsd4_cstate_assign_replay(cstate, so);
5264 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005265 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005266 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005267 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005268 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005269 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005270 if (open->op_odstate)
5271 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005272}
5273
Al Virob37ad282006-10-19 23:28:59 -07005274__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005275nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005276 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005278 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005280 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005281 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005283 trace_nfsd_clid_renew(clid);
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005284 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005285 if (status)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005286 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005287 clp = cstate->clp;
NeilBrownea1da632005-06-23 22:04:17 -07005288 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005289 && clp->cl_cb_state != NFSD4_CB_UP)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005290 return nfserr_cb_path_down;
5291 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292}
5293
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005294void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005295nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005296{
Jeff Layton33dcc482012-04-10 11:08:48 -04005297 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005298 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005299 return;
5300
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005301 trace_nfsd_grace_complete(nn);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005302 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005303 /*
5304 * If the server goes down again right now, an NFSv4
5305 * client will still be allowed to reclaim after it comes back up,
5306 * even if it hasn't yet had a chance to reclaim state this time.
5307 *
5308 */
Jeff Layton919b8042014-09-12 16:40:20 -04005309 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005310 /*
5311 * At this point, NFSv4 clients can still reclaim. But if the
5312 * server crashes, any that have not yet reclaimed will be out
5313 * of luck on the next boot.
5314 *
5315 * (NFSv4.1+ clients are considered to have reclaimed once they
5316 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5317 * have reclaimed after their first OPEN.)
5318 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005319 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005320 /*
5321 * At this point, and once lockd and/or any other containers
5322 * exit their grace period, further reclaims will fail and
5323 * regular locking can resume.
5324 */
NeilBrowna76b4312005-06-23 22:04:01 -07005325}
5326
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005327/*
5328 * If we've waited a lease period but there are still clients trying to
5329 * reclaim, wait a little longer to give them a chance to finish.
5330 */
5331static bool clients_still_reclaiming(struct nfsd_net *nn)
5332{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005333 time64_t double_grace_period_end = nn->boot_time +
5334 2 * nn->nfsd4_lease;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005335
Scott Mayhew362063a2019-03-26 18:06:28 -04005336 if (nn->track_reclaim_completes &&
5337 atomic_read(&nn->nr_reclaim_complete) ==
5338 nn->reclaim_str_hashtbl_size)
5339 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005340 if (!nn->somebody_reclaimed)
5341 return false;
5342 nn->somebody_reclaimed = false;
5343 /*
5344 * If we've given them *two* lease times to reclaim, and they're
5345 * still not done, give up:
5346 */
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005347 if (ktime_get_boottime_seconds() > double_grace_period_end)
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005348 return false;
5349 return true;
5350}
5351
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005352struct laundry_time {
5353 time64_t cutoff;
5354 time64_t new_timeo;
5355};
5356
5357static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5358{
5359 time64_t time_remaining;
5360
5361 if (last_refresh < lt->cutoff)
5362 return true;
5363 time_remaining = last_refresh - lt->cutoff;
5364 lt->new_timeo = min(lt->new_timeo, time_remaining);
5365 return false;
5366}
5367
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005368static time64_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005369nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370{
5371 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005372 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005374 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005375 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 struct list_head *pos, *next, reaplist;
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005377 struct laundry_time lt = {
5378 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
5379 .new_timeo = nn->nfsd4_lease
5380 };
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005381 struct nfs4_cpntf_state *cps;
5382 copy_stateid_t *cps_t;
5383 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005385 if (clients_still_reclaiming(nn)) {
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005386 lt.new_timeo = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005387 goto out;
5388 }
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005389 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005390 INIT_LIST_HEAD(&reaplist);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005391
5392 spin_lock(&nn->s2s_cp_lock);
5393 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5394 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5395 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005396 state_expired(&lt, cps->cpntf_time))
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005397 _free_cpntf_state_locked(nn, cps);
5398 }
5399 spin_unlock(&nn->s2s_cp_lock);
5400
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005401 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005402 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 clp = list_entry(pos, struct nfs4_client, cl_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005404 if (!state_expired(&lt, clp->cl_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 break;
J. Bruce Fields221a6872013-04-01 22:23:49 -04005406 if (mark_client_expired_locked(clp)) {
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005407 trace_nfsd_clid_expired(&clp->cl_clientid);
Benny Halevyd7682982010-05-12 00:13:54 +03005408 continue;
5409 }
Trond Myklebust4864af92014-07-30 08:27:03 -04005410 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005411 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005412 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005413 list_for_each_safe(pos, next, &reaplist) {
5414 clp = list_entry(pos, struct nfs4_client, cl_lru);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005415 trace_nfsd_clid_purged(&clp->cl_clientid);
Trond Myklebust4864af92014-07-30 08:27:03 -04005416 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 expire_client(clp);
5418 }
Benny Halevycdc975052014-05-30 09:09:30 -04005419 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005420 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005422 if (!state_expired(&lt, dp->dl_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 break;
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005424 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005425 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 }
Benny Halevycdc975052014-05-30 09:09:30 -04005427 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005428 while (!list_empty(&reaplist)) {
5429 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5430 dl_recall_lru);
5431 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005432 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 }
Jeff Layton217526e2014-07-30 08:27:11 -04005434
5435 spin_lock(&nn->client_lock);
5436 while (!list_empty(&nn->close_lru)) {
5437 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5438 oo_close_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005439 if (!state_expired(&lt, oo->oo_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 break;
Jeff Layton217526e2014-07-30 08:27:11 -04005441 list_del_init(&oo->oo_close_lru);
5442 stp = oo->oo_last_closed_stid;
5443 oo->oo_last_closed_stid = NULL;
5444 spin_unlock(&nn->client_lock);
5445 nfs4_put_stid(&stp->st_stid);
5446 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 }
Jeff Layton217526e2014-07-30 08:27:11 -04005448 spin_unlock(&nn->client_lock);
5449
Jeff Layton7919d0a2016-09-16 16:28:25 -04005450 /*
5451 * It's possible for a client to try and acquire an already held lock
5452 * that is being held for a long time, and then lose interest in it.
5453 * So, we clean out any un-revisited request after a lease period
5454 * under the assumption that the client is no longer interested.
5455 *
5456 * RFC5661, sec. 9.6 states that the client must not rely on getting
5457 * notifications and must continue to poll for locks, even when the
5458 * server supports them. Thus this shouldn't lead to clients blocking
5459 * indefinitely once the lock does become free.
5460 */
5461 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005462 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005463 while (!list_empty(&nn->blocked_locks_lru)) {
5464 nbl = list_first_entry(&nn->blocked_locks_lru,
5465 struct nfsd4_blocked_lock, nbl_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005466 if (!state_expired(&lt, nbl->nbl_time))
Jeff Layton7919d0a2016-09-16 16:28:25 -04005467 break;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005468 list_move(&nbl->nbl_lru, &reaplist);
5469 list_del_init(&nbl->nbl_list);
5470 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005471 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005472
5473 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005474 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005475 struct nfsd4_blocked_lock, nbl_lru);
5476 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005477 free_blocked_lock(nbl);
5478 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005479out:
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005480 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481}
5482
Harvey Harrisona254b242008-02-20 12:49:00 -08005483static struct workqueue_struct *laundry_wq;
5484static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005485
5486static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005487laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005489 time64_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005490 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005491 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5492 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005494 t = nfs4_laundromat(nn);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005495 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496}
5497
Jeff Layton8fcd4612015-07-30 06:57:46 -04005498static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005499{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005500 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005501 return nfserr_bad_stateid;
5502 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503}
5504
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005506access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005508 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5509 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5510 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511}
5512
5513static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005514access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005516 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5517 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518}
5519
5520static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005521__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522{
Al Virob37ad282006-10-19 23:28:59 -07005523 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524
J. Bruce Fields02921912010-07-29 15:16:59 -04005525 /* For lock stateid's, we test the parent open, not the lock: */
5526 if (stp->st_openstp)
5527 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005528 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005530 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 goto out;
5532 status = nfs_ok;
5533out:
5534 return status;
5535}
5536
Al Virob37ad282006-10-19 23:28:59 -07005537static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005538check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005540 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005542 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005543 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 * conflicting state; so we must wait out the grace period. */
5545 return nfserr_grace;
5546 } else if (flags & WR_STATE)
5547 return nfs4_share_conflict(current_fh,
5548 NFS4_SHARE_DENY_WRITE);
5549 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5550 return nfs4_share_conflict(current_fh,
5551 NFS4_SHARE_DENY_READ);
5552}
5553
5554/*
5555 * Allow READ/WRITE during grace period on recovered state only for files
5556 * that are not able to provide mandatory locking.
5557 */
5558static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005559grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005561 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562}
5563
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005564static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005565{
Andy Adamson66689582009-04-03 08:28:45 +03005566 /*
5567 * When sessions are used the stateid generation number is ignored
5568 * when it is zero.
5569 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005570 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005571 return nfs_ok;
5572
5573 if (in->si_generation == ref->si_generation)
5574 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005575
J. Bruce Fields0836f582008-01-26 19:08:12 -05005576 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005577 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005578 return nfserr_bad_stateid;
5579 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005580 * However, we could see a stateid from the past, even from a
5581 * non-buggy client. For example, if the client sends a lock
5582 * while some IO is outstanding, the lock may bump si_generation
5583 * while the IO is still in flight. The client could avoid that
5584 * situation by waiting for responses on all the IO requests,
5585 * but better performance may result in retrying IO that
5586 * receives an old_stateid error if requests are rarely
5587 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005588 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005589 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005590}
5591
Trond Myklebust03da3162017-11-03 08:00:16 -04005592static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5593{
5594 __be32 ret;
5595
5596 spin_lock(&s->sc_lock);
5597 ret = nfsd4_verify_open_stid(s);
5598 if (ret == nfs_ok)
5599 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5600 spin_unlock(&s->sc_lock);
5601 return ret;
5602}
5603
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005604static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5605{
5606 if (ols->st_stateowner->so_is_open_owner &&
5607 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5608 return nfserr_bad_stateid;
5609 return nfs_ok;
5610}
5611
Chuck Lever7df302f2012-05-29 13:56:37 -04005612static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005613{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005614 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005615 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005616
Andrew Elbleae254dac2017-11-09 13:41:10 -05005617 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5618 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005619 return status;
J. Bruce Fields663e36f2020-03-19 10:18:49 -04005620 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005621 return status;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005622 spin_lock(&cl->cl_lock);
5623 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005624 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005625 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005626 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005627 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005628 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005629 switch (s->sc_type) {
5630 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005631 status = nfs_ok;
5632 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005633 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005634 status = nfserr_deleg_revoked;
5635 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005636 case NFS4_OPEN_STID:
5637 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005638 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005639 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005640 default:
5641 printk("unknown stateid type %x\n", s->sc_type);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05005642 fallthrough;
J. Bruce Fields23340032013-04-09 17:42:28 -04005643 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005644 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005645 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005646 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005647out_unlock:
5648 spin_unlock(&cl->cl_lock);
5649 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005650}
5651
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005652__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005653nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5654 stateid_t *stateid, unsigned char typemask,
5655 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005656{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005657 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005658 bool return_revoked = false;
5659
5660 /*
5661 * only return revoked delegations if explicitly asked.
5662 * otherwise we report revoked or bad_stateid status.
5663 */
5664 if (typemask & NFS4_REVOKED_DELEG_STID)
5665 return_revoked = true;
5666 else if (typemask & NFS4_DELEG_STID)
5667 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005668
Andrew Elbleae254dac2017-11-09 13:41:10 -05005669 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5670 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005671 return nfserr_bad_stateid;
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005672 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005673 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005674 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005675 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005676 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005677 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005678 if (status)
5679 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005680 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005681 if (!*s)
5682 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005683 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5684 nfs4_put_stid(*s);
5685 if (cstate->minorversion)
5686 return nfserr_deleg_revoked;
5687 return nfserr_bad_stateid;
5688 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005689 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005690}
5691
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005692static struct nfsd_file *
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005693nfs4_find_file(struct nfs4_stid *s, int flags)
5694{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005695 if (!s)
5696 return NULL;
5697
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005698 switch (s->sc_type) {
5699 case NFS4_DELEG_STID:
5700 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5701 return NULL;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005702 return nfsd_file_get(s->sc_file->fi_deleg_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005703 case NFS4_OPEN_STID:
5704 case NFS4_LOCK_STID:
5705 if (flags & RD_STATE)
5706 return find_readable_file(s->sc_file);
5707 else
5708 return find_writeable_file(s->sc_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005709 }
5710
5711 return NULL;
5712}
5713
5714static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005715nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005716{
5717 __be32 status;
5718
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005719 status = nfsd4_check_openowner_confirmed(ols);
5720 if (status)
5721 return status;
5722 return nfs4_check_openmode(ols, flags);
5723}
5724
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005725static __be32
5726nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
Jeff Layton5c4583b2019-08-18 14:18:54 -04005727 struct nfsd_file **nfp, int flags)
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005728{
5729 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005730 struct nfsd_file *nf;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005731 __be32 status;
5732
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005733 nf = nfs4_find_file(s, flags);
5734 if (nf) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005735 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5736 acc | NFSD_MAY_OWNER_OVERRIDE);
Jeff Layton5c4583b2019-08-18 14:18:54 -04005737 if (status) {
5738 nfsd_file_put(nf);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005739 goto out;
Jeff Layton5c4583b2019-08-18 14:18:54 -04005740 }
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005741 } else {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005742 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005743 if (status)
5744 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005745 }
Jeff Layton5c4583b2019-08-18 14:18:54 -04005746 *nfp = nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005747out:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005748 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005749}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005750static void
5751_free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5752{
5753 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5754 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5755 return;
5756 list_del(&cps->cp_list);
5757 idr_remove(&nn->s2s_cp_stateids,
5758 cps->cp_stateid.stid.si_opaque.so_id);
5759 kfree(cps);
5760}
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005761/*
5762 * A READ from an inter server to server COPY will have a
5763 * copy stateid. Look up the copy notify stateid from the
5764 * idr structure and take a reference on it.
5765 */
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005766__be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5767 struct nfs4_client *clp,
5768 struct nfs4_cpntf_state **cps)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005769{
5770 copy_stateid_t *cps_t;
5771 struct nfs4_cpntf_state *state = NULL;
5772
5773 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5774 return nfserr_bad_stateid;
5775 spin_lock(&nn->s2s_cp_lock);
5776 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5777 if (cps_t) {
5778 state = container_of(cps_t, struct nfs4_cpntf_state,
5779 cp_stateid);
Dan Carpenter5277a792019-12-04 10:59:36 +03005780 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5781 state = NULL;
5782 goto unlock;
5783 }
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005784 if (!clp)
5785 refcount_inc(&state->cp_stateid.sc_count);
5786 else
5787 _free_cpntf_state_locked(nn, state);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005788 }
Dan Carpenter5277a792019-12-04 10:59:36 +03005789unlock:
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005790 spin_unlock(&nn->s2s_cp_lock);
5791 if (!state)
5792 return nfserr_bad_stateid;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005793 if (!clp && state)
5794 *cps = state;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005795 return 0;
5796}
5797
5798static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5799 struct nfs4_stid **stid)
5800{
5801 __be32 status;
5802 struct nfs4_cpntf_state *cps = NULL;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005803 struct nfs4_client *found;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005804
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005805 status = manage_cpntf_state(nn, st, NULL, &cps);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005806 if (status)
5807 return status;
5808
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005809 cps->cpntf_time = ktime_get_boottime_seconds();
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005810
5811 status = nfserr_expired;
5812 found = lookup_clientid(&cps->cp_p_clid, true, nn);
5813 if (!found)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005814 goto out;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005815
5816 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
5817 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
5818 if (*stid)
5819 status = nfs_ok;
5820 else
5821 status = nfserr_bad_stateid;
5822
5823 put_client_renew(found);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005824out:
5825 nfs4_put_cpntf_state(nn, cps);
5826 return status;
5827}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005828
5829void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5830{
5831 spin_lock(&nn->s2s_cp_lock);
5832 _free_cpntf_state_locked(nn, cps);
5833 spin_unlock(&nn->s2s_cp_lock);
5834}
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005835
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005837 * Checks for stateid operations
5838 */
Al Virob37ad282006-10-19 23:28:59 -07005839__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005840nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005841 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005842 stateid_t *stateid, int flags, struct nfsd_file **nfp,
5843 struct nfs4_stid **cstid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005845 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005846 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005847 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005848 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005849 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850
Jeff Layton5c4583b2019-08-18 14:18:54 -04005851 if (nfp)
5852 *nfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005854 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 return nfserr_grace;
5856
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005857 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5858 status = check_special_stateids(net, fhp, stateid, flags);
5859 goto done;
5860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005862 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005863 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005864 &s, nn);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005865 if (status == nfserr_bad_stateid)
5866 status = find_cpntf_state(nn, stateid, &s);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005867 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005868 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005869 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005870 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005871 if (status)
5872 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005873
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005874 switch (s->sc_type) {
5875 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005876 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005877 break;
5878 case NFS4_OPEN_STID:
5879 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005880 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005881 break;
5882 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005883 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005884 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005886 if (status)
5887 goto out;
5888 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005889
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005890done:
Jeff Layton5c4583b2019-08-18 14:18:54 -04005891 if (status == nfs_ok && nfp)
5892 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893out:
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005894 if (s) {
5895 if (!status && cstid)
5896 *cstid = s;
5897 else
5898 nfs4_put_stid(s);
5899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 return status;
5901}
5902
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005903/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005904 * Test if the stateid is valid
5905 */
5906__be32
5907nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005908 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005909{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005910 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005911 struct nfsd4_test_stateid_id *stateid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05005912 struct nfs4_client *cl = cstate->clp;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005913
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005914 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005915 stateid->ts_id_status =
5916 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005917
Bryan Schumaker17456802011-07-13 10:50:48 -04005918 return nfs_ok;
5919}
5920
Chuck Lever42691392016-08-11 10:37:30 -04005921static __be32
5922nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5923{
5924 struct nfs4_ol_stateid *stp = openlockstateid(s);
5925 __be32 ret;
5926
Trond Myklebust659aefb2017-11-03 08:00:13 -04005927 ret = nfsd4_lock_ol_stateid(stp);
5928 if (ret)
5929 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005930
5931 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5932 if (ret)
5933 goto out;
5934
5935 ret = nfserr_locks_held;
5936 if (check_for_locks(stp->st_stid.sc_file,
5937 lockowner(stp->st_stateowner)))
5938 goto out;
5939
5940 release_lock_stateid(stp);
5941 ret = nfs_ok;
5942
5943out:
5944 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005945out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005946 nfs4_put_stid(s);
5947 return ret;
5948}
5949
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005950__be32
5951nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005952 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005953{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005954 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005955 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005956 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005957 struct nfs4_delegation *dp;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05005958 struct nfs4_client *cl = cstate->clp;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005959 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005960
Jeff Layton1af71cc2014-07-29 21:34:14 -04005961 spin_lock(&cl->cl_lock);
5962 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005963 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005964 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005965 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005966 switch (s->sc_type) {
5967 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005968 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005969 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005970 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005971 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5972 if (ret)
5973 break;
5974 ret = nfserr_locks_held;
5975 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005976 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005977 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005978 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005979 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005980 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005981 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005982 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005983 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005984 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005985 list_del_init(&dp->dl_recall_lru);
5986 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005987 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005988 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005989 goto out;
5990 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005991 }
Trond Myklebust03da3162017-11-03 08:00:16 -04005992 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005993out_unlock:
5994 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005995out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005996 return ret;
5997}
5998
NeilBrown4c4cd222005-07-07 17:59:27 -07005999static inline int
6000setlkflg (int type)
6001{
6002 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6003 RD_STATE : WR_STATE;
6004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006006static __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 -04006007{
6008 struct svc_fh *current_fh = &cstate->current_fh;
6009 struct nfs4_stateowner *sop = stp->st_stateowner;
6010 __be32 status;
6011
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006012 status = nfsd4_check_seqid(cstate, sop, seqid);
6013 if (status)
6014 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04006015 status = nfsd4_lock_ol_stateid(stp);
6016 if (status != nfs_ok)
6017 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006018 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04006019 if (status == nfs_ok)
6020 status = nfs4_check_fh(current_fh, &stp->st_stid);
6021 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006022 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04006023 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006024}
6025
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026/*
6027 * Checks for sequence id mutating operations.
6028 */
Al Virob37ad282006-10-19 23:28:59 -07006029static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03006030nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04006031 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006032 struct nfs4_ol_stateid **stpp,
6033 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034{
J. Bruce Fields0836f582008-01-26 19:08:12 -05006035 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006036 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006037 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006039 trace_nfsd_preprocess(seqid, stateid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006040
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006042 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006043 if (status)
6044 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006045 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04006046 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006048 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006049 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006050 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04006051 else
6052 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006053 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006054}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05006055
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006056static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6057 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006058{
6059 __be32 status;
6060 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006061 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006063 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006064 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04006065 if (status)
6066 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006067 oo = openowner(stp->st_stateowner);
6068 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006069 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006070 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006071 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006072 }
6073 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07006074 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075}
6076
Al Virob37ad282006-10-19 23:28:59 -07006077__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006078nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006079 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006081 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07006082 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006083 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006084 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006085 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
Al Viroa6a9f182013-09-16 10:57:01 -04006087 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6088 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006090 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07006091 if (status)
6092 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006094 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006095 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006096 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006097 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006098 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006099 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006100 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006101 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006102 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04006103 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006104 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04006105 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04006106 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006107 mutex_unlock(&stp->st_mutex);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006108 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
Jeff Layton2a4317c2012-03-21 16:42:43 -04006109 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006110 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04006111put_stateid:
6112 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006114 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 return status;
6116}
6117
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006118static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119{
Jeff Layton82c5ff12012-05-11 09:45:13 -04006120 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006121 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04006122 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006123 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006124}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04006125
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006126static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6127{
6128 switch (to_access) {
6129 case NFS4_SHARE_ACCESS_READ:
6130 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6131 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6132 break;
6133 case NFS4_SHARE_ACCESS_WRITE:
6134 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6135 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6136 break;
6137 case NFS4_SHARE_ACCESS_BOTH:
6138 break;
6139 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006140 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 }
6142}
6143
Al Virob37ad282006-10-19 23:28:59 -07006144__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006145nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006146 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006148 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07006149 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006150 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006151 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152
Al Viroa6a9f182013-09-16 10:57:01 -04006153 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6154 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04006156 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02006157 if (od->od_deleg_want)
6158 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6159 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006161 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006162 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006163 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04006166 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006167 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006169 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04006171 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006172 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006174 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006176 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04006177 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04006178 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006180put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006181 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006182 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006184 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 return status;
6186}
6187
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006188static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6189{
Trond Myklebustacf92952014-06-30 11:48:37 -04006190 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04006191 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04006192 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006193
Jeff Layton2c41beb2014-07-29 21:34:41 -04006194 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04006195 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006196
Jeff Laytond83017f2014-07-29 21:34:42 -04006197 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04006198 if (unhashed)
6199 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04006200 spin_unlock(&clp->cl_lock);
6201 free_ol_stateid_reaplist(&reaplist);
6202 } else {
6203 spin_unlock(&clp->cl_lock);
6204 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04006205 if (unhashed)
6206 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04006207 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04006208}
6209
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210/*
6211 * nfs4_unlock_state() called after encode
6212 */
Al Virob37ad282006-10-19 23:28:59 -07006213__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006214nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006215 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006217 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07006218 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006219 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006220 struct net *net = SVC_NET(rqstp);
6221 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222
Al Viroa6a9f182013-09-16 10:57:01 -04006223 dprintk("NFSD: nfsd4_close on file %pd\n",
6224 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006226 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6227 &close->cl_stateid,
6228 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006229 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006230 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006231 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006233
6234 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006235
6236 /*
6237 * Technically we don't _really_ have to increment or copy it, since
6238 * it should just be gone after this operation and we clobber the
6239 * copied value below, but we continue to do so here just to ensure
6240 * that racing ops see that there was a state change.
6241 */
Jeff Layton9767feb2015-10-01 09:05:50 -04006242 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006244 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006245 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006246
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006247 /* v4.1+ suggests that we send a special stateid in here, since the
6248 * clients should just ignore this anyway. Since this is not useful
6249 * for v4.0 clients either, we set it to the special close_stateid
6250 * universally.
6251 *
6252 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6253 */
6254 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04006255
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006256 /* put reference from nfs4_preprocess_seqid_op */
6257 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 return status;
6260}
6261
Al Virob37ad282006-10-19 23:28:59 -07006262__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006263nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006264 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006266 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006267 struct nfs4_delegation *dp;
6268 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006269 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07006270 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006271 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006273 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006274 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006276 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006277 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006278 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006279 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04006280 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006281 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04006282 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006283
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006284 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006285put_stateid:
6286 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287out:
6288 return status;
6289}
6290
Benny Halevy87df4de2008-12-15 19:42:03 +02006291static inline u64
6292end_offset(u64 start, u64 len)
6293{
6294 u64 end;
6295
6296 end = start + len;
6297 return end >= start ? end: NFS4_MAX_UINT64;
6298}
6299
6300/* last octet in a range */
6301static inline u64
6302last_byte_offset(u64 start, u64 len)
6303{
6304 u64 end;
6305
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006306 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006307 end = start + len;
6308 return end > start ? end - 1: NFS4_MAX_UINT64;
6309}
6310
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311/*
6312 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6313 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6314 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6315 * locking, this prevents us from being completely protocol-compliant. The
6316 * real solution to this problem is to start using unsigned file offsets in
6317 * the VFS, but this is a very deep change!
6318 */
6319static inline void
6320nfs4_transform_lock_offset(struct file_lock *lock)
6321{
6322 if (lock->fl_start < 0)
6323 lock->fl_start = OFFSET_MAX;
6324 if (lock->fl_end < 0)
6325 lock->fl_end = OFFSET_MAX;
6326}
6327
Jeff Laytoncae80b32015-04-03 09:04:04 -04006328static fl_owner_t
6329nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006330{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006331 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6332
6333 nfs4_get_stateowner(&lo->lo_owner);
6334 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006335}
6336
Jeff Laytoncae80b32015-04-03 09:04:04 -04006337static void
6338nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006339{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006340 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006341
Jeff Laytoncae80b32015-04-03 09:04:04 -04006342 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006343 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006344}
6345
Jeff Layton76d348f2016-09-16 16:28:24 -04006346static void
6347nfsd4_lm_notify(struct file_lock *fl)
6348{
6349 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6350 struct net *net = lo->lo_owner.so_client->net;
6351 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6352 struct nfsd4_blocked_lock *nbl = container_of(fl,
6353 struct nfsd4_blocked_lock, nbl_lock);
6354 bool queue = false;
6355
Jeff Layton7919d0a2016-09-16 16:28:25 -04006356 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006357 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006358 if (!list_empty(&nbl->nbl_list)) {
6359 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006360 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006361 queue = true;
6362 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006363 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006364
6365 if (queue)
6366 nfsd4_run_cb(&nbl->nbl_cb);
6367}
6368
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006369static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006370 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006371 .lm_get_owner = nfsd4_fl_get_owner,
6372 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006373};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374
6375static inline void
6376nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6377{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006378 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379
NeilBrownd5b90262006-04-10 22:55:22 -07006380 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006381 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04006382 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6383 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006384 if (!deny->ld_owner.data)
6385 /* We just don't care that much */
6386 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006387 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006388 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006389nevermind:
6390 deny->ld_owner.len = 0;
6391 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006392 deny->ld_clientid.cl_boot = 0;
6393 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 }
6395 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006396 deny->ld_length = NFS4_MAX_UINT64;
6397 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6399 deny->ld_type = NFS4_READ_LT;
6400 if (fl->fl_type != F_RDLCK)
6401 deny->ld_type = NFS4_WRITE_LT;
6402}
6403
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006404static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006405find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006407 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006408 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409
Trond Myklebust0a880a22014-07-30 08:27:10 -04006410 lockdep_assert_held(&clp->cl_lock);
6411
Trond Myklebustd4f04892014-07-29 21:34:36 -04006412 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6413 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006414 if (so->so_is_open_owner)
6415 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006416 if (same_owner_str(so, owner))
6417 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 }
6419 return NULL;
6420}
6421
Trond Myklebustc58c6612014-07-29 21:34:35 -04006422static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006423find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006424{
6425 struct nfs4_lockowner *lo;
6426
Trond Myklebustd4f04892014-07-29 21:34:36 -04006427 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006428 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006429 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006430 return lo;
6431}
6432
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006433static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6434{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006435 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006436}
6437
Jeff Layton6b180f02014-07-29 21:34:26 -04006438static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6439{
6440 struct nfs4_lockowner *lo = lockowner(sop);
6441
6442 kmem_cache_free(lockowner_slab, lo);
6443}
6444
6445static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006446 .so_unhash = nfs4_unhash_lockowner,
6447 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006448};
6449
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450/*
6451 * Alloc a lock owner structure.
6452 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006453 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006455 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006457static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006458alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6459 struct nfs4_ol_stateid *open_stp,
6460 struct nfsd4_lock *lock)
6461{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006462 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006464 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6465 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006467 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006468 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6469 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006470 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006471 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006472 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006473 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006474 if (ret == NULL) {
6475 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006476 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006477 ret = lo;
6478 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006479 nfs4_free_stateowner(&lo->lo_owner);
6480
Trond Myklebustd4f04892014-07-29 21:34:36 -04006481 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006482 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483}
6484
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006485static struct nfs4_ol_stateid *
Trond Myklebusta451b122020-03-01 18:21:38 -05006486find_lock_stateid(const struct nfs4_lockowner *lo,
6487 const struct nfs4_ol_stateid *ost)
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006488{
6489 struct nfs4_ol_stateid *lst;
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006490
Trond Myklebusta451b122020-03-01 18:21:38 -05006491 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006492
Trond Myklebusta451b122020-03-01 18:21:38 -05006493 /* If ost is not hashed, ost->st_locks will not be valid */
6494 if (!nfs4_ol_stateid_unhashed(ost))
6495 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6496 if (lst->st_stateowner == &lo->lo_owner) {
6497 refcount_inc(&lst->st_stid.sc_count);
6498 return lst;
6499 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006500 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006501 return NULL;
6502}
6503
Trond Myklebustbeeca192017-11-03 08:00:14 -04006504static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006505init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6506 struct nfs4_file *fp, struct inode *inode,
6507 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006509 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006510 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511
Trond Myklebustbeeca192017-11-03 08:00:14 -04006512 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006513 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006514retry:
6515 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006516 if (nfs4_ol_stateid_unhashed(open_stp))
6517 goto out_close;
6518 retstp = find_lock_stateid(lo, open_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006519 if (retstp)
Trond Myklebusta451b122020-03-01 18:21:38 -05006520 goto out_found;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006521 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006522 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006523 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006524 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006525 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006526 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006528 stp->st_openstp = open_stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006529 spin_lock(&fp->fi_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006530 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006531 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006532 list_add(&stp->st_perfile, &fp->fi_stateids);
6533 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006534 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006535 return stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006536out_found:
6537 spin_unlock(&clp->cl_lock);
6538 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6539 nfs4_put_stid(&retstp->st_stid);
6540 goto retry;
6541 }
6542 /* To keep mutex tracking happy */
6543 mutex_unlock(&stp->st_mutex);
6544 return retstp;
6545out_close:
6546 spin_unlock(&clp->cl_lock);
6547 mutex_unlock(&stp->st_mutex);
6548 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549}
6550
Jeff Laytonc53530d2014-06-30 11:48:39 -04006551static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006552find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6553 struct inode *inode, struct nfs4_ol_stateid *ost,
6554 bool *new)
6555{
6556 struct nfs4_stid *ns = NULL;
6557 struct nfs4_ol_stateid *lst;
6558 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6559 struct nfs4_client *clp = oo->oo_owner.so_client;
6560
Trond Myklebustbeeca192017-11-03 08:00:14 -04006561 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006562 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006563 lst = find_lock_stateid(lo, ost);
Jeff Layton356a95e2014-07-29 21:34:13 -04006564 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006565 if (lst != NULL) {
6566 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6567 goto out;
6568 nfs4_put_stid(&lst->st_stid);
6569 }
6570 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6571 if (ns == NULL)
6572 return NULL;
6573
6574 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6575 if (lst == openlockstateid(ns))
6576 *new = true;
6577 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006578 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006579out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006580 return lst;
6581}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006582
NeilBrownfd39ca92005-06-23 22:04:03 -07006583static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584check_lock_length(u64 offset, u64 length)
6585{
Kinglong Meee7969312015-07-13 17:34:19 +08006586 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6587 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588}
6589
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006590static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006591{
Trond Myklebust11b91642014-07-29 21:34:08 -04006592 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006593
Jeff Layton7214e862014-07-10 14:07:33 -04006594 lockdep_assert_held(&fp->fi_lock);
6595
Jeff Layton82c5ff12012-05-11 09:45:13 -04006596 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006597 return;
Jeff Layton12659652014-07-10 14:07:28 -04006598 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006599 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006600}
6601
Jeff Layton356a95e2014-07-29 21:34:13 -04006602static __be32
6603lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6604 struct nfs4_ol_stateid *ost,
6605 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006606 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006607{
Jeff Layton5db1c032014-07-29 21:34:28 -04006608 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006609 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006610 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6611 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006612 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006613 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006614 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006615 unsigned int strhashval;
6616
Kinglong Meec8623992015-07-13 17:35:37 +08006617 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006618 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006619 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006620 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6621 if (lo == NULL)
6622 return nfserr_jukebox;
6623 } else {
6624 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006625 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006626 if (!cstate->minorversion &&
6627 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006628 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006629 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006630
Jeff Laytondd257932016-08-11 10:37:39 -04006631 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6632 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006633 status = nfserr_jukebox;
6634 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006635 }
Jeff Laytondd257932016-08-11 10:37:39 -04006636
Jeff Layton5db1c032014-07-29 21:34:28 -04006637 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006638 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006639out:
6640 nfs4_put_stateowner(&lo->lo_owner);
6641 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006642}
6643
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644/*
6645 * LOCK operation
6646 */
Al Virob37ad282006-10-19 23:28:59 -07006647__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006648nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006649 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006651 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006652 struct nfs4_openowner *open_sop = NULL;
6653 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006654 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006655 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006656 struct nfs4_file *fp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006657 struct nfsd_file *nf = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006658 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006659 struct file_lock *file_lock = NULL;
6660 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006661 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006662 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006663 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006664 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006665 unsigned char fl_type;
6666 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006667 struct net *net = SVC_NET(rqstp);
6668 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669
6670 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6671 (long long) lock->lk_offset,
6672 (long long) lock->lk_length);
6673
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 if (check_lock_length(lock->lk_offset, lock->lk_length))
6675 return nfserr_inval;
6676
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006677 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006678 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006679 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6680 return status;
6681 }
6682
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006684 if (nfsd4_has_session(cstate))
6685 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006686 memcpy(&lock->lk_new_clientid,
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006687 &cstate->clp->cl_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006688 sizeof(clientid_t));
6689
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006691 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 lock->lk_new_open_seqid,
6693 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006694 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006695 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006697 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006698 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006699 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006700 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006701 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006702 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006703 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006704 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006705 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006706 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006707 lock->lk_old_lock_seqid,
6708 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006709 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006710 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006711 if (status)
6712 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006713 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006715 lkflg = setlkflg(lock->lk_type);
6716 status = nfs4_check_openmode(lock_stp, lkflg);
6717 if (status)
6718 goto out;
6719
NeilBrown0dd395d2005-07-07 17:59:15 -07006720 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006721 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006722 goto out;
6723 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006724 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006725 goto out;
6726
Trond Myklebust11b91642014-07-29 21:34:08 -04006727 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006730 if (nfsd4_has_session(cstate))
6731 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006732 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006733 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006734 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006735 nf = find_readable_file_locked(fp);
6736 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006737 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006738 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006739 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006740 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 case NFS4_WRITEW_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_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006746 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006747 nf = find_writeable_file_locked(fp);
6748 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006749 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006750 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006751 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006752 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 default:
6754 status = nfserr_inval;
6755 goto out;
6756 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006757
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006758 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006759 status = nfserr_openmode;
6760 goto out;
6761 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006762
Jeff Layton76d348f2016-09-16 16:28:24 -04006763 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6764 if (!nbl) {
6765 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6766 status = nfserr_jukebox;
6767 goto out;
6768 }
6769
6770 file_lock = &nbl->nbl_lock;
6771 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006772 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006773 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006774 file_lock->fl_file = nf->nf_file;
Jeff Layton76d348f2016-09-16 16:28:24 -04006775 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006776 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6777 file_lock->fl_start = lock->lk_offset;
6778 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6779 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780
Jeff Layton21179d82012-08-21 08:03:32 -04006781 conflock = locks_alloc_lock();
6782 if (!conflock) {
6783 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6784 status = nfserr_jukebox;
6785 goto out;
6786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787
Jeff Layton76d348f2016-09-16 16:28:24 -04006788 if (fl_flags & FL_SLEEP) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01006789 nbl->nbl_time = ktime_get_boottime_seconds();
Jeff Layton0cc11a62016-10-20 09:34:31 -04006790 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006791 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006792 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006793 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006794 }
6795
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006796 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006797 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006799 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006800 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006801 if (lock->lk_reclaim)
6802 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006803 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006804 case FILE_LOCK_DEFERRED:
6805 nbl = NULL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006806 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006807 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006808 status = nfserr_denied;
6809 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006810 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006811 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006812 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006814 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006815 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006816 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006817 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006821 if (nbl) {
6822 /* dequeue it if we queued it before */
6823 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006824 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006825 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006826 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006827 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006828 }
6829 free_blocked_lock(nbl);
6830 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006831 if (nf)
6832 nfsd_file_put(nf);
Jeff Layton5db1c032014-07-29 21:34:28 -04006833 if (lock_stp) {
6834 /* Bump seqid manually if the 4.0 replay owner is openowner */
6835 if (cstate->replay_owner &&
6836 cstate->replay_owner != &lock_sop->lo_owner &&
6837 seqid_mutating_err(ntohl(status)))
6838 lock_sop->lo_owner.so_seqid++;
6839
6840 /*
6841 * If this is a new, never-before-used stateid, and we are
6842 * returning an error, then just go ahead and release it.
6843 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006844 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006845 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006846
6847 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006848
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006849 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006850 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006851 if (open_stp)
6852 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006853 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006854 if (conflock)
6855 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 return status;
6857}
6858
6859/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006860 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6861 * so we do a temporary open here just to get an open file to pass to
6862 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6863 * inode operation.)
6864 */
Al Viro04da6e92012-04-13 00:00:04 -04006865static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006866{
Jeff Layton6b556ca2019-08-18 14:18:55 -04006867 struct nfsd_file *nf;
6868 __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
Al Viro04da6e92012-04-13 00:00:04 -04006869 if (!err) {
Jeff Layton6b556ca2019-08-18 14:18:55 -04006870 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6871 nfsd_file_put(nf);
Al Viro04da6e92012-04-13 00:00:04 -04006872 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006873 return err;
6874}
6875
6876/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 * LOCKT operation
6878 */
Al Virob37ad282006-10-19 23:28:59 -07006879__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006880nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006881 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006883 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006884 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006885 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006886 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006887 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006889 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890 return nfserr_grace;
6891
6892 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6893 return nfserr_inval;
6894
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006895 if (!nfsd4_has_session(cstate)) {
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05006896 status = set_client(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006897 if (status)
6898 goto out;
6899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006901 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903
Jeff Layton21179d82012-08-21 08:03:32 -04006904 file_lock = locks_alloc_lock();
6905 if (!file_lock) {
6906 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6907 status = nfserr_jukebox;
6908 goto out;
6909 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006910
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 switch (lockt->lt_type) {
6912 case NFS4_READ_LT:
6913 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006914 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006915 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 case NFS4_WRITE_LT:
6917 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006918 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006919 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006921 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006923 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 }
6925
Kinglong Meec8623992015-07-13 17:35:37 +08006926 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006927 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006928 file_lock->fl_owner = (fl_owner_t)lo;
6929 file_lock->fl_pid = current->tgid;
6930 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931
Jeff Layton21179d82012-08-21 08:03:32 -04006932 file_lock->fl_start = lockt->lt_offset;
6933 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934
Jeff Layton21179d82012-08-21 08:03:32 -04006935 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936
Jeff Layton21179d82012-08-21 08:03:32 -04006937 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006938 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006939 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006940
Jeff Layton21179d82012-08-21 08:03:32 -04006941 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006943 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 }
6945out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006946 if (lo)
6947 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006948 if (file_lock)
6949 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 return status;
6951}
6952
Al Virob37ad282006-10-19 23:28:59 -07006953__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006954nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006955 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006957 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006958 struct nfs4_ol_stateid *stp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006959 struct nfsd_file *nf = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006960 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006961 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006962 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006963 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6964
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6966 (long long) locku->lu_offset,
6967 (long long) locku->lu_length);
6968
6969 if (check_lock_length(locku->lu_offset, locku->lu_length))
6970 return nfserr_inval;
6971
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006972 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006973 &locku->lu_stateid, NFS4_LOCK_STID,
6974 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006975 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 goto out;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006977 nf = find_any_file(stp->st_stid.sc_file);
6978 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006979 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006980 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006981 }
Jeff Layton21179d82012-08-21 08:03:32 -04006982 file_lock = locks_alloc_lock();
6983 if (!file_lock) {
6984 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6985 status = nfserr_jukebox;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006986 goto put_file;
Jeff Layton21179d82012-08-21 08:03:32 -04006987 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006988
Jeff Layton21179d82012-08-21 08:03:32 -04006989 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006990 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006991 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006992 file_lock->fl_file = nf->nf_file;
Jeff Layton21179d82012-08-21 08:03:32 -04006993 file_lock->fl_flags = FL_POSIX;
6994 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6995 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
Jeff Layton21179d82012-08-21 08:03:32 -04006997 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6998 locku->lu_length);
6999 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007001 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07007002 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05007003 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 goto out_nfserr;
7005 }
Jeff Layton9767feb2015-10-01 09:05:50 -04007006 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007007put_file:
7008 nfsd_file_put(nf);
Trond Myklebust858cc572014-07-29 21:34:16 -04007009put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04007010 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04007011 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04007013 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04007014 if (file_lock)
7015 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 return status;
7017
7018out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07007019 status = nfserrno(err);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007020 goto put_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021}
7022
7023/*
7024 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007025 * true: locks held by lockowner
7026 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007028static bool
7029check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007031 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007032 int status = false;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007033 struct nfsd_file *nf = find_any_file(fp);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007034 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007035 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007036
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007037 if (!nf) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007038 /* Any valid lock stateid should have some sort of access */
7039 WARN_ON_ONCE(1);
7040 return status;
7041 }
7042
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007043 inode = locks_inode(nf->nf_file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007044 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007046 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05007047 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007048 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7049 if (fl->fl_owner == (fl_owner_t)lowner) {
7050 status = true;
7051 break;
7052 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08007053 }
Jeff Layton6109c852015-01-16 15:05:57 -05007054 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007056 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 return status;
7058}
7059
Al Virob37ad282006-10-19 23:28:59 -07007060__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08007061nfsd4_release_lockowner(struct svc_rqst *rqstp,
7062 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007063 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007065 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04007067 struct nfs4_stateowner *sop;
7068 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04007069 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04007071 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07007072 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03007073 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04007074 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04007075 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076
7077 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7078 clid->cl_boot, clid->cl_id);
7079
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05007080 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007081 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04007082 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007083
Trond Myklebustd4f04892014-07-29 21:34:36 -04007084 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04007085 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04007086 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04007087 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04007088 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04007089
7090 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05007091 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04007092
Jeff Layton882e9d22014-07-29 21:34:37 -04007093 /* see if there are still any locks associated with it */
7094 lo = lockowner(sop);
7095 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
7096 if (check_for_locks(stp->st_stid.sc_file, lo)) {
7097 status = nfserr_locks_held;
7098 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04007099 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04007100 }
Jeff Layton5adfd882014-07-29 21:34:31 -04007101 }
Jeff Layton882e9d22014-07-29 21:34:37 -04007102
Kinglong Meeb5971af2014-08-22 10:18:43 -04007103 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04007104 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04007105 }
Chuck Lever88584812016-07-13 16:40:14 -04007106 if (!lo) {
7107 spin_unlock(&clp->cl_lock);
7108 return status;
7109 }
7110
7111 unhash_lockowner_locked(lo);
7112 while (!list_empty(&lo->lo_owner.so_stateids)) {
7113 stp = list_first_entry(&lo->lo_owner.so_stateids,
7114 struct nfs4_ol_stateid,
7115 st_perstateowner);
7116 WARN_ON(!unhash_lock_stateid(stp));
7117 put_ol_stateid_locked(stp, &reaplist);
7118 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04007119 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04007120 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007121 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04007122 nfs4_put_stateowner(&lo->lo_owner);
7123
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 return status;
7125}
7126
7127static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07007128alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129{
NeilBrowna55370a2005-06-23 22:03:52 -07007130 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131}
7132
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007133bool
Scott Mayhew6b189102019-03-26 18:06:26 -04007134nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07007135{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007136 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07007137
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007138 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007139 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07007140}
7141
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142/*
7143 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04007144 *
7145 * The caller is responsible for freeing name.data if NULL is returned (it
7146 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05007148struct nfs4_client_reclaim *
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007149nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7150 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151{
7152 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007153 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007155 trace_nfsd_clid_reclaim(nn, name.len, name.data);
NeilBrowna55370a2005-06-23 22:03:52 -07007156 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05007157 if (crp) {
7158 strhashval = clientstr_hashval(name);
7159 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007160 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04007161 crp->cr_name.data = name.data;
7162 crp->cr_name.len = name.len;
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007163 crp->cr_princhash.data = princhash.data;
7164 crp->cr_princhash.len = princhash.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007165 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007166 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007167 }
7168 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169}
7170
Jeff Layton2a4317c2012-03-21 16:42:43 -04007171void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007172nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05007173{
7174 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04007175 kfree(crp->cr_name.data);
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007176 kfree(crp->cr_princhash.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05007177 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007178 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05007179}
7180
7181void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007182nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183{
7184 struct nfs4_client_reclaim *crp = NULL;
7185 int i;
7186
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007188 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7189 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007191 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 }
7193 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05007194 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195}
7196
7197/*
7198 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04007199struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04007200nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201{
7202 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 struct nfs4_client_reclaim *crp = NULL;
7204
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007205 trace_nfsd_clid_find(nn, name.len, name.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206
Scott Mayhew6b189102019-03-26 18:06:26 -04007207 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007208 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04007209 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 return crp;
7211 }
7212 }
7213 return NULL;
7214}
7215
Al Virob37ad282006-10-19 23:28:59 -07007216__be32
J. Bruce Fields1722b042021-01-21 17:57:44 -05007217nfs4_check_open_reclaim(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218{
J. Bruce Fields1722b042021-01-21 17:57:44 -05007219 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Jeff Layton3b3e7b72014-09-12 16:40:22 -04007220 return nfserr_no_grace;
7221
J. Bruce Fields1722b042021-01-21 17:57:44 -05007222 if (nfsd4_client_record_check(clp))
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007223 return nfserr_reclaim_bad;
7224
7225 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226}
7227
Meelap Shahc2f1a552007-07-17 04:04:39 -07007228/*
7229 * Since the lifetime of a delegation isn't limited to that of an open, a
7230 * client may quite reasonably hang on to a delegation as long as it has
7231 * the inode cached. This becomes an obvious problem the first time a
7232 * client's inode cache approaches the size of the server's total memory.
7233 *
7234 * For now we avoid this problem by imposing a hard limit on the number
7235 * of delegations, which varies according to the server's memory size.
7236 */
7237static void
7238set_max_delegations(void)
7239{
7240 /*
7241 * Allow at most 4 delegations per megabyte of RAM. Quick
7242 * estimates suggest that in the worst case (where every delegation
7243 * is for a different inode), a delegation could take about 1.5K,
7244 * giving a worst case usage of about 6% of memory.
7245 */
7246 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7247}
7248
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007249static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007250{
7251 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7252 int i;
7253
Kees Cook6da2ec52018-06-12 13:55:00 -07007254 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7255 sizeof(struct list_head),
7256 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007257 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007258 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007259 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7260 sizeof(struct list_head),
7261 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007262 if (!nn->unconf_id_hashtbl)
7263 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007264 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7265 sizeof(struct list_head),
7266 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007267 if (!nn->sessionid_hashtbl)
7268 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007269
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007270 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007271 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007272 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007273 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007274 for (i = 0; i < SESSION_HASH_SIZE; i++)
7275 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007276 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007277 nn->unconf_name_tree = RB_ROOT;
Arnd Bergmann9cc76802019-10-31 15:53:13 +01007278 nn->boot_time = ktime_get_real_seconds();
Vasily Averin81833de2017-11-13 07:25:40 +03007279 nn->grace_ended = false;
7280 nn->nfsd4_manager.block_opens = true;
7281 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007282 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007283 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007284 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007285 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007286 spin_lock_init(&nn->s2s_cp_lock);
7287 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007288
Jeff Layton0cc11a62016-10-20 09:34:31 -04007289 spin_lock_init(&nn->blocked_locks_lock);
7290 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7291
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007292 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007293 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007294
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007295 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007296
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007297err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007298 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007299err_unconf_id:
7300 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007301err:
7302 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007303}
7304
7305static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007306nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007307{
7308 int i;
7309 struct nfs4_client *clp = NULL;
7310 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7311
7312 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7313 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7314 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7315 destroy_client(clp);
7316 }
7317 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007318
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007319 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7320
Kinglong Mee2b905632014-03-26 22:09:30 +08007321 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7322 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7323 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7324 destroy_client(clp);
7325 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007326 }
7327
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007328 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007329 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007330 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007331 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007332}
7333
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007334int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007335nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007336{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007337 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007338 int ret;
7339
Trond Myklebustc6c7f2a2021-03-13 16:08:47 -05007340 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007341 if (ret)
7342 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007343 locks_start_grace(net, &nn->nfsd4_manager);
7344 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007345 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7346 goto skip_grace;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01007347 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
Vasily Averin7e981a82017-11-06 16:46:04 +03007348 nn->nfsd4_grace, net->ns.inum);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007349 trace_nfsd_grace_start(nn);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007350 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007351 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007352
7353skip_grace:
7354 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7355 net->ns.inum);
7356 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7357 nfsd4_end_grace(nn);
7358 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007359}
7360
7361/* initialization to perform when the nfsd service is started: */
7362
7363int
7364nfs4_state_start(void)
7365{
7366 int ret;
7367
Jeff Layton51a54562015-08-20 07:17:01 -04007368 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007369 if (laundry_wq == NULL) {
7370 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007371 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007372 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007373 ret = nfsd4_create_callback_queue();
7374 if (ret)
7375 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007376
Meelap Shahc2f1a552007-07-17 04:04:39 -07007377 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007378 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007379
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007380out_free_laundry:
7381 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007382out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007383 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384}
7385
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007386void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007387nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007391 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007393 cancel_delayed_work_sync(&nn->laundromat_work);
7394 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007395
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007397 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007398 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007400 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007401 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 }
Benny Halevycdc975052014-05-30 09:09:30 -04007403 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 list_for_each_safe(pos, next, &reaplist) {
7405 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007406 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007407 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 }
7409
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007410 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007411 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412}
7413
7414void
7415nfs4_state_shutdown(void)
7416{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007417 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007418 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007420
7421static void
7422get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7423{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007424 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7425 CURRENT_STATEID(stateid))
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007426 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007427}
7428
7429static void
7430put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7431{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007432 if (cstate->minorversion) {
7433 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007434 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007435 }
7436}
7437
7438void
7439clear_current_stateid(struct nfsd4_compound_state *cstate)
7440{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007441 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007442}
7443
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007444/*
7445 * functions to set current state id
7446 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007447void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007448nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7449 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007450{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007451 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007452}
7453
7454void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007455nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7456 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007457{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007458 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007459}
7460
7461void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007462nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7463 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007464{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007465 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007466}
7467
7468void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007469nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7470 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007471{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007472 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007473}
7474
7475/*
7476 * functions to consume current state id
7477 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007478
7479void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007480nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7481 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007482{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007483 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007484}
7485
7486void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007487nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7488 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007489{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007490 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007491}
7492
7493void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007494nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7495 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007496{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007497 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007498}
7499
7500void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007501nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7502 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007503{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007504 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007505}
7506
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007507void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007508nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7509 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007510{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007511 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007512}
7513
7514void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007515nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7516 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007517{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007518 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007519}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007520
7521void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007522nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7523 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007524{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007525 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007526}
7527
7528void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007529nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7530 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007531{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007532 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007533}