blob: e011b407d00741f71f505b732b7c6f7527f061dd [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>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020046#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040047#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050048#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040049#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040051#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020052#include "pnfs.h"
Jeff Laytonfd4f83f2019-08-18 14:18:52 -040053#include "filecache.h"
Chuck Leverdd5e3fb2020-04-05 11:42:29 -040054#include "trace.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define NFSDDBG_FACILITY NFSDDBG_PROC
57
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050058#define all_ones {{~0,~0},~0}
59static const stateid_t one_stateid = {
60 .si_generation = ~0,
61 .si_opaque = all_ones,
62};
63static const stateid_t zero_stateid = {
64 /* all fields zero */
65};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010066static const stateid_t currentstateid = {
67 .si_generation = 1,
68};
Trond Myklebustfb500a72017-11-03 08:00:12 -040069static const stateid_t close_stateid = {
70 .si_generation = 0xffffffffU,
71};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050072
Andy Adamsonec6b5d72009-04-03 08:28:28 +030073static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070074
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050075#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
76#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010077#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elbleae254dac2017-11-09 13:41:10 -050078#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040081static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040082static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Scott Mayhew362063a2019-03-26 18:06:28 -040083void nfsd4_end_grace(struct nfsd_net *nn);
Olga Kornievskaia624322f2019-10-04 16:34:26 -040084static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
J. Bruce Fields8b671b82009-02-22 14:51:34 -080086/* Locking: */
87
J. Bruce Fields8b671b82009-02-22 14:51:34 -080088/*
89 * Currently used for the del_recall_lru and file hash table. In an
90 * effort to decrease the scope of the client_mutex, this spinlock may
91 * eventually cover more:
92 */
Benny Halevycdc975052014-05-30 09:09:30 -040093static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080094
Andrew Elble4f34bd02017-11-08 17:29:51 -050095enum nfsd4_st_mutex_lock_subclass {
96 OPEN_STATEID_MUTEX = 0,
97 LOCK_STATEID_MUTEX = 1,
98};
99
Jeff Laytonb401be222014-07-29 21:34:33 -0400100/*
101 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
102 * the refcount on the open stateid to drop.
103 */
104static DECLARE_WAIT_QUEUE_HEAD(close_wq);
105
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400106/*
107 * A waitqueue where a writer to clients/#/ctl destroying a client can
108 * wait for cl_rpc_users to drop to 0 and then for the client to be
109 * unhashed.
110 */
111static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
112
Jeff Layton9258a2d2018-03-16 09:47:22 -0400113static struct kmem_cache *client_slab;
Christoph Hellwigabf11352014-05-21 07:43:03 -0700114static struct kmem_cache *openowner_slab;
115static struct kmem_cache *lockowner_slab;
116static struct kmem_cache *file_slab;
117static struct kmem_cache *stateid_slab;
118static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200119static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700120
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400121static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800122
Julia Lawallc4cb8972015-11-21 22:57:39 +0100123static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400124static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200125
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400126static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800127{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400128 return ses->se_flags & NFS4_SESSION_DEAD;
129}
130
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400131static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
132{
133 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400134 return nfserr_jukebox;
135 ses->se_flags |= NFS4_SESSION_DEAD;
136 return nfs_ok;
137}
138
J. Bruce Fields221a6872013-04-01 22:23:49 -0400139static bool is_client_expired(struct nfs4_client *clp)
140{
141 return clp->cl_time == 0;
142}
143
J. Bruce Fields221a6872013-04-01 22:23:49 -0400144static __be32 get_client_locked(struct nfs4_client *clp)
145{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400146 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
147
148 lockdep_assert_held(&nn->client_lock);
149
J. Bruce Fields221a6872013-04-01 22:23:49 -0400150 if (is_client_expired(clp))
151 return nfserr_expired;
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400152 atomic_inc(&clp->cl_rpc_users);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400153 return nfs_ok;
154}
155
156/* must be called under the client_lock */
157static inline void
158renew_client_locked(struct nfs4_client *clp)
159{
160 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
161
162 if (is_client_expired(clp)) {
163 WARN_ON(1);
164 printk("%s: client (clientid %08x/%08x) already expired\n",
165 __func__,
166 clp->cl_clientid.cl_boot,
167 clp->cl_clientid.cl_id);
168 return;
169 }
170
J. Bruce Fields221a6872013-04-01 22:23:49 -0400171 list_move_tail(&clp->cl_lru, &nn->client_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100172 clp->cl_time = ktime_get_boottime_seconds();
J. Bruce Fields221a6872013-04-01 22:23:49 -0400173}
174
Fengguang Wuba138432013-04-16 22:14:15 -0400175static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400176{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178
179 lockdep_assert_held(&nn->client_lock);
180
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400181 if (!atomic_dec_and_test(&clp->cl_rpc_users))
J. Bruce Fields221a6872013-04-01 22:23:49 -0400182 return;
183 if (!is_client_expired(clp))
184 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400185 else
186 wake_up_all(&expiry_wq);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400187}
188
Jeff Layton4b24ca72014-06-30 11:48:44 -0400189static void put_client_renew(struct nfs4_client *clp)
190{
191 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
192
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400193 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
Jeff Laytond6c249b2014-07-08 14:02:50 -0400194 return;
195 if (!is_client_expired(clp))
196 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400197 else
198 wake_up_all(&expiry_wq);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400199 spin_unlock(&nn->client_lock);
200}
201
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400202static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
203{
204 __be32 status;
205
206 if (is_session_dead(ses))
207 return nfserr_badsession;
208 status = get_client_locked(ses->se_client);
209 if (status)
210 return status;
211 atomic_inc(&ses->se_ref);
212 return nfs_ok;
213}
214
215static void nfsd4_put_session_locked(struct nfsd4_session *ses)
216{
217 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400218 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
219
220 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400221
222 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
223 free_session(ses);
224 put_client_renew_locked(clp);
225}
226
227static void nfsd4_put_session(struct nfsd4_session *ses)
228{
229 struct nfs4_client *clp = ses->se_client;
230 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
231
232 spin_lock(&nn->client_lock);
233 nfsd4_put_session_locked(ses);
234 spin_unlock(&nn->client_lock);
235}
236
Jeff Layton76d348f2016-09-16 16:28:24 -0400237static struct nfsd4_blocked_lock *
238find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
239 struct nfsd_net *nn)
240{
241 struct nfsd4_blocked_lock *cur, *found = NULL;
242
Jeff Layton0cc11a62016-10-20 09:34:31 -0400243 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400244 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
245 if (fh_match(fh, &cur->nbl_fh)) {
246 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400247 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400248 found = cur;
249 break;
250 }
251 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400252 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400253 if (found)
NeilBrowncb03f942018-11-30 10:04:08 +1100254 locks_delete_block(&found->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400255 return found;
256}
257
258static struct nfsd4_blocked_lock *
259find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
260 struct nfsd_net *nn)
261{
262 struct nfsd4_blocked_lock *nbl;
263
264 nbl = find_blocked_lock(lo, fh, nn);
265 if (!nbl) {
266 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
267 if (nbl) {
Vasily Averine1e83992020-03-27 07:50:40 +0300268 INIT_LIST_HEAD(&nbl->nbl_list);
269 INIT_LIST_HEAD(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400270 fh_copy_shallow(&nbl->nbl_fh, fh);
271 locks_init_lock(&nbl->nbl_lock);
272 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
273 &nfsd4_cb_notify_lock_ops,
274 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
275 }
276 }
277 return nbl;
278}
279
280static void
281free_blocked_lock(struct nfsd4_blocked_lock *nbl)
282{
Jeff Layton6aaafc42019-04-22 12:34:23 -0400283 locks_delete_block(&nbl->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400284 locks_release_private(&nbl->nbl_lock);
285 kfree(nbl);
286}
287
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400288static void
289remove_blocked_locks(struct nfs4_lockowner *lo)
290{
291 struct nfs4_client *clp = lo->lo_owner.so_client;
292 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
293 struct nfsd4_blocked_lock *nbl;
294 LIST_HEAD(reaplist);
295
296 /* Dequeue all blocked locks */
297 spin_lock(&nn->blocked_locks_lock);
298 while (!list_empty(&lo->lo_blocked)) {
299 nbl = list_first_entry(&lo->lo_blocked,
300 struct nfsd4_blocked_lock,
301 nbl_list);
302 list_del_init(&nbl->nbl_list);
303 list_move(&nbl->nbl_lru, &reaplist);
304 }
305 spin_unlock(&nn->blocked_locks_lock);
306
307 /* Now free them */
308 while (!list_empty(&reaplist)) {
309 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
310 nbl_lru);
311 list_del_init(&nbl->nbl_lru);
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400312 free_blocked_lock(nbl);
313 }
314}
315
Jeff Laytonf4564582019-04-22 12:34:24 -0400316static void
317nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
318{
319 struct nfsd4_blocked_lock *nbl = container_of(cb,
320 struct nfsd4_blocked_lock, nbl_cb);
321 locks_delete_block(&nbl->nbl_lock);
322}
323
Jeff Layton76d348f2016-09-16 16:28:24 -0400324static int
325nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
326{
327 /*
328 * Since this is just an optimization, we don't try very hard if it
329 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
330 * just quit trying on anything else.
331 */
332 switch (task->tk_status) {
333 case -NFS4ERR_DELAY:
334 rpc_delay(task, 1 * HZ);
335 return 0;
336 default:
337 return 1;
338 }
339}
340
341static void
342nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
343{
344 struct nfsd4_blocked_lock *nbl = container_of(cb,
345 struct nfsd4_blocked_lock, nbl_cb);
346
347 free_blocked_lock(nbl);
348}
349
350static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
Jeff Laytonf4564582019-04-22 12:34:24 -0400351 .prepare = nfsd4_cb_notify_lock_prepare,
Jeff Layton76d348f2016-09-16 16:28:24 -0400352 .done = nfsd4_cb_notify_lock_done,
353 .release = nfsd4_cb_notify_lock_release,
354};
355
Kinglong Meeb5971af2014-08-22 10:18:43 -0400356static inline struct nfs4_stateowner *
357nfs4_get_stateowner(struct nfs4_stateowner *sop)
358{
359 atomic_inc(&sop->so_count);
360 return sop;
361}
362
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400363static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400364same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400365{
366 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400367 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400368}
369
370static struct nfs4_openowner *
371find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400372 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400373{
374 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400375
Trond Myklebustd4f04892014-07-29 21:34:36 -0400376 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400377
Trond Myklebustd4f04892014-07-29 21:34:36 -0400378 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
379 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400380 if (!so->so_is_open_owner)
381 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400382 if (same_owner_str(so, &open->op_owner))
383 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400384 }
385 return NULL;
386}
387
388static struct nfs4_openowner *
389find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400390 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400391{
392 struct nfs4_openowner *oo;
393
Trond Myklebustd4f04892014-07-29 21:34:36 -0400394 spin_lock(&clp->cl_lock);
395 oo = find_openstateowner_str_locked(hashval, open, clp);
396 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400397 return oo;
398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400static inline u32
401opaque_hashval(const void *ptr, int nbytes)
402{
403 unsigned char *cptr = (unsigned char *) ptr;
404
405 u32 x = 0;
406 while (nbytes--) {
407 x *= 37;
408 x += *cptr++;
409 }
410 return x;
411}
412
Jeff Layton5b095e92014-10-23 08:01:02 -0400413static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400414{
Jeff Layton5b095e92014-10-23 08:01:02 -0400415 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
416
417 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400418}
419
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200420void
NeilBrown13cd2182005-06-23 22:03:10 -0700421put_nfs4_file(struct nfs4_file *fi)
422{
Jeff Layton02e12152014-07-16 10:31:57 -0400423 might_lock(&state_lock);
424
Elena Reshetova818a34e2017-10-20 12:53:30 +0300425 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400426 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400427 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200428 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400429 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
430 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800431 }
NeilBrown13cd2182005-06-23 22:03:10 -0700432}
433
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400434static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400435__nfs4_get_fd(struct nfs4_file *f, int oflag)
436{
437 if (f->fi_fds[oflag])
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400438 return nfsd_file_get(f->fi_fds[oflag]);
Trond Myklebustde186432014-07-10 14:07:26 -0400439 return NULL;
440}
441
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400442static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400443find_writeable_file_locked(struct nfs4_file *f)
444{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400445 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400446
447 lockdep_assert_held(&f->fi_lock);
448
449 ret = __nfs4_get_fd(f, O_WRONLY);
450 if (!ret)
451 ret = __nfs4_get_fd(f, O_RDWR);
452 return ret;
453}
454
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400455static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400456find_writeable_file(struct nfs4_file *f)
457{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400458 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400459
460 spin_lock(&f->fi_lock);
461 ret = find_writeable_file_locked(f);
462 spin_unlock(&f->fi_lock);
463
464 return ret;
465}
466
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400467static struct nfsd_file *
468find_readable_file_locked(struct nfs4_file *f)
Trond Myklebustde186432014-07-10 14:07:26 -0400469{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400470 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400471
472 lockdep_assert_held(&f->fi_lock);
473
474 ret = __nfs4_get_fd(f, O_RDONLY);
475 if (!ret)
476 ret = __nfs4_get_fd(f, O_RDWR);
477 return ret;
478}
479
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400480static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400481find_readable_file(struct nfs4_file *f)
482{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400483 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400484
485 spin_lock(&f->fi_lock);
486 ret = find_readable_file_locked(f);
487 spin_unlock(&f->fi_lock);
488
489 return ret;
490}
491
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400492struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400493find_any_file(struct nfs4_file *f)
494{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400495 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400496
Trond Myklebusta451b122020-03-01 18:21:38 -0500497 if (!f)
498 return NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400499 spin_lock(&f->fi_lock);
500 ret = __nfs4_get_fd(f, O_RDWR);
501 if (!ret) {
502 ret = __nfs4_get_fd(f, O_WRONLY);
503 if (!ret)
504 ret = __nfs4_get_fd(f, O_RDONLY);
505 }
506 spin_unlock(&f->fi_lock);
507 return ret;
508}
509
J. Bruce Fields9affa432020-07-15 13:31:36 -0400510static struct nfsd_file *find_deleg_file(struct nfs4_file *f)
511{
512 struct nfsd_file *ret = NULL;
513
514 spin_lock(&f->fi_lock);
515 if (f->fi_deleg_file)
516 ret = nfsd_file_get(f->fi_deleg_file);
517 spin_unlock(&f->fi_lock);
518 return ret;
519}
520
Trond Myklebust02a35082014-07-25 07:34:22 -0400521static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800522unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700523
524/*
525 * Open owner state (share locks)
526 */
527
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500528/* hash tables for lock and open owners */
529#define OWNER_HASH_BITS 8
530#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
531#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700532
Trond Myklebustd4f04892014-07-29 21:34:36 -0400533static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400534{
535 unsigned int ret;
536
537 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500538 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400539}
NeilBrownef0f3392006-04-10 22:55:41 -0700540
NeilBrownef0f3392006-04-10 22:55:41 -0700541/* hash table for nfs4_file */
542#define FILE_HASH_BITS 8
543#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800544
Trond Myklebustca943212014-07-23 16:17:39 -0400545static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400546{
Trond Myklebustca943212014-07-23 16:17:39 -0400547 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
548}
549
550static unsigned int file_hashval(struct knfsd_fh *fh)
551{
552 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
553}
554
Jeff Layton89876f82013-04-02 09:01:59 -0400555static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700556
Jeff Layton12659652014-07-10 14:07:28 -0400557static void
558__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400559{
Jeff Layton7214e862014-07-10 14:07:33 -0400560 lockdep_assert_held(&fp->fi_lock);
561
Jeff Layton12659652014-07-10 14:07:28 -0400562 if (access & NFS4_SHARE_ACCESS_WRITE)
563 atomic_inc(&fp->fi_access[O_WRONLY]);
564 if (access & NFS4_SHARE_ACCESS_READ)
565 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400566}
567
Jeff Layton12659652014-07-10 14:07:28 -0400568static __be32
569nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400570{
Jeff Layton7214e862014-07-10 14:07:33 -0400571 lockdep_assert_held(&fp->fi_lock);
572
Jeff Layton12659652014-07-10 14:07:28 -0400573 /* Does this access mode make sense? */
574 if (access & ~NFS4_SHARE_ACCESS_BOTH)
575 return nfserr_inval;
576
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400577 /* Does it conflict with a deny mode already set? */
578 if ((access & fp->fi_share_deny) != 0)
579 return nfserr_share_denied;
580
Jeff Layton12659652014-07-10 14:07:28 -0400581 __nfs4_file_get_access(fp, access);
582 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400583}
584
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400585static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
586{
587 /* Common case is that there is no deny mode. */
588 if (deny) {
589 /* Does this deny mode make sense? */
590 if (deny & ~NFS4_SHARE_DENY_BOTH)
591 return nfserr_inval;
592
593 if ((deny & NFS4_SHARE_DENY_READ) &&
594 atomic_read(&fp->fi_access[O_RDONLY]))
595 return nfserr_share_denied;
596
597 if ((deny & NFS4_SHARE_DENY_WRITE) &&
598 atomic_read(&fp->fi_access[O_WRONLY]))
599 return nfserr_share_denied;
600 }
601 return nfs_ok;
602}
603
J. Bruce Fields998db522010-08-07 09:21:41 -0400604static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400605{
Trond Myklebustde186432014-07-10 14:07:26 -0400606 might_lock(&fp->fi_lock);
607
608 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400609 struct nfsd_file *f1 = NULL;
610 struct nfsd_file *f2 = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400611
Jeff Layton6d338b52014-07-10 14:07:29 -0400612 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400613 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400614 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400615 spin_unlock(&fp->fi_lock);
616 if (f1)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400617 nfsd_file_put(f1);
Trond Myklebustde186432014-07-10 14:07:26 -0400618 if (f2)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400619 nfsd_file_put(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400620 }
621}
622
Jeff Layton12659652014-07-10 14:07:28 -0400623static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400624{
Jeff Layton12659652014-07-10 14:07:28 -0400625 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
626
627 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400628 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400629 if (access & NFS4_SHARE_ACCESS_READ)
630 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400631}
632
Sachin Bhamare8287f002015-04-27 14:50:14 +0200633/*
634 * Allocate a new open/delegation state counter. This is needed for
635 * pNFS for proper return on close semantics.
636 *
637 * Note that we only allocate it for pNFS-enabled exports, otherwise
638 * all pointers to struct nfs4_clnt_odstate are always NULL.
639 */
640static struct nfs4_clnt_odstate *
641alloc_clnt_odstate(struct nfs4_client *clp)
642{
643 struct nfs4_clnt_odstate *co;
644
645 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
646 if (co) {
647 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300648 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200649 }
650 return co;
651}
652
653static void
654hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
655{
656 struct nfs4_file *fp = co->co_file;
657
658 lockdep_assert_held(&fp->fi_lock);
659 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
660}
661
662static inline void
663get_clnt_odstate(struct nfs4_clnt_odstate *co)
664{
665 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300666 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200667}
668
669static void
670put_clnt_odstate(struct nfs4_clnt_odstate *co)
671{
672 struct nfs4_file *fp;
673
674 if (!co)
675 return;
676
677 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300678 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200679 list_del(&co->co_perfile);
680 spin_unlock(&fp->fi_lock);
681
682 nfsd4_return_all_file_layouts(co->co_client, fp);
683 kmem_cache_free(odstate_slab, co);
684 }
685}
686
687static struct nfs4_clnt_odstate *
688find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
689{
690 struct nfs4_clnt_odstate *co;
691 struct nfs4_client *cl;
692
693 if (!new)
694 return NULL;
695
696 cl = new->co_client;
697
698 spin_lock(&fp->fi_lock);
699 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
700 if (co->co_client == cl) {
701 get_clnt_odstate(co);
702 goto out;
703 }
704 }
705 co = new;
706 co->co_file = fp;
707 hash_clnt_odstate_locked(new);
708out:
709 spin_unlock(&fp->fi_lock);
710 return co;
711}
712
Kinglong Meed19fb702017-01-18 19:04:42 +0800713struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
714 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400715{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500716 struct nfs4_stid *stid;
717 int new_id;
718
Trond Myklebustf8338832014-07-25 07:34:19 -0400719 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500720 if (!stid)
721 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400722
Jeff Layton4770d722014-07-29 21:34:10 -0400723 idr_preload(GFP_KERNEL);
724 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400725 /* Reserving 0 for start of file in nfsdfs "states" file: */
726 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400727 spin_unlock(&cl->cl_lock);
728 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700729 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500730 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800731
732 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500733 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500734 stid->sc_stateid.si_opaque.so_id = new_id;
735 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
736 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300737 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400738 spin_lock_init(&stid->sc_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400739 INIT_LIST_HEAD(&stid->sc_cp_list);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500740
J. Bruce Fields996e0932011-10-17 11:14:48 -0400741 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500742 * It shouldn't be a problem to reuse an opaque stateid value.
743 * I don't think it is for 4.1. But with 4.0 I worry that, for
744 * example, a stray write retransmission could be accepted by
745 * the server when it should have been rejected. Therefore,
746 * adopt a trick from the sctp code to attempt to maximize the
747 * amount of time until an id is reused, by ensuring they always
748 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400749 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500750 return stid;
751out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800752 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500753 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400754}
755
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400756/*
757 * Create a unique stateid_t to represent each COPY.
758 */
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400759static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
760 unsigned char sc_type)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400761{
762 int new_id;
763
Arnd Bergmann9cc76802019-10-31 15:53:13 +0100764 stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400765 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
766 stid->sc_type = sc_type;
767
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400768 idr_preload(GFP_KERNEL);
769 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400770 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
771 stid->stid.si_opaque.so_id = new_id;
Dai Ngoca9364d2020-11-30 16:24:49 -0500772 stid->stid.si_generation = 1;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400773 spin_unlock(&nn->s2s_cp_lock);
774 idr_preload_end();
775 if (new_id < 0)
776 return 0;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400777 return 1;
778}
779
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400780int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
781{
782 return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
783}
784
785struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
786 struct nfs4_stid *p_stid)
787{
788 struct nfs4_cpntf_state *cps;
789
790 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
791 if (!cps)
792 return NULL;
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100793 cps->cpntf_time = ktime_get_boottime_seconds();
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400794 refcount_set(&cps->cp_stateid.sc_count, 1);
795 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
796 goto out_free;
797 spin_lock(&nn->s2s_cp_lock);
798 list_add(&cps->cp_list, &p_stid->sc_cp_list);
799 spin_unlock(&nn->s2s_cp_lock);
800 return cps;
801out_free:
802 kfree(cps);
803 return NULL;
804}
805
806void nfs4_free_copy_state(struct nfsd4_copy *copy)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400807{
808 struct nfsd_net *nn;
809
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400810 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400811 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
812 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400813 idr_remove(&nn->s2s_cp_stateids,
814 copy->cp_stateid.stid.si_opaque.so_id);
815 spin_unlock(&nn->s2s_cp_lock);
816}
817
818static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
819{
820 struct nfs4_cpntf_state *cps;
821 struct nfsd_net *nn;
822
823 nn = net_generic(net, nfsd_net_id);
824 spin_lock(&nn->s2s_cp_lock);
825 while (!list_empty(&stid->sc_cp_list)) {
826 cps = list_first_entry(&stid->sc_cp_list,
827 struct nfs4_cpntf_state, cp_list);
828 _free_cpntf_state_locked(nn, cps);
829 }
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400830 spin_unlock(&nn->s2s_cp_lock);
831}
832
Jeff Laytonb49e0842014-07-29 21:34:11 -0400833static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400834{
Trond Myklebust60116952014-07-29 21:34:06 -0400835 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400836
Kinglong Meed19fb702017-01-18 19:04:42 +0800837 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400838 if (!stid)
839 return NULL;
840
Kinglong Meed19fb702017-01-18 19:04:42 +0800841 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400842}
843
844static void nfs4_free_deleg(struct nfs4_stid *stid)
845{
Trond Myklebust60116952014-07-29 21:34:06 -0400846 kmem_cache_free(deleg_slab, stid);
847 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400848}
849
NeilBrown6282cd52014-06-04 17:39:26 +1000850/*
851 * When we recall a delegation, we should be careful not to hand it
852 * out again straight away.
853 * To ensure this we keep a pair of bloom filters ('new' and 'old')
854 * in which the filehandles of recalled delegations are "stored".
855 * If a filehandle appear in either filter, a delegation is blocked.
856 * When a delegation is recalled, the filehandle is stored in the "new"
857 * filter.
858 * Every 30 seconds we swap the filters and clear the "new" one,
859 * unless both are empty of course.
860 *
861 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
862 * low 3 bytes as hash-table indices.
863 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400864 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000865 * is used to manage concurrent access. Testing does not need the lock
866 * except when swapping the two filters.
867 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400868static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000869static struct bloom_pair {
870 int entries, old_entries;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200871 time64_t swap_time;
NeilBrown6282cd52014-06-04 17:39:26 +1000872 int new; /* index into 'set' */
873 DECLARE_BITMAP(set[2], 256);
874} blocked_delegations;
875
876static int delegation_blocked(struct knfsd_fh *fh)
877{
878 u32 hash;
879 struct bloom_pair *bd = &blocked_delegations;
880
881 if (bd->entries == 0)
882 return 0;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200883 if (ktime_get_seconds() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400884 spin_lock(&blocked_delegations_lock);
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200885 if (ktime_get_seconds() - bd->swap_time > 30) {
NeilBrown6282cd52014-06-04 17:39:26 +1000886 bd->entries -= bd->old_entries;
887 bd->old_entries = bd->entries;
888 memset(bd->set[bd->new], 0,
889 sizeof(bd->set[0]));
890 bd->new = 1-bd->new;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200891 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +1000892 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400893 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000894 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100895 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000896 if (test_bit(hash&255, bd->set[0]) &&
897 test_bit((hash>>8)&255, bd->set[0]) &&
898 test_bit((hash>>16)&255, bd->set[0]))
899 return 1;
900
901 if (test_bit(hash&255, bd->set[1]) &&
902 test_bit((hash>>8)&255, bd->set[1]) &&
903 test_bit((hash>>16)&255, bd->set[1]))
904 return 1;
905
906 return 0;
907}
908
909static void block_delegations(struct knfsd_fh *fh)
910{
911 u32 hash;
912 struct bloom_pair *bd = &blocked_delegations;
913
Daniel Borkmann87545892014-12-10 16:33:11 +0100914 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000915
Jeff Laytonf54fe962014-07-25 07:34:26 -0400916 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000917 __set_bit(hash&255, bd->set[bd->new]);
918 __set_bit((hash>>8)&255, bd->set[bd->new]);
919 __set_bit((hash>>16)&255, bd->set[bd->new]);
920 if (bd->entries == 0)
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200921 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +1000922 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400923 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000924}
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500927alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
928 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200929 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400932 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400935 n = atomic_long_inc_return(&num_delegations);
936 if (n < 0 || n > max_delegations)
937 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000938 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400939 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800940 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700941 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400942 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400943
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400944 /*
945 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400946 * meaning of seqid 0 isn't meaningful, really, but let's avoid
947 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400948 */
949 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700950 INIT_LIST_HEAD(&dp->dl_perfile);
951 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200953 dp->dl_clnt_odstate = odstate;
954 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400955 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200956 dp->dl_retries = 1;
957 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200958 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500959 get_nfs4_file(fp);
960 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400962out_dec:
963 atomic_long_dec(&num_delegations);
964 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
967void
Trond Myklebust60116952014-07-29 21:34:06 -0400968nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Trond Myklebust11b91642014-07-29 21:34:08 -0400970 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400971 struct nfs4_client *clp = s->sc_client;
972
Jeff Layton4770d722014-07-29 21:34:10 -0400973 might_lock(&clp->cl_lock);
974
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300975 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400976 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400977 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400978 }
Trond Myklebust60116952014-07-29 21:34:06 -0400979 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400980 nfs4_free_cpntf_statelist(clp->net, s);
Jeff Layton4770d722014-07-29 21:34:10 -0400981 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400982 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400983 if (fp)
984 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Jeff Layton9767feb2015-10-01 09:05:50 -0400987void
988nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
989{
990 stateid_t *src = &stid->sc_stateid;
991
992 spin_lock(&stid->sc_lock);
993 if (unlikely(++src->si_generation == 0))
994 src->si_generation = 1;
995 memcpy(dst, src, sizeof(*dst));
996 spin_unlock(&stid->sc_lock);
997}
998
J. Bruce Fields353601e2018-02-16 14:29:42 -0500999static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001001 struct nfsd_file *nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05001002
1003 spin_lock(&fp->fi_lock);
1004 if (--fp->fi_delegees == 0)
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001005 swap(nf, fp->fi_deleg_file);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001006 spin_unlock(&fp->fi_lock);
1007
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001008 if (nf)
1009 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001010}
1011
1012static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1013{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -05001014 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001015 struct nfsd_file *nf = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -04001016
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -05001017 WARN_ON_ONCE(!fp->fi_delegees);
1018
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001019 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001020 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001023static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1024{
1025 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001026 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001027 nfs4_put_stid(&dp->dl_stid);
1028}
1029
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001030void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001031{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001032 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001033}
1034
Andrew Elble34ed9872015-10-15 12:07:28 -04001035/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001036 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -04001037 * @clp: a pointer to the nfs4_client we're granting a delegation to
1038 * @fp: a pointer to the nfs4_file we're granting a delegation on
1039 *
1040 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001041 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -04001042 */
1043
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001044static bool
1045nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -04001046{
Andrew Elble34ed9872015-10-15 12:07:28 -04001047 struct nfs4_delegation *searchdp = NULL;
1048 struct nfs4_client *searchclp = NULL;
1049
Benny Halevycdc975052014-05-30 09:09:30 -04001050 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04001051 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001052
Andrew Elble34ed9872015-10-15 12:07:28 -04001053 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1054 searchclp = searchdp->dl_stid.sc_client;
1055 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001056 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -04001057 }
1058 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001059 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -04001060}
1061
1062/**
1063 * hash_delegation_locked - Add a delegation to the appropriate lists
1064 * @dp: a pointer to the nfs4_delegation we are adding.
1065 * @fp: a pointer to the nfs4_file we're granting a delegation on
1066 *
1067 * Return:
1068 * On success: NULL if the delegation was successfully hashed.
1069 *
1070 * On error: -EAGAIN if one was previously granted to this
1071 * nfs4_client for this nfs4_file. Delegation is not hashed.
1072 *
1073 */
1074
1075static int
1076hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1077{
Andrew Elble34ed9872015-10-15 12:07:28 -04001078 struct nfs4_client *clp = dp->dl_stid.sc_client;
1079
1080 lockdep_assert_held(&state_lock);
1081 lockdep_assert_held(&fp->fi_lock);
1082
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001083 if (nfs4_delegation_exists(clp, fp))
1084 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001085 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001086 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001087 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001088 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1089 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001090}
1091
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001092static bool
Jeff Layton42690672014-07-25 07:34:20 -04001093unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Trond Myklebust11b91642014-07-29 21:34:08 -04001095 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001096
Jeff Layton42690672014-07-25 07:34:20 -04001097 lockdep_assert_held(&state_lock);
1098
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001099 if (list_empty(&dp->dl_perfile))
1100 return false;
1101
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001102 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001103 /* Ensure that deleg break won't try to requeue it */
1104 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001105 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001106 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001108 list_del_init(&dp->dl_perfile);
1109 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001110 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001111}
1112
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001113static void destroy_delegation(struct nfs4_delegation *dp)
1114{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001115 bool unhashed;
1116
Jeff Layton42690672014-07-25 07:34:20 -04001117 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001118 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001119 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001120 if (unhashed)
1121 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001122}
1123
1124static void revoke_delegation(struct nfs4_delegation *dp)
1125{
1126 struct nfs4_client *clp = dp->dl_stid.sc_client;
1127
Jeff Layton2d4a5322014-07-25 07:34:21 -04001128 WARN_ON(!list_empty(&dp->dl_recall_lru));
1129
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001130 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001131 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001132 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001133 spin_lock(&clp->cl_lock);
1134 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1135 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001136 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001137 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001138}
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140/*
1141 * SETCLIENTID state
1142 */
1143
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001144static unsigned int clientid_hashval(u32 id)
1145{
1146 return id & CLIENT_HASH_MASK;
1147}
1148
Scott Mayhew6b189102019-03-26 18:06:26 -04001149static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001150{
Scott Mayhew6b189102019-03-26 18:06:26 -04001151 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001152}
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001155 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1156 * st_{access,deny}_bmap field of the stateid, in order to track not
1157 * only what share bits are currently in force, but also what
1158 * combinations of share bits previous opens have used. This allows us
1159 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1160 * return an error if the client attempt to downgrade to a combination
1161 * of share bits not explicable by closing some of its previous opens.
1162 *
1163 * XXX: This enforcement is actually incomplete, since we don't keep
1164 * track of access/deny bit combinations; so, e.g., we allow:
1165 *
1166 * OPEN allow read, deny write
1167 * OPEN allow both, deny none
1168 * DOWNGRADE allow read, deny none
1169 *
1170 * which we should reject.
1171 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001172static unsigned int
1173bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001174 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001175 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001176
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001177 for (i = 1; i < 4; i++) {
1178 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001179 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001180 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001181 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001182}
1183
Jeff Layton82c5ff12012-05-11 09:45:13 -04001184/* set share access for a given stateid */
1185static inline void
1186set_access(u32 access, struct nfs4_ol_stateid *stp)
1187{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001188 unsigned char mask = 1 << access;
1189
1190 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1191 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001192}
1193
1194/* clear share access for a given stateid */
1195static inline void
1196clear_access(u32 access, struct nfs4_ol_stateid *stp)
1197{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001198 unsigned char mask = 1 << access;
1199
1200 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1201 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001202}
1203
1204/* test whether a given stateid has access */
1205static inline bool
1206test_access(u32 access, struct nfs4_ol_stateid *stp)
1207{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001208 unsigned char mask = 1 << access;
1209
1210 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001211}
1212
Jeff Laytonce0fc432012-05-11 09:45:14 -04001213/* set share deny for a given stateid */
1214static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001215set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001216{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001217 unsigned char mask = 1 << deny;
1218
1219 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1220 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001221}
1222
1223/* clear share deny for a given stateid */
1224static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001225clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001226{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001227 unsigned char mask = 1 << deny;
1228
1229 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1230 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001231}
1232
1233/* test whether a given stateid is denying specific access */
1234static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001235test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001236{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001237 unsigned char mask = 1 << deny;
1238
1239 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001240}
1241
1242static int nfs4_access_to_omode(u32 access)
1243{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001244 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001245 case NFS4_SHARE_ACCESS_READ:
1246 return O_RDONLY;
1247 case NFS4_SHARE_ACCESS_WRITE:
1248 return O_WRONLY;
1249 case NFS4_SHARE_ACCESS_BOTH:
1250 return O_RDWR;
1251 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001252 WARN_ON_ONCE(1);
1253 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001254}
1255
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001256/*
1257 * A stateid that had a deny mode associated with it is being released
1258 * or downgraded. Recalculate the deny mode on the file.
1259 */
1260static void
1261recalculate_deny_mode(struct nfs4_file *fp)
1262{
1263 struct nfs4_ol_stateid *stp;
1264
1265 spin_lock(&fp->fi_lock);
1266 fp->fi_share_deny = 0;
1267 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1268 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1269 spin_unlock(&fp->fi_lock);
1270}
1271
1272static void
1273reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1274{
1275 int i;
1276 bool change = false;
1277
1278 for (i = 1; i < 4; i++) {
1279 if ((i & deny) != i) {
1280 change = true;
1281 clear_deny(i, stp);
1282 }
1283 }
1284
1285 /* Recalculate per-file deny mode if there was a change */
1286 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001287 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001288}
1289
Jeff Layton82c5ff12012-05-11 09:45:13 -04001290/* release all access and file references for a given stateid */
1291static void
1292release_all_access(struct nfs4_ol_stateid *stp)
1293{
1294 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001295 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001296
1297 if (fp && stp->st_deny_bmap != 0)
1298 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001299
1300 for (i = 1; i < 4; i++) {
1301 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001302 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001303 clear_access(i, stp);
1304 }
1305}
1306
Kinglong Meed50ffde2015-07-16 12:05:07 +08001307static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1308{
1309 kfree(sop->so_owner.data);
1310 sop->so_ops->so_free(sop);
1311}
1312
Jeff Layton6b180f02014-07-29 21:34:26 -04001313static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1314{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001315 struct nfs4_client *clp = sop->so_client;
1316
1317 might_lock(&clp->cl_lock);
1318
1319 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001320 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001321 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001322 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001323 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001324}
1325
Trond Myklebusta451b122020-03-01 18:21:38 -05001326static bool
1327nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1328{
1329 return list_empty(&stp->st_perfile);
1330}
1331
Jeff Laytone8568732015-08-24 12:41:47 -04001332static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001333{
Trond Myklebust11b91642014-07-29 21:34:08 -04001334 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001335
Jeff Layton1c755dc2014-07-29 21:34:12 -04001336 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1337
Jeff Laytone8568732015-08-24 12:41:47 -04001338 if (list_empty(&stp->st_perfile))
1339 return false;
1340
Trond Myklebust1d31a252014-07-10 14:07:25 -04001341 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001342 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001343 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001344 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001345 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001346}
1347
Trond Myklebust60116952014-07-29 21:34:06 -04001348static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001349{
Trond Myklebust60116952014-07-29 21:34:06 -04001350 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001351
Sachin Bhamare8287f002015-04-27 14:50:14 +02001352 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001353 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001354 if (stp->st_stateowner)
1355 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001356 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001357}
1358
Jeff Laytonb49e0842014-07-29 21:34:11 -04001359static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001360{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001361 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1362 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001363 struct nfsd_file *nf;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001364
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001365 nf = find_any_file(stp->st_stid.sc_file);
1366 if (nf) {
1367 get_file(nf->nf_file);
1368 filp_close(nf->nf_file, (fl_owner_t)lo);
1369 nfsd_file_put(nf);
1370 }
Jeff Laytonb49e0842014-07-29 21:34:11 -04001371 nfs4_free_ol_stateid(stid);
1372}
1373
Jeff Layton2c41beb2014-07-29 21:34:41 -04001374/*
1375 * Put the persistent reference to an already unhashed generic stateid, while
1376 * holding the cl_lock. If it's the last reference, then put it onto the
1377 * reaplist for later destruction.
1378 */
1379static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1380 struct list_head *reaplist)
1381{
1382 struct nfs4_stid *s = &stp->st_stid;
1383 struct nfs4_client *clp = s->sc_client;
1384
1385 lockdep_assert_held(&clp->cl_lock);
1386
1387 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1388
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001389 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001390 wake_up_all(&close_wq);
1391 return;
1392 }
1393
1394 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1395 list_add(&stp->st_locks, reaplist);
1396}
1397
Jeff Laytone8568732015-08-24 12:41:47 -04001398static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001399{
Chuck Leverf46c4452016-10-29 18:19:03 -04001400 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001401
Trond Myklebusta451b122020-03-01 18:21:38 -05001402 if (!unhash_ol_stateid(stp))
1403 return false;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001404 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001405 nfs4_unhash_stid(&stp->st_stid);
Trond Myklebusta451b122020-03-01 18:21:38 -05001406 return true;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001407}
1408
Jeff Layton5adfd882014-07-29 21:34:31 -04001409static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001410{
Chuck Leverf46c4452016-10-29 18:19:03 -04001411 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001412 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001413
Chuck Leverf46c4452016-10-29 18:19:03 -04001414 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001415 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001416 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001417 if (unhashed)
1418 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001419}
1420
Trond Myklebustc58c6612014-07-29 21:34:35 -04001421static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001422{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001423 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001424
Trond Myklebustd4f04892014-07-29 21:34:36 -04001425 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001426
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001427 list_del_init(&lo->lo_owner.so_strhash);
1428}
1429
Jeff Layton2c41beb2014-07-29 21:34:41 -04001430/*
1431 * Free a list of generic stateids that were collected earlier after being
1432 * fully unhashed.
1433 */
1434static void
1435free_ol_stateid_reaplist(struct list_head *reaplist)
1436{
1437 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001438 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001439
1440 might_sleep();
1441
1442 while (!list_empty(reaplist)) {
1443 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1444 st_locks);
1445 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001446 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001447 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001448 if (fp)
1449 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001450 }
1451}
1452
Jeff Laytond83017f2014-07-29 21:34:42 -04001453static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1454 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001455{
1456 struct nfs4_ol_stateid *stp;
1457
Jeff Laytone8568732015-08-24 12:41:47 -04001458 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1459
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001460 while (!list_empty(&open_stp->st_locks)) {
1461 stp = list_entry(open_stp->st_locks.next,
1462 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001463 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001464 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001465 }
1466}
1467
Jeff Laytone8568732015-08-24 12:41:47 -04001468static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001469 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001470{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001471 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1472
Trond Myklebusta451b122020-03-01 18:21:38 -05001473 if (!unhash_ol_stateid(stp))
1474 return false;
Jeff Laytond83017f2014-07-29 21:34:42 -04001475 release_open_stateid_locks(stp, reaplist);
Trond Myklebusta451b122020-03-01 18:21:38 -05001476 return true;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001477}
1478
1479static void release_open_stateid(struct nfs4_ol_stateid *stp)
1480{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001481 LIST_HEAD(reaplist);
1482
1483 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001484 if (unhash_open_stateid(stp, &reaplist))
1485 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001486 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1487 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001488}
1489
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001490static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001491{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001492 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001493
Trond Myklebustd4f04892014-07-29 21:34:36 -04001494 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001495
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001496 list_del_init(&oo->oo_owner.so_strhash);
1497 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001498}
1499
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001500static void release_last_closed_stateid(struct nfs4_openowner *oo)
1501{
Jeff Layton217526e2014-07-30 08:27:11 -04001502 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1503 nfsd_net_id);
1504 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001505
Jeff Layton217526e2014-07-30 08:27:11 -04001506 spin_lock(&nn->client_lock);
1507 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001508 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001509 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001510 oo->oo_last_closed_stid = NULL;
1511 }
Jeff Layton217526e2014-07-30 08:27:11 -04001512 spin_unlock(&nn->client_lock);
1513 if (s)
1514 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001515}
1516
Jeff Layton2c41beb2014-07-29 21:34:41 -04001517static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001518{
1519 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001520 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001521 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001522
Jeff Layton2c41beb2014-07-29 21:34:41 -04001523 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001524
Trond Myklebustd4f04892014-07-29 21:34:36 -04001525 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001526 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001527 while (!list_empty(&oo->oo_owner.so_stateids)) {
1528 stp = list_first_entry(&oo->oo_owner.so_stateids,
1529 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001530 if (unhash_open_stateid(stp, &reaplist))
1531 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001532 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001533 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001534 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001535 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001536 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001537}
1538
Marc Eshel5282fd72009-04-03 08:27:52 +03001539static inline int
1540hash_sessionid(struct nfs4_sessionid *sessionid)
1541{
1542 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1543
1544 return sid->sequence % SESSION_HASH_SIZE;
1545}
1546
Mark Salter135dd002015-04-06 09:46:00 -04001547#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001548static inline void
1549dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1550{
1551 u32 *ptr = (u32 *)(&sessionid->data[0]);
1552 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1553}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001554#else
1555static inline void
1556dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1557{
1558}
1559#endif
1560
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001561/*
1562 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1563 * won't be used for replay.
1564 */
1565void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1566{
1567 struct nfs4_stateowner *so = cstate->replay_owner;
1568
1569 if (nfserr == nfserr_replay_me)
1570 return;
1571
1572 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001573 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001574 return;
1575 }
1576 if (!so)
1577 return;
1578 if (so->so_is_open_owner)
1579 release_last_closed_stateid(openowner(so));
1580 so->so_seqid++;
1581 return;
1582}
Marc Eshel5282fd72009-04-03 08:27:52 +03001583
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001584static void
1585gen_sessionid(struct nfsd4_session *ses)
1586{
1587 struct nfs4_client *clp = ses->se_client;
1588 struct nfsd4_sessionid *sid;
1589
1590 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1591 sid->clientid = clp->cl_clientid;
1592 sid->sequence = current_sessionid++;
1593 sid->reserved = 0;
1594}
1595
1596/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001597 * The protocol defines ca_maxresponssize_cached to include the size of
1598 * the rpc header, but all we need to cache is the data starting after
1599 * the end of the initial SEQUENCE operation--the rest we regenerate
1600 * each time. Therefore we can advertise a ca_maxresponssize_cached
1601 * value that is the number of bytes in our cache plus a few additional
1602 * bytes. In order to stay on the safe side, and not promise more than
1603 * we can cache, those additional bytes must be the minimum possible: 24
1604 * bytes of rpc header (xid through accept state, with AUTH_NULL
1605 * verifier), 12 for the compound header (with zero-length tag), and 44
1606 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001607 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001608#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1609
Andy Adamson557ce262009-08-28 08:45:04 -04001610static void
1611free_session_slots(struct nfsd4_session *ses)
1612{
1613 int i;
1614
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001615 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1616 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001617 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001618 }
Andy Adamson557ce262009-08-28 08:45:04 -04001619}
1620
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001621/*
1622 * We don't actually need to cache the rpc and session headers, so we
1623 * can allocate a little less for each slot:
1624 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001625static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001626{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001627 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001628
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001629 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1630 size = 0;
1631 else
1632 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1633 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001634}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001635
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001636/*
1637 * XXX: If we run out of reserved DRC memory we could (up to a point)
1638 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001639 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001640 */
NeilBrown2030ca52019-09-20 16:36:45 +10001641static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001642{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001643 u32 slotsize = slot_bytes(ca);
1644 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001645 unsigned long avail, total_avail;
NeilBrown2030ca52019-09-20 16:36:45 +10001646 unsigned int scale_factor;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001647
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001648 spin_lock(&nfsd_drc_lock);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001649 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1650 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1651 else
1652 /* We have handed out more space than we chose in
1653 * set_max_drc() to allow. That isn't really a
1654 * problem as long as that doesn't make us think we
1655 * have lots more due to integer overflow.
1656 */
1657 total_avail = 0;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001658 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001659 /*
NeilBrown2030ca52019-09-20 16:36:45 +10001660 * Never use more than a fraction of the remaining memory,
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001661 * unless it's the only way to give this client a slot.
NeilBrown2030ca52019-09-20 16:36:45 +10001662 * The chosen fraction is either 1/8 or 1/number of threads,
1663 * whichever is smaller. This ensures there are adequate
1664 * slots to support multiple clients per thread.
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001665 * Give the client one slot even if that would require
1666 * over-allocation--it is better than failure.
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001667 */
NeilBrown2030ca52019-09-20 16:36:45 +10001668 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1669
1670 avail = clamp_t(unsigned long, avail, slotsize,
1671 total_avail/scale_factor);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001672 num = min_t(int, num, avail / slotsize);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001673 num = max_t(int, num, 1);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001674 nfsd_drc_mem_used += num * slotsize;
1675 spin_unlock(&nfsd_drc_lock);
1676
1677 return num;
1678}
1679
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001680static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001681{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001682 int slotsize = slot_bytes(ca);
1683
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001684 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001685 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001686 spin_unlock(&nfsd_drc_lock);
1687}
1688
Kinglong Mee60810e52014-01-01 00:35:47 +08001689static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1690 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001691{
Kinglong Mee60810e52014-01-01 00:35:47 +08001692 int numslots = fattrs->maxreqs;
1693 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001694 struct nfsd4_session *new;
1695 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001696
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001697 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001698 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1699 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001700
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001701 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001702 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001703 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001704 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001705 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001706 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001707 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001708 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001709 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001710
1711 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1712 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1713
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001714 return new;
1715out_free:
1716 while (i--)
1717 kfree(new->se_slots[i]);
1718 kfree(new);
1719 return NULL;
1720}
1721
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001722static void free_conn(struct nfsd4_conn *c)
1723{
1724 svc_xprt_put(c->cn_xprt);
1725 kfree(c);
1726}
1727
1728static void nfsd4_conn_lost(struct svc_xpt_user *u)
1729{
1730 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1731 struct nfs4_client *clp = c->cn_session->se_client;
1732
1733 spin_lock(&clp->cl_lock);
1734 if (!list_empty(&c->cn_persession)) {
1735 list_del(&c->cn_persession);
1736 free_conn(c);
1737 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001738 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001739 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001740}
1741
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001742static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001743{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001744 struct nfsd4_conn *conn;
1745
1746 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1747 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001748 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001749 svc_xprt_get(rqstp->rq_xprt);
1750 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001751 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001752 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1753 return conn;
1754}
1755
J. Bruce Fields328ead22010-09-29 16:11:06 -04001756static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1757{
1758 conn->cn_session = ses;
1759 list_add(&conn->cn_persession, &ses->se_conns);
1760}
1761
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001762static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1763{
1764 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001765
1766 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001767 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001768 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001769}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001770
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001771static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001772{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001773 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001774 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001775}
1776
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001777static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001778{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001779 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001780
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001781 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001782 ret = nfsd4_register_conn(conn);
1783 if (ret)
1784 /* oops; xprt is already down: */
1785 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001786 /* We may have gained or lost a callback channel: */
1787 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001788}
1789
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001790static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001791{
1792 u32 dir = NFS4_CDFC4_FORE;
1793
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001794 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001795 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001796 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001797}
1798
1799/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001800static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001801{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001802 struct nfs4_client *clp = s->se_client;
1803 struct nfsd4_conn *c;
1804
1805 spin_lock(&clp->cl_lock);
1806 while (!list_empty(&s->se_conns)) {
1807 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1808 list_del_init(&c->cn_persession);
1809 spin_unlock(&clp->cl_lock);
1810
1811 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1812 free_conn(c);
1813
1814 spin_lock(&clp->cl_lock);
1815 }
1816 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001817}
1818
J. Bruce Fields1377b692012-09-11 21:42:40 -04001819static void __free_session(struct nfsd4_session *ses)
1820{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001821 free_session_slots(ses);
1822 kfree(ses);
1823}
1824
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001825static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001826{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001827 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001828 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001829 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001830}
Andy Adamson557ce262009-08-28 08:45:04 -04001831
Fengguang Wu135ae822012-11-10 07:20:25 -05001832static 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 -04001833{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001834 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001835 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001836
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001837 new->se_client = clp;
1838 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001839
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001840 INIT_LIST_HEAD(&new->se_conns);
1841
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001842 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001843 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001844 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001845 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001846 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001847 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001848 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001849 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001850 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001851 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001852
Chuck Leverb0d2e422014-08-22 15:10:59 -04001853 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001854 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001855 /*
1856 * This is a little silly; with sessions there's no real
1857 * use for the callback address. Use the peer address
1858 * as a reasonable default for now, but consider fixing
1859 * the rpc client not to require an address in the
1860 * future:
1861 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001862 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1863 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001864 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001865}
1866
Benny Halevy9089f1b2010-05-12 00:12:26 +03001867/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001868static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001869__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001870{
1871 struct nfsd4_session *elem;
1872 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001873 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001874
Trond Myklebust0a880a22014-07-30 08:27:10 -04001875 lockdep_assert_held(&nn->client_lock);
1876
Marc Eshel5282fd72009-04-03 08:27:52 +03001877 dump_sessionid(__func__, sessionid);
1878 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001879 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001880 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001881 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1882 NFS4_MAX_SESSIONID_LEN)) {
1883 return elem;
1884 }
1885 }
1886
1887 dprintk("%s: session not found\n", __func__);
1888 return NULL;
1889}
1890
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001891static struct nfsd4_session *
1892find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1893 __be32 *ret)
1894{
1895 struct nfsd4_session *session;
1896 __be32 status = nfserr_badsession;
1897
1898 session = __find_in_sessionid_hashtbl(sessionid, net);
1899 if (!session)
1900 goto out;
1901 status = nfsd4_get_session_locked(session);
1902 if (status)
1903 session = NULL;
1904out:
1905 *ret = status;
1906 return session;
1907}
1908
Benny Halevy9089f1b2010-05-12 00:12:26 +03001909/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001910static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001911unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001912{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001913 struct nfs4_client *clp = ses->se_client;
1914 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1915
1916 lockdep_assert_held(&nn->client_lock);
1917
Andy Adamson7116ed62009-04-03 08:27:43 +03001918 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001919 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001920 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001921 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001922}
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1925static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001926STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001928 /*
1929 * We're assuming the clid was not given out from a boot
1930 * precisely 2^32 (about 136 years) before this one. That seems
1931 * a safe assumption:
1932 */
1933 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return 0;
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04001935 trace_nfsd_clid_stale(clid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return 1;
1937}
1938
1939/*
1940 * XXX Should we use a slab cache ?
1941 * This type of memory management is somewhat inefficient, but we use it
1942 * anyway since SETCLIENTID is not a common operation.
1943 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001944static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001947 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Jeff Layton9258a2d2018-03-16 09:47:22 -04001949 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001950 if (clp == NULL)
1951 return NULL;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04001952 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001953 if (clp->cl_name.data == NULL)
1954 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001955 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1956 sizeof(struct list_head),
1957 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001958 if (!clp->cl_ownerstr_hashtbl)
1959 goto err_no_hashtbl;
1960 for (i = 0; i < OWNER_HASH_SIZE; i++)
1961 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
Trond Myklebust5694c932014-04-18 14:43:56 -04001962 INIT_LIST_HEAD(&clp->cl_sessions);
1963 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001964 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04001965 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1966 INIT_LIST_HEAD(&clp->cl_idhash);
1967 INIT_LIST_HEAD(&clp->cl_openowners);
1968 INIT_LIST_HEAD(&clp->cl_delegations);
1969 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001970 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001971#ifdef CONFIG_NFSD_PNFS
1972 INIT_LIST_HEAD(&clp->cl_lo_states);
1973#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001974 INIT_LIST_HEAD(&clp->async_copies);
1975 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001976 spin_lock_init(&clp->cl_lock);
1977 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001979err_no_hashtbl:
1980 kfree(clp->cl_name.data);
1981err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001982 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001983 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001986static void __free_client(struct kref *k)
1987{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001988 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1989 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001990
1991 free_svc_cred(&clp->cl_cred);
1992 kfree(clp->cl_ownerstr_hashtbl);
1993 kfree(clp->cl_name.data);
J. Bruce Fields79123442019-06-05 12:42:05 -04001994 kfree(clp->cl_nii_domain.data);
1995 kfree(clp->cl_nii_name.data);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001996 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001997 kmem_cache_free(client_slab, clp);
1998}
1999
YueHaibing297e57a2019-07-08 15:29:33 +08002000static void drop_client(struct nfs4_client *clp)
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002001{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002002 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002003}
2004
Trond Myklebust4dd86e152014-04-18 14:43:58 -04002005static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006free_client(struct nfs4_client *clp)
2007{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002008 while (!list_empty(&clp->cl_sessions)) {
2009 struct nfsd4_session *ses;
2010 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2011 se_perclnt);
2012 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002013 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2014 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002015 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04002016 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002017 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002018 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002019 clp->cl_nfsd_dentry = NULL;
2020 wake_up_all(&expiry_wq);
2021 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002022 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
Benny Halevy84d38ac2010-05-12 00:13:16 +03002025/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04002026static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03002027unhash_client_locked(struct nfs4_client *clp)
2028{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002029 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002030 struct nfsd4_session *ses;
2031
Trond Myklebust0a880a22014-07-30 08:27:10 -04002032 lockdep_assert_held(&nn->client_lock);
2033
Trond Myklebust4beb3452014-07-30 08:27:02 -04002034 /* Mark the client as expired! */
2035 clp->cl_time = 0;
2036 /* Make it invisible */
2037 if (!list_empty(&clp->cl_idhash)) {
2038 list_del_init(&clp->cl_idhash);
2039 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2040 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2041 else
2042 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2043 }
2044 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002045 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002046 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2047 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002048 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03002049}
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04002052unhash_client(struct nfs4_client *clp)
2053{
2054 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2055
2056 spin_lock(&nn->client_lock);
2057 unhash_client_locked(clp);
2058 spin_unlock(&nn->client_lock);
2059}
2060
Jeff Layton97403d92014-07-30 08:27:12 -04002061static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2062{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04002063 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04002064 return nfserr_jukebox;
2065 unhash_client_locked(clp);
2066 return nfs_ok;
2067}
2068
Trond Myklebust4beb3452014-07-30 08:27:02 -04002069static void
2070__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002072 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002073 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 struct list_head reaplist;
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04002078 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07002079 while (!list_empty(&clp->cl_delegations)) {
2080 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04002081 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04002082 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
Benny Halevycdc975052014-05-30 09:09:30 -04002084 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 while (!list_empty(&reaplist)) {
2086 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04002087 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05002088 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04002090 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05002091 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04002092 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04002093 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002094 }
NeilBrownea1da632005-06-23 22:04:17 -07002095 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002096 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002097 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002098 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002100 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2101 struct nfs4_stateowner *so, *tmp;
2102
2103 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2104 so_strhash) {
2105 /* Should be no openowners at this point */
2106 WARN_ON_ONCE(so->so_is_open_owner);
2107 remove_blocked_locks(lockowner(so));
2108 }
2109 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002110 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002111 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002112 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002113 if (clp->cl_cb_conn.cb_xprt)
2114 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002115 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002116 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
Trond Myklebust4beb3452014-07-30 08:27:02 -04002119static void
2120destroy_client(struct nfs4_client *clp)
2121{
2122 unhash_client(clp);
2123 __destroy_client(clp);
2124}
2125
Scott Mayhew362063a2019-03-26 18:06:28 -04002126static void inc_reclaim_complete(struct nfs4_client *clp)
2127{
2128 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2129
2130 if (!nn->track_reclaim_completes)
2131 return;
2132 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2133 return;
2134 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2135 nn->reclaim_str_hashtbl_size) {
2136 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2137 clp->net->ns.inum);
2138 nfsd4_end_grace(nn);
2139 }
2140}
2141
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002142static void expire_client(struct nfs4_client *clp)
2143{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002144 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002145 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002146 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002147}
2148
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002149static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2150{
2151 memcpy(target->cl_verifier.data, source->data,
2152 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002155static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2156{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2158 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2159}
2160
J. Bruce Fields50043852015-11-20 15:58:37 -05002161static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2162{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002163 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2164 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2165 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002166 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2167 if ((source->cr_principal && !target->cr_principal) ||
2168 (source->cr_raw_principal && !target->cr_raw_principal) ||
2169 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002170 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002171
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002172 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 target->cr_uid = source->cr_uid;
2174 target->cr_gid = source->cr_gid;
2175 target->cr_group_info = source->cr_group_info;
2176 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002177 target->cr_gss_mech = source->cr_gss_mech;
2178 if (source->cr_gss_mech)
2179 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002183static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002184compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2185{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002186 if (o1->len < o2->len)
2187 return -1;
2188 if (o1->len > o2->len)
2189 return 1;
2190 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002191}
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002194same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2195{
2196 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
2199static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002200same_clid(clientid_t *cl1, clientid_t *cl2)
2201{
2202 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002205static bool groups_equal(struct group_info *g1, struct group_info *g2)
2206{
2207 int i;
2208
2209 if (g1->ngroups != g2->ngroups)
2210 return false;
2211 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002212 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002213 return false;
2214 return true;
2215}
2216
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002217/*
2218 * RFC 3530 language requires clid_inuse be returned when the
2219 * "principal" associated with a requests differs from that previously
2220 * used. We use uid, gid's, and gss principal string as our best
2221 * approximation. We also don't want to allow non-gss use of a client
2222 * established using gss: in theory cr_principal should catch that
2223 * change, but in practice cr_principal can be null even in the gss case
2224 * since gssd doesn't always pass down a principal string.
2225 */
2226static bool is_gss_cred(struct svc_cred *cr)
2227{
2228 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2229 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2230}
2231
2232
Vivek Trivedi5559b502012-07-24 21:18:20 +05302233static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002234same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2235{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002236 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002237 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2238 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002239 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2240 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002241 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002242 if (cr1->cr_principal == cr2->cr_principal)
2243 return true;
2244 if (!cr1->cr_principal || !cr2->cr_principal)
2245 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302246 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
J. Bruce Fields57266a62013-04-13 14:27:29 -04002249static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2250{
2251 struct svc_cred *cr = &rqstp->rq_cred;
2252 u32 service;
2253
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002254 if (!cr->cr_gss_mech)
2255 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002256 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2257 return service == RPC_GSS_SVC_INTEGRITY ||
2258 service == RPC_GSS_SVC_PRIVACY;
2259}
2260
Andrew Elblededeb132016-06-15 12:52:08 -04002261bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002262{
2263 struct svc_cred *cr = &rqstp->rq_cred;
2264
2265 if (!cl->cl_mach_cred)
2266 return true;
2267 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2268 return false;
2269 if (!svc_rqst_integrity_protected(rqstp))
2270 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002271 if (cl->cl_cred.cr_raw_principal)
2272 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2273 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002274 if (!cr->cr_principal)
2275 return false;
2276 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2277}
2278
Jeff Layton294ac322014-07-30 08:27:15 -04002279static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002280{
Chuck Leverab4684d2012-03-02 17:13:50 -05002281 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Jeff Laytonf4199922014-06-17 07:44:11 -04002283 /*
2284 * This is opaque to client, so no need to byte-swap. Use
2285 * __force to keep sparse happy
2286 */
Arnd Bergmann9104ae42019-11-04 16:45:30 +01002287 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002288 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002289 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
Jeff Layton294ac322014-07-30 08:27:15 -04002292static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2293{
Arnd Bergmann9cc76802019-10-31 15:53:13 +01002294 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
Jeff Layton294ac322014-07-30 08:27:15 -04002295 clp->cl_clientid.cl_id = nn->clientid_counter++;
2296 gen_confirm(clp, nn);
2297}
2298
Jeff Layton4770d722014-07-29 21:34:10 -04002299static struct nfs4_stid *
2300find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002301{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002302 struct nfs4_stid *ret;
2303
2304 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2305 if (!ret || !ret->sc_type)
2306 return NULL;
2307 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002308}
2309
Jeff Layton4770d722014-07-29 21:34:10 -04002310static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002311find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002312{
2313 struct nfs4_stid *s;
2314
Jeff Layton4770d722014-07-29 21:34:10 -04002315 spin_lock(&cl->cl_lock);
2316 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002317 if (s != NULL) {
2318 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002319 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002320 else
2321 s = NULL;
2322 }
Jeff Layton4770d722014-07-29 21:34:10 -04002323 spin_unlock(&cl->cl_lock);
2324 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002325}
2326
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002327static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2328{
2329 struct nfsdfs_client *nc;
2330 nc = get_nfsdfs_client(inode);
2331 if (!nc)
2332 return NULL;
2333 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2334}
2335
J. Bruce Fields169319f2019-06-19 12:39:46 -04002336static void seq_quote_mem(struct seq_file *m, char *data, int len)
2337{
2338 seq_printf(m, "\"");
2339 seq_escape_mem_ascii(m, data, len);
2340 seq_printf(m, "\"");
2341}
2342
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002343static int client_info_show(struct seq_file *m, void *v)
2344{
2345 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002346 struct nfs4_client *clp;
2347 u64 clid;
2348
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002349 clp = get_nfsdfs_clp(inode);
2350 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002351 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002352 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2353 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002354 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2355 seq_printf(m, "name: ");
2356 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2357 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields79123442019-06-05 12:42:05 -04002358 if (clp->cl_nii_domain.data) {
2359 seq_printf(m, "Implementation domain: ");
2360 seq_quote_mem(m, clp->cl_nii_domain.data,
2361 clp->cl_nii_domain.len);
2362 seq_printf(m, "\nImplementation name: ");
2363 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
Arnd Bergmanne29f4702019-10-31 14:52:43 +01002364 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
J. Bruce Fields79123442019-06-05 12:42:05 -04002365 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2366 }
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002367 drop_client(clp);
2368
2369 return 0;
2370}
2371
2372static int client_info_open(struct inode *inode, struct file *file)
2373{
2374 return single_open(file, client_info_show, inode);
2375}
2376
2377static const struct file_operations client_info_fops = {
2378 .open = client_info_open,
2379 .read = seq_read,
2380 .llseek = seq_lseek,
2381 .release = single_release,
2382};
2383
J. Bruce Fields78599c42019-04-22 15:26:23 -04002384static void *states_start(struct seq_file *s, loff_t *pos)
2385 __acquires(&clp->cl_lock)
2386{
2387 struct nfs4_client *clp = s->private;
2388 unsigned long id = *pos;
2389 void *ret;
2390
2391 spin_lock(&clp->cl_lock);
2392 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2393 *pos = id;
2394 return ret;
2395}
2396
2397static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2398{
2399 struct nfs4_client *clp = s->private;
2400 unsigned long id = *pos;
2401 void *ret;
2402
2403 id = *pos;
2404 id++;
2405 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2406 *pos = id;
2407 return ret;
2408}
2409
2410static void states_stop(struct seq_file *s, void *v)
2411 __releases(&clp->cl_lock)
2412{
2413 struct nfs4_client *clp = s->private;
2414
2415 spin_unlock(&clp->cl_lock);
2416}
2417
Achilles Gaikwad580da462020-04-20 18:20:31 +05302418static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2419{
2420 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2421}
2422
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002423static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002424{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002425 struct inode *inode = f->nf_inode;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002426
2427 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2428 MAJOR(inode->i_sb->s_dev),
2429 MINOR(inode->i_sb->s_dev),
2430 inode->i_ino);
2431}
2432
2433static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2434{
2435 seq_printf(s, "owner: ");
2436 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2437}
2438
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002439static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2440{
J. Bruce Fieldsee590d22020-04-13 22:03:06 -04002441 seq_printf(s, "0x%.8x", stid->si_generation);
2442 seq_printf(s, "%12phN", &stid->si_opaque);
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002443}
2444
J. Bruce Fields78599c42019-04-22 15:26:23 -04002445static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2446{
2447 struct nfs4_ol_stateid *ols;
2448 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002449 struct nfsd_file *file;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002450 struct nfs4_stateowner *oo;
2451 unsigned int access, deny;
2452
2453 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2454 return 0; /* XXX: or SEQ_SKIP? */
2455 ols = openlockstateid(st);
2456 oo = ols->st_stateowner;
2457 nf = st->sc_file;
2458 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002459 if (!file)
2460 return 0;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002461
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002462 seq_printf(s, "- ");
2463 nfs4_show_stateid(s, &st->sc_stateid);
2464 seq_printf(s, ": { type: open, ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002465
2466 access = bmap_to_share_mode(ols->st_access_bmap);
2467 deny = bmap_to_share_mode(ols->st_deny_bmap);
2468
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002469 seq_printf(s, "access: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002470 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2471 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002472 seq_printf(s, "deny: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002473 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2474 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2475
2476 nfs4_show_superblock(s, file);
2477 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302478 nfs4_show_fname(s, file);
2479 seq_printf(s, ", ");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002480 nfs4_show_owner(s, oo);
2481 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002482 nfsd_file_put(file);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002483
2484 return 0;
2485}
2486
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002487static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2488{
2489 struct nfs4_ol_stateid *ols;
2490 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002491 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002492 struct nfs4_stateowner *oo;
2493
2494 ols = openlockstateid(st);
2495 oo = ols->st_stateowner;
2496 nf = st->sc_file;
2497 file = find_any_file(nf);
J. Bruce Fields9affa432020-07-15 13:31:36 -04002498 if (!file)
2499 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002500
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002501 seq_printf(s, "- ");
2502 nfs4_show_stateid(s, &st->sc_stateid);
2503 seq_printf(s, ": { type: lock, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002504
2505 /*
2506 * Note: a lock stateid isn't really the same thing as a lock,
2507 * it's the locking state held by one owner on a file, and there
2508 * may be multiple (or no) lock ranges associated with it.
2509 * (Same for the matter is true of open stateids.)
2510 */
2511
2512 nfs4_show_superblock(s, file);
2513 /* XXX: open stateid? */
2514 seq_printf(s, ", ");
Achilles Gaikwad580da462020-04-20 18:20:31 +05302515 nfs4_show_fname(s, file);
2516 seq_printf(s, ", ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002517 nfs4_show_owner(s, oo);
2518 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002519 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002520
2521 return 0;
2522}
2523
2524static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2525{
2526 struct nfs4_delegation *ds;
2527 struct nfs4_file *nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002528 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002529
2530 ds = delegstateid(st);
2531 nf = st->sc_file;
J. Bruce Fields9affa432020-07-15 13:31:36 -04002532 file = find_deleg_file(nf);
2533 if (!file)
2534 return 0;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002535
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002536 seq_printf(s, "- ");
2537 nfs4_show_stateid(s, &st->sc_stateid);
2538 seq_printf(s, ": { type: deleg, ");
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002539
2540 /* Kinda dead code as long as we only support read delegs: */
2541 seq_printf(s, "access: %s, ",
2542 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2543
2544 /* XXX: lease time, whether it's being recalled. */
2545
2546 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302547 seq_printf(s, ", ");
2548 nfs4_show_fname(s, file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002549 seq_printf(s, " }\n");
J. Bruce Fields9affa432020-07-15 13:31:36 -04002550 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002551
2552 return 0;
2553}
2554
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002555static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2556{
2557 struct nfs4_layout_stateid *ls;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002558 struct nfsd_file *file;
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002559
2560 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2561 file = ls->ls_file;
2562
J. Bruce Fieldsace7ade2020-04-13 21:37:19 -04002563 seq_printf(s, "- ");
2564 nfs4_show_stateid(s, &st->sc_stateid);
2565 seq_printf(s, ": { type: layout, ");
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002566
2567 /* XXX: What else would be useful? */
2568
2569 nfs4_show_superblock(s, file);
Achilles Gaikwad580da462020-04-20 18:20:31 +05302570 seq_printf(s, ", ");
2571 nfs4_show_fname(s, file);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002572 seq_printf(s, " }\n");
2573
2574 return 0;
2575}
2576
J. Bruce Fields78599c42019-04-22 15:26:23 -04002577static int states_show(struct seq_file *s, void *v)
2578{
2579 struct nfs4_stid *st = v;
2580
2581 switch (st->sc_type) {
2582 case NFS4_OPEN_STID:
2583 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002584 case NFS4_LOCK_STID:
2585 return nfs4_show_lock(s, st);
2586 case NFS4_DELEG_STID:
2587 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002588 case NFS4_LAYOUT_STID:
2589 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002590 default:
2591 return 0; /* XXX: or SEQ_SKIP? */
2592 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002593 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002594}
2595
2596static struct seq_operations states_seq_ops = {
2597 .start = states_start,
2598 .next = states_next,
2599 .stop = states_stop,
2600 .show = states_show
2601};
2602
2603static int client_states_open(struct inode *inode, struct file *file)
2604{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002605 struct seq_file *s;
2606 struct nfs4_client *clp;
2607 int ret;
2608
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002609 clp = get_nfsdfs_clp(inode);
2610 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002611 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002612
2613 ret = seq_open(file, &states_seq_ops);
2614 if (ret)
2615 return ret;
2616 s = file->private_data;
2617 s->private = clp;
2618 return 0;
2619}
2620
2621static int client_opens_release(struct inode *inode, struct file *file)
2622{
2623 struct seq_file *m = file->private_data;
2624 struct nfs4_client *clp = m->private;
2625
2626 /* XXX: alternatively, we could get/drop in seq start/stop */
2627 drop_client(clp);
2628 return 0;
2629}
2630
2631static const struct file_operations client_states_fops = {
2632 .open = client_states_open,
2633 .read = seq_read,
2634 .llseek = seq_lseek,
2635 .release = client_opens_release,
2636};
2637
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002638/*
2639 * Normally we refuse to destroy clients that are in use, but here the
2640 * administrator is telling us to just do it. We also want to wait
2641 * so the caller has a guarantee that the client's locks are gone by
2642 * the time the write returns:
2643 */
YueHaibing297e57a2019-07-08 15:29:33 +08002644static void force_expire_client(struct nfs4_client *clp)
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002645{
2646 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2647 bool already_expired;
2648
2649 spin_lock(&clp->cl_lock);
2650 clp->cl_time = 0;
2651 spin_unlock(&clp->cl_lock);
2652
2653 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2654 spin_lock(&nn->client_lock);
2655 already_expired = list_empty(&clp->cl_lru);
2656 if (!already_expired)
2657 unhash_client_locked(clp);
2658 spin_unlock(&nn->client_lock);
2659
2660 if (!already_expired)
2661 expire_client(clp);
2662 else
2663 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2664}
2665
2666static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2667 size_t size, loff_t *pos)
2668{
2669 char *data;
2670 struct nfs4_client *clp;
2671
2672 data = simple_transaction_get(file, buf, size);
2673 if (IS_ERR(data))
2674 return PTR_ERR(data);
2675 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2676 return -EINVAL;
2677 clp = get_nfsdfs_clp(file_inode(file));
2678 if (!clp)
2679 return -ENXIO;
2680 force_expire_client(clp);
2681 drop_client(clp);
2682 return 7;
2683}
2684
2685static const struct file_operations client_ctl_fops = {
2686 .write = client_ctl_write,
2687 .release = simple_transaction_release,
2688};
2689
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002690static const struct tree_descr client_files[] = {
2691 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002692 [1] = {"states", &client_states_fops, S_IRUSR},
Petr Vorel6cbfad52020-03-13 13:39:57 +01002693 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002694 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002695};
2696
Jeff Layton2216d442012-11-12 15:00:57 -05002697static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002698 struct svc_rqst *rqstp, nfs4_verifier *verf)
2699{
2700 struct nfs4_client *clp;
2701 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002702 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002703 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002704 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002705
2706 clp = alloc_client(name);
2707 if (clp == NULL)
2708 return NULL;
2709
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002710 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2711 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002712 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002713 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002714 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002715 gen_clid(clp, nn);
2716 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002717 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01002718 clp->cl_time = ktime_get_boottime_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002719 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002720 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002721 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002722 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002723 clp->net = net;
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002724 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002725 clp->cl_clientid.cl_id - nn->clientid_base,
2726 client_files);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002727 if (!clp->cl_nfsd_dentry) {
2728 free_client(clp);
2729 return NULL;
2730 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002731 return clp;
2732}
2733
NeilBrownfd39ca92005-06-23 22:04:03 -07002734static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002735add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2736{
2737 struct rb_node **new = &(root->rb_node), *parent = NULL;
2738 struct nfs4_client *clp;
2739
2740 while (*new) {
2741 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2742 parent = *new;
2743
2744 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2745 new = &((*new)->rb_left);
2746 else
2747 new = &((*new)->rb_right);
2748 }
2749
2750 rb_link_node(&new_clp->cl_namenode, parent, new);
2751 rb_insert_color(&new_clp->cl_namenode, root);
2752}
2753
2754static struct nfs4_client *
2755find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2756{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002757 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002758 struct rb_node *node = root->rb_node;
2759 struct nfs4_client *clp;
2760
2761 while (node) {
2762 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2763 cmp = compare_blob(&clp->cl_name, name);
2764 if (cmp > 0)
2765 node = node->rb_left;
2766 else if (cmp < 0)
2767 node = node->rb_right;
2768 else
2769 return clp;
2770 }
2771 return NULL;
2772}
2773
2774static void
2775add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
2777 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002778 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Trond Myklebust0a880a22014-07-30 08:27:10 -04002780 lockdep_assert_held(&nn->client_lock);
2781
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002782 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002783 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002785 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002786 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
NeilBrownfd39ca92005-06-23 22:04:03 -07002789static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790move_to_confirmed(struct nfs4_client *clp)
2791{
2792 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002793 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Trond Myklebust0a880a22014-07-30 08:27:10 -04002795 lockdep_assert_held(&nn->client_lock);
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002798 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002799 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002800 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002801 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002802 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
2805static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002806find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
2808 struct nfs4_client *clp;
2809 unsigned int idhashval = clientid_hashval(clid->cl_id);
2810
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002811 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002812 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002813 if ((bool)clp->cl_minorversion != sessions)
2814 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002815 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 }
2819 return NULL;
2820}
2821
2822static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002823find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2824{
2825 struct list_head *tbl = nn->conf_id_hashtbl;
2826
Trond Myklebust0a880a22014-07-30 08:27:10 -04002827 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002828 return find_client_in_id_table(tbl, clid, sessions);
2829}
2830
2831static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002832find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002834 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Trond Myklebust0a880a22014-07-30 08:27:10 -04002836 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002837 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
2839
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002840static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002841{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002842 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002843}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002844
NeilBrown28ce6052005-06-23 22:03:56 -07002845static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002846find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002847{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002848 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002849 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002850}
2851
2852static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002853find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002854{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002855 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002856 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002857}
2858
NeilBrownfd39ca92005-06-23 22:04:03 -07002859static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002860gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002862 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002863 struct sockaddr *sa = svc_addr(rqstp);
2864 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002865 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Jeff Layton7077ecb2009-08-14 12:57:58 -04002867 /* Currently, we only support tcp and tcp6 for the callback channel */
2868 if (se->se_callback_netid_len == 3 &&
2869 !memcmp(se->se_callback_netid_val, "tcp", 3))
2870 expected_family = AF_INET;
2871 else if (se->se_callback_netid_len == 4 &&
2872 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2873 expected_family = AF_INET6;
2874 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 goto out_err;
2876
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002877 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002878 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002879 (struct sockaddr *)&conn->cb_addr,
2880 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002881
J. Bruce Fields07263f12010-05-31 19:09:40 -04002882 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002884
J. Bruce Fields07263f12010-05-31 19:09:40 -04002885 if (conn->cb_addr.ss_family == AF_INET6)
2886 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002887
J. Bruce Fields07263f12010-05-31 19:09:40 -04002888 conn->cb_prog = se->se_callback_prog;
2889 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002890 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Chuck Lever1eace0d2020-04-05 14:15:29 -04002891 trace_nfsd_cb_args(clp, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 return;
2893out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002894 conn->cb_addr.ss_family = AF_UNSPEC;
2895 conn->cb_addrlen = 0;
Chuck Lever1eace0d2020-04-05 14:15:29 -04002896 trace_nfsd_cb_nodelegs(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return;
2898}
2899
Andy Adamson074fe892009-04-03 08:28:15 +03002900/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002901 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002902 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002903static void
Andy Adamson074fe892009-04-03 08:28:15 +03002904nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2905{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04002906 struct xdr_buf *buf = resp->xdr->buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002907 struct nfsd4_slot *slot = resp->cstate.slot;
2908 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002909
Andy Adamson557ce262009-08-28 08:45:04 -04002910 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002911
J. Bruce Fields085def32017-10-18 16:17:18 -04002912 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002913 slot->sl_opcnt = resp->opcnt;
2914 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002915 free_svc_cred(&slot->sl_cred);
2916 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002917
J. Bruce Fields085def32017-10-18 16:17:18 -04002918 if (!nfsd4_cache_this(resp)) {
2919 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002920 return;
2921 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002922 slot->sl_flags |= NFSD4_SLOT_CACHED;
2923
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002924 base = resp->cstate.data_offset;
2925 slot->sl_datalen = buf->len - base;
2926 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002927 WARN(1, "%s: sessions DRC could not cache compound\n",
2928 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002929 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002930}
2931
2932/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002933 * Encode the replay sequence operation from the slot values.
2934 * If cachethis is FALSE encode the uncached rep error on the next
2935 * operation which sets resp->p and increments resp->opcnt for
2936 * nfs4svc_encode_compoundres.
2937 *
Andy Adamson074fe892009-04-03 08:28:15 +03002938 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002939static __be32
2940nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2941 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002942{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002943 struct nfsd4_op *op;
2944 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002945
Andy Adamsonabfabf82009-07-23 19:02:18 -04002946 /* Encode the replayed sequence operation */
2947 op = &args->ops[resp->opcnt - 1];
2948 nfsd4_encode_operation(resp, op);
2949
J. Bruce Fields085def32017-10-18 16:17:18 -04002950 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2951 return op->status;
2952 if (args->opcnt == 1) {
2953 /*
2954 * The original operation wasn't a solo sequence--we
2955 * always cache those--so this retry must not match the
2956 * original:
2957 */
2958 op->status = nfserr_seq_false_retry;
2959 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002960 op = &args->ops[resp->opcnt++];
2961 op->status = nfserr_retry_uncached_rep;
2962 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002963 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002964 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002965}
2966
2967/*
Andy Adamson557ce262009-08-28 08:45:04 -04002968 * The sequence operation is not cached because we can use the slot and
2969 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002970 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002971static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002972nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2973 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002974{
Andy Adamson557ce262009-08-28 08:45:04 -04002975 struct nfsd4_slot *slot = resp->cstate.slot;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04002976 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002977 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002978 __be32 status;
2979
Andy Adamson557ce262009-08-28 08:45:04 -04002980 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002981
Andy Adamsonabfabf82009-07-23 19:02:18 -04002982 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002983 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002984 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002985
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002986 p = xdr_reserve_space(xdr, slot->sl_datalen);
2987 if (!p) {
2988 WARN_ON_ONCE(1);
2989 return nfserr_serverfault;
2990 }
2991 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2992 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002993
Andy Adamson557ce262009-08-28 08:45:04 -04002994 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002995 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002996}
2997
Andy Adamson0733d212009-04-03 08:28:01 +03002998/*
2999 * Set the exchange_id flags returned by the server.
3000 */
3001static void
3002nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3003{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003004#ifdef CONFIG_NFSD_PNFS
3005 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3006#else
Andy Adamson0733d212009-04-03 08:28:01 +03003007 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003008#endif
Andy Adamson0733d212009-04-03 08:28:01 +03003009
3010 /* Referrals are supported, Migration is not. */
3011 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3012
3013 /* set the wire flags to return to client. */
3014 clid->flags = new->cl_exchange_flags;
3015}
3016
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003017static bool client_has_openowners(struct nfs4_client *clp)
3018{
3019 struct nfs4_openowner *oo;
3020
3021 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3022 if (!list_empty(&oo->oo_owner.so_stateids))
3023 return true;
3024 }
3025 return false;
3026}
3027
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003028static bool client_has_state(struct nfs4_client *clp)
3029{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04003030 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08003031#ifdef CONFIG_NFSD_PNFS
3032 || !list_empty(&clp->cl_lo_states)
3033#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04003034 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04003035 || !list_empty(&clp->cl_sessions)
3036 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003037}
3038
J. Bruce Fields79123442019-06-05 12:42:05 -04003039static __be32 copy_impl_id(struct nfs4_client *clp,
3040 struct nfsd4_exchange_id *exid)
3041{
3042 if (!exid->nii_domain.data)
3043 return 0;
3044 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3045 if (!clp->cl_nii_domain.data)
3046 return nfserr_jukebox;
3047 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3048 if (!clp->cl_nii_name.data)
3049 return nfserr_jukebox;
Arnd Bergmanne29f4702019-10-31 14:52:43 +01003050 clp->cl_nii_time = exid->nii_time;
J. Bruce Fields79123442019-06-05 12:42:05 -04003051 return 0;
3052}
3053
Al Virob37ad282006-10-19 23:28:59 -07003054__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003055nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3056 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003057{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003058 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003059 struct nfs4_client *conf, *new;
3060 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003061 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04003062 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03003063 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04003064 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003065 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003066 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03003067
Jeff Layton363168b2009-08-14 12:57:56 -04003068 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03003069 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Chuck Lever523ec6e2020-11-02 15:19:12 -05003070 "ip_addr=%s flags %x, spa_how %u\n",
Andy Adamson0733d212009-04-03 08:28:01 +03003071 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04003072 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03003073
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003074 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03003075 return nfserr_inval;
3076
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003077 new = create_client(exid->clname, rqstp, &verf);
3078 if (new == NULL)
3079 return nfserr_jukebox;
J. Bruce Fields79123442019-06-05 12:42:05 -04003080 status = copy_impl_id(new, exid);
3081 if (status)
3082 goto out_nolock;
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003083
Andy Adamson0733d212009-04-03 08:28:01 +03003084 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003085 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04003086 exid->spo_must_enforce[0] = 0;
3087 exid->spo_must_enforce[1] = (
3088 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3089 1 << (OP_EXCHANGE_ID - 32) |
3090 1 << (OP_CREATE_SESSION - 32) |
3091 1 << (OP_DESTROY_SESSION - 32) |
3092 1 << (OP_DESTROY_CLIENTID - 32));
3093
3094 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3095 1 << (OP_OPEN_DOWNGRADE) |
3096 1 << (OP_LOCKU) |
3097 1 << (OP_DELEGRETURN));
3098
3099 exid->spo_must_allow[1] &= (
3100 1 << (OP_TEST_STATEID - 32) |
3101 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003102 if (!svc_rqst_integrity_protected(rqstp)) {
3103 status = nfserr_inval;
3104 goto out_nolock;
3105 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003106 /*
3107 * Sometimes userspace doesn't give us a principal.
3108 * Which is a bug, really. Anyway, we can't enforce
3109 * MACH_CRED in that case, better to give up now:
3110 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05003111 if (!new->cl_cred.cr_principal &&
3112 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003113 status = nfserr_serverfault;
3114 goto out_nolock;
3115 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003116 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03003117 case SP4_NONE:
3118 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003119 default: /* checked by xdr code */
3120 WARN_ON_ONCE(1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003121 fallthrough;
Andy Adamson0733d212009-04-03 08:28:01 +03003122 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08003123 status = nfserr_encr_alg_unsupp;
3124 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03003125 }
3126
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003127 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003128 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003129 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03003130 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003131 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3132 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3133
J. Bruce Fields136e6582012-05-12 20:37:23 -04003134 if (update) {
3135 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003136 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03003137 goto out;
3138 }
Andrew Elblededeb132016-06-15 12:52:08 -04003139 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003140 status = nfserr_wrong_cred;
3141 goto out;
3142 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003143 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03003144 status = nfserr_perm;
3145 goto out;
3146 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003147 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03003148 status = nfserr_not_same;
3149 goto out;
3150 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003151 /* case 6 */
3152 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003153 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003154 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003155 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003156 if (client_has_state(conf)) {
3157 status = nfserr_clid_inuse;
3158 goto out;
3159 }
Andy Adamson0733d212009-04-03 08:28:01 +03003160 goto out_new;
3161 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003162 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003163 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003164 goto out_copy;
3165 }
3166 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003167 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003168 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003169 }
3170
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003171 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003172 status = nfserr_noent;
3173 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003174 }
3175
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003176 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003177 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003178 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003179
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003180 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03003181out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003182 if (conf) {
3183 status = mark_client_expired_locked(conf);
3184 if (status)
3185 goto out;
3186 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003187 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003188 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3189 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003190
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003191 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003192 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003193out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003194 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3195 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003196
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003197 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3198 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003199
3200 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003201 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003202 status = nfs_ok;
3203
3204out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003205 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003206out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003207 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003208 expire_client(new);
3209 if (unconf)
3210 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003211 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003212}
3213
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003214static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003215check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003216{
Andy Adamson88e588d2009-07-23 19:02:15 -04003217 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3218 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003219
3220 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003221 if (slot_inuse) {
3222 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003223 return nfserr_jukebox;
3224 else
3225 return nfserr_seq_misordered;
3226 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003227 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003228 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003229 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003230 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003231 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003232 return nfserr_seq_misordered;
3233}
3234
Andy Adamson49557cc2009-07-23 19:02:16 -04003235/*
3236 * Cache the create session result into the create session single DRC
3237 * slot cache by saving the xdr structure. sl_seqid has been set.
3238 * Do this for solo or embedded create session operations.
3239 */
3240static void
3241nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003242 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003243{
3244 slot->sl_status = nfserr;
3245 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3246}
3247
3248static __be32
3249nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3250 struct nfsd4_clid_slot *slot)
3251{
3252 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3253 return slot->sl_status;
3254}
3255
Mi Jinlong1b74c252011-07-14 14:50:17 +08003256#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3257 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3258 1 + /* MIN tag is length with zero, only length */ \
3259 3 + /* version, opcount, opcode */ \
3260 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3261 /* seqid, slotID, slotID, cache */ \
3262 4 ) * sizeof(__be32))
3263
3264#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3265 2 + /* verifier: AUTH_NULL, length 0 */\
3266 1 + /* status */ \
3267 1 + /* MIN tag is length with zero, only length */ \
3268 3 + /* opcount, opcode, opstatus*/ \
3269 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3270 /* seqid, slotID, slotID, slotID, status */ \
3271 5 ) * sizeof(__be32))
3272
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003273static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003274{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003275 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3276
J. Bruce Fields373cd402013-04-08 15:42:12 -04003277 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3278 return nfserr_toosmall;
3279 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3280 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003281 ca->headerpadsz = 0;
3282 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3283 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3284 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3285 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3286 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3287 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3288 /*
3289 * Note decreasing slot size below client's request may make it
3290 * difficult for client to function correctly, whereas
3291 * decreasing the number of slots will (just?) affect
3292 * performance. When short on memory we therefore prefer to
3293 * decrease number of slots instead of their size. Clients that
3294 * request larger slots than they need will get poor results:
NeilBrown7f49fd5d2019-09-20 16:33:16 +10003295 * Note that we always allow at least one slot, because our
3296 * accounting is soft and provides no guarantees either way.
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003297 */
NeilBrown2030ca52019-09-20 16:36:45 +10003298 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003299
J. Bruce Fields373cd402013-04-08 15:42:12 -04003300 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003301}
3302
Chuck Lever45006322016-03-01 13:06:02 -05003303/*
3304 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3305 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3306 */
3307#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3308 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3309
3310#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3311 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3312
Kinglong Mee8a891632013-12-23 18:11:02 +08003313#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003314 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003315#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003316 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3317 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003318
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003319static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3320{
3321 ca->headerpadsz = 0;
3322
Kinglong Mee8a891632013-12-23 18:11:02 +08003323 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003324 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003325 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003326 return nfserr_toosmall;
3327 ca->maxresp_cached = 0;
3328 if (ca->maxops < 2)
3329 return nfserr_toosmall;
3330
3331 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003332}
3333
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003334static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3335{
3336 switch (cbs->flavor) {
3337 case RPC_AUTH_NULL:
3338 case RPC_AUTH_UNIX:
3339 return nfs_ok;
3340 default:
3341 /*
3342 * GSS case: the spec doesn't allow us to return this
3343 * error. But it also doesn't allow us not to support
3344 * GSS.
3345 * I'd rather this fail hard than return some error the
3346 * client might think it can already handle:
3347 */
3348 return nfserr_encr_alg_unsupp;
3349 }
3350}
3351
Andy Adamson069b6ad2009-04-03 08:27:58 +03003352__be32
3353nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003354 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003355{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003356 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003357 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003358 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003359 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003360 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003361 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003362 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003363 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003364 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003365
Mi Jinlonga62573d2011-03-23 17:57:07 +08003366 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3367 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003368 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3369 if (status)
3370 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003371 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003372 if (status)
3373 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003374 status = check_backchannel_attrs(&cr_ses->back_channel);
3375 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003376 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003377 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003378 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003379 if (!new)
3380 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003381 conn = alloc_conn_from_crses(rqstp, cr_ses);
3382 if (!conn)
3383 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003384
Jeff Laytond20c11d2014-07-30 08:27:07 -04003385 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003386 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003387 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003388 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003389
3390 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003391 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003392 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003393 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003394 cs_slot = &conf->cl_cs_slot;
3395 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003396 if (status) {
3397 if (status == nfserr_replay_cache)
3398 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003399 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003400 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003401 } else if (unconf) {
3402 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003403 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003404 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003405 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003406 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003407 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003408 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003409 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003410 cs_slot = &unconf->cl_cs_slot;
3411 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003412 if (status) {
3413 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003414 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003415 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003416 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003417 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003418 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003419 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003420 if (status) {
3421 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003422 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003423 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003424 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003425 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003426 conf = unconf;
3427 } else {
3428 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003429 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003430 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003431 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003432 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003433 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003434 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003435 cr_ses->flags &= ~SESSION4_RDMA;
3436
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003437 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003438 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003439
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003440 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003441 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003442 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003443 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003444
Jeff Laytond20c11d2014-07-30 08:27:07 -04003445 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003446 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003447 spin_unlock(&nn->client_lock);
3448 /* init connection and backchannel */
3449 nfsd4_init_conn(rqstp, conn, new);
3450 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003451 if (old)
3452 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003453 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003454out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003455 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003456 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003457 if (old)
3458 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003459out_free_session:
3460 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003461out_release_drc_mem:
3462 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003463 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003464}
3465
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003466static __be32 nfsd4_map_bcts_dir(u32 *dir)
3467{
3468 switch (*dir) {
3469 case NFS4_CDFC4_FORE:
3470 case NFS4_CDFC4_BACK:
3471 return nfs_ok;
3472 case NFS4_CDFC4_FORE_OR_BOTH:
3473 case NFS4_CDFC4_BACK_OR_BOTH:
3474 *dir = NFS4_CDFC4_BOTH;
3475 return nfs_ok;
zhengbinfc5fc5d2019-12-19 17:29:20 +08003476 }
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003477 return nfserr_inval;
3478}
3479
Christoph Hellwigeb698532017-05-08 20:58:35 +02003480__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3481 struct nfsd4_compound_state *cstate,
3482 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003483{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003484 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003485 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003486 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003487 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003488
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003489 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3490 if (status)
3491 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003492 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003493 session->se_cb_prog = bc->bc_cb_program;
3494 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003495 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003496
3497 nfsd4_probe_callback(session->se_client);
3498
3499 return nfs_ok;
3500}
3501
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003502static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3503{
3504 struct nfsd4_conn *c;
3505
3506 list_for_each_entry(c, &s->se_conns, cn_persession) {
3507 if (c->cn_xprt == xpt) {
3508 return c;
3509 }
3510 }
3511 return NULL;
3512}
3513
3514static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3515 struct nfsd4_session *session, u32 req)
3516{
3517 struct nfs4_client *clp = session->se_client;
3518 struct svc_xprt *xpt = rqst->rq_xprt;
3519 struct nfsd4_conn *c;
3520 __be32 status;
3521
3522 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3523 spin_lock(&clp->cl_lock);
3524 c = __nfsd4_find_conn(xpt, session);
3525 if (!c)
3526 status = nfserr_noent;
3527 else if (req == c->cn_flags)
3528 status = nfs_ok;
3529 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3530 c->cn_flags != NFS4_CDFC4_BACK)
3531 status = nfs_ok;
3532 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3533 c->cn_flags != NFS4_CDFC4_FORE)
3534 status = nfs_ok;
3535 else
3536 status = nfserr_inval;
3537 spin_unlock(&clp->cl_lock);
3538 return status;
3539}
3540
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003541__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3542 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003543 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003544{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003545 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003546 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003547 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003548 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003549 struct net *net = SVC_NET(rqstp);
3550 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003551
3552 if (!nfsd4_last_compound_op(rqstp))
3553 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003554 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003555 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003556 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003557 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003558 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003559 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003560 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003561 goto out;
J. Bruce Fieldsc2d715a2020-04-27 17:59:01 -04003562 status = nfsd4_match_existing_connection(rqstp, session, bcts->dir);
3563 if (status == nfs_ok || status == nfserr_inval)
3564 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003565 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003566 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003567 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003568 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003569 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003570 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003571 goto out;
3572 nfsd4_init_conn(rqstp, conn, session);
3573 status = nfs_ok;
3574out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003575 nfsd4_put_session(session);
3576out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003577 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003578}
3579
J. Bruce Fields665d5072018-04-11 16:53:36 -04003580static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003581{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003582 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003583 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003584 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003585}
3586
Andy Adamson069b6ad2009-04-03 08:27:58 +03003587__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003588nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3589 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003590{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003591 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003592 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003593 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003594 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003595 struct net *net = SVC_NET(r);
3596 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003597
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003598 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003599 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003600 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003601 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003602 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003603 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003604 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003605 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003606 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003607 if (!ses)
3608 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003609 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003610 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003611 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003612 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003613 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003614 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003615 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003616 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003617
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003618 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003619
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003620 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003621 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003622out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003623 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003624out_client_lock:
3625 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003626out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003627 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003628}
3629
J. Bruce Fields57266a62013-04-13 14:27:29 -04003630static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003631{
3632 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003633 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003634 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003635 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003636
3637 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003638 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003639 if (c)
3640 goto out_free;
3641 status = nfserr_conn_not_bound_to_session;
3642 if (clp->cl_mach_cred)
3643 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003644 __nfsd4_hash_conn(new, ses);
3645 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003646 ret = nfsd4_register_conn(new);
3647 if (ret)
3648 /* oops; xprt is already down: */
3649 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003650 return nfs_ok;
3651out_free:
3652 spin_unlock(&clp->cl_lock);
3653 free_conn(new);
3654 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003655}
3656
Mi Jinlong868b89c2011-04-27 09:09:58 +08003657static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3658{
3659 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3660
3661 return args->opcnt > session->se_fchannel.maxops;
3662}
3663
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003664static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3665 struct nfsd4_session *session)
3666{
3667 struct xdr_buf *xb = &rqstp->rq_arg;
3668
3669 return xb->len > session->se_fchannel.maxreq_sz;
3670}
3671
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003672static bool replay_matches_cache(struct svc_rqst *rqstp,
3673 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3674{
3675 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3676
3677 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3678 (bool)seq->cachethis)
3679 return false;
3680 /*
Scott Mayhew6e73e922019-10-09 15:11:37 -04003681 * If there's an error then the reply can have fewer ops than
3682 * the call.
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003683 */
Scott Mayhew6e73e922019-10-09 15:11:37 -04003684 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3685 return false;
3686 /*
3687 * But if we cached a reply with *more* ops than the call you're
3688 * sending us now, then this new call is clearly not really a
3689 * replay of the old one:
3690 */
3691 if (slot->sl_opcnt > argp->opcnt)
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003692 return false;
3693 /* This is the only check explicitly called by spec: */
3694 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3695 return false;
3696 /*
3697 * There may be more comparisons we could actually do, but the
3698 * spec doesn't require us to catch every case where the calls
3699 * don't match (that would require caching the call as well as
3700 * the reply), so we don't bother.
3701 */
3702 return true;
3703}
3704
Andy Adamson069b6ad2009-04-03 08:27:58 +03003705__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003706nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3707 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003708{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003709 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003710 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003711 struct xdr_stream *xdr = resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003712 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003713 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003714 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003715 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003716 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003717 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003718 struct net *net = SVC_NET(rqstp);
3719 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003720
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003721 if (resp->opcnt != 1)
3722 return nfserr_sequence_pos;
3723
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003724 /*
3725 * Will be either used or freed by nfsd4_sequence_check_conn
3726 * below.
3727 */
3728 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3729 if (!conn)
3730 return nfserr_jukebox;
3731
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003732 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003733 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003734 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003735 goto out_no_session;
3736 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003737
Mi Jinlong868b89c2011-04-27 09:09:58 +08003738 status = nfserr_too_many_ops;
3739 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003740 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003741
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003742 status = nfserr_req_too_big;
3743 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003744 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003745
Benny Halevyb85d4c02009-04-03 08:28:08 +03003746 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003747 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003748 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003749
Andy Adamson557ce262009-08-28 08:45:04 -04003750 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003751 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3752
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003753 /* We do not negotiate the number of slots yet, so set the
3754 * maxslots to the session maxreqs which is used to encode
3755 * sr_highest_slotid and the sr_target_slot id to maxslots */
3756 seq->maxslots = session->se_fchannel.maxreqs;
3757
J. Bruce Fields73e79482012-02-13 16:39:00 -05003758 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3759 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003760 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003761 status = nfserr_seq_misordered;
3762 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003763 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003764 status = nfserr_seq_false_retry;
3765 if (!replay_matches_cache(rqstp, seq, slot))
3766 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003767 cstate->slot = slot;
3768 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003769 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003770 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003771 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003772 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003773 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003774 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003775 }
3776 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003777 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003778
J. Bruce Fields57266a62013-04-13 14:27:29 -04003779 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003780 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003781 if (status)
3782 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003783
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003784 buflen = (seq->cachethis) ?
3785 session->se_fchannel.maxresp_cached :
3786 session->se_fchannel.maxresp_sz;
3787 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3788 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003789 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003790 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003791 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003792
3793 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003794 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003795 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003796 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003797 if (seq->cachethis)
3798 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003799 else
3800 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003801
3802 cstate->slot = slot;
3803 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003804 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003805
Benny Halevyb85d4c02009-04-03 08:28:08 +03003806out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003807 switch (clp->cl_cb_state) {
3808 case NFSD4_CB_DOWN:
3809 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3810 break;
3811 case NFSD4_CB_FAULT:
3812 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3813 break;
3814 default:
3815 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003816 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003817 if (!list_empty(&clp->cl_revoked))
3818 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003819out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003820 if (conn)
3821 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003822 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003823 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003824out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003825 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003826 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003827}
3828
Trond Myklebustb6076642014-06-30 11:48:35 -04003829void
3830nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3831{
3832 struct nfsd4_compound_state *cs = &resp->cstate;
3833
3834 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003835 if (cs->status != nfserr_replay_cache) {
3836 nfsd4_store_cache_entry(resp);
3837 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3838 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003839 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003840 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003841 } else if (cs->clp)
3842 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003843}
3844
Mi Jinlong345c2842011-10-20 17:51:39 +08003845__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003846nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3847 struct nfsd4_compound_state *cstate,
3848 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003849{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003850 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003851 struct nfs4_client *conf, *unconf;
3852 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003853 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003854 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003855
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003856 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003857 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003858 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003859 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003860
3861 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003862 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003863 status = nfserr_clientid_busy;
3864 goto out;
3865 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003866 status = mark_client_expired_locked(conf);
3867 if (status)
3868 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003869 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003870 } else if (unconf)
3871 clp = unconf;
3872 else {
3873 status = nfserr_stale_clientid;
3874 goto out;
3875 }
Andrew Elblededeb132016-06-15 12:52:08 -04003876 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003877 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003878 status = nfserr_wrong_cred;
3879 goto out;
3880 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003881 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003882out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003883 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003884 if (clp)
3885 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003886 return status;
3887}
3888
Andy Adamson069b6ad2009-04-03 08:27:58 +03003889__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003890nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3891 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003892{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003893 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003894 struct nfs4_client *clp = cstate->clp;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003895 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003896
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003897 if (rc->rca_one_fs) {
3898 if (!cstate->current_fh.fh_dentry)
3899 return nfserr_nofilehandle;
3900 /*
3901 * We don't take advantage of the rca_one_fs case.
3902 * That's OK, it's optional, we can safely ignore it.
3903 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003904 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003905 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003906
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003907 status = nfserr_complete_already;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003908 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003909 goto out;
3910
3911 status = nfserr_stale_clientid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003912 if (is_client_expired(clp))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003913 /*
3914 * The following error isn't really legal.
3915 * But we only get here if the client just explicitly
3916 * destroyed the client. Surely it no longer cares what
3917 * error it gets back on an operation for the dead
3918 * client.
3919 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003920 goto out;
3921
3922 status = nfs_ok;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05003923 nfsd4_client_record_create(clp);
3924 inc_reclaim_complete(clp);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003925out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003926 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003927}
3928
3929__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003930nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003931 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003933 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003934 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003936 struct nfs4_client *conf, *new;
3937 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003938 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003939 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3940
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003941 new = create_client(clname, rqstp, &clverifier);
3942 if (new == NULL)
3943 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003944 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003945 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003946 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003947 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003948 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003950 if (clp_used_exchangeid(conf))
3951 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003952 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04003953 trace_nfsd_clid_inuse_err(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 goto out;
3955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003957 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003958 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003959 unhash_client_locked(unconf);
Chuck Lever45f56da2020-04-05 14:40:25 -04003960 /* We need to handle only case 1: probable callback update */
Kinglong Mee41eb1672015-07-13 17:29:41 +08003961 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003963 gen_confirm(new, nn);
Chuck Lever45f56da2020-04-05 14:40:25 -04003964 }
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003965 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003966 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003967 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3969 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3970 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003971 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 status = nfs_ok;
3973out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003974 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003975 if (new)
3976 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003977 if (unconf)
3978 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 return status;
3980}
3981
3982
Al Virob37ad282006-10-19 23:28:59 -07003983__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003984nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003985 struct nfsd4_compound_state *cstate,
3986 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003988 struct nfsd4_setclientid_confirm *setclientid_confirm =
3989 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003990 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003991 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3993 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003994 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003995 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003997 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003999
Jeff Laytond20c11d2014-07-30 08:27:07 -04004000 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03004001 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03004002 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004003 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04004004 * We try hard to give out unique clientid's, so if we get an
4005 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004006 * the client may be buggy; this should never happen.
4007 *
4008 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05004009 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04004010 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04004011 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
4012 goto out;
4013 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
4014 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04004015 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004016 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04004017 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4018 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04004020 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004021 status = nfserr_stale_clientid;
4022 goto out;
4023 }
4024 status = nfs_ok;
4025 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04004026 old = unconf;
4027 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04004028 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04004029 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04004030 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4031 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04004032 status = nfserr_clid_inuse;
4033 if (client_has_state(old)
4034 && !same_creds(&unconf->cl_cred,
4035 &old->cl_cred))
4036 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04004037 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04004038 if (status) {
4039 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04004040 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04004041 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04004042 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04004043 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04004044 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07004045 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04004046 get_client_locked(conf);
4047 spin_unlock(&nn->client_lock);
4048 nfsd4_probe_callback(conf);
4049 spin_lock(&nn->client_lock);
4050 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04004052 spin_unlock(&nn->client_lock);
4053 if (old)
4054 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 return status;
4056}
4057
J. Bruce Fields32513b42011-10-13 16:00:16 -04004058static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
J. Bruce Fields32513b42011-10-13 16:00:16 -04004060 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4061}
4062
4063/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04004064static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
4065 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04004066{
Trond Myklebust950e0112014-06-30 11:48:31 -04004067 lockdep_assert_held(&state_lock);
4068
Elena Reshetova818a34e2017-10-20 12:53:30 +03004069 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004070 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04004071 INIT_LIST_HEAD(&fp->fi_stateids);
4072 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004073 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04004074 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04004075 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004076 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004077 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004078 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4079 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004080#ifdef CONFIG_NFSD_PNFS
4081 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02004082 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004083#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04004084 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085}
4086
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004087void
NeilBrowne60d4392005-06-23 22:03:01 -07004088nfsd4_free_slabs(void)
4089{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004090 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004091 kmem_cache_destroy(openowner_slab);
4092 kmem_cache_destroy(lockowner_slab);
4093 kmem_cache_destroy(file_slab);
4094 kmem_cache_destroy(stateid_slab);
4095 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004096 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07004097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
Bryan Schumaker72083392011-11-01 15:24:59 -04004099int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100nfsd4_init_slabs(void)
4101{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004102 client_slab = kmem_cache_create("nfsd4_clients",
4103 sizeof(struct nfs4_client), 0, 0, NULL);
4104 if (client_slab == NULL)
4105 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004106 openowner_slab = kmem_cache_create("nfsd4_openowners",
4107 sizeof(struct nfs4_openowner), 0, 0, NULL);
4108 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04004109 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004110 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08004111 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004112 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004113 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07004114 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09004115 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07004116 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004117 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07004118 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004119 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07004120 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004121 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07004122 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09004123 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07004124 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004125 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004126 odstate_slab = kmem_cache_create("nfsd4_odstate",
4127 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4128 if (odstate_slab == NULL)
4129 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07004131
Sachin Bhamare8287f002015-04-27 14:50:14 +02004132out_free_deleg_slab:
4133 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004134out_free_stateid_slab:
4135 kmem_cache_destroy(stateid_slab);
4136out_free_file_slab:
4137 kmem_cache_destroy(file_slab);
4138out_free_lockowner_slab:
4139 kmem_cache_destroy(lockowner_slab);
4140out_free_openowner_slab:
4141 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004142out_free_client_slab:
4143 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004144out:
NeilBrowne60d4392005-06-23 22:03:01 -07004145 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146}
4147
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004148static void init_nfs4_replay(struct nfs4_replay *rp)
4149{
4150 rp->rp_status = nfserr_serverfault;
4151 rp->rp_buflen = 0;
4152 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04004153 mutex_init(&rp->rp_mutex);
4154}
4155
4156static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4157 struct nfs4_stateowner *so)
4158{
4159 if (!nfsd4_has_session(cstate)) {
4160 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004161 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004162 }
4163}
4164
4165void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4166{
4167 struct nfs4_stateowner *so = cstate->replay_owner;
4168
4169 if (so != NULL) {
4170 cstate->replay_owner = NULL;
4171 mutex_unlock(&so->so_replay.rp_mutex);
4172 nfs4_put_stateowner(so);
4173 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004174}
4175
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004176static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177{
4178 struct nfs4_stateowner *sop;
4179
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004180 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004181 if (!sop)
4182 return NULL;
4183
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04004184 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004185 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004186 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004187 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004189
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004190 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004191 sop->so_client = clp;
4192 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04004193 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004194 return sop;
4195}
4196
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004197static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004198{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004199 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004200
Trond Myklebustd4f04892014-07-29 21:34:36 -04004201 list_add(&oo->oo_owner.so_strhash,
4202 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004203 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204}
4205
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004206static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4207{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004208 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004209}
4210
Jeff Layton6b180f02014-07-29 21:34:26 -04004211static void nfs4_free_openowner(struct nfs4_stateowner *so)
4212{
4213 struct nfs4_openowner *oo = openowner(so);
4214
4215 kmem_cache_free(openowner_slab, oo);
4216}
4217
4218static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004219 .so_unhash = nfs4_unhash_openowner,
4220 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004221};
4222
Andrew Elble7fc05642015-11-05 20:42:43 -05004223static struct nfs4_ol_stateid *
4224nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4225{
4226 struct nfs4_ol_stateid *local, *ret = NULL;
4227 struct nfs4_openowner *oo = open->op_openowner;
4228
4229 lockdep_assert_held(&fp->fi_lock);
4230
4231 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4232 /* ignore lock owners */
4233 if (local->st_stateowner->so_is_open_owner == 0)
4234 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004235 if (local->st_stateowner != &oo->oo_owner)
4236 continue;
4237 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004238 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004239 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05004240 break;
4241 }
4242 }
4243 return ret;
4244}
4245
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004246static __be32
4247nfsd4_verify_open_stid(struct nfs4_stid *s)
4248{
4249 __be32 ret = nfs_ok;
4250
4251 switch (s->sc_type) {
4252 default:
4253 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004254 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004255 case NFS4_CLOSED_STID:
4256 case NFS4_CLOSED_DELEG_STID:
4257 ret = nfserr_bad_stateid;
4258 break;
4259 case NFS4_REVOKED_DELEG_STID:
4260 ret = nfserr_deleg_revoked;
4261 }
4262 return ret;
4263}
4264
4265/* Lock the stateid st_mutex, and deal with races with CLOSE */
4266static __be32
4267nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4268{
4269 __be32 ret;
4270
Andrew Elble4f34bd02017-11-08 17:29:51 -05004271 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004272 ret = nfsd4_verify_open_stid(&stp->st_stid);
4273 if (ret != nfs_ok)
4274 mutex_unlock(&stp->st_mutex);
4275 return ret;
4276}
4277
4278static struct nfs4_ol_stateid *
4279nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4280{
4281 struct nfs4_ol_stateid *stp;
4282 for (;;) {
4283 spin_lock(&fp->fi_lock);
4284 stp = nfsd4_find_existing_open(fp, open);
4285 spin_unlock(&fp->fi_lock);
4286 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4287 break;
4288 nfs4_put_stid(&stp->st_stid);
4289 }
4290 return stp;
4291}
4292
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004293static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004294alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004295 struct nfsd4_compound_state *cstate)
4296{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004297 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004298 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004300 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4301 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004303 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004304 oo->oo_owner.so_is_open_owner = 1;
4305 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004306 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004307 if (nfsd4_has_session(cstate))
4308 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004309 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004310 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004311 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004312 spin_lock(&clp->cl_lock);
4313 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004314 if (ret == NULL) {
4315 hash_openowner(oo, clp, strhashval);
4316 ret = oo;
4317 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004318 nfs4_free_stateowner(&oo->oo_owner);
4319
Trond Myklebustd4f04892014-07-29 21:34:36 -04004320 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004321 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322}
4323
Andrew Elble7fc05642015-11-05 20:42:43 -05004324static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004325init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
Andrew Elble7fc05642015-11-05 20:42:43 -05004328 struct nfs4_openowner *oo = open->op_openowner;
4329 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004330 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004331
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004332 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004333 /* We are moving these outside of the spinlocks to avoid the warnings */
4334 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004335 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004336
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004337retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004338 spin_lock(&oo->oo_owner.so_client->cl_lock);
4339 spin_lock(&fp->fi_lock);
4340
4341 retstp = nfsd4_find_existing_open(fp, open);
4342 if (retstp)
4343 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004344
4345 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004346 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004347 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004348 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004349 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004350 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004351 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 stp->st_access_bmap = 0;
4353 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004354 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004355 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004356 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004357
4358out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004359 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004360 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004361 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004362 /* Handle races with CLOSE */
4363 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4364 nfs4_put_stid(&retstp->st_stid);
4365 goto retry;
4366 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004367 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004368 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004369 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004370 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004371 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372}
4373
Jeff Laytond3134b12014-07-29 21:34:32 -04004374/*
4375 * In the 4.0 case we need to keep the owners around a little while to handle
4376 * CLOSE replay. We still do need to release any file access that is held by
4377 * them before returning however.
4378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004380move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
Jeff Layton217526e2014-07-30 08:27:11 -04004382 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004383 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4384 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4385 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004386
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004387 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Jeff Laytonb401be222014-07-29 21:34:33 -04004389 /*
4390 * We know that we hold one reference via nfsd4_close, and another
4391 * "persistent" reference for the client. If the refcount is higher
4392 * than 2, then there are still calls in progress that are using this
4393 * stateid. We can't put the sc_file reference until they are finished.
4394 * Wait for the refcount to drop to 2. Since it has been unhashed,
4395 * there should be no danger of the refcount going back up again at
4396 * this point.
4397 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004398 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004399
Jeff Laytond3134b12014-07-29 21:34:32 -04004400 release_all_access(s);
4401 if (s->st_stid.sc_file) {
4402 put_nfs4_file(s->st_stid.sc_file);
4403 s->st_stid.sc_file = NULL;
4404 }
Jeff Layton217526e2014-07-30 08:27:11 -04004405
4406 spin_lock(&nn->client_lock);
4407 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004408 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004409 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004410 oo->oo_time = ktime_get_boottime_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004411 spin_unlock(&nn->client_lock);
4412 if (last)
4413 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414}
4415
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416/* search file_hashtbl[] for file */
4417static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04004418find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 struct nfs4_file *fp;
4421
Madhuparna Bhowmik36a80492020-02-13 20:33:31 +05304422 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4423 lockdep_is_held(&state_lock)) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02004424 if (fh_match(&fp->fi_fhandle, fh)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004425 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004426 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 }
4429 return NULL;
4430}
4431
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02004432struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04004433find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004434{
4435 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004436 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004437
Jeff Layton5b095e92014-10-23 08:01:02 -04004438 rcu_read_lock();
4439 fp = find_file_locked(fh, hashval);
4440 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004441 return fp;
4442}
4443
4444static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004445find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004446{
4447 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004448 unsigned int hashval = file_hashval(fh);
4449
4450 rcu_read_lock();
4451 fp = find_file_locked(fh, hashval);
4452 rcu_read_unlock();
4453 if (fp)
4454 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004455
4456 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04004457 fp = find_file_locked(fh, hashval);
4458 if (likely(fp == NULL)) {
4459 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04004460 fp = new;
4461 }
4462 spin_unlock(&state_lock);
4463
4464 return fp;
4465}
4466
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004467/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 * Called to check deny when READ with all zero stateid or
4469 * WRITE with all zero or all one stateid
4470 */
Al Virob37ad282006-10-19 23:28:59 -07004471static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4473{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004475 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
Trond Myklebustca943212014-07-23 16:17:39 -04004477 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07004478 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004479 return ret;
4480 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004481 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004482 if (fp->fi_share_deny & deny_type)
4483 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004484 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004485 put_nfs4_file(fp);
4486 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487}
4488
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004489static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004491 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004492 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4493 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004494
Trond Myklebust11b91642014-07-29 21:34:08 -04004495 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004496
Jeff Layton02e12152014-07-16 10:31:57 -04004497 /*
4498 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004499 * already holding inode->i_lock.
4500 *
Jeff Laytondff13992014-07-08 14:02:49 -04004501 * If the dl_time != 0, then we know that it has already been
4502 * queued for a lease break. Don't queue it again.
4503 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004504 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004505 if (dp->dl_time == 0) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004506 dp->dl_time = ktime_get_boottime_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004507 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004508 }
Jeff Layton02e12152014-07-16 10:31:57 -04004509 spin_unlock(&state_lock);
4510}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004512static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4513 struct rpc_task *task)
4514{
4515 struct nfs4_delegation *dp = cb_to_delegation(cb);
4516
J. Bruce Fields12ed22f2020-07-31 16:20:05 -04004517 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4518 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
Andrew Elblea4579742015-08-31 12:06:41 -04004519 return 1;
4520
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004521 switch (task->tk_status) {
4522 case 0:
4523 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004524 case -NFS4ERR_DELAY:
4525 rpc_delay(task, 2 * HZ);
4526 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004527 case -EBADHANDLE:
4528 case -NFS4ERR_BAD_STATEID:
4529 /*
4530 * Race: client probably got cb_recall before open reply
4531 * granting delegation.
4532 */
4533 if (dp->dl_retries--) {
4534 rpc_delay(task, 2 * HZ);
4535 return 0;
4536 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004537 fallthrough;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004538 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004539 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004540 }
4541}
4542
4543static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4544{
4545 struct nfs4_delegation *dp = cb_to_delegation(cb);
4546
4547 nfs4_put_stid(&dp->dl_stid);
4548}
4549
Julia Lawallc4cb8972015-11-21 22:57:39 +01004550static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004551 .prepare = nfsd4_cb_recall_prepare,
4552 .done = nfsd4_cb_recall_done,
4553 .release = nfsd4_cb_recall_release,
4554};
4555
Jeff Layton02e12152014-07-16 10:31:57 -04004556static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4557{
4558 /*
4559 * We're assuming the state code never drops its reference
4560 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004561 * callback (and since the lease code is serialized by the
4562 * i_lock) we know the server hasn't removed the lease yet, and
4563 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004564 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004565 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004566 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004567}
4568
Jeff Layton1c8c6012013-06-21 08:58:15 -04004569/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004570static bool
4571nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004572{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004573 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004574 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4575 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004576
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04004577 trace_nfsd_deleg_break(&dp->dl_stid.sc_stateid);
4578
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004579 /*
4580 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004581 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004582 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004583 */
4584 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Jeff Layton02e12152014-07-16 10:31:57 -04004586 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004587 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004588 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004589 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004590 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591}
4592
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004593static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4594{
4595 struct nfs4_delegation *dl = fl->fl_owner;
4596 struct svc_rqst *rqst;
4597 struct nfs4_client *clp;
4598
4599 if (!i_am_nfsd())
4600 return NULL;
4601 rqst = kthread_data(current);
J. Bruce Fields13956162020-09-25 10:12:39 -04004602 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4603 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
J. Bruce Fields34b09af2020-08-05 15:10:11 -04004604 return NULL;
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004605 clp = *(rqst->rq_lease_breaker);
4606 return dl->dl_stid.sc_client == clp;
4607}
4608
Jeff Laytonc45198e2014-09-01 07:12:07 -04004609static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004610nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4611 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612{
4613 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004614 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 else
4616 return -EAGAIN;
4617}
4618
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004619static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields28df3d12017-07-28 16:35:15 -04004620 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004621 .lm_break = nfsd_break_deleg_cb,
4622 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623};
4624
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004625static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4626{
4627 if (nfsd4_has_session(cstate))
4628 return nfs_ok;
4629 if (seqid == so->so_seqid - 1)
4630 return nfserr_replay_me;
4631 if (seqid == so->so_seqid)
4632 return nfs_ok;
4633 return nfserr_bad_seqid;
4634}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
J. Bruce Fields7950b532021-01-21 17:57:41 -05004636static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
4637 struct nfsd_net *nn)
4638{
4639 struct nfs4_client *found;
4640
4641 spin_lock(&nn->client_lock);
4642 found = find_confirmed_client(clid, sessions, nn);
4643 if (found)
4644 atomic_inc(&found->cl_rpc_users);
4645 spin_unlock(&nn->client_lock);
4646 return found;
4647}
4648
J. Bruce Fields460d2702021-01-21 17:57:40 -05004649static __be32 set_client(clientid_t *clid,
Jeff Layton4b24ca72014-06-30 11:48:44 -04004650 struct nfsd4_compound_state *cstate,
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004651 struct nfsd_net *nn)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004652{
Jeff Layton4b24ca72014-06-30 11:48:44 -04004653 if (cstate->clp) {
J. Bruce Fields7950b532021-01-21 17:57:41 -05004654 if (!same_clid(&cstate->clp->cl_clientid, clid))
Jeff Layton4b24ca72014-06-30 11:48:44 -04004655 return nfserr_stale_clientid;
4656 return nfs_ok;
4657 }
Jeff Layton4b24ca72014-06-30 11:48:44 -04004658 if (STALE_CLIENTID(clid, nn))
4659 return nfserr_stale_clientid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004660 /*
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004661 * We're in the 4.0 case (otherwise the SEQUENCE op would have
4662 * set cstate->clp), so session = false:
Jeff Layton4b24ca72014-06-30 11:48:44 -04004663 */
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004664 cstate->clp = lookup_clientid(clid, false, nn);
J. Bruce Fields7950b532021-01-21 17:57:41 -05004665 if (!cstate->clp)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004666 return nfserr_expired;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004667 return nfs_ok;
4668}
4669
Al Virob37ad282006-10-19 23:28:59 -07004670__be32
Andy Adamson66689582009-04-03 08:28:45 +03004671nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004672 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 clientid_t *clientid = &open->op_clientid;
4675 struct nfs4_client *clp = NULL;
4676 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004677 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004678 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
J. Bruce Fields32513b42011-10-13 16:00:16 -04004680 /*
4681 * In case we need it later, after we've already created the
4682 * file and don't want to risk a further failure:
4683 */
4684 open->op_file = nfsd4_alloc_file();
4685 if (open->op_file == NULL)
4686 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05004688 status = set_client(clientid, cstate, nn);
Trond Myklebust2d91e892014-06-30 11:48:46 -04004689 if (status)
4690 return status;
4691 clp = cstate->clp;
4692
Trond Myklebustd4f04892014-07-29 21:34:36 -04004693 strhashval = ownerstr_hashval(&open->op_owner);
4694 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004695 open->op_openowner = oo;
4696 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004697 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004699 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004700 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004701 release_openowner(oo);
4702 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004703 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004704 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004705 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4706 if (status)
4707 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004708 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004709new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004710 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004711 if (oo == NULL)
4712 return nfserr_jukebox;
4713 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004714alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004715 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004716 if (!open->op_stp)
4717 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004718
4719 if (nfsd4_has_session(cstate) &&
4720 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4721 open->op_odstate = alloc_clnt_odstate(clp);
4722 if (!open->op_odstate)
4723 return nfserr_jukebox;
4724 }
4725
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004726 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727}
4728
Al Virob37ad282006-10-19 23:28:59 -07004729static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004730nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4731{
4732 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4733 return nfserr_openmode;
4734 else
4735 return nfs_ok;
4736}
4737
Daniel Mackc47d8322011-05-16 16:38:14 +02004738static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004739{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004740 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4741}
4742
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004743static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004744{
4745 struct nfs4_stid *ret;
4746
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004747 ret = find_stateid_by_type(cl, s,
4748 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004749 if (!ret)
4750 return NULL;
4751 return delegstateid(ret);
4752}
4753
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004754static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4755{
4756 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4757 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4758}
4759
Al Virob37ad282006-10-19 23:28:59 -07004760static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004761nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004762 struct nfs4_delegation **dp)
4763{
4764 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004765 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004766 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004767
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004768 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4769 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004770 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004771 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4772 nfs4_put_stid(&deleg->dl_stid);
4773 if (cl->cl_minorversion)
4774 status = nfserr_deleg_revoked;
4775 goto out;
4776 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004777 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004778 status = nfs4_check_delegmode(deleg, flags);
4779 if (status) {
4780 nfs4_put_stid(&deleg->dl_stid);
4781 goto out;
4782 }
4783 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004784out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004785 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004786 return nfs_ok;
4787 if (status)
4788 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004789 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004790 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004791}
4792
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004793static inline int nfs4_access_to_access(u32 nfs4_access)
4794{
4795 int flags = 0;
4796
4797 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4798 flags |= NFSD_MAY_READ;
4799 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4800 flags |= NFSD_MAY_WRITE;
4801 return flags;
4802}
4803
Al Virob37ad282006-10-19 23:28:59 -07004804static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4806 struct nfsd4_open *open)
4807{
4808 struct iattr iattr = {
4809 .ia_valid = ATTR_SIZE,
4810 .ia_size = 0,
4811 };
4812 if (!open->op_truncate)
4813 return 0;
4814 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004815 return nfserr_inval;
Arnd Bergmann2a1aa482019-11-03 17:50:18 +01004816 return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817}
4818
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004819static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004820 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4821 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004822{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004823 struct nfsd_file *nf = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004824 __be32 status;
4825 int oflag = nfs4_access_to_omode(open->op_share_access);
4826 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004827 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004828
Trond Myklebustde186432014-07-10 14:07:26 -04004829 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004830
4831 /*
4832 * Are we trying to set a deny mode that would conflict with
4833 * current access?
4834 */
4835 status = nfs4_file_check_deny(fp, open->op_share_deny);
4836 if (status != nfs_ok) {
4837 spin_unlock(&fp->fi_lock);
4838 goto out;
4839 }
4840
4841 /* set access to the file */
4842 status = nfs4_file_get_access(fp, open->op_share_access);
4843 if (status != nfs_ok) {
4844 spin_unlock(&fp->fi_lock);
4845 goto out;
4846 }
4847
4848 /* Set access bits in stateid */
4849 old_access_bmap = stp->st_access_bmap;
4850 set_access(open->op_share_access, stp);
4851
4852 /* Set new deny mask */
4853 old_deny_bmap = stp->st_deny_bmap;
4854 set_deny(open->op_share_deny, stp);
4855 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4856
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004857 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004858 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004859 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004860 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004861 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004862 spin_lock(&fp->fi_lock);
4863 if (!fp->fi_fds[oflag]) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004864 fp->fi_fds[oflag] = nf;
4865 nf = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -04004866 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004867 }
Trond Myklebustde186432014-07-10 14:07:26 -04004868 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004869 if (nf)
4870 nfsd_file_put(nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004871
4872 status = nfsd4_truncate(rqstp, cur_fh, open);
4873 if (status)
4874 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004875out:
4876 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004877out_put_access:
4878 stp->st_access_bmap = old_access_bmap;
4879 nfs4_file_put_access(fp, open->op_share_access);
4880 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4881 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004882}
4883
Al Virob37ad282006-10-19 23:28:59 -07004884static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004885nfs4_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 -07004886{
Al Virob37ad282006-10-19 23:28:59 -07004887 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004888 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004890 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004891 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004892
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004893 /* test and set deny mode */
4894 spin_lock(&fp->fi_lock);
4895 status = nfs4_file_check_deny(fp, open->op_share_deny);
4896 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004897 set_deny(open->op_share_deny, stp);
4898 fp->fi_share_deny |=
4899 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4900 }
4901 spin_unlock(&fp->fi_lock);
4902
4903 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004906 status = nfsd4_truncate(rqstp, cur_fh, open);
4907 if (status != nfs_ok)
4908 reset_union_bmap_deny(old_deny_bmap, stp);
4909 return status;
4910}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004912/* Should we give out recallable state?: */
4913static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4914{
4915 if (clp->cl_cb_state == NFSD4_CB_UP)
4916 return true;
4917 /*
4918 * In the sessions case, since we don't have to establish a
4919 * separate connection for callbacks, we assume it's OK
4920 * until we hear otherwise:
4921 */
4922 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4923}
4924
J. Bruce Fields653e5142018-02-15 22:08:45 -05004925static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4926 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004927{
4928 struct file_lock *fl;
4929
4930 fl = locks_alloc_lock();
4931 if (!fl)
4932 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004933 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004934 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004935 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4936 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004937 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004938 fl->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004939 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004940 return fl;
4941}
4942
Jeff Layton0b266932014-07-25 07:34:25 -04004943static struct nfs4_delegation *
4944nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004945 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004946{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004947 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004948 struct nfs4_delegation *dp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004949 struct nfsd_file *nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004950 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004951
J. Bruce Fields353601e2018-02-16 14:29:42 -05004952 /*
4953 * The fi_had_conflict and nfs_get_existing_delegation checks
4954 * here are just optimizations; we'll need to recheck them at
4955 * the end:
4956 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004957 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004958 return ERR_PTR(-EAGAIN);
4959
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004960 nf = find_readable_file(fp);
4961 if (!nf) {
J. Bruce Fields6ee65a772021-03-08 10:52:29 -05004962 /* We should always have a readable file here */
4963 WARN_ON_ONCE(1);
4964 return ERR_PTR(-EBADF);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004965 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004966 spin_lock(&state_lock);
4967 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004968 if (nfs4_delegation_exists(clp, fp))
4969 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004970 else if (!fp->fi_deleg_file) {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004971 fp->fi_deleg_file = nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004972 /* increment early to prevent fi_deleg_file from being
4973 * cleared */
4974 fp->fi_delegees = 1;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004975 nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004976 } else
4977 fp->fi_delegees++;
4978 spin_unlock(&fp->fi_lock);
4979 spin_unlock(&state_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004980 if (nf)
4981 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004982 if (status)
4983 return ERR_PTR(status);
4984
4985 status = -ENOMEM;
4986 dp = alloc_init_deleg(clp, fp, fh, odstate);
4987 if (!dp)
4988 goto out_delegees;
4989
4990 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4991 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04004992 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004993
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004994 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004995 if (fl)
4996 locks_free_lock(fl);
4997 if (status)
4998 goto out_clnt_odstate;
4999
5000 spin_lock(&state_lock);
5001 spin_lock(&fp->fi_lock);
5002 if (fp->fi_had_conflict)
5003 status = -EAGAIN;
5004 else
5005 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04005006 spin_unlock(&fp->fi_lock);
5007 spin_unlock(&state_lock);
5008
5009 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04005010 goto out_unlock;
5011
Jeff Layton0b266932014-07-25 07:34:25 -04005012 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04005013out_unlock:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005014 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005015out_clnt_odstate:
5016 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05005017 nfs4_put_stid(&dp->dl_stid);
5018out_delegees:
5019 put_deleg_file(fp);
5020 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05005021}
5022
Benny Halevy4aa89132012-02-21 14:16:44 -08005023static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5024{
5025 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5026 if (status == -EAGAIN)
5027 open->op_why_no_deleg = WND4_CONTENTION;
5028 else {
5029 open->op_why_no_deleg = WND4_RESOURCE;
5030 switch (open->op_deleg_want) {
5031 case NFS4_SHARE_WANT_READ_DELEG:
5032 case NFS4_SHARE_WANT_WRITE_DELEG:
5033 case NFS4_SHARE_WANT_ANY_DELEG:
5034 break;
5035 case NFS4_SHARE_WANT_CANCEL:
5036 open->op_why_no_deleg = WND4_CANCELLED;
5037 break;
5038 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005039 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08005040 }
5041 }
5042}
5043
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044/*
5045 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04005046 *
5047 * Note we don't support write delegations, and won't until the vfs has
5048 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 */
5050static void
Jeff Layton4cf59222014-07-25 07:34:24 -04005051nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
5052 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053{
5054 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04005055 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5056 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05005057 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005058 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005060 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07005061 open->op_recall = 0;
5062 switch (open->op_claim_type) {
5063 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05005064 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07005065 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04005066 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5067 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005068 break;
5069 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00005070 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005071 /*
5072 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005073 * had the chance to reclaim theirs, *and* until
5074 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005075 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005076 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005077 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005078 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04005079 goto out_no_deleg;
J. Bruce Fields6ee65a772021-03-08 10:52:29 -05005080 /*
5081 * Also, if the file was opened for write or
5082 * create, there's a good chance the client's
5083 * about to write to it, resulting in an
5084 * immediate recall (since we don't support
5085 * write delegations):
5086 */
5087 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
5088 goto out_no_deleg;
5089 if (open->op_create == NFS4_OPEN_CREATE)
5090 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005091 break;
5092 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005093 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005094 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005095 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04005096 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005097 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005099 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
Hou Tao3caf9172020-08-28 15:02:55 +08005101 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
J. Bruce Fields99c41512013-05-21 16:21:25 -04005102 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04005103 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005104 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005105out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005106 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5107 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005108 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04005109 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005110 open->op_recall = 1;
5111 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04005112
5113 /* 4.1 client asking for a delegation? */
5114 if (open->op_deleg_want)
5115 nfsd4_open_deleg_none_ext(open, status);
5116 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117}
5118
Benny Halevye27f49c2012-02-21 14:16:54 -08005119static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5120 struct nfs4_delegation *dp)
5121{
5122 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5123 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5124 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5125 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5126 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5127 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5128 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5129 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5130 }
5131 /* Otherwise the client must be confused wanting a delegation
5132 * it already has, therefore we don't return
5133 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5134 */
5135}
5136
Al Virob37ad282006-10-19 23:28:59 -07005137__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5139{
Andy Adamson66689582009-04-03 08:28:45 +03005140 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005141 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005143 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07005144 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005145 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005146 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 /*
5149 * Lookup file; if found, lookup stateid and check open request,
5150 * and check for delegations in the process of being recalled.
5151 * If not found, create the nfs4_file struct
5152 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005153 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04005154 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04005155 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07005156 if (status)
5157 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005158 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04005160 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07005161 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04005162 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07005163 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 }
5165
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005166 if (!stp) {
5167 stp = init_open_stateid(fp, open);
5168 if (!open->op_stp)
5169 new_stp = true;
5170 }
5171
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 /*
5173 * OPEN the file, or upgrade an existing OPEN.
5174 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005175 *
5176 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005178 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005180 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005181 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005182 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005186 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5187 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005188 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005189 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005190 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005191 goto out;
5192 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005193
5194 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5195 open->op_odstate);
5196 if (stp->st_clnt_odstate == open->op_odstate)
5197 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005199
Jeff Layton9767feb2015-10-01 09:05:50 -04005200 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005201 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
Benny Halevyd24433c2012-02-16 20:57:17 +02005203 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005204 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5205 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5206 open->op_why_no_deleg = WND4_NOT_WANTED;
5207 goto nodeleg;
5208 }
5209 }
5210
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 /*
5212 * Attempt to hand out a delegation. No error return, because the
5213 * OPEN succeeds even if we fail.
5214 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005215 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005216nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 status = nfs_ok;
Hou Tao3caf9172020-08-28 15:02:55 +08005218 trace_nfsd_open(&stp->st_stid.sc_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005220 /* 4.1 client trying to upgrade/downgrade delegation? */
5221 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005222 open->op_deleg_want)
5223 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005224
NeilBrown13cd2182005-06-23 22:03:10 -07005225 if (fp)
5226 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005227 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005228 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 /*
5230 * To finish the open response, we just need to set the rflags.
5231 */
5232 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005233 if (nfsd4_has_session(&resp->cstate))
5234 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5235 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005237
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005238 if (dp)
5239 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005240 if (stp)
5241 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
5243 return status;
5244}
5245
Jeff Layton58fb12e2014-07-29 21:34:27 -04005246void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005247 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005248{
5249 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005250 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005251
Jeff Laytond3134b12014-07-29 21:34:32 -04005252 nfsd4_cstate_assign_replay(cstate, so);
5253 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005254 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005255 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005256 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005257 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005258 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005259 if (open->op_odstate)
5260 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005261}
5262
Al Virob37ad282006-10-19 23:28:59 -07005263__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005264nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005265 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005267 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005269 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005270 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005272 trace_nfsd_clid_renew(clid);
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005273 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005274 if (status)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005275 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005276 clp = cstate->clp;
NeilBrownea1da632005-06-23 22:04:17 -07005277 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005278 && clp->cl_cb_state != NFSD4_CB_UP)
J. Bruce Fieldsb4587eb2021-01-21 17:57:39 -05005279 return nfserr_cb_path_down;
5280 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281}
5282
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005283void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005284nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005285{
Jeff Layton33dcc482012-04-10 11:08:48 -04005286 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005287 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005288 return;
5289
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005290 trace_nfsd_grace_complete(nn);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005291 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005292 /*
5293 * If the server goes down again right now, an NFSv4
5294 * client will still be allowed to reclaim after it comes back up,
5295 * even if it hasn't yet had a chance to reclaim state this time.
5296 *
5297 */
Jeff Layton919b8042014-09-12 16:40:20 -04005298 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005299 /*
5300 * At this point, NFSv4 clients can still reclaim. But if the
5301 * server crashes, any that have not yet reclaimed will be out
5302 * of luck on the next boot.
5303 *
5304 * (NFSv4.1+ clients are considered to have reclaimed once they
5305 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5306 * have reclaimed after their first OPEN.)
5307 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005308 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005309 /*
5310 * At this point, and once lockd and/or any other containers
5311 * exit their grace period, further reclaims will fail and
5312 * regular locking can resume.
5313 */
NeilBrowna76b4312005-06-23 22:04:01 -07005314}
5315
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005316/*
5317 * If we've waited a lease period but there are still clients trying to
5318 * reclaim, wait a little longer to give them a chance to finish.
5319 */
5320static bool clients_still_reclaiming(struct nfsd_net *nn)
5321{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005322 time64_t double_grace_period_end = nn->boot_time +
5323 2 * nn->nfsd4_lease;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005324
Scott Mayhew362063a2019-03-26 18:06:28 -04005325 if (nn->track_reclaim_completes &&
5326 atomic_read(&nn->nr_reclaim_complete) ==
5327 nn->reclaim_str_hashtbl_size)
5328 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005329 if (!nn->somebody_reclaimed)
5330 return false;
5331 nn->somebody_reclaimed = false;
5332 /*
5333 * If we've given them *two* lease times to reclaim, and they're
5334 * still not done, give up:
5335 */
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005336 if (ktime_get_boottime_seconds() > double_grace_period_end)
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005337 return false;
5338 return true;
5339}
5340
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005341struct laundry_time {
5342 time64_t cutoff;
5343 time64_t new_timeo;
5344};
5345
5346static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
5347{
5348 time64_t time_remaining;
5349
5350 if (last_refresh < lt->cutoff)
5351 return true;
5352 time_remaining = last_refresh - lt->cutoff;
5353 lt->new_timeo = min(lt->new_timeo, time_remaining);
5354 return false;
5355}
5356
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005357static time64_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005358nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359{
5360 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005361 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005363 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005364 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 struct list_head *pos, *next, reaplist;
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005366 struct laundry_time lt = {
5367 .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
5368 .new_timeo = nn->nfsd4_lease
5369 };
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005370 struct nfs4_cpntf_state *cps;
5371 copy_stateid_t *cps_t;
5372 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005374 if (clients_still_reclaiming(nn)) {
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005375 lt.new_timeo = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005376 goto out;
5377 }
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005378 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005379 INIT_LIST_HEAD(&reaplist);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005380
5381 spin_lock(&nn->s2s_cp_lock);
5382 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5383 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5384 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005385 state_expired(&lt, cps->cpntf_time))
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005386 _free_cpntf_state_locked(nn, cps);
5387 }
5388 spin_unlock(&nn->s2s_cp_lock);
5389
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005390 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005391 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 clp = list_entry(pos, struct nfs4_client, cl_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005393 if (!state_expired(&lt, clp->cl_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 break;
J. Bruce Fields221a6872013-04-01 22:23:49 -04005395 if (mark_client_expired_locked(clp)) {
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005396 trace_nfsd_clid_expired(&clp->cl_clientid);
Benny Halevyd7682982010-05-12 00:13:54 +03005397 continue;
5398 }
Trond Myklebust4864af92014-07-30 08:27:03 -04005399 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005400 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005401 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005402 list_for_each_safe(pos, next, &reaplist) {
5403 clp = list_entry(pos, struct nfs4_client, cl_lru);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005404 trace_nfsd_clid_purged(&clp->cl_clientid);
Trond Myklebust4864af92014-07-30 08:27:03 -04005405 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 expire_client(clp);
5407 }
Benny Halevycdc975052014-05-30 09:09:30 -04005408 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005409 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005411 if (!state_expired(&lt, dp->dl_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 break;
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005413 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005414 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 }
Benny Halevycdc975052014-05-30 09:09:30 -04005416 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005417 while (!list_empty(&reaplist)) {
5418 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5419 dl_recall_lru);
5420 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005421 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 }
Jeff Layton217526e2014-07-30 08:27:11 -04005423
5424 spin_lock(&nn->client_lock);
5425 while (!list_empty(&nn->close_lru)) {
5426 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5427 oo_close_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005428 if (!state_expired(&lt, oo->oo_time))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 break;
Jeff Layton217526e2014-07-30 08:27:11 -04005430 list_del_init(&oo->oo_close_lru);
5431 stp = oo->oo_last_closed_stid;
5432 oo->oo_last_closed_stid = NULL;
5433 spin_unlock(&nn->client_lock);
5434 nfs4_put_stid(&stp->st_stid);
5435 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 }
Jeff Layton217526e2014-07-30 08:27:11 -04005437 spin_unlock(&nn->client_lock);
5438
Jeff Layton7919d0a2016-09-16 16:28:25 -04005439 /*
5440 * It's possible for a client to try and acquire an already held lock
5441 * that is being held for a long time, and then lose interest in it.
5442 * So, we clean out any un-revisited request after a lease period
5443 * under the assumption that the client is no longer interested.
5444 *
5445 * RFC5661, sec. 9.6 states that the client must not rely on getting
5446 * notifications and must continue to poll for locks, even when the
5447 * server supports them. Thus this shouldn't lead to clients blocking
5448 * indefinitely once the lock does become free.
5449 */
5450 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005451 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005452 while (!list_empty(&nn->blocked_locks_lru)) {
5453 nbl = list_first_entry(&nn->blocked_locks_lru,
5454 struct nfsd4_blocked_lock, nbl_lru);
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005455 if (!state_expired(&lt, nbl->nbl_time))
Jeff Layton7919d0a2016-09-16 16:28:25 -04005456 break;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005457 list_move(&nbl->nbl_lru, &reaplist);
5458 list_del_init(&nbl->nbl_list);
5459 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005460 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005461
5462 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005463 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005464 struct nfsd4_blocked_lock, nbl_lru);
5465 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005466 free_blocked_lock(nbl);
5467 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005468out:
J. Bruce Fields7f7e7a42021-03-02 10:46:23 -05005469 return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470}
5471
Harvey Harrisona254b242008-02-20 12:49:00 -08005472static struct workqueue_struct *laundry_wq;
5473static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005474
5475static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005476laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005478 time64_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005479 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005480 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5481 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005483 t = nfs4_laundromat(nn);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005484 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485}
5486
Jeff Layton8fcd4612015-07-30 06:57:46 -04005487static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005488{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005489 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005490 return nfserr_bad_stateid;
5491 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492}
5493
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005495access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005497 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5498 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5499 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500}
5501
5502static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005503access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005505 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5506 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507}
5508
5509static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005510__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511{
Al Virob37ad282006-10-19 23:28:59 -07005512 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
J. Bruce Fields02921912010-07-29 15:16:59 -04005514 /* For lock stateid's, we test the parent open, not the lock: */
5515 if (stp->st_openstp)
5516 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005517 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005519 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 goto out;
5521 status = nfs_ok;
5522out:
5523 return status;
5524}
5525
Al Virob37ad282006-10-19 23:28:59 -07005526static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005527check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005529 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005531 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005532 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 * conflicting state; so we must wait out the grace period. */
5534 return nfserr_grace;
5535 } else if (flags & WR_STATE)
5536 return nfs4_share_conflict(current_fh,
5537 NFS4_SHARE_DENY_WRITE);
5538 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5539 return nfs4_share_conflict(current_fh,
5540 NFS4_SHARE_DENY_READ);
5541}
5542
5543/*
5544 * Allow READ/WRITE during grace period on recovered state only for files
5545 * that are not able to provide mandatory locking.
5546 */
5547static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005548grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005550 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551}
5552
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005553static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005554{
Andy Adamson66689582009-04-03 08:28:45 +03005555 /*
5556 * When sessions are used the stateid generation number is ignored
5557 * when it is zero.
5558 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005559 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005560 return nfs_ok;
5561
5562 if (in->si_generation == ref->si_generation)
5563 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005564
J. Bruce Fields0836f582008-01-26 19:08:12 -05005565 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005566 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005567 return nfserr_bad_stateid;
5568 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005569 * However, we could see a stateid from the past, even from a
5570 * non-buggy client. For example, if the client sends a lock
5571 * while some IO is outstanding, the lock may bump si_generation
5572 * while the IO is still in flight. The client could avoid that
5573 * situation by waiting for responses on all the IO requests,
5574 * but better performance may result in retrying IO that
5575 * receives an old_stateid error if requests are rarely
5576 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005577 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005578 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005579}
5580
Trond Myklebust03da3162017-11-03 08:00:16 -04005581static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5582{
5583 __be32 ret;
5584
5585 spin_lock(&s->sc_lock);
5586 ret = nfsd4_verify_open_stid(s);
5587 if (ret == nfs_ok)
5588 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5589 spin_unlock(&s->sc_lock);
5590 return ret;
5591}
5592
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005593static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5594{
5595 if (ols->st_stateowner->so_is_open_owner &&
5596 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5597 return nfserr_bad_stateid;
5598 return nfs_ok;
5599}
5600
Chuck Lever7df302f2012-05-29 13:56:37 -04005601static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005602{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005603 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005604 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005605
Andrew Elbleae254dac2017-11-09 13:41:10 -05005606 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5607 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005608 return status;
J. Bruce Fields663e36f2020-03-19 10:18:49 -04005609 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005610 return status;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005611 spin_lock(&cl->cl_lock);
5612 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005613 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005614 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005615 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005616 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005617 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005618 switch (s->sc_type) {
5619 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005620 status = nfs_ok;
5621 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005622 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005623 status = nfserr_deleg_revoked;
5624 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005625 case NFS4_OPEN_STID:
5626 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005627 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005628 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005629 default:
5630 printk("unknown stateid type %x\n", s->sc_type);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05005631 fallthrough;
J. Bruce Fields23340032013-04-09 17:42:28 -04005632 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005633 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005634 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005635 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005636out_unlock:
5637 spin_unlock(&cl->cl_lock);
5638 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005639}
5640
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005641__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005642nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5643 stateid_t *stateid, unsigned char typemask,
5644 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005645{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005646 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005647 bool return_revoked = false;
5648
5649 /*
5650 * only return revoked delegations if explicitly asked.
5651 * otherwise we report revoked or bad_stateid status.
5652 */
5653 if (typemask & NFS4_REVOKED_DELEG_STID)
5654 return_revoked = true;
5655 else if (typemask & NFS4_DELEG_STID)
5656 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005657
Andrew Elbleae254dac2017-11-09 13:41:10 -05005658 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5659 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005660 return nfserr_bad_stateid;
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05005661 status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005662 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005663 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005664 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005665 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005666 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005667 if (status)
5668 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005669 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005670 if (!*s)
5671 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005672 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5673 nfs4_put_stid(*s);
5674 if (cstate->minorversion)
5675 return nfserr_deleg_revoked;
5676 return nfserr_bad_stateid;
5677 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005678 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005679}
5680
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005681static struct nfsd_file *
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005682nfs4_find_file(struct nfs4_stid *s, int flags)
5683{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005684 if (!s)
5685 return NULL;
5686
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005687 switch (s->sc_type) {
5688 case NFS4_DELEG_STID:
5689 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5690 return NULL;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005691 return nfsd_file_get(s->sc_file->fi_deleg_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005692 case NFS4_OPEN_STID:
5693 case NFS4_LOCK_STID:
5694 if (flags & RD_STATE)
5695 return find_readable_file(s->sc_file);
5696 else
5697 return find_writeable_file(s->sc_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005698 }
5699
5700 return NULL;
5701}
5702
5703static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005704nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005705{
5706 __be32 status;
5707
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005708 status = nfsd4_check_openowner_confirmed(ols);
5709 if (status)
5710 return status;
5711 return nfs4_check_openmode(ols, flags);
5712}
5713
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005714static __be32
5715nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
Jeff Layton5c4583b2019-08-18 14:18:54 -04005716 struct nfsd_file **nfp, int flags)
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005717{
5718 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005719 struct nfsd_file *nf;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005720 __be32 status;
5721
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005722 nf = nfs4_find_file(s, flags);
5723 if (nf) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005724 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5725 acc | NFSD_MAY_OWNER_OVERRIDE);
Jeff Layton5c4583b2019-08-18 14:18:54 -04005726 if (status) {
5727 nfsd_file_put(nf);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005728 goto out;
Jeff Layton5c4583b2019-08-18 14:18:54 -04005729 }
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005730 } else {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005731 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005732 if (status)
5733 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005734 }
Jeff Layton5c4583b2019-08-18 14:18:54 -04005735 *nfp = nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005736out:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005737 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005738}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005739static void
5740_free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5741{
5742 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5743 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5744 return;
5745 list_del(&cps->cp_list);
5746 idr_remove(&nn->s2s_cp_stateids,
5747 cps->cp_stateid.stid.si_opaque.so_id);
5748 kfree(cps);
5749}
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005750/*
5751 * A READ from an inter server to server COPY will have a
5752 * copy stateid. Look up the copy notify stateid from the
5753 * idr structure and take a reference on it.
5754 */
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005755__be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5756 struct nfs4_client *clp,
5757 struct nfs4_cpntf_state **cps)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005758{
5759 copy_stateid_t *cps_t;
5760 struct nfs4_cpntf_state *state = NULL;
5761
5762 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5763 return nfserr_bad_stateid;
5764 spin_lock(&nn->s2s_cp_lock);
5765 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5766 if (cps_t) {
5767 state = container_of(cps_t, struct nfs4_cpntf_state,
5768 cp_stateid);
Dan Carpenter5277a792019-12-04 10:59:36 +03005769 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5770 state = NULL;
5771 goto unlock;
5772 }
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005773 if (!clp)
5774 refcount_inc(&state->cp_stateid.sc_count);
5775 else
5776 _free_cpntf_state_locked(nn, state);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005777 }
Dan Carpenter5277a792019-12-04 10:59:36 +03005778unlock:
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005779 spin_unlock(&nn->s2s_cp_lock);
5780 if (!state)
5781 return nfserr_bad_stateid;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005782 if (!clp && state)
5783 *cps = state;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005784 return 0;
5785}
5786
5787static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5788 struct nfs4_stid **stid)
5789{
5790 __be32 status;
5791 struct nfs4_cpntf_state *cps = NULL;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005792 struct nfs4_client *found;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005793
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005794 status = manage_cpntf_state(nn, st, NULL, &cps);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005795 if (status)
5796 return status;
5797
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005798 cps->cpntf_time = ktime_get_boottime_seconds();
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005799
5800 status = nfserr_expired;
5801 found = lookup_clientid(&cps->cp_p_clid, true, nn);
5802 if (!found)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005803 goto out;
J. Bruce Fields47fdb222021-01-21 17:57:42 -05005804
5805 *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
5806 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
5807 if (*stid)
5808 status = nfs_ok;
5809 else
5810 status = nfserr_bad_stateid;
5811
5812 put_client_renew(found);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005813out:
5814 nfs4_put_cpntf_state(nn, cps);
5815 return status;
5816}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005817
5818void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5819{
5820 spin_lock(&nn->s2s_cp_lock);
5821 _free_cpntf_state_locked(nn, cps);
5822 spin_unlock(&nn->s2s_cp_lock);
5823}
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005824
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005826 * Checks for stateid operations
5827 */
Al Virob37ad282006-10-19 23:28:59 -07005828__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005829nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005830 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005831 stateid_t *stateid, int flags, struct nfsd_file **nfp,
5832 struct nfs4_stid **cstid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005834 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005835 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005836 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005837 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005838 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
Jeff Layton5c4583b2019-08-18 14:18:54 -04005840 if (nfp)
5841 *nfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005843 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 return nfserr_grace;
5845
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005846 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5847 status = check_special_stateids(net, fhp, stateid, flags);
5848 goto done;
5849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005851 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005852 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005853 &s, nn);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005854 if (status == nfserr_bad_stateid)
5855 status = find_cpntf_state(nn, stateid, &s);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005856 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005857 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005858 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005859 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005860 if (status)
5861 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005862
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005863 switch (s->sc_type) {
5864 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005865 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005866 break;
5867 case NFS4_OPEN_STID:
5868 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005869 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005870 break;
5871 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005872 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005873 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005875 if (status)
5876 goto out;
5877 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005878
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005879done:
Jeff Layton5c4583b2019-08-18 14:18:54 -04005880 if (status == nfs_ok && nfp)
5881 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882out:
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005883 if (s) {
5884 if (!status && cstid)
5885 *cstid = s;
5886 else
5887 nfs4_put_stid(s);
5888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 return status;
5890}
5891
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005892/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005893 * Test if the stateid is valid
5894 */
5895__be32
5896nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005897 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005898{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005899 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005900 struct nfsd4_test_stateid_id *stateid;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05005901 struct nfs4_client *cl = cstate->clp;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005902
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005903 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005904 stateid->ts_id_status =
5905 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005906
Bryan Schumaker17456802011-07-13 10:50:48 -04005907 return nfs_ok;
5908}
5909
Chuck Lever42691392016-08-11 10:37:30 -04005910static __be32
5911nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5912{
5913 struct nfs4_ol_stateid *stp = openlockstateid(s);
5914 __be32 ret;
5915
Trond Myklebust659aefb2017-11-03 08:00:13 -04005916 ret = nfsd4_lock_ol_stateid(stp);
5917 if (ret)
5918 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005919
5920 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5921 if (ret)
5922 goto out;
5923
5924 ret = nfserr_locks_held;
5925 if (check_for_locks(stp->st_stid.sc_file,
5926 lockowner(stp->st_stateowner)))
5927 goto out;
5928
5929 release_lock_stateid(stp);
5930 ret = nfs_ok;
5931
5932out:
5933 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005934out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005935 nfs4_put_stid(s);
5936 return ret;
5937}
5938
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005939__be32
5940nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005941 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005942{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005943 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005944 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005945 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005946 struct nfs4_delegation *dp;
J. Bruce Fieldsec596592021-01-21 17:57:45 -05005947 struct nfs4_client *cl = cstate->clp;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005948 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005949
Jeff Layton1af71cc2014-07-29 21:34:14 -04005950 spin_lock(&cl->cl_lock);
5951 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005952 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005953 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005954 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005955 switch (s->sc_type) {
5956 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005957 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005958 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005959 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005960 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5961 if (ret)
5962 break;
5963 ret = nfserr_locks_held;
5964 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005965 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005966 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005967 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005968 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005969 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005970 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005971 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005972 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005973 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005974 list_del_init(&dp->dl_recall_lru);
5975 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005976 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005977 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005978 goto out;
5979 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005980 }
Trond Myklebust03da3162017-11-03 08:00:16 -04005981 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005982out_unlock:
5983 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005984out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005985 return ret;
5986}
5987
NeilBrown4c4cd222005-07-07 17:59:27 -07005988static inline int
5989setlkflg (int type)
5990{
5991 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5992 RD_STATE : WR_STATE;
5993}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005995static __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 -04005996{
5997 struct svc_fh *current_fh = &cstate->current_fh;
5998 struct nfs4_stateowner *sop = stp->st_stateowner;
5999 __be32 status;
6000
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006001 status = nfsd4_check_seqid(cstate, sop, seqid);
6002 if (status)
6003 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04006004 status = nfsd4_lock_ol_stateid(stp);
6005 if (status != nfs_ok)
6006 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006007 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04006008 if (status == nfs_ok)
6009 status = nfs4_check_fh(current_fh, &stp->st_stid);
6010 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006011 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04006012 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006013}
6014
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015/*
6016 * Checks for sequence id mutating operations.
6017 */
Al Virob37ad282006-10-19 23:28:59 -07006018static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03006019nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04006020 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006021 struct nfs4_ol_stateid **stpp,
6022 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023{
J. Bruce Fields0836f582008-01-26 19:08:12 -05006024 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006025 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006026 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006028 trace_nfsd_preprocess(seqid, stateid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006029
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006031 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006032 if (status)
6033 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006034 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04006035 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006037 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006038 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006039 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04006040 else
6041 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04006042 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006043}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05006044
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006045static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6046 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006047{
6048 __be32 status;
6049 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006050 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006052 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006053 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04006054 if (status)
6055 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006056 oo = openowner(stp->st_stateowner);
6057 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006058 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006059 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07006060 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04006061 }
6062 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07006063 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064}
6065
Al Virob37ad282006-10-19 23:28:59 -07006066__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006067nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006068 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006070 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07006071 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006072 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006073 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006074 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
Al Viroa6a9f182013-09-16 10:57:01 -04006076 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6077 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006079 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07006080 if (status)
6081 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006083 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006084 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006085 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006086 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006087 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006088 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006089 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006090 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006091 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04006092 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006093 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04006094 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04006095 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006096 mutex_unlock(&stp->st_mutex);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006097 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
Jeff Layton2a4317c2012-03-21 16:42:43 -04006098 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006099 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04006100put_stateid:
6101 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006103 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 return status;
6105}
6106
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006107static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108{
Jeff Layton82c5ff12012-05-11 09:45:13 -04006109 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006110 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04006111 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006112 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006113}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04006114
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006115static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6116{
6117 switch (to_access) {
6118 case NFS4_SHARE_ACCESS_READ:
6119 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6120 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6121 break;
6122 case NFS4_SHARE_ACCESS_WRITE:
6123 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6124 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6125 break;
6126 case NFS4_SHARE_ACCESS_BOTH:
6127 break;
6128 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006129 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 }
6131}
6132
Al Virob37ad282006-10-19 23:28:59 -07006133__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006134nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006135 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006137 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07006138 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006139 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006140 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141
Al Viroa6a9f182013-09-16 10:57:01 -04006142 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6143 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04006145 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02006146 if (od->od_deleg_want)
6147 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6148 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006150 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006151 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006152 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04006155 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006156 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006158 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04006160 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006161 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006163 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006165 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04006166 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04006167 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006169put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006170 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006171 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006173 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 return status;
6175}
6176
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006177static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6178{
Trond Myklebustacf92952014-06-30 11:48:37 -04006179 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04006180 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04006181 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006182
Jeff Layton2c41beb2014-07-29 21:34:41 -04006183 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04006184 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006185
Jeff Laytond83017f2014-07-29 21:34:42 -04006186 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04006187 if (unhashed)
6188 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04006189 spin_unlock(&clp->cl_lock);
6190 free_ol_stateid_reaplist(&reaplist);
6191 } else {
6192 spin_unlock(&clp->cl_lock);
6193 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04006194 if (unhashed)
6195 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04006196 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04006197}
6198
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199/*
6200 * nfs4_unlock_state() called after encode
6201 */
Al Virob37ad282006-10-19 23:28:59 -07006202__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006203nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006204 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006206 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07006207 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006208 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006209 struct net *net = SVC_NET(rqstp);
6210 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
Al Viroa6a9f182013-09-16 10:57:01 -04006212 dprintk("NFSD: nfsd4_close on file %pd\n",
6213 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006215 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6216 &close->cl_stateid,
6217 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006218 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006219 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006220 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006222
6223 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006224
6225 /*
6226 * Technically we don't _really_ have to increment or copy it, since
6227 * it should just be gone after this operation and we clobber the
6228 * copied value below, but we continue to do so here just to ensure
6229 * that racing ops see that there was a state change.
6230 */
Jeff Layton9767feb2015-10-01 09:05:50 -04006231 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006233 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006234 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006235
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006236 /* v4.1+ suggests that we send a special stateid in here, since the
6237 * clients should just ignore this anyway. Since this is not useful
6238 * for v4.0 clients either, we set it to the special close_stateid
6239 * universally.
6240 *
6241 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6242 */
6243 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04006244
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006245 /* put reference from nfs4_preprocess_seqid_op */
6246 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 return status;
6249}
6250
Al Virob37ad282006-10-19 23:28:59 -07006251__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006252nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006253 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006255 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006256 struct nfs4_delegation *dp;
6257 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006258 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07006259 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006260 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006262 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006263 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006265 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006266 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006267 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006268 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04006269 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006270 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04006271 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006272
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006273 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006274put_stateid:
6275 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276out:
6277 return status;
6278}
6279
Benny Halevy87df4de2008-12-15 19:42:03 +02006280static inline u64
6281end_offset(u64 start, u64 len)
6282{
6283 u64 end;
6284
6285 end = start + len;
6286 return end >= start ? end: NFS4_MAX_UINT64;
6287}
6288
6289/* last octet in a range */
6290static inline u64
6291last_byte_offset(u64 start, u64 len)
6292{
6293 u64 end;
6294
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006295 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006296 end = start + len;
6297 return end > start ? end - 1: NFS4_MAX_UINT64;
6298}
6299
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300/*
6301 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6302 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6303 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6304 * locking, this prevents us from being completely protocol-compliant. The
6305 * real solution to this problem is to start using unsigned file offsets in
6306 * the VFS, but this is a very deep change!
6307 */
6308static inline void
6309nfs4_transform_lock_offset(struct file_lock *lock)
6310{
6311 if (lock->fl_start < 0)
6312 lock->fl_start = OFFSET_MAX;
6313 if (lock->fl_end < 0)
6314 lock->fl_end = OFFSET_MAX;
6315}
6316
Jeff Laytoncae80b32015-04-03 09:04:04 -04006317static fl_owner_t
6318nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006319{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006320 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6321
6322 nfs4_get_stateowner(&lo->lo_owner);
6323 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006324}
6325
Jeff Laytoncae80b32015-04-03 09:04:04 -04006326static void
6327nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006328{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006329 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006330
Jeff Laytoncae80b32015-04-03 09:04:04 -04006331 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006332 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006333}
6334
Jeff Layton76d348f2016-09-16 16:28:24 -04006335static void
6336nfsd4_lm_notify(struct file_lock *fl)
6337{
6338 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6339 struct net *net = lo->lo_owner.so_client->net;
6340 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6341 struct nfsd4_blocked_lock *nbl = container_of(fl,
6342 struct nfsd4_blocked_lock, nbl_lock);
6343 bool queue = false;
6344
Jeff Layton7919d0a2016-09-16 16:28:25 -04006345 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006346 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006347 if (!list_empty(&nbl->nbl_list)) {
6348 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006349 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006350 queue = true;
6351 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006352 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006353
6354 if (queue)
6355 nfsd4_run_cb(&nbl->nbl_cb);
6356}
6357
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006358static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006359 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006360 .lm_get_owner = nfsd4_fl_get_owner,
6361 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006362};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363
6364static inline void
6365nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6366{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006367 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
NeilBrownd5b90262006-04-10 22:55:22 -07006369 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006370 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04006371 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6372 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006373 if (!deny->ld_owner.data)
6374 /* We just don't care that much */
6375 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006376 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006377 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006378nevermind:
6379 deny->ld_owner.len = 0;
6380 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006381 deny->ld_clientid.cl_boot = 0;
6382 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 }
6384 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006385 deny->ld_length = NFS4_MAX_UINT64;
6386 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6388 deny->ld_type = NFS4_READ_LT;
6389 if (fl->fl_type != F_RDLCK)
6390 deny->ld_type = NFS4_WRITE_LT;
6391}
6392
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006393static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006394find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006396 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006397 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398
Trond Myklebust0a880a22014-07-30 08:27:10 -04006399 lockdep_assert_held(&clp->cl_lock);
6400
Trond Myklebustd4f04892014-07-29 21:34:36 -04006401 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6402 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006403 if (so->so_is_open_owner)
6404 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006405 if (same_owner_str(so, owner))
6406 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 }
6408 return NULL;
6409}
6410
Trond Myklebustc58c6612014-07-29 21:34:35 -04006411static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006412find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006413{
6414 struct nfs4_lockowner *lo;
6415
Trond Myklebustd4f04892014-07-29 21:34:36 -04006416 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006417 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006418 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006419 return lo;
6420}
6421
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006422static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6423{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006424 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006425}
6426
Jeff Layton6b180f02014-07-29 21:34:26 -04006427static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6428{
6429 struct nfs4_lockowner *lo = lockowner(sop);
6430
6431 kmem_cache_free(lockowner_slab, lo);
6432}
6433
6434static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006435 .so_unhash = nfs4_unhash_lockowner,
6436 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006437};
6438
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439/*
6440 * Alloc a lock owner structure.
6441 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006442 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006444 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006446static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006447alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6448 struct nfs4_ol_stateid *open_stp,
6449 struct nfsd4_lock *lock)
6450{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006451 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006453 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6454 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006456 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006457 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6458 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006459 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006460 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006461 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006462 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006463 if (ret == NULL) {
6464 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006465 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006466 ret = lo;
6467 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006468 nfs4_free_stateowner(&lo->lo_owner);
6469
Trond Myklebustd4f04892014-07-29 21:34:36 -04006470 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006471 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472}
6473
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006474static struct nfs4_ol_stateid *
Trond Myklebusta451b122020-03-01 18:21:38 -05006475find_lock_stateid(const struct nfs4_lockowner *lo,
6476 const struct nfs4_ol_stateid *ost)
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006477{
6478 struct nfs4_ol_stateid *lst;
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006479
Trond Myklebusta451b122020-03-01 18:21:38 -05006480 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006481
Trond Myklebusta451b122020-03-01 18:21:38 -05006482 /* If ost is not hashed, ost->st_locks will not be valid */
6483 if (!nfs4_ol_stateid_unhashed(ost))
6484 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6485 if (lst->st_stateowner == &lo->lo_owner) {
6486 refcount_inc(&lst->st_stid.sc_count);
6487 return lst;
6488 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006489 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006490 return NULL;
6491}
6492
Trond Myklebustbeeca192017-11-03 08:00:14 -04006493static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006494init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6495 struct nfs4_file *fp, struct inode *inode,
6496 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006498 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006499 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500
Trond Myklebustbeeca192017-11-03 08:00:14 -04006501 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006502 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006503retry:
6504 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006505 if (nfs4_ol_stateid_unhashed(open_stp))
6506 goto out_close;
6507 retstp = find_lock_stateid(lo, open_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006508 if (retstp)
Trond Myklebusta451b122020-03-01 18:21:38 -05006509 goto out_found;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006510 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006511 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006512 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006513 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006514 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006515 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006517 stp->st_openstp = open_stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006518 spin_lock(&fp->fi_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006519 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006520 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006521 list_add(&stp->st_perfile, &fp->fi_stateids);
6522 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006523 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006524 return stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006525out_found:
6526 spin_unlock(&clp->cl_lock);
6527 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6528 nfs4_put_stid(&retstp->st_stid);
6529 goto retry;
6530 }
6531 /* To keep mutex tracking happy */
6532 mutex_unlock(&stp->st_mutex);
6533 return retstp;
6534out_close:
6535 spin_unlock(&clp->cl_lock);
6536 mutex_unlock(&stp->st_mutex);
6537 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538}
6539
Jeff Laytonc53530d2014-06-30 11:48:39 -04006540static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006541find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6542 struct inode *inode, struct nfs4_ol_stateid *ost,
6543 bool *new)
6544{
6545 struct nfs4_stid *ns = NULL;
6546 struct nfs4_ol_stateid *lst;
6547 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6548 struct nfs4_client *clp = oo->oo_owner.so_client;
6549
Trond Myklebustbeeca192017-11-03 08:00:14 -04006550 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006551 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006552 lst = find_lock_stateid(lo, ost);
Jeff Layton356a95e2014-07-29 21:34:13 -04006553 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006554 if (lst != NULL) {
6555 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6556 goto out;
6557 nfs4_put_stid(&lst->st_stid);
6558 }
6559 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6560 if (ns == NULL)
6561 return NULL;
6562
6563 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6564 if (lst == openlockstateid(ns))
6565 *new = true;
6566 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006567 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006568out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006569 return lst;
6570}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006571
NeilBrownfd39ca92005-06-23 22:04:03 -07006572static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573check_lock_length(u64 offset, u64 length)
6574{
Kinglong Meee7969312015-07-13 17:34:19 +08006575 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6576 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577}
6578
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006579static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006580{
Trond Myklebust11b91642014-07-29 21:34:08 -04006581 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006582
Jeff Layton7214e862014-07-10 14:07:33 -04006583 lockdep_assert_held(&fp->fi_lock);
6584
Jeff Layton82c5ff12012-05-11 09:45:13 -04006585 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006586 return;
Jeff Layton12659652014-07-10 14:07:28 -04006587 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006588 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006589}
6590
Jeff Layton356a95e2014-07-29 21:34:13 -04006591static __be32
6592lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6593 struct nfs4_ol_stateid *ost,
6594 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006595 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006596{
Jeff Layton5db1c032014-07-29 21:34:28 -04006597 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006598 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006599 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6600 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006601 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006602 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006603 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006604 unsigned int strhashval;
6605
Kinglong Meec8623992015-07-13 17:35:37 +08006606 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006607 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006608 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006609 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6610 if (lo == NULL)
6611 return nfserr_jukebox;
6612 } else {
6613 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006614 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006615 if (!cstate->minorversion &&
6616 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006617 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006618 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006619
Jeff Laytondd257932016-08-11 10:37:39 -04006620 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6621 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006622 status = nfserr_jukebox;
6623 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006624 }
Jeff Laytondd257932016-08-11 10:37:39 -04006625
Jeff Layton5db1c032014-07-29 21:34:28 -04006626 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006627 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006628out:
6629 nfs4_put_stateowner(&lo->lo_owner);
6630 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006631}
6632
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633/*
6634 * LOCK operation
6635 */
Al Virob37ad282006-10-19 23:28:59 -07006636__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006637nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006638 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006640 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006641 struct nfs4_openowner *open_sop = NULL;
6642 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006643 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006644 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006645 struct nfs4_file *fp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006646 struct nfsd_file *nf = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006647 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006648 struct file_lock *file_lock = NULL;
6649 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006650 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006651 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006652 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006653 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006654 unsigned char fl_type;
6655 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006656 struct net *net = SVC_NET(rqstp);
6657 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658
6659 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6660 (long long) lock->lk_offset,
6661 (long long) lock->lk_length);
6662
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 if (check_lock_length(lock->lk_offset, lock->lk_length))
6664 return nfserr_inval;
6665
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006666 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006667 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006668 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6669 return status;
6670 }
6671
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006673 if (nfsd4_has_session(cstate))
6674 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006675 memcpy(&lock->lk_new_clientid,
J. Bruce Fieldsec596592021-01-21 17:57:45 -05006676 &cstate->clp->cl_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006677 sizeof(clientid_t));
6678
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006680 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681 lock->lk_new_open_seqid,
6682 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006683 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006684 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006686 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006687 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006688 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006689 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006690 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006691 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006692 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006693 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006694 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006695 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006696 lock->lk_old_lock_seqid,
6697 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006698 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006699 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006700 if (status)
6701 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006702 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006704 lkflg = setlkflg(lock->lk_type);
6705 status = nfs4_check_openmode(lock_stp, lkflg);
6706 if (status)
6707 goto out;
6708
NeilBrown0dd395d2005-07-07 17:59:15 -07006709 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006710 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006711 goto out;
6712 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006713 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006714 goto out;
6715
Trond Myklebust11b91642014-07-29 21:34:08 -04006716 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006719 if (nfsd4_has_session(cstate))
6720 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006721 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006722 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006723 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006724 nf = find_readable_file_locked(fp);
6725 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006726 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006727 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006728 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006729 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006731 if (nfsd4_has_session(cstate))
6732 fl_flags |= FL_SLEEP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006733 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006734 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006735 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006736 nf = find_writeable_file_locked(fp);
6737 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006738 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006739 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006740 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006741 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 default:
6743 status = nfserr_inval;
6744 goto out;
6745 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006746
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006747 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006748 status = nfserr_openmode;
6749 goto out;
6750 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006751
Jeff Layton76d348f2016-09-16 16:28:24 -04006752 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6753 if (!nbl) {
6754 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6755 status = nfserr_jukebox;
6756 goto out;
6757 }
6758
6759 file_lock = &nbl->nbl_lock;
6760 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006761 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006762 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006763 file_lock->fl_file = nf->nf_file;
Jeff Layton76d348f2016-09-16 16:28:24 -04006764 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006765 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6766 file_lock->fl_start = lock->lk_offset;
6767 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6768 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769
Jeff Layton21179d82012-08-21 08:03:32 -04006770 conflock = locks_alloc_lock();
6771 if (!conflock) {
6772 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6773 status = nfserr_jukebox;
6774 goto out;
6775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
Jeff Layton76d348f2016-09-16 16:28:24 -04006777 if (fl_flags & FL_SLEEP) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01006778 nbl->nbl_time = ktime_get_boottime_seconds();
Jeff Layton0cc11a62016-10-20 09:34:31 -04006779 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006780 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006781 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006782 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006783 }
6784
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006785 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006786 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006788 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006789 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006790 if (lock->lk_reclaim)
6791 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006792 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006793 case FILE_LOCK_DEFERRED:
6794 nbl = NULL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006795 fallthrough;
Jeff Layton76d348f2016-09-16 16:28:24 -04006796 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006797 status = nfserr_denied;
6798 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006799 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006800 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006801 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006803 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006804 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006805 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006806 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006810 if (nbl) {
6811 /* dequeue it if we queued it before */
6812 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006813 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006814 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006815 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006816 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006817 }
6818 free_blocked_lock(nbl);
6819 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006820 if (nf)
6821 nfsd_file_put(nf);
Jeff Layton5db1c032014-07-29 21:34:28 -04006822 if (lock_stp) {
6823 /* Bump seqid manually if the 4.0 replay owner is openowner */
6824 if (cstate->replay_owner &&
6825 cstate->replay_owner != &lock_sop->lo_owner &&
6826 seqid_mutating_err(ntohl(status)))
6827 lock_sop->lo_owner.so_seqid++;
6828
6829 /*
6830 * If this is a new, never-before-used stateid, and we are
6831 * returning an error, then just go ahead and release it.
6832 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006833 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006834 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006835
6836 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006837
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006838 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006839 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006840 if (open_stp)
6841 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006842 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006843 if (conflock)
6844 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 return status;
6846}
6847
6848/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006849 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6850 * so we do a temporary open here just to get an open file to pass to
6851 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6852 * inode operation.)
6853 */
Al Viro04da6e92012-04-13 00:00:04 -04006854static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006855{
Jeff Layton6b556ca2019-08-18 14:18:55 -04006856 struct nfsd_file *nf;
6857 __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
Al Viro04da6e92012-04-13 00:00:04 -04006858 if (!err) {
Jeff Layton6b556ca2019-08-18 14:18:55 -04006859 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6860 nfsd_file_put(nf);
Al Viro04da6e92012-04-13 00:00:04 -04006861 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006862 return err;
6863}
6864
6865/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 * LOCKT operation
6867 */
Al Virob37ad282006-10-19 23:28:59 -07006868__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006869nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006870 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006872 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006873 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006874 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006875 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006876 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006878 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 return nfserr_grace;
6880
6881 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6882 return nfserr_inval;
6883
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006884 if (!nfsd4_has_session(cstate)) {
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05006885 status = set_client(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006886 if (status)
6887 goto out;
6888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006890 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892
Jeff Layton21179d82012-08-21 08:03:32 -04006893 file_lock = locks_alloc_lock();
6894 if (!file_lock) {
6895 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6896 status = nfserr_jukebox;
6897 goto out;
6898 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006899
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 switch (lockt->lt_type) {
6901 case NFS4_READ_LT:
6902 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006903 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006904 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 case NFS4_WRITE_LT:
6906 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006907 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006908 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006910 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006912 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 }
6914
Kinglong Meec8623992015-07-13 17:35:37 +08006915 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006916 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006917 file_lock->fl_owner = (fl_owner_t)lo;
6918 file_lock->fl_pid = current->tgid;
6919 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920
Jeff Layton21179d82012-08-21 08:03:32 -04006921 file_lock->fl_start = lockt->lt_offset;
6922 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923
Jeff Layton21179d82012-08-21 08:03:32 -04006924 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925
Jeff Layton21179d82012-08-21 08:03:32 -04006926 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006927 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006928 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006929
Jeff Layton21179d82012-08-21 08:03:32 -04006930 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006932 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 }
6934out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006935 if (lo)
6936 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006937 if (file_lock)
6938 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 return status;
6940}
6941
Al Virob37ad282006-10-19 23:28:59 -07006942__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006943nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006944 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006946 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006947 struct nfs4_ol_stateid *stp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006948 struct nfsd_file *nf = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006949 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006950 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006951 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006952 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6953
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6955 (long long) locku->lu_offset,
6956 (long long) locku->lu_length);
6957
6958 if (check_lock_length(locku->lu_offset, locku->lu_length))
6959 return nfserr_inval;
6960
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006961 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006962 &locku->lu_stateid, NFS4_LOCK_STID,
6963 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006964 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 goto out;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006966 nf = find_any_file(stp->st_stid.sc_file);
6967 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006968 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006969 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006970 }
Jeff Layton21179d82012-08-21 08:03:32 -04006971 file_lock = locks_alloc_lock();
6972 if (!file_lock) {
6973 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6974 status = nfserr_jukebox;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006975 goto put_file;
Jeff Layton21179d82012-08-21 08:03:32 -04006976 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006977
Jeff Layton21179d82012-08-21 08:03:32 -04006978 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006979 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006980 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006981 file_lock->fl_file = nf->nf_file;
Jeff Layton21179d82012-08-21 08:03:32 -04006982 file_lock->fl_flags = FL_POSIX;
6983 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6984 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
Jeff Layton21179d82012-08-21 08:03:32 -04006986 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6987 locku->lu_length);
6988 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006990 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006991 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006992 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 goto out_nfserr;
6994 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006995 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006996put_file:
6997 nfsd_file_put(nf);
Trond Myklebust858cc572014-07-29 21:34:16 -04006998put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006999 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04007000 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04007002 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04007003 if (file_lock)
7004 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 return status;
7006
7007out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07007008 status = nfserrno(err);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007009 goto put_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010}
7011
7012/*
7013 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007014 * true: locks held by lockowner
7015 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007017static bool
7018check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007020 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007021 int status = false;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007022 struct nfsd_file *nf = find_any_file(fp);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007023 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007024 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007025
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007026 if (!nf) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04007027 /* Any valid lock stateid should have some sort of access */
7028 WARN_ON_ONCE(1);
7029 return status;
7030 }
7031
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007032 inode = locks_inode(nf->nf_file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007033 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007035 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05007036 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05007037 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7038 if (fl->fl_owner == (fl_owner_t)lowner) {
7039 status = true;
7040 break;
7041 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08007042 }
Jeff Layton6109c852015-01-16 15:05:57 -05007043 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04007045 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046 return status;
7047}
7048
Al Virob37ad282006-10-19 23:28:59 -07007049__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08007050nfsd4_release_lockowner(struct svc_rqst *rqstp,
7051 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02007052 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053{
Christoph Hellwigeb698532017-05-08 20:58:35 +02007054 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04007056 struct nfs4_stateowner *sop;
7057 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04007058 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04007060 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07007061 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03007062 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04007063 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04007064 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065
7066 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7067 clid->cl_boot, clid->cl_id);
7068
J. Bruce Fieldsf71475b2021-01-21 17:57:43 -05007069 status = set_client(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007070 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04007071 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007072
Trond Myklebustd4f04892014-07-29 21:34:36 -04007073 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04007074 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04007075 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04007076 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04007077 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04007078
7079 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05007080 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04007081
Jeff Layton882e9d22014-07-29 21:34:37 -04007082 /* see if there are still any locks associated with it */
7083 lo = lockowner(sop);
7084 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
7085 if (check_for_locks(stp->st_stid.sc_file, lo)) {
7086 status = nfserr_locks_held;
7087 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04007088 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04007089 }
Jeff Layton5adfd882014-07-29 21:34:31 -04007090 }
Jeff Layton882e9d22014-07-29 21:34:37 -04007091
Kinglong Meeb5971af2014-08-22 10:18:43 -04007092 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04007093 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04007094 }
Chuck Lever88584812016-07-13 16:40:14 -04007095 if (!lo) {
7096 spin_unlock(&clp->cl_lock);
7097 return status;
7098 }
7099
7100 unhash_lockowner_locked(lo);
7101 while (!list_empty(&lo->lo_owner.so_stateids)) {
7102 stp = list_first_entry(&lo->lo_owner.so_stateids,
7103 struct nfs4_ol_stateid,
7104 st_perstateowner);
7105 WARN_ON(!unhash_lock_stateid(stp));
7106 put_ol_stateid_locked(stp, &reaplist);
7107 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04007108 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04007109 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007110 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04007111 nfs4_put_stateowner(&lo->lo_owner);
7112
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 return status;
7114}
7115
7116static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07007117alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118{
NeilBrowna55370a2005-06-23 22:03:52 -07007119 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120}
7121
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007122bool
Scott Mayhew6b189102019-03-26 18:06:26 -04007123nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07007124{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007125 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07007126
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007127 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007128 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07007129}
7130
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131/*
7132 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04007133 *
7134 * The caller is responsible for freeing name.data if NULL is returned (it
7135 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05007137struct nfs4_client_reclaim *
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007138nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7139 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140{
7141 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007142 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007144 trace_nfsd_clid_reclaim(nn, name.len, name.data);
NeilBrowna55370a2005-06-23 22:03:52 -07007145 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05007146 if (crp) {
7147 strhashval = clientstr_hashval(name);
7148 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007149 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04007150 crp->cr_name.data = name.data;
7151 crp->cr_name.len = name.len;
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007152 crp->cr_princhash.data = princhash.data;
7153 crp->cr_princhash.len = princhash.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007154 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007155 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007156 }
7157 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158}
7159
Jeff Layton2a4317c2012-03-21 16:42:43 -04007160void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007161nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05007162{
7163 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04007164 kfree(crp->cr_name.data);
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007165 kfree(crp->cr_princhash.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05007166 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007167 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05007168}
7169
7170void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007171nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172{
7173 struct nfs4_client_reclaim *crp = NULL;
7174 int i;
7175
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007177 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7178 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007180 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 }
7182 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05007183 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184}
7185
7186/*
7187 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04007188struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04007189nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190{
7191 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 struct nfs4_client_reclaim *crp = NULL;
7193
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007194 trace_nfsd_clid_find(nn, name.len, name.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195
Scott Mayhew6b189102019-03-26 18:06:26 -04007196 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007197 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04007198 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 return crp;
7200 }
7201 }
7202 return NULL;
7203}
7204
Al Virob37ad282006-10-19 23:28:59 -07007205__be32
J. Bruce Fields1722b042021-01-21 17:57:44 -05007206nfs4_check_open_reclaim(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207{
J. Bruce Fields1722b042021-01-21 17:57:44 -05007208 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
Jeff Layton3b3e7b72014-09-12 16:40:22 -04007209 return nfserr_no_grace;
7210
J. Bruce Fields1722b042021-01-21 17:57:44 -05007211 if (nfsd4_client_record_check(clp))
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007212 return nfserr_reclaim_bad;
7213
7214 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215}
7216
Meelap Shahc2f1a552007-07-17 04:04:39 -07007217/*
7218 * Since the lifetime of a delegation isn't limited to that of an open, a
7219 * client may quite reasonably hang on to a delegation as long as it has
7220 * the inode cached. This becomes an obvious problem the first time a
7221 * client's inode cache approaches the size of the server's total memory.
7222 *
7223 * For now we avoid this problem by imposing a hard limit on the number
7224 * of delegations, which varies according to the server's memory size.
7225 */
7226static void
7227set_max_delegations(void)
7228{
7229 /*
7230 * Allow at most 4 delegations per megabyte of RAM. Quick
7231 * estimates suggest that in the worst case (where every delegation
7232 * is for a different inode), a delegation could take about 1.5K,
7233 * giving a worst case usage of about 6% of memory.
7234 */
7235 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7236}
7237
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007238static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007239{
7240 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7241 int i;
7242
Kees Cook6da2ec52018-06-12 13:55:00 -07007243 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7244 sizeof(struct list_head),
7245 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007246 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007247 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007248 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7249 sizeof(struct list_head),
7250 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007251 if (!nn->unconf_id_hashtbl)
7252 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007253 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7254 sizeof(struct list_head),
7255 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007256 if (!nn->sessionid_hashtbl)
7257 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007258
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007259 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007260 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007261 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007262 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007263 for (i = 0; i < SESSION_HASH_SIZE; i++)
7264 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007265 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007266 nn->unconf_name_tree = RB_ROOT;
Arnd Bergmann9cc76802019-10-31 15:53:13 +01007267 nn->boot_time = ktime_get_real_seconds();
Vasily Averin81833de2017-11-13 07:25:40 +03007268 nn->grace_ended = false;
7269 nn->nfsd4_manager.block_opens = true;
7270 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007271 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007272 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007273 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007274 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007275 spin_lock_init(&nn->s2s_cp_lock);
7276 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007277
Jeff Layton0cc11a62016-10-20 09:34:31 -04007278 spin_lock_init(&nn->blocked_locks_lock);
7279 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7280
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007281 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007282 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007283
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007284 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007285
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007286err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007287 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007288err_unconf_id:
7289 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007290err:
7291 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007292}
7293
7294static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007295nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007296{
7297 int i;
7298 struct nfs4_client *clp = NULL;
7299 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7300
7301 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7302 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7303 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7304 destroy_client(clp);
7305 }
7306 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007307
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007308 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7309
Kinglong Mee2b905632014-03-26 22:09:30 +08007310 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7311 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7312 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7313 destroy_client(clp);
7314 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007315 }
7316
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007317 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007318 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007319 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007320 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007321}
7322
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007323int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007324nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007325{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007326 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007327 int ret;
7328
Trond Myklebustc6c7f2a2021-03-13 16:08:47 -05007329 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007330 if (ret)
7331 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007332 locks_start_grace(net, &nn->nfsd4_manager);
7333 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007334 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7335 goto skip_grace;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01007336 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
Vasily Averin7e981a82017-11-06 16:46:04 +03007337 nn->nfsd4_grace, net->ns.inum);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007338 trace_nfsd_grace_start(nn);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007339 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007340 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007341
7342skip_grace:
7343 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7344 net->ns.inum);
7345 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7346 nfsd4_end_grace(nn);
7347 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007348}
7349
7350/* initialization to perform when the nfsd service is started: */
7351
7352int
7353nfs4_state_start(void)
7354{
7355 int ret;
7356
Jeff Layton51a54562015-08-20 07:17:01 -04007357 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007358 if (laundry_wq == NULL) {
7359 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007360 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007361 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007362 ret = nfsd4_create_callback_queue();
7363 if (ret)
7364 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007365
Meelap Shahc2f1a552007-07-17 04:04:39 -07007366 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007367 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007368
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007369out_free_laundry:
7370 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007371out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007372 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373}
7374
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007375void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007376nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007380 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007382 cancel_delayed_work_sync(&nn->laundromat_work);
7383 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007384
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007386 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007387 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007389 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007390 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 }
Benny Halevycdc975052014-05-30 09:09:30 -04007392 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 list_for_each_safe(pos, next, &reaplist) {
7394 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007395 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007396 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 }
7398
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007399 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007400 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401}
7402
7403void
7404nfs4_state_shutdown(void)
7405{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007406 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007407 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007409
7410static void
7411get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7412{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007413 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7414 CURRENT_STATEID(stateid))
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007415 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007416}
7417
7418static void
7419put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7420{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007421 if (cstate->minorversion) {
7422 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007423 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007424 }
7425}
7426
7427void
7428clear_current_stateid(struct nfsd4_compound_state *cstate)
7429{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007430 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007431}
7432
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007433/*
7434 * functions to set current state id
7435 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007436void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007437nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7438 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007439{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007440 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007441}
7442
7443void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007444nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7445 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007446{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007447 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007448}
7449
7450void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007451nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7452 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007453{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007454 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007455}
7456
7457void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007458nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7459 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007460{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007461 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007462}
7463
7464/*
7465 * functions to consume current state id
7466 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007467
7468void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007469nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7470 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007471{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007472 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007473}
7474
7475void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007476nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7477 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007478{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007479 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007480}
7481
7482void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007483nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7484 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007485{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007486 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007487}
7488
7489void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007490nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7491 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007492{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007493 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007494}
7495
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007496void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007497nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7498 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007499{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007500 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007501}
7502
7503void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007504nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7505 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007506{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007507 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007508}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007509
7510void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007511nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7512 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007513{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007514 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007515}
7516
7517void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007518nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7519 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007520{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007521 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007522}