blob: 04d80f9e2f919a790b8d0b5b9216ae640f31f884 [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
Trond Myklebust02a35082014-07-25 07:34:22 -0400510static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800511unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700512
513/*
514 * Open owner state (share locks)
515 */
516
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500517/* hash tables for lock and open owners */
518#define OWNER_HASH_BITS 8
519#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
520#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700521
Trond Myklebustd4f04892014-07-29 21:34:36 -0400522static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400523{
524 unsigned int ret;
525
526 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500527 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400528}
NeilBrownef0f3392006-04-10 22:55:41 -0700529
NeilBrownef0f3392006-04-10 22:55:41 -0700530/* hash table for nfs4_file */
531#define FILE_HASH_BITS 8
532#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800533
Trond Myklebustca943212014-07-23 16:17:39 -0400534static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400535{
Trond Myklebustca943212014-07-23 16:17:39 -0400536 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
537}
538
539static unsigned int file_hashval(struct knfsd_fh *fh)
540{
541 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
542}
543
Jeff Layton89876f82013-04-02 09:01:59 -0400544static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700545
Jeff Layton12659652014-07-10 14:07:28 -0400546static void
547__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400548{
Jeff Layton7214e862014-07-10 14:07:33 -0400549 lockdep_assert_held(&fp->fi_lock);
550
Jeff Layton12659652014-07-10 14:07:28 -0400551 if (access & NFS4_SHARE_ACCESS_WRITE)
552 atomic_inc(&fp->fi_access[O_WRONLY]);
553 if (access & NFS4_SHARE_ACCESS_READ)
554 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400555}
556
Jeff Layton12659652014-07-10 14:07:28 -0400557static __be32
558nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400559{
Jeff Layton7214e862014-07-10 14:07:33 -0400560 lockdep_assert_held(&fp->fi_lock);
561
Jeff Layton12659652014-07-10 14:07:28 -0400562 /* Does this access mode make sense? */
563 if (access & ~NFS4_SHARE_ACCESS_BOTH)
564 return nfserr_inval;
565
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400566 /* Does it conflict with a deny mode already set? */
567 if ((access & fp->fi_share_deny) != 0)
568 return nfserr_share_denied;
569
Jeff Layton12659652014-07-10 14:07:28 -0400570 __nfs4_file_get_access(fp, access);
571 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400572}
573
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400574static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
575{
576 /* Common case is that there is no deny mode. */
577 if (deny) {
578 /* Does this deny mode make sense? */
579 if (deny & ~NFS4_SHARE_DENY_BOTH)
580 return nfserr_inval;
581
582 if ((deny & NFS4_SHARE_DENY_READ) &&
583 atomic_read(&fp->fi_access[O_RDONLY]))
584 return nfserr_share_denied;
585
586 if ((deny & NFS4_SHARE_DENY_WRITE) &&
587 atomic_read(&fp->fi_access[O_WRONLY]))
588 return nfserr_share_denied;
589 }
590 return nfs_ok;
591}
592
J. Bruce Fields998db522010-08-07 09:21:41 -0400593static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400594{
Trond Myklebustde186432014-07-10 14:07:26 -0400595 might_lock(&fp->fi_lock);
596
597 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400598 struct nfsd_file *f1 = NULL;
599 struct nfsd_file *f2 = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400600
Jeff Layton6d338b52014-07-10 14:07:29 -0400601 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400602 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400603 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400604 spin_unlock(&fp->fi_lock);
605 if (f1)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400606 nfsd_file_put(f1);
Trond Myklebustde186432014-07-10 14:07:26 -0400607 if (f2)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400608 nfsd_file_put(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400609 }
610}
611
Jeff Layton12659652014-07-10 14:07:28 -0400612static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400613{
Jeff Layton12659652014-07-10 14:07:28 -0400614 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
615
616 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400617 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400618 if (access & NFS4_SHARE_ACCESS_READ)
619 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400620}
621
Sachin Bhamare8287f002015-04-27 14:50:14 +0200622/*
623 * Allocate a new open/delegation state counter. This is needed for
624 * pNFS for proper return on close semantics.
625 *
626 * Note that we only allocate it for pNFS-enabled exports, otherwise
627 * all pointers to struct nfs4_clnt_odstate are always NULL.
628 */
629static struct nfs4_clnt_odstate *
630alloc_clnt_odstate(struct nfs4_client *clp)
631{
632 struct nfs4_clnt_odstate *co;
633
634 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
635 if (co) {
636 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300637 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200638 }
639 return co;
640}
641
642static void
643hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
644{
645 struct nfs4_file *fp = co->co_file;
646
647 lockdep_assert_held(&fp->fi_lock);
648 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
649}
650
651static inline void
652get_clnt_odstate(struct nfs4_clnt_odstate *co)
653{
654 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300655 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200656}
657
658static void
659put_clnt_odstate(struct nfs4_clnt_odstate *co)
660{
661 struct nfs4_file *fp;
662
663 if (!co)
664 return;
665
666 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300667 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200668 list_del(&co->co_perfile);
669 spin_unlock(&fp->fi_lock);
670
671 nfsd4_return_all_file_layouts(co->co_client, fp);
672 kmem_cache_free(odstate_slab, co);
673 }
674}
675
676static struct nfs4_clnt_odstate *
677find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
678{
679 struct nfs4_clnt_odstate *co;
680 struct nfs4_client *cl;
681
682 if (!new)
683 return NULL;
684
685 cl = new->co_client;
686
687 spin_lock(&fp->fi_lock);
688 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
689 if (co->co_client == cl) {
690 get_clnt_odstate(co);
691 goto out;
692 }
693 }
694 co = new;
695 co->co_file = fp;
696 hash_clnt_odstate_locked(new);
697out:
698 spin_unlock(&fp->fi_lock);
699 return co;
700}
701
Kinglong Meed19fb702017-01-18 19:04:42 +0800702struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
703 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400704{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500705 struct nfs4_stid *stid;
706 int new_id;
707
Trond Myklebustf8338832014-07-25 07:34:19 -0400708 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500709 if (!stid)
710 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400711
Jeff Layton4770d722014-07-29 21:34:10 -0400712 idr_preload(GFP_KERNEL);
713 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400714 /* Reserving 0 for start of file in nfsdfs "states" file: */
715 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400716 spin_unlock(&cl->cl_lock);
717 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700718 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500719 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800720
721 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500722 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500723 stid->sc_stateid.si_opaque.so_id = new_id;
724 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
725 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300726 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400727 spin_lock_init(&stid->sc_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400728 INIT_LIST_HEAD(&stid->sc_cp_list);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500729
J. Bruce Fields996e0932011-10-17 11:14:48 -0400730 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500731 * It shouldn't be a problem to reuse an opaque stateid value.
732 * I don't think it is for 4.1. But with 4.0 I worry that, for
733 * example, a stray write retransmission could be accepted by
734 * the server when it should have been rejected. Therefore,
735 * adopt a trick from the sctp code to attempt to maximize the
736 * amount of time until an id is reused, by ensuring they always
737 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400738 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500739 return stid;
740out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800741 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500742 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400743}
744
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400745/*
746 * Create a unique stateid_t to represent each COPY.
747 */
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400748static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
749 unsigned char sc_type)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400750{
751 int new_id;
752
Arnd Bergmann9cc76802019-10-31 15:53:13 +0100753 stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400754 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
755 stid->sc_type = sc_type;
756
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400757 idr_preload(GFP_KERNEL);
758 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400759 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
760 stid->stid.si_opaque.so_id = new_id;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400761 spin_unlock(&nn->s2s_cp_lock);
762 idr_preload_end();
763 if (new_id < 0)
764 return 0;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400765 return 1;
766}
767
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400768int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
769{
770 return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
771}
772
773struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
774 struct nfs4_stid *p_stid)
775{
776 struct nfs4_cpntf_state *cps;
777
778 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
779 if (!cps)
780 return NULL;
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100781 cps->cpntf_time = ktime_get_boottime_seconds();
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400782 refcount_set(&cps->cp_stateid.sc_count, 1);
783 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
784 goto out_free;
785 spin_lock(&nn->s2s_cp_lock);
786 list_add(&cps->cp_list, &p_stid->sc_cp_list);
787 spin_unlock(&nn->s2s_cp_lock);
788 return cps;
789out_free:
790 kfree(cps);
791 return NULL;
792}
793
794void nfs4_free_copy_state(struct nfsd4_copy *copy)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400795{
796 struct nfsd_net *nn;
797
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400798 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400799 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
800 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400801 idr_remove(&nn->s2s_cp_stateids,
802 copy->cp_stateid.stid.si_opaque.so_id);
803 spin_unlock(&nn->s2s_cp_lock);
804}
805
806static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
807{
808 struct nfs4_cpntf_state *cps;
809 struct nfsd_net *nn;
810
811 nn = net_generic(net, nfsd_net_id);
812 spin_lock(&nn->s2s_cp_lock);
813 while (!list_empty(&stid->sc_cp_list)) {
814 cps = list_first_entry(&stid->sc_cp_list,
815 struct nfs4_cpntf_state, cp_list);
816 _free_cpntf_state_locked(nn, cps);
817 }
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400818 spin_unlock(&nn->s2s_cp_lock);
819}
820
Jeff Laytonb49e0842014-07-29 21:34:11 -0400821static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400822{
Trond Myklebust60116952014-07-29 21:34:06 -0400823 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400824
Kinglong Meed19fb702017-01-18 19:04:42 +0800825 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400826 if (!stid)
827 return NULL;
828
Kinglong Meed19fb702017-01-18 19:04:42 +0800829 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400830}
831
832static void nfs4_free_deleg(struct nfs4_stid *stid)
833{
Trond Myklebust60116952014-07-29 21:34:06 -0400834 kmem_cache_free(deleg_slab, stid);
835 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400836}
837
NeilBrown6282cd52014-06-04 17:39:26 +1000838/*
839 * When we recall a delegation, we should be careful not to hand it
840 * out again straight away.
841 * To ensure this we keep a pair of bloom filters ('new' and 'old')
842 * in which the filehandles of recalled delegations are "stored".
843 * If a filehandle appear in either filter, a delegation is blocked.
844 * When a delegation is recalled, the filehandle is stored in the "new"
845 * filter.
846 * Every 30 seconds we swap the filters and clear the "new" one,
847 * unless both are empty of course.
848 *
849 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
850 * low 3 bytes as hash-table indices.
851 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400852 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000853 * is used to manage concurrent access. Testing does not need the lock
854 * except when swapping the two filters.
855 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400856static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000857static struct bloom_pair {
858 int entries, old_entries;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200859 time64_t swap_time;
NeilBrown6282cd52014-06-04 17:39:26 +1000860 int new; /* index into 'set' */
861 DECLARE_BITMAP(set[2], 256);
862} blocked_delegations;
863
864static int delegation_blocked(struct knfsd_fh *fh)
865{
866 u32 hash;
867 struct bloom_pair *bd = &blocked_delegations;
868
869 if (bd->entries == 0)
870 return 0;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200871 if (ktime_get_seconds() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400872 spin_lock(&blocked_delegations_lock);
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200873 if (ktime_get_seconds() - bd->swap_time > 30) {
NeilBrown6282cd52014-06-04 17:39:26 +1000874 bd->entries -= bd->old_entries;
875 bd->old_entries = bd->entries;
876 memset(bd->set[bd->new], 0,
877 sizeof(bd->set[0]));
878 bd->new = 1-bd->new;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200879 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +1000880 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400881 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000882 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100883 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000884 if (test_bit(hash&255, bd->set[0]) &&
885 test_bit((hash>>8)&255, bd->set[0]) &&
886 test_bit((hash>>16)&255, bd->set[0]))
887 return 1;
888
889 if (test_bit(hash&255, bd->set[1]) &&
890 test_bit((hash>>8)&255, bd->set[1]) &&
891 test_bit((hash>>16)&255, bd->set[1]))
892 return 1;
893
894 return 0;
895}
896
897static void block_delegations(struct knfsd_fh *fh)
898{
899 u32 hash;
900 struct bloom_pair *bd = &blocked_delegations;
901
Daniel Borkmann87545892014-12-10 16:33:11 +0100902 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000903
Jeff Laytonf54fe962014-07-25 07:34:26 -0400904 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000905 __set_bit(hash&255, bd->set[bd->new]);
906 __set_bit((hash>>8)&255, bd->set[bd->new]);
907 __set_bit((hash>>16)&255, bd->set[bd->new]);
908 if (bd->entries == 0)
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200909 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +1000910 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400911 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000912}
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500915alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
916 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200917 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400920 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400923 n = atomic_long_inc_return(&num_delegations);
924 if (n < 0 || n > max_delegations)
925 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000926 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400927 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800928 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700929 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400930 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400931
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400932 /*
933 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400934 * meaning of seqid 0 isn't meaningful, really, but let's avoid
935 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400936 */
937 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700938 INIT_LIST_HEAD(&dp->dl_perfile);
939 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200941 dp->dl_clnt_odstate = odstate;
942 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400943 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200944 dp->dl_retries = 1;
945 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200946 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500947 get_nfs4_file(fp);
948 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400950out_dec:
951 atomic_long_dec(&num_delegations);
952 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955void
Trond Myklebust60116952014-07-29 21:34:06 -0400956nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Trond Myklebust11b91642014-07-29 21:34:08 -0400958 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400959 struct nfs4_client *clp = s->sc_client;
960
Jeff Layton4770d722014-07-29 21:34:10 -0400961 might_lock(&clp->cl_lock);
962
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300963 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400964 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400965 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400966 }
Trond Myklebust60116952014-07-29 21:34:06 -0400967 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400968 nfs4_free_cpntf_statelist(clp->net, s);
Jeff Layton4770d722014-07-29 21:34:10 -0400969 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400970 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400971 if (fp)
972 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Jeff Layton9767feb2015-10-01 09:05:50 -0400975void
976nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
977{
978 stateid_t *src = &stid->sc_stateid;
979
980 spin_lock(&stid->sc_lock);
981 if (unlikely(++src->si_generation == 0))
982 src->si_generation = 1;
983 memcpy(dst, src, sizeof(*dst));
984 spin_unlock(&stid->sc_lock);
985}
986
J. Bruce Fields353601e2018-02-16 14:29:42 -0500987static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400989 struct nfsd_file *nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500990
991 spin_lock(&fp->fi_lock);
992 if (--fp->fi_delegees == 0)
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400993 swap(nf, fp->fi_deleg_file);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500994 spin_unlock(&fp->fi_lock);
995
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400996 if (nf)
997 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500998}
999
1000static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1001{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -05001002 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001003 struct nfsd_file *nf = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -04001004
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -05001005 WARN_ON_ONCE(!fp->fi_delegees);
1006
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001007 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001008 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001011static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1012{
1013 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001014 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001015 nfs4_put_stid(&dp->dl_stid);
1016}
1017
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001018void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001019{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001020 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001021}
1022
Andrew Elble34ed9872015-10-15 12:07:28 -04001023/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001024 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -04001025 * @clp: a pointer to the nfs4_client we're granting a delegation to
1026 * @fp: a pointer to the nfs4_file we're granting a delegation on
1027 *
1028 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001029 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -04001030 */
1031
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001032static bool
1033nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -04001034{
Andrew Elble34ed9872015-10-15 12:07:28 -04001035 struct nfs4_delegation *searchdp = NULL;
1036 struct nfs4_client *searchclp = NULL;
1037
Benny Halevycdc975052014-05-30 09:09:30 -04001038 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04001039 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001040
Andrew Elble34ed9872015-10-15 12:07:28 -04001041 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1042 searchclp = searchdp->dl_stid.sc_client;
1043 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001044 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -04001045 }
1046 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001047 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -04001048}
1049
1050/**
1051 * hash_delegation_locked - Add a delegation to the appropriate lists
1052 * @dp: a pointer to the nfs4_delegation we are adding.
1053 * @fp: a pointer to the nfs4_file we're granting a delegation on
1054 *
1055 * Return:
1056 * On success: NULL if the delegation was successfully hashed.
1057 *
1058 * On error: -EAGAIN if one was previously granted to this
1059 * nfs4_client for this nfs4_file. Delegation is not hashed.
1060 *
1061 */
1062
1063static int
1064hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1065{
Andrew Elble34ed9872015-10-15 12:07:28 -04001066 struct nfs4_client *clp = dp->dl_stid.sc_client;
1067
1068 lockdep_assert_held(&state_lock);
1069 lockdep_assert_held(&fp->fi_lock);
1070
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001071 if (nfs4_delegation_exists(clp, fp))
1072 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001073 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001074 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001075 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001076 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1077 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001078}
1079
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001080static bool
Jeff Layton42690672014-07-25 07:34:20 -04001081unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Trond Myklebust11b91642014-07-29 21:34:08 -04001083 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001084
Jeff Layton42690672014-07-25 07:34:20 -04001085 lockdep_assert_held(&state_lock);
1086
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001087 if (list_empty(&dp->dl_perfile))
1088 return false;
1089
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001090 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001091 /* Ensure that deleg break won't try to requeue it */
1092 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001093 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001094 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001096 list_del_init(&dp->dl_perfile);
1097 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001098 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001099}
1100
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001101static void destroy_delegation(struct nfs4_delegation *dp)
1102{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001103 bool unhashed;
1104
Jeff Layton42690672014-07-25 07:34:20 -04001105 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001106 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001107 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001108 if (unhashed)
1109 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001110}
1111
1112static void revoke_delegation(struct nfs4_delegation *dp)
1113{
1114 struct nfs4_client *clp = dp->dl_stid.sc_client;
1115
Jeff Layton2d4a5322014-07-25 07:34:21 -04001116 WARN_ON(!list_empty(&dp->dl_recall_lru));
1117
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001118 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001119 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001120 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001121 spin_lock(&clp->cl_lock);
1122 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1123 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001124 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001125 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001126}
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128/*
1129 * SETCLIENTID state
1130 */
1131
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001132static unsigned int clientid_hashval(u32 id)
1133{
1134 return id & CLIENT_HASH_MASK;
1135}
1136
Scott Mayhew6b189102019-03-26 18:06:26 -04001137static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001138{
Scott Mayhew6b189102019-03-26 18:06:26 -04001139 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001140}
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001143 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1144 * st_{access,deny}_bmap field of the stateid, in order to track not
1145 * only what share bits are currently in force, but also what
1146 * combinations of share bits previous opens have used. This allows us
1147 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1148 * return an error if the client attempt to downgrade to a combination
1149 * of share bits not explicable by closing some of its previous opens.
1150 *
1151 * XXX: This enforcement is actually incomplete, since we don't keep
1152 * track of access/deny bit combinations; so, e.g., we allow:
1153 *
1154 * OPEN allow read, deny write
1155 * OPEN allow both, deny none
1156 * DOWNGRADE allow read, deny none
1157 *
1158 * which we should reject.
1159 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001160static unsigned int
1161bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001162 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001163 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001164
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001165 for (i = 1; i < 4; i++) {
1166 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001167 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001168 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001169 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001170}
1171
Jeff Layton82c5ff12012-05-11 09:45:13 -04001172/* set share access for a given stateid */
1173static inline void
1174set_access(u32 access, struct nfs4_ol_stateid *stp)
1175{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001176 unsigned char mask = 1 << access;
1177
1178 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1179 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001180}
1181
1182/* clear share access for a given stateid */
1183static inline void
1184clear_access(u32 access, struct nfs4_ol_stateid *stp)
1185{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001186 unsigned char mask = 1 << access;
1187
1188 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1189 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001190}
1191
1192/* test whether a given stateid has access */
1193static inline bool
1194test_access(u32 access, struct nfs4_ol_stateid *stp)
1195{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001196 unsigned char mask = 1 << access;
1197
1198 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001199}
1200
Jeff Laytonce0fc432012-05-11 09:45:14 -04001201/* set share deny for a given stateid */
1202static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001203set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001204{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001205 unsigned char mask = 1 << deny;
1206
1207 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1208 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001209}
1210
1211/* clear share deny for a given stateid */
1212static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001213clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001214{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001215 unsigned char mask = 1 << deny;
1216
1217 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1218 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001219}
1220
1221/* test whether a given stateid is denying specific access */
1222static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001223test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001224{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001225 unsigned char mask = 1 << deny;
1226
1227 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001228}
1229
1230static int nfs4_access_to_omode(u32 access)
1231{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001232 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001233 case NFS4_SHARE_ACCESS_READ:
1234 return O_RDONLY;
1235 case NFS4_SHARE_ACCESS_WRITE:
1236 return O_WRONLY;
1237 case NFS4_SHARE_ACCESS_BOTH:
1238 return O_RDWR;
1239 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001240 WARN_ON_ONCE(1);
1241 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001242}
1243
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001244/*
1245 * A stateid that had a deny mode associated with it is being released
1246 * or downgraded. Recalculate the deny mode on the file.
1247 */
1248static void
1249recalculate_deny_mode(struct nfs4_file *fp)
1250{
1251 struct nfs4_ol_stateid *stp;
1252
1253 spin_lock(&fp->fi_lock);
1254 fp->fi_share_deny = 0;
1255 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1256 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1257 spin_unlock(&fp->fi_lock);
1258}
1259
1260static void
1261reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1262{
1263 int i;
1264 bool change = false;
1265
1266 for (i = 1; i < 4; i++) {
1267 if ((i & deny) != i) {
1268 change = true;
1269 clear_deny(i, stp);
1270 }
1271 }
1272
1273 /* Recalculate per-file deny mode if there was a change */
1274 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001275 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001276}
1277
Jeff Layton82c5ff12012-05-11 09:45:13 -04001278/* release all access and file references for a given stateid */
1279static void
1280release_all_access(struct nfs4_ol_stateid *stp)
1281{
1282 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001283 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001284
1285 if (fp && stp->st_deny_bmap != 0)
1286 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001287
1288 for (i = 1; i < 4; i++) {
1289 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001290 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001291 clear_access(i, stp);
1292 }
1293}
1294
Kinglong Meed50ffde2015-07-16 12:05:07 +08001295static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1296{
1297 kfree(sop->so_owner.data);
1298 sop->so_ops->so_free(sop);
1299}
1300
Jeff Layton6b180f02014-07-29 21:34:26 -04001301static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1302{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001303 struct nfs4_client *clp = sop->so_client;
1304
1305 might_lock(&clp->cl_lock);
1306
1307 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001308 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001309 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001310 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001311 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001312}
1313
Trond Myklebusta451b122020-03-01 18:21:38 -05001314static bool
1315nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1316{
1317 return list_empty(&stp->st_perfile);
1318}
1319
Jeff Laytone8568732015-08-24 12:41:47 -04001320static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001321{
Trond Myklebust11b91642014-07-29 21:34:08 -04001322 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001323
Jeff Layton1c755dc2014-07-29 21:34:12 -04001324 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1325
Jeff Laytone8568732015-08-24 12:41:47 -04001326 if (list_empty(&stp->st_perfile))
1327 return false;
1328
Trond Myklebust1d31a252014-07-10 14:07:25 -04001329 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001330 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001331 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001332 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001333 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001334}
1335
Trond Myklebust60116952014-07-29 21:34:06 -04001336static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001337{
Trond Myklebust60116952014-07-29 21:34:06 -04001338 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001339
Sachin Bhamare8287f002015-04-27 14:50:14 +02001340 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001341 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001342 if (stp->st_stateowner)
1343 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001344 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001345}
1346
Jeff Laytonb49e0842014-07-29 21:34:11 -04001347static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001348{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001349 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1350 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001351 struct nfsd_file *nf;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001352
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001353 nf = find_any_file(stp->st_stid.sc_file);
1354 if (nf) {
1355 get_file(nf->nf_file);
1356 filp_close(nf->nf_file, (fl_owner_t)lo);
1357 nfsd_file_put(nf);
1358 }
Jeff Laytonb49e0842014-07-29 21:34:11 -04001359 nfs4_free_ol_stateid(stid);
1360}
1361
Jeff Layton2c41beb2014-07-29 21:34:41 -04001362/*
1363 * Put the persistent reference to an already unhashed generic stateid, while
1364 * holding the cl_lock. If it's the last reference, then put it onto the
1365 * reaplist for later destruction.
1366 */
1367static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1368 struct list_head *reaplist)
1369{
1370 struct nfs4_stid *s = &stp->st_stid;
1371 struct nfs4_client *clp = s->sc_client;
1372
1373 lockdep_assert_held(&clp->cl_lock);
1374
1375 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1376
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001377 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001378 wake_up_all(&close_wq);
1379 return;
1380 }
1381
1382 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1383 list_add(&stp->st_locks, reaplist);
1384}
1385
Jeff Laytone8568732015-08-24 12:41:47 -04001386static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001387{
Chuck Leverf46c4452016-10-29 18:19:03 -04001388 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001389
Trond Myklebusta451b122020-03-01 18:21:38 -05001390 if (!unhash_ol_stateid(stp))
1391 return false;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001392 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001393 nfs4_unhash_stid(&stp->st_stid);
Trond Myklebusta451b122020-03-01 18:21:38 -05001394 return true;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001395}
1396
Jeff Layton5adfd882014-07-29 21:34:31 -04001397static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001398{
Chuck Leverf46c4452016-10-29 18:19:03 -04001399 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001400 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001401
Chuck Leverf46c4452016-10-29 18:19:03 -04001402 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001403 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001404 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001405 if (unhashed)
1406 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001407}
1408
Trond Myklebustc58c6612014-07-29 21:34:35 -04001409static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001410{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001411 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001412
Trond Myklebustd4f04892014-07-29 21:34:36 -04001413 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001414
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001415 list_del_init(&lo->lo_owner.so_strhash);
1416}
1417
Jeff Layton2c41beb2014-07-29 21:34:41 -04001418/*
1419 * Free a list of generic stateids that were collected earlier after being
1420 * fully unhashed.
1421 */
1422static void
1423free_ol_stateid_reaplist(struct list_head *reaplist)
1424{
1425 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001426 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001427
1428 might_sleep();
1429
1430 while (!list_empty(reaplist)) {
1431 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1432 st_locks);
1433 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001434 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001435 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001436 if (fp)
1437 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001438 }
1439}
1440
Jeff Laytond83017f2014-07-29 21:34:42 -04001441static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1442 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001443{
1444 struct nfs4_ol_stateid *stp;
1445
Jeff Laytone8568732015-08-24 12:41:47 -04001446 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1447
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001448 while (!list_empty(&open_stp->st_locks)) {
1449 stp = list_entry(open_stp->st_locks.next,
1450 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001451 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001452 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001453 }
1454}
1455
Jeff Laytone8568732015-08-24 12:41:47 -04001456static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001457 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001458{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001459 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1460
Trond Myklebusta451b122020-03-01 18:21:38 -05001461 if (!unhash_ol_stateid(stp))
1462 return false;
Jeff Laytond83017f2014-07-29 21:34:42 -04001463 release_open_stateid_locks(stp, reaplist);
Trond Myklebusta451b122020-03-01 18:21:38 -05001464 return true;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001465}
1466
1467static void release_open_stateid(struct nfs4_ol_stateid *stp)
1468{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001469 LIST_HEAD(reaplist);
1470
1471 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001472 if (unhash_open_stateid(stp, &reaplist))
1473 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001474 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1475 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001476}
1477
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001478static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001479{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001480 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001481
Trond Myklebustd4f04892014-07-29 21:34:36 -04001482 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001483
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001484 list_del_init(&oo->oo_owner.so_strhash);
1485 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001486}
1487
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001488static void release_last_closed_stateid(struct nfs4_openowner *oo)
1489{
Jeff Layton217526e2014-07-30 08:27:11 -04001490 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1491 nfsd_net_id);
1492 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001493
Jeff Layton217526e2014-07-30 08:27:11 -04001494 spin_lock(&nn->client_lock);
1495 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001496 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001497 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001498 oo->oo_last_closed_stid = NULL;
1499 }
Jeff Layton217526e2014-07-30 08:27:11 -04001500 spin_unlock(&nn->client_lock);
1501 if (s)
1502 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001503}
1504
Jeff Layton2c41beb2014-07-29 21:34:41 -04001505static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001506{
1507 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001508 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001509 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001510
Jeff Layton2c41beb2014-07-29 21:34:41 -04001511 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001512
Trond Myklebustd4f04892014-07-29 21:34:36 -04001513 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001514 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001515 while (!list_empty(&oo->oo_owner.so_stateids)) {
1516 stp = list_first_entry(&oo->oo_owner.so_stateids,
1517 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001518 if (unhash_open_stateid(stp, &reaplist))
1519 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001520 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001521 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001522 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001523 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001524 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001525}
1526
Marc Eshel5282fd72009-04-03 08:27:52 +03001527static inline int
1528hash_sessionid(struct nfs4_sessionid *sessionid)
1529{
1530 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1531
1532 return sid->sequence % SESSION_HASH_SIZE;
1533}
1534
Mark Salter135dd002015-04-06 09:46:00 -04001535#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001536static inline void
1537dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1538{
1539 u32 *ptr = (u32 *)(&sessionid->data[0]);
1540 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1541}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001542#else
1543static inline void
1544dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1545{
1546}
1547#endif
1548
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001549/*
1550 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1551 * won't be used for replay.
1552 */
1553void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1554{
1555 struct nfs4_stateowner *so = cstate->replay_owner;
1556
1557 if (nfserr == nfserr_replay_me)
1558 return;
1559
1560 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001561 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001562 return;
1563 }
1564 if (!so)
1565 return;
1566 if (so->so_is_open_owner)
1567 release_last_closed_stateid(openowner(so));
1568 so->so_seqid++;
1569 return;
1570}
Marc Eshel5282fd72009-04-03 08:27:52 +03001571
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001572static void
1573gen_sessionid(struct nfsd4_session *ses)
1574{
1575 struct nfs4_client *clp = ses->se_client;
1576 struct nfsd4_sessionid *sid;
1577
1578 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1579 sid->clientid = clp->cl_clientid;
1580 sid->sequence = current_sessionid++;
1581 sid->reserved = 0;
1582}
1583
1584/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001585 * The protocol defines ca_maxresponssize_cached to include the size of
1586 * the rpc header, but all we need to cache is the data starting after
1587 * the end of the initial SEQUENCE operation--the rest we regenerate
1588 * each time. Therefore we can advertise a ca_maxresponssize_cached
1589 * value that is the number of bytes in our cache plus a few additional
1590 * bytes. In order to stay on the safe side, and not promise more than
1591 * we can cache, those additional bytes must be the minimum possible: 24
1592 * bytes of rpc header (xid through accept state, with AUTH_NULL
1593 * verifier), 12 for the compound header (with zero-length tag), and 44
1594 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001595 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001596#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1597
Andy Adamson557ce262009-08-28 08:45:04 -04001598static void
1599free_session_slots(struct nfsd4_session *ses)
1600{
1601 int i;
1602
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001603 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1604 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001605 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001606 }
Andy Adamson557ce262009-08-28 08:45:04 -04001607}
1608
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001609/*
1610 * We don't actually need to cache the rpc and session headers, so we
1611 * can allocate a little less for each slot:
1612 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001613static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001614{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001615 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001616
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001617 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1618 size = 0;
1619 else
1620 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1621 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001622}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001623
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001624/*
1625 * XXX: If we run out of reserved DRC memory we could (up to a point)
1626 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001627 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001628 */
NeilBrown2030ca52019-09-20 16:36:45 +10001629static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001630{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001631 u32 slotsize = slot_bytes(ca);
1632 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001633 unsigned long avail, total_avail;
NeilBrown2030ca52019-09-20 16:36:45 +10001634 unsigned int scale_factor;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001635
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001636 spin_lock(&nfsd_drc_lock);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001637 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1638 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1639 else
1640 /* We have handed out more space than we chose in
1641 * set_max_drc() to allow. That isn't really a
1642 * problem as long as that doesn't make us think we
1643 * have lots more due to integer overflow.
1644 */
1645 total_avail = 0;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001646 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001647 /*
NeilBrown2030ca52019-09-20 16:36:45 +10001648 * Never use more than a fraction of the remaining memory,
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001649 * unless it's the only way to give this client a slot.
NeilBrown2030ca52019-09-20 16:36:45 +10001650 * The chosen fraction is either 1/8 or 1/number of threads,
1651 * whichever is smaller. This ensures there are adequate
1652 * slots to support multiple clients per thread.
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001653 * Give the client one slot even if that would require
1654 * over-allocation--it is better than failure.
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001655 */
NeilBrown2030ca52019-09-20 16:36:45 +10001656 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1657
1658 avail = clamp_t(unsigned long, avail, slotsize,
1659 total_avail/scale_factor);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001660 num = min_t(int, num, avail / slotsize);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001661 num = max_t(int, num, 1);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001662 nfsd_drc_mem_used += num * slotsize;
1663 spin_unlock(&nfsd_drc_lock);
1664
1665 return num;
1666}
1667
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001668static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001669{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001670 int slotsize = slot_bytes(ca);
1671
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001672 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001673 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001674 spin_unlock(&nfsd_drc_lock);
1675}
1676
Kinglong Mee60810e52014-01-01 00:35:47 +08001677static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1678 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001679{
Kinglong Mee60810e52014-01-01 00:35:47 +08001680 int numslots = fattrs->maxreqs;
1681 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001682 struct nfsd4_session *new;
1683 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001684
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001685 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001686 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1687 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001688
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001689 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001690 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001691 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001692 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001693 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001694 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001695 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001696 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001697 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001698
1699 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1700 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1701
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001702 return new;
1703out_free:
1704 while (i--)
1705 kfree(new->se_slots[i]);
1706 kfree(new);
1707 return NULL;
1708}
1709
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001710static void free_conn(struct nfsd4_conn *c)
1711{
1712 svc_xprt_put(c->cn_xprt);
1713 kfree(c);
1714}
1715
1716static void nfsd4_conn_lost(struct svc_xpt_user *u)
1717{
1718 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1719 struct nfs4_client *clp = c->cn_session->se_client;
1720
1721 spin_lock(&clp->cl_lock);
1722 if (!list_empty(&c->cn_persession)) {
1723 list_del(&c->cn_persession);
1724 free_conn(c);
1725 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001726 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001727 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001728}
1729
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001730static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001731{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001732 struct nfsd4_conn *conn;
1733
1734 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1735 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001736 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001737 svc_xprt_get(rqstp->rq_xprt);
1738 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001739 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001740 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1741 return conn;
1742}
1743
J. Bruce Fields328ead22010-09-29 16:11:06 -04001744static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1745{
1746 conn->cn_session = ses;
1747 list_add(&conn->cn_persession, &ses->se_conns);
1748}
1749
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001750static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1751{
1752 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001753
1754 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001755 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001756 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001757}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001758
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001759static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001760{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001761 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001762 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001763}
1764
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001765static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001766{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001767 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001768
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001769 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001770 ret = nfsd4_register_conn(conn);
1771 if (ret)
1772 /* oops; xprt is already down: */
1773 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001774 /* We may have gained or lost a callback channel: */
1775 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001776}
1777
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001778static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001779{
1780 u32 dir = NFS4_CDFC4_FORE;
1781
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001782 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001783 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001784 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001785}
1786
1787/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001788static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001789{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001790 struct nfs4_client *clp = s->se_client;
1791 struct nfsd4_conn *c;
1792
1793 spin_lock(&clp->cl_lock);
1794 while (!list_empty(&s->se_conns)) {
1795 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1796 list_del_init(&c->cn_persession);
1797 spin_unlock(&clp->cl_lock);
1798
1799 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1800 free_conn(c);
1801
1802 spin_lock(&clp->cl_lock);
1803 }
1804 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001805}
1806
J. Bruce Fields1377b692012-09-11 21:42:40 -04001807static void __free_session(struct nfsd4_session *ses)
1808{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001809 free_session_slots(ses);
1810 kfree(ses);
1811}
1812
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001813static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001814{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001815 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001816 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001817 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001818}
Andy Adamson557ce262009-08-28 08:45:04 -04001819
Fengguang Wu135ae822012-11-10 07:20:25 -05001820static 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 -04001821{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001822 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001823 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001824
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001825 new->se_client = clp;
1826 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001827
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001828 INIT_LIST_HEAD(&new->se_conns);
1829
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001830 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001831 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001832 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001833 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001834 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001835 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001836 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001837 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001838 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001839 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001840
Chuck Leverb0d2e422014-08-22 15:10:59 -04001841 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001842 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001843 /*
1844 * This is a little silly; with sessions there's no real
1845 * use for the callback address. Use the peer address
1846 * as a reasonable default for now, but consider fixing
1847 * the rpc client not to require an address in the
1848 * future:
1849 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001850 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1851 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001852 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001853}
1854
Benny Halevy9089f1b2010-05-12 00:12:26 +03001855/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001856static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001857__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001858{
1859 struct nfsd4_session *elem;
1860 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001861 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001862
Trond Myklebust0a880a22014-07-30 08:27:10 -04001863 lockdep_assert_held(&nn->client_lock);
1864
Marc Eshel5282fd72009-04-03 08:27:52 +03001865 dump_sessionid(__func__, sessionid);
1866 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001867 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001868 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001869 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1870 NFS4_MAX_SESSIONID_LEN)) {
1871 return elem;
1872 }
1873 }
1874
1875 dprintk("%s: session not found\n", __func__);
1876 return NULL;
1877}
1878
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001879static struct nfsd4_session *
1880find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1881 __be32 *ret)
1882{
1883 struct nfsd4_session *session;
1884 __be32 status = nfserr_badsession;
1885
1886 session = __find_in_sessionid_hashtbl(sessionid, net);
1887 if (!session)
1888 goto out;
1889 status = nfsd4_get_session_locked(session);
1890 if (status)
1891 session = NULL;
1892out:
1893 *ret = status;
1894 return session;
1895}
1896
Benny Halevy9089f1b2010-05-12 00:12:26 +03001897/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001898static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001899unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001900{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001901 struct nfs4_client *clp = ses->se_client;
1902 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1903
1904 lockdep_assert_held(&nn->client_lock);
1905
Andy Adamson7116ed62009-04-03 08:27:43 +03001906 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001907 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001908 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001909 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001910}
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1913static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001914STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001916 /*
1917 * We're assuming the clid was not given out from a boot
1918 * precisely 2^32 (about 136 years) before this one. That seems
1919 * a safe assumption:
1920 */
1921 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return 0;
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04001923 trace_nfsd_clid_stale(clid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 1;
1925}
1926
1927/*
1928 * XXX Should we use a slab cache ?
1929 * This type of memory management is somewhat inefficient, but we use it
1930 * anyway since SETCLIENTID is not a common operation.
1931 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001932static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001935 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Jeff Layton9258a2d2018-03-16 09:47:22 -04001937 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001938 if (clp == NULL)
1939 return NULL;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04001940 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001941 if (clp->cl_name.data == NULL)
1942 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001943 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1944 sizeof(struct list_head),
1945 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001946 if (!clp->cl_ownerstr_hashtbl)
1947 goto err_no_hashtbl;
1948 for (i = 0; i < OWNER_HASH_SIZE; i++)
1949 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
Trond Myklebust5694c932014-04-18 14:43:56 -04001950 INIT_LIST_HEAD(&clp->cl_sessions);
1951 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001952 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04001953 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1954 INIT_LIST_HEAD(&clp->cl_idhash);
1955 INIT_LIST_HEAD(&clp->cl_openowners);
1956 INIT_LIST_HEAD(&clp->cl_delegations);
1957 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001958 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001959#ifdef CONFIG_NFSD_PNFS
1960 INIT_LIST_HEAD(&clp->cl_lo_states);
1961#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001962 INIT_LIST_HEAD(&clp->async_copies);
1963 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001964 spin_lock_init(&clp->cl_lock);
1965 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001967err_no_hashtbl:
1968 kfree(clp->cl_name.data);
1969err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001970 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001971 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001974static void __free_client(struct kref *k)
1975{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001976 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1977 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001978
1979 free_svc_cred(&clp->cl_cred);
1980 kfree(clp->cl_ownerstr_hashtbl);
1981 kfree(clp->cl_name.data);
J. Bruce Fields79123442019-06-05 12:42:05 -04001982 kfree(clp->cl_nii_domain.data);
1983 kfree(clp->cl_nii_name.data);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001984 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001985 kmem_cache_free(client_slab, clp);
1986}
1987
YueHaibing297e57a2019-07-08 15:29:33 +08001988static void drop_client(struct nfs4_client *clp)
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001989{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001990 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001991}
1992
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001993static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994free_client(struct nfs4_client *clp)
1995{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001996 while (!list_empty(&clp->cl_sessions)) {
1997 struct nfsd4_session *ses;
1998 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1999 se_perclnt);
2000 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002001 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2002 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002003 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04002004 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002005 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002006 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002007 clp->cl_nfsd_dentry = NULL;
2008 wake_up_all(&expiry_wq);
2009 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002010 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Benny Halevy84d38ac2010-05-12 00:13:16 +03002013/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04002014static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03002015unhash_client_locked(struct nfs4_client *clp)
2016{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002017 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002018 struct nfsd4_session *ses;
2019
Trond Myklebust0a880a22014-07-30 08:27:10 -04002020 lockdep_assert_held(&nn->client_lock);
2021
Trond Myklebust4beb3452014-07-30 08:27:02 -04002022 /* Mark the client as expired! */
2023 clp->cl_time = 0;
2024 /* Make it invisible */
2025 if (!list_empty(&clp->cl_idhash)) {
2026 list_del_init(&clp->cl_idhash);
2027 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2028 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2029 else
2030 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2031 }
2032 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002033 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002034 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2035 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002036 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03002037}
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04002040unhash_client(struct nfs4_client *clp)
2041{
2042 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2043
2044 spin_lock(&nn->client_lock);
2045 unhash_client_locked(clp);
2046 spin_unlock(&nn->client_lock);
2047}
2048
Jeff Layton97403d92014-07-30 08:27:12 -04002049static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2050{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04002051 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04002052 return nfserr_jukebox;
2053 unhash_client_locked(clp);
2054 return nfs_ok;
2055}
2056
Trond Myklebust4beb3452014-07-30 08:27:02 -04002057static void
2058__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002060 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002061 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct list_head reaplist;
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04002066 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07002067 while (!list_empty(&clp->cl_delegations)) {
2068 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04002069 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04002070 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
Benny Halevycdc975052014-05-30 09:09:30 -04002072 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 while (!list_empty(&reaplist)) {
2074 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04002075 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05002076 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04002078 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05002079 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04002080 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04002081 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002082 }
NeilBrownea1da632005-06-23 22:04:17 -07002083 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002084 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002085 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002086 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002088 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2089 struct nfs4_stateowner *so, *tmp;
2090
2091 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2092 so_strhash) {
2093 /* Should be no openowners at this point */
2094 WARN_ON_ONCE(so->so_is_open_owner);
2095 remove_blocked_locks(lockowner(so));
2096 }
2097 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002098 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002099 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002100 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002101 if (clp->cl_cb_conn.cb_xprt)
2102 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002103 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002104 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Trond Myklebust4beb3452014-07-30 08:27:02 -04002107static void
2108destroy_client(struct nfs4_client *clp)
2109{
2110 unhash_client(clp);
2111 __destroy_client(clp);
2112}
2113
Scott Mayhew362063a2019-03-26 18:06:28 -04002114static void inc_reclaim_complete(struct nfs4_client *clp)
2115{
2116 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2117
2118 if (!nn->track_reclaim_completes)
2119 return;
2120 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2121 return;
2122 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2123 nn->reclaim_str_hashtbl_size) {
2124 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2125 clp->net->ns.inum);
2126 nfsd4_end_grace(nn);
2127 }
2128}
2129
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002130static void expire_client(struct nfs4_client *clp)
2131{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002132 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002133 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002134 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002135}
2136
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002137static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2138{
2139 memcpy(target->cl_verifier.data, source->data,
2140 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002143static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2146 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2147}
2148
J. Bruce Fields50043852015-11-20 15:58:37 -05002149static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2150{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002151 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2152 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2153 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002154 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2155 if ((source->cr_principal && !target->cr_principal) ||
2156 (source->cr_raw_principal && !target->cr_raw_principal) ||
2157 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002158 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002159
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002160 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 target->cr_uid = source->cr_uid;
2162 target->cr_gid = source->cr_gid;
2163 target->cr_group_info = source->cr_group_info;
2164 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002165 target->cr_gss_mech = source->cr_gss_mech;
2166 if (source->cr_gss_mech)
2167 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002171static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002172compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2173{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002174 if (o1->len < o2->len)
2175 return -1;
2176 if (o1->len > o2->len)
2177 return 1;
2178 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002179}
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002182same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2183{
2184 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
2187static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002188same_clid(clientid_t *cl1, clientid_t *cl2)
2189{
2190 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002193static bool groups_equal(struct group_info *g1, struct group_info *g2)
2194{
2195 int i;
2196
2197 if (g1->ngroups != g2->ngroups)
2198 return false;
2199 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002200 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002201 return false;
2202 return true;
2203}
2204
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002205/*
2206 * RFC 3530 language requires clid_inuse be returned when the
2207 * "principal" associated with a requests differs from that previously
2208 * used. We use uid, gid's, and gss principal string as our best
2209 * approximation. We also don't want to allow non-gss use of a client
2210 * established using gss: in theory cr_principal should catch that
2211 * change, but in practice cr_principal can be null even in the gss case
2212 * since gssd doesn't always pass down a principal string.
2213 */
2214static bool is_gss_cred(struct svc_cred *cr)
2215{
2216 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2217 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2218}
2219
2220
Vivek Trivedi5559b502012-07-24 21:18:20 +05302221static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002222same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2223{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002224 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002225 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2226 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002227 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2228 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002229 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002230 if (cr1->cr_principal == cr2->cr_principal)
2231 return true;
2232 if (!cr1->cr_principal || !cr2->cr_principal)
2233 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302234 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
J. Bruce Fields57266a62013-04-13 14:27:29 -04002237static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2238{
2239 struct svc_cred *cr = &rqstp->rq_cred;
2240 u32 service;
2241
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002242 if (!cr->cr_gss_mech)
2243 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002244 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2245 return service == RPC_GSS_SVC_INTEGRITY ||
2246 service == RPC_GSS_SVC_PRIVACY;
2247}
2248
Andrew Elblededeb132016-06-15 12:52:08 -04002249bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002250{
2251 struct svc_cred *cr = &rqstp->rq_cred;
2252
2253 if (!cl->cl_mach_cred)
2254 return true;
2255 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2256 return false;
2257 if (!svc_rqst_integrity_protected(rqstp))
2258 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002259 if (cl->cl_cred.cr_raw_principal)
2260 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2261 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002262 if (!cr->cr_principal)
2263 return false;
2264 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2265}
2266
Jeff Layton294ac322014-07-30 08:27:15 -04002267static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002268{
Chuck Leverab4684d2012-03-02 17:13:50 -05002269 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Jeff Laytonf4199922014-06-17 07:44:11 -04002271 /*
2272 * This is opaque to client, so no need to byte-swap. Use
2273 * __force to keep sparse happy
2274 */
Arnd Bergmann9104ae42019-11-04 16:45:30 +01002275 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002276 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002277 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
Jeff Layton294ac322014-07-30 08:27:15 -04002280static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2281{
Arnd Bergmann9cc76802019-10-31 15:53:13 +01002282 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
Jeff Layton294ac322014-07-30 08:27:15 -04002283 clp->cl_clientid.cl_id = nn->clientid_counter++;
2284 gen_confirm(clp, nn);
2285}
2286
Jeff Layton4770d722014-07-29 21:34:10 -04002287static struct nfs4_stid *
2288find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002289{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002290 struct nfs4_stid *ret;
2291
2292 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2293 if (!ret || !ret->sc_type)
2294 return NULL;
2295 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002296}
2297
Jeff Layton4770d722014-07-29 21:34:10 -04002298static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002299find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002300{
2301 struct nfs4_stid *s;
2302
Jeff Layton4770d722014-07-29 21:34:10 -04002303 spin_lock(&cl->cl_lock);
2304 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002305 if (s != NULL) {
2306 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002307 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002308 else
2309 s = NULL;
2310 }
Jeff Layton4770d722014-07-29 21:34:10 -04002311 spin_unlock(&cl->cl_lock);
2312 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002313}
2314
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002315static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2316{
2317 struct nfsdfs_client *nc;
2318 nc = get_nfsdfs_client(inode);
2319 if (!nc)
2320 return NULL;
2321 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2322}
2323
J. Bruce Fields169319f2019-06-19 12:39:46 -04002324static void seq_quote_mem(struct seq_file *m, char *data, int len)
2325{
2326 seq_printf(m, "\"");
2327 seq_escape_mem_ascii(m, data, len);
2328 seq_printf(m, "\"");
2329}
2330
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002331static int client_info_show(struct seq_file *m, void *v)
2332{
2333 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002334 struct nfs4_client *clp;
2335 u64 clid;
2336
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002337 clp = get_nfsdfs_clp(inode);
2338 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002339 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002340 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2341 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002342 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2343 seq_printf(m, "name: ");
2344 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2345 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields79123442019-06-05 12:42:05 -04002346 if (clp->cl_nii_domain.data) {
2347 seq_printf(m, "Implementation domain: ");
2348 seq_quote_mem(m, clp->cl_nii_domain.data,
2349 clp->cl_nii_domain.len);
2350 seq_printf(m, "\nImplementation name: ");
2351 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
Arnd Bergmanne29f4702019-10-31 14:52:43 +01002352 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
J. Bruce Fields79123442019-06-05 12:42:05 -04002353 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2354 }
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002355 drop_client(clp);
2356
2357 return 0;
2358}
2359
2360static int client_info_open(struct inode *inode, struct file *file)
2361{
2362 return single_open(file, client_info_show, inode);
2363}
2364
2365static const struct file_operations client_info_fops = {
2366 .open = client_info_open,
2367 .read = seq_read,
2368 .llseek = seq_lseek,
2369 .release = single_release,
2370};
2371
J. Bruce Fields78599c42019-04-22 15:26:23 -04002372static void *states_start(struct seq_file *s, loff_t *pos)
2373 __acquires(&clp->cl_lock)
2374{
2375 struct nfs4_client *clp = s->private;
2376 unsigned long id = *pos;
2377 void *ret;
2378
2379 spin_lock(&clp->cl_lock);
2380 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2381 *pos = id;
2382 return ret;
2383}
2384
2385static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2386{
2387 struct nfs4_client *clp = s->private;
2388 unsigned long id = *pos;
2389 void *ret;
2390
2391 id = *pos;
2392 id++;
2393 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2394 *pos = id;
2395 return ret;
2396}
2397
2398static void states_stop(struct seq_file *s, void *v)
2399 __releases(&clp->cl_lock)
2400{
2401 struct nfs4_client *clp = s->private;
2402
2403 spin_unlock(&clp->cl_lock);
2404}
2405
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002406static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002407{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002408 struct inode *inode = f->nf_inode;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002409
2410 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2411 MAJOR(inode->i_sb->s_dev),
2412 MINOR(inode->i_sb->s_dev),
2413 inode->i_ino);
2414}
2415
2416static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2417{
2418 seq_printf(s, "owner: ");
2419 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2420}
2421
2422static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2423{
2424 struct nfs4_ol_stateid *ols;
2425 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002426 struct nfsd_file *file;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002427 struct nfs4_stateowner *oo;
2428 unsigned int access, deny;
2429
2430 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2431 return 0; /* XXX: or SEQ_SKIP? */
2432 ols = openlockstateid(st);
2433 oo = ols->st_stateowner;
2434 nf = st->sc_file;
2435 file = find_any_file(nf);
2436
2437 seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid);
2438
2439 access = bmap_to_share_mode(ols->st_access_bmap);
2440 deny = bmap_to_share_mode(ols->st_deny_bmap);
2441
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002442 seq_printf(s, "access: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002443 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2444 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002445 seq_printf(s, "deny: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002446 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2447 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2448
2449 nfs4_show_superblock(s, file);
2450 seq_printf(s, ", ");
2451 nfs4_show_owner(s, oo);
2452 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002453 nfsd_file_put(file);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002454
2455 return 0;
2456}
2457
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002458static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2459{
2460 struct nfs4_ol_stateid *ols;
2461 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002462 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002463 struct nfs4_stateowner *oo;
2464
2465 ols = openlockstateid(st);
2466 oo = ols->st_stateowner;
2467 nf = st->sc_file;
2468 file = find_any_file(nf);
2469
2470 seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid);
2471
2472 /*
2473 * Note: a lock stateid isn't really the same thing as a lock,
2474 * it's the locking state held by one owner on a file, and there
2475 * may be multiple (or no) lock ranges associated with it.
2476 * (Same for the matter is true of open stateids.)
2477 */
2478
2479 nfs4_show_superblock(s, file);
2480 /* XXX: open stateid? */
2481 seq_printf(s, ", ");
2482 nfs4_show_owner(s, oo);
2483 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002484 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002485
2486 return 0;
2487}
2488
2489static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2490{
2491 struct nfs4_delegation *ds;
2492 struct nfs4_file *nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002493 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002494
2495 ds = delegstateid(st);
2496 nf = st->sc_file;
2497 file = nf->fi_deleg_file;
2498
2499 seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid);
2500
2501 /* Kinda dead code as long as we only support read delegs: */
2502 seq_printf(s, "access: %s, ",
2503 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2504
2505 /* XXX: lease time, whether it's being recalled. */
2506
2507 nfs4_show_superblock(s, file);
2508 seq_printf(s, " }\n");
2509
2510 return 0;
2511}
2512
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002513static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2514{
2515 struct nfs4_layout_stateid *ls;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002516 struct nfsd_file *file;
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002517
2518 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2519 file = ls->ls_file;
2520
2521 seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid);
2522
2523 /* XXX: What else would be useful? */
2524
2525 nfs4_show_superblock(s, file);
2526 seq_printf(s, " }\n");
2527
2528 return 0;
2529}
2530
J. Bruce Fields78599c42019-04-22 15:26:23 -04002531static int states_show(struct seq_file *s, void *v)
2532{
2533 struct nfs4_stid *st = v;
2534
2535 switch (st->sc_type) {
2536 case NFS4_OPEN_STID:
2537 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002538 case NFS4_LOCK_STID:
2539 return nfs4_show_lock(s, st);
2540 case NFS4_DELEG_STID:
2541 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002542 case NFS4_LAYOUT_STID:
2543 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002544 default:
2545 return 0; /* XXX: or SEQ_SKIP? */
2546 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002547 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002548}
2549
2550static struct seq_operations states_seq_ops = {
2551 .start = states_start,
2552 .next = states_next,
2553 .stop = states_stop,
2554 .show = states_show
2555};
2556
2557static int client_states_open(struct inode *inode, struct file *file)
2558{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002559 struct seq_file *s;
2560 struct nfs4_client *clp;
2561 int ret;
2562
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002563 clp = get_nfsdfs_clp(inode);
2564 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002565 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002566
2567 ret = seq_open(file, &states_seq_ops);
2568 if (ret)
2569 return ret;
2570 s = file->private_data;
2571 s->private = clp;
2572 return 0;
2573}
2574
2575static int client_opens_release(struct inode *inode, struct file *file)
2576{
2577 struct seq_file *m = file->private_data;
2578 struct nfs4_client *clp = m->private;
2579
2580 /* XXX: alternatively, we could get/drop in seq start/stop */
2581 drop_client(clp);
2582 return 0;
2583}
2584
2585static const struct file_operations client_states_fops = {
2586 .open = client_states_open,
2587 .read = seq_read,
2588 .llseek = seq_lseek,
2589 .release = client_opens_release,
2590};
2591
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002592/*
2593 * Normally we refuse to destroy clients that are in use, but here the
2594 * administrator is telling us to just do it. We also want to wait
2595 * so the caller has a guarantee that the client's locks are gone by
2596 * the time the write returns:
2597 */
YueHaibing297e57a2019-07-08 15:29:33 +08002598static void force_expire_client(struct nfs4_client *clp)
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002599{
2600 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2601 bool already_expired;
2602
2603 spin_lock(&clp->cl_lock);
2604 clp->cl_time = 0;
2605 spin_unlock(&clp->cl_lock);
2606
2607 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2608 spin_lock(&nn->client_lock);
2609 already_expired = list_empty(&clp->cl_lru);
2610 if (!already_expired)
2611 unhash_client_locked(clp);
2612 spin_unlock(&nn->client_lock);
2613
2614 if (!already_expired)
2615 expire_client(clp);
2616 else
2617 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2618}
2619
2620static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2621 size_t size, loff_t *pos)
2622{
2623 char *data;
2624 struct nfs4_client *clp;
2625
2626 data = simple_transaction_get(file, buf, size);
2627 if (IS_ERR(data))
2628 return PTR_ERR(data);
2629 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2630 return -EINVAL;
2631 clp = get_nfsdfs_clp(file_inode(file));
2632 if (!clp)
2633 return -ENXIO;
2634 force_expire_client(clp);
2635 drop_client(clp);
2636 return 7;
2637}
2638
2639static const struct file_operations client_ctl_fops = {
2640 .write = client_ctl_write,
2641 .release = simple_transaction_release,
2642};
2643
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002644static const struct tree_descr client_files[] = {
2645 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002646 [1] = {"states", &client_states_fops, S_IRUSR},
Petr Vorel6cbfad52020-03-13 13:39:57 +01002647 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002648 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002649};
2650
Jeff Layton2216d442012-11-12 15:00:57 -05002651static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002652 struct svc_rqst *rqstp, nfs4_verifier *verf)
2653{
2654 struct nfs4_client *clp;
2655 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002656 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002657 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002658 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002659
2660 clp = alloc_client(name);
2661 if (clp == NULL)
2662 return NULL;
2663
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002664 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2665 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002666 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002667 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002668 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002669 gen_clid(clp, nn);
2670 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002671 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01002672 clp->cl_time = ktime_get_boottime_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002673 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002674 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002675 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002676 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002677 clp->net = net;
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002678 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002679 clp->cl_clientid.cl_id - nn->clientid_base,
2680 client_files);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002681 if (!clp->cl_nfsd_dentry) {
2682 free_client(clp);
2683 return NULL;
2684 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002685 return clp;
2686}
2687
NeilBrownfd39ca92005-06-23 22:04:03 -07002688static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002689add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2690{
2691 struct rb_node **new = &(root->rb_node), *parent = NULL;
2692 struct nfs4_client *clp;
2693
2694 while (*new) {
2695 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2696 parent = *new;
2697
2698 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2699 new = &((*new)->rb_left);
2700 else
2701 new = &((*new)->rb_right);
2702 }
2703
2704 rb_link_node(&new_clp->cl_namenode, parent, new);
2705 rb_insert_color(&new_clp->cl_namenode, root);
2706}
2707
2708static struct nfs4_client *
2709find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2710{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002711 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002712 struct rb_node *node = root->rb_node;
2713 struct nfs4_client *clp;
2714
2715 while (node) {
2716 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2717 cmp = compare_blob(&clp->cl_name, name);
2718 if (cmp > 0)
2719 node = node->rb_left;
2720 else if (cmp < 0)
2721 node = node->rb_right;
2722 else
2723 return clp;
2724 }
2725 return NULL;
2726}
2727
2728static void
2729add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
2731 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002732 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Trond Myklebust0a880a22014-07-30 08:27:10 -04002734 lockdep_assert_held(&nn->client_lock);
2735
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002736 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002737 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002739 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002740 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
NeilBrownfd39ca92005-06-23 22:04:03 -07002743static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744move_to_confirmed(struct nfs4_client *clp)
2745{
2746 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002747 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Trond Myklebust0a880a22014-07-30 08:27:10 -04002749 lockdep_assert_held(&nn->client_lock);
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002752 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002753 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002754 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002755 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002756 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
2759static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002760find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
2762 struct nfs4_client *clp;
2763 unsigned int idhashval = clientid_hashval(clid->cl_id);
2764
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002765 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002766 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002767 if ((bool)clp->cl_minorversion != sessions)
2768 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002769 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
2773 return NULL;
2774}
2775
2776static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002777find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2778{
2779 struct list_head *tbl = nn->conf_id_hashtbl;
2780
Trond Myklebust0a880a22014-07-30 08:27:10 -04002781 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002782 return find_client_in_id_table(tbl, clid, sessions);
2783}
2784
2785static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002786find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002788 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Trond Myklebust0a880a22014-07-30 08:27:10 -04002790 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002791 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
2793
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002794static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002795{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002796 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002797}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002798
NeilBrown28ce6052005-06-23 22:03:56 -07002799static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002800find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002801{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002802 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002803 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002804}
2805
2806static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002807find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002808{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002809 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002810 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002811}
2812
NeilBrownfd39ca92005-06-23 22:04:03 -07002813static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002814gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002816 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002817 struct sockaddr *sa = svc_addr(rqstp);
2818 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002819 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Jeff Layton7077ecb2009-08-14 12:57:58 -04002821 /* Currently, we only support tcp and tcp6 for the callback channel */
2822 if (se->se_callback_netid_len == 3 &&
2823 !memcmp(se->se_callback_netid_val, "tcp", 3))
2824 expected_family = AF_INET;
2825 else if (se->se_callback_netid_len == 4 &&
2826 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2827 expected_family = AF_INET6;
2828 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 goto out_err;
2830
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002831 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002832 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002833 (struct sockaddr *)&conn->cb_addr,
2834 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002835
J. Bruce Fields07263f12010-05-31 19:09:40 -04002836 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002838
J. Bruce Fields07263f12010-05-31 19:09:40 -04002839 if (conn->cb_addr.ss_family == AF_INET6)
2840 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002841
J. Bruce Fields07263f12010-05-31 19:09:40 -04002842 conn->cb_prog = se->se_callback_prog;
2843 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002844 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return;
2846out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002847 conn->cb_addr.ss_family = AF_UNSPEC;
2848 conn->cb_addrlen = 0;
Rasmus Villemoes4ab495b2017-02-24 01:15:55 +01002849 dprintk("NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 "will not receive delegations\n",
2851 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return;
2854}
2855
Andy Adamson074fe892009-04-03 08:28:15 +03002856/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002857 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002858 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002859static void
Andy Adamson074fe892009-04-03 08:28:15 +03002860nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2861{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002862 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002863 struct nfsd4_slot *slot = resp->cstate.slot;
2864 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002865
Andy Adamson557ce262009-08-28 08:45:04 -04002866 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002867
J. Bruce Fields085def32017-10-18 16:17:18 -04002868 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002869 slot->sl_opcnt = resp->opcnt;
2870 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002871 free_svc_cred(&slot->sl_cred);
2872 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002873
J. Bruce Fields085def32017-10-18 16:17:18 -04002874 if (!nfsd4_cache_this(resp)) {
2875 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002876 return;
2877 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002878 slot->sl_flags |= NFSD4_SLOT_CACHED;
2879
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002880 base = resp->cstate.data_offset;
2881 slot->sl_datalen = buf->len - base;
2882 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002883 WARN(1, "%s: sessions DRC could not cache compound\n",
2884 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002885 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002886}
2887
2888/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002889 * Encode the replay sequence operation from the slot values.
2890 * If cachethis is FALSE encode the uncached rep error on the next
2891 * operation which sets resp->p and increments resp->opcnt for
2892 * nfs4svc_encode_compoundres.
2893 *
Andy Adamson074fe892009-04-03 08:28:15 +03002894 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002895static __be32
2896nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2897 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002898{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002899 struct nfsd4_op *op;
2900 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002901
Andy Adamsonabfabf82009-07-23 19:02:18 -04002902 /* Encode the replayed sequence operation */
2903 op = &args->ops[resp->opcnt - 1];
2904 nfsd4_encode_operation(resp, op);
2905
J. Bruce Fields085def32017-10-18 16:17:18 -04002906 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2907 return op->status;
2908 if (args->opcnt == 1) {
2909 /*
2910 * The original operation wasn't a solo sequence--we
2911 * always cache those--so this retry must not match the
2912 * original:
2913 */
2914 op->status = nfserr_seq_false_retry;
2915 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002916 op = &args->ops[resp->opcnt++];
2917 op->status = nfserr_retry_uncached_rep;
2918 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002919 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002920 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002921}
2922
2923/*
Andy Adamson557ce262009-08-28 08:45:04 -04002924 * The sequence operation is not cached because we can use the slot and
2925 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002926 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002927static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002928nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2929 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002930{
Andy Adamson557ce262009-08-28 08:45:04 -04002931 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002932 struct xdr_stream *xdr = &resp->xdr;
2933 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002934 __be32 status;
2935
Andy Adamson557ce262009-08-28 08:45:04 -04002936 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002937
Andy Adamsonabfabf82009-07-23 19:02:18 -04002938 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002939 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002940 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002941
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002942 p = xdr_reserve_space(xdr, slot->sl_datalen);
2943 if (!p) {
2944 WARN_ON_ONCE(1);
2945 return nfserr_serverfault;
2946 }
2947 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2948 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002949
Andy Adamson557ce262009-08-28 08:45:04 -04002950 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002951 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002952}
2953
Andy Adamson0733d212009-04-03 08:28:01 +03002954/*
2955 * Set the exchange_id flags returned by the server.
2956 */
2957static void
2958nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2959{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002960#ifdef CONFIG_NFSD_PNFS
2961 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2962#else
Andy Adamson0733d212009-04-03 08:28:01 +03002963 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002964#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002965
2966 /* Referrals are supported, Migration is not. */
2967 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2968
2969 /* set the wire flags to return to client. */
2970 clid->flags = new->cl_exchange_flags;
2971}
2972
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002973static bool client_has_openowners(struct nfs4_client *clp)
2974{
2975 struct nfs4_openowner *oo;
2976
2977 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2978 if (!list_empty(&oo->oo_owner.so_stateids))
2979 return true;
2980 }
2981 return false;
2982}
2983
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002984static bool client_has_state(struct nfs4_client *clp)
2985{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002986 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002987#ifdef CONFIG_NFSD_PNFS
2988 || !list_empty(&clp->cl_lo_states)
2989#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002990 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002991 || !list_empty(&clp->cl_sessions)
2992 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002993}
2994
J. Bruce Fields79123442019-06-05 12:42:05 -04002995static __be32 copy_impl_id(struct nfs4_client *clp,
2996 struct nfsd4_exchange_id *exid)
2997{
2998 if (!exid->nii_domain.data)
2999 return 0;
3000 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3001 if (!clp->cl_nii_domain.data)
3002 return nfserr_jukebox;
3003 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3004 if (!clp->cl_nii_name.data)
3005 return nfserr_jukebox;
Arnd Bergmanne29f4702019-10-31 14:52:43 +01003006 clp->cl_nii_time = exid->nii_time;
J. Bruce Fields79123442019-06-05 12:42:05 -04003007 return 0;
3008}
3009
Al Virob37ad282006-10-19 23:28:59 -07003010__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003011nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3012 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003013{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003014 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003015 struct nfs4_client *conf, *new;
3016 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003017 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04003018 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03003019 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04003020 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003021 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003022 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03003023
Jeff Layton363168b2009-08-14 12:57:56 -04003024 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03003025 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04003026 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03003027 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04003028 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03003029
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003030 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03003031 return nfserr_inval;
3032
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003033 new = create_client(exid->clname, rqstp, &verf);
3034 if (new == NULL)
3035 return nfserr_jukebox;
J. Bruce Fields79123442019-06-05 12:42:05 -04003036 status = copy_impl_id(new, exid);
3037 if (status)
3038 goto out_nolock;
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003039
Andy Adamson0733d212009-04-03 08:28:01 +03003040 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003041 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04003042 exid->spo_must_enforce[0] = 0;
3043 exid->spo_must_enforce[1] = (
3044 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3045 1 << (OP_EXCHANGE_ID - 32) |
3046 1 << (OP_CREATE_SESSION - 32) |
3047 1 << (OP_DESTROY_SESSION - 32) |
3048 1 << (OP_DESTROY_CLIENTID - 32));
3049
3050 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3051 1 << (OP_OPEN_DOWNGRADE) |
3052 1 << (OP_LOCKU) |
3053 1 << (OP_DELEGRETURN));
3054
3055 exid->spo_must_allow[1] &= (
3056 1 << (OP_TEST_STATEID - 32) |
3057 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003058 if (!svc_rqst_integrity_protected(rqstp)) {
3059 status = nfserr_inval;
3060 goto out_nolock;
3061 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003062 /*
3063 * Sometimes userspace doesn't give us a principal.
3064 * Which is a bug, really. Anyway, we can't enforce
3065 * MACH_CRED in that case, better to give up now:
3066 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05003067 if (!new->cl_cred.cr_principal &&
3068 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003069 status = nfserr_serverfault;
3070 goto out_nolock;
3071 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003072 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03003073 case SP4_NONE:
3074 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003075 default: /* checked by xdr code */
3076 WARN_ON_ONCE(1);
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06003077 /* fall through */
Andy Adamson0733d212009-04-03 08:28:01 +03003078 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08003079 status = nfserr_encr_alg_unsupp;
3080 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03003081 }
3082
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003083 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003084 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003085 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03003086 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003087 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3088 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3089
J. Bruce Fields136e6582012-05-12 20:37:23 -04003090 if (update) {
3091 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003092 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03003093 goto out;
3094 }
Andrew Elblededeb132016-06-15 12:52:08 -04003095 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003096 status = nfserr_wrong_cred;
3097 goto out;
3098 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003099 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03003100 status = nfserr_perm;
3101 goto out;
3102 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003103 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03003104 status = nfserr_not_same;
3105 goto out;
3106 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003107 /* case 6 */
3108 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003109 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003110 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003111 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003112 if (client_has_state(conf)) {
3113 status = nfserr_clid_inuse;
3114 goto out;
3115 }
Andy Adamson0733d212009-04-03 08:28:01 +03003116 goto out_new;
3117 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003118 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003119 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003120 goto out_copy;
3121 }
3122 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003123 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003124 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003125 }
3126
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003127 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003128 status = nfserr_noent;
3129 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003130 }
3131
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003132 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003133 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003134 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003135
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003136 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03003137out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003138 if (conf) {
3139 status = mark_client_expired_locked(conf);
3140 if (status)
3141 goto out;
3142 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003143 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003144 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3145 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003146
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003147 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003148 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003149out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003150 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3151 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003152
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003153 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3154 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003155
3156 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003157 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003158 status = nfs_ok;
3159
3160out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003161 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003162out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003163 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003164 expire_client(new);
3165 if (unconf)
3166 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003167 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003168}
3169
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003170static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003171check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003172{
Andy Adamson88e588d2009-07-23 19:02:15 -04003173 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3174 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003175
3176 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003177 if (slot_inuse) {
3178 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003179 return nfserr_jukebox;
3180 else
3181 return nfserr_seq_misordered;
3182 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003183 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003184 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003185 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003186 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003187 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003188 return nfserr_seq_misordered;
3189}
3190
Andy Adamson49557cc2009-07-23 19:02:16 -04003191/*
3192 * Cache the create session result into the create session single DRC
3193 * slot cache by saving the xdr structure. sl_seqid has been set.
3194 * Do this for solo or embedded create session operations.
3195 */
3196static void
3197nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003198 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003199{
3200 slot->sl_status = nfserr;
3201 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3202}
3203
3204static __be32
3205nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3206 struct nfsd4_clid_slot *slot)
3207{
3208 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3209 return slot->sl_status;
3210}
3211
Mi Jinlong1b74c252011-07-14 14:50:17 +08003212#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3213 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3214 1 + /* MIN tag is length with zero, only length */ \
3215 3 + /* version, opcount, opcode */ \
3216 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3217 /* seqid, slotID, slotID, cache */ \
3218 4 ) * sizeof(__be32))
3219
3220#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3221 2 + /* verifier: AUTH_NULL, length 0 */\
3222 1 + /* status */ \
3223 1 + /* MIN tag is length with zero, only length */ \
3224 3 + /* opcount, opcode, opstatus*/ \
3225 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3226 /* seqid, slotID, slotID, slotID, status */ \
3227 5 ) * sizeof(__be32))
3228
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003229static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003230{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003231 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3232
J. Bruce Fields373cd402013-04-08 15:42:12 -04003233 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3234 return nfserr_toosmall;
3235 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3236 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003237 ca->headerpadsz = 0;
3238 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3239 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3240 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3241 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3242 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3243 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3244 /*
3245 * Note decreasing slot size below client's request may make it
3246 * difficult for client to function correctly, whereas
3247 * decreasing the number of slots will (just?) affect
3248 * performance. When short on memory we therefore prefer to
3249 * decrease number of slots instead of their size. Clients that
3250 * request larger slots than they need will get poor results:
NeilBrown7f49fd5d2019-09-20 16:33:16 +10003251 * Note that we always allow at least one slot, because our
3252 * accounting is soft and provides no guarantees either way.
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003253 */
NeilBrown2030ca52019-09-20 16:36:45 +10003254 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003255
J. Bruce Fields373cd402013-04-08 15:42:12 -04003256 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003257}
3258
Chuck Lever45006322016-03-01 13:06:02 -05003259/*
3260 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3261 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3262 */
3263#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3264 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3265
3266#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3267 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3268
Kinglong Mee8a891632013-12-23 18:11:02 +08003269#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003270 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003271#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003272 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3273 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003274
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003275static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3276{
3277 ca->headerpadsz = 0;
3278
Kinglong Mee8a891632013-12-23 18:11:02 +08003279 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003280 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003281 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003282 return nfserr_toosmall;
3283 ca->maxresp_cached = 0;
3284 if (ca->maxops < 2)
3285 return nfserr_toosmall;
3286
3287 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003288}
3289
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003290static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3291{
3292 switch (cbs->flavor) {
3293 case RPC_AUTH_NULL:
3294 case RPC_AUTH_UNIX:
3295 return nfs_ok;
3296 default:
3297 /*
3298 * GSS case: the spec doesn't allow us to return this
3299 * error. But it also doesn't allow us not to support
3300 * GSS.
3301 * I'd rather this fail hard than return some error the
3302 * client might think it can already handle:
3303 */
3304 return nfserr_encr_alg_unsupp;
3305 }
3306}
3307
Andy Adamson069b6ad2009-04-03 08:27:58 +03003308__be32
3309nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003310 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003311{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003312 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003313 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003314 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003315 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003316 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003317 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003318 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003319 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003320 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003321
Mi Jinlonga62573d2011-03-23 17:57:07 +08003322 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3323 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003324 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3325 if (status)
3326 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003327 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003328 if (status)
3329 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003330 status = check_backchannel_attrs(&cr_ses->back_channel);
3331 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003332 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003333 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003334 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003335 if (!new)
3336 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003337 conn = alloc_conn_from_crses(rqstp, cr_ses);
3338 if (!conn)
3339 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003340
Jeff Laytond20c11d2014-07-30 08:27:07 -04003341 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003342 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003343 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003344 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003345
3346 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003347 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003348 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003349 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003350 cs_slot = &conf->cl_cs_slot;
3351 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003352 if (status) {
3353 if (status == nfserr_replay_cache)
3354 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003355 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003356 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003357 } else if (unconf) {
3358 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003359 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003360 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003361 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003362 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003363 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003364 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003365 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003366 cs_slot = &unconf->cl_cs_slot;
3367 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003368 if (status) {
3369 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003370 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003371 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003372 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003373 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003374 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003375 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003376 if (status) {
3377 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003378 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003379 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003380 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003381 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003382 conf = unconf;
3383 } else {
3384 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003385 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003386 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003387 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003388 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003389 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003390 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003391 cr_ses->flags &= ~SESSION4_RDMA;
3392
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003393 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003394 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003395
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003396 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003397 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003398 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003399 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003400
Jeff Laytond20c11d2014-07-30 08:27:07 -04003401 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003402 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003403 spin_unlock(&nn->client_lock);
3404 /* init connection and backchannel */
3405 nfsd4_init_conn(rqstp, conn, new);
3406 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003407 if (old)
3408 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003409 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003410out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003411 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003412 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003413 if (old)
3414 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003415out_free_session:
3416 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003417out_release_drc_mem:
3418 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003419 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003420}
3421
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003422static __be32 nfsd4_map_bcts_dir(u32 *dir)
3423{
3424 switch (*dir) {
3425 case NFS4_CDFC4_FORE:
3426 case NFS4_CDFC4_BACK:
3427 return nfs_ok;
3428 case NFS4_CDFC4_FORE_OR_BOTH:
3429 case NFS4_CDFC4_BACK_OR_BOTH:
3430 *dir = NFS4_CDFC4_BOTH;
3431 return nfs_ok;
zhengbinfc5fc5d2019-12-19 17:29:20 +08003432 }
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003433 return nfserr_inval;
3434}
3435
Christoph Hellwigeb698532017-05-08 20:58:35 +02003436__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3437 struct nfsd4_compound_state *cstate,
3438 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003439{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003440 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003441 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003442 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003443 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003444
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003445 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3446 if (status)
3447 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003448 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003449 session->se_cb_prog = bc->bc_cb_program;
3450 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003451 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003452
3453 nfsd4_probe_callback(session->se_client);
3454
3455 return nfs_ok;
3456}
3457
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003458__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3459 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003460 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003461{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003462 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003463 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003464 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003465 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003466 struct net *net = SVC_NET(rqstp);
3467 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003468
3469 if (!nfsd4_last_compound_op(rqstp))
3470 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003471 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003472 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003473 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003474 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003475 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003476 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003477 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003478 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003479 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003480 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003481 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003482 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003483 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003484 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003485 goto out;
3486 nfsd4_init_conn(rqstp, conn, session);
3487 status = nfs_ok;
3488out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003489 nfsd4_put_session(session);
3490out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003491 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003492}
3493
J. Bruce Fields665d5072018-04-11 16:53:36 -04003494static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003495{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003496 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003497 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003498 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003499}
3500
Andy Adamson069b6ad2009-04-03 08:27:58 +03003501__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003502nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3503 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003504{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003505 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003506 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003507 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003508 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003509 struct net *net = SVC_NET(r);
3510 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003511
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003512 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003513 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003514 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003515 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003516 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003517 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003518 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003519 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003520 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003521 if (!ses)
3522 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003523 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003524 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003525 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003526 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003527 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003528 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003529 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003530 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003531
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003532 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003533
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003534 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003535 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003536out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003537 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003538out_client_lock:
3539 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003540out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003541 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003542}
3543
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003544static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003545{
3546 struct nfsd4_conn *c;
3547
3548 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003549 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04003550 return c;
3551 }
3552 }
3553 return NULL;
3554}
3555
J. Bruce Fields57266a62013-04-13 14:27:29 -04003556static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003557{
3558 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003559 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003560 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003561 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003562
3563 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003564 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003565 if (c)
3566 goto out_free;
3567 status = nfserr_conn_not_bound_to_session;
3568 if (clp->cl_mach_cred)
3569 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003570 __nfsd4_hash_conn(new, ses);
3571 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003572 ret = nfsd4_register_conn(new);
3573 if (ret)
3574 /* oops; xprt is already down: */
3575 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003576 return nfs_ok;
3577out_free:
3578 spin_unlock(&clp->cl_lock);
3579 free_conn(new);
3580 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003581}
3582
Mi Jinlong868b89c2011-04-27 09:09:58 +08003583static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3584{
3585 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3586
3587 return args->opcnt > session->se_fchannel.maxops;
3588}
3589
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003590static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3591 struct nfsd4_session *session)
3592{
3593 struct xdr_buf *xb = &rqstp->rq_arg;
3594
3595 return xb->len > session->se_fchannel.maxreq_sz;
3596}
3597
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003598static bool replay_matches_cache(struct svc_rqst *rqstp,
3599 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3600{
3601 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3602
3603 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3604 (bool)seq->cachethis)
3605 return false;
3606 /*
Scott Mayhew6e73e922019-10-09 15:11:37 -04003607 * If there's an error then the reply can have fewer ops than
3608 * the call.
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003609 */
Scott Mayhew6e73e922019-10-09 15:11:37 -04003610 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3611 return false;
3612 /*
3613 * But if we cached a reply with *more* ops than the call you're
3614 * sending us now, then this new call is clearly not really a
3615 * replay of the old one:
3616 */
3617 if (slot->sl_opcnt > argp->opcnt)
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003618 return false;
3619 /* This is the only check explicitly called by spec: */
3620 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3621 return false;
3622 /*
3623 * There may be more comparisons we could actually do, but the
3624 * spec doesn't require us to catch every case where the calls
3625 * don't match (that would require caching the call as well as
3626 * the reply), so we don't bother.
3627 */
3628 return true;
3629}
3630
Andy Adamson069b6ad2009-04-03 08:27:58 +03003631__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003632nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3633 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003634{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003635 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003636 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003637 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003638 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003639 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003640 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003641 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003642 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003643 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003644 struct net *net = SVC_NET(rqstp);
3645 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003646
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003647 if (resp->opcnt != 1)
3648 return nfserr_sequence_pos;
3649
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003650 /*
3651 * Will be either used or freed by nfsd4_sequence_check_conn
3652 * below.
3653 */
3654 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3655 if (!conn)
3656 return nfserr_jukebox;
3657
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003658 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003659 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003660 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003661 goto out_no_session;
3662 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003663
Mi Jinlong868b89c2011-04-27 09:09:58 +08003664 status = nfserr_too_many_ops;
3665 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003666 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003667
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003668 status = nfserr_req_too_big;
3669 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003670 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003671
Benny Halevyb85d4c02009-04-03 08:28:08 +03003672 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003673 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003674 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003675
Andy Adamson557ce262009-08-28 08:45:04 -04003676 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003677 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3678
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003679 /* We do not negotiate the number of slots yet, so set the
3680 * maxslots to the session maxreqs which is used to encode
3681 * sr_highest_slotid and the sr_target_slot id to maxslots */
3682 seq->maxslots = session->se_fchannel.maxreqs;
3683
J. Bruce Fields73e79482012-02-13 16:39:00 -05003684 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3685 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003686 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003687 status = nfserr_seq_misordered;
3688 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003689 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003690 status = nfserr_seq_false_retry;
3691 if (!replay_matches_cache(rqstp, seq, slot))
3692 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003693 cstate->slot = slot;
3694 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003695 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003696 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003697 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003698 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003699 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003700 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003701 }
3702 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003703 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003704
J. Bruce Fields57266a62013-04-13 14:27:29 -04003705 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003706 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003707 if (status)
3708 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003709
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003710 buflen = (seq->cachethis) ?
3711 session->se_fchannel.maxresp_cached :
3712 session->se_fchannel.maxresp_sz;
3713 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3714 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003715 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003716 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003717 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003718
3719 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003720 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003721 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003722 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003723 if (seq->cachethis)
3724 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003725 else
3726 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003727
3728 cstate->slot = slot;
3729 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003730 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003731
Benny Halevyb85d4c02009-04-03 08:28:08 +03003732out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003733 switch (clp->cl_cb_state) {
3734 case NFSD4_CB_DOWN:
3735 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3736 break;
3737 case NFSD4_CB_FAULT:
3738 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3739 break;
3740 default:
3741 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003742 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003743 if (!list_empty(&clp->cl_revoked))
3744 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003745out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003746 if (conn)
3747 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003748 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003749 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003750out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003751 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003752 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003753}
3754
Trond Myklebustb6076642014-06-30 11:48:35 -04003755void
3756nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3757{
3758 struct nfsd4_compound_state *cs = &resp->cstate;
3759
3760 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003761 if (cs->status != nfserr_replay_cache) {
3762 nfsd4_store_cache_entry(resp);
3763 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3764 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003765 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003766 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003767 } else if (cs->clp)
3768 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003769}
3770
Mi Jinlong345c2842011-10-20 17:51:39 +08003771__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003772nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3773 struct nfsd4_compound_state *cstate,
3774 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003775{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003776 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003777 struct nfs4_client *conf, *unconf;
3778 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003779 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003780 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003781
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003782 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003783 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003784 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003785 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003786
3787 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003788 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003789 status = nfserr_clientid_busy;
3790 goto out;
3791 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003792 status = mark_client_expired_locked(conf);
3793 if (status)
3794 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003795 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003796 } else if (unconf)
3797 clp = unconf;
3798 else {
3799 status = nfserr_stale_clientid;
3800 goto out;
3801 }
Andrew Elblededeb132016-06-15 12:52:08 -04003802 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003803 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003804 status = nfserr_wrong_cred;
3805 goto out;
3806 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003807 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003808out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003809 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003810 if (clp)
3811 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003812 return status;
3813}
3814
Andy Adamson069b6ad2009-04-03 08:27:58 +03003815__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003816nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3817 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003818{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003819 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003820 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003821
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003822 if (rc->rca_one_fs) {
3823 if (!cstate->current_fh.fh_dentry)
3824 return nfserr_nofilehandle;
3825 /*
3826 * We don't take advantage of the rca_one_fs case.
3827 * That's OK, it's optional, we can safely ignore it.
3828 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003829 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003830 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003831
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003832 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003833 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3834 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003835 goto out;
3836
3837 status = nfserr_stale_clientid;
3838 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003839 /*
3840 * The following error isn't really legal.
3841 * But we only get here if the client just explicitly
3842 * destroyed the client. Surely it no longer cares what
3843 * error it gets back on an operation for the dead
3844 * client.
3845 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003846 goto out;
3847
3848 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003849 nfsd4_client_record_create(cstate->session->se_client);
Scott Mayhew362063a2019-03-26 18:06:28 -04003850 inc_reclaim_complete(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003851out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003852 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003853}
3854
3855__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003856nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003857 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003859 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003860 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003862 struct nfs4_client *conf, *new;
3863 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003864 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003865 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3866
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003867 new = create_client(clname, rqstp, &clverifier);
3868 if (new == NULL)
3869 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003870 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003871 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003872 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003873 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003874 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003876 if (clp_used_exchangeid(conf))
3877 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003878 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04003879 trace_nfsd_clid_inuse_err(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 goto out;
3881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003883 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003884 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003885 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003886 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003887 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003889 gen_confirm(new, nn);
3890 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04003891 ;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003892 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003893 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003894 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3896 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3897 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003898 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 status = nfs_ok;
3900out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003901 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003902 if (new)
3903 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003904 if (unconf)
3905 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 return status;
3907}
3908
3909
Al Virob37ad282006-10-19 23:28:59 -07003910__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003911nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003912 struct nfsd4_compound_state *cstate,
3913 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003915 struct nfsd4_setclientid_confirm *setclientid_confirm =
3916 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003917 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003918 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3920 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003921 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003922 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003924 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003926
Jeff Laytond20c11d2014-07-30 08:27:07 -04003927 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003928 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003929 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003930 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003931 * We try hard to give out unique clientid's, so if we get an
3932 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003933 * the client may be buggy; this should never happen.
3934 *
3935 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003936 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003937 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003938 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3939 goto out;
3940 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3941 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003942 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003943 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003944 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3945 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003947 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003948 status = nfserr_stale_clientid;
3949 goto out;
3950 }
3951 status = nfs_ok;
3952 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003953 old = unconf;
3954 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003955 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003956 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003957 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3958 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003959 status = nfserr_clid_inuse;
3960 if (client_has_state(old)
3961 && !same_creds(&unconf->cl_cred,
3962 &old->cl_cred))
3963 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003964 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003965 if (status) {
3966 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003967 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003968 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003969 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003970 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003971 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003972 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003973 get_client_locked(conf);
3974 spin_unlock(&nn->client_lock);
3975 nfsd4_probe_callback(conf);
3976 spin_lock(&nn->client_lock);
3977 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003979 spin_unlock(&nn->client_lock);
3980 if (old)
3981 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 return status;
3983}
3984
J. Bruce Fields32513b42011-10-13 16:00:16 -04003985static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003987 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3988}
3989
3990/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003991static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3992 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003993{
Trond Myklebust950e0112014-06-30 11:48:31 -04003994 lockdep_assert_held(&state_lock);
3995
Elena Reshetova818a34e2017-10-20 12:53:30 +03003996 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003997 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003998 INIT_LIST_HEAD(&fp->fi_stateids);
3999 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004000 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04004001 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04004002 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004003 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004004 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004005 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4006 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004007#ifdef CONFIG_NFSD_PNFS
4008 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02004009 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004010#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04004011 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012}
4013
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004014void
NeilBrowne60d4392005-06-23 22:03:01 -07004015nfsd4_free_slabs(void)
4016{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004017 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004018 kmem_cache_destroy(openowner_slab);
4019 kmem_cache_destroy(lockowner_slab);
4020 kmem_cache_destroy(file_slab);
4021 kmem_cache_destroy(stateid_slab);
4022 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004023 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07004024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
Bryan Schumaker72083392011-11-01 15:24:59 -04004026int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027nfsd4_init_slabs(void)
4028{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004029 client_slab = kmem_cache_create("nfsd4_clients",
4030 sizeof(struct nfs4_client), 0, 0, NULL);
4031 if (client_slab == NULL)
4032 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004033 openowner_slab = kmem_cache_create("nfsd4_openowners",
4034 sizeof(struct nfs4_openowner), 0, 0, NULL);
4035 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04004036 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004037 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08004038 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004039 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004040 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07004041 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09004042 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07004043 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004044 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07004045 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004046 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07004047 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004048 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07004049 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09004050 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07004051 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004052 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004053 odstate_slab = kmem_cache_create("nfsd4_odstate",
4054 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4055 if (odstate_slab == NULL)
4056 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07004058
Sachin Bhamare8287f002015-04-27 14:50:14 +02004059out_free_deleg_slab:
4060 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004061out_free_stateid_slab:
4062 kmem_cache_destroy(stateid_slab);
4063out_free_file_slab:
4064 kmem_cache_destroy(file_slab);
4065out_free_lockowner_slab:
4066 kmem_cache_destroy(lockowner_slab);
4067out_free_openowner_slab:
4068 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004069out_free_client_slab:
4070 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004071out:
NeilBrowne60d4392005-06-23 22:03:01 -07004072 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073}
4074
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004075static void init_nfs4_replay(struct nfs4_replay *rp)
4076{
4077 rp->rp_status = nfserr_serverfault;
4078 rp->rp_buflen = 0;
4079 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04004080 mutex_init(&rp->rp_mutex);
4081}
4082
4083static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4084 struct nfs4_stateowner *so)
4085{
4086 if (!nfsd4_has_session(cstate)) {
4087 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004088 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004089 }
4090}
4091
4092void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4093{
4094 struct nfs4_stateowner *so = cstate->replay_owner;
4095
4096 if (so != NULL) {
4097 cstate->replay_owner = NULL;
4098 mutex_unlock(&so->so_replay.rp_mutex);
4099 nfs4_put_stateowner(so);
4100 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004101}
4102
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004103static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104{
4105 struct nfs4_stateowner *sop;
4106
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004107 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004108 if (!sop)
4109 return NULL;
4110
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04004111 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004112 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004113 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004114 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004116
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004117 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004118 sop->so_client = clp;
4119 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04004120 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004121 return sop;
4122}
4123
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004124static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004125{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004126 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004127
Trond Myklebustd4f04892014-07-29 21:34:36 -04004128 list_add(&oo->oo_owner.so_strhash,
4129 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004130 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
4132
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004133static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4134{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004135 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004136}
4137
Jeff Layton6b180f02014-07-29 21:34:26 -04004138static void nfs4_free_openowner(struct nfs4_stateowner *so)
4139{
4140 struct nfs4_openowner *oo = openowner(so);
4141
4142 kmem_cache_free(openowner_slab, oo);
4143}
4144
4145static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004146 .so_unhash = nfs4_unhash_openowner,
4147 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004148};
4149
Andrew Elble7fc05642015-11-05 20:42:43 -05004150static struct nfs4_ol_stateid *
4151nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4152{
4153 struct nfs4_ol_stateid *local, *ret = NULL;
4154 struct nfs4_openowner *oo = open->op_openowner;
4155
4156 lockdep_assert_held(&fp->fi_lock);
4157
4158 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4159 /* ignore lock owners */
4160 if (local->st_stateowner->so_is_open_owner == 0)
4161 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004162 if (local->st_stateowner != &oo->oo_owner)
4163 continue;
4164 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004165 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004166 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05004167 break;
4168 }
4169 }
4170 return ret;
4171}
4172
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004173static __be32
4174nfsd4_verify_open_stid(struct nfs4_stid *s)
4175{
4176 __be32 ret = nfs_ok;
4177
4178 switch (s->sc_type) {
4179 default:
4180 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004181 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004182 case NFS4_CLOSED_STID:
4183 case NFS4_CLOSED_DELEG_STID:
4184 ret = nfserr_bad_stateid;
4185 break;
4186 case NFS4_REVOKED_DELEG_STID:
4187 ret = nfserr_deleg_revoked;
4188 }
4189 return ret;
4190}
4191
4192/* Lock the stateid st_mutex, and deal with races with CLOSE */
4193static __be32
4194nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4195{
4196 __be32 ret;
4197
Andrew Elble4f34bd02017-11-08 17:29:51 -05004198 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004199 ret = nfsd4_verify_open_stid(&stp->st_stid);
4200 if (ret != nfs_ok)
4201 mutex_unlock(&stp->st_mutex);
4202 return ret;
4203}
4204
4205static struct nfs4_ol_stateid *
4206nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4207{
4208 struct nfs4_ol_stateid *stp;
4209 for (;;) {
4210 spin_lock(&fp->fi_lock);
4211 stp = nfsd4_find_existing_open(fp, open);
4212 spin_unlock(&fp->fi_lock);
4213 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4214 break;
4215 nfs4_put_stid(&stp->st_stid);
4216 }
4217 return stp;
4218}
4219
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004220static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004221alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004222 struct nfsd4_compound_state *cstate)
4223{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004224 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004225 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004227 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4228 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004230 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004231 oo->oo_owner.so_is_open_owner = 1;
4232 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004233 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004234 if (nfsd4_has_session(cstate))
4235 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004236 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004237 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004238 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004239 spin_lock(&clp->cl_lock);
4240 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004241 if (ret == NULL) {
4242 hash_openowner(oo, clp, strhashval);
4243 ret = oo;
4244 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004245 nfs4_free_stateowner(&oo->oo_owner);
4246
Trond Myklebustd4f04892014-07-29 21:34:36 -04004247 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004248 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
4250
Andrew Elble7fc05642015-11-05 20:42:43 -05004251static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004252init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004253{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Andrew Elble7fc05642015-11-05 20:42:43 -05004255 struct nfs4_openowner *oo = open->op_openowner;
4256 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004257 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004258
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004259 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004260 /* We are moving these outside of the spinlocks to avoid the warnings */
4261 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004262 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004263
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004264retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004265 spin_lock(&oo->oo_owner.so_client->cl_lock);
4266 spin_lock(&fp->fi_lock);
4267
4268 retstp = nfsd4_find_existing_open(fp, open);
4269 if (retstp)
4270 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004271
4272 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004273 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004274 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004275 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004276 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004277 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004278 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 stp->st_access_bmap = 0;
4280 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004281 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004282 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004283 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004284
4285out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004286 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004287 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004288 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004289 /* Handle races with CLOSE */
4290 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4291 nfs4_put_stid(&retstp->st_stid);
4292 goto retry;
4293 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004294 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004295 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004296 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004297 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004298 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299}
4300
Jeff Laytond3134b12014-07-29 21:34:32 -04004301/*
4302 * In the 4.0 case we need to keep the owners around a little while to handle
4303 * CLOSE replay. We still do need to release any file access that is held by
4304 * them before returning however.
4305 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004307move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
Jeff Layton217526e2014-07-30 08:27:11 -04004309 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004310 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4311 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4312 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004313
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004314 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Jeff Laytonb401be222014-07-29 21:34:33 -04004316 /*
4317 * We know that we hold one reference via nfsd4_close, and another
4318 * "persistent" reference for the client. If the refcount is higher
4319 * than 2, then there are still calls in progress that are using this
4320 * stateid. We can't put the sc_file reference until they are finished.
4321 * Wait for the refcount to drop to 2. Since it has been unhashed,
4322 * there should be no danger of the refcount going back up again at
4323 * this point.
4324 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004325 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004326
Jeff Laytond3134b12014-07-29 21:34:32 -04004327 release_all_access(s);
4328 if (s->st_stid.sc_file) {
4329 put_nfs4_file(s->st_stid.sc_file);
4330 s->st_stid.sc_file = NULL;
4331 }
Jeff Layton217526e2014-07-30 08:27:11 -04004332
4333 spin_lock(&nn->client_lock);
4334 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004335 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004336 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004337 oo->oo_time = ktime_get_boottime_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004338 spin_unlock(&nn->client_lock);
4339 if (last)
4340 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341}
4342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343/* search file_hashtbl[] for file */
4344static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04004345find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 struct nfs4_file *fp;
4348
Madhuparna Bhowmik36a80492020-02-13 20:33:31 +05304349 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4350 lockdep_is_held(&state_lock)) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02004351 if (fh_match(&fp->fi_fhandle, fh)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004352 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004353 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 }
4356 return NULL;
4357}
4358
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02004359struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04004360find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004361{
4362 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004363 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004364
Jeff Layton5b095e92014-10-23 08:01:02 -04004365 rcu_read_lock();
4366 fp = find_file_locked(fh, hashval);
4367 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004368 return fp;
4369}
4370
4371static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004372find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004373{
4374 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004375 unsigned int hashval = file_hashval(fh);
4376
4377 rcu_read_lock();
4378 fp = find_file_locked(fh, hashval);
4379 rcu_read_unlock();
4380 if (fp)
4381 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004382
4383 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04004384 fp = find_file_locked(fh, hashval);
4385 if (likely(fp == NULL)) {
4386 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04004387 fp = new;
4388 }
4389 spin_unlock(&state_lock);
4390
4391 return fp;
4392}
4393
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004394/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 * Called to check deny when READ with all zero stateid or
4396 * WRITE with all zero or all one stateid
4397 */
Al Virob37ad282006-10-19 23:28:59 -07004398static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4400{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004402 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Trond Myklebustca943212014-07-23 16:17:39 -04004404 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07004405 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004406 return ret;
4407 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004408 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004409 if (fp->fi_share_deny & deny_type)
4410 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004411 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004412 put_nfs4_file(fp);
4413 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414}
4415
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004416static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004418 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004419 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4420 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004421
Trond Myklebust11b91642014-07-29 21:34:08 -04004422 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004423
Jeff Layton02e12152014-07-16 10:31:57 -04004424 /*
4425 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004426 * already holding inode->i_lock.
4427 *
Jeff Laytondff13992014-07-08 14:02:49 -04004428 * If the dl_time != 0, then we know that it has already been
4429 * queued for a lease break. Don't queue it again.
4430 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004431 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004432 if (dp->dl_time == 0) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004433 dp->dl_time = ktime_get_boottime_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004434 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004435 }
Jeff Layton02e12152014-07-16 10:31:57 -04004436 spin_unlock(&state_lock);
4437}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004439static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4440 struct rpc_task *task)
4441{
4442 struct nfs4_delegation *dp = cb_to_delegation(cb);
4443
Andrew Elblea4579742015-08-31 12:06:41 -04004444 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
4445 return 1;
4446
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004447 switch (task->tk_status) {
4448 case 0:
4449 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004450 case -NFS4ERR_DELAY:
4451 rpc_delay(task, 2 * HZ);
4452 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004453 case -EBADHANDLE:
4454 case -NFS4ERR_BAD_STATEID:
4455 /*
4456 * Race: client probably got cb_recall before open reply
4457 * granting delegation.
4458 */
4459 if (dp->dl_retries--) {
4460 rpc_delay(task, 2 * HZ);
4461 return 0;
4462 }
4463 /*FALLTHRU*/
4464 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004465 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004466 }
4467}
4468
4469static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4470{
4471 struct nfs4_delegation *dp = cb_to_delegation(cb);
4472
4473 nfs4_put_stid(&dp->dl_stid);
4474}
4475
Julia Lawallc4cb8972015-11-21 22:57:39 +01004476static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004477 .prepare = nfsd4_cb_recall_prepare,
4478 .done = nfsd4_cb_recall_done,
4479 .release = nfsd4_cb_recall_release,
4480};
4481
Jeff Layton02e12152014-07-16 10:31:57 -04004482static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4483{
4484 /*
4485 * We're assuming the state code never drops its reference
4486 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004487 * callback (and since the lease code is serialized by the
4488 * i_lock) we know the server hasn't removed the lease yet, and
4489 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004490 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004491 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004492 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004493}
4494
Jeff Layton1c8c6012013-06-21 08:58:15 -04004495/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004496static bool
4497nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004498{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004499 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004500 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4501 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004502
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04004503 trace_nfsd_deleg_break(&dp->dl_stid.sc_stateid);
4504
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004505 /*
4506 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004507 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004508 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004509 */
4510 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
Jeff Layton02e12152014-07-16 10:31:57 -04004512 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004513 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004514 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004515 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004516 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517}
4518
Jeff Laytonc45198e2014-09-01 07:12:07 -04004519static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004520nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4521 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522{
4523 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004524 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 else
4526 return -EAGAIN;
4527}
4528
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004529static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004530 .lm_break = nfsd_break_deleg_cb,
4531 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532};
4533
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004534static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4535{
4536 if (nfsd4_has_session(cstate))
4537 return nfs_ok;
4538 if (seqid == so->so_seqid - 1)
4539 return nfserr_replay_me;
4540 if (seqid == so->so_seqid)
4541 return nfs_ok;
4542 return nfserr_bad_seqid;
4543}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Jeff Layton4b24ca72014-06-30 11:48:44 -04004545static __be32 lookup_clientid(clientid_t *clid,
4546 struct nfsd4_compound_state *cstate,
Olga Kornievskaiab7342202019-09-06 15:17:21 -04004547 struct nfsd_net *nn,
4548 bool sessions)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004549{
4550 struct nfs4_client *found;
4551
4552 if (cstate->clp) {
4553 found = cstate->clp;
4554 if (!same_clid(&found->cl_clientid, clid))
4555 return nfserr_stale_clientid;
4556 return nfs_ok;
4557 }
4558
4559 if (STALE_CLIENTID(clid, nn))
4560 return nfserr_stale_clientid;
4561
4562 /*
4563 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4564 * cached already then we know this is for is for v4.0 and "sessions"
4565 * will be false.
4566 */
4567 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004568 spin_lock(&nn->client_lock);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04004569 found = find_confirmed_client(clid, sessions, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004570 if (!found) {
4571 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004572 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004573 }
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04004574 atomic_inc(&found->cl_rpc_users);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004575 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004576
4577 /* Cache the nfs4_client in cstate! */
4578 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004579 return nfs_ok;
4580}
4581
Al Virob37ad282006-10-19 23:28:59 -07004582__be32
Andy Adamson66689582009-04-03 08:28:45 +03004583nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004584 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 clientid_t *clientid = &open->op_clientid;
4587 struct nfs4_client *clp = NULL;
4588 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004589 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004590 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004592 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004594 /*
4595 * In case we need it later, after we've already created the
4596 * file and don't want to risk a further failure:
4597 */
4598 open->op_file = nfsd4_alloc_file();
4599 if (open->op_file == NULL)
4600 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Olga Kornievskaiab7342202019-09-06 15:17:21 -04004602 status = lookup_clientid(clientid, cstate, nn, false);
Trond Myklebust2d91e892014-06-30 11:48:46 -04004603 if (status)
4604 return status;
4605 clp = cstate->clp;
4606
Trond Myklebustd4f04892014-07-29 21:34:36 -04004607 strhashval = ownerstr_hashval(&open->op_owner);
4608 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004609 open->op_openowner = oo;
4610 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004611 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004613 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004614 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004615 release_openowner(oo);
4616 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004617 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004618 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004619 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4620 if (status)
4621 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004622 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004623new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004624 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004625 if (oo == NULL)
4626 return nfserr_jukebox;
4627 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004628alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004629 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004630 if (!open->op_stp)
4631 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004632
4633 if (nfsd4_has_session(cstate) &&
4634 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4635 open->op_odstate = alloc_clnt_odstate(clp);
4636 if (!open->op_odstate)
4637 return nfserr_jukebox;
4638 }
4639
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004640 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
Al Virob37ad282006-10-19 23:28:59 -07004643static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004644nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4645{
4646 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4647 return nfserr_openmode;
4648 else
4649 return nfs_ok;
4650}
4651
Daniel Mackc47d8322011-05-16 16:38:14 +02004652static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004653{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004654 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4655}
4656
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004657static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004658{
4659 struct nfs4_stid *ret;
4660
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004661 ret = find_stateid_by_type(cl, s,
4662 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004663 if (!ret)
4664 return NULL;
4665 return delegstateid(ret);
4666}
4667
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004668static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4669{
4670 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4671 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4672}
4673
Al Virob37ad282006-10-19 23:28:59 -07004674static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004675nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004676 struct nfs4_delegation **dp)
4677{
4678 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004679 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004680 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004681
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004682 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4683 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004684 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004685 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4686 nfs4_put_stid(&deleg->dl_stid);
4687 if (cl->cl_minorversion)
4688 status = nfserr_deleg_revoked;
4689 goto out;
4690 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004691 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004692 status = nfs4_check_delegmode(deleg, flags);
4693 if (status) {
4694 nfs4_put_stid(&deleg->dl_stid);
4695 goto out;
4696 }
4697 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004698out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004699 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004700 return nfs_ok;
4701 if (status)
4702 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004703 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004704 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004705}
4706
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004707static inline int nfs4_access_to_access(u32 nfs4_access)
4708{
4709 int flags = 0;
4710
4711 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4712 flags |= NFSD_MAY_READ;
4713 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4714 flags |= NFSD_MAY_WRITE;
4715 return flags;
4716}
4717
Al Virob37ad282006-10-19 23:28:59 -07004718static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4720 struct nfsd4_open *open)
4721{
4722 struct iattr iattr = {
4723 .ia_valid = ATTR_SIZE,
4724 .ia_size = 0,
4725 };
4726 if (!open->op_truncate)
4727 return 0;
4728 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004729 return nfserr_inval;
Arnd Bergmann2a1aa482019-11-03 17:50:18 +01004730 return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731}
4732
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004733static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004734 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4735 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004736{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004737 struct nfsd_file *nf = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004738 __be32 status;
4739 int oflag = nfs4_access_to_omode(open->op_share_access);
4740 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004741 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004742
Trond Myklebustde186432014-07-10 14:07:26 -04004743 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004744
4745 /*
4746 * Are we trying to set a deny mode that would conflict with
4747 * current access?
4748 */
4749 status = nfs4_file_check_deny(fp, open->op_share_deny);
4750 if (status != nfs_ok) {
4751 spin_unlock(&fp->fi_lock);
4752 goto out;
4753 }
4754
4755 /* set access to the file */
4756 status = nfs4_file_get_access(fp, open->op_share_access);
4757 if (status != nfs_ok) {
4758 spin_unlock(&fp->fi_lock);
4759 goto out;
4760 }
4761
4762 /* Set access bits in stateid */
4763 old_access_bmap = stp->st_access_bmap;
4764 set_access(open->op_share_access, stp);
4765
4766 /* Set new deny mask */
4767 old_deny_bmap = stp->st_deny_bmap;
4768 set_deny(open->op_share_deny, stp);
4769 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4770
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004771 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004772 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004773 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004774 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004775 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004776 spin_lock(&fp->fi_lock);
4777 if (!fp->fi_fds[oflag]) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004778 fp->fi_fds[oflag] = nf;
4779 nf = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -04004780 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004781 }
Trond Myklebustde186432014-07-10 14:07:26 -04004782 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004783 if (nf)
4784 nfsd_file_put(nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004785
4786 status = nfsd4_truncate(rqstp, cur_fh, open);
4787 if (status)
4788 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004789out:
4790 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004791out_put_access:
4792 stp->st_access_bmap = old_access_bmap;
4793 nfs4_file_put_access(fp, open->op_share_access);
4794 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4795 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004796}
4797
Al Virob37ad282006-10-19 23:28:59 -07004798static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004799nfs4_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 -07004800{
Al Virob37ad282006-10-19 23:28:59 -07004801 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004802 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004804 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004805 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004806
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004807 /* test and set deny mode */
4808 spin_lock(&fp->fi_lock);
4809 status = nfs4_file_check_deny(fp, open->op_share_deny);
4810 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004811 set_deny(open->op_share_deny, stp);
4812 fp->fi_share_deny |=
4813 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4814 }
4815 spin_unlock(&fp->fi_lock);
4816
4817 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004820 status = nfsd4_truncate(rqstp, cur_fh, open);
4821 if (status != nfs_ok)
4822 reset_union_bmap_deny(old_deny_bmap, stp);
4823 return status;
4824}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004826/* Should we give out recallable state?: */
4827static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4828{
4829 if (clp->cl_cb_state == NFSD4_CB_UP)
4830 return true;
4831 /*
4832 * In the sessions case, since we don't have to establish a
4833 * separate connection for callbacks, we assume it's OK
4834 * until we hear otherwise:
4835 */
4836 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4837}
4838
J. Bruce Fields653e5142018-02-15 22:08:45 -05004839static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4840 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004841{
4842 struct file_lock *fl;
4843
4844 fl = locks_alloc_lock();
4845 if (!fl)
4846 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004847 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004848 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004849 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4850 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004851 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004852 fl->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004853 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004854 return fl;
4855}
4856
Jeff Layton0b266932014-07-25 07:34:25 -04004857static struct nfs4_delegation *
4858nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004859 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004860{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004861 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004862 struct nfs4_delegation *dp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004863 struct nfsd_file *nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004864 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004865
J. Bruce Fields353601e2018-02-16 14:29:42 -05004866 /*
4867 * The fi_had_conflict and nfs_get_existing_delegation checks
4868 * here are just optimizations; we'll need to recheck them at
4869 * the end:
4870 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004871 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004872 return ERR_PTR(-EAGAIN);
4873
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004874 nf = find_readable_file(fp);
4875 if (!nf) {
J. Bruce Fields353601e2018-02-16 14:29:42 -05004876 /* We should always have a readable file here */
4877 WARN_ON_ONCE(1);
4878 return ERR_PTR(-EBADF);
4879 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004880 spin_lock(&state_lock);
4881 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004882 if (nfs4_delegation_exists(clp, fp))
4883 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004884 else if (!fp->fi_deleg_file) {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004885 fp->fi_deleg_file = nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004886 /* increment early to prevent fi_deleg_file from being
4887 * cleared */
4888 fp->fi_delegees = 1;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004889 nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004890 } else
4891 fp->fi_delegees++;
4892 spin_unlock(&fp->fi_lock);
4893 spin_unlock(&state_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004894 if (nf)
4895 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004896 if (status)
4897 return ERR_PTR(status);
4898
4899 status = -ENOMEM;
4900 dp = alloc_init_deleg(clp, fp, fh, odstate);
4901 if (!dp)
4902 goto out_delegees;
4903
4904 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4905 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04004906 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004907
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004908 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004909 if (fl)
4910 locks_free_lock(fl);
4911 if (status)
4912 goto out_clnt_odstate;
4913
4914 spin_lock(&state_lock);
4915 spin_lock(&fp->fi_lock);
4916 if (fp->fi_had_conflict)
4917 status = -EAGAIN;
4918 else
4919 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04004920 spin_unlock(&fp->fi_lock);
4921 spin_unlock(&state_lock);
4922
4923 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04004924 goto out_unlock;
4925
Jeff Layton0b266932014-07-25 07:34:25 -04004926 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04004927out_unlock:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004928 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004929out_clnt_odstate:
4930 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004931 nfs4_put_stid(&dp->dl_stid);
4932out_delegees:
4933 put_deleg_file(fp);
4934 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004935}
4936
Benny Halevy4aa89132012-02-21 14:16:44 -08004937static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4938{
4939 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4940 if (status == -EAGAIN)
4941 open->op_why_no_deleg = WND4_CONTENTION;
4942 else {
4943 open->op_why_no_deleg = WND4_RESOURCE;
4944 switch (open->op_deleg_want) {
4945 case NFS4_SHARE_WANT_READ_DELEG:
4946 case NFS4_SHARE_WANT_WRITE_DELEG:
4947 case NFS4_SHARE_WANT_ANY_DELEG:
4948 break;
4949 case NFS4_SHARE_WANT_CANCEL:
4950 open->op_why_no_deleg = WND4_CANCELLED;
4951 break;
4952 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004953 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004954 }
4955 }
4956}
4957
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958/*
4959 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004960 *
4961 * Note we don't support write delegations, and won't until the vfs has
4962 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 */
4964static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004965nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4966 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967{
4968 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004969 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4970 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004971 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004972 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004974 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004975 open->op_recall = 0;
4976 switch (open->op_claim_type) {
4977 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004978 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004979 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004980 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4981 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004982 break;
4983 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004984 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004985 /*
4986 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004987 * had the chance to reclaim theirs, *and* until
4988 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004989 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004990 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004991 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004992 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004993 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004994 /*
4995 * Also, if the file was opened for write or
4996 * create, there's a good chance the client's
4997 * about to write to it, resulting in an
4998 * immediate recall (since we don't support
4999 * write delegations):
5000 */
NeilBrown7b190fe2005-06-23 22:03:23 -07005001 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04005002 goto out_no_deleg;
5003 if (open->op_create == NFS4_OPEN_CREATE)
5004 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005005 break;
5006 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005007 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005008 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005009 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04005010 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005011 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005013 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005015 trace_nfsd_deleg_open(&dp->dl_stid.sc_stateid);
J. Bruce Fields99c41512013-05-21 16:21:25 -04005016 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04005017 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005018 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005019out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005020 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5021 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005022 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04005023 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005024 open->op_recall = 1;
5025 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04005026
5027 /* 4.1 client asking for a delegation? */
5028 if (open->op_deleg_want)
5029 nfsd4_open_deleg_none_ext(open, status);
5030 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031}
5032
Benny Halevye27f49c2012-02-21 14:16:54 -08005033static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5034 struct nfs4_delegation *dp)
5035{
5036 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5037 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5038 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5039 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5040 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5041 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5042 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5043 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5044 }
5045 /* Otherwise the client must be confused wanting a delegation
5046 * it already has, therefore we don't return
5047 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5048 */
5049}
5050
Al Virob37ad282006-10-19 23:28:59 -07005051__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5053{
Andy Adamson66689582009-04-03 08:28:45 +03005054 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005055 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005057 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07005058 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005059 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005060 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 /*
5063 * Lookup file; if found, lookup stateid and check open request,
5064 * and check for delegations in the process of being recalled.
5065 * If not found, create the nfs4_file struct
5066 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005067 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04005068 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04005069 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07005070 if (status)
5071 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005072 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04005074 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07005075 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04005076 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07005077 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 }
5079
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005080 if (!stp) {
5081 stp = init_open_stateid(fp, open);
5082 if (!open->op_stp)
5083 new_stp = true;
5084 }
5085
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 /*
5087 * OPEN the file, or upgrade an existing OPEN.
5088 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005089 *
5090 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005092 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005094 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005095 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005096 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005100 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5101 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005102 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005103 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005104 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005105 goto out;
5106 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005107
5108 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5109 open->op_odstate);
5110 if (stp->st_clnt_odstate == open->op_odstate)
5111 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005113
Jeff Layton9767feb2015-10-01 09:05:50 -04005114 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005115 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
Benny Halevyd24433c2012-02-16 20:57:17 +02005117 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005118 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5119 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5120 open->op_why_no_deleg = WND4_NOT_WANTED;
5121 goto nodeleg;
5122 }
5123 }
5124
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 /*
5126 * Attempt to hand out a delegation. No error return, because the
5127 * OPEN succeeds even if we fail.
5128 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005129 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005130nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 status = nfs_ok;
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005132 trace_nfsd_deleg_none(&stp->st_stid.sc_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005134 /* 4.1 client trying to upgrade/downgrade delegation? */
5135 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005136 open->op_deleg_want)
5137 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005138
NeilBrown13cd2182005-06-23 22:03:10 -07005139 if (fp)
5140 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005141 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005142 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 /*
5144 * To finish the open response, we just need to set the rflags.
5145 */
5146 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005147 if (nfsd4_has_session(&resp->cstate))
5148 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5149 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005151
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005152 if (dp)
5153 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005154 if (stp)
5155 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156
5157 return status;
5158}
5159
Jeff Layton58fb12e2014-07-29 21:34:27 -04005160void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005161 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005162{
5163 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005164 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005165
Jeff Laytond3134b12014-07-29 21:34:32 -04005166 nfsd4_cstate_assign_replay(cstate, so);
5167 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005168 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005169 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005170 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005171 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005172 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005173 if (open->op_odstate)
5174 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005175}
5176
Al Virob37ad282006-10-19 23:28:59 -07005177__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005178nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005179 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005181 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005183 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005184 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005186 trace_nfsd_clid_renew(clid);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005187 status = lookup_clientid(clid, cstate, nn, false);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005188 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005190 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07005192 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005193 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 goto out;
5195 status = nfs_ok;
5196out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 return status;
5198}
5199
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005200void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005201nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005202{
Jeff Layton33dcc482012-04-10 11:08:48 -04005203 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005204 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005205 return;
5206
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005207 trace_nfsd_grace_complete(nn);
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005208 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005209 /*
5210 * If the server goes down again right now, an NFSv4
5211 * client will still be allowed to reclaim after it comes back up,
5212 * even if it hasn't yet had a chance to reclaim state this time.
5213 *
5214 */
Jeff Layton919b8042014-09-12 16:40:20 -04005215 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005216 /*
5217 * At this point, NFSv4 clients can still reclaim. But if the
5218 * server crashes, any that have not yet reclaimed will be out
5219 * of luck on the next boot.
5220 *
5221 * (NFSv4.1+ clients are considered to have reclaimed once they
5222 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5223 * have reclaimed after their first OPEN.)
5224 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005225 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005226 /*
5227 * At this point, and once lockd and/or any other containers
5228 * exit their grace period, further reclaims will fail and
5229 * regular locking can resume.
5230 */
NeilBrowna76b4312005-06-23 22:04:01 -07005231}
5232
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005233/*
5234 * If we've waited a lease period but there are still clients trying to
5235 * reclaim, wait a little longer to give them a chance to finish.
5236 */
5237static bool clients_still_reclaiming(struct nfsd_net *nn)
5238{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005239 time64_t double_grace_period_end = nn->boot_time +
5240 2 * nn->nfsd4_lease;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005241
Scott Mayhew362063a2019-03-26 18:06:28 -04005242 if (nn->track_reclaim_completes &&
5243 atomic_read(&nn->nr_reclaim_complete) ==
5244 nn->reclaim_str_hashtbl_size)
5245 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005246 if (!nn->somebody_reclaimed)
5247 return false;
5248 nn->somebody_reclaimed = false;
5249 /*
5250 * If we've given them *two* lease times to reclaim, and they're
5251 * still not done, give up:
5252 */
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005253 if (ktime_get_boottime_seconds() > double_grace_period_end)
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005254 return false;
5255 return true;
5256}
5257
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005258static time64_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005259nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260{
5261 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005262 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005264 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005265 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 struct list_head *pos, *next, reaplist;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005267 time64_t cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease;
5268 time64_t t, new_timeo = nn->nfsd4_lease;
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005269 struct nfs4_cpntf_state *cps;
5270 copy_stateid_t *cps_t;
5271 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005273 if (clients_still_reclaiming(nn)) {
5274 new_timeo = 0;
5275 goto out;
5276 }
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005277 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005278 INIT_LIST_HEAD(&reaplist);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005279
5280 spin_lock(&nn->s2s_cp_lock);
5281 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5282 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5283 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005284 cps->cpntf_time > cutoff)
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005285 _free_cpntf_state_locked(nn, cps);
5286 }
5287 spin_unlock(&nn->s2s_cp_lock);
5288
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005289 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005290 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 clp = list_entry(pos, struct nfs4_client, cl_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005292 if (clp->cl_time > cutoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04005294 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 break;
5296 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04005297 if (mark_client_expired_locked(clp)) {
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005298 trace_nfsd_clid_expired(&clp->cl_clientid);
Benny Halevyd7682982010-05-12 00:13:54 +03005299 continue;
5300 }
Trond Myklebust4864af92014-07-30 08:27:03 -04005301 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005302 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005303 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005304 list_for_each_safe(pos, next, &reaplist) {
5305 clp = list_entry(pos, struct nfs4_client, cl_lru);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005306 trace_nfsd_clid_purged(&clp->cl_clientid);
Trond Myklebust4864af92014-07-30 08:27:03 -04005307 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 expire_client(clp);
5309 }
Benny Halevycdc975052014-05-30 09:09:30 -04005310 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005311 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005313 if (dp->dl_time > cutoff) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005314 t = dp->dl_time - cutoff;
5315 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 break;
5317 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005318 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005319 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 }
Benny Halevycdc975052014-05-30 09:09:30 -04005321 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005322 while (!list_empty(&reaplist)) {
5323 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5324 dl_recall_lru);
5325 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005326 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 }
Jeff Layton217526e2014-07-30 08:27:11 -04005328
5329 spin_lock(&nn->client_lock);
5330 while (!list_empty(&nn->close_lru)) {
5331 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5332 oo_close_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005333 if (oo->oo_time > cutoff) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005334 t = oo->oo_time - cutoff;
5335 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 break;
5337 }
Jeff Layton217526e2014-07-30 08:27:11 -04005338 list_del_init(&oo->oo_close_lru);
5339 stp = oo->oo_last_closed_stid;
5340 oo->oo_last_closed_stid = NULL;
5341 spin_unlock(&nn->client_lock);
5342 nfs4_put_stid(&stp->st_stid);
5343 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 }
Jeff Layton217526e2014-07-30 08:27:11 -04005345 spin_unlock(&nn->client_lock);
5346
Jeff Layton7919d0a2016-09-16 16:28:25 -04005347 /*
5348 * It's possible for a client to try and acquire an already held lock
5349 * that is being held for a long time, and then lose interest in it.
5350 * So, we clean out any un-revisited request after a lease period
5351 * under the assumption that the client is no longer interested.
5352 *
5353 * RFC5661, sec. 9.6 states that the client must not rely on getting
5354 * notifications and must continue to poll for locks, even when the
5355 * server supports them. Thus this shouldn't lead to clients blocking
5356 * indefinitely once the lock does become free.
5357 */
5358 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005359 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005360 while (!list_empty(&nn->blocked_locks_lru)) {
5361 nbl = list_first_entry(&nn->blocked_locks_lru,
5362 struct nfsd4_blocked_lock, nbl_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005363 if (nbl->nbl_time > cutoff) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04005364 t = nbl->nbl_time - cutoff;
5365 new_timeo = min(new_timeo, t);
5366 break;
5367 }
5368 list_move(&nbl->nbl_lru, &reaplist);
5369 list_del_init(&nbl->nbl_list);
5370 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005371 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005372
5373 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005374 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005375 struct nfsd4_blocked_lock, nbl_lru);
5376 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005377 free_blocked_lock(nbl);
5378 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005379out:
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005380 new_timeo = max_t(time64_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04005381 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382}
5383
Harvey Harrisona254b242008-02-20 12:49:00 -08005384static struct workqueue_struct *laundry_wq;
5385static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005386
5387static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005388laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005390 time64_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005391 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005392 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5393 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005395 t = nfs4_laundromat(nn);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005396 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397}
5398
Jeff Layton8fcd4612015-07-30 06:57:46 -04005399static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005400{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005401 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005402 return nfserr_bad_stateid;
5403 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404}
5405
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005407access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005409 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5410 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5411 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412}
5413
5414static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005415access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005417 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5418 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419}
5420
5421static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005422__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
Al Virob37ad282006-10-19 23:28:59 -07005424 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
J. Bruce Fields02921912010-07-29 15:16:59 -04005426 /* For lock stateid's, we test the parent open, not the lock: */
5427 if (stp->st_openstp)
5428 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005429 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005431 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 goto out;
5433 status = nfs_ok;
5434out:
5435 return status;
5436}
5437
Al Virob37ad282006-10-19 23:28:59 -07005438static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005439check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005441 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005443 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005444 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 * conflicting state; so we must wait out the grace period. */
5446 return nfserr_grace;
5447 } else if (flags & WR_STATE)
5448 return nfs4_share_conflict(current_fh,
5449 NFS4_SHARE_DENY_WRITE);
5450 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5451 return nfs4_share_conflict(current_fh,
5452 NFS4_SHARE_DENY_READ);
5453}
5454
5455/*
5456 * Allow READ/WRITE during grace period on recovered state only for files
5457 * that are not able to provide mandatory locking.
5458 */
5459static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005460grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005462 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463}
5464
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005465static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005466{
Andy Adamson66689582009-04-03 08:28:45 +03005467 /*
5468 * When sessions are used the stateid generation number is ignored
5469 * when it is zero.
5470 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005471 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005472 return nfs_ok;
5473
5474 if (in->si_generation == ref->si_generation)
5475 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005476
J. Bruce Fields0836f582008-01-26 19:08:12 -05005477 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005478 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005479 return nfserr_bad_stateid;
5480 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005481 * However, we could see a stateid from the past, even from a
5482 * non-buggy client. For example, if the client sends a lock
5483 * while some IO is outstanding, the lock may bump si_generation
5484 * while the IO is still in flight. The client could avoid that
5485 * situation by waiting for responses on all the IO requests,
5486 * but better performance may result in retrying IO that
5487 * receives an old_stateid error if requests are rarely
5488 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005489 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005490 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005491}
5492
Trond Myklebust03da3162017-11-03 08:00:16 -04005493static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5494{
5495 __be32 ret;
5496
5497 spin_lock(&s->sc_lock);
5498 ret = nfsd4_verify_open_stid(s);
5499 if (ret == nfs_ok)
5500 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5501 spin_unlock(&s->sc_lock);
5502 return ret;
5503}
5504
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005505static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5506{
5507 if (ols->st_stateowner->so_is_open_owner &&
5508 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5509 return nfserr_bad_stateid;
5510 return nfs_ok;
5511}
5512
Chuck Lever7df302f2012-05-29 13:56:37 -04005513static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005514{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005515 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005516 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005517
Andrew Elbleae254dac2017-11-09 13:41:10 -05005518 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5519 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005520 return status;
J. Bruce Fields663e36f2020-03-19 10:18:49 -04005521 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005522 return status;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005523 spin_lock(&cl->cl_lock);
5524 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005525 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005526 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005527 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005528 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005529 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005530 switch (s->sc_type) {
5531 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005532 status = nfs_ok;
5533 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005534 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005535 status = nfserr_deleg_revoked;
5536 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005537 case NFS4_OPEN_STID:
5538 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005539 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005540 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005541 default:
5542 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005543 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04005544 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005545 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005546 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005547 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005548out_unlock:
5549 spin_unlock(&cl->cl_lock);
5550 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005551}
5552
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005553__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005554nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5555 stateid_t *stateid, unsigned char typemask,
5556 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005557{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005558 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005559 bool return_revoked = false;
5560
5561 /*
5562 * only return revoked delegations if explicitly asked.
5563 * otherwise we report revoked or bad_stateid status.
5564 */
5565 if (typemask & NFS4_REVOKED_DELEG_STID)
5566 return_revoked = true;
5567 else if (typemask & NFS4_DELEG_STID)
5568 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005569
Andrew Elbleae254dac2017-11-09 13:41:10 -05005570 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5571 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005572 return nfserr_bad_stateid;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005573 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn,
5574 false);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005575 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005576 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005577 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005578 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005579 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005580 if (status)
5581 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005582 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005583 if (!*s)
5584 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005585 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5586 nfs4_put_stid(*s);
5587 if (cstate->minorversion)
5588 return nfserr_deleg_revoked;
5589 return nfserr_bad_stateid;
5590 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005591 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005592}
5593
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005594static struct nfsd_file *
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005595nfs4_find_file(struct nfs4_stid *s, int flags)
5596{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005597 if (!s)
5598 return NULL;
5599
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005600 switch (s->sc_type) {
5601 case NFS4_DELEG_STID:
5602 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5603 return NULL;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005604 return nfsd_file_get(s->sc_file->fi_deleg_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005605 case NFS4_OPEN_STID:
5606 case NFS4_LOCK_STID:
5607 if (flags & RD_STATE)
5608 return find_readable_file(s->sc_file);
5609 else
5610 return find_writeable_file(s->sc_file);
5611 break;
5612 }
5613
5614 return NULL;
5615}
5616
5617static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005618nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005619{
5620 __be32 status;
5621
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005622 status = nfsd4_check_openowner_confirmed(ols);
5623 if (status)
5624 return status;
5625 return nfs4_check_openmode(ols, flags);
5626}
5627
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005628static __be32
5629nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
Jeff Layton5c4583b2019-08-18 14:18:54 -04005630 struct nfsd_file **nfp, int flags)
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005631{
5632 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005633 struct nfsd_file *nf;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005634 __be32 status;
5635
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005636 nf = nfs4_find_file(s, flags);
5637 if (nf) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005638 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5639 acc | NFSD_MAY_OWNER_OVERRIDE);
Jeff Layton5c4583b2019-08-18 14:18:54 -04005640 if (status) {
5641 nfsd_file_put(nf);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005642 goto out;
Jeff Layton5c4583b2019-08-18 14:18:54 -04005643 }
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005644 } else {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005645 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005646 if (status)
5647 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005648 }
Jeff Layton5c4583b2019-08-18 14:18:54 -04005649 *nfp = nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005650out:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005651 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005652}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005653static void
5654_free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5655{
5656 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5657 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5658 return;
5659 list_del(&cps->cp_list);
5660 idr_remove(&nn->s2s_cp_stateids,
5661 cps->cp_stateid.stid.si_opaque.so_id);
5662 kfree(cps);
5663}
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005664/*
5665 * A READ from an inter server to server COPY will have a
5666 * copy stateid. Look up the copy notify stateid from the
5667 * idr structure and take a reference on it.
5668 */
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005669__be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5670 struct nfs4_client *clp,
5671 struct nfs4_cpntf_state **cps)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005672{
5673 copy_stateid_t *cps_t;
5674 struct nfs4_cpntf_state *state = NULL;
5675
5676 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5677 return nfserr_bad_stateid;
5678 spin_lock(&nn->s2s_cp_lock);
5679 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5680 if (cps_t) {
5681 state = container_of(cps_t, struct nfs4_cpntf_state,
5682 cp_stateid);
Dan Carpenter5277a792019-12-04 10:59:36 +03005683 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5684 state = NULL;
5685 goto unlock;
5686 }
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005687 if (!clp)
5688 refcount_inc(&state->cp_stateid.sc_count);
5689 else
5690 _free_cpntf_state_locked(nn, state);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005691 }
Dan Carpenter5277a792019-12-04 10:59:36 +03005692unlock:
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005693 spin_unlock(&nn->s2s_cp_lock);
5694 if (!state)
5695 return nfserr_bad_stateid;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005696 if (!clp && state)
5697 *cps = state;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005698 return 0;
5699}
5700
5701static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5702 struct nfs4_stid **stid)
5703{
5704 __be32 status;
5705 struct nfs4_cpntf_state *cps = NULL;
5706 struct nfsd4_compound_state cstate;
5707
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005708 status = manage_cpntf_state(nn, st, NULL, &cps);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005709 if (status)
5710 return status;
5711
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005712 cps->cpntf_time = ktime_get_boottime_seconds();
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005713 memset(&cstate, 0, sizeof(cstate));
5714 status = lookup_clientid(&cps->cp_p_clid, &cstate, nn, true);
5715 if (status)
5716 goto out;
5717 status = nfsd4_lookup_stateid(&cstate, &cps->cp_p_stateid,
5718 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5719 stid, nn);
5720 put_client_renew(cstate.clp);
5721out:
5722 nfs4_put_cpntf_state(nn, cps);
5723 return status;
5724}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005725
5726void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5727{
5728 spin_lock(&nn->s2s_cp_lock);
5729 _free_cpntf_state_locked(nn, cps);
5730 spin_unlock(&nn->s2s_cp_lock);
5731}
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005732
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005734 * Checks for stateid operations
5735 */
Al Virob37ad282006-10-19 23:28:59 -07005736__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005737nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005738 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005739 stateid_t *stateid, int flags, struct nfsd_file **nfp,
5740 struct nfs4_stid **cstid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005742 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005743 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005744 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005745 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005746 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
Jeff Layton5c4583b2019-08-18 14:18:54 -04005748 if (nfp)
5749 *nfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005751 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 return nfserr_grace;
5753
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005754 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5755 status = check_special_stateids(net, fhp, stateid, flags);
5756 goto done;
5757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005759 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005760 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005761 &s, nn);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005762 if (status == nfserr_bad_stateid)
5763 status = find_cpntf_state(nn, stateid, &s);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005764 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005765 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005766 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005767 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005768 if (status)
5769 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005770
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005771 switch (s->sc_type) {
5772 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005773 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005774 break;
5775 case NFS4_OPEN_STID:
5776 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005777 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005778 break;
5779 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005780 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005781 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005783 if (status)
5784 goto out;
5785 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005786
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005787done:
Jeff Layton5c4583b2019-08-18 14:18:54 -04005788 if (status == nfs_ok && nfp)
5789 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790out:
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005791 if (s) {
5792 if (!status && cstid)
5793 *cstid = s;
5794 else
5795 nfs4_put_stid(s);
5796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 return status;
5798}
5799
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005800/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005801 * Test if the stateid is valid
5802 */
5803__be32
5804nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005805 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005806{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005807 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005808 struct nfsd4_test_stateid_id *stateid;
5809 struct nfs4_client *cl = cstate->session->se_client;
5810
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005811 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005812 stateid->ts_id_status =
5813 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005814
Bryan Schumaker17456802011-07-13 10:50:48 -04005815 return nfs_ok;
5816}
5817
Chuck Lever42691392016-08-11 10:37:30 -04005818static __be32
5819nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5820{
5821 struct nfs4_ol_stateid *stp = openlockstateid(s);
5822 __be32 ret;
5823
Trond Myklebust659aefb2017-11-03 08:00:13 -04005824 ret = nfsd4_lock_ol_stateid(stp);
5825 if (ret)
5826 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005827
5828 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5829 if (ret)
5830 goto out;
5831
5832 ret = nfserr_locks_held;
5833 if (check_for_locks(stp->st_stid.sc_file,
5834 lockowner(stp->st_stateowner)))
5835 goto out;
5836
5837 release_lock_stateid(stp);
5838 ret = nfs_ok;
5839
5840out:
5841 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005842out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005843 nfs4_put_stid(s);
5844 return ret;
5845}
5846
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005847__be32
5848nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005849 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005850{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005851 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005852 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005853 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005854 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005855 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005856 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005857
Jeff Layton1af71cc2014-07-29 21:34:14 -04005858 spin_lock(&cl->cl_lock);
5859 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005860 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005861 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005862 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005863 switch (s->sc_type) {
5864 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005865 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005866 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005867 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005868 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5869 if (ret)
5870 break;
5871 ret = nfserr_locks_held;
5872 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005873 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005874 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005875 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005876 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005877 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005878 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005879 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005880 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005881 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005882 list_del_init(&dp->dl_recall_lru);
5883 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005884 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005885 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005886 goto out;
5887 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005888 }
Trond Myklebust03da3162017-11-03 08:00:16 -04005889 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005890out_unlock:
5891 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005892out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005893 return ret;
5894}
5895
NeilBrown4c4cd222005-07-07 17:59:27 -07005896static inline int
5897setlkflg (int type)
5898{
5899 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5900 RD_STATE : WR_STATE;
5901}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005903static __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 -04005904{
5905 struct svc_fh *current_fh = &cstate->current_fh;
5906 struct nfs4_stateowner *sop = stp->st_stateowner;
5907 __be32 status;
5908
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005909 status = nfsd4_check_seqid(cstate, sop, seqid);
5910 if (status)
5911 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04005912 status = nfsd4_lock_ol_stateid(stp);
5913 if (status != nfs_ok)
5914 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005915 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005916 if (status == nfs_ok)
5917 status = nfs4_check_fh(current_fh, &stp->st_stid);
5918 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005919 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005920 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005921}
5922
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923/*
5924 * Checks for sequence id mutating operations.
5925 */
Al Virob37ad282006-10-19 23:28:59 -07005926static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005927nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005928 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005929 struct nfs4_ol_stateid **stpp,
5930 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005932 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005933 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005934 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04005936 trace_nfsd_preprocess(seqid, stateid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005937
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005939 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005940 if (status)
5941 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005942 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005943 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005945 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005946 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005947 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005948 else
5949 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005950 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005951}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005952
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005953static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5954 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005955{
5956 __be32 status;
5957 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005958 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005960 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005961 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005962 if (status)
5963 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005964 oo = openowner(stp->st_stateowner);
5965 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005966 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005967 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005968 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005969 }
5970 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005971 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972}
5973
Al Virob37ad282006-10-19 23:28:59 -07005974__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005975nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005976 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005978 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07005979 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005980 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005981 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005982 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983
Al Viroa6a9f182013-09-16 10:57:01 -04005984 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5985 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005987 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005988 if (status)
5989 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005991 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005992 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005993 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005994 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005995 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005996 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005997 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005998 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005999 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04006000 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006001 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04006002 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04006003 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006004 mutex_unlock(&stp->st_mutex);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04006005 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
Jeff Layton2a4317c2012-03-21 16:42:43 -04006006 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006007 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04006008put_stateid:
6009 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006011 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 return status;
6013}
6014
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006015static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016{
Jeff Layton82c5ff12012-05-11 09:45:13 -04006017 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006018 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04006019 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006020 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006021}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04006022
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006023static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6024{
6025 switch (to_access) {
6026 case NFS4_SHARE_ACCESS_READ:
6027 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6028 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6029 break;
6030 case NFS4_SHARE_ACCESS_WRITE:
6031 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6032 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6033 break;
6034 case NFS4_SHARE_ACCESS_BOTH:
6035 break;
6036 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006037 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 }
6039}
6040
Al Virob37ad282006-10-19 23:28:59 -07006041__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006042nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006043 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006045 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07006046 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006047 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006048 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
Al Viroa6a9f182013-09-16 10:57:01 -04006050 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6051 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04006053 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02006054 if (od->od_deleg_want)
6055 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6056 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006058 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006059 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006060 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04006063 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006064 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006066 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04006068 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006069 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006071 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006073 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04006074 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04006075 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006077put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006078 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006079 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006081 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 return status;
6083}
6084
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006085static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6086{
Trond Myklebustacf92952014-06-30 11:48:37 -04006087 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04006088 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04006089 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006090
Jeff Layton2c41beb2014-07-29 21:34:41 -04006091 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04006092 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006093
Jeff Laytond83017f2014-07-29 21:34:42 -04006094 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04006095 if (unhashed)
6096 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04006097 spin_unlock(&clp->cl_lock);
6098 free_ol_stateid_reaplist(&reaplist);
6099 } else {
6100 spin_unlock(&clp->cl_lock);
6101 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04006102 if (unhashed)
6103 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04006104 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04006105}
6106
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107/*
6108 * nfs4_unlock_state() called after encode
6109 */
Al Virob37ad282006-10-19 23:28:59 -07006110__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006111nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006112 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006114 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07006115 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006116 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006117 struct net *net = SVC_NET(rqstp);
6118 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
Al Viroa6a9f182013-09-16 10:57:01 -04006120 dprintk("NFSD: nfsd4_close on file %pd\n",
6121 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006123 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6124 &close->cl_stateid,
6125 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006126 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006127 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006128 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006130
6131 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006132
6133 /*
6134 * Technically we don't _really_ have to increment or copy it, since
6135 * it should just be gone after this operation and we clobber the
6136 * copied value below, but we continue to do so here just to ensure
6137 * that racing ops see that there was a state change.
6138 */
Jeff Layton9767feb2015-10-01 09:05:50 -04006139 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006141 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006142 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006143
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006144 /* v4.1+ suggests that we send a special stateid in here, since the
6145 * clients should just ignore this anyway. Since this is not useful
6146 * for v4.0 clients either, we set it to the special close_stateid
6147 * universally.
6148 *
6149 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6150 */
6151 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04006152
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006153 /* put reference from nfs4_preprocess_seqid_op */
6154 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 return status;
6157}
6158
Al Virob37ad282006-10-19 23:28:59 -07006159__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006160nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006161 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006163 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006164 struct nfs4_delegation *dp;
6165 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006166 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07006167 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006168 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006170 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006171 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006173 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006174 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006175 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006176 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04006177 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006178 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04006179 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006180
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006181 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006182put_stateid:
6183 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184out:
6185 return status;
6186}
6187
Benny Halevy87df4de2008-12-15 19:42:03 +02006188static inline u64
6189end_offset(u64 start, u64 len)
6190{
6191 u64 end;
6192
6193 end = start + len;
6194 return end >= start ? end: NFS4_MAX_UINT64;
6195}
6196
6197/* last octet in a range */
6198static inline u64
6199last_byte_offset(u64 start, u64 len)
6200{
6201 u64 end;
6202
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006203 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006204 end = start + len;
6205 return end > start ? end - 1: NFS4_MAX_UINT64;
6206}
6207
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208/*
6209 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6210 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6211 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6212 * locking, this prevents us from being completely protocol-compliant. The
6213 * real solution to this problem is to start using unsigned file offsets in
6214 * the VFS, but this is a very deep change!
6215 */
6216static inline void
6217nfs4_transform_lock_offset(struct file_lock *lock)
6218{
6219 if (lock->fl_start < 0)
6220 lock->fl_start = OFFSET_MAX;
6221 if (lock->fl_end < 0)
6222 lock->fl_end = OFFSET_MAX;
6223}
6224
Jeff Laytoncae80b32015-04-03 09:04:04 -04006225static fl_owner_t
6226nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006227{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006228 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6229
6230 nfs4_get_stateowner(&lo->lo_owner);
6231 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006232}
6233
Jeff Laytoncae80b32015-04-03 09:04:04 -04006234static void
6235nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006236{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006237 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006238
Jeff Laytoncae80b32015-04-03 09:04:04 -04006239 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006240 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006241}
6242
Jeff Layton76d348f2016-09-16 16:28:24 -04006243static void
6244nfsd4_lm_notify(struct file_lock *fl)
6245{
6246 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6247 struct net *net = lo->lo_owner.so_client->net;
6248 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6249 struct nfsd4_blocked_lock *nbl = container_of(fl,
6250 struct nfsd4_blocked_lock, nbl_lock);
6251 bool queue = false;
6252
Jeff Layton7919d0a2016-09-16 16:28:25 -04006253 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006254 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006255 if (!list_empty(&nbl->nbl_list)) {
6256 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006257 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006258 queue = true;
6259 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006260 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006261
6262 if (queue)
6263 nfsd4_run_cb(&nbl->nbl_cb);
6264}
6265
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006266static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006267 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006268 .lm_get_owner = nfsd4_fl_get_owner,
6269 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006270};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271
6272static inline void
6273nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6274{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006275 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276
NeilBrownd5b90262006-04-10 22:55:22 -07006277 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006278 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04006279 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6280 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006281 if (!deny->ld_owner.data)
6282 /* We just don't care that much */
6283 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006284 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006285 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006286nevermind:
6287 deny->ld_owner.len = 0;
6288 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006289 deny->ld_clientid.cl_boot = 0;
6290 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 }
6292 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006293 deny->ld_length = NFS4_MAX_UINT64;
6294 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6296 deny->ld_type = NFS4_READ_LT;
6297 if (fl->fl_type != F_RDLCK)
6298 deny->ld_type = NFS4_WRITE_LT;
6299}
6300
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006301static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006302find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006304 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006305 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
Trond Myklebust0a880a22014-07-30 08:27:10 -04006307 lockdep_assert_held(&clp->cl_lock);
6308
Trond Myklebustd4f04892014-07-29 21:34:36 -04006309 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6310 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006311 if (so->so_is_open_owner)
6312 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006313 if (same_owner_str(so, owner))
6314 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 }
6316 return NULL;
6317}
6318
Trond Myklebustc58c6612014-07-29 21:34:35 -04006319static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006320find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006321{
6322 struct nfs4_lockowner *lo;
6323
Trond Myklebustd4f04892014-07-29 21:34:36 -04006324 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006325 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006326 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006327 return lo;
6328}
6329
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006330static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6331{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006332 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006333}
6334
Jeff Layton6b180f02014-07-29 21:34:26 -04006335static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6336{
6337 struct nfs4_lockowner *lo = lockowner(sop);
6338
6339 kmem_cache_free(lockowner_slab, lo);
6340}
6341
6342static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006343 .so_unhash = nfs4_unhash_lockowner,
6344 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006345};
6346
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347/*
6348 * Alloc a lock owner structure.
6349 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006350 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006352 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006354static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006355alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6356 struct nfs4_ol_stateid *open_stp,
6357 struct nfsd4_lock *lock)
6358{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006359 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006361 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6362 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006364 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006365 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6366 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006367 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006368 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006369 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006370 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006371 if (ret == NULL) {
6372 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006373 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006374 ret = lo;
6375 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006376 nfs4_free_stateowner(&lo->lo_owner);
6377
Trond Myklebustd4f04892014-07-29 21:34:36 -04006378 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006379 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380}
6381
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006382static struct nfs4_ol_stateid *
Trond Myklebusta451b122020-03-01 18:21:38 -05006383find_lock_stateid(const struct nfs4_lockowner *lo,
6384 const struct nfs4_ol_stateid *ost)
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006385{
6386 struct nfs4_ol_stateid *lst;
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006387
Trond Myklebusta451b122020-03-01 18:21:38 -05006388 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006389
Trond Myklebusta451b122020-03-01 18:21:38 -05006390 /* If ost is not hashed, ost->st_locks will not be valid */
6391 if (!nfs4_ol_stateid_unhashed(ost))
6392 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6393 if (lst->st_stateowner == &lo->lo_owner) {
6394 refcount_inc(&lst->st_stid.sc_count);
6395 return lst;
6396 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006397 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006398 return NULL;
6399}
6400
Trond Myklebustbeeca192017-11-03 08:00:14 -04006401static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006402init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6403 struct nfs4_file *fp, struct inode *inode,
6404 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006406 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006407 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408
Trond Myklebustbeeca192017-11-03 08:00:14 -04006409 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006410 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006411retry:
6412 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006413 if (nfs4_ol_stateid_unhashed(open_stp))
6414 goto out_close;
6415 retstp = find_lock_stateid(lo, open_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006416 if (retstp)
Trond Myklebusta451b122020-03-01 18:21:38 -05006417 goto out_found;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006418 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006419 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006420 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006421 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006422 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006423 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006425 stp->st_openstp = open_stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006426 spin_lock(&fp->fi_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006427 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006428 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006429 list_add(&stp->st_perfile, &fp->fi_stateids);
6430 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006431 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006432 return stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006433out_found:
6434 spin_unlock(&clp->cl_lock);
6435 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6436 nfs4_put_stid(&retstp->st_stid);
6437 goto retry;
6438 }
6439 /* To keep mutex tracking happy */
6440 mutex_unlock(&stp->st_mutex);
6441 return retstp;
6442out_close:
6443 spin_unlock(&clp->cl_lock);
6444 mutex_unlock(&stp->st_mutex);
6445 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446}
6447
Jeff Laytonc53530d2014-06-30 11:48:39 -04006448static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006449find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6450 struct inode *inode, struct nfs4_ol_stateid *ost,
6451 bool *new)
6452{
6453 struct nfs4_stid *ns = NULL;
6454 struct nfs4_ol_stateid *lst;
6455 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6456 struct nfs4_client *clp = oo->oo_owner.so_client;
6457
Trond Myklebustbeeca192017-11-03 08:00:14 -04006458 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006459 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006460 lst = find_lock_stateid(lo, ost);
Jeff Layton356a95e2014-07-29 21:34:13 -04006461 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006462 if (lst != NULL) {
6463 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6464 goto out;
6465 nfs4_put_stid(&lst->st_stid);
6466 }
6467 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6468 if (ns == NULL)
6469 return NULL;
6470
6471 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6472 if (lst == openlockstateid(ns))
6473 *new = true;
6474 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006475 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006476out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006477 return lst;
6478}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006479
NeilBrownfd39ca92005-06-23 22:04:03 -07006480static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481check_lock_length(u64 offset, u64 length)
6482{
Kinglong Meee7969312015-07-13 17:34:19 +08006483 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6484 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485}
6486
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006487static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006488{
Trond Myklebust11b91642014-07-29 21:34:08 -04006489 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006490
Jeff Layton7214e862014-07-10 14:07:33 -04006491 lockdep_assert_held(&fp->fi_lock);
6492
Jeff Layton82c5ff12012-05-11 09:45:13 -04006493 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006494 return;
Jeff Layton12659652014-07-10 14:07:28 -04006495 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006496 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006497}
6498
Jeff Layton356a95e2014-07-29 21:34:13 -04006499static __be32
6500lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6501 struct nfs4_ol_stateid *ost,
6502 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006503 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006504{
Jeff Layton5db1c032014-07-29 21:34:28 -04006505 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006506 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006507 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6508 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006509 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006510 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006511 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006512 unsigned int strhashval;
6513
Kinglong Meec8623992015-07-13 17:35:37 +08006514 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006515 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006516 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006517 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6518 if (lo == NULL)
6519 return nfserr_jukebox;
6520 } else {
6521 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006522 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006523 if (!cstate->minorversion &&
6524 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006525 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006526 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006527
Jeff Laytondd257932016-08-11 10:37:39 -04006528 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6529 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006530 status = nfserr_jukebox;
6531 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006532 }
Jeff Laytondd257932016-08-11 10:37:39 -04006533
Jeff Layton5db1c032014-07-29 21:34:28 -04006534 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006535 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006536out:
6537 nfs4_put_stateowner(&lo->lo_owner);
6538 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006539}
6540
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541/*
6542 * LOCK operation
6543 */
Al Virob37ad282006-10-19 23:28:59 -07006544__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006545nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006546 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006548 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006549 struct nfs4_openowner *open_sop = NULL;
6550 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006551 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006552 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006553 struct nfs4_file *fp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006554 struct nfsd_file *nf = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006555 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006556 struct file_lock *file_lock = NULL;
6557 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006558 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006559 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006560 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006561 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006562 unsigned char fl_type;
6563 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006564 struct net *net = SVC_NET(rqstp);
6565 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566
6567 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6568 (long long) lock->lk_offset,
6569 (long long) lock->lk_length);
6570
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 if (check_lock_length(lock->lk_offset, lock->lk_length))
6572 return nfserr_inval;
6573
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006574 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006575 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006576 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6577 return status;
6578 }
6579
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006581 if (nfsd4_has_session(cstate))
6582 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006583 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006584 &cstate->session->se_client->cl_clientid,
6585 sizeof(clientid_t));
6586
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006588 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006592 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593 lock->lk_new_open_seqid,
6594 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006595 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006596 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006598 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006599 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006600 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006601 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006602 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006603 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006604 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006605 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006606 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006607 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006608 lock->lk_old_lock_seqid,
6609 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006610 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006611 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006612 if (status)
6613 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006614 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006616 lkflg = setlkflg(lock->lk_type);
6617 status = nfs4_check_openmode(lock_stp, lkflg);
6618 if (status)
6619 goto out;
6620
NeilBrown0dd395d2005-07-07 17:59:15 -07006621 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006622 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006623 goto out;
6624 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006625 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006626 goto out;
6627
Trond Myklebust11b91642014-07-29 21:34:08 -04006628 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006631 if (nfsd4_has_session(cstate))
6632 fl_flags |= FL_SLEEP;
6633 /* Fallthrough */
6634 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006635 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006636 nf = find_readable_file_locked(fp);
6637 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006638 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006639 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006640 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006641 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006643 if (nfsd4_has_session(cstate))
6644 fl_flags |= FL_SLEEP;
6645 /* Fallthrough */
6646 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006647 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006648 nf = find_writeable_file_locked(fp);
6649 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006650 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006651 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006652 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 default:
6655 status = nfserr_inval;
6656 goto out;
6657 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006658
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006659 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006660 status = nfserr_openmode;
6661 goto out;
6662 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006663
Jeff Layton76d348f2016-09-16 16:28:24 -04006664 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6665 if (!nbl) {
6666 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6667 status = nfserr_jukebox;
6668 goto out;
6669 }
6670
6671 file_lock = &nbl->nbl_lock;
6672 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006673 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006674 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006675 file_lock->fl_file = nf->nf_file;
Jeff Layton76d348f2016-09-16 16:28:24 -04006676 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006677 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6678 file_lock->fl_start = lock->lk_offset;
6679 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6680 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681
Jeff Layton21179d82012-08-21 08:03:32 -04006682 conflock = locks_alloc_lock();
6683 if (!conflock) {
6684 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6685 status = nfserr_jukebox;
6686 goto out;
6687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688
Jeff Layton76d348f2016-09-16 16:28:24 -04006689 if (fl_flags & FL_SLEEP) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01006690 nbl->nbl_time = ktime_get_boottime_seconds();
Jeff Layton0cc11a62016-10-20 09:34:31 -04006691 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006692 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006693 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006694 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006695 }
6696
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006697 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006698 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006700 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006701 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006702 if (lock->lk_reclaim)
6703 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006704 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006705 case FILE_LOCK_DEFERRED:
6706 nbl = NULL;
6707 /* Fallthrough */
6708 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006709 status = nfserr_denied;
6710 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006711 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006712 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006713 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006715 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006716 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006717 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006718 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006719 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006722 if (nbl) {
6723 /* dequeue it if we queued it before */
6724 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006725 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006726 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006727 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006728 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006729 }
6730 free_blocked_lock(nbl);
6731 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006732 if (nf)
6733 nfsd_file_put(nf);
Jeff Layton5db1c032014-07-29 21:34:28 -04006734 if (lock_stp) {
6735 /* Bump seqid manually if the 4.0 replay owner is openowner */
6736 if (cstate->replay_owner &&
6737 cstate->replay_owner != &lock_sop->lo_owner &&
6738 seqid_mutating_err(ntohl(status)))
6739 lock_sop->lo_owner.so_seqid++;
6740
6741 /*
6742 * If this is a new, never-before-used stateid, and we are
6743 * returning an error, then just go ahead and release it.
6744 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006745 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006746 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006747
6748 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006749
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006750 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006751 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006752 if (open_stp)
6753 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006754 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006755 if (conflock)
6756 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 return status;
6758}
6759
6760/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006761 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6762 * so we do a temporary open here just to get an open file to pass to
6763 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6764 * inode operation.)
6765 */
Al Viro04da6e92012-04-13 00:00:04 -04006766static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006767{
Jeff Layton6b556ca2019-08-18 14:18:55 -04006768 struct nfsd_file *nf;
6769 __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
Al Viro04da6e92012-04-13 00:00:04 -04006770 if (!err) {
Jeff Layton6b556ca2019-08-18 14:18:55 -04006771 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6772 nfsd_file_put(nf);
Al Viro04da6e92012-04-13 00:00:04 -04006773 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006774 return err;
6775}
6776
6777/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 * LOCKT operation
6779 */
Al Virob37ad282006-10-19 23:28:59 -07006780__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006781nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006782 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006784 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006785 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006786 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006787 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006788 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006790 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 return nfserr_grace;
6792
6793 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6794 return nfserr_inval;
6795
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006796 if (!nfsd4_has_session(cstate)) {
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006797 status = lookup_clientid(&lockt->lt_clientid, cstate, nn,
6798 false);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006799 if (status)
6800 goto out;
6801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006803 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805
Jeff Layton21179d82012-08-21 08:03:32 -04006806 file_lock = locks_alloc_lock();
6807 if (!file_lock) {
6808 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6809 status = nfserr_jukebox;
6810 goto out;
6811 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006812
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 switch (lockt->lt_type) {
6814 case NFS4_READ_LT:
6815 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006816 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006817 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 case NFS4_WRITE_LT:
6819 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006820 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006821 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006823 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006825 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 }
6827
Kinglong Meec8623992015-07-13 17:35:37 +08006828 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006829 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006830 file_lock->fl_owner = (fl_owner_t)lo;
6831 file_lock->fl_pid = current->tgid;
6832 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
Jeff Layton21179d82012-08-21 08:03:32 -04006834 file_lock->fl_start = lockt->lt_offset;
6835 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836
Jeff Layton21179d82012-08-21 08:03:32 -04006837 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
Jeff Layton21179d82012-08-21 08:03:32 -04006839 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006840 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006841 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006842
Jeff Layton21179d82012-08-21 08:03:32 -04006843 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006845 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 }
6847out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006848 if (lo)
6849 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006850 if (file_lock)
6851 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 return status;
6853}
6854
Al Virob37ad282006-10-19 23:28:59 -07006855__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006856nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006857 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006859 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006860 struct nfs4_ol_stateid *stp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006861 struct nfsd_file *nf = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006862 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006863 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006864 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006865 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6866
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6868 (long long) locku->lu_offset,
6869 (long long) locku->lu_length);
6870
6871 if (check_lock_length(locku->lu_offset, locku->lu_length))
6872 return nfserr_inval;
6873
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006874 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006875 &locku->lu_stateid, NFS4_LOCK_STID,
6876 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006877 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 goto out;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006879 nf = find_any_file(stp->st_stid.sc_file);
6880 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006881 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006882 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006883 }
Jeff Layton21179d82012-08-21 08:03:32 -04006884 file_lock = locks_alloc_lock();
6885 if (!file_lock) {
6886 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6887 status = nfserr_jukebox;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006888 goto put_file;
Jeff Layton21179d82012-08-21 08:03:32 -04006889 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006890
Jeff Layton21179d82012-08-21 08:03:32 -04006891 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006892 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006893 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006894 file_lock->fl_file = nf->nf_file;
Jeff Layton21179d82012-08-21 08:03:32 -04006895 file_lock->fl_flags = FL_POSIX;
6896 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6897 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898
Jeff Layton21179d82012-08-21 08:03:32 -04006899 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6900 locku->lu_length);
6901 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006903 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006904 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006905 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 goto out_nfserr;
6907 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006908 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006909put_file:
6910 nfsd_file_put(nf);
Trond Myklebust858cc572014-07-29 21:34:16 -04006911put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006912 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006913 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006915 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006916 if (file_lock)
6917 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 return status;
6919
6920out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006921 status = nfserrno(err);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006922 goto put_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923}
6924
6925/*
6926 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006927 * true: locks held by lockowner
6928 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006930static bool
6931check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006933 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006934 int status = false;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006935 struct nfsd_file *nf = find_any_file(fp);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006936 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006937 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006938
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006939 if (!nf) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006940 /* Any valid lock stateid should have some sort of access */
6941 WARN_ON_ONCE(1);
6942 return status;
6943 }
6944
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006945 inode = locks_inode(nf->nf_file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006946 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006948 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006949 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006950 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6951 if (fl->fl_owner == (fl_owner_t)lowner) {
6952 status = true;
6953 break;
6954 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006955 }
Jeff Layton6109c852015-01-16 15:05:57 -05006956 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006958 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 return status;
6960}
6961
Al Virob37ad282006-10-19 23:28:59 -07006962__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006963nfsd4_release_lockowner(struct svc_rqst *rqstp,
6964 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006965 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006967 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006969 struct nfs4_stateowner *sop;
6970 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006971 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006973 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006974 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006975 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006976 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006977 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978
6979 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6980 clid->cl_boot, clid->cl_id);
6981
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006982 status = lookup_clientid(clid, cstate, nn, false);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006983 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006984 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006985
Trond Myklebustd4f04892014-07-29 21:34:36 -04006986 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006987 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006988 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006989 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006990 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006991
6992 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006993 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006994
Jeff Layton882e9d22014-07-29 21:34:37 -04006995 /* see if there are still any locks associated with it */
6996 lo = lockowner(sop);
6997 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6998 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6999 status = nfserr_locks_held;
7000 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04007001 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04007002 }
Jeff Layton5adfd882014-07-29 21:34:31 -04007003 }
Jeff Layton882e9d22014-07-29 21:34:37 -04007004
Kinglong Meeb5971af2014-08-22 10:18:43 -04007005 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04007006 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04007007 }
Chuck Lever88584812016-07-13 16:40:14 -04007008 if (!lo) {
7009 spin_unlock(&clp->cl_lock);
7010 return status;
7011 }
7012
7013 unhash_lockowner_locked(lo);
7014 while (!list_empty(&lo->lo_owner.so_stateids)) {
7015 stp = list_first_entry(&lo->lo_owner.so_stateids,
7016 struct nfs4_ol_stateid,
7017 st_perstateowner);
7018 WARN_ON(!unhash_lock_stateid(stp));
7019 put_ol_stateid_locked(stp, &reaplist);
7020 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04007021 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04007022 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007023 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04007024 nfs4_put_stateowner(&lo->lo_owner);
7025
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 return status;
7027}
7028
7029static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07007030alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031{
NeilBrowna55370a2005-06-23 22:03:52 -07007032 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033}
7034
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007035bool
Scott Mayhew6b189102019-03-26 18:06:26 -04007036nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07007037{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007038 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07007039
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007040 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007041 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07007042}
7043
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044/*
7045 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04007046 *
7047 * The caller is responsible for freeing name.data if NULL is returned (it
7048 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05007050struct nfs4_client_reclaim *
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007051nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7052 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053{
7054 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007055 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007057 trace_nfsd_clid_reclaim(nn, name.len, name.data);
NeilBrowna55370a2005-06-23 22:03:52 -07007058 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05007059 if (crp) {
7060 strhashval = clientstr_hashval(name);
7061 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007062 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04007063 crp->cr_name.data = name.data;
7064 crp->cr_name.len = name.len;
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007065 crp->cr_princhash.data = princhash.data;
7066 crp->cr_princhash.len = princhash.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007067 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007068 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007069 }
7070 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071}
7072
Jeff Layton2a4317c2012-03-21 16:42:43 -04007073void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007074nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05007075{
7076 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04007077 kfree(crp->cr_name.data);
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007078 kfree(crp->cr_princhash.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05007079 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007080 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05007081}
7082
7083void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007084nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085{
7086 struct nfs4_client_reclaim *crp = NULL;
7087 int i;
7088
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007090 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7091 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007093 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094 }
7095 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05007096 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097}
7098
7099/*
7100 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04007101struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04007102nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103{
7104 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 struct nfs4_client_reclaim *crp = NULL;
7106
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007107 trace_nfsd_clid_find(nn, name.len, name.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108
Scott Mayhew6b189102019-03-26 18:06:26 -04007109 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007110 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04007111 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 return crp;
7113 }
7114 }
7115 return NULL;
7116}
7117
7118/*
7119* Called from OPEN. Look for clientid in reclaim list.
7120*/
Al Virob37ad282006-10-19 23:28:59 -07007121__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007122nfs4_check_open_reclaim(clientid_t *clid,
7123 struct nfsd4_compound_state *cstate,
7124 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007126 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04007127
7128 /* find clientid in conf_id_hashtbl */
Olga Kornievskaiab7342202019-09-06 15:17:21 -04007129 status = lookup_clientid(clid, cstate, nn, false);
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007130 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04007131 return nfserr_reclaim_bad;
7132
Jeff Layton3b3e7b72014-09-12 16:40:22 -04007133 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
7134 return nfserr_no_grace;
7135
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007136 if (nfsd4_client_record_check(cstate->clp))
7137 return nfserr_reclaim_bad;
7138
7139 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140}
7141
Bryan Schumaker65178db2011-11-01 13:35:21 -04007142#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04007143static inline void
7144put_client(struct nfs4_client *clp)
7145{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007146 atomic_dec(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04007147}
7148
Jeff Layton285abde2014-07-30 08:27:24 -04007149static struct nfs4_client *
7150nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
7151{
7152 struct nfs4_client *clp;
7153 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7154 nfsd_net_id);
7155
7156 if (!nfsd_netns_ready(nn))
7157 return NULL;
7158
7159 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7160 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
7161 return clp;
7162 }
7163 return NULL;
7164}
7165
Jeff Layton7ec0e362014-07-30 08:27:17 -04007166u64
Jeff Layton285abde2014-07-30 08:27:24 -04007167nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04007168{
7169 struct nfs4_client *clp;
7170 u64 count = 0;
7171 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7172 nfsd_net_id);
7173 char buf[INET6_ADDRSTRLEN];
7174
7175 if (!nfsd_netns_ready(nn))
7176 return 0;
7177
7178 spin_lock(&nn->client_lock);
7179 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7180 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7181 pr_info("NFS Client: %s\n", buf);
7182 ++count;
7183 }
7184 spin_unlock(&nn->client_lock);
7185
7186 return count;
7187}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007188
Jeff Laytona0926d12014-07-30 08:27:18 -04007189u64
Jeff Layton285abde2014-07-30 08:27:24 -04007190nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04007191{
7192 u64 count = 0;
7193 struct nfs4_client *clp;
7194 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7195 nfsd_net_id);
7196
7197 if (!nfsd_netns_ready(nn))
7198 return count;
7199
7200 spin_lock(&nn->client_lock);
7201 clp = nfsd_find_client(addr, addr_size);
7202 if (clp) {
7203 if (mark_client_expired_locked(clp) == nfs_ok)
7204 ++count;
7205 else
7206 clp = NULL;
7207 }
7208 spin_unlock(&nn->client_lock);
7209
7210 if (clp)
7211 expire_client(clp);
7212
7213 return count;
7214}
7215
Jeff Layton69fc9ed2014-07-30 08:27:19 -04007216u64
Jeff Layton285abde2014-07-30 08:27:24 -04007217nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04007218{
7219 u64 count = 0;
7220 struct nfs4_client *clp, *next;
7221 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7222 nfsd_net_id);
7223 LIST_HEAD(reaplist);
7224
7225 if (!nfsd_netns_ready(nn))
7226 return count;
7227
7228 spin_lock(&nn->client_lock);
7229 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7230 if (mark_client_expired_locked(clp) == nfs_ok) {
7231 list_add(&clp->cl_lru, &reaplist);
7232 if (max != 0 && ++count >= max)
7233 break;
7234 }
7235 }
7236 spin_unlock(&nn->client_lock);
7237
7238 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
7239 expire_client(clp);
7240
7241 return count;
7242}
7243
Bryan Schumaker184c1842012-11-29 11:40:44 -05007244static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
7245 const char *type)
7246{
7247 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05007248 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05007249 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
7250}
7251
Jeff Layton016200c2014-07-30 08:27:21 -04007252static void
7253nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
7254 struct list_head *collect)
7255{
7256 struct nfs4_client *clp = lst->st_stid.sc_client;
7257 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7258 nfsd_net_id);
7259
7260 if (!collect)
7261 return;
7262
7263 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007264 atomic_inc(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04007265 list_add(&lst->st_locks, collect);
7266}
7267
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007268static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04007269 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04007270 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05007271{
7272 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007273 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007274 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007275 u64 count = 0;
7276
Jeff Layton016200c2014-07-30 08:27:21 -04007277 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007278 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007279 list_for_each_entry_safe(stp, st_next,
7280 &oop->oo_owner.so_stateids, st_perstateowner) {
7281 list_for_each_entry_safe(lst, lst_next,
7282 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04007283 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04007284 if (func(lst))
7285 nfsd_inject_add_lock_to_list(lst,
7286 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04007287 }
Jeff Layton016200c2014-07-30 08:27:21 -04007288 ++count;
7289 /*
7290 * Despite the fact that these functions deal
7291 * with 64-bit integers for "count", we must
7292 * ensure that it doesn't blow up the
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007293 * clp->cl_rpc_users. Throw a warning if we
Jeff Layton016200c2014-07-30 08:27:21 -04007294 * start to approach INT_MAX here.
7295 */
7296 WARN_ON_ONCE(count == (INT_MAX / 2));
7297 if (count == max)
7298 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007299 }
7300 }
7301 }
Jeff Layton016200c2014-07-30 08:27:21 -04007302out:
7303 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007304
7305 return count;
7306}
7307
Jeff Layton016200c2014-07-30 08:27:21 -04007308static u64
7309nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
7310 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05007311{
Jeff Layton016200c2014-07-30 08:27:21 -04007312 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007313}
7314
Jeff Layton016200c2014-07-30 08:27:21 -04007315static u64
7316nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007317{
Jeff Layton016200c2014-07-30 08:27:21 -04007318 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007319 nfsd_print_count(clp, count, "locked files");
7320 return count;
7321}
7322
Jeff Layton016200c2014-07-30 08:27:21 -04007323u64
Jeff Layton285abde2014-07-30 08:27:24 -04007324nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04007325{
7326 struct nfs4_client *clp;
7327 u64 count = 0;
7328 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7329 nfsd_net_id);
7330
7331 if (!nfsd_netns_ready(nn))
7332 return 0;
7333
7334 spin_lock(&nn->client_lock);
7335 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7336 count += nfsd_print_client_locks(clp);
7337 spin_unlock(&nn->client_lock);
7338
7339 return count;
7340}
7341
7342static void
7343nfsd_reap_locks(struct list_head *reaplist)
7344{
7345 struct nfs4_client *clp;
7346 struct nfs4_ol_stateid *stp, *next;
7347
7348 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
7349 list_del_init(&stp->st_locks);
7350 clp = stp->st_stid.sc_client;
7351 nfs4_put_stid(&stp->st_stid);
7352 put_client(clp);
7353 }
7354}
7355
7356u64
Jeff Layton285abde2014-07-30 08:27:24 -04007357nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04007358{
7359 unsigned int count = 0;
7360 struct nfs4_client *clp;
7361 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7362 nfsd_net_id);
7363 LIST_HEAD(reaplist);
7364
7365 if (!nfsd_netns_ready(nn))
7366 return count;
7367
7368 spin_lock(&nn->client_lock);
7369 clp = nfsd_find_client(addr, addr_size);
7370 if (clp)
7371 count = nfsd_collect_client_locks(clp, &reaplist, 0);
7372 spin_unlock(&nn->client_lock);
7373 nfsd_reap_locks(&reaplist);
7374 return count;
7375}
7376
7377u64
Jeff Layton285abde2014-07-30 08:27:24 -04007378nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04007379{
7380 u64 count = 0;
7381 struct nfs4_client *clp;
7382 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7383 nfsd_net_id);
7384 LIST_HEAD(reaplist);
7385
7386 if (!nfsd_netns_ready(nn))
7387 return count;
7388
7389 spin_lock(&nn->client_lock);
7390 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7391 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
7392 if (max != 0 && count >= max)
7393 break;
7394 }
7395 spin_unlock(&nn->client_lock);
7396 nfsd_reap_locks(&reaplist);
7397 return count;
7398}
7399
Jeff Layton82e05ef2014-07-30 08:27:22 -04007400static u64
7401nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
7402 struct list_head *collect,
7403 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007404{
7405 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04007406 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7407 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007408 u64 count = 0;
7409
Jeff Layton82e05ef2014-07-30 08:27:22 -04007410 lockdep_assert_held(&nn->client_lock);
7411
7412 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007413 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04007414 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007415 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007416 if (collect) {
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007417 atomic_inc(&clp->cl_rpc_users);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007418 list_add(&oop->oo_perclient, collect);
7419 }
7420 }
7421 ++count;
7422 /*
7423 * Despite the fact that these functions deal with
7424 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007425 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton82e05ef2014-07-30 08:27:22 -04007426 * warning if we start to approach INT_MAX here.
7427 */
7428 WARN_ON_ONCE(count == (INT_MAX / 2));
7429 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007430 break;
7431 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04007432 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007433
7434 return count;
7435}
7436
Jeff Layton82e05ef2014-07-30 08:27:22 -04007437static u64
7438nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007439{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007440 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
7441
7442 nfsd_print_count(clp, count, "openowners");
7443 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007444}
7445
Jeff Layton82e05ef2014-07-30 08:27:22 -04007446static u64
7447nfsd_collect_client_openowners(struct nfs4_client *clp,
7448 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007449{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007450 return nfsd_foreach_client_openowner(clp, max, collect,
7451 unhash_openowner_locked);
7452}
7453
7454u64
Jeff Layton285abde2014-07-30 08:27:24 -04007455nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007456{
7457 struct nfs4_client *clp;
7458 u64 count = 0;
7459 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7460 nfsd_net_id);
7461
7462 if (!nfsd_netns_ready(nn))
7463 return 0;
7464
7465 spin_lock(&nn->client_lock);
7466 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7467 count += nfsd_print_client_openowners(clp);
7468 spin_unlock(&nn->client_lock);
7469
7470 return count;
7471}
7472
7473static void
7474nfsd_reap_openowners(struct list_head *reaplist)
7475{
7476 struct nfs4_client *clp;
7477 struct nfs4_openowner *oop, *next;
7478
7479 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
7480 list_del_init(&oop->oo_perclient);
7481 clp = oop->oo_owner.so_client;
7482 release_openowner(oop);
7483 put_client(clp);
7484 }
7485}
7486
7487u64
Jeff Layton285abde2014-07-30 08:27:24 -04007488nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
7489 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007490{
7491 unsigned int count = 0;
7492 struct nfs4_client *clp;
7493 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7494 nfsd_net_id);
7495 LIST_HEAD(reaplist);
7496
7497 if (!nfsd_netns_ready(nn))
7498 return count;
7499
7500 spin_lock(&nn->client_lock);
7501 clp = nfsd_find_client(addr, addr_size);
7502 if (clp)
7503 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
7504 spin_unlock(&nn->client_lock);
7505 nfsd_reap_openowners(&reaplist);
7506 return count;
7507}
7508
7509u64
Jeff Layton285abde2014-07-30 08:27:24 -04007510nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007511{
7512 u64 count = 0;
7513 struct nfs4_client *clp;
7514 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7515 nfsd_net_id);
7516 LIST_HEAD(reaplist);
7517
7518 if (!nfsd_netns_ready(nn))
7519 return count;
7520
7521 spin_lock(&nn->client_lock);
7522 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7523 count += nfsd_collect_client_openowners(clp, &reaplist,
7524 max - count);
7525 if (max != 0 && count >= max)
7526 break;
7527 }
7528 spin_unlock(&nn->client_lock);
7529 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007530 return count;
7531}
7532
Bryan Schumaker269de302012-11-29 11:40:42 -05007533static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
7534 struct list_head *victims)
7535{
7536 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007537 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7538 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05007539 u64 count = 0;
7540
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007541 lockdep_assert_held(&nn->client_lock);
7542
7543 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007544 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04007545 if (victims) {
7546 /*
7547 * It's not safe to mess with delegations that have a
7548 * non-zero dl_time. They might have already been broken
7549 * and could be processed by the laundromat outside of
7550 * the state_lock. Just leave them be.
7551 */
7552 if (dp->dl_time != 0)
7553 continue;
7554
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007555 atomic_inc(&clp->cl_rpc_users);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007556 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007557 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04007558 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007559 ++count;
7560 /*
7561 * Despite the fact that these functions deal with
7562 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007563 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007564 * warning if we start to approach INT_MAX here.
7565 */
7566 WARN_ON_ONCE(count == (INT_MAX / 2));
7567 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05007568 break;
7569 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007570 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007571 return count;
7572}
7573
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007574static u64
7575nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05007576{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007577 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007578
7579 nfsd_print_count(clp, count, "delegations");
7580 return count;
7581}
7582
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007583u64
Jeff Layton285abde2014-07-30 08:27:24 -04007584nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007585{
7586 struct nfs4_client *clp;
7587 u64 count = 0;
7588 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7589 nfsd_net_id);
7590
7591 if (!nfsd_netns_ready(nn))
7592 return 0;
7593
7594 spin_lock(&nn->client_lock);
7595 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7596 count += nfsd_print_client_delegations(clp);
7597 spin_unlock(&nn->client_lock);
7598
7599 return count;
7600}
7601
7602static void
7603nfsd_forget_delegations(struct list_head *reaplist)
7604{
7605 struct nfs4_client *clp;
7606 struct nfs4_delegation *dp, *next;
7607
7608 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7609 list_del_init(&dp->dl_recall_lru);
7610 clp = dp->dl_stid.sc_client;
7611 revoke_delegation(dp);
7612 put_client(clp);
7613 }
7614}
7615
7616u64
Jeff Layton285abde2014-07-30 08:27:24 -04007617nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7618 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007619{
7620 u64 count = 0;
7621 struct nfs4_client *clp;
7622 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7623 nfsd_net_id);
7624 LIST_HEAD(reaplist);
7625
7626 if (!nfsd_netns_ready(nn))
7627 return count;
7628
7629 spin_lock(&nn->client_lock);
7630 clp = nfsd_find_client(addr, addr_size);
7631 if (clp)
7632 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7633 spin_unlock(&nn->client_lock);
7634
7635 nfsd_forget_delegations(&reaplist);
7636 return count;
7637}
7638
7639u64
Jeff Layton285abde2014-07-30 08:27:24 -04007640nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007641{
7642 u64 count = 0;
7643 struct nfs4_client *clp;
7644 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7645 nfsd_net_id);
7646 LIST_HEAD(reaplist);
7647
7648 if (!nfsd_netns_ready(nn))
7649 return count;
7650
7651 spin_lock(&nn->client_lock);
7652 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7653 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7654 if (max != 0 && count >= max)
7655 break;
7656 }
7657 spin_unlock(&nn->client_lock);
7658 nfsd_forget_delegations(&reaplist);
7659 return count;
7660}
7661
7662static void
7663nfsd_recall_delegations(struct list_head *reaplist)
7664{
7665 struct nfs4_client *clp;
7666 struct nfs4_delegation *dp, *next;
7667
7668 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7669 list_del_init(&dp->dl_recall_lru);
7670 clp = dp->dl_stid.sc_client;
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007671
7672 trace_nfsd_deleg_recall(&dp->dl_stid.sc_stateid);
7673
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007674 /*
7675 * We skipped all entries that had a zero dl_time before,
7676 * so we can now reset the dl_time back to 0. If a delegation
7677 * break comes in now, then it won't make any difference since
7678 * we're recalling it either way.
7679 */
7680 spin_lock(&state_lock);
7681 dp->dl_time = 0;
7682 spin_unlock(&state_lock);
7683 nfsd_break_one_deleg(dp);
7684 put_client(clp);
7685 }
7686}
7687
7688u64
Jeff Layton285abde2014-07-30 08:27:24 -04007689nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007690 size_t addr_size)
7691{
7692 u64 count = 0;
7693 struct nfs4_client *clp;
7694 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7695 nfsd_net_id);
7696 LIST_HEAD(reaplist);
7697
7698 if (!nfsd_netns_ready(nn))
7699 return count;
7700
7701 spin_lock(&nn->client_lock);
7702 clp = nfsd_find_client(addr, addr_size);
7703 if (clp)
7704 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7705 spin_unlock(&nn->client_lock);
7706
7707 nfsd_recall_delegations(&reaplist);
7708 return count;
7709}
7710
7711u64
Jeff Layton285abde2014-07-30 08:27:24 -04007712nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007713{
7714 u64 count = 0;
7715 struct nfs4_client *clp, *next;
7716 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7717 nfsd_net_id);
7718 LIST_HEAD(reaplist);
7719
7720 if (!nfsd_netns_ready(nn))
7721 return count;
7722
7723 spin_lock(&nn->client_lock);
7724 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7725 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7726 if (max != 0 && ++count >= max)
7727 break;
7728 }
7729 spin_unlock(&nn->client_lock);
7730 nfsd_recall_delegations(&reaplist);
7731 return count;
7732}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007733#endif /* CONFIG_NFSD_FAULT_INJECTION */
7734
Meelap Shahc2f1a552007-07-17 04:04:39 -07007735/*
7736 * Since the lifetime of a delegation isn't limited to that of an open, a
7737 * client may quite reasonably hang on to a delegation as long as it has
7738 * the inode cached. This becomes an obvious problem the first time a
7739 * client's inode cache approaches the size of the server's total memory.
7740 *
7741 * For now we avoid this problem by imposing a hard limit on the number
7742 * of delegations, which varies according to the server's memory size.
7743 */
7744static void
7745set_max_delegations(void)
7746{
7747 /*
7748 * Allow at most 4 delegations per megabyte of RAM. Quick
7749 * estimates suggest that in the worst case (where every delegation
7750 * is for a different inode), a delegation could take about 1.5K,
7751 * giving a worst case usage of about 6% of memory.
7752 */
7753 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7754}
7755
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007756static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007757{
7758 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7759 int i;
7760
Kees Cook6da2ec52018-06-12 13:55:00 -07007761 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7762 sizeof(struct list_head),
7763 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007764 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007765 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007766 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7767 sizeof(struct list_head),
7768 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007769 if (!nn->unconf_id_hashtbl)
7770 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007771 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7772 sizeof(struct list_head),
7773 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007774 if (!nn->sessionid_hashtbl)
7775 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007776
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007777 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007778 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007779 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007780 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007781 for (i = 0; i < SESSION_HASH_SIZE; i++)
7782 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007783 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007784 nn->unconf_name_tree = RB_ROOT;
Arnd Bergmann9cc76802019-10-31 15:53:13 +01007785 nn->boot_time = ktime_get_real_seconds();
Vasily Averin81833de2017-11-13 07:25:40 +03007786 nn->grace_ended = false;
7787 nn->nfsd4_manager.block_opens = true;
7788 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007789 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007790 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007791 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007792 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007793 spin_lock_init(&nn->s2s_cp_lock);
7794 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007795
Jeff Layton0cc11a62016-10-20 09:34:31 -04007796 spin_lock_init(&nn->blocked_locks_lock);
7797 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7798
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007799 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007800 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007801
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007802 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007803
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007804err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007805 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007806err_unconf_id:
7807 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007808err:
7809 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007810}
7811
7812static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007813nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007814{
7815 int i;
7816 struct nfs4_client *clp = NULL;
7817 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7818
7819 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7820 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7821 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7822 destroy_client(clp);
7823 }
7824 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007825
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007826 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7827
Kinglong Mee2b905632014-03-26 22:09:30 +08007828 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7829 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7830 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7831 destroy_client(clp);
7832 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007833 }
7834
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007835 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007836 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007837 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007838 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007839}
7840
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007841int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007842nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007843{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007844 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007845 int ret;
7846
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007847 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007848 if (ret)
7849 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007850 locks_start_grace(net, &nn->nfsd4_manager);
7851 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007852 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7853 goto skip_grace;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01007854 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
Vasily Averin7e981a82017-11-06 16:46:04 +03007855 nn->nfsd4_grace, net->ns.inum);
Chuck Leverdd5e3fb2020-04-05 11:42:29 -04007856 trace_nfsd_grace_start(nn);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007857 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007858 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007859
7860skip_grace:
7861 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7862 net->ns.inum);
7863 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7864 nfsd4_end_grace(nn);
7865 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007866}
7867
7868/* initialization to perform when the nfsd service is started: */
7869
7870int
7871nfs4_state_start(void)
7872{
7873 int ret;
7874
Jeff Layton51a54562015-08-20 07:17:01 -04007875 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007876 if (laundry_wq == NULL) {
7877 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007878 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007879 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007880 ret = nfsd4_create_callback_queue();
7881 if (ret)
7882 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007883
Meelap Shahc2f1a552007-07-17 04:04:39 -07007884 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007885 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007886
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007887out_free_laundry:
7888 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007889out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891}
7892
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007893void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007894nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007898 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007900 cancel_delayed_work_sync(&nn->laundromat_work);
7901 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007902
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007904 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007905 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007907 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007908 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 }
Benny Halevycdc975052014-05-30 09:09:30 -04007910 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911 list_for_each_safe(pos, next, &reaplist) {
7912 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007913 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007914 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915 }
7916
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007917 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007918 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919}
7920
7921void
7922nfs4_state_shutdown(void)
7923{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007924 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007925 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007927
7928static void
7929get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7930{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007931 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7932 CURRENT_STATEID(stateid))
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007933 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007934}
7935
7936static void
7937put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7938{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007939 if (cstate->minorversion) {
7940 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007941 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007942 }
7943}
7944
7945void
7946clear_current_stateid(struct nfsd4_compound_state *cstate)
7947{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007948 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007949}
7950
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007951/*
7952 * functions to set current state id
7953 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007954void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007955nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7956 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007957{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007958 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007959}
7960
7961void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007962nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7963 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007964{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007965 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007966}
7967
7968void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007969nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7970 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007971{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007972 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007973}
7974
7975void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007976nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7977 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007978{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007979 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007980}
7981
7982/*
7983 * functions to consume current state id
7984 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007985
7986void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007987nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7988 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007989{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007990 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007991}
7992
7993void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007994nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7995 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007996{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007997 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007998}
7999
8000void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008001nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
8002 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008003{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008004 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008005}
8006
8007void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008008nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
8009 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008010{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008011 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008012}
8013
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01008014void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008015nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
8016 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008017{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008018 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008019}
8020
8021void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008022nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
8023 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008024{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008025 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008026}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008027
8028void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008029nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
8030 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008031{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008032 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008033}
8034
8035void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008036nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
8037 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008038{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008039 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008040}