blob: be141136aec25dacf9fc1d6e4d5a6f80d4871036 [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"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define NFSDDBG_FACILITY NFSDDBG_PROC
56
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050057#define all_ones {{~0,~0},~0}
58static const stateid_t one_stateid = {
59 .si_generation = ~0,
60 .si_opaque = all_ones,
61};
62static const stateid_t zero_stateid = {
63 /* all fields zero */
64};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010065static const stateid_t currentstateid = {
66 .si_generation = 1,
67};
Trond Myklebustfb500a72017-11-03 08:00:12 -040068static const stateid_t close_stateid = {
69 .si_generation = 0xffffffffU,
70};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050071
Andy Adamsonec6b5d72009-04-03 08:28:28 +030072static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070073
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050074#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
75#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010076#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elbleae254dac2017-11-09 13:41:10 -050077#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040080static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040081static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Scott Mayhew362063a2019-03-26 18:06:28 -040082void nfsd4_end_grace(struct nfsd_net *nn);
Olga Kornievskaia624322f2019-10-04 16:34:26 -040083static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
J. Bruce Fields8b671b82009-02-22 14:51:34 -080085/* Locking: */
86
J. Bruce Fields8b671b82009-02-22 14:51:34 -080087/*
88 * Currently used for the del_recall_lru and file hash table. In an
89 * effort to decrease the scope of the client_mutex, this spinlock may
90 * eventually cover more:
91 */
Benny Halevycdc975052014-05-30 09:09:30 -040092static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080093
Andrew Elble4f34bd02017-11-08 17:29:51 -050094enum nfsd4_st_mutex_lock_subclass {
95 OPEN_STATEID_MUTEX = 0,
96 LOCK_STATEID_MUTEX = 1,
97};
98
Jeff Laytonb401be222014-07-29 21:34:33 -040099/*
100 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
101 * the refcount on the open stateid to drop.
102 */
103static DECLARE_WAIT_QUEUE_HEAD(close_wq);
104
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400105/*
106 * A waitqueue where a writer to clients/#/ctl destroying a client can
107 * wait for cl_rpc_users to drop to 0 and then for the client to be
108 * unhashed.
109 */
110static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
111
Jeff Layton9258a2d2018-03-16 09:47:22 -0400112static struct kmem_cache *client_slab;
Christoph Hellwigabf11352014-05-21 07:43:03 -0700113static struct kmem_cache *openowner_slab;
114static struct kmem_cache *lockowner_slab;
115static struct kmem_cache *file_slab;
116static struct kmem_cache *stateid_slab;
117static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200118static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700119
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400120static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800121
Julia Lawallc4cb8972015-11-21 22:57:39 +0100122static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400123static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200124
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400125static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800126{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400127 return ses->se_flags & NFS4_SESSION_DEAD;
128}
129
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400130static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
131{
132 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400133 return nfserr_jukebox;
134 ses->se_flags |= NFS4_SESSION_DEAD;
135 return nfs_ok;
136}
137
J. Bruce Fields221a6872013-04-01 22:23:49 -0400138static bool is_client_expired(struct nfs4_client *clp)
139{
140 return clp->cl_time == 0;
141}
142
J. Bruce Fields221a6872013-04-01 22:23:49 -0400143static __be32 get_client_locked(struct nfs4_client *clp)
144{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400145 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
146
147 lockdep_assert_held(&nn->client_lock);
148
J. Bruce Fields221a6872013-04-01 22:23:49 -0400149 if (is_client_expired(clp))
150 return nfserr_expired;
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400151 atomic_inc(&clp->cl_rpc_users);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400152 return nfs_ok;
153}
154
155/* must be called under the client_lock */
156static inline void
157renew_client_locked(struct nfs4_client *clp)
158{
159 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
160
161 if (is_client_expired(clp)) {
162 WARN_ON(1);
163 printk("%s: client (clientid %08x/%08x) already expired\n",
164 __func__,
165 clp->cl_clientid.cl_boot,
166 clp->cl_clientid.cl_id);
167 return;
168 }
169
170 dprintk("renewing client (clientid %08x/%08x)\n",
171 clp->cl_clientid.cl_boot,
172 clp->cl_clientid.cl_id);
173 list_move_tail(&clp->cl_lru, &nn->client_lru);
174 clp->cl_time = get_seconds();
175}
176
Fengguang Wuba138432013-04-16 22:14:15 -0400177static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400178{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400179 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
180
181 lockdep_assert_held(&nn->client_lock);
182
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400183 if (!atomic_dec_and_test(&clp->cl_rpc_users))
J. Bruce Fields221a6872013-04-01 22:23:49 -0400184 return;
185 if (!is_client_expired(clp))
186 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400187 else
188 wake_up_all(&expiry_wq);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400189}
190
Jeff Layton4b24ca72014-06-30 11:48:44 -0400191static void put_client_renew(struct nfs4_client *clp)
192{
193 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
194
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400195 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
Jeff Laytond6c249b2014-07-08 14:02:50 -0400196 return;
197 if (!is_client_expired(clp))
198 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400199 else
200 wake_up_all(&expiry_wq);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400201 spin_unlock(&nn->client_lock);
202}
203
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400204static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
205{
206 __be32 status;
207
208 if (is_session_dead(ses))
209 return nfserr_badsession;
210 status = get_client_locked(ses->se_client);
211 if (status)
212 return status;
213 atomic_inc(&ses->se_ref);
214 return nfs_ok;
215}
216
217static void nfsd4_put_session_locked(struct nfsd4_session *ses)
218{
219 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400220 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
221
222 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400223
224 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
225 free_session(ses);
226 put_client_renew_locked(clp);
227}
228
229static void nfsd4_put_session(struct nfsd4_session *ses)
230{
231 struct nfs4_client *clp = ses->se_client;
232 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
233
234 spin_lock(&nn->client_lock);
235 nfsd4_put_session_locked(ses);
236 spin_unlock(&nn->client_lock);
237}
238
Jeff Layton76d348f2016-09-16 16:28:24 -0400239static struct nfsd4_blocked_lock *
240find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
241 struct nfsd_net *nn)
242{
243 struct nfsd4_blocked_lock *cur, *found = NULL;
244
Jeff Layton0cc11a62016-10-20 09:34:31 -0400245 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400246 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
247 if (fh_match(fh, &cur->nbl_fh)) {
248 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400249 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400250 found = cur;
251 break;
252 }
253 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400254 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400255 if (found)
NeilBrowncb03f942018-11-30 10:04:08 +1100256 locks_delete_block(&found->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400257 return found;
258}
259
260static struct nfsd4_blocked_lock *
261find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
262 struct nfsd_net *nn)
263{
264 struct nfsd4_blocked_lock *nbl;
265
266 nbl = find_blocked_lock(lo, fh, nn);
267 if (!nbl) {
268 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
269 if (nbl) {
270 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
497 spin_lock(&f->fi_lock);
498 ret = __nfs4_get_fd(f, O_RDWR);
499 if (!ret) {
500 ret = __nfs4_get_fd(f, O_WRONLY);
501 if (!ret)
502 ret = __nfs4_get_fd(f, O_RDONLY);
503 }
504 spin_unlock(&f->fi_lock);
505 return ret;
506}
507
Trond Myklebust02a35082014-07-25 07:34:22 -0400508static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800509unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700510
511/*
512 * Open owner state (share locks)
513 */
514
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500515/* hash tables for lock and open owners */
516#define OWNER_HASH_BITS 8
517#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
518#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700519
Trond Myklebustd4f04892014-07-29 21:34:36 -0400520static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400521{
522 unsigned int ret;
523
524 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500525 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400526}
NeilBrownef0f3392006-04-10 22:55:41 -0700527
NeilBrownef0f3392006-04-10 22:55:41 -0700528/* hash table for nfs4_file */
529#define FILE_HASH_BITS 8
530#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800531
Trond Myklebustca943212014-07-23 16:17:39 -0400532static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400533{
Trond Myklebustca943212014-07-23 16:17:39 -0400534 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
535}
536
537static unsigned int file_hashval(struct knfsd_fh *fh)
538{
539 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
540}
541
Jeff Layton89876f82013-04-02 09:01:59 -0400542static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700543
Jeff Layton12659652014-07-10 14:07:28 -0400544static void
545__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400546{
Jeff Layton7214e862014-07-10 14:07:33 -0400547 lockdep_assert_held(&fp->fi_lock);
548
Jeff Layton12659652014-07-10 14:07:28 -0400549 if (access & NFS4_SHARE_ACCESS_WRITE)
550 atomic_inc(&fp->fi_access[O_WRONLY]);
551 if (access & NFS4_SHARE_ACCESS_READ)
552 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400553}
554
Jeff Layton12659652014-07-10 14:07:28 -0400555static __be32
556nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400557{
Jeff Layton7214e862014-07-10 14:07:33 -0400558 lockdep_assert_held(&fp->fi_lock);
559
Jeff Layton12659652014-07-10 14:07:28 -0400560 /* Does this access mode make sense? */
561 if (access & ~NFS4_SHARE_ACCESS_BOTH)
562 return nfserr_inval;
563
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400564 /* Does it conflict with a deny mode already set? */
565 if ((access & fp->fi_share_deny) != 0)
566 return nfserr_share_denied;
567
Jeff Layton12659652014-07-10 14:07:28 -0400568 __nfs4_file_get_access(fp, access);
569 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400570}
571
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400572static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
573{
574 /* Common case is that there is no deny mode. */
575 if (deny) {
576 /* Does this deny mode make sense? */
577 if (deny & ~NFS4_SHARE_DENY_BOTH)
578 return nfserr_inval;
579
580 if ((deny & NFS4_SHARE_DENY_READ) &&
581 atomic_read(&fp->fi_access[O_RDONLY]))
582 return nfserr_share_denied;
583
584 if ((deny & NFS4_SHARE_DENY_WRITE) &&
585 atomic_read(&fp->fi_access[O_WRONLY]))
586 return nfserr_share_denied;
587 }
588 return nfs_ok;
589}
590
J. Bruce Fields998db522010-08-07 09:21:41 -0400591static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400592{
Trond Myklebustde186432014-07-10 14:07:26 -0400593 might_lock(&fp->fi_lock);
594
595 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400596 struct nfsd_file *f1 = NULL;
597 struct nfsd_file *f2 = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400598
Jeff Layton6d338b52014-07-10 14:07:29 -0400599 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400600 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400601 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400602 spin_unlock(&fp->fi_lock);
603 if (f1)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400604 nfsd_file_put(f1);
Trond Myklebustde186432014-07-10 14:07:26 -0400605 if (f2)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400606 nfsd_file_put(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400607 }
608}
609
Jeff Layton12659652014-07-10 14:07:28 -0400610static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400611{
Jeff Layton12659652014-07-10 14:07:28 -0400612 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
613
614 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400615 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400616 if (access & NFS4_SHARE_ACCESS_READ)
617 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400618}
619
Sachin Bhamare8287f002015-04-27 14:50:14 +0200620/*
621 * Allocate a new open/delegation state counter. This is needed for
622 * pNFS for proper return on close semantics.
623 *
624 * Note that we only allocate it for pNFS-enabled exports, otherwise
625 * all pointers to struct nfs4_clnt_odstate are always NULL.
626 */
627static struct nfs4_clnt_odstate *
628alloc_clnt_odstate(struct nfs4_client *clp)
629{
630 struct nfs4_clnt_odstate *co;
631
632 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
633 if (co) {
634 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300635 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200636 }
637 return co;
638}
639
640static void
641hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
642{
643 struct nfs4_file *fp = co->co_file;
644
645 lockdep_assert_held(&fp->fi_lock);
646 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
647}
648
649static inline void
650get_clnt_odstate(struct nfs4_clnt_odstate *co)
651{
652 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300653 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200654}
655
656static void
657put_clnt_odstate(struct nfs4_clnt_odstate *co)
658{
659 struct nfs4_file *fp;
660
661 if (!co)
662 return;
663
664 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300665 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200666 list_del(&co->co_perfile);
667 spin_unlock(&fp->fi_lock);
668
669 nfsd4_return_all_file_layouts(co->co_client, fp);
670 kmem_cache_free(odstate_slab, co);
671 }
672}
673
674static struct nfs4_clnt_odstate *
675find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
676{
677 struct nfs4_clnt_odstate *co;
678 struct nfs4_client *cl;
679
680 if (!new)
681 return NULL;
682
683 cl = new->co_client;
684
685 spin_lock(&fp->fi_lock);
686 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
687 if (co->co_client == cl) {
688 get_clnt_odstate(co);
689 goto out;
690 }
691 }
692 co = new;
693 co->co_file = fp;
694 hash_clnt_odstate_locked(new);
695out:
696 spin_unlock(&fp->fi_lock);
697 return co;
698}
699
Kinglong Meed19fb702017-01-18 19:04:42 +0800700struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
701 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400702{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500703 struct nfs4_stid *stid;
704 int new_id;
705
Trond Myklebustf8338832014-07-25 07:34:19 -0400706 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500707 if (!stid)
708 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400709
Jeff Layton4770d722014-07-29 21:34:10 -0400710 idr_preload(GFP_KERNEL);
711 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400712 /* Reserving 0 for start of file in nfsdfs "states" file: */
713 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400714 spin_unlock(&cl->cl_lock);
715 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700716 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500717 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800718
719 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500720 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500721 stid->sc_stateid.si_opaque.so_id = new_id;
722 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
723 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300724 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400725 spin_lock_init(&stid->sc_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400726 INIT_LIST_HEAD(&stid->sc_cp_list);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500727
J. Bruce Fields996e0932011-10-17 11:14:48 -0400728 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500729 * It shouldn't be a problem to reuse an opaque stateid value.
730 * I don't think it is for 4.1. But with 4.0 I worry that, for
731 * example, a stray write retransmission could be accepted by
732 * the server when it should have been rejected. Therefore,
733 * adopt a trick from the sctp code to attempt to maximize the
734 * amount of time until an id is reused, by ensuring they always
735 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400736 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500737 return stid;
738out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800739 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500740 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400741}
742
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400743/*
744 * Create a unique stateid_t to represent each COPY.
745 */
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400746static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
747 unsigned char sc_type)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400748{
749 int new_id;
750
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400751 stid->stid.si_opaque.so_clid.cl_boot = nn->boot_time;
752 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
753 stid->sc_type = sc_type;
754
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400755 idr_preload(GFP_KERNEL);
756 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400757 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
758 stid->stid.si_opaque.so_id = new_id;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400759 spin_unlock(&nn->s2s_cp_lock);
760 idr_preload_end();
761 if (new_id < 0)
762 return 0;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400763 return 1;
764}
765
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400766int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
767{
768 return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
769}
770
771struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
772 struct nfs4_stid *p_stid)
773{
774 struct nfs4_cpntf_state *cps;
775
776 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
777 if (!cps)
778 return NULL;
779 cps->cpntf_time = get_seconds();
780 refcount_set(&cps->cp_stateid.sc_count, 1);
781 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
782 goto out_free;
783 spin_lock(&nn->s2s_cp_lock);
784 list_add(&cps->cp_list, &p_stid->sc_cp_list);
785 spin_unlock(&nn->s2s_cp_lock);
786 return cps;
787out_free:
788 kfree(cps);
789 return NULL;
790}
791
792void nfs4_free_copy_state(struct nfsd4_copy *copy)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400793{
794 struct nfsd_net *nn;
795
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400796 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400797 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
798 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400799 idr_remove(&nn->s2s_cp_stateids,
800 copy->cp_stateid.stid.si_opaque.so_id);
801 spin_unlock(&nn->s2s_cp_lock);
802}
803
804static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
805{
806 struct nfs4_cpntf_state *cps;
807 struct nfsd_net *nn;
808
809 nn = net_generic(net, nfsd_net_id);
810 spin_lock(&nn->s2s_cp_lock);
811 while (!list_empty(&stid->sc_cp_list)) {
812 cps = list_first_entry(&stid->sc_cp_list,
813 struct nfs4_cpntf_state, cp_list);
814 _free_cpntf_state_locked(nn, cps);
815 }
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400816 spin_unlock(&nn->s2s_cp_lock);
817}
818
Jeff Laytonb49e0842014-07-29 21:34:11 -0400819static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400820{
Trond Myklebust60116952014-07-29 21:34:06 -0400821 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400822
Kinglong Meed19fb702017-01-18 19:04:42 +0800823 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400824 if (!stid)
825 return NULL;
826
Kinglong Meed19fb702017-01-18 19:04:42 +0800827 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400828}
829
830static void nfs4_free_deleg(struct nfs4_stid *stid)
831{
Trond Myklebust60116952014-07-29 21:34:06 -0400832 kmem_cache_free(deleg_slab, stid);
833 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400834}
835
NeilBrown6282cd52014-06-04 17:39:26 +1000836/*
837 * When we recall a delegation, we should be careful not to hand it
838 * out again straight away.
839 * To ensure this we keep a pair of bloom filters ('new' and 'old')
840 * in which the filehandles of recalled delegations are "stored".
841 * If a filehandle appear in either filter, a delegation is blocked.
842 * When a delegation is recalled, the filehandle is stored in the "new"
843 * filter.
844 * Every 30 seconds we swap the filters and clear the "new" one,
845 * unless both are empty of course.
846 *
847 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
848 * low 3 bytes as hash-table indices.
849 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400850 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000851 * is used to manage concurrent access. Testing does not need the lock
852 * except when swapping the two filters.
853 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400854static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000855static struct bloom_pair {
856 int entries, old_entries;
857 time_t swap_time;
858 int new; /* index into 'set' */
859 DECLARE_BITMAP(set[2], 256);
860} blocked_delegations;
861
862static int delegation_blocked(struct knfsd_fh *fh)
863{
864 u32 hash;
865 struct bloom_pair *bd = &blocked_delegations;
866
867 if (bd->entries == 0)
868 return 0;
869 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400870 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000871 if (seconds_since_boot() - bd->swap_time > 30) {
872 bd->entries -= bd->old_entries;
873 bd->old_entries = bd->entries;
874 memset(bd->set[bd->new], 0,
875 sizeof(bd->set[0]));
876 bd->new = 1-bd->new;
877 bd->swap_time = seconds_since_boot();
878 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400879 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000880 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100881 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000882 if (test_bit(hash&255, bd->set[0]) &&
883 test_bit((hash>>8)&255, bd->set[0]) &&
884 test_bit((hash>>16)&255, bd->set[0]))
885 return 1;
886
887 if (test_bit(hash&255, bd->set[1]) &&
888 test_bit((hash>>8)&255, bd->set[1]) &&
889 test_bit((hash>>16)&255, bd->set[1]))
890 return 1;
891
892 return 0;
893}
894
895static void block_delegations(struct knfsd_fh *fh)
896{
897 u32 hash;
898 struct bloom_pair *bd = &blocked_delegations;
899
Daniel Borkmann87545892014-12-10 16:33:11 +0100900 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000901
Jeff Laytonf54fe962014-07-25 07:34:26 -0400902 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000903 __set_bit(hash&255, bd->set[bd->new]);
904 __set_bit((hash>>8)&255, bd->set[bd->new]);
905 __set_bit((hash>>16)&255, bd->set[bd->new]);
906 if (bd->entries == 0)
907 bd->swap_time = seconds_since_boot();
908 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400909 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000910}
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500913alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
914 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200915 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400918 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400921 n = atomic_long_inc_return(&num_delegations);
922 if (n < 0 || n > max_delegations)
923 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000924 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400925 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800926 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700927 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400928 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400929
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400930 /*
931 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400932 * meaning of seqid 0 isn't meaningful, really, but let's avoid
933 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400934 */
935 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700936 INIT_LIST_HEAD(&dp->dl_perfile);
937 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200939 dp->dl_clnt_odstate = odstate;
940 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400941 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200942 dp->dl_retries = 1;
943 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200944 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500945 get_nfs4_file(fp);
946 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400948out_dec:
949 atomic_long_dec(&num_delegations);
950 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953void
Trond Myklebust60116952014-07-29 21:34:06 -0400954nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Trond Myklebust11b91642014-07-29 21:34:08 -0400956 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400957 struct nfs4_client *clp = s->sc_client;
958
Jeff Layton4770d722014-07-29 21:34:10 -0400959 might_lock(&clp->cl_lock);
960
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300961 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400962 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400963 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400964 }
Trond Myklebust60116952014-07-29 21:34:06 -0400965 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400966 nfs4_free_cpntf_statelist(clp->net, s);
Jeff Layton4770d722014-07-29 21:34:10 -0400967 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400968 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400969 if (fp)
970 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Jeff Layton9767feb2015-10-01 09:05:50 -0400973void
974nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
975{
976 stateid_t *src = &stid->sc_stateid;
977
978 spin_lock(&stid->sc_lock);
979 if (unlikely(++src->si_generation == 0))
980 src->si_generation = 1;
981 memcpy(dst, src, sizeof(*dst));
982 spin_unlock(&stid->sc_lock);
983}
984
J. Bruce Fields353601e2018-02-16 14:29:42 -0500985static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400987 struct nfsd_file *nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500988
989 spin_lock(&fp->fi_lock);
990 if (--fp->fi_delegees == 0)
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400991 swap(nf, fp->fi_deleg_file);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500992 spin_unlock(&fp->fi_lock);
993
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400994 if (nf)
995 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500996}
997
998static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
999{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -05001000 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001001 struct nfsd_file *nf = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -04001002
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -05001003 WARN_ON_ONCE(!fp->fi_delegees);
1004
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001005 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001006 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001009static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1010{
1011 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001012 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001013 nfs4_put_stid(&dp->dl_stid);
1014}
1015
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001016void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001017{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001018 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001019}
1020
Andrew Elble34ed9872015-10-15 12:07:28 -04001021/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001022 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -04001023 * @clp: a pointer to the nfs4_client we're granting a delegation to
1024 * @fp: a pointer to the nfs4_file we're granting a delegation on
1025 *
1026 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001027 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -04001028 */
1029
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001030static bool
1031nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -04001032{
Andrew Elble34ed9872015-10-15 12:07:28 -04001033 struct nfs4_delegation *searchdp = NULL;
1034 struct nfs4_client *searchclp = NULL;
1035
Benny Halevycdc975052014-05-30 09:09:30 -04001036 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04001037 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001038
Andrew Elble34ed9872015-10-15 12:07:28 -04001039 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1040 searchclp = searchdp->dl_stid.sc_client;
1041 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001042 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -04001043 }
1044 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001045 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -04001046}
1047
1048/**
1049 * hash_delegation_locked - Add a delegation to the appropriate lists
1050 * @dp: a pointer to the nfs4_delegation we are adding.
1051 * @fp: a pointer to the nfs4_file we're granting a delegation on
1052 *
1053 * Return:
1054 * On success: NULL if the delegation was successfully hashed.
1055 *
1056 * On error: -EAGAIN if one was previously granted to this
1057 * nfs4_client for this nfs4_file. Delegation is not hashed.
1058 *
1059 */
1060
1061static int
1062hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1063{
Andrew Elble34ed9872015-10-15 12:07:28 -04001064 struct nfs4_client *clp = dp->dl_stid.sc_client;
1065
1066 lockdep_assert_held(&state_lock);
1067 lockdep_assert_held(&fp->fi_lock);
1068
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001069 if (nfs4_delegation_exists(clp, fp))
1070 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001071 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001072 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001073 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001074 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1075 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001076}
1077
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001078static bool
Jeff Layton42690672014-07-25 07:34:20 -04001079unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Trond Myklebust11b91642014-07-29 21:34:08 -04001081 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001082
Jeff Layton42690672014-07-25 07:34:20 -04001083 lockdep_assert_held(&state_lock);
1084
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001085 if (list_empty(&dp->dl_perfile))
1086 return false;
1087
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001088 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001089 /* Ensure that deleg break won't try to requeue it */
1090 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001091 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001092 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001094 list_del_init(&dp->dl_perfile);
1095 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001096 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001097}
1098
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001099static void destroy_delegation(struct nfs4_delegation *dp)
1100{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001101 bool unhashed;
1102
Jeff Layton42690672014-07-25 07:34:20 -04001103 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001104 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001105 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001106 if (unhashed)
1107 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001108}
1109
1110static void revoke_delegation(struct nfs4_delegation *dp)
1111{
1112 struct nfs4_client *clp = dp->dl_stid.sc_client;
1113
Jeff Layton2d4a5322014-07-25 07:34:21 -04001114 WARN_ON(!list_empty(&dp->dl_recall_lru));
1115
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001116 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001117 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001118 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001119 spin_lock(&clp->cl_lock);
1120 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1121 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001122 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001123 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001124}
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126/*
1127 * SETCLIENTID state
1128 */
1129
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001130static unsigned int clientid_hashval(u32 id)
1131{
1132 return id & CLIENT_HASH_MASK;
1133}
1134
Scott Mayhew6b189102019-03-26 18:06:26 -04001135static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001136{
Scott Mayhew6b189102019-03-26 18:06:26 -04001137 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001138}
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001141 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1142 * st_{access,deny}_bmap field of the stateid, in order to track not
1143 * only what share bits are currently in force, but also what
1144 * combinations of share bits previous opens have used. This allows us
1145 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1146 * return an error if the client attempt to downgrade to a combination
1147 * of share bits not explicable by closing some of its previous opens.
1148 *
1149 * XXX: This enforcement is actually incomplete, since we don't keep
1150 * track of access/deny bit combinations; so, e.g., we allow:
1151 *
1152 * OPEN allow read, deny write
1153 * OPEN allow both, deny none
1154 * DOWNGRADE allow read, deny none
1155 *
1156 * which we should reject.
1157 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001158static unsigned int
1159bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001160 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001161 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001162
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001163 for (i = 1; i < 4; i++) {
1164 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001165 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001166 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001167 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001168}
1169
Jeff Layton82c5ff12012-05-11 09:45:13 -04001170/* set share access for a given stateid */
1171static inline void
1172set_access(u32 access, struct nfs4_ol_stateid *stp)
1173{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001174 unsigned char mask = 1 << access;
1175
1176 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1177 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001178}
1179
1180/* clear share access for a given stateid */
1181static inline void
1182clear_access(u32 access, struct nfs4_ol_stateid *stp)
1183{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001184 unsigned char mask = 1 << access;
1185
1186 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1187 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001188}
1189
1190/* test whether a given stateid has access */
1191static inline bool
1192test_access(u32 access, struct nfs4_ol_stateid *stp)
1193{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001194 unsigned char mask = 1 << access;
1195
1196 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001197}
1198
Jeff Laytonce0fc432012-05-11 09:45:14 -04001199/* set share deny for a given stateid */
1200static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001201set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001202{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001203 unsigned char mask = 1 << deny;
1204
1205 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1206 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001207}
1208
1209/* clear share deny for a given stateid */
1210static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001211clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001212{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001213 unsigned char mask = 1 << deny;
1214
1215 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1216 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001217}
1218
1219/* test whether a given stateid is denying specific access */
1220static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001221test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001222{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001223 unsigned char mask = 1 << deny;
1224
1225 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001226}
1227
1228static int nfs4_access_to_omode(u32 access)
1229{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001230 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001231 case NFS4_SHARE_ACCESS_READ:
1232 return O_RDONLY;
1233 case NFS4_SHARE_ACCESS_WRITE:
1234 return O_WRONLY;
1235 case NFS4_SHARE_ACCESS_BOTH:
1236 return O_RDWR;
1237 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001238 WARN_ON_ONCE(1);
1239 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001240}
1241
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001242/*
1243 * A stateid that had a deny mode associated with it is being released
1244 * or downgraded. Recalculate the deny mode on the file.
1245 */
1246static void
1247recalculate_deny_mode(struct nfs4_file *fp)
1248{
1249 struct nfs4_ol_stateid *stp;
1250
1251 spin_lock(&fp->fi_lock);
1252 fp->fi_share_deny = 0;
1253 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1254 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1255 spin_unlock(&fp->fi_lock);
1256}
1257
1258static void
1259reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1260{
1261 int i;
1262 bool change = false;
1263
1264 for (i = 1; i < 4; i++) {
1265 if ((i & deny) != i) {
1266 change = true;
1267 clear_deny(i, stp);
1268 }
1269 }
1270
1271 /* Recalculate per-file deny mode if there was a change */
1272 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001273 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001274}
1275
Jeff Layton82c5ff12012-05-11 09:45:13 -04001276/* release all access and file references for a given stateid */
1277static void
1278release_all_access(struct nfs4_ol_stateid *stp)
1279{
1280 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001281 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001282
1283 if (fp && stp->st_deny_bmap != 0)
1284 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001285
1286 for (i = 1; i < 4; i++) {
1287 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001288 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001289 clear_access(i, stp);
1290 }
1291}
1292
Kinglong Meed50ffde2015-07-16 12:05:07 +08001293static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1294{
1295 kfree(sop->so_owner.data);
1296 sop->so_ops->so_free(sop);
1297}
1298
Jeff Layton6b180f02014-07-29 21:34:26 -04001299static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1300{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001301 struct nfs4_client *clp = sop->so_client;
1302
1303 might_lock(&clp->cl_lock);
1304
1305 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001306 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001307 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001308 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001309 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001310}
1311
Jeff Laytone8568732015-08-24 12:41:47 -04001312static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001313{
Trond Myklebust11b91642014-07-29 21:34:08 -04001314 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001315
Jeff Layton1c755dc2014-07-29 21:34:12 -04001316 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1317
Jeff Laytone8568732015-08-24 12:41:47 -04001318 if (list_empty(&stp->st_perfile))
1319 return false;
1320
Trond Myklebust1d31a252014-07-10 14:07:25 -04001321 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001322 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001323 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001324 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001325 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001326}
1327
Trond Myklebust60116952014-07-29 21:34:06 -04001328static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001329{
Trond Myklebust60116952014-07-29 21:34:06 -04001330 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001331
Sachin Bhamare8287f002015-04-27 14:50:14 +02001332 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001333 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001334 if (stp->st_stateowner)
1335 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001336 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001337}
1338
Jeff Laytonb49e0842014-07-29 21:34:11 -04001339static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001340{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001341 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1342 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001343 struct nfsd_file *nf;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001344
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001345 nf = find_any_file(stp->st_stid.sc_file);
1346 if (nf) {
1347 get_file(nf->nf_file);
1348 filp_close(nf->nf_file, (fl_owner_t)lo);
1349 nfsd_file_put(nf);
1350 }
Jeff Laytonb49e0842014-07-29 21:34:11 -04001351 nfs4_free_ol_stateid(stid);
1352}
1353
Jeff Layton2c41beb2014-07-29 21:34:41 -04001354/*
1355 * Put the persistent reference to an already unhashed generic stateid, while
1356 * holding the cl_lock. If it's the last reference, then put it onto the
1357 * reaplist for later destruction.
1358 */
1359static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1360 struct list_head *reaplist)
1361{
1362 struct nfs4_stid *s = &stp->st_stid;
1363 struct nfs4_client *clp = s->sc_client;
1364
1365 lockdep_assert_held(&clp->cl_lock);
1366
1367 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1368
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001369 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001370 wake_up_all(&close_wq);
1371 return;
1372 }
1373
1374 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1375 list_add(&stp->st_locks, reaplist);
1376}
1377
Jeff Laytone8568732015-08-24 12:41:47 -04001378static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001379{
Chuck Leverf46c4452016-10-29 18:19:03 -04001380 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001381
1382 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001383 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001384 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001385}
1386
Jeff Layton5adfd882014-07-29 21:34:31 -04001387static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001388{
Chuck Leverf46c4452016-10-29 18:19:03 -04001389 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001390 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001391
Chuck Leverf46c4452016-10-29 18:19:03 -04001392 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001393 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001394 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001395 if (unhashed)
1396 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001397}
1398
Trond Myklebustc58c6612014-07-29 21:34:35 -04001399static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001400{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001401 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001402
Trond Myklebustd4f04892014-07-29 21:34:36 -04001403 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001404
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001405 list_del_init(&lo->lo_owner.so_strhash);
1406}
1407
Jeff Layton2c41beb2014-07-29 21:34:41 -04001408/*
1409 * Free a list of generic stateids that were collected earlier after being
1410 * fully unhashed.
1411 */
1412static void
1413free_ol_stateid_reaplist(struct list_head *reaplist)
1414{
1415 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001416 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001417
1418 might_sleep();
1419
1420 while (!list_empty(reaplist)) {
1421 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1422 st_locks);
1423 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001424 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001425 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001426 if (fp)
1427 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001428 }
1429}
1430
Jeff Laytond83017f2014-07-29 21:34:42 -04001431static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1432 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001433{
1434 struct nfs4_ol_stateid *stp;
1435
Jeff Laytone8568732015-08-24 12:41:47 -04001436 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1437
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001438 while (!list_empty(&open_stp->st_locks)) {
1439 stp = list_entry(open_stp->st_locks.next,
1440 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001441 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001442 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001443 }
1444}
1445
Jeff Laytone8568732015-08-24 12:41:47 -04001446static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001447 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001448{
Jeff Laytone8568732015-08-24 12:41:47 -04001449 bool unhashed;
1450
Jeff Layton2c41beb2014-07-29 21:34:41 -04001451 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1452
Jeff Laytone8568732015-08-24 12:41:47 -04001453 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001454 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001455 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001456}
1457
1458static void release_open_stateid(struct nfs4_ol_stateid *stp)
1459{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001460 LIST_HEAD(reaplist);
1461
1462 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001463 if (unhash_open_stateid(stp, &reaplist))
1464 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001465 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1466 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001467}
1468
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001469static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001470{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001471 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001472
Trond Myklebustd4f04892014-07-29 21:34:36 -04001473 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001474
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001475 list_del_init(&oo->oo_owner.so_strhash);
1476 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001477}
1478
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001479static void release_last_closed_stateid(struct nfs4_openowner *oo)
1480{
Jeff Layton217526e2014-07-30 08:27:11 -04001481 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1482 nfsd_net_id);
1483 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001484
Jeff Layton217526e2014-07-30 08:27:11 -04001485 spin_lock(&nn->client_lock);
1486 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001487 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001488 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001489 oo->oo_last_closed_stid = NULL;
1490 }
Jeff Layton217526e2014-07-30 08:27:11 -04001491 spin_unlock(&nn->client_lock);
1492 if (s)
1493 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001494}
1495
Jeff Layton2c41beb2014-07-29 21:34:41 -04001496static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001497{
1498 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001499 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001500 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001501
Jeff Layton2c41beb2014-07-29 21:34:41 -04001502 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001503
Trond Myklebustd4f04892014-07-29 21:34:36 -04001504 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001505 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001506 while (!list_empty(&oo->oo_owner.so_stateids)) {
1507 stp = list_first_entry(&oo->oo_owner.so_stateids,
1508 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001509 if (unhash_open_stateid(stp, &reaplist))
1510 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001511 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001512 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001513 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001514 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001515 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001516}
1517
Marc Eshel5282fd72009-04-03 08:27:52 +03001518static inline int
1519hash_sessionid(struct nfs4_sessionid *sessionid)
1520{
1521 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1522
1523 return sid->sequence % SESSION_HASH_SIZE;
1524}
1525
Mark Salter135dd002015-04-06 09:46:00 -04001526#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001527static inline void
1528dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1529{
1530 u32 *ptr = (u32 *)(&sessionid->data[0]);
1531 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1532}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001533#else
1534static inline void
1535dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1536{
1537}
1538#endif
1539
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001540/*
1541 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1542 * won't be used for replay.
1543 */
1544void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1545{
1546 struct nfs4_stateowner *so = cstate->replay_owner;
1547
1548 if (nfserr == nfserr_replay_me)
1549 return;
1550
1551 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001552 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001553 return;
1554 }
1555 if (!so)
1556 return;
1557 if (so->so_is_open_owner)
1558 release_last_closed_stateid(openowner(so));
1559 so->so_seqid++;
1560 return;
1561}
Marc Eshel5282fd72009-04-03 08:27:52 +03001562
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001563static void
1564gen_sessionid(struct nfsd4_session *ses)
1565{
1566 struct nfs4_client *clp = ses->se_client;
1567 struct nfsd4_sessionid *sid;
1568
1569 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1570 sid->clientid = clp->cl_clientid;
1571 sid->sequence = current_sessionid++;
1572 sid->reserved = 0;
1573}
1574
1575/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001576 * The protocol defines ca_maxresponssize_cached to include the size of
1577 * the rpc header, but all we need to cache is the data starting after
1578 * the end of the initial SEQUENCE operation--the rest we regenerate
1579 * each time. Therefore we can advertise a ca_maxresponssize_cached
1580 * value that is the number of bytes in our cache plus a few additional
1581 * bytes. In order to stay on the safe side, and not promise more than
1582 * we can cache, those additional bytes must be the minimum possible: 24
1583 * bytes of rpc header (xid through accept state, with AUTH_NULL
1584 * verifier), 12 for the compound header (with zero-length tag), and 44
1585 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001586 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001587#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1588
Andy Adamson557ce262009-08-28 08:45:04 -04001589static void
1590free_session_slots(struct nfsd4_session *ses)
1591{
1592 int i;
1593
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001594 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1595 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001596 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001597 }
Andy Adamson557ce262009-08-28 08:45:04 -04001598}
1599
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001600/*
1601 * We don't actually need to cache the rpc and session headers, so we
1602 * can allocate a little less for each slot:
1603 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001604static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001605{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001606 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001607
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001608 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1609 size = 0;
1610 else
1611 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1612 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001613}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001614
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001615/*
1616 * XXX: If we run out of reserved DRC memory we could (up to a point)
1617 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001618 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001619 */
NeilBrown2030ca52019-09-20 16:36:45 +10001620static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001621{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001622 u32 slotsize = slot_bytes(ca);
1623 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001624 unsigned long avail, total_avail;
NeilBrown2030ca52019-09-20 16:36:45 +10001625 unsigned int scale_factor;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001626
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001627 spin_lock(&nfsd_drc_lock);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001628 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1629 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1630 else
1631 /* We have handed out more space than we chose in
1632 * set_max_drc() to allow. That isn't really a
1633 * problem as long as that doesn't make us think we
1634 * have lots more due to integer overflow.
1635 */
1636 total_avail = 0;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001637 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001638 /*
NeilBrown2030ca52019-09-20 16:36:45 +10001639 * Never use more than a fraction of the remaining memory,
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001640 * unless it's the only way to give this client a slot.
NeilBrown2030ca52019-09-20 16:36:45 +10001641 * The chosen fraction is either 1/8 or 1/number of threads,
1642 * whichever is smaller. This ensures there are adequate
1643 * slots to support multiple clients per thread.
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001644 * Give the client one slot even if that would require
1645 * over-allocation--it is better than failure.
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001646 */
NeilBrown2030ca52019-09-20 16:36:45 +10001647 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1648
1649 avail = clamp_t(unsigned long, avail, slotsize,
1650 total_avail/scale_factor);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001651 num = min_t(int, num, avail / slotsize);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001652 num = max_t(int, num, 1);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001653 nfsd_drc_mem_used += num * slotsize;
1654 spin_unlock(&nfsd_drc_lock);
1655
1656 return num;
1657}
1658
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001659static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001660{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001661 int slotsize = slot_bytes(ca);
1662
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001663 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001664 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001665 spin_unlock(&nfsd_drc_lock);
1666}
1667
Kinglong Mee60810e52014-01-01 00:35:47 +08001668static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1669 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001670{
Kinglong Mee60810e52014-01-01 00:35:47 +08001671 int numslots = fattrs->maxreqs;
1672 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001673 struct nfsd4_session *new;
1674 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001675
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001676 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001677 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1678 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001679
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001680 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001681 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001682 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001683 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001684 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001685 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001686 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001687 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001688 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001689
1690 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1691 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1692
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001693 return new;
1694out_free:
1695 while (i--)
1696 kfree(new->se_slots[i]);
1697 kfree(new);
1698 return NULL;
1699}
1700
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001701static void free_conn(struct nfsd4_conn *c)
1702{
1703 svc_xprt_put(c->cn_xprt);
1704 kfree(c);
1705}
1706
1707static void nfsd4_conn_lost(struct svc_xpt_user *u)
1708{
1709 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1710 struct nfs4_client *clp = c->cn_session->se_client;
1711
1712 spin_lock(&clp->cl_lock);
1713 if (!list_empty(&c->cn_persession)) {
1714 list_del(&c->cn_persession);
1715 free_conn(c);
1716 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001717 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001718 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001719}
1720
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001721static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001722{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001723 struct nfsd4_conn *conn;
1724
1725 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1726 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001727 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001728 svc_xprt_get(rqstp->rq_xprt);
1729 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001730 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001731 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1732 return conn;
1733}
1734
J. Bruce Fields328ead22010-09-29 16:11:06 -04001735static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1736{
1737 conn->cn_session = ses;
1738 list_add(&conn->cn_persession, &ses->se_conns);
1739}
1740
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001741static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1742{
1743 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001744
1745 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001746 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001747 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001748}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001749
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001750static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001751{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001752 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001753 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001754}
1755
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001756static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001757{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001758 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001759
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001760 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001761 ret = nfsd4_register_conn(conn);
1762 if (ret)
1763 /* oops; xprt is already down: */
1764 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001765 /* We may have gained or lost a callback channel: */
1766 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001767}
1768
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001769static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001770{
1771 u32 dir = NFS4_CDFC4_FORE;
1772
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001773 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001774 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001775 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001776}
1777
1778/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001779static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001780{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001781 struct nfs4_client *clp = s->se_client;
1782 struct nfsd4_conn *c;
1783
1784 spin_lock(&clp->cl_lock);
1785 while (!list_empty(&s->se_conns)) {
1786 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1787 list_del_init(&c->cn_persession);
1788 spin_unlock(&clp->cl_lock);
1789
1790 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1791 free_conn(c);
1792
1793 spin_lock(&clp->cl_lock);
1794 }
1795 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001796}
1797
J. Bruce Fields1377b692012-09-11 21:42:40 -04001798static void __free_session(struct nfsd4_session *ses)
1799{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001800 free_session_slots(ses);
1801 kfree(ses);
1802}
1803
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001804static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001805{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001806 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001807 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001808 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001809}
Andy Adamson557ce262009-08-28 08:45:04 -04001810
Fengguang Wu135ae822012-11-10 07:20:25 -05001811static 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 -04001812{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001813 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001814 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001815
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001816 new->se_client = clp;
1817 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001818
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001819 INIT_LIST_HEAD(&new->se_conns);
1820
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001821 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001822 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001823 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001824 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001825 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001826 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001827 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001828 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001829 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001830 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001831
Chuck Leverb0d2e422014-08-22 15:10:59 -04001832 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001833 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001834 /*
1835 * This is a little silly; with sessions there's no real
1836 * use for the callback address. Use the peer address
1837 * as a reasonable default for now, but consider fixing
1838 * the rpc client not to require an address in the
1839 * future:
1840 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001841 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1842 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001843 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001844}
1845
Benny Halevy9089f1b2010-05-12 00:12:26 +03001846/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001847static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001848__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001849{
1850 struct nfsd4_session *elem;
1851 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001852 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001853
Trond Myklebust0a880a22014-07-30 08:27:10 -04001854 lockdep_assert_held(&nn->client_lock);
1855
Marc Eshel5282fd72009-04-03 08:27:52 +03001856 dump_sessionid(__func__, sessionid);
1857 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001858 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001859 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001860 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1861 NFS4_MAX_SESSIONID_LEN)) {
1862 return elem;
1863 }
1864 }
1865
1866 dprintk("%s: session not found\n", __func__);
1867 return NULL;
1868}
1869
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001870static struct nfsd4_session *
1871find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1872 __be32 *ret)
1873{
1874 struct nfsd4_session *session;
1875 __be32 status = nfserr_badsession;
1876
1877 session = __find_in_sessionid_hashtbl(sessionid, net);
1878 if (!session)
1879 goto out;
1880 status = nfsd4_get_session_locked(session);
1881 if (status)
1882 session = NULL;
1883out:
1884 *ret = status;
1885 return session;
1886}
1887
Benny Halevy9089f1b2010-05-12 00:12:26 +03001888/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001889static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001890unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001891{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001892 struct nfs4_client *clp = ses->se_client;
1893 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1894
1895 lockdep_assert_held(&nn->client_lock);
1896
Andy Adamson7116ed62009-04-03 08:27:43 +03001897 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001898 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001899 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001900 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001901}
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1904static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001905STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001907 /*
1908 * We're assuming the clid was not given out from a boot
1909 * precisely 2^32 (about 136 years) before this one. That seems
1910 * a safe assumption:
1911 */
1912 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001914 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001915 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return 1;
1917}
1918
1919/*
1920 * XXX Should we use a slab cache ?
1921 * This type of memory management is somewhat inefficient, but we use it
1922 * anyway since SETCLIENTID is not a common operation.
1923 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001924static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001927 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Jeff Layton9258a2d2018-03-16 09:47:22 -04001929 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001930 if (clp == NULL)
1931 return NULL;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04001932 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001933 if (clp->cl_name.data == NULL)
1934 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001935 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1936 sizeof(struct list_head),
1937 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001938 if (!clp->cl_ownerstr_hashtbl)
1939 goto err_no_hashtbl;
1940 for (i = 0; i < OWNER_HASH_SIZE; i++)
1941 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
Trond Myklebust5694c932014-04-18 14:43:56 -04001942 INIT_LIST_HEAD(&clp->cl_sessions);
1943 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001944 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04001945 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1946 INIT_LIST_HEAD(&clp->cl_idhash);
1947 INIT_LIST_HEAD(&clp->cl_openowners);
1948 INIT_LIST_HEAD(&clp->cl_delegations);
1949 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001950 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001951#ifdef CONFIG_NFSD_PNFS
1952 INIT_LIST_HEAD(&clp->cl_lo_states);
1953#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001954 INIT_LIST_HEAD(&clp->async_copies);
1955 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001956 spin_lock_init(&clp->cl_lock);
1957 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001959err_no_hashtbl:
1960 kfree(clp->cl_name.data);
1961err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001962 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001963 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001966static void __free_client(struct kref *k)
1967{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001968 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1969 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001970
1971 free_svc_cred(&clp->cl_cred);
1972 kfree(clp->cl_ownerstr_hashtbl);
1973 kfree(clp->cl_name.data);
J. Bruce Fields79123442019-06-05 12:42:05 -04001974 kfree(clp->cl_nii_domain.data);
1975 kfree(clp->cl_nii_name.data);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001976 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001977 kmem_cache_free(client_slab, clp);
1978}
1979
YueHaibing297e57a2019-07-08 15:29:33 +08001980static void drop_client(struct nfs4_client *clp)
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001981{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001982 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001983}
1984
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001985static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986free_client(struct nfs4_client *clp)
1987{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001988 while (!list_empty(&clp->cl_sessions)) {
1989 struct nfsd4_session *ses;
1990 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1991 se_perclnt);
1992 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001993 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1994 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001995 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001996 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04001997 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001998 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04001999 clp->cl_nfsd_dentry = NULL;
2000 wake_up_all(&expiry_wq);
2001 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002002 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
Benny Halevy84d38ac2010-05-12 00:13:16 +03002005/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04002006static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03002007unhash_client_locked(struct nfs4_client *clp)
2008{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002009 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002010 struct nfsd4_session *ses;
2011
Trond Myklebust0a880a22014-07-30 08:27:10 -04002012 lockdep_assert_held(&nn->client_lock);
2013
Trond Myklebust4beb3452014-07-30 08:27:02 -04002014 /* Mark the client as expired! */
2015 clp->cl_time = 0;
2016 /* Make it invisible */
2017 if (!list_empty(&clp->cl_idhash)) {
2018 list_del_init(&clp->cl_idhash);
2019 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2020 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2021 else
2022 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2023 }
2024 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002025 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002026 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2027 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002028 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03002029}
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04002032unhash_client(struct nfs4_client *clp)
2033{
2034 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2035
2036 spin_lock(&nn->client_lock);
2037 unhash_client_locked(clp);
2038 spin_unlock(&nn->client_lock);
2039}
2040
Jeff Layton97403d92014-07-30 08:27:12 -04002041static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2042{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04002043 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04002044 return nfserr_jukebox;
2045 unhash_client_locked(clp);
2046 return nfs_ok;
2047}
2048
Trond Myklebust4beb3452014-07-30 08:27:02 -04002049static void
2050__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002052 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002053 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 struct list_head reaplist;
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04002058 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07002059 while (!list_empty(&clp->cl_delegations)) {
2060 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04002061 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04002062 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
Benny Halevycdc975052014-05-30 09:09:30 -04002064 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 while (!list_empty(&reaplist)) {
2066 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04002067 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05002068 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04002070 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05002071 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04002072 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04002073 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002074 }
NeilBrownea1da632005-06-23 22:04:17 -07002075 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002076 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002077 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002078 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002080 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2081 struct nfs4_stateowner *so, *tmp;
2082
2083 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2084 so_strhash) {
2085 /* Should be no openowners at this point */
2086 WARN_ON_ONCE(so->so_is_open_owner);
2087 remove_blocked_locks(lockowner(so));
2088 }
2089 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002090 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002091 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002092 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002093 if (clp->cl_cb_conn.cb_xprt)
2094 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002095 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002096 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098
Trond Myklebust4beb3452014-07-30 08:27:02 -04002099static void
2100destroy_client(struct nfs4_client *clp)
2101{
2102 unhash_client(clp);
2103 __destroy_client(clp);
2104}
2105
Scott Mayhew362063a2019-03-26 18:06:28 -04002106static void inc_reclaim_complete(struct nfs4_client *clp)
2107{
2108 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2109
2110 if (!nn->track_reclaim_completes)
2111 return;
2112 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2113 return;
2114 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2115 nn->reclaim_str_hashtbl_size) {
2116 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2117 clp->net->ns.inum);
2118 nfsd4_end_grace(nn);
2119 }
2120}
2121
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002122static void expire_client(struct nfs4_client *clp)
2123{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002124 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002125 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002126 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002127}
2128
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002129static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2130{
2131 memcpy(target->cl_verifier.data, source->data,
2132 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002135static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2136{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2138 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2139}
2140
J. Bruce Fields50043852015-11-20 15:58:37 -05002141static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2142{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002143 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2144 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2145 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002146 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2147 if ((source->cr_principal && !target->cr_principal) ||
2148 (source->cr_raw_principal && !target->cr_raw_principal) ||
2149 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002150 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002151
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002152 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 target->cr_uid = source->cr_uid;
2154 target->cr_gid = source->cr_gid;
2155 target->cr_group_info = source->cr_group_info;
2156 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002157 target->cr_gss_mech = source->cr_gss_mech;
2158 if (source->cr_gss_mech)
2159 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002163static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002164compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2165{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002166 if (o1->len < o2->len)
2167 return -1;
2168 if (o1->len > o2->len)
2169 return 1;
2170 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002171}
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002174same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2175{
2176 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
2179static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002180same_clid(clientid_t *cl1, clientid_t *cl2)
2181{
2182 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002185static bool groups_equal(struct group_info *g1, struct group_info *g2)
2186{
2187 int i;
2188
2189 if (g1->ngroups != g2->ngroups)
2190 return false;
2191 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002192 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002193 return false;
2194 return true;
2195}
2196
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002197/*
2198 * RFC 3530 language requires clid_inuse be returned when the
2199 * "principal" associated with a requests differs from that previously
2200 * used. We use uid, gid's, and gss principal string as our best
2201 * approximation. We also don't want to allow non-gss use of a client
2202 * established using gss: in theory cr_principal should catch that
2203 * change, but in practice cr_principal can be null even in the gss case
2204 * since gssd doesn't always pass down a principal string.
2205 */
2206static bool is_gss_cred(struct svc_cred *cr)
2207{
2208 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2209 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2210}
2211
2212
Vivek Trivedi5559b502012-07-24 21:18:20 +05302213static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002214same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2215{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002216 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002217 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2218 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002219 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2220 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002221 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002222 if (cr1->cr_principal == cr2->cr_principal)
2223 return true;
2224 if (!cr1->cr_principal || !cr2->cr_principal)
2225 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302226 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
J. Bruce Fields57266a62013-04-13 14:27:29 -04002229static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2230{
2231 struct svc_cred *cr = &rqstp->rq_cred;
2232 u32 service;
2233
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002234 if (!cr->cr_gss_mech)
2235 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002236 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2237 return service == RPC_GSS_SVC_INTEGRITY ||
2238 service == RPC_GSS_SVC_PRIVACY;
2239}
2240
Andrew Elblededeb132016-06-15 12:52:08 -04002241bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002242{
2243 struct svc_cred *cr = &rqstp->rq_cred;
2244
2245 if (!cl->cl_mach_cred)
2246 return true;
2247 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2248 return false;
2249 if (!svc_rqst_integrity_protected(rqstp))
2250 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002251 if (cl->cl_cred.cr_raw_principal)
2252 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2253 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002254 if (!cr->cr_principal)
2255 return false;
2256 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2257}
2258
Jeff Layton294ac322014-07-30 08:27:15 -04002259static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002260{
Chuck Leverab4684d2012-03-02 17:13:50 -05002261 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Jeff Laytonf4199922014-06-17 07:44:11 -04002263 /*
2264 * This is opaque to client, so no need to byte-swap. Use
2265 * __force to keep sparse happy
2266 */
2267 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002268 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002269 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
Jeff Layton294ac322014-07-30 08:27:15 -04002272static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2273{
2274 clp->cl_clientid.cl_boot = nn->boot_time;
2275 clp->cl_clientid.cl_id = nn->clientid_counter++;
2276 gen_confirm(clp, nn);
2277}
2278
Jeff Layton4770d722014-07-29 21:34:10 -04002279static struct nfs4_stid *
2280find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002281{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002282 struct nfs4_stid *ret;
2283
2284 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2285 if (!ret || !ret->sc_type)
2286 return NULL;
2287 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002288}
2289
Jeff Layton4770d722014-07-29 21:34:10 -04002290static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002291find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002292{
2293 struct nfs4_stid *s;
2294
Jeff Layton4770d722014-07-29 21:34:10 -04002295 spin_lock(&cl->cl_lock);
2296 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002297 if (s != NULL) {
2298 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002299 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002300 else
2301 s = NULL;
2302 }
Jeff Layton4770d722014-07-29 21:34:10 -04002303 spin_unlock(&cl->cl_lock);
2304 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002305}
2306
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002307static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2308{
2309 struct nfsdfs_client *nc;
2310 nc = get_nfsdfs_client(inode);
2311 if (!nc)
2312 return NULL;
2313 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2314}
2315
J. Bruce Fields169319f2019-06-19 12:39:46 -04002316static void seq_quote_mem(struct seq_file *m, char *data, int len)
2317{
2318 seq_printf(m, "\"");
2319 seq_escape_mem_ascii(m, data, len);
2320 seq_printf(m, "\"");
2321}
2322
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002323static int client_info_show(struct seq_file *m, void *v)
2324{
2325 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002326 struct nfs4_client *clp;
2327 u64 clid;
2328
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002329 clp = get_nfsdfs_clp(inode);
2330 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002331 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002332 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2333 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002334 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2335 seq_printf(m, "name: ");
2336 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2337 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields79123442019-06-05 12:42:05 -04002338 if (clp->cl_nii_domain.data) {
2339 seq_printf(m, "Implementation domain: ");
2340 seq_quote_mem(m, clp->cl_nii_domain.data,
2341 clp->cl_nii_domain.len);
2342 seq_printf(m, "\nImplementation name: ");
2343 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2344 seq_printf(m, "\nImplementation time: [%ld, %ld]\n",
2345 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2346 }
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002347 drop_client(clp);
2348
2349 return 0;
2350}
2351
2352static int client_info_open(struct inode *inode, struct file *file)
2353{
2354 return single_open(file, client_info_show, inode);
2355}
2356
2357static const struct file_operations client_info_fops = {
2358 .open = client_info_open,
2359 .read = seq_read,
2360 .llseek = seq_lseek,
2361 .release = single_release,
2362};
2363
J. Bruce Fields78599c42019-04-22 15:26:23 -04002364static void *states_start(struct seq_file *s, loff_t *pos)
2365 __acquires(&clp->cl_lock)
2366{
2367 struct nfs4_client *clp = s->private;
2368 unsigned long id = *pos;
2369 void *ret;
2370
2371 spin_lock(&clp->cl_lock);
2372 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2373 *pos = id;
2374 return ret;
2375}
2376
2377static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2378{
2379 struct nfs4_client *clp = s->private;
2380 unsigned long id = *pos;
2381 void *ret;
2382
2383 id = *pos;
2384 id++;
2385 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2386 *pos = id;
2387 return ret;
2388}
2389
2390static void states_stop(struct seq_file *s, void *v)
2391 __releases(&clp->cl_lock)
2392{
2393 struct nfs4_client *clp = s->private;
2394
2395 spin_unlock(&clp->cl_lock);
2396}
2397
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002398static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002399{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002400 struct inode *inode = f->nf_inode;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002401
2402 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2403 MAJOR(inode->i_sb->s_dev),
2404 MINOR(inode->i_sb->s_dev),
2405 inode->i_ino);
2406}
2407
2408static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2409{
2410 seq_printf(s, "owner: ");
2411 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2412}
2413
2414static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2415{
2416 struct nfs4_ol_stateid *ols;
2417 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002418 struct nfsd_file *file;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002419 struct nfs4_stateowner *oo;
2420 unsigned int access, deny;
2421
2422 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2423 return 0; /* XXX: or SEQ_SKIP? */
2424 ols = openlockstateid(st);
2425 oo = ols->st_stateowner;
2426 nf = st->sc_file;
2427 file = find_any_file(nf);
2428
2429 seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid);
2430
2431 access = bmap_to_share_mode(ols->st_access_bmap);
2432 deny = bmap_to_share_mode(ols->st_deny_bmap);
2433
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002434 seq_printf(s, "access: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002435 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2436 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002437 seq_printf(s, "deny: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002438 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2439 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2440
2441 nfs4_show_superblock(s, file);
2442 seq_printf(s, ", ");
2443 nfs4_show_owner(s, oo);
2444 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002445 nfsd_file_put(file);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002446
2447 return 0;
2448}
2449
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002450static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2451{
2452 struct nfs4_ol_stateid *ols;
2453 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002454 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002455 struct nfs4_stateowner *oo;
2456
2457 ols = openlockstateid(st);
2458 oo = ols->st_stateowner;
2459 nf = st->sc_file;
2460 file = find_any_file(nf);
2461
2462 seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid);
2463
2464 /*
2465 * Note: a lock stateid isn't really the same thing as a lock,
2466 * it's the locking state held by one owner on a file, and there
2467 * may be multiple (or no) lock ranges associated with it.
2468 * (Same for the matter is true of open stateids.)
2469 */
2470
2471 nfs4_show_superblock(s, file);
2472 /* XXX: open stateid? */
2473 seq_printf(s, ", ");
2474 nfs4_show_owner(s, oo);
2475 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002476 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002477
2478 return 0;
2479}
2480
2481static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2482{
2483 struct nfs4_delegation *ds;
2484 struct nfs4_file *nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002485 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002486
2487 ds = delegstateid(st);
2488 nf = st->sc_file;
2489 file = nf->fi_deleg_file;
2490
2491 seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid);
2492
2493 /* Kinda dead code as long as we only support read delegs: */
2494 seq_printf(s, "access: %s, ",
2495 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2496
2497 /* XXX: lease time, whether it's being recalled. */
2498
2499 nfs4_show_superblock(s, file);
2500 seq_printf(s, " }\n");
2501
2502 return 0;
2503}
2504
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002505static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2506{
2507 struct nfs4_layout_stateid *ls;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002508 struct nfsd_file *file;
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002509
2510 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2511 file = ls->ls_file;
2512
2513 seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid);
2514
2515 /* XXX: What else would be useful? */
2516
2517 nfs4_show_superblock(s, file);
2518 seq_printf(s, " }\n");
2519
2520 return 0;
2521}
2522
J. Bruce Fields78599c42019-04-22 15:26:23 -04002523static int states_show(struct seq_file *s, void *v)
2524{
2525 struct nfs4_stid *st = v;
2526
2527 switch (st->sc_type) {
2528 case NFS4_OPEN_STID:
2529 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002530 case NFS4_LOCK_STID:
2531 return nfs4_show_lock(s, st);
2532 case NFS4_DELEG_STID:
2533 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002534 case NFS4_LAYOUT_STID:
2535 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002536 default:
2537 return 0; /* XXX: or SEQ_SKIP? */
2538 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002539 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002540}
2541
2542static struct seq_operations states_seq_ops = {
2543 .start = states_start,
2544 .next = states_next,
2545 .stop = states_stop,
2546 .show = states_show
2547};
2548
2549static int client_states_open(struct inode *inode, struct file *file)
2550{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002551 struct seq_file *s;
2552 struct nfs4_client *clp;
2553 int ret;
2554
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002555 clp = get_nfsdfs_clp(inode);
2556 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002557 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002558
2559 ret = seq_open(file, &states_seq_ops);
2560 if (ret)
2561 return ret;
2562 s = file->private_data;
2563 s->private = clp;
2564 return 0;
2565}
2566
2567static int client_opens_release(struct inode *inode, struct file *file)
2568{
2569 struct seq_file *m = file->private_data;
2570 struct nfs4_client *clp = m->private;
2571
2572 /* XXX: alternatively, we could get/drop in seq start/stop */
2573 drop_client(clp);
2574 return 0;
2575}
2576
2577static const struct file_operations client_states_fops = {
2578 .open = client_states_open,
2579 .read = seq_read,
2580 .llseek = seq_lseek,
2581 .release = client_opens_release,
2582};
2583
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002584/*
2585 * Normally we refuse to destroy clients that are in use, but here the
2586 * administrator is telling us to just do it. We also want to wait
2587 * so the caller has a guarantee that the client's locks are gone by
2588 * the time the write returns:
2589 */
YueHaibing297e57a2019-07-08 15:29:33 +08002590static void force_expire_client(struct nfs4_client *clp)
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002591{
2592 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2593 bool already_expired;
2594
2595 spin_lock(&clp->cl_lock);
2596 clp->cl_time = 0;
2597 spin_unlock(&clp->cl_lock);
2598
2599 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2600 spin_lock(&nn->client_lock);
2601 already_expired = list_empty(&clp->cl_lru);
2602 if (!already_expired)
2603 unhash_client_locked(clp);
2604 spin_unlock(&nn->client_lock);
2605
2606 if (!already_expired)
2607 expire_client(clp);
2608 else
2609 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2610}
2611
2612static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2613 size_t size, loff_t *pos)
2614{
2615 char *data;
2616 struct nfs4_client *clp;
2617
2618 data = simple_transaction_get(file, buf, size);
2619 if (IS_ERR(data))
2620 return PTR_ERR(data);
2621 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2622 return -EINVAL;
2623 clp = get_nfsdfs_clp(file_inode(file));
2624 if (!clp)
2625 return -ENXIO;
2626 force_expire_client(clp);
2627 drop_client(clp);
2628 return 7;
2629}
2630
2631static const struct file_operations client_ctl_fops = {
2632 .write = client_ctl_write,
2633 .release = simple_transaction_release,
2634};
2635
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002636static const struct tree_descr client_files[] = {
2637 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002638 [1] = {"states", &client_states_fops, S_IRUSR},
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002639 [2] = {"ctl", &client_ctl_fops, S_IRUSR|S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002640 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002641};
2642
Jeff Layton2216d442012-11-12 15:00:57 -05002643static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002644 struct svc_rqst *rqstp, nfs4_verifier *verf)
2645{
2646 struct nfs4_client *clp;
2647 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002648 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002649 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002650 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002651
2652 clp = alloc_client(name);
2653 if (clp == NULL)
2654 return NULL;
2655
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002656 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2657 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002658 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002659 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002660 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002661 gen_clid(clp, nn);
2662 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002663 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002664 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002665 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002666 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002667 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002668 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002669 clp->net = net;
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002670 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002671 clp->cl_clientid.cl_id - nn->clientid_base,
2672 client_files);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002673 if (!clp->cl_nfsd_dentry) {
2674 free_client(clp);
2675 return NULL;
2676 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002677 return clp;
2678}
2679
NeilBrownfd39ca92005-06-23 22:04:03 -07002680static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002681add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2682{
2683 struct rb_node **new = &(root->rb_node), *parent = NULL;
2684 struct nfs4_client *clp;
2685
2686 while (*new) {
2687 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2688 parent = *new;
2689
2690 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2691 new = &((*new)->rb_left);
2692 else
2693 new = &((*new)->rb_right);
2694 }
2695
2696 rb_link_node(&new_clp->cl_namenode, parent, new);
2697 rb_insert_color(&new_clp->cl_namenode, root);
2698}
2699
2700static struct nfs4_client *
2701find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2702{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002703 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002704 struct rb_node *node = root->rb_node;
2705 struct nfs4_client *clp;
2706
2707 while (node) {
2708 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2709 cmp = compare_blob(&clp->cl_name, name);
2710 if (cmp > 0)
2711 node = node->rb_left;
2712 else if (cmp < 0)
2713 node = node->rb_right;
2714 else
2715 return clp;
2716 }
2717 return NULL;
2718}
2719
2720static void
2721add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
2723 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002724 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Trond Myklebust0a880a22014-07-30 08:27:10 -04002726 lockdep_assert_held(&nn->client_lock);
2727
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002728 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002729 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002731 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002732 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733}
2734
NeilBrownfd39ca92005-06-23 22:04:03 -07002735static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736move_to_confirmed(struct nfs4_client *clp)
2737{
2738 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002739 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Trond Myklebust0a880a22014-07-30 08:27:10 -04002741 lockdep_assert_held(&nn->client_lock);
2742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002744 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002745 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002746 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002747 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002748 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
2750
2751static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002752find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
2754 struct nfs4_client *clp;
2755 unsigned int idhashval = clientid_hashval(clid->cl_id);
2756
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002757 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002758 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002759 if ((bool)clp->cl_minorversion != sessions)
2760 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002761 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 }
2765 return NULL;
2766}
2767
2768static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002769find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2770{
2771 struct list_head *tbl = nn->conf_id_hashtbl;
2772
Trond Myklebust0a880a22014-07-30 08:27:10 -04002773 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002774 return find_client_in_id_table(tbl, clid, sessions);
2775}
2776
2777static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002778find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002780 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Trond Myklebust0a880a22014-07-30 08:27:10 -04002782 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002783 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002786static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002787{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002788 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002789}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002790
NeilBrown28ce6052005-06-23 22:03:56 -07002791static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002792find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002793{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002794 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002795 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002796}
2797
2798static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002799find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002800{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002801 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002802 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002803}
2804
NeilBrownfd39ca92005-06-23 22:04:03 -07002805static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002806gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002808 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002809 struct sockaddr *sa = svc_addr(rqstp);
2810 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002811 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Jeff Layton7077ecb2009-08-14 12:57:58 -04002813 /* Currently, we only support tcp and tcp6 for the callback channel */
2814 if (se->se_callback_netid_len == 3 &&
2815 !memcmp(se->se_callback_netid_val, "tcp", 3))
2816 expected_family = AF_INET;
2817 else if (se->se_callback_netid_len == 4 &&
2818 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2819 expected_family = AF_INET6;
2820 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 goto out_err;
2822
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002823 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002824 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002825 (struct sockaddr *)&conn->cb_addr,
2826 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002827
J. Bruce Fields07263f12010-05-31 19:09:40 -04002828 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002830
J. Bruce Fields07263f12010-05-31 19:09:40 -04002831 if (conn->cb_addr.ss_family == AF_INET6)
2832 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002833
J. Bruce Fields07263f12010-05-31 19:09:40 -04002834 conn->cb_prog = se->se_callback_prog;
2835 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002836 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 return;
2838out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002839 conn->cb_addr.ss_family = AF_UNSPEC;
2840 conn->cb_addrlen = 0;
Rasmus Villemoes4ab495b2017-02-24 01:15:55 +01002841 dprintk("NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 "will not receive delegations\n",
2843 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return;
2846}
2847
Andy Adamson074fe892009-04-03 08:28:15 +03002848/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002849 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002850 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002851static void
Andy Adamson074fe892009-04-03 08:28:15 +03002852nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2853{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002854 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002855 struct nfsd4_slot *slot = resp->cstate.slot;
2856 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002857
Andy Adamson557ce262009-08-28 08:45:04 -04002858 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002859
J. Bruce Fields085def32017-10-18 16:17:18 -04002860 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002861 slot->sl_opcnt = resp->opcnt;
2862 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002863 free_svc_cred(&slot->sl_cred);
2864 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002865
J. Bruce Fields085def32017-10-18 16:17:18 -04002866 if (!nfsd4_cache_this(resp)) {
2867 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002868 return;
2869 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002870 slot->sl_flags |= NFSD4_SLOT_CACHED;
2871
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002872 base = resp->cstate.data_offset;
2873 slot->sl_datalen = buf->len - base;
2874 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002875 WARN(1, "%s: sessions DRC could not cache compound\n",
2876 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002877 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002878}
2879
2880/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002881 * Encode the replay sequence operation from the slot values.
2882 * If cachethis is FALSE encode the uncached rep error on the next
2883 * operation which sets resp->p and increments resp->opcnt for
2884 * nfs4svc_encode_compoundres.
2885 *
Andy Adamson074fe892009-04-03 08:28:15 +03002886 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002887static __be32
2888nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2889 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002890{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002891 struct nfsd4_op *op;
2892 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002893
Andy Adamsonabfabf82009-07-23 19:02:18 -04002894 /* Encode the replayed sequence operation */
2895 op = &args->ops[resp->opcnt - 1];
2896 nfsd4_encode_operation(resp, op);
2897
J. Bruce Fields085def32017-10-18 16:17:18 -04002898 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2899 return op->status;
2900 if (args->opcnt == 1) {
2901 /*
2902 * The original operation wasn't a solo sequence--we
2903 * always cache those--so this retry must not match the
2904 * original:
2905 */
2906 op->status = nfserr_seq_false_retry;
2907 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002908 op = &args->ops[resp->opcnt++];
2909 op->status = nfserr_retry_uncached_rep;
2910 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002911 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002912 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002913}
2914
2915/*
Andy Adamson557ce262009-08-28 08:45:04 -04002916 * The sequence operation is not cached because we can use the slot and
2917 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002918 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002919static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002920nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2921 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002922{
Andy Adamson557ce262009-08-28 08:45:04 -04002923 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002924 struct xdr_stream *xdr = &resp->xdr;
2925 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002926 __be32 status;
2927
Andy Adamson557ce262009-08-28 08:45:04 -04002928 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002929
Andy Adamsonabfabf82009-07-23 19:02:18 -04002930 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002931 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002932 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002933
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002934 p = xdr_reserve_space(xdr, slot->sl_datalen);
2935 if (!p) {
2936 WARN_ON_ONCE(1);
2937 return nfserr_serverfault;
2938 }
2939 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2940 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002941
Andy Adamson557ce262009-08-28 08:45:04 -04002942 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002943 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002944}
2945
Andy Adamson0733d212009-04-03 08:28:01 +03002946/*
2947 * Set the exchange_id flags returned by the server.
2948 */
2949static void
2950nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2951{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002952#ifdef CONFIG_NFSD_PNFS
2953 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2954#else
Andy Adamson0733d212009-04-03 08:28:01 +03002955 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002956#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002957
2958 /* Referrals are supported, Migration is not. */
2959 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2960
2961 /* set the wire flags to return to client. */
2962 clid->flags = new->cl_exchange_flags;
2963}
2964
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002965static bool client_has_openowners(struct nfs4_client *clp)
2966{
2967 struct nfs4_openowner *oo;
2968
2969 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2970 if (!list_empty(&oo->oo_owner.so_stateids))
2971 return true;
2972 }
2973 return false;
2974}
2975
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002976static bool client_has_state(struct nfs4_client *clp)
2977{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002978 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002979#ifdef CONFIG_NFSD_PNFS
2980 || !list_empty(&clp->cl_lo_states)
2981#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002982 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002983 || !list_empty(&clp->cl_sessions)
2984 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002985}
2986
J. Bruce Fields79123442019-06-05 12:42:05 -04002987static __be32 copy_impl_id(struct nfs4_client *clp,
2988 struct nfsd4_exchange_id *exid)
2989{
2990 if (!exid->nii_domain.data)
2991 return 0;
2992 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
2993 if (!clp->cl_nii_domain.data)
2994 return nfserr_jukebox;
2995 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
2996 if (!clp->cl_nii_name.data)
2997 return nfserr_jukebox;
2998 clp->cl_nii_time.tv_sec = exid->nii_time.tv_sec;
2999 clp->cl_nii_time.tv_nsec = exid->nii_time.tv_nsec;
3000 return 0;
3001}
3002
Al Virob37ad282006-10-19 23:28:59 -07003003__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003004nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3005 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003006{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003007 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003008 struct nfs4_client *conf, *new;
3009 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003010 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04003011 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03003012 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04003013 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003014 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003015 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03003016
Jeff Layton363168b2009-08-14 12:57:56 -04003017 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03003018 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04003019 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03003020 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04003021 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03003022
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003023 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03003024 return nfserr_inval;
3025
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003026 new = create_client(exid->clname, rqstp, &verf);
3027 if (new == NULL)
3028 return nfserr_jukebox;
J. Bruce Fields79123442019-06-05 12:42:05 -04003029 status = copy_impl_id(new, exid);
3030 if (status)
3031 goto out_nolock;
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003032
Andy Adamson0733d212009-04-03 08:28:01 +03003033 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003034 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04003035 exid->spo_must_enforce[0] = 0;
3036 exid->spo_must_enforce[1] = (
3037 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3038 1 << (OP_EXCHANGE_ID - 32) |
3039 1 << (OP_CREATE_SESSION - 32) |
3040 1 << (OP_DESTROY_SESSION - 32) |
3041 1 << (OP_DESTROY_CLIENTID - 32));
3042
3043 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3044 1 << (OP_OPEN_DOWNGRADE) |
3045 1 << (OP_LOCKU) |
3046 1 << (OP_DELEGRETURN));
3047
3048 exid->spo_must_allow[1] &= (
3049 1 << (OP_TEST_STATEID - 32) |
3050 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003051 if (!svc_rqst_integrity_protected(rqstp)) {
3052 status = nfserr_inval;
3053 goto out_nolock;
3054 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003055 /*
3056 * Sometimes userspace doesn't give us a principal.
3057 * Which is a bug, really. Anyway, we can't enforce
3058 * MACH_CRED in that case, better to give up now:
3059 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05003060 if (!new->cl_cred.cr_principal &&
3061 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003062 status = nfserr_serverfault;
3063 goto out_nolock;
3064 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003065 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03003066 case SP4_NONE:
3067 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003068 default: /* checked by xdr code */
3069 WARN_ON_ONCE(1);
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06003070 /* fall through */
Andy Adamson0733d212009-04-03 08:28:01 +03003071 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08003072 status = nfserr_encr_alg_unsupp;
3073 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03003074 }
3075
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003076 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003077 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003078 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03003079 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003080 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3081 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3082
J. Bruce Fields136e6582012-05-12 20:37:23 -04003083 if (update) {
3084 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003085 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03003086 goto out;
3087 }
Andrew Elblededeb132016-06-15 12:52:08 -04003088 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003089 status = nfserr_wrong_cred;
3090 goto out;
3091 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003092 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03003093 status = nfserr_perm;
3094 goto out;
3095 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003096 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03003097 status = nfserr_not_same;
3098 goto out;
3099 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003100 /* case 6 */
3101 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003102 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003103 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003104 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003105 if (client_has_state(conf)) {
3106 status = nfserr_clid_inuse;
3107 goto out;
3108 }
Andy Adamson0733d212009-04-03 08:28:01 +03003109 goto out_new;
3110 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003111 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003112 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003113 goto out_copy;
3114 }
3115 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003116 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003117 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003118 }
3119
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003120 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003121 status = nfserr_noent;
3122 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003123 }
3124
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003125 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003126 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003127 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003128
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003129 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03003130out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003131 if (conf) {
3132 status = mark_client_expired_locked(conf);
3133 if (status)
3134 goto out;
3135 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003136 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003137 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3138 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003139
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003140 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003141 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003142out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003143 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3144 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003145
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003146 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3147 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003148
3149 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003150 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003151 status = nfs_ok;
3152
3153out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003154 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003155out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003156 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003157 expire_client(new);
3158 if (unconf)
3159 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003160 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003161}
3162
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003163static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003164check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003165{
Andy Adamson88e588d2009-07-23 19:02:15 -04003166 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3167 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003168
3169 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003170 if (slot_inuse) {
3171 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003172 return nfserr_jukebox;
3173 else
3174 return nfserr_seq_misordered;
3175 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003176 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003177 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003178 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003179 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003180 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003181 return nfserr_seq_misordered;
3182}
3183
Andy Adamson49557cc2009-07-23 19:02:16 -04003184/*
3185 * Cache the create session result into the create session single DRC
3186 * slot cache by saving the xdr structure. sl_seqid has been set.
3187 * Do this for solo or embedded create session operations.
3188 */
3189static void
3190nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003191 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003192{
3193 slot->sl_status = nfserr;
3194 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3195}
3196
3197static __be32
3198nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3199 struct nfsd4_clid_slot *slot)
3200{
3201 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3202 return slot->sl_status;
3203}
3204
Mi Jinlong1b74c252011-07-14 14:50:17 +08003205#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3206 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3207 1 + /* MIN tag is length with zero, only length */ \
3208 3 + /* version, opcount, opcode */ \
3209 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3210 /* seqid, slotID, slotID, cache */ \
3211 4 ) * sizeof(__be32))
3212
3213#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3214 2 + /* verifier: AUTH_NULL, length 0 */\
3215 1 + /* status */ \
3216 1 + /* MIN tag is length with zero, only length */ \
3217 3 + /* opcount, opcode, opstatus*/ \
3218 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3219 /* seqid, slotID, slotID, slotID, status */ \
3220 5 ) * sizeof(__be32))
3221
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003222static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003223{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003224 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3225
J. Bruce Fields373cd402013-04-08 15:42:12 -04003226 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3227 return nfserr_toosmall;
3228 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3229 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003230 ca->headerpadsz = 0;
3231 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3232 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3233 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3234 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3235 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3236 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3237 /*
3238 * Note decreasing slot size below client's request may make it
3239 * difficult for client to function correctly, whereas
3240 * decreasing the number of slots will (just?) affect
3241 * performance. When short on memory we therefore prefer to
3242 * decrease number of slots instead of their size. Clients that
3243 * request larger slots than they need will get poor results:
NeilBrown7f49fd5d2019-09-20 16:33:16 +10003244 * Note that we always allow at least one slot, because our
3245 * accounting is soft and provides no guarantees either way.
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003246 */
NeilBrown2030ca52019-09-20 16:36:45 +10003247 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003248
J. Bruce Fields373cd402013-04-08 15:42:12 -04003249 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003250}
3251
Chuck Lever45006322016-03-01 13:06:02 -05003252/*
3253 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3254 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3255 */
3256#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3257 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3258
3259#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3260 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3261
Kinglong Mee8a891632013-12-23 18:11:02 +08003262#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003263 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003264#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003265 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3266 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003267
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003268static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3269{
3270 ca->headerpadsz = 0;
3271
Kinglong Mee8a891632013-12-23 18:11:02 +08003272 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003273 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003274 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003275 return nfserr_toosmall;
3276 ca->maxresp_cached = 0;
3277 if (ca->maxops < 2)
3278 return nfserr_toosmall;
3279
3280 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003281}
3282
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003283static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3284{
3285 switch (cbs->flavor) {
3286 case RPC_AUTH_NULL:
3287 case RPC_AUTH_UNIX:
3288 return nfs_ok;
3289 default:
3290 /*
3291 * GSS case: the spec doesn't allow us to return this
3292 * error. But it also doesn't allow us not to support
3293 * GSS.
3294 * I'd rather this fail hard than return some error the
3295 * client might think it can already handle:
3296 */
3297 return nfserr_encr_alg_unsupp;
3298 }
3299}
3300
Andy Adamson069b6ad2009-04-03 08:27:58 +03003301__be32
3302nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003303 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003304{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003305 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003306 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003307 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003308 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003309 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003310 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003311 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003312 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003313 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003314
Mi Jinlonga62573d2011-03-23 17:57:07 +08003315 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3316 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003317 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3318 if (status)
3319 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003320 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003321 if (status)
3322 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003323 status = check_backchannel_attrs(&cr_ses->back_channel);
3324 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003325 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003326 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003327 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003328 if (!new)
3329 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003330 conn = alloc_conn_from_crses(rqstp, cr_ses);
3331 if (!conn)
3332 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003333
Jeff Laytond20c11d2014-07-30 08:27:07 -04003334 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003335 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003336 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003337 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003338
3339 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003340 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003341 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003342 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003343 cs_slot = &conf->cl_cs_slot;
3344 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003345 if (status) {
3346 if (status == nfserr_replay_cache)
3347 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003348 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003349 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003350 } else if (unconf) {
3351 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003352 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003353 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003354 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003355 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003356 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003357 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003358 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003359 cs_slot = &unconf->cl_cs_slot;
3360 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003361 if (status) {
3362 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003363 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003364 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003365 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003366 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003367 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003368 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003369 if (status) {
3370 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003371 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003372 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003373 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003374 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003375 conf = unconf;
3376 } else {
3377 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003378 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003379 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003380 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003381 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003382 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003383 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003384 cr_ses->flags &= ~SESSION4_RDMA;
3385
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003386 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003387 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003388
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003389 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003390 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003391 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003392 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003393
Jeff Laytond20c11d2014-07-30 08:27:07 -04003394 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003395 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003396 spin_unlock(&nn->client_lock);
3397 /* init connection and backchannel */
3398 nfsd4_init_conn(rqstp, conn, new);
3399 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003400 if (old)
3401 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003402 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003403out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003404 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003405 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003406 if (old)
3407 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003408out_free_session:
3409 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003410out_release_drc_mem:
3411 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003412 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003413}
3414
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003415static __be32 nfsd4_map_bcts_dir(u32 *dir)
3416{
3417 switch (*dir) {
3418 case NFS4_CDFC4_FORE:
3419 case NFS4_CDFC4_BACK:
3420 return nfs_ok;
3421 case NFS4_CDFC4_FORE_OR_BOTH:
3422 case NFS4_CDFC4_BACK_OR_BOTH:
3423 *dir = NFS4_CDFC4_BOTH;
3424 return nfs_ok;
zhengbinfc5fc5d2019-12-19 17:29:20 +08003425 }
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003426 return nfserr_inval;
3427}
3428
Christoph Hellwigeb698532017-05-08 20:58:35 +02003429__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3430 struct nfsd4_compound_state *cstate,
3431 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003432{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003433 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003434 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003435 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003436 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003437
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003438 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3439 if (status)
3440 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003441 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003442 session->se_cb_prog = bc->bc_cb_program;
3443 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003444 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003445
3446 nfsd4_probe_callback(session->se_client);
3447
3448 return nfs_ok;
3449}
3450
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003451__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3452 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003453 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003454{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003455 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003456 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003457 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003458 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003459 struct net *net = SVC_NET(rqstp);
3460 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003461
3462 if (!nfsd4_last_compound_op(rqstp))
3463 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003464 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003465 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003466 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003467 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003468 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003469 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003470 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003471 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003472 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003473 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003474 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003475 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003476 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003477 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003478 goto out;
3479 nfsd4_init_conn(rqstp, conn, session);
3480 status = nfs_ok;
3481out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003482 nfsd4_put_session(session);
3483out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003484 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003485}
3486
J. Bruce Fields665d5072018-04-11 16:53:36 -04003487static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003488{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003489 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003490 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003491 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003492}
3493
Andy Adamson069b6ad2009-04-03 08:27:58 +03003494__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003495nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3496 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003497{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003498 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003499 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003500 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003501 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003502 struct net *net = SVC_NET(r);
3503 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003504
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003505 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003506 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003507 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003508 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003509 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003510 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003511 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003512 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003513 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003514 if (!ses)
3515 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003516 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003517 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003518 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003519 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003520 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003521 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003522 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003523 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003524
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003525 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003526
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003527 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003528 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003529out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003530 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003531out_client_lock:
3532 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003533out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003534 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003535}
3536
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003537static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003538{
3539 struct nfsd4_conn *c;
3540
3541 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003542 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04003543 return c;
3544 }
3545 }
3546 return NULL;
3547}
3548
J. Bruce Fields57266a62013-04-13 14:27:29 -04003549static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003550{
3551 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003552 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003553 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003554 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003555
3556 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003557 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003558 if (c)
3559 goto out_free;
3560 status = nfserr_conn_not_bound_to_session;
3561 if (clp->cl_mach_cred)
3562 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003563 __nfsd4_hash_conn(new, ses);
3564 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003565 ret = nfsd4_register_conn(new);
3566 if (ret)
3567 /* oops; xprt is already down: */
3568 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003569 return nfs_ok;
3570out_free:
3571 spin_unlock(&clp->cl_lock);
3572 free_conn(new);
3573 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003574}
3575
Mi Jinlong868b89c2011-04-27 09:09:58 +08003576static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3577{
3578 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3579
3580 return args->opcnt > session->se_fchannel.maxops;
3581}
3582
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003583static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3584 struct nfsd4_session *session)
3585{
3586 struct xdr_buf *xb = &rqstp->rq_arg;
3587
3588 return xb->len > session->se_fchannel.maxreq_sz;
3589}
3590
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003591static bool replay_matches_cache(struct svc_rqst *rqstp,
3592 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3593{
3594 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3595
3596 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3597 (bool)seq->cachethis)
3598 return false;
3599 /*
Scott Mayhew6e73e922019-10-09 15:11:37 -04003600 * If there's an error then the reply can have fewer ops than
3601 * the call.
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003602 */
Scott Mayhew6e73e922019-10-09 15:11:37 -04003603 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3604 return false;
3605 /*
3606 * But if we cached a reply with *more* ops than the call you're
3607 * sending us now, then this new call is clearly not really a
3608 * replay of the old one:
3609 */
3610 if (slot->sl_opcnt > argp->opcnt)
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003611 return false;
3612 /* This is the only check explicitly called by spec: */
3613 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3614 return false;
3615 /*
3616 * There may be more comparisons we could actually do, but the
3617 * spec doesn't require us to catch every case where the calls
3618 * don't match (that would require caching the call as well as
3619 * the reply), so we don't bother.
3620 */
3621 return true;
3622}
3623
Andy Adamson069b6ad2009-04-03 08:27:58 +03003624__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003625nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3626 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003627{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003628 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003629 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003630 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003631 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003632 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003633 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003634 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003635 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003636 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003637 struct net *net = SVC_NET(rqstp);
3638 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003639
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003640 if (resp->opcnt != 1)
3641 return nfserr_sequence_pos;
3642
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003643 /*
3644 * Will be either used or freed by nfsd4_sequence_check_conn
3645 * below.
3646 */
3647 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3648 if (!conn)
3649 return nfserr_jukebox;
3650
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003651 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003652 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003653 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003654 goto out_no_session;
3655 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003656
Mi Jinlong868b89c2011-04-27 09:09:58 +08003657 status = nfserr_too_many_ops;
3658 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003659 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003660
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003661 status = nfserr_req_too_big;
3662 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003663 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003664
Benny Halevyb85d4c02009-04-03 08:28:08 +03003665 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003666 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003667 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003668
Andy Adamson557ce262009-08-28 08:45:04 -04003669 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003670 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3671
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003672 /* We do not negotiate the number of slots yet, so set the
3673 * maxslots to the session maxreqs which is used to encode
3674 * sr_highest_slotid and the sr_target_slot id to maxslots */
3675 seq->maxslots = session->se_fchannel.maxreqs;
3676
J. Bruce Fields73e79482012-02-13 16:39:00 -05003677 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3678 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003679 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003680 status = nfserr_seq_misordered;
3681 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003682 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003683 status = nfserr_seq_false_retry;
3684 if (!replay_matches_cache(rqstp, seq, slot))
3685 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003686 cstate->slot = slot;
3687 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003688 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003689 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003690 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003691 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003692 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003693 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003694 }
3695 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003696 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003697
J. Bruce Fields57266a62013-04-13 14:27:29 -04003698 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003699 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003700 if (status)
3701 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003702
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003703 buflen = (seq->cachethis) ?
3704 session->se_fchannel.maxresp_cached :
3705 session->se_fchannel.maxresp_sz;
3706 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3707 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003708 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003709 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003710 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003711
3712 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003713 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003714 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003715 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003716 if (seq->cachethis)
3717 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003718 else
3719 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003720
3721 cstate->slot = slot;
3722 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003723 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003724
Benny Halevyb85d4c02009-04-03 08:28:08 +03003725out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003726 switch (clp->cl_cb_state) {
3727 case NFSD4_CB_DOWN:
3728 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3729 break;
3730 case NFSD4_CB_FAULT:
3731 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3732 break;
3733 default:
3734 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003735 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003736 if (!list_empty(&clp->cl_revoked))
3737 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003738out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003739 if (conn)
3740 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003741 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003742 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003743out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003744 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003745 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003746}
3747
Trond Myklebustb6076642014-06-30 11:48:35 -04003748void
3749nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3750{
3751 struct nfsd4_compound_state *cs = &resp->cstate;
3752
3753 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003754 if (cs->status != nfserr_replay_cache) {
3755 nfsd4_store_cache_entry(resp);
3756 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3757 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003758 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003759 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003760 } else if (cs->clp)
3761 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003762}
3763
Mi Jinlong345c2842011-10-20 17:51:39 +08003764__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003765nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3766 struct nfsd4_compound_state *cstate,
3767 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003768{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003769 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003770 struct nfs4_client *conf, *unconf;
3771 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003772 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003773 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003774
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003775 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003776 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003777 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003778 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003779
3780 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003781 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003782 status = nfserr_clientid_busy;
3783 goto out;
3784 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003785 status = mark_client_expired_locked(conf);
3786 if (status)
3787 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003788 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003789 } else if (unconf)
3790 clp = unconf;
3791 else {
3792 status = nfserr_stale_clientid;
3793 goto out;
3794 }
Andrew Elblededeb132016-06-15 12:52:08 -04003795 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003796 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003797 status = nfserr_wrong_cred;
3798 goto out;
3799 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003800 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003801out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003802 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003803 if (clp)
3804 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003805 return status;
3806}
3807
Andy Adamson069b6ad2009-04-03 08:27:58 +03003808__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003809nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3810 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003811{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003812 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003813 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003814
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003815 if (rc->rca_one_fs) {
3816 if (!cstate->current_fh.fh_dentry)
3817 return nfserr_nofilehandle;
3818 /*
3819 * We don't take advantage of the rca_one_fs case.
3820 * That's OK, it's optional, we can safely ignore it.
3821 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003822 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003823 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003824
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003825 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003826 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3827 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003828 goto out;
3829
3830 status = nfserr_stale_clientid;
3831 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003832 /*
3833 * The following error isn't really legal.
3834 * But we only get here if the client just explicitly
3835 * destroyed the client. Surely it no longer cares what
3836 * error it gets back on an operation for the dead
3837 * client.
3838 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003839 goto out;
3840
3841 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003842 nfsd4_client_record_create(cstate->session->se_client);
Scott Mayhew362063a2019-03-26 18:06:28 -04003843 inc_reclaim_complete(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003844out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003845 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003846}
3847
3848__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003849nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003850 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003852 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003853 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003855 struct nfs4_client *conf, *new;
3856 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003857 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003858 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3859
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003860 new = create_client(clname, rqstp, &clverifier);
3861 if (new == NULL)
3862 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003863 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003864 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003865 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003866 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003867 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003869 if (clp_used_exchangeid(conf))
3870 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003871 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003872 char addr_str[INET6_ADDRSTRLEN];
3873 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3874 sizeof(addr_str));
3875 dprintk("NFSD: setclientid: string in use by client "
3876 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 goto out;
3878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003880 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003881 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003882 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003883 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003884 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003886 gen_confirm(new, nn);
3887 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04003888 ;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003889 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003890 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003891 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3893 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3894 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003895 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 status = nfs_ok;
3897out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003898 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003899 if (new)
3900 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003901 if (unconf)
3902 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 return status;
3904}
3905
3906
Al Virob37ad282006-10-19 23:28:59 -07003907__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003908nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003909 struct nfsd4_compound_state *cstate,
3910 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003912 struct nfsd4_setclientid_confirm *setclientid_confirm =
3913 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003914 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003915 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3917 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003918 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003919 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003921 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003923
Jeff Laytond20c11d2014-07-30 08:27:07 -04003924 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003925 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003926 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003927 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003928 * We try hard to give out unique clientid's, so if we get an
3929 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003930 * the client may be buggy; this should never happen.
3931 *
3932 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003933 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003934 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003935 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3936 goto out;
3937 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3938 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003939 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003940 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003941 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3942 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003944 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003945 status = nfserr_stale_clientid;
3946 goto out;
3947 }
3948 status = nfs_ok;
3949 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003950 old = unconf;
3951 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003952 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003953 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003954 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3955 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003956 status = nfserr_clid_inuse;
3957 if (client_has_state(old)
3958 && !same_creds(&unconf->cl_cred,
3959 &old->cl_cred))
3960 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003961 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003962 if (status) {
3963 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003964 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003965 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003966 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003967 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003968 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003969 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003970 get_client_locked(conf);
3971 spin_unlock(&nn->client_lock);
3972 nfsd4_probe_callback(conf);
3973 spin_lock(&nn->client_lock);
3974 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003976 spin_unlock(&nn->client_lock);
3977 if (old)
3978 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 return status;
3980}
3981
J. Bruce Fields32513b42011-10-13 16:00:16 -04003982static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003984 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3985}
3986
3987/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003988static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3989 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003990{
Trond Myklebust950e0112014-06-30 11:48:31 -04003991 lockdep_assert_held(&state_lock);
3992
Elena Reshetova818a34e2017-10-20 12:53:30 +03003993 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003994 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003995 INIT_LIST_HEAD(&fp->fi_stateids);
3996 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003997 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003998 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003999 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004000 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004001 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004002 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4003 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004004#ifdef CONFIG_NFSD_PNFS
4005 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02004006 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004007#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04004008 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009}
4010
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004011void
NeilBrowne60d4392005-06-23 22:03:01 -07004012nfsd4_free_slabs(void)
4013{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004014 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004015 kmem_cache_destroy(openowner_slab);
4016 kmem_cache_destroy(lockowner_slab);
4017 kmem_cache_destroy(file_slab);
4018 kmem_cache_destroy(stateid_slab);
4019 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004020 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07004021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Bryan Schumaker72083392011-11-01 15:24:59 -04004023int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024nfsd4_init_slabs(void)
4025{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004026 client_slab = kmem_cache_create("nfsd4_clients",
4027 sizeof(struct nfs4_client), 0, 0, NULL);
4028 if (client_slab == NULL)
4029 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004030 openowner_slab = kmem_cache_create("nfsd4_openowners",
4031 sizeof(struct nfs4_openowner), 0, 0, NULL);
4032 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04004033 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004034 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08004035 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004036 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004037 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07004038 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09004039 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07004040 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004041 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07004042 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004043 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07004044 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004045 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07004046 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09004047 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07004048 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004049 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004050 odstate_slab = kmem_cache_create("nfsd4_odstate",
4051 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4052 if (odstate_slab == NULL)
4053 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07004055
Sachin Bhamare8287f002015-04-27 14:50:14 +02004056out_free_deleg_slab:
4057 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004058out_free_stateid_slab:
4059 kmem_cache_destroy(stateid_slab);
4060out_free_file_slab:
4061 kmem_cache_destroy(file_slab);
4062out_free_lockowner_slab:
4063 kmem_cache_destroy(lockowner_slab);
4064out_free_openowner_slab:
4065 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004066out_free_client_slab:
4067 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004068out:
NeilBrowne60d4392005-06-23 22:03:01 -07004069 dprintk("nfsd4: out of memory while initializing nfsv4\n");
4070 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071}
4072
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004073static void init_nfs4_replay(struct nfs4_replay *rp)
4074{
4075 rp->rp_status = nfserr_serverfault;
4076 rp->rp_buflen = 0;
4077 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04004078 mutex_init(&rp->rp_mutex);
4079}
4080
4081static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4082 struct nfs4_stateowner *so)
4083{
4084 if (!nfsd4_has_session(cstate)) {
4085 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004086 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004087 }
4088}
4089
4090void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4091{
4092 struct nfs4_stateowner *so = cstate->replay_owner;
4093
4094 if (so != NULL) {
4095 cstate->replay_owner = NULL;
4096 mutex_unlock(&so->so_replay.rp_mutex);
4097 nfs4_put_stateowner(so);
4098 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004099}
4100
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004101static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102{
4103 struct nfs4_stateowner *sop;
4104
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004105 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004106 if (!sop)
4107 return NULL;
4108
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04004109 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004110 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004111 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004112 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004114
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004115 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004116 sop->so_client = clp;
4117 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04004118 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004119 return sop;
4120}
4121
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004122static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004123{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004124 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004125
Trond Myklebustd4f04892014-07-29 21:34:36 -04004126 list_add(&oo->oo_owner.so_strhash,
4127 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004128 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129}
4130
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004131static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4132{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004133 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004134}
4135
Jeff Layton6b180f02014-07-29 21:34:26 -04004136static void nfs4_free_openowner(struct nfs4_stateowner *so)
4137{
4138 struct nfs4_openowner *oo = openowner(so);
4139
4140 kmem_cache_free(openowner_slab, oo);
4141}
4142
4143static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004144 .so_unhash = nfs4_unhash_openowner,
4145 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004146};
4147
Andrew Elble7fc05642015-11-05 20:42:43 -05004148static struct nfs4_ol_stateid *
4149nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4150{
4151 struct nfs4_ol_stateid *local, *ret = NULL;
4152 struct nfs4_openowner *oo = open->op_openowner;
4153
4154 lockdep_assert_held(&fp->fi_lock);
4155
4156 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4157 /* ignore lock owners */
4158 if (local->st_stateowner->so_is_open_owner == 0)
4159 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004160 if (local->st_stateowner != &oo->oo_owner)
4161 continue;
4162 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004163 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004164 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05004165 break;
4166 }
4167 }
4168 return ret;
4169}
4170
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004171static __be32
4172nfsd4_verify_open_stid(struct nfs4_stid *s)
4173{
4174 __be32 ret = nfs_ok;
4175
4176 switch (s->sc_type) {
4177 default:
4178 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004179 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004180 case NFS4_CLOSED_STID:
4181 case NFS4_CLOSED_DELEG_STID:
4182 ret = nfserr_bad_stateid;
4183 break;
4184 case NFS4_REVOKED_DELEG_STID:
4185 ret = nfserr_deleg_revoked;
4186 }
4187 return ret;
4188}
4189
4190/* Lock the stateid st_mutex, and deal with races with CLOSE */
4191static __be32
4192nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4193{
4194 __be32 ret;
4195
Andrew Elble4f34bd02017-11-08 17:29:51 -05004196 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004197 ret = nfsd4_verify_open_stid(&stp->st_stid);
4198 if (ret != nfs_ok)
4199 mutex_unlock(&stp->st_mutex);
4200 return ret;
4201}
4202
4203static struct nfs4_ol_stateid *
4204nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4205{
4206 struct nfs4_ol_stateid *stp;
4207 for (;;) {
4208 spin_lock(&fp->fi_lock);
4209 stp = nfsd4_find_existing_open(fp, open);
4210 spin_unlock(&fp->fi_lock);
4211 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4212 break;
4213 nfs4_put_stid(&stp->st_stid);
4214 }
4215 return stp;
4216}
4217
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004218static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004219alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004220 struct nfsd4_compound_state *cstate)
4221{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004222 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004223 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004225 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4226 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004228 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004229 oo->oo_owner.so_is_open_owner = 1;
4230 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004231 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004232 if (nfsd4_has_session(cstate))
4233 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004234 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004235 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004236 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004237 spin_lock(&clp->cl_lock);
4238 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004239 if (ret == NULL) {
4240 hash_openowner(oo, clp, strhashval);
4241 ret = oo;
4242 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004243 nfs4_free_stateowner(&oo->oo_owner);
4244
Trond Myklebustd4f04892014-07-29 21:34:36 -04004245 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004246 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247}
4248
Andrew Elble7fc05642015-11-05 20:42:43 -05004249static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004250init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004251{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
Andrew Elble7fc05642015-11-05 20:42:43 -05004253 struct nfs4_openowner *oo = open->op_openowner;
4254 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004255 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004256
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004257 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004258 /* We are moving these outside of the spinlocks to avoid the warnings */
4259 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004260 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004261
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004262retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004263 spin_lock(&oo->oo_owner.so_client->cl_lock);
4264 spin_lock(&fp->fi_lock);
4265
4266 retstp = nfsd4_find_existing_open(fp, open);
4267 if (retstp)
4268 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004269
4270 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004271 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004272 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004273 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004274 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004275 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004276 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 stp->st_access_bmap = 0;
4278 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004279 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004280 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004281 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004282
4283out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004284 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004285 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004286 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004287 /* Handle races with CLOSE */
4288 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4289 nfs4_put_stid(&retstp->st_stid);
4290 goto retry;
4291 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004292 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004293 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004294 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004295 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004296 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297}
4298
Jeff Laytond3134b12014-07-29 21:34:32 -04004299/*
4300 * In the 4.0 case we need to keep the owners around a little while to handle
4301 * CLOSE replay. We still do need to release any file access that is held by
4302 * them before returning however.
4303 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004305move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306{
Jeff Layton217526e2014-07-30 08:27:11 -04004307 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004308 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4309 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4310 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004311
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004312 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Jeff Laytonb401be222014-07-29 21:34:33 -04004314 /*
4315 * We know that we hold one reference via nfsd4_close, and another
4316 * "persistent" reference for the client. If the refcount is higher
4317 * than 2, then there are still calls in progress that are using this
4318 * stateid. We can't put the sc_file reference until they are finished.
4319 * Wait for the refcount to drop to 2. Since it has been unhashed,
4320 * there should be no danger of the refcount going back up again at
4321 * this point.
4322 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004323 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004324
Jeff Laytond3134b12014-07-29 21:34:32 -04004325 release_all_access(s);
4326 if (s->st_stid.sc_file) {
4327 put_nfs4_file(s->st_stid.sc_file);
4328 s->st_stid.sc_file = NULL;
4329 }
Jeff Layton217526e2014-07-30 08:27:11 -04004330
4331 spin_lock(&nn->client_lock);
4332 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004333 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004334 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004335 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004336 spin_unlock(&nn->client_lock);
4337 if (last)
4338 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339}
4340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341/* search file_hashtbl[] for file */
4342static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04004343find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 struct nfs4_file *fp;
4346
Jeff Layton5b095e92014-10-23 08:01:02 -04004347 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02004348 if (fh_match(&fp->fi_fhandle, fh)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004349 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004350 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 }
4353 return NULL;
4354}
4355
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02004356struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04004357find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004358{
4359 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004360 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004361
Jeff Layton5b095e92014-10-23 08:01:02 -04004362 rcu_read_lock();
4363 fp = find_file_locked(fh, hashval);
4364 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004365 return fp;
4366}
4367
4368static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004369find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004370{
4371 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004372 unsigned int hashval = file_hashval(fh);
4373
4374 rcu_read_lock();
4375 fp = find_file_locked(fh, hashval);
4376 rcu_read_unlock();
4377 if (fp)
4378 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004379
4380 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04004381 fp = find_file_locked(fh, hashval);
4382 if (likely(fp == NULL)) {
4383 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04004384 fp = new;
4385 }
4386 spin_unlock(&state_lock);
4387
4388 return fp;
4389}
4390
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004391/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 * Called to check deny when READ with all zero stateid or
4393 * WRITE with all zero or all one stateid
4394 */
Al Virob37ad282006-10-19 23:28:59 -07004395static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4397{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004399 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Trond Myklebustca943212014-07-23 16:17:39 -04004401 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07004402 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004403 return ret;
4404 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004405 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004406 if (fp->fi_share_deny & deny_type)
4407 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004408 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004409 put_nfs4_file(fp);
4410 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411}
4412
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004413static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004415 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004416 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4417 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004418
Trond Myklebust11b91642014-07-29 21:34:08 -04004419 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004420
Jeff Layton02e12152014-07-16 10:31:57 -04004421 /*
4422 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004423 * already holding inode->i_lock.
4424 *
Jeff Laytondff13992014-07-08 14:02:49 -04004425 * If the dl_time != 0, then we know that it has already been
4426 * queued for a lease break. Don't queue it again.
4427 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004428 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004429 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04004430 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004431 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004432 }
Jeff Layton02e12152014-07-16 10:31:57 -04004433 spin_unlock(&state_lock);
4434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004436static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4437 struct rpc_task *task)
4438{
4439 struct nfs4_delegation *dp = cb_to_delegation(cb);
4440
Andrew Elblea4579742015-08-31 12:06:41 -04004441 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
4442 return 1;
4443
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004444 switch (task->tk_status) {
4445 case 0:
4446 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004447 case -NFS4ERR_DELAY:
4448 rpc_delay(task, 2 * HZ);
4449 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004450 case -EBADHANDLE:
4451 case -NFS4ERR_BAD_STATEID:
4452 /*
4453 * Race: client probably got cb_recall before open reply
4454 * granting delegation.
4455 */
4456 if (dp->dl_retries--) {
4457 rpc_delay(task, 2 * HZ);
4458 return 0;
4459 }
4460 /*FALLTHRU*/
4461 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004462 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004463 }
4464}
4465
4466static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4467{
4468 struct nfs4_delegation *dp = cb_to_delegation(cb);
4469
4470 nfs4_put_stid(&dp->dl_stid);
4471}
4472
Julia Lawallc4cb8972015-11-21 22:57:39 +01004473static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004474 .prepare = nfsd4_cb_recall_prepare,
4475 .done = nfsd4_cb_recall_done,
4476 .release = nfsd4_cb_recall_release,
4477};
4478
Jeff Layton02e12152014-07-16 10:31:57 -04004479static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4480{
4481 /*
4482 * We're assuming the state code never drops its reference
4483 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004484 * callback (and since the lease code is serialized by the
4485 * i_lock) we know the server hasn't removed the lease yet, and
4486 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004487 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004488 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004489 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004490}
4491
Jeff Layton1c8c6012013-06-21 08:58:15 -04004492/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004493static bool
4494nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004495{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004496 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004497 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4498 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004499
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004500 /*
4501 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004502 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004503 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004504 */
4505 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
Jeff Layton02e12152014-07-16 10:31:57 -04004507 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004508 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004509 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004510 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004511 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512}
4513
Jeff Laytonc45198e2014-09-01 07:12:07 -04004514static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004515nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4516 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517{
4518 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004519 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 else
4521 return -EAGAIN;
4522}
4523
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004524static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004525 .lm_break = nfsd_break_deleg_cb,
4526 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527};
4528
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004529static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4530{
4531 if (nfsd4_has_session(cstate))
4532 return nfs_ok;
4533 if (seqid == so->so_seqid - 1)
4534 return nfserr_replay_me;
4535 if (seqid == so->so_seqid)
4536 return nfs_ok;
4537 return nfserr_bad_seqid;
4538}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Jeff Layton4b24ca72014-06-30 11:48:44 -04004540static __be32 lookup_clientid(clientid_t *clid,
4541 struct nfsd4_compound_state *cstate,
Olga Kornievskaiab7342202019-09-06 15:17:21 -04004542 struct nfsd_net *nn,
4543 bool sessions)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004544{
4545 struct nfs4_client *found;
4546
4547 if (cstate->clp) {
4548 found = cstate->clp;
4549 if (!same_clid(&found->cl_clientid, clid))
4550 return nfserr_stale_clientid;
4551 return nfs_ok;
4552 }
4553
4554 if (STALE_CLIENTID(clid, nn))
4555 return nfserr_stale_clientid;
4556
4557 /*
4558 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4559 * cached already then we know this is for is for v4.0 and "sessions"
4560 * will be false.
4561 */
4562 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004563 spin_lock(&nn->client_lock);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04004564 found = find_confirmed_client(clid, sessions, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004565 if (!found) {
4566 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004567 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004568 }
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04004569 atomic_inc(&found->cl_rpc_users);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004570 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004571
4572 /* Cache the nfs4_client in cstate! */
4573 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004574 return nfs_ok;
4575}
4576
Al Virob37ad282006-10-19 23:28:59 -07004577__be32
Andy Adamson66689582009-04-03 08:28:45 +03004578nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004579 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 clientid_t *clientid = &open->op_clientid;
4582 struct nfs4_client *clp = NULL;
4583 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004584 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004585 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004587 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004589 /*
4590 * In case we need it later, after we've already created the
4591 * file and don't want to risk a further failure:
4592 */
4593 open->op_file = nfsd4_alloc_file();
4594 if (open->op_file == NULL)
4595 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
Olga Kornievskaiab7342202019-09-06 15:17:21 -04004597 status = lookup_clientid(clientid, cstate, nn, false);
Trond Myklebust2d91e892014-06-30 11:48:46 -04004598 if (status)
4599 return status;
4600 clp = cstate->clp;
4601
Trond Myklebustd4f04892014-07-29 21:34:36 -04004602 strhashval = ownerstr_hashval(&open->op_owner);
4603 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004604 open->op_openowner = oo;
4605 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004606 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004608 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004609 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004610 release_openowner(oo);
4611 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004612 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004613 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004614 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4615 if (status)
4616 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004617 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004618new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004619 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004620 if (oo == NULL)
4621 return nfserr_jukebox;
4622 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004623alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004624 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004625 if (!open->op_stp)
4626 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004627
4628 if (nfsd4_has_session(cstate) &&
4629 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4630 open->op_odstate = alloc_clnt_odstate(clp);
4631 if (!open->op_odstate)
4632 return nfserr_jukebox;
4633 }
4634
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004635 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636}
4637
Al Virob37ad282006-10-19 23:28:59 -07004638static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004639nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4640{
4641 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4642 return nfserr_openmode;
4643 else
4644 return nfs_ok;
4645}
4646
Daniel Mackc47d8322011-05-16 16:38:14 +02004647static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004648{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004649 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4650}
4651
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004652static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004653{
4654 struct nfs4_stid *ret;
4655
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004656 ret = find_stateid_by_type(cl, s,
4657 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004658 if (!ret)
4659 return NULL;
4660 return delegstateid(ret);
4661}
4662
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004663static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4664{
4665 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4666 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4667}
4668
Al Virob37ad282006-10-19 23:28:59 -07004669static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004670nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004671 struct nfs4_delegation **dp)
4672{
4673 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004674 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004675 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004676
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004677 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4678 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004679 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004680 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4681 nfs4_put_stid(&deleg->dl_stid);
4682 if (cl->cl_minorversion)
4683 status = nfserr_deleg_revoked;
4684 goto out;
4685 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004686 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004687 status = nfs4_check_delegmode(deleg, flags);
4688 if (status) {
4689 nfs4_put_stid(&deleg->dl_stid);
4690 goto out;
4691 }
4692 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004693out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004694 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004695 return nfs_ok;
4696 if (status)
4697 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004698 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004699 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004700}
4701
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004702static inline int nfs4_access_to_access(u32 nfs4_access)
4703{
4704 int flags = 0;
4705
4706 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4707 flags |= NFSD_MAY_READ;
4708 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4709 flags |= NFSD_MAY_WRITE;
4710 return flags;
4711}
4712
Al Virob37ad282006-10-19 23:28:59 -07004713static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4715 struct nfsd4_open *open)
4716{
4717 struct iattr iattr = {
4718 .ia_valid = ATTR_SIZE,
4719 .ia_size = 0,
4720 };
4721 if (!open->op_truncate)
4722 return 0;
4723 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004724 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4726}
4727
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004728static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004729 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4730 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004731{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004732 struct nfsd_file *nf = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004733 __be32 status;
4734 int oflag = nfs4_access_to_omode(open->op_share_access);
4735 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004736 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004737
Trond Myklebustde186432014-07-10 14:07:26 -04004738 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004739
4740 /*
4741 * Are we trying to set a deny mode that would conflict with
4742 * current access?
4743 */
4744 status = nfs4_file_check_deny(fp, open->op_share_deny);
4745 if (status != nfs_ok) {
4746 spin_unlock(&fp->fi_lock);
4747 goto out;
4748 }
4749
4750 /* set access to the file */
4751 status = nfs4_file_get_access(fp, open->op_share_access);
4752 if (status != nfs_ok) {
4753 spin_unlock(&fp->fi_lock);
4754 goto out;
4755 }
4756
4757 /* Set access bits in stateid */
4758 old_access_bmap = stp->st_access_bmap;
4759 set_access(open->op_share_access, stp);
4760
4761 /* Set new deny mask */
4762 old_deny_bmap = stp->st_deny_bmap;
4763 set_deny(open->op_share_deny, stp);
4764 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4765
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004766 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004767 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004768 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004769 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004770 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004771 spin_lock(&fp->fi_lock);
4772 if (!fp->fi_fds[oflag]) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004773 fp->fi_fds[oflag] = nf;
4774 nf = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -04004775 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004776 }
Trond Myklebustde186432014-07-10 14:07:26 -04004777 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004778 if (nf)
4779 nfsd_file_put(nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004780
4781 status = nfsd4_truncate(rqstp, cur_fh, open);
4782 if (status)
4783 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004784out:
4785 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004786out_put_access:
4787 stp->st_access_bmap = old_access_bmap;
4788 nfs4_file_put_access(fp, open->op_share_access);
4789 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4790 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004791}
4792
Al Virob37ad282006-10-19 23:28:59 -07004793static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004794nfs4_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 -07004795{
Al Virob37ad282006-10-19 23:28:59 -07004796 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004797 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004799 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004800 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004801
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004802 /* test and set deny mode */
4803 spin_lock(&fp->fi_lock);
4804 status = nfs4_file_check_deny(fp, open->op_share_deny);
4805 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004806 set_deny(open->op_share_deny, stp);
4807 fp->fi_share_deny |=
4808 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4809 }
4810 spin_unlock(&fp->fi_lock);
4811
4812 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004815 status = nfsd4_truncate(rqstp, cur_fh, open);
4816 if (status != nfs_ok)
4817 reset_union_bmap_deny(old_deny_bmap, stp);
4818 return status;
4819}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004821/* Should we give out recallable state?: */
4822static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4823{
4824 if (clp->cl_cb_state == NFSD4_CB_UP)
4825 return true;
4826 /*
4827 * In the sessions case, since we don't have to establish a
4828 * separate connection for callbacks, we assume it's OK
4829 * until we hear otherwise:
4830 */
4831 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4832}
4833
J. Bruce Fields653e5142018-02-15 22:08:45 -05004834static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4835 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004836{
4837 struct file_lock *fl;
4838
4839 fl = locks_alloc_lock();
4840 if (!fl)
4841 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004842 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004843 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004844 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4845 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004846 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004847 fl->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004848 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004849 return fl;
4850}
4851
Jeff Layton0b266932014-07-25 07:34:25 -04004852static struct nfs4_delegation *
4853nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004854 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004855{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004856 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004857 struct nfs4_delegation *dp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004858 struct nfsd_file *nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004859 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004860
J. Bruce Fields353601e2018-02-16 14:29:42 -05004861 /*
4862 * The fi_had_conflict and nfs_get_existing_delegation checks
4863 * here are just optimizations; we'll need to recheck them at
4864 * the end:
4865 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004866 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004867 return ERR_PTR(-EAGAIN);
4868
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004869 nf = find_readable_file(fp);
4870 if (!nf) {
J. Bruce Fields353601e2018-02-16 14:29:42 -05004871 /* We should always have a readable file here */
4872 WARN_ON_ONCE(1);
4873 return ERR_PTR(-EBADF);
4874 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004875 spin_lock(&state_lock);
4876 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004877 if (nfs4_delegation_exists(clp, fp))
4878 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004879 else if (!fp->fi_deleg_file) {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004880 fp->fi_deleg_file = nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004881 /* increment early to prevent fi_deleg_file from being
4882 * cleared */
4883 fp->fi_delegees = 1;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004884 nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004885 } else
4886 fp->fi_delegees++;
4887 spin_unlock(&fp->fi_lock);
4888 spin_unlock(&state_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004889 if (nf)
4890 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004891 if (status)
4892 return ERR_PTR(status);
4893
4894 status = -ENOMEM;
4895 dp = alloc_init_deleg(clp, fp, fh, odstate);
4896 if (!dp)
4897 goto out_delegees;
4898
4899 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4900 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04004901 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004902
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004903 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004904 if (fl)
4905 locks_free_lock(fl);
4906 if (status)
4907 goto out_clnt_odstate;
4908
4909 spin_lock(&state_lock);
4910 spin_lock(&fp->fi_lock);
4911 if (fp->fi_had_conflict)
4912 status = -EAGAIN;
4913 else
4914 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04004915 spin_unlock(&fp->fi_lock);
4916 spin_unlock(&state_lock);
4917
4918 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04004919 goto out_unlock;
4920
Jeff Layton0b266932014-07-25 07:34:25 -04004921 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04004922out_unlock:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004923 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004924out_clnt_odstate:
4925 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004926 nfs4_put_stid(&dp->dl_stid);
4927out_delegees:
4928 put_deleg_file(fp);
4929 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004930}
4931
Benny Halevy4aa89132012-02-21 14:16:44 -08004932static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4933{
4934 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4935 if (status == -EAGAIN)
4936 open->op_why_no_deleg = WND4_CONTENTION;
4937 else {
4938 open->op_why_no_deleg = WND4_RESOURCE;
4939 switch (open->op_deleg_want) {
4940 case NFS4_SHARE_WANT_READ_DELEG:
4941 case NFS4_SHARE_WANT_WRITE_DELEG:
4942 case NFS4_SHARE_WANT_ANY_DELEG:
4943 break;
4944 case NFS4_SHARE_WANT_CANCEL:
4945 open->op_why_no_deleg = WND4_CANCELLED;
4946 break;
4947 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004948 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004949 }
4950 }
4951}
4952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953/*
4954 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004955 *
4956 * Note we don't support write delegations, and won't until the vfs has
4957 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 */
4959static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004960nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4961 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962{
4963 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004964 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4965 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004966 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004967 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004969 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004970 open->op_recall = 0;
4971 switch (open->op_claim_type) {
4972 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004973 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004974 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004975 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4976 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004977 break;
4978 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004979 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004980 /*
4981 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004982 * had the chance to reclaim theirs, *and* until
4983 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004984 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004985 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004986 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004987 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004988 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004989 /*
4990 * Also, if the file was opened for write or
4991 * create, there's a good chance the client's
4992 * about to write to it, resulting in an
4993 * immediate recall (since we don't support
4994 * write delegations):
4995 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004996 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004997 goto out_no_deleg;
4998 if (open->op_create == NFS4_OPEN_CREATE)
4999 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005000 break;
5001 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005002 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005003 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005004 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04005005 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005006 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005008 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005010 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005011 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04005012 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04005013 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005014 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005015out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005016 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5017 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005018 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04005019 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005020 open->op_recall = 1;
5021 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04005022
5023 /* 4.1 client asking for a delegation? */
5024 if (open->op_deleg_want)
5025 nfsd4_open_deleg_none_ext(open, status);
5026 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027}
5028
Benny Halevye27f49c2012-02-21 14:16:54 -08005029static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5030 struct nfs4_delegation *dp)
5031{
5032 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5033 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5034 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5035 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5036 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_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_UPGRADE;
5040 }
5041 /* Otherwise the client must be confused wanting a delegation
5042 * it already has, therefore we don't return
5043 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5044 */
5045}
5046
Al Virob37ad282006-10-19 23:28:59 -07005047__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5049{
Andy Adamson66689582009-04-03 08:28:45 +03005050 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005051 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005053 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07005054 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005055 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005056 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 /*
5059 * Lookup file; if found, lookup stateid and check open request,
5060 * and check for delegations in the process of being recalled.
5061 * If not found, create the nfs4_file struct
5062 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005063 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04005064 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04005065 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07005066 if (status)
5067 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005068 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04005070 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07005071 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04005072 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07005073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 }
5075
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005076 if (!stp) {
5077 stp = init_open_stateid(fp, open);
5078 if (!open->op_stp)
5079 new_stp = true;
5080 }
5081
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 /*
5083 * OPEN the file, or upgrade an existing OPEN.
5084 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005085 *
5086 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005088 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005090 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005091 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005092 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005096 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5097 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005098 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005099 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005100 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005101 goto out;
5102 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005103
5104 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5105 open->op_odstate);
5106 if (stp->st_clnt_odstate == open->op_odstate)
5107 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005109
Jeff Layton9767feb2015-10-01 09:05:50 -04005110 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005111 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
Benny Halevyd24433c2012-02-16 20:57:17 +02005113 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005114 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5115 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5116 open->op_why_no_deleg = WND4_NOT_WANTED;
5117 goto nodeleg;
5118 }
5119 }
5120
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 /*
5122 * Attempt to hand out a delegation. No error return, because the
5123 * OPEN succeeds even if we fail.
5124 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005125 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005126nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 status = nfs_ok;
5128
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005129 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005130 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005132 /* 4.1 client trying to upgrade/downgrade delegation? */
5133 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005134 open->op_deleg_want)
5135 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005136
NeilBrown13cd2182005-06-23 22:03:10 -07005137 if (fp)
5138 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005139 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005140 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 /*
5142 * To finish the open response, we just need to set the rflags.
5143 */
5144 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005145 if (nfsd4_has_session(&resp->cstate))
5146 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5147 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005149
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005150 if (dp)
5151 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005152 if (stp)
5153 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154
5155 return status;
5156}
5157
Jeff Layton58fb12e2014-07-29 21:34:27 -04005158void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005159 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005160{
5161 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005162 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005163
Jeff Laytond3134b12014-07-29 21:34:32 -04005164 nfsd4_cstate_assign_replay(cstate, so);
5165 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005166 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005167 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005168 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005169 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005170 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005171 if (open->op_odstate)
5172 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005173}
5174
Al Virob37ad282006-10-19 23:28:59 -07005175__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005176nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005177 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005179 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005181 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005182 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 dprintk("process_renew(%08x/%08x): starting\n",
5185 clid->cl_boot, clid->cl_id);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005186 status = lookup_clientid(clid, cstate, nn, false);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005187 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005189 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07005191 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005192 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 goto out;
5194 status = nfs_ok;
5195out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 return status;
5197}
5198
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005199void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005200nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005201{
Jeff Layton33dcc482012-04-10 11:08:48 -04005202 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005203 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005204 return;
5205
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005206 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005207 /*
5208 * If the server goes down again right now, an NFSv4
5209 * client will still be allowed to reclaim after it comes back up,
5210 * even if it hasn't yet had a chance to reclaim state this time.
5211 *
5212 */
Jeff Layton919b8042014-09-12 16:40:20 -04005213 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005214 /*
5215 * At this point, NFSv4 clients can still reclaim. But if the
5216 * server crashes, any that have not yet reclaimed will be out
5217 * of luck on the next boot.
5218 *
5219 * (NFSv4.1+ clients are considered to have reclaimed once they
5220 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5221 * have reclaimed after their first OPEN.)
5222 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005223 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005224 /*
5225 * At this point, and once lockd and/or any other containers
5226 * exit their grace period, further reclaims will fail and
5227 * regular locking can resume.
5228 */
NeilBrowna76b4312005-06-23 22:04:01 -07005229}
5230
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005231/*
5232 * If we've waited a lease period but there are still clients trying to
5233 * reclaim, wait a little longer to give them a chance to finish.
5234 */
5235static bool clients_still_reclaiming(struct nfsd_net *nn)
5236{
5237 unsigned long now = get_seconds();
5238 unsigned long double_grace_period_end = nn->boot_time +
5239 2 * nn->nfsd4_lease;
5240
Scott Mayhew362063a2019-03-26 18:06:28 -04005241 if (nn->track_reclaim_completes &&
5242 atomic_read(&nn->nr_reclaim_complete) ==
5243 nn->reclaim_str_hashtbl_size)
5244 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005245 if (!nn->somebody_reclaimed)
5246 return false;
5247 nn->somebody_reclaimed = false;
5248 /*
5249 * If we've given them *two* lease times to reclaim, and they're
5250 * still not done, give up:
5251 */
5252 if (time_after(now, double_grace_period_end))
5253 return false;
5254 return true;
5255}
5256
NeilBrownfd39ca92005-06-23 22:04:03 -07005257static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005258nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259{
5260 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005261 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005263 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005264 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03005266 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04005267 time_t t, new_timeo = nn->nfsd4_lease;
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005268 struct nfs4_cpntf_state *cps;
5269 copy_stateid_t *cps_t;
5270 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005273
5274 if (clients_still_reclaiming(nn)) {
5275 new_timeo = 0;
5276 goto out;
5277 }
Scott Mayhew362063a2019-03-26 18:06:28 -04005278 dprintk("NFSD: end of grace period\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005279 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005280 INIT_LIST_HEAD(&reaplist);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005281
5282 spin_lock(&nn->s2s_cp_lock);
5283 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5284 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5285 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
5286 !time_after((unsigned long)cps->cpntf_time,
5287 (unsigned long)cutoff))
5288 _free_cpntf_state_locked(nn, cps);
5289 }
5290 spin_unlock(&nn->s2s_cp_lock);
5291
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005292 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005293 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 clp = list_entry(pos, struct nfs4_client, cl_lru);
5295 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
5296 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04005297 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 break;
5299 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04005300 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03005301 dprintk("NFSD: client in use (clientid %08x)\n",
5302 clp->cl_clientid.cl_id);
5303 continue;
5304 }
Trond Myklebust4864af92014-07-30 08:27:03 -04005305 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005306 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005307 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005308 list_for_each_safe(pos, next, &reaplist) {
5309 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 dprintk("NFSD: purging unused client (clientid %08x)\n",
5311 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04005312 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 expire_client(clp);
5314 }
Benny Halevycdc975052014-05-30 09:09:30 -04005315 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005316 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5318 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005319 t = dp->dl_time - cutoff;
5320 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 break;
5322 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005323 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005324 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 }
Benny Halevycdc975052014-05-30 09:09:30 -04005326 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005327 while (!list_empty(&reaplist)) {
5328 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5329 dl_recall_lru);
5330 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005331 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 }
Jeff Layton217526e2014-07-30 08:27:11 -04005333
5334 spin_lock(&nn->client_lock);
5335 while (!list_empty(&nn->close_lru)) {
5336 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5337 oo_close_lru);
5338 if (time_after((unsigned long)oo->oo_time,
5339 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005340 t = oo->oo_time - cutoff;
5341 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 break;
5343 }
Jeff Layton217526e2014-07-30 08:27:11 -04005344 list_del_init(&oo->oo_close_lru);
5345 stp = oo->oo_last_closed_stid;
5346 oo->oo_last_closed_stid = NULL;
5347 spin_unlock(&nn->client_lock);
5348 nfs4_put_stid(&stp->st_stid);
5349 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 }
Jeff Layton217526e2014-07-30 08:27:11 -04005351 spin_unlock(&nn->client_lock);
5352
Jeff Layton7919d0a2016-09-16 16:28:25 -04005353 /*
5354 * It's possible for a client to try and acquire an already held lock
5355 * that is being held for a long time, and then lose interest in it.
5356 * So, we clean out any un-revisited request after a lease period
5357 * under the assumption that the client is no longer interested.
5358 *
5359 * RFC5661, sec. 9.6 states that the client must not rely on getting
5360 * notifications and must continue to poll for locks, even when the
5361 * server supports them. Thus this shouldn't lead to clients blocking
5362 * indefinitely once the lock does become free.
5363 */
5364 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005365 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005366 while (!list_empty(&nn->blocked_locks_lru)) {
5367 nbl = list_first_entry(&nn->blocked_locks_lru,
5368 struct nfsd4_blocked_lock, nbl_lru);
5369 if (time_after((unsigned long)nbl->nbl_time,
5370 (unsigned long)cutoff)) {
5371 t = nbl->nbl_time - cutoff;
5372 new_timeo = min(new_timeo, t);
5373 break;
5374 }
5375 list_move(&nbl->nbl_lru, &reaplist);
5376 list_del_init(&nbl->nbl_list);
5377 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005378 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005379
5380 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005381 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005382 struct nfsd4_blocked_lock, nbl_lru);
5383 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005384 free_blocked_lock(nbl);
5385 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005386out:
Jeff Laytona832e7a2014-05-30 09:09:26 -04005387 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04005388 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389}
5390
Harvey Harrisona254b242008-02-20 12:49:00 -08005391static struct workqueue_struct *laundry_wq;
5392static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005393
5394static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005395laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396{
5397 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005398 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005399 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5400 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005402 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005404 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405}
5406
Jeff Layton8fcd4612015-07-30 06:57:46 -04005407static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005408{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005409 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005410 return nfserr_bad_stateid;
5411 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412}
5413
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005415access_permit_read(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_READ, stp) ||
5418 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5419 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420}
5421
5422static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005423access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005425 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5426 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427}
5428
5429static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005430__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431{
Al Virob37ad282006-10-19 23:28:59 -07005432 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433
J. Bruce Fields02921912010-07-29 15:16:59 -04005434 /* For lock stateid's, we test the parent open, not the lock: */
5435 if (stp->st_openstp)
5436 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005437 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005439 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 goto out;
5441 status = nfs_ok;
5442out:
5443 return status;
5444}
5445
Al Virob37ad282006-10-19 23:28:59 -07005446static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005447check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005449 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005451 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005452 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 * conflicting state; so we must wait out the grace period. */
5454 return nfserr_grace;
5455 } else if (flags & WR_STATE)
5456 return nfs4_share_conflict(current_fh,
5457 NFS4_SHARE_DENY_WRITE);
5458 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5459 return nfs4_share_conflict(current_fh,
5460 NFS4_SHARE_DENY_READ);
5461}
5462
5463/*
5464 * Allow READ/WRITE during grace period on recovered state only for files
5465 * that are not able to provide mandatory locking.
5466 */
5467static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005468grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005470 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471}
5472
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005473static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005474{
Andy Adamson66689582009-04-03 08:28:45 +03005475 /*
5476 * When sessions are used the stateid generation number is ignored
5477 * when it is zero.
5478 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005479 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005480 return nfs_ok;
5481
5482 if (in->si_generation == ref->si_generation)
5483 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005484
J. Bruce Fields0836f582008-01-26 19:08:12 -05005485 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005486 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005487 return nfserr_bad_stateid;
5488 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005489 * However, we could see a stateid from the past, even from a
5490 * non-buggy client. For example, if the client sends a lock
5491 * while some IO is outstanding, the lock may bump si_generation
5492 * while the IO is still in flight. The client could avoid that
5493 * situation by waiting for responses on all the IO requests,
5494 * but better performance may result in retrying IO that
5495 * receives an old_stateid error if requests are rarely
5496 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005497 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005498 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005499}
5500
Trond Myklebust03da3162017-11-03 08:00:16 -04005501static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5502{
5503 __be32 ret;
5504
5505 spin_lock(&s->sc_lock);
5506 ret = nfsd4_verify_open_stid(s);
5507 if (ret == nfs_ok)
5508 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5509 spin_unlock(&s->sc_lock);
5510 return ret;
5511}
5512
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005513static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5514{
5515 if (ols->st_stateowner->so_is_open_owner &&
5516 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5517 return nfserr_bad_stateid;
5518 return nfs_ok;
5519}
5520
Chuck Lever7df302f2012-05-29 13:56:37 -04005521static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005522{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005523 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005524 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005525
Andrew Elbleae254dac2017-11-09 13:41:10 -05005526 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5527 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005528 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005529 /* Client debugging aid. */
5530 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5531 char addr_str[INET6_ADDRSTRLEN];
5532 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5533 sizeof(addr_str));
5534 pr_warn_ratelimited("NFSD: client %s testing state ID "
5535 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005536 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005537 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005538 spin_lock(&cl->cl_lock);
5539 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005540 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005541 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005542 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005543 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005544 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005545 switch (s->sc_type) {
5546 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005547 status = nfs_ok;
5548 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005549 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005550 status = nfserr_deleg_revoked;
5551 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005552 case NFS4_OPEN_STID:
5553 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005554 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005555 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005556 default:
5557 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005558 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04005559 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005560 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005561 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005562 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005563out_unlock:
5564 spin_unlock(&cl->cl_lock);
5565 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005566}
5567
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005568__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005569nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5570 stateid_t *stateid, unsigned char typemask,
5571 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005572{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005573 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005574 bool return_revoked = false;
5575
5576 /*
5577 * only return revoked delegations if explicitly asked.
5578 * otherwise we report revoked or bad_stateid status.
5579 */
5580 if (typemask & NFS4_REVOKED_DELEG_STID)
5581 return_revoked = true;
5582 else if (typemask & NFS4_DELEG_STID)
5583 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005584
Andrew Elbleae254dac2017-11-09 13:41:10 -05005585 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5586 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005587 return nfserr_bad_stateid;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005588 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn,
5589 false);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005590 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005591 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005592 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005593 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005594 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005595 if (status)
5596 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005597 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005598 if (!*s)
5599 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005600 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5601 nfs4_put_stid(*s);
5602 if (cstate->minorversion)
5603 return nfserr_deleg_revoked;
5604 return nfserr_bad_stateid;
5605 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005606 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005607}
5608
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005609static struct nfsd_file *
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005610nfs4_find_file(struct nfs4_stid *s, int flags)
5611{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005612 if (!s)
5613 return NULL;
5614
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005615 switch (s->sc_type) {
5616 case NFS4_DELEG_STID:
5617 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5618 return NULL;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005619 return nfsd_file_get(s->sc_file->fi_deleg_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005620 case NFS4_OPEN_STID:
5621 case NFS4_LOCK_STID:
5622 if (flags & RD_STATE)
5623 return find_readable_file(s->sc_file);
5624 else
5625 return find_writeable_file(s->sc_file);
5626 break;
5627 }
5628
5629 return NULL;
5630}
5631
5632static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005633nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005634{
5635 __be32 status;
5636
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005637 status = nfsd4_check_openowner_confirmed(ols);
5638 if (status)
5639 return status;
5640 return nfs4_check_openmode(ols, flags);
5641}
5642
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005643static __be32
5644nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
Jeff Layton5c4583b2019-08-18 14:18:54 -04005645 struct nfsd_file **nfp, int flags)
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005646{
5647 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005648 struct nfsd_file *nf;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005649 __be32 status;
5650
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005651 nf = nfs4_find_file(s, flags);
5652 if (nf) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005653 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5654 acc | NFSD_MAY_OWNER_OVERRIDE);
Jeff Layton5c4583b2019-08-18 14:18:54 -04005655 if (status) {
5656 nfsd_file_put(nf);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005657 goto out;
Jeff Layton5c4583b2019-08-18 14:18:54 -04005658 }
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005659 } else {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005660 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005661 if (status)
5662 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005663 }
Jeff Layton5c4583b2019-08-18 14:18:54 -04005664 *nfp = nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005665out:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005666 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005667}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005668static void
5669_free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5670{
5671 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5672 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5673 return;
5674 list_del(&cps->cp_list);
5675 idr_remove(&nn->s2s_cp_stateids,
5676 cps->cp_stateid.stid.si_opaque.so_id);
5677 kfree(cps);
5678}
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005679/*
5680 * A READ from an inter server to server COPY will have a
5681 * copy stateid. Look up the copy notify stateid from the
5682 * idr structure and take a reference on it.
5683 */
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005684__be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5685 struct nfs4_client *clp,
5686 struct nfs4_cpntf_state **cps)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005687{
5688 copy_stateid_t *cps_t;
5689 struct nfs4_cpntf_state *state = NULL;
5690
5691 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5692 return nfserr_bad_stateid;
5693 spin_lock(&nn->s2s_cp_lock);
5694 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5695 if (cps_t) {
5696 state = container_of(cps_t, struct nfs4_cpntf_state,
5697 cp_stateid);
Dan Carpenter5277a792019-12-04 10:59:36 +03005698 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5699 state = NULL;
5700 goto unlock;
5701 }
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005702 if (!clp)
5703 refcount_inc(&state->cp_stateid.sc_count);
5704 else
5705 _free_cpntf_state_locked(nn, state);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005706 }
Dan Carpenter5277a792019-12-04 10:59:36 +03005707unlock:
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005708 spin_unlock(&nn->s2s_cp_lock);
5709 if (!state)
5710 return nfserr_bad_stateid;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005711 if (!clp && state)
5712 *cps = state;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005713 return 0;
5714}
5715
5716static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5717 struct nfs4_stid **stid)
5718{
5719 __be32 status;
5720 struct nfs4_cpntf_state *cps = NULL;
5721 struct nfsd4_compound_state cstate;
5722
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005723 status = manage_cpntf_state(nn, st, NULL, &cps);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005724 if (status)
5725 return status;
5726
5727 cps->cpntf_time = get_seconds();
5728 memset(&cstate, 0, sizeof(cstate));
5729 status = lookup_clientid(&cps->cp_p_clid, &cstate, nn, true);
5730 if (status)
5731 goto out;
5732 status = nfsd4_lookup_stateid(&cstate, &cps->cp_p_stateid,
5733 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5734 stid, nn);
5735 put_client_renew(cstate.clp);
5736out:
5737 nfs4_put_cpntf_state(nn, cps);
5738 return status;
5739}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005740
5741void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5742{
5743 spin_lock(&nn->s2s_cp_lock);
5744 _free_cpntf_state_locked(nn, cps);
5745 spin_unlock(&nn->s2s_cp_lock);
5746}
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005747
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005749 * Checks for stateid operations
5750 */
Al Virob37ad282006-10-19 23:28:59 -07005751__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005752nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005753 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005754 stateid_t *stateid, int flags, struct nfsd_file **nfp,
5755 struct nfs4_stid **cstid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005757 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005758 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005759 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005760 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005761 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
Jeff Layton5c4583b2019-08-18 14:18:54 -04005763 if (nfp)
5764 *nfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005766 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 return nfserr_grace;
5768
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005769 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5770 status = check_special_stateids(net, fhp, stateid, flags);
5771 goto done;
5772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005774 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005775 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005776 &s, nn);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005777 if (status == nfserr_bad_stateid)
5778 status = find_cpntf_state(nn, stateid, &s);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005779 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005780 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005781 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005782 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005783 if (status)
5784 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005785
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005786 switch (s->sc_type) {
5787 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005788 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005789 break;
5790 case NFS4_OPEN_STID:
5791 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005792 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005793 break;
5794 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005795 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005798 if (status)
5799 goto out;
5800 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005801
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005802done:
Jeff Layton5c4583b2019-08-18 14:18:54 -04005803 if (status == nfs_ok && nfp)
5804 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805out:
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005806 if (s) {
5807 if (!status && cstid)
5808 *cstid = s;
5809 else
5810 nfs4_put_stid(s);
5811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 return status;
5813}
5814
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005815/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005816 * Test if the stateid is valid
5817 */
5818__be32
5819nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005820 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005821{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005822 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005823 struct nfsd4_test_stateid_id *stateid;
5824 struct nfs4_client *cl = cstate->session->se_client;
5825
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005826 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005827 stateid->ts_id_status =
5828 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005829
Bryan Schumaker17456802011-07-13 10:50:48 -04005830 return nfs_ok;
5831}
5832
Chuck Lever42691392016-08-11 10:37:30 -04005833static __be32
5834nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5835{
5836 struct nfs4_ol_stateid *stp = openlockstateid(s);
5837 __be32 ret;
5838
Trond Myklebust659aefb2017-11-03 08:00:13 -04005839 ret = nfsd4_lock_ol_stateid(stp);
5840 if (ret)
5841 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005842
5843 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5844 if (ret)
5845 goto out;
5846
5847 ret = nfserr_locks_held;
5848 if (check_for_locks(stp->st_stid.sc_file,
5849 lockowner(stp->st_stateowner)))
5850 goto out;
5851
5852 release_lock_stateid(stp);
5853 ret = nfs_ok;
5854
5855out:
5856 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005857out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005858 nfs4_put_stid(s);
5859 return ret;
5860}
5861
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005862__be32
5863nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005864 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005865{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005866 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005867 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005868 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005869 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005870 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005871 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005872
Jeff Layton1af71cc2014-07-29 21:34:14 -04005873 spin_lock(&cl->cl_lock);
5874 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005875 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005876 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005877 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005878 switch (s->sc_type) {
5879 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005880 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005881 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005882 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005883 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5884 if (ret)
5885 break;
5886 ret = nfserr_locks_held;
5887 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005888 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005889 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005890 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005891 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005892 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005893 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005894 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005895 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005896 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005897 list_del_init(&dp->dl_recall_lru);
5898 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005899 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005900 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005901 goto out;
5902 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005903 }
Trond Myklebust03da3162017-11-03 08:00:16 -04005904 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005905out_unlock:
5906 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005907out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005908 return ret;
5909}
5910
NeilBrown4c4cd222005-07-07 17:59:27 -07005911static inline int
5912setlkflg (int type)
5913{
5914 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5915 RD_STATE : WR_STATE;
5916}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005918static __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 -04005919{
5920 struct svc_fh *current_fh = &cstate->current_fh;
5921 struct nfs4_stateowner *sop = stp->st_stateowner;
5922 __be32 status;
5923
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005924 status = nfsd4_check_seqid(cstate, sop, seqid);
5925 if (status)
5926 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04005927 status = nfsd4_lock_ol_stateid(stp);
5928 if (status != nfs_ok)
5929 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005930 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005931 if (status == nfs_ok)
5932 status = nfs4_check_fh(current_fh, &stp->st_stid);
5933 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005934 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005935 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005936}
5937
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938/*
5939 * Checks for sequence id mutating operations.
5940 */
Al Virob37ad282006-10-19 23:28:59 -07005941static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005942nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005943 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005944 struct nfs4_ol_stateid **stpp,
5945 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005947 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005948 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005949 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005951 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5952 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005953
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005955 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005956 if (status)
5957 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005958 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005959 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005961 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005962 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005963 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005964 else
5965 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005966 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005967}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005968
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005969static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5970 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005971{
5972 __be32 status;
5973 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005974 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005976 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005977 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005978 if (status)
5979 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005980 oo = openowner(stp->st_stateowner);
5981 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005982 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005983 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005984 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005985 }
5986 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005987 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988}
5989
Al Virob37ad282006-10-19 23:28:59 -07005990__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005991nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005992 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005994 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07005995 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005996 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005997 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005998 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
Al Viroa6a9f182013-09-16 10:57:01 -04006000 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6001 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006003 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07006004 if (status)
6005 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006007 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006008 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006009 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006010 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006011 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006012 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006013 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006014 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006015 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04006016 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006017 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04006018 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04006019 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006020 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02006021 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006022 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07006023
Jeff Layton2a4317c2012-03-21 16:42:43 -04006024 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006025 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04006026put_stateid:
6027 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006029 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 return status;
6031}
6032
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006033static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034{
Jeff Layton82c5ff12012-05-11 09:45:13 -04006035 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006036 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04006037 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006038 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006039}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04006040
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006041static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6042{
6043 switch (to_access) {
6044 case NFS4_SHARE_ACCESS_READ:
6045 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6046 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6047 break;
6048 case NFS4_SHARE_ACCESS_WRITE:
6049 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6050 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6051 break;
6052 case NFS4_SHARE_ACCESS_BOTH:
6053 break;
6054 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006055 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 }
6057}
6058
Al Virob37ad282006-10-19 23:28:59 -07006059__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006060nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006061 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006063 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07006064 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006065 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006066 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
Al Viroa6a9f182013-09-16 10:57:01 -04006068 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6069 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04006071 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02006072 if (od->od_deleg_want)
6073 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6074 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006076 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006077 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006078 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04006081 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006082 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006084 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04006086 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006087 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006089 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006091 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04006092 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04006093 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006095put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006096 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006097 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006099 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 return status;
6101}
6102
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006103static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6104{
Trond Myklebustacf92952014-06-30 11:48:37 -04006105 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04006106 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04006107 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006108
Jeff Layton2c41beb2014-07-29 21:34:41 -04006109 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04006110 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006111
Jeff Laytond83017f2014-07-29 21:34:42 -04006112 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04006113 if (unhashed)
6114 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04006115 spin_unlock(&clp->cl_lock);
6116 free_ol_stateid_reaplist(&reaplist);
6117 } else {
6118 spin_unlock(&clp->cl_lock);
6119 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04006120 if (unhashed)
6121 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04006122 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04006123}
6124
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125/*
6126 * nfs4_unlock_state() called after encode
6127 */
Al Virob37ad282006-10-19 23:28:59 -07006128__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006129nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006130 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006132 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07006133 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006134 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006135 struct net *net = SVC_NET(rqstp);
6136 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137
Al Viroa6a9f182013-09-16 10:57:01 -04006138 dprintk("NFSD: nfsd4_close on file %pd\n",
6139 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006141 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6142 &close->cl_stateid,
6143 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006144 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006145 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006146 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006148
6149 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006150
6151 /*
6152 * Technically we don't _really_ have to increment or copy it, since
6153 * it should just be gone after this operation and we clobber the
6154 * copied value below, but we continue to do so here just to ensure
6155 * that racing ops see that there was a state change.
6156 */
Jeff Layton9767feb2015-10-01 09:05:50 -04006157 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006159 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006160 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006161
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006162 /* v4.1+ suggests that we send a special stateid in here, since the
6163 * clients should just ignore this anyway. Since this is not useful
6164 * for v4.0 clients either, we set it to the special close_stateid
6165 * universally.
6166 *
6167 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6168 */
6169 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04006170
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006171 /* put reference from nfs4_preprocess_seqid_op */
6172 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 return status;
6175}
6176
Al Virob37ad282006-10-19 23:28:59 -07006177__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006178nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006179 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006181 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006182 struct nfs4_delegation *dp;
6183 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006184 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07006185 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006186 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006188 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006189 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006191 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006192 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006193 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006194 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04006195 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006196 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04006197 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006198
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006199 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006200put_stateid:
6201 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202out:
6203 return status;
6204}
6205
Benny Halevy87df4de2008-12-15 19:42:03 +02006206static inline u64
6207end_offset(u64 start, u64 len)
6208{
6209 u64 end;
6210
6211 end = start + len;
6212 return end >= start ? end: NFS4_MAX_UINT64;
6213}
6214
6215/* last octet in a range */
6216static inline u64
6217last_byte_offset(u64 start, u64 len)
6218{
6219 u64 end;
6220
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006221 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006222 end = start + len;
6223 return end > start ? end - 1: NFS4_MAX_UINT64;
6224}
6225
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226/*
6227 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6228 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6229 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6230 * locking, this prevents us from being completely protocol-compliant. The
6231 * real solution to this problem is to start using unsigned file offsets in
6232 * the VFS, but this is a very deep change!
6233 */
6234static inline void
6235nfs4_transform_lock_offset(struct file_lock *lock)
6236{
6237 if (lock->fl_start < 0)
6238 lock->fl_start = OFFSET_MAX;
6239 if (lock->fl_end < 0)
6240 lock->fl_end = OFFSET_MAX;
6241}
6242
Jeff Laytoncae80b32015-04-03 09:04:04 -04006243static fl_owner_t
6244nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006245{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006246 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6247
6248 nfs4_get_stateowner(&lo->lo_owner);
6249 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006250}
6251
Jeff Laytoncae80b32015-04-03 09:04:04 -04006252static void
6253nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006254{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006255 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006256
Jeff Laytoncae80b32015-04-03 09:04:04 -04006257 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006258 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006259}
6260
Jeff Layton76d348f2016-09-16 16:28:24 -04006261static void
6262nfsd4_lm_notify(struct file_lock *fl)
6263{
6264 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6265 struct net *net = lo->lo_owner.so_client->net;
6266 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6267 struct nfsd4_blocked_lock *nbl = container_of(fl,
6268 struct nfsd4_blocked_lock, nbl_lock);
6269 bool queue = false;
6270
Jeff Layton7919d0a2016-09-16 16:28:25 -04006271 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006272 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006273 if (!list_empty(&nbl->nbl_list)) {
6274 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006275 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006276 queue = true;
6277 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006278 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006279
6280 if (queue)
6281 nfsd4_run_cb(&nbl->nbl_cb);
6282}
6283
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006284static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006285 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006286 .lm_get_owner = nfsd4_fl_get_owner,
6287 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006288};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
6290static inline void
6291nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6292{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006293 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
NeilBrownd5b90262006-04-10 22:55:22 -07006295 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006296 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04006297 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6298 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006299 if (!deny->ld_owner.data)
6300 /* We just don't care that much */
6301 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006302 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006303 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006304nevermind:
6305 deny->ld_owner.len = 0;
6306 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006307 deny->ld_clientid.cl_boot = 0;
6308 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 }
6310 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006311 deny->ld_length = NFS4_MAX_UINT64;
6312 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6314 deny->ld_type = NFS4_READ_LT;
6315 if (fl->fl_type != F_RDLCK)
6316 deny->ld_type = NFS4_WRITE_LT;
6317}
6318
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006319static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006320find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006322 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006323 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324
Trond Myklebust0a880a22014-07-30 08:27:10 -04006325 lockdep_assert_held(&clp->cl_lock);
6326
Trond Myklebustd4f04892014-07-29 21:34:36 -04006327 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6328 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006329 if (so->so_is_open_owner)
6330 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006331 if (same_owner_str(so, owner))
6332 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 }
6334 return NULL;
6335}
6336
Trond Myklebustc58c6612014-07-29 21:34:35 -04006337static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006338find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006339{
6340 struct nfs4_lockowner *lo;
6341
Trond Myklebustd4f04892014-07-29 21:34:36 -04006342 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006343 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006344 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006345 return lo;
6346}
6347
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006348static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6349{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006350 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006351}
6352
Jeff Layton6b180f02014-07-29 21:34:26 -04006353static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6354{
6355 struct nfs4_lockowner *lo = lockowner(sop);
6356
6357 kmem_cache_free(lockowner_slab, lo);
6358}
6359
6360static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006361 .so_unhash = nfs4_unhash_lockowner,
6362 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006363};
6364
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365/*
6366 * Alloc a lock owner structure.
6367 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006368 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006370 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006372static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006373alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6374 struct nfs4_ol_stateid *open_stp,
6375 struct nfsd4_lock *lock)
6376{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006377 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006379 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6380 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006382 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006383 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6384 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006385 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006386 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006387 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006388 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006389 if (ret == NULL) {
6390 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006391 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006392 ret = lo;
6393 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006394 nfs4_free_stateowner(&lo->lo_owner);
6395
Trond Myklebustd4f04892014-07-29 21:34:36 -04006396 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006397 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398}
6399
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006400static struct nfs4_ol_stateid *
6401find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
6402{
6403 struct nfs4_ol_stateid *lst;
6404 struct nfs4_client *clp = lo->lo_owner.so_client;
6405
6406 lockdep_assert_held(&clp->cl_lock);
6407
6408 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
6409 if (lst->st_stid.sc_type != NFS4_LOCK_STID)
6410 continue;
6411 if (lst->st_stid.sc_file == fp) {
6412 refcount_inc(&lst->st_stid.sc_count);
6413 return lst;
6414 }
6415 }
6416 return NULL;
6417}
6418
Trond Myklebustbeeca192017-11-03 08:00:14 -04006419static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006420init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6421 struct nfs4_file *fp, struct inode *inode,
6422 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006424 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006425 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
Trond Myklebustbeeca192017-11-03 08:00:14 -04006427 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006428 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006429retry:
6430 spin_lock(&clp->cl_lock);
6431 spin_lock(&fp->fi_lock);
6432 retstp = find_lock_stateid(lo, fp);
6433 if (retstp)
6434 goto out_unlock;
Jeff Layton356a95e2014-07-29 21:34:13 -04006435
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006436 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006437 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006438 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006439 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006440 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006441 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006443 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006444 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006445 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006446 list_add(&stp->st_perfile, &fp->fi_stateids);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006447out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04006448 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006449 spin_unlock(&clp->cl_lock);
6450 if (retstp) {
6451 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6452 nfs4_put_stid(&retstp->st_stid);
6453 goto retry;
6454 }
6455 /* To keep mutex tracking happy */
6456 mutex_unlock(&stp->st_mutex);
6457 stp = retstp;
6458 }
6459 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460}
6461
Jeff Laytonc53530d2014-06-30 11:48:39 -04006462static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006463find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6464 struct inode *inode, struct nfs4_ol_stateid *ost,
6465 bool *new)
6466{
6467 struct nfs4_stid *ns = NULL;
6468 struct nfs4_ol_stateid *lst;
6469 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6470 struct nfs4_client *clp = oo->oo_owner.so_client;
6471
Trond Myklebustbeeca192017-11-03 08:00:14 -04006472 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006473 spin_lock(&clp->cl_lock);
6474 lst = find_lock_stateid(lo, fi);
Jeff Layton356a95e2014-07-29 21:34:13 -04006475 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006476 if (lst != NULL) {
6477 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6478 goto out;
6479 nfs4_put_stid(&lst->st_stid);
6480 }
6481 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6482 if (ns == NULL)
6483 return NULL;
6484
6485 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6486 if (lst == openlockstateid(ns))
6487 *new = true;
6488 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006489 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006490out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006491 return lst;
6492}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006493
NeilBrownfd39ca92005-06-23 22:04:03 -07006494static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495check_lock_length(u64 offset, u64 length)
6496{
Kinglong Meee7969312015-07-13 17:34:19 +08006497 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6498 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499}
6500
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006501static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006502{
Trond Myklebust11b91642014-07-29 21:34:08 -04006503 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006504
Jeff Layton7214e862014-07-10 14:07:33 -04006505 lockdep_assert_held(&fp->fi_lock);
6506
Jeff Layton82c5ff12012-05-11 09:45:13 -04006507 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006508 return;
Jeff Layton12659652014-07-10 14:07:28 -04006509 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006510 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006511}
6512
Jeff Layton356a95e2014-07-29 21:34:13 -04006513static __be32
6514lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6515 struct nfs4_ol_stateid *ost,
6516 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006517 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006518{
Jeff Layton5db1c032014-07-29 21:34:28 -04006519 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006520 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006521 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6522 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006523 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006524 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006525 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006526 unsigned int strhashval;
6527
Kinglong Meec8623992015-07-13 17:35:37 +08006528 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006529 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006530 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006531 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6532 if (lo == NULL)
6533 return nfserr_jukebox;
6534 } else {
6535 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006536 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006537 if (!cstate->minorversion &&
6538 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006539 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006540 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006541
Jeff Laytondd257932016-08-11 10:37:39 -04006542 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6543 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006544 status = nfserr_jukebox;
6545 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006546 }
Jeff Laytondd257932016-08-11 10:37:39 -04006547
Jeff Layton5db1c032014-07-29 21:34:28 -04006548 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006549 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006550out:
6551 nfs4_put_stateowner(&lo->lo_owner);
6552 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006553}
6554
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555/*
6556 * LOCK operation
6557 */
Al Virob37ad282006-10-19 23:28:59 -07006558__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006559nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006560 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006562 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006563 struct nfs4_openowner *open_sop = NULL;
6564 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006565 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006566 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006567 struct nfs4_file *fp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006568 struct nfsd_file *nf = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006569 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006570 struct file_lock *file_lock = NULL;
6571 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006572 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006573 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006574 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006575 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006576 unsigned char fl_type;
6577 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006578 struct net *net = SVC_NET(rqstp);
6579 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580
6581 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6582 (long long) lock->lk_offset,
6583 (long long) lock->lk_length);
6584
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 if (check_lock_length(lock->lk_offset, lock->lk_length))
6586 return nfserr_inval;
6587
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006588 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006589 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006590 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6591 return status;
6592 }
6593
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006595 if (nfsd4_has_session(cstate))
6596 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006597 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006598 &cstate->session->se_client->cl_clientid,
6599 sizeof(clientid_t));
6600
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006602 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006606 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607 lock->lk_new_open_seqid,
6608 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006609 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006610 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006612 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006613 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006614 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006615 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006616 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006617 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006618 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006619 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006620 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006621 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006622 lock->lk_old_lock_seqid,
6623 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006624 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006625 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006626 if (status)
6627 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006628 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006630 lkflg = setlkflg(lock->lk_type);
6631 status = nfs4_check_openmode(lock_stp, lkflg);
6632 if (status)
6633 goto out;
6634
NeilBrown0dd395d2005-07-07 17:59:15 -07006635 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006636 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006637 goto out;
6638 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006639 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006640 goto out;
6641
Trond Myklebust11b91642014-07-29 21:34:08 -04006642 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006645 if (nfsd4_has_session(cstate))
6646 fl_flags |= FL_SLEEP;
6647 /* Fallthrough */
6648 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006649 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006650 nf = find_readable_file_locked(fp);
6651 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006652 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006653 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006654 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006657 if (nfsd4_has_session(cstate))
6658 fl_flags |= FL_SLEEP;
6659 /* Fallthrough */
6660 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006661 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006662 nf = find_writeable_file_locked(fp);
6663 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006664 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006665 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006666 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006667 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 default:
6669 status = nfserr_inval;
6670 goto out;
6671 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006672
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006673 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006674 status = nfserr_openmode;
6675 goto out;
6676 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006677
Jeff Layton76d348f2016-09-16 16:28:24 -04006678 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6679 if (!nbl) {
6680 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6681 status = nfserr_jukebox;
6682 goto out;
6683 }
6684
6685 file_lock = &nbl->nbl_lock;
6686 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006687 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006688 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006689 file_lock->fl_file = nf->nf_file;
Jeff Layton76d348f2016-09-16 16:28:24 -04006690 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006691 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6692 file_lock->fl_start = lock->lk_offset;
6693 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6694 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695
Jeff Layton21179d82012-08-21 08:03:32 -04006696 conflock = locks_alloc_lock();
6697 if (!conflock) {
6698 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6699 status = nfserr_jukebox;
6700 goto out;
6701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702
Jeff Layton76d348f2016-09-16 16:28:24 -04006703 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04006704 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04006705 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006706 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006707 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006708 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006709 }
6710
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006711 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006712 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006714 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006715 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006716 if (lock->lk_reclaim)
6717 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006718 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006719 case FILE_LOCK_DEFERRED:
6720 nbl = NULL;
6721 /* Fallthrough */
6722 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006723 status = nfserr_denied;
6724 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006725 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006726 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006727 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006729 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006730 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006731 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006732 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006736 if (nbl) {
6737 /* dequeue it if we queued it before */
6738 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006739 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006740 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006741 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006742 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006743 }
6744 free_blocked_lock(nbl);
6745 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006746 if (nf)
6747 nfsd_file_put(nf);
Jeff Layton5db1c032014-07-29 21:34:28 -04006748 if (lock_stp) {
6749 /* Bump seqid manually if the 4.0 replay owner is openowner */
6750 if (cstate->replay_owner &&
6751 cstate->replay_owner != &lock_sop->lo_owner &&
6752 seqid_mutating_err(ntohl(status)))
6753 lock_sop->lo_owner.so_seqid++;
6754
6755 /*
6756 * If this is a new, never-before-used stateid, and we are
6757 * returning an error, then just go ahead and release it.
6758 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006759 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006760 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006761
6762 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006763
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006764 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006765 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006766 if (open_stp)
6767 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006768 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006769 if (conflock)
6770 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 return status;
6772}
6773
6774/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006775 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6776 * so we do a temporary open here just to get an open file to pass to
6777 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6778 * inode operation.)
6779 */
Al Viro04da6e92012-04-13 00:00:04 -04006780static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006781{
Jeff Layton6b556ca2019-08-18 14:18:55 -04006782 struct nfsd_file *nf;
6783 __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
Al Viro04da6e92012-04-13 00:00:04 -04006784 if (!err) {
Jeff Layton6b556ca2019-08-18 14:18:55 -04006785 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6786 nfsd_file_put(nf);
Al Viro04da6e92012-04-13 00:00:04 -04006787 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006788 return err;
6789}
6790
6791/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 * LOCKT operation
6793 */
Al Virob37ad282006-10-19 23:28:59 -07006794__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006795nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006796 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006798 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006799 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006800 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006801 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006802 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006804 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 return nfserr_grace;
6806
6807 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6808 return nfserr_inval;
6809
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006810 if (!nfsd4_has_session(cstate)) {
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006811 status = lookup_clientid(&lockt->lt_clientid, cstate, nn,
6812 false);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006813 if (status)
6814 goto out;
6815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006817 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819
Jeff Layton21179d82012-08-21 08:03:32 -04006820 file_lock = locks_alloc_lock();
6821 if (!file_lock) {
6822 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6823 status = nfserr_jukebox;
6824 goto out;
6825 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006826
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 switch (lockt->lt_type) {
6828 case NFS4_READ_LT:
6829 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006830 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006831 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 case NFS4_WRITE_LT:
6833 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006834 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006835 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006837 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006839 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 }
6841
Kinglong Meec8623992015-07-13 17:35:37 +08006842 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006843 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006844 file_lock->fl_owner = (fl_owner_t)lo;
6845 file_lock->fl_pid = current->tgid;
6846 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847
Jeff Layton21179d82012-08-21 08:03:32 -04006848 file_lock->fl_start = lockt->lt_offset;
6849 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850
Jeff Layton21179d82012-08-21 08:03:32 -04006851 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852
Jeff Layton21179d82012-08-21 08:03:32 -04006853 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006854 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006855 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006856
Jeff Layton21179d82012-08-21 08:03:32 -04006857 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006859 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 }
6861out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006862 if (lo)
6863 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006864 if (file_lock)
6865 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 return status;
6867}
6868
Al Virob37ad282006-10-19 23:28:59 -07006869__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006870nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006871 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006873 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006874 struct nfs4_ol_stateid *stp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006875 struct nfsd_file *nf = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006876 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006877 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006878 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006879 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6880
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6882 (long long) locku->lu_offset,
6883 (long long) locku->lu_length);
6884
6885 if (check_lock_length(locku->lu_offset, locku->lu_length))
6886 return nfserr_inval;
6887
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006888 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006889 &locku->lu_stateid, NFS4_LOCK_STID,
6890 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006891 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 goto out;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006893 nf = find_any_file(stp->st_stid.sc_file);
6894 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006895 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006896 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006897 }
Jeff Layton21179d82012-08-21 08:03:32 -04006898 file_lock = locks_alloc_lock();
6899 if (!file_lock) {
6900 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6901 status = nfserr_jukebox;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006902 goto put_file;
Jeff Layton21179d82012-08-21 08:03:32 -04006903 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006904
Jeff Layton21179d82012-08-21 08:03:32 -04006905 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006906 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006907 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006908 file_lock->fl_file = nf->nf_file;
Jeff Layton21179d82012-08-21 08:03:32 -04006909 file_lock->fl_flags = FL_POSIX;
6910 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6911 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912
Jeff Layton21179d82012-08-21 08:03:32 -04006913 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6914 locku->lu_length);
6915 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006917 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006918 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006919 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920 goto out_nfserr;
6921 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006922 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006923put_file:
6924 nfsd_file_put(nf);
Trond Myklebust858cc572014-07-29 21:34:16 -04006925put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006926 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006927 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006929 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006930 if (file_lock)
6931 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932 return status;
6933
6934out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006935 status = nfserrno(err);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006936 goto put_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937}
6938
6939/*
6940 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006941 * true: locks held by lockowner
6942 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006944static bool
6945check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006947 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006948 int status = false;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006949 struct nfsd_file *nf = find_any_file(fp);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006950 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006951 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006952
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006953 if (!nf) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006954 /* Any valid lock stateid should have some sort of access */
6955 WARN_ON_ONCE(1);
6956 return status;
6957 }
6958
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006959 inode = locks_inode(nf->nf_file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006960 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006962 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006963 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006964 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6965 if (fl->fl_owner == (fl_owner_t)lowner) {
6966 status = true;
6967 break;
6968 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006969 }
Jeff Layton6109c852015-01-16 15:05:57 -05006970 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006972 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 return status;
6974}
6975
Al Virob37ad282006-10-19 23:28:59 -07006976__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006977nfsd4_release_lockowner(struct svc_rqst *rqstp,
6978 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006979 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006981 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006983 struct nfs4_stateowner *sop;
6984 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006985 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006987 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006988 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006989 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006990 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006991 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992
6993 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6994 clid->cl_boot, clid->cl_id);
6995
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006996 status = lookup_clientid(clid, cstate, nn, false);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006997 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006998 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006999
Trond Myklebustd4f04892014-07-29 21:34:36 -04007000 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04007001 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04007002 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04007003 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04007004 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04007005
7006 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05007007 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04007008
Jeff Layton882e9d22014-07-29 21:34:37 -04007009 /* see if there are still any locks associated with it */
7010 lo = lockowner(sop);
7011 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
7012 if (check_for_locks(stp->st_stid.sc_file, lo)) {
7013 status = nfserr_locks_held;
7014 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04007015 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04007016 }
Jeff Layton5adfd882014-07-29 21:34:31 -04007017 }
Jeff Layton882e9d22014-07-29 21:34:37 -04007018
Kinglong Meeb5971af2014-08-22 10:18:43 -04007019 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04007020 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04007021 }
Chuck Lever88584812016-07-13 16:40:14 -04007022 if (!lo) {
7023 spin_unlock(&clp->cl_lock);
7024 return status;
7025 }
7026
7027 unhash_lockowner_locked(lo);
7028 while (!list_empty(&lo->lo_owner.so_stateids)) {
7029 stp = list_first_entry(&lo->lo_owner.so_stateids,
7030 struct nfs4_ol_stateid,
7031 st_perstateowner);
7032 WARN_ON(!unhash_lock_stateid(stp));
7033 put_ol_stateid_locked(stp, &reaplist);
7034 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04007035 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04007036 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007037 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04007038 nfs4_put_stateowner(&lo->lo_owner);
7039
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 return status;
7041}
7042
7043static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07007044alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045{
NeilBrowna55370a2005-06-23 22:03:52 -07007046 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047}
7048
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007049bool
Scott Mayhew6b189102019-03-26 18:06:26 -04007050nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07007051{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007052 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07007053
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007054 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007055 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07007056}
7057
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058/*
7059 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04007060 *
7061 * The caller is responsible for freeing name.data if NULL is returned (it
7062 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05007064struct nfs4_client_reclaim *
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007065nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7066 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067{
7068 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007069 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070
Scott Mayhew6b189102019-03-26 18:06:26 -04007071 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data);
NeilBrowna55370a2005-06-23 22:03:52 -07007072 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05007073 if (crp) {
7074 strhashval = clientstr_hashval(name);
7075 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007076 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04007077 crp->cr_name.data = name.data;
7078 crp->cr_name.len = name.len;
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007079 crp->cr_princhash.data = princhash.data;
7080 crp->cr_princhash.len = princhash.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007081 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007082 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007083 }
7084 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085}
7086
Jeff Layton2a4317c2012-03-21 16:42:43 -04007087void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007088nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05007089{
7090 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04007091 kfree(crp->cr_name.data);
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007092 kfree(crp->cr_princhash.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05007093 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007094 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05007095}
7096
7097void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007098nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099{
7100 struct nfs4_client_reclaim *crp = NULL;
7101 int i;
7102
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007104 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7105 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007107 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 }
7109 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05007110 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111}
7112
7113/*
7114 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04007115struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04007116nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117{
7118 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 struct nfs4_client_reclaim *crp = NULL;
7120
Scott Mayhew6b189102019-03-26 18:06:26 -04007121 dprintk("NFSD: nfs4_find_reclaim_client for name %.*s\n", name.len, name.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122
Scott Mayhew6b189102019-03-26 18:06:26 -04007123 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007124 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04007125 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 return crp;
7127 }
7128 }
7129 return NULL;
7130}
7131
7132/*
7133* Called from OPEN. Look for clientid in reclaim list.
7134*/
Al Virob37ad282006-10-19 23:28:59 -07007135__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007136nfs4_check_open_reclaim(clientid_t *clid,
7137 struct nfsd4_compound_state *cstate,
7138 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007140 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04007141
7142 /* find clientid in conf_id_hashtbl */
Olga Kornievskaiab7342202019-09-06 15:17:21 -04007143 status = lookup_clientid(clid, cstate, nn, false);
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007144 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04007145 return nfserr_reclaim_bad;
7146
Jeff Layton3b3e7b72014-09-12 16:40:22 -04007147 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
7148 return nfserr_no_grace;
7149
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007150 if (nfsd4_client_record_check(cstate->clp))
7151 return nfserr_reclaim_bad;
7152
7153 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154}
7155
Bryan Schumaker65178db2011-11-01 13:35:21 -04007156#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04007157static inline void
7158put_client(struct nfs4_client *clp)
7159{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007160 atomic_dec(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04007161}
7162
Jeff Layton285abde2014-07-30 08:27:24 -04007163static struct nfs4_client *
7164nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
7165{
7166 struct nfs4_client *clp;
7167 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7168 nfsd_net_id);
7169
7170 if (!nfsd_netns_ready(nn))
7171 return NULL;
7172
7173 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7174 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
7175 return clp;
7176 }
7177 return NULL;
7178}
7179
Jeff Layton7ec0e362014-07-30 08:27:17 -04007180u64
Jeff Layton285abde2014-07-30 08:27:24 -04007181nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04007182{
7183 struct nfs4_client *clp;
7184 u64 count = 0;
7185 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7186 nfsd_net_id);
7187 char buf[INET6_ADDRSTRLEN];
7188
7189 if (!nfsd_netns_ready(nn))
7190 return 0;
7191
7192 spin_lock(&nn->client_lock);
7193 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7194 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7195 pr_info("NFS Client: %s\n", buf);
7196 ++count;
7197 }
7198 spin_unlock(&nn->client_lock);
7199
7200 return count;
7201}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007202
Jeff Laytona0926d12014-07-30 08:27:18 -04007203u64
Jeff Layton285abde2014-07-30 08:27:24 -04007204nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04007205{
7206 u64 count = 0;
7207 struct nfs4_client *clp;
7208 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7209 nfsd_net_id);
7210
7211 if (!nfsd_netns_ready(nn))
7212 return count;
7213
7214 spin_lock(&nn->client_lock);
7215 clp = nfsd_find_client(addr, addr_size);
7216 if (clp) {
7217 if (mark_client_expired_locked(clp) == nfs_ok)
7218 ++count;
7219 else
7220 clp = NULL;
7221 }
7222 spin_unlock(&nn->client_lock);
7223
7224 if (clp)
7225 expire_client(clp);
7226
7227 return count;
7228}
7229
Jeff Layton69fc9ed2014-07-30 08:27:19 -04007230u64
Jeff Layton285abde2014-07-30 08:27:24 -04007231nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04007232{
7233 u64 count = 0;
7234 struct nfs4_client *clp, *next;
7235 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7236 nfsd_net_id);
7237 LIST_HEAD(reaplist);
7238
7239 if (!nfsd_netns_ready(nn))
7240 return count;
7241
7242 spin_lock(&nn->client_lock);
7243 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7244 if (mark_client_expired_locked(clp) == nfs_ok) {
7245 list_add(&clp->cl_lru, &reaplist);
7246 if (max != 0 && ++count >= max)
7247 break;
7248 }
7249 }
7250 spin_unlock(&nn->client_lock);
7251
7252 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
7253 expire_client(clp);
7254
7255 return count;
7256}
7257
Bryan Schumaker184c1842012-11-29 11:40:44 -05007258static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
7259 const char *type)
7260{
7261 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05007262 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05007263 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
7264}
7265
Jeff Layton016200c2014-07-30 08:27:21 -04007266static void
7267nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
7268 struct list_head *collect)
7269{
7270 struct nfs4_client *clp = lst->st_stid.sc_client;
7271 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7272 nfsd_net_id);
7273
7274 if (!collect)
7275 return;
7276
7277 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007278 atomic_inc(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04007279 list_add(&lst->st_locks, collect);
7280}
7281
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007282static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04007283 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04007284 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05007285{
7286 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007287 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007288 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007289 u64 count = 0;
7290
Jeff Layton016200c2014-07-30 08:27:21 -04007291 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007292 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007293 list_for_each_entry_safe(stp, st_next,
7294 &oop->oo_owner.so_stateids, st_perstateowner) {
7295 list_for_each_entry_safe(lst, lst_next,
7296 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04007297 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04007298 if (func(lst))
7299 nfsd_inject_add_lock_to_list(lst,
7300 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04007301 }
Jeff Layton016200c2014-07-30 08:27:21 -04007302 ++count;
7303 /*
7304 * Despite the fact that these functions deal
7305 * with 64-bit integers for "count", we must
7306 * ensure that it doesn't blow up the
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007307 * clp->cl_rpc_users. Throw a warning if we
Jeff Layton016200c2014-07-30 08:27:21 -04007308 * start to approach INT_MAX here.
7309 */
7310 WARN_ON_ONCE(count == (INT_MAX / 2));
7311 if (count == max)
7312 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007313 }
7314 }
7315 }
Jeff Layton016200c2014-07-30 08:27:21 -04007316out:
7317 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007318
7319 return count;
7320}
7321
Jeff Layton016200c2014-07-30 08:27:21 -04007322static u64
7323nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
7324 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05007325{
Jeff Layton016200c2014-07-30 08:27:21 -04007326 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007327}
7328
Jeff Layton016200c2014-07-30 08:27:21 -04007329static u64
7330nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007331{
Jeff Layton016200c2014-07-30 08:27:21 -04007332 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007333 nfsd_print_count(clp, count, "locked files");
7334 return count;
7335}
7336
Jeff Layton016200c2014-07-30 08:27:21 -04007337u64
Jeff Layton285abde2014-07-30 08:27:24 -04007338nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04007339{
7340 struct nfs4_client *clp;
7341 u64 count = 0;
7342 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7343 nfsd_net_id);
7344
7345 if (!nfsd_netns_ready(nn))
7346 return 0;
7347
7348 spin_lock(&nn->client_lock);
7349 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7350 count += nfsd_print_client_locks(clp);
7351 spin_unlock(&nn->client_lock);
7352
7353 return count;
7354}
7355
7356static void
7357nfsd_reap_locks(struct list_head *reaplist)
7358{
7359 struct nfs4_client *clp;
7360 struct nfs4_ol_stateid *stp, *next;
7361
7362 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
7363 list_del_init(&stp->st_locks);
7364 clp = stp->st_stid.sc_client;
7365 nfs4_put_stid(&stp->st_stid);
7366 put_client(clp);
7367 }
7368}
7369
7370u64
Jeff Layton285abde2014-07-30 08:27:24 -04007371nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04007372{
7373 unsigned int count = 0;
7374 struct nfs4_client *clp;
7375 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7376 nfsd_net_id);
7377 LIST_HEAD(reaplist);
7378
7379 if (!nfsd_netns_ready(nn))
7380 return count;
7381
7382 spin_lock(&nn->client_lock);
7383 clp = nfsd_find_client(addr, addr_size);
7384 if (clp)
7385 count = nfsd_collect_client_locks(clp, &reaplist, 0);
7386 spin_unlock(&nn->client_lock);
7387 nfsd_reap_locks(&reaplist);
7388 return count;
7389}
7390
7391u64
Jeff Layton285abde2014-07-30 08:27:24 -04007392nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04007393{
7394 u64 count = 0;
7395 struct nfs4_client *clp;
7396 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7397 nfsd_net_id);
7398 LIST_HEAD(reaplist);
7399
7400 if (!nfsd_netns_ready(nn))
7401 return count;
7402
7403 spin_lock(&nn->client_lock);
7404 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7405 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
7406 if (max != 0 && count >= max)
7407 break;
7408 }
7409 spin_unlock(&nn->client_lock);
7410 nfsd_reap_locks(&reaplist);
7411 return count;
7412}
7413
Jeff Layton82e05ef2014-07-30 08:27:22 -04007414static u64
7415nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
7416 struct list_head *collect,
7417 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007418{
7419 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04007420 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7421 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007422 u64 count = 0;
7423
Jeff Layton82e05ef2014-07-30 08:27:22 -04007424 lockdep_assert_held(&nn->client_lock);
7425
7426 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007427 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04007428 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007429 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007430 if (collect) {
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007431 atomic_inc(&clp->cl_rpc_users);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007432 list_add(&oop->oo_perclient, collect);
7433 }
7434 }
7435 ++count;
7436 /*
7437 * Despite the fact that these functions deal with
7438 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007439 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton82e05ef2014-07-30 08:27:22 -04007440 * warning if we start to approach INT_MAX here.
7441 */
7442 WARN_ON_ONCE(count == (INT_MAX / 2));
7443 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007444 break;
7445 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04007446 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007447
7448 return count;
7449}
7450
Jeff Layton82e05ef2014-07-30 08:27:22 -04007451static u64
7452nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007453{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007454 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
7455
7456 nfsd_print_count(clp, count, "openowners");
7457 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007458}
7459
Jeff Layton82e05ef2014-07-30 08:27:22 -04007460static u64
7461nfsd_collect_client_openowners(struct nfs4_client *clp,
7462 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007463{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007464 return nfsd_foreach_client_openowner(clp, max, collect,
7465 unhash_openowner_locked);
7466}
7467
7468u64
Jeff Layton285abde2014-07-30 08:27:24 -04007469nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007470{
7471 struct nfs4_client *clp;
7472 u64 count = 0;
7473 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7474 nfsd_net_id);
7475
7476 if (!nfsd_netns_ready(nn))
7477 return 0;
7478
7479 spin_lock(&nn->client_lock);
7480 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7481 count += nfsd_print_client_openowners(clp);
7482 spin_unlock(&nn->client_lock);
7483
7484 return count;
7485}
7486
7487static void
7488nfsd_reap_openowners(struct list_head *reaplist)
7489{
7490 struct nfs4_client *clp;
7491 struct nfs4_openowner *oop, *next;
7492
7493 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
7494 list_del_init(&oop->oo_perclient);
7495 clp = oop->oo_owner.so_client;
7496 release_openowner(oop);
7497 put_client(clp);
7498 }
7499}
7500
7501u64
Jeff Layton285abde2014-07-30 08:27:24 -04007502nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
7503 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007504{
7505 unsigned int count = 0;
7506 struct nfs4_client *clp;
7507 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7508 nfsd_net_id);
7509 LIST_HEAD(reaplist);
7510
7511 if (!nfsd_netns_ready(nn))
7512 return count;
7513
7514 spin_lock(&nn->client_lock);
7515 clp = nfsd_find_client(addr, addr_size);
7516 if (clp)
7517 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
7518 spin_unlock(&nn->client_lock);
7519 nfsd_reap_openowners(&reaplist);
7520 return count;
7521}
7522
7523u64
Jeff Layton285abde2014-07-30 08:27:24 -04007524nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007525{
7526 u64 count = 0;
7527 struct nfs4_client *clp;
7528 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7529 nfsd_net_id);
7530 LIST_HEAD(reaplist);
7531
7532 if (!nfsd_netns_ready(nn))
7533 return count;
7534
7535 spin_lock(&nn->client_lock);
7536 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7537 count += nfsd_collect_client_openowners(clp, &reaplist,
7538 max - count);
7539 if (max != 0 && count >= max)
7540 break;
7541 }
7542 spin_unlock(&nn->client_lock);
7543 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007544 return count;
7545}
7546
Bryan Schumaker269de302012-11-29 11:40:42 -05007547static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
7548 struct list_head *victims)
7549{
7550 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007551 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7552 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05007553 u64 count = 0;
7554
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007555 lockdep_assert_held(&nn->client_lock);
7556
7557 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007558 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04007559 if (victims) {
7560 /*
7561 * It's not safe to mess with delegations that have a
7562 * non-zero dl_time. They might have already been broken
7563 * and could be processed by the laundromat outside of
7564 * the state_lock. Just leave them be.
7565 */
7566 if (dp->dl_time != 0)
7567 continue;
7568
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007569 atomic_inc(&clp->cl_rpc_users);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007570 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007571 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04007572 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007573 ++count;
7574 /*
7575 * Despite the fact that these functions deal with
7576 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007577 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007578 * warning if we start to approach INT_MAX here.
7579 */
7580 WARN_ON_ONCE(count == (INT_MAX / 2));
7581 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05007582 break;
7583 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007584 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007585 return count;
7586}
7587
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007588static u64
7589nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05007590{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007591 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007592
7593 nfsd_print_count(clp, count, "delegations");
7594 return count;
7595}
7596
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007597u64
Jeff Layton285abde2014-07-30 08:27:24 -04007598nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007599{
7600 struct nfs4_client *clp;
7601 u64 count = 0;
7602 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7603 nfsd_net_id);
7604
7605 if (!nfsd_netns_ready(nn))
7606 return 0;
7607
7608 spin_lock(&nn->client_lock);
7609 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7610 count += nfsd_print_client_delegations(clp);
7611 spin_unlock(&nn->client_lock);
7612
7613 return count;
7614}
7615
7616static void
7617nfsd_forget_delegations(struct list_head *reaplist)
7618{
7619 struct nfs4_client *clp;
7620 struct nfs4_delegation *dp, *next;
7621
7622 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7623 list_del_init(&dp->dl_recall_lru);
7624 clp = dp->dl_stid.sc_client;
7625 revoke_delegation(dp);
7626 put_client(clp);
7627 }
7628}
7629
7630u64
Jeff Layton285abde2014-07-30 08:27:24 -04007631nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7632 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007633{
7634 u64 count = 0;
7635 struct nfs4_client *clp;
7636 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7637 nfsd_net_id);
7638 LIST_HEAD(reaplist);
7639
7640 if (!nfsd_netns_ready(nn))
7641 return count;
7642
7643 spin_lock(&nn->client_lock);
7644 clp = nfsd_find_client(addr, addr_size);
7645 if (clp)
7646 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7647 spin_unlock(&nn->client_lock);
7648
7649 nfsd_forget_delegations(&reaplist);
7650 return count;
7651}
7652
7653u64
Jeff Layton285abde2014-07-30 08:27:24 -04007654nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007655{
7656 u64 count = 0;
7657 struct nfs4_client *clp;
7658 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7659 nfsd_net_id);
7660 LIST_HEAD(reaplist);
7661
7662 if (!nfsd_netns_ready(nn))
7663 return count;
7664
7665 spin_lock(&nn->client_lock);
7666 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7667 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7668 if (max != 0 && count >= max)
7669 break;
7670 }
7671 spin_unlock(&nn->client_lock);
7672 nfsd_forget_delegations(&reaplist);
7673 return count;
7674}
7675
7676static void
7677nfsd_recall_delegations(struct list_head *reaplist)
7678{
7679 struct nfs4_client *clp;
7680 struct nfs4_delegation *dp, *next;
7681
7682 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7683 list_del_init(&dp->dl_recall_lru);
7684 clp = dp->dl_stid.sc_client;
7685 /*
7686 * We skipped all entries that had a zero dl_time before,
7687 * so we can now reset the dl_time back to 0. If a delegation
7688 * break comes in now, then it won't make any difference since
7689 * we're recalling it either way.
7690 */
7691 spin_lock(&state_lock);
7692 dp->dl_time = 0;
7693 spin_unlock(&state_lock);
7694 nfsd_break_one_deleg(dp);
7695 put_client(clp);
7696 }
7697}
7698
7699u64
Jeff Layton285abde2014-07-30 08:27:24 -04007700nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007701 size_t addr_size)
7702{
7703 u64 count = 0;
7704 struct nfs4_client *clp;
7705 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7706 nfsd_net_id);
7707 LIST_HEAD(reaplist);
7708
7709 if (!nfsd_netns_ready(nn))
7710 return count;
7711
7712 spin_lock(&nn->client_lock);
7713 clp = nfsd_find_client(addr, addr_size);
7714 if (clp)
7715 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7716 spin_unlock(&nn->client_lock);
7717
7718 nfsd_recall_delegations(&reaplist);
7719 return count;
7720}
7721
7722u64
Jeff Layton285abde2014-07-30 08:27:24 -04007723nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007724{
7725 u64 count = 0;
7726 struct nfs4_client *clp, *next;
7727 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7728 nfsd_net_id);
7729 LIST_HEAD(reaplist);
7730
7731 if (!nfsd_netns_ready(nn))
7732 return count;
7733
7734 spin_lock(&nn->client_lock);
7735 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7736 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7737 if (max != 0 && ++count >= max)
7738 break;
7739 }
7740 spin_unlock(&nn->client_lock);
7741 nfsd_recall_delegations(&reaplist);
7742 return count;
7743}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007744#endif /* CONFIG_NFSD_FAULT_INJECTION */
7745
Meelap Shahc2f1a552007-07-17 04:04:39 -07007746/*
7747 * Since the lifetime of a delegation isn't limited to that of an open, a
7748 * client may quite reasonably hang on to a delegation as long as it has
7749 * the inode cached. This becomes an obvious problem the first time a
7750 * client's inode cache approaches the size of the server's total memory.
7751 *
7752 * For now we avoid this problem by imposing a hard limit on the number
7753 * of delegations, which varies according to the server's memory size.
7754 */
7755static void
7756set_max_delegations(void)
7757{
7758 /*
7759 * Allow at most 4 delegations per megabyte of RAM. Quick
7760 * estimates suggest that in the worst case (where every delegation
7761 * is for a different inode), a delegation could take about 1.5K,
7762 * giving a worst case usage of about 6% of memory.
7763 */
7764 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7765}
7766
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007767static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007768{
7769 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7770 int i;
7771
Kees Cook6da2ec52018-06-12 13:55:00 -07007772 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7773 sizeof(struct list_head),
7774 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007775 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007776 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007777 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7778 sizeof(struct list_head),
7779 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007780 if (!nn->unconf_id_hashtbl)
7781 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007782 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7783 sizeof(struct list_head),
7784 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007785 if (!nn->sessionid_hashtbl)
7786 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007787
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007788 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007789 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007790 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007791 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007792 for (i = 0; i < SESSION_HASH_SIZE; i++)
7793 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007794 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007795 nn->unconf_name_tree = RB_ROOT;
Vasily Averin81833de2017-11-13 07:25:40 +03007796 nn->boot_time = get_seconds();
7797 nn->grace_ended = false;
7798 nn->nfsd4_manager.block_opens = true;
7799 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007800 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007801 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007802 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007803 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007804 spin_lock_init(&nn->s2s_cp_lock);
7805 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007806
Jeff Layton0cc11a62016-10-20 09:34:31 -04007807 spin_lock_init(&nn->blocked_locks_lock);
7808 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7809
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007810 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007811 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007812
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007813 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007814
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007815err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007816 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007817err_unconf_id:
7818 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007819err:
7820 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007821}
7822
7823static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007824nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007825{
7826 int i;
7827 struct nfs4_client *clp = NULL;
7828 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7829
7830 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7831 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7832 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7833 destroy_client(clp);
7834 }
7835 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007836
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007837 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7838
Kinglong Mee2b905632014-03-26 22:09:30 +08007839 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7840 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7841 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7842 destroy_client(clp);
7843 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007844 }
7845
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007846 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007847 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007848 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007849 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007850}
7851
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007852int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007853nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007854{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007855 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007856 int ret;
7857
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007858 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007859 if (ret)
7860 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007861 locks_start_grace(net, &nn->nfsd4_manager);
7862 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007863 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7864 goto skip_grace;
Vasily Averin7e981a82017-11-06 16:46:04 +03007865 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7866 nn->nfsd4_grace, net->ns.inum);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007867 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007868 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007869
7870skip_grace:
7871 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7872 net->ns.inum);
7873 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7874 nfsd4_end_grace(nn);
7875 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007876}
7877
7878/* initialization to perform when the nfsd service is started: */
7879
7880int
7881nfs4_state_start(void)
7882{
7883 int ret;
7884
Jeff Layton51a54562015-08-20 07:17:01 -04007885 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007886 if (laundry_wq == NULL) {
7887 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007888 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007889 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007890 ret = nfsd4_create_callback_queue();
7891 if (ret)
7892 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007893
Meelap Shahc2f1a552007-07-17 04:04:39 -07007894 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007895 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007896
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007897out_free_laundry:
7898 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007899out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007900 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901}
7902
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007903void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007904nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007908 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007910 cancel_delayed_work_sync(&nn->laundromat_work);
7911 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007912
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007914 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007915 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007917 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007918 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 }
Benny Halevycdc975052014-05-30 09:09:30 -04007920 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921 list_for_each_safe(pos, next, &reaplist) {
7922 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007923 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007924 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 }
7926
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007927 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007928 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929}
7930
7931void
7932nfs4_state_shutdown(void)
7933{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007934 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007935 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007937
7938static void
7939get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7940{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007941 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7942 CURRENT_STATEID(stateid))
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007943 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007944}
7945
7946static void
7947put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7948{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007949 if (cstate->minorversion) {
7950 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007951 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007952 }
7953}
7954
7955void
7956clear_current_stateid(struct nfsd4_compound_state *cstate)
7957{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007958 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007959}
7960
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007961/*
7962 * functions to set current state id
7963 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007964void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007965nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7966 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007967{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007968 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007969}
7970
7971void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007972nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7973 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007974{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007975 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007976}
7977
7978void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007979nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7980 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007981{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007982 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007983}
7984
7985void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007986nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7987 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007988{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007989 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007990}
7991
7992/*
7993 * functions to consume current state id
7994 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007995
7996void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007997nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7998 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007999{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008000 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01008001}
8002
8003void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008004nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
8005 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01008006{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008007 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01008008}
8009
8010void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008011nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
8012 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008013{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008014 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008015}
8016
8017void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008018nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
8019 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008020{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008021 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008022}
8023
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01008024void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008025nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
8026 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008027{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008028 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008029}
8030
8031void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008032nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
8033 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008034{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008035 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008036}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008037
8038void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008039nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
8040 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008041{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008042 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008043}
8044
8045void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008046nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
8047 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008048{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008049 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008050}