blob: 3a24b3c328c334d22d347e289786fb97945ffe08 [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);
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100174 clp->cl_time = ktime_get_boottime_seconds();
J. Bruce Fields221a6872013-04-01 22:23:49 -0400175}
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
Trond Myklebusta451b122020-03-01 18:21:38 -0500497 if (!f)
498 return NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400499 spin_lock(&f->fi_lock);
500 ret = __nfs4_get_fd(f, O_RDWR);
501 if (!ret) {
502 ret = __nfs4_get_fd(f, O_WRONLY);
503 if (!ret)
504 ret = __nfs4_get_fd(f, O_RDONLY);
505 }
506 spin_unlock(&f->fi_lock);
507 return ret;
508}
509
Trond Myklebust02a35082014-07-25 07:34:22 -0400510static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800511unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700512
513/*
514 * Open owner state (share locks)
515 */
516
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500517/* hash tables for lock and open owners */
518#define OWNER_HASH_BITS 8
519#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
520#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700521
Trond Myklebustd4f04892014-07-29 21:34:36 -0400522static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400523{
524 unsigned int ret;
525
526 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500527 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400528}
NeilBrownef0f3392006-04-10 22:55:41 -0700529
NeilBrownef0f3392006-04-10 22:55:41 -0700530/* hash table for nfs4_file */
531#define FILE_HASH_BITS 8
532#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800533
Trond Myklebustca943212014-07-23 16:17:39 -0400534static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400535{
Trond Myklebustca943212014-07-23 16:17:39 -0400536 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
537}
538
539static unsigned int file_hashval(struct knfsd_fh *fh)
540{
541 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
542}
543
Jeff Layton89876f82013-04-02 09:01:59 -0400544static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700545
Jeff Layton12659652014-07-10 14:07:28 -0400546static void
547__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400548{
Jeff Layton7214e862014-07-10 14:07:33 -0400549 lockdep_assert_held(&fp->fi_lock);
550
Jeff Layton12659652014-07-10 14:07:28 -0400551 if (access & NFS4_SHARE_ACCESS_WRITE)
552 atomic_inc(&fp->fi_access[O_WRONLY]);
553 if (access & NFS4_SHARE_ACCESS_READ)
554 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400555}
556
Jeff Layton12659652014-07-10 14:07:28 -0400557static __be32
558nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400559{
Jeff Layton7214e862014-07-10 14:07:33 -0400560 lockdep_assert_held(&fp->fi_lock);
561
Jeff Layton12659652014-07-10 14:07:28 -0400562 /* Does this access mode make sense? */
563 if (access & ~NFS4_SHARE_ACCESS_BOTH)
564 return nfserr_inval;
565
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400566 /* Does it conflict with a deny mode already set? */
567 if ((access & fp->fi_share_deny) != 0)
568 return nfserr_share_denied;
569
Jeff Layton12659652014-07-10 14:07:28 -0400570 __nfs4_file_get_access(fp, access);
571 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400572}
573
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400574static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
575{
576 /* Common case is that there is no deny mode. */
577 if (deny) {
578 /* Does this deny mode make sense? */
579 if (deny & ~NFS4_SHARE_DENY_BOTH)
580 return nfserr_inval;
581
582 if ((deny & NFS4_SHARE_DENY_READ) &&
583 atomic_read(&fp->fi_access[O_RDONLY]))
584 return nfserr_share_denied;
585
586 if ((deny & NFS4_SHARE_DENY_WRITE) &&
587 atomic_read(&fp->fi_access[O_WRONLY]))
588 return nfserr_share_denied;
589 }
590 return nfs_ok;
591}
592
J. Bruce Fields998db522010-08-07 09:21:41 -0400593static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400594{
Trond Myklebustde186432014-07-10 14:07:26 -0400595 might_lock(&fp->fi_lock);
596
597 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400598 struct nfsd_file *f1 = NULL;
599 struct nfsd_file *f2 = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400600
Jeff Layton6d338b52014-07-10 14:07:29 -0400601 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400602 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400603 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400604 spin_unlock(&fp->fi_lock);
605 if (f1)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400606 nfsd_file_put(f1);
Trond Myklebustde186432014-07-10 14:07:26 -0400607 if (f2)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400608 nfsd_file_put(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400609 }
610}
611
Jeff Layton12659652014-07-10 14:07:28 -0400612static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400613{
Jeff Layton12659652014-07-10 14:07:28 -0400614 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
615
616 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400617 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400618 if (access & NFS4_SHARE_ACCESS_READ)
619 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400620}
621
Sachin Bhamare8287f002015-04-27 14:50:14 +0200622/*
623 * Allocate a new open/delegation state counter. This is needed for
624 * pNFS for proper return on close semantics.
625 *
626 * Note that we only allocate it for pNFS-enabled exports, otherwise
627 * all pointers to struct nfs4_clnt_odstate are always NULL.
628 */
629static struct nfs4_clnt_odstate *
630alloc_clnt_odstate(struct nfs4_client *clp)
631{
632 struct nfs4_clnt_odstate *co;
633
634 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
635 if (co) {
636 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300637 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200638 }
639 return co;
640}
641
642static void
643hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
644{
645 struct nfs4_file *fp = co->co_file;
646
647 lockdep_assert_held(&fp->fi_lock);
648 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
649}
650
651static inline void
652get_clnt_odstate(struct nfs4_clnt_odstate *co)
653{
654 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300655 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200656}
657
658static void
659put_clnt_odstate(struct nfs4_clnt_odstate *co)
660{
661 struct nfs4_file *fp;
662
663 if (!co)
664 return;
665
666 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300667 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200668 list_del(&co->co_perfile);
669 spin_unlock(&fp->fi_lock);
670
671 nfsd4_return_all_file_layouts(co->co_client, fp);
672 kmem_cache_free(odstate_slab, co);
673 }
674}
675
676static struct nfs4_clnt_odstate *
677find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
678{
679 struct nfs4_clnt_odstate *co;
680 struct nfs4_client *cl;
681
682 if (!new)
683 return NULL;
684
685 cl = new->co_client;
686
687 spin_lock(&fp->fi_lock);
688 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
689 if (co->co_client == cl) {
690 get_clnt_odstate(co);
691 goto out;
692 }
693 }
694 co = new;
695 co->co_file = fp;
696 hash_clnt_odstate_locked(new);
697out:
698 spin_unlock(&fp->fi_lock);
699 return co;
700}
701
Kinglong Meed19fb702017-01-18 19:04:42 +0800702struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
703 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400704{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500705 struct nfs4_stid *stid;
706 int new_id;
707
Trond Myklebustf8338832014-07-25 07:34:19 -0400708 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500709 if (!stid)
710 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400711
Jeff Layton4770d722014-07-29 21:34:10 -0400712 idr_preload(GFP_KERNEL);
713 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400714 /* Reserving 0 for start of file in nfsdfs "states" file: */
715 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400716 spin_unlock(&cl->cl_lock);
717 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700718 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500719 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800720
721 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500722 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500723 stid->sc_stateid.si_opaque.so_id = new_id;
724 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
725 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300726 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400727 spin_lock_init(&stid->sc_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400728 INIT_LIST_HEAD(&stid->sc_cp_list);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500729
J. Bruce Fields996e0932011-10-17 11:14:48 -0400730 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500731 * It shouldn't be a problem to reuse an opaque stateid value.
732 * I don't think it is for 4.1. But with 4.0 I worry that, for
733 * example, a stray write retransmission could be accepted by
734 * the server when it should have been rejected. Therefore,
735 * adopt a trick from the sctp code to attempt to maximize the
736 * amount of time until an id is reused, by ensuring they always
737 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400738 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500739 return stid;
740out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800741 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500742 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400743}
744
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400745/*
746 * Create a unique stateid_t to represent each COPY.
747 */
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400748static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
749 unsigned char sc_type)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400750{
751 int new_id;
752
Arnd Bergmann9cc76802019-10-31 15:53:13 +0100753 stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400754 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
755 stid->sc_type = sc_type;
756
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400757 idr_preload(GFP_KERNEL);
758 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400759 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
760 stid->stid.si_opaque.so_id = new_id;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400761 spin_unlock(&nn->s2s_cp_lock);
762 idr_preload_end();
763 if (new_id < 0)
764 return 0;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400765 return 1;
766}
767
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400768int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
769{
770 return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
771}
772
773struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
774 struct nfs4_stid *p_stid)
775{
776 struct nfs4_cpntf_state *cps;
777
778 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
779 if (!cps)
780 return NULL;
Arnd Bergmann20b7d862019-11-04 16:31:52 +0100781 cps->cpntf_time = ktime_get_boottime_seconds();
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400782 refcount_set(&cps->cp_stateid.sc_count, 1);
783 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
784 goto out_free;
785 spin_lock(&nn->s2s_cp_lock);
786 list_add(&cps->cp_list, &p_stid->sc_cp_list);
787 spin_unlock(&nn->s2s_cp_lock);
788 return cps;
789out_free:
790 kfree(cps);
791 return NULL;
792}
793
794void nfs4_free_copy_state(struct nfsd4_copy *copy)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400795{
796 struct nfsd_net *nn;
797
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400798 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400799 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
800 spin_lock(&nn->s2s_cp_lock);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400801 idr_remove(&nn->s2s_cp_stateids,
802 copy->cp_stateid.stid.si_opaque.so_id);
803 spin_unlock(&nn->s2s_cp_lock);
804}
805
806static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
807{
808 struct nfs4_cpntf_state *cps;
809 struct nfsd_net *nn;
810
811 nn = net_generic(net, nfsd_net_id);
812 spin_lock(&nn->s2s_cp_lock);
813 while (!list_empty(&stid->sc_cp_list)) {
814 cps = list_first_entry(&stid->sc_cp_list,
815 struct nfs4_cpntf_state, cp_list);
816 _free_cpntf_state_locked(nn, cps);
817 }
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400818 spin_unlock(&nn->s2s_cp_lock);
819}
820
Jeff Laytonb49e0842014-07-29 21:34:11 -0400821static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400822{
Trond Myklebust60116952014-07-29 21:34:06 -0400823 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400824
Kinglong Meed19fb702017-01-18 19:04:42 +0800825 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400826 if (!stid)
827 return NULL;
828
Kinglong Meed19fb702017-01-18 19:04:42 +0800829 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400830}
831
832static void nfs4_free_deleg(struct nfs4_stid *stid)
833{
Trond Myklebust60116952014-07-29 21:34:06 -0400834 kmem_cache_free(deleg_slab, stid);
835 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400836}
837
NeilBrown6282cd52014-06-04 17:39:26 +1000838/*
839 * When we recall a delegation, we should be careful not to hand it
840 * out again straight away.
841 * To ensure this we keep a pair of bloom filters ('new' and 'old')
842 * in which the filehandles of recalled delegations are "stored".
843 * If a filehandle appear in either filter, a delegation is blocked.
844 * When a delegation is recalled, the filehandle is stored in the "new"
845 * filter.
846 * Every 30 seconds we swap the filters and clear the "new" one,
847 * unless both are empty of course.
848 *
849 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
850 * low 3 bytes as hash-table indices.
851 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400852 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000853 * is used to manage concurrent access. Testing does not need the lock
854 * except when swapping the two filters.
855 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400856static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000857static struct bloom_pair {
858 int entries, old_entries;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200859 time64_t swap_time;
NeilBrown6282cd52014-06-04 17:39:26 +1000860 int new; /* index into 'set' */
861 DECLARE_BITMAP(set[2], 256);
862} blocked_delegations;
863
864static int delegation_blocked(struct knfsd_fh *fh)
865{
866 u32 hash;
867 struct bloom_pair *bd = &blocked_delegations;
868
869 if (bd->entries == 0)
870 return 0;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200871 if (ktime_get_seconds() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400872 spin_lock(&blocked_delegations_lock);
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200873 if (ktime_get_seconds() - bd->swap_time > 30) {
NeilBrown6282cd52014-06-04 17:39:26 +1000874 bd->entries -= bd->old_entries;
875 bd->old_entries = bd->entries;
876 memset(bd->set[bd->new], 0,
877 sizeof(bd->set[0]));
878 bd->new = 1-bd->new;
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200879 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +1000880 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400881 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000882 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100883 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000884 if (test_bit(hash&255, bd->set[0]) &&
885 test_bit((hash>>8)&255, bd->set[0]) &&
886 test_bit((hash>>16)&255, bd->set[0]))
887 return 1;
888
889 if (test_bit(hash&255, bd->set[1]) &&
890 test_bit((hash>>8)&255, bd->set[1]) &&
891 test_bit((hash>>16)&255, bd->set[1]))
892 return 1;
893
894 return 0;
895}
896
897static void block_delegations(struct knfsd_fh *fh)
898{
899 u32 hash;
900 struct bloom_pair *bd = &blocked_delegations;
901
Daniel Borkmann87545892014-12-10 16:33:11 +0100902 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000903
Jeff Laytonf54fe962014-07-25 07:34:26 -0400904 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000905 __set_bit(hash&255, bd->set[bd->new]);
906 __set_bit((hash>>8)&255, bd->set[bd->new]);
907 __set_bit((hash>>16)&255, bd->set[bd->new]);
908 if (bd->entries == 0)
Arnd Bergmannb3f255e2017-10-20 11:25:34 +0200909 bd->swap_time = ktime_get_seconds();
NeilBrown6282cd52014-06-04 17:39:26 +1000910 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400911 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000912}
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500915alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
916 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200917 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400920 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400923 n = atomic_long_inc_return(&num_delegations);
924 if (n < 0 || n > max_delegations)
925 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000926 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400927 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800928 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700929 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400930 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400931
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400932 /*
933 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400934 * meaning of seqid 0 isn't meaningful, really, but let's avoid
935 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400936 */
937 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700938 INIT_LIST_HEAD(&dp->dl_perfile);
939 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200941 dp->dl_clnt_odstate = odstate;
942 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400943 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200944 dp->dl_retries = 1;
945 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200946 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500947 get_nfs4_file(fp);
948 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400950out_dec:
951 atomic_long_dec(&num_delegations);
952 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955void
Trond Myklebust60116952014-07-29 21:34:06 -0400956nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Trond Myklebust11b91642014-07-29 21:34:08 -0400958 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400959 struct nfs4_client *clp = s->sc_client;
960
Jeff Layton4770d722014-07-29 21:34:10 -0400961 might_lock(&clp->cl_lock);
962
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300963 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400964 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400965 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400966 }
Trond Myklebust60116952014-07-29 21:34:06 -0400967 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Olga Kornievskaia624322f2019-10-04 16:34:26 -0400968 nfs4_free_cpntf_statelist(clp->net, s);
Jeff Layton4770d722014-07-29 21:34:10 -0400969 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400970 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400971 if (fp)
972 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Jeff Layton9767feb2015-10-01 09:05:50 -0400975void
976nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
977{
978 stateid_t *src = &stid->sc_stateid;
979
980 spin_lock(&stid->sc_lock);
981 if (unlikely(++src->si_generation == 0))
982 src->si_generation = 1;
983 memcpy(dst, src, sizeof(*dst));
984 spin_unlock(&stid->sc_lock);
985}
986
J. Bruce Fields353601e2018-02-16 14:29:42 -0500987static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400989 struct nfsd_file *nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500990
991 spin_lock(&fp->fi_lock);
992 if (--fp->fi_delegees == 0)
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400993 swap(nf, fp->fi_deleg_file);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500994 spin_unlock(&fp->fi_lock);
995
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400996 if (nf)
997 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500998}
999
1000static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1001{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -05001002 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001003 struct nfsd_file *nf = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -04001004
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -05001005 WARN_ON_ONCE(!fp->fi_delegees);
1006
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001007 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001008 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001011static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1012{
1013 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05001014 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001015 nfs4_put_stid(&dp->dl_stid);
1016}
1017
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001018void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001019{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05001020 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -04001021}
1022
Andrew Elble34ed9872015-10-15 12:07:28 -04001023/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001024 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -04001025 * @clp: a pointer to the nfs4_client we're granting a delegation to
1026 * @fp: a pointer to the nfs4_file we're granting a delegation on
1027 *
1028 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001029 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -04001030 */
1031
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001032static bool
1033nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -04001034{
Andrew Elble34ed9872015-10-15 12:07:28 -04001035 struct nfs4_delegation *searchdp = NULL;
1036 struct nfs4_client *searchclp = NULL;
1037
Benny Halevycdc975052014-05-30 09:09:30 -04001038 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04001039 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001040
Andrew Elble34ed9872015-10-15 12:07:28 -04001041 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1042 searchclp = searchdp->dl_stid.sc_client;
1043 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001044 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -04001045 }
1046 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +08001047 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -04001048}
1049
1050/**
1051 * hash_delegation_locked - Add a delegation to the appropriate lists
1052 * @dp: a pointer to the nfs4_delegation we are adding.
1053 * @fp: a pointer to the nfs4_file we're granting a delegation on
1054 *
1055 * Return:
1056 * On success: NULL if the delegation was successfully hashed.
1057 *
1058 * On error: -EAGAIN if one was previously granted to this
1059 * nfs4_client for this nfs4_file. Delegation is not hashed.
1060 *
1061 */
1062
1063static int
1064hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1065{
Andrew Elble34ed9872015-10-15 12:07:28 -04001066 struct nfs4_client *clp = dp->dl_stid.sc_client;
1067
1068 lockdep_assert_held(&state_lock);
1069 lockdep_assert_held(&fp->fi_lock);
1070
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001071 if (nfs4_delegation_exists(clp, fp))
1072 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001073 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001074 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001075 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001076 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1077 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001078}
1079
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001080static bool
Jeff Layton42690672014-07-25 07:34:20 -04001081unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Trond Myklebust11b91642014-07-29 21:34:08 -04001083 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001084
Jeff Layton42690672014-07-25 07:34:20 -04001085 lockdep_assert_held(&state_lock);
1086
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001087 if (list_empty(&dp->dl_perfile))
1088 return false;
1089
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001090 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001091 /* Ensure that deleg break won't try to requeue it */
1092 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001093 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001094 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001096 list_del_init(&dp->dl_perfile);
1097 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001098 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001099}
1100
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001101static void destroy_delegation(struct nfs4_delegation *dp)
1102{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001103 bool unhashed;
1104
Jeff Layton42690672014-07-25 07:34:20 -04001105 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001106 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001107 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001108 if (unhashed)
1109 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001110}
1111
1112static void revoke_delegation(struct nfs4_delegation *dp)
1113{
1114 struct nfs4_client *clp = dp->dl_stid.sc_client;
1115
Jeff Layton2d4a5322014-07-25 07:34:21 -04001116 WARN_ON(!list_empty(&dp->dl_recall_lru));
1117
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001118 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001119 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001120 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001121 spin_lock(&clp->cl_lock);
1122 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1123 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001124 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001125 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001126}
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128/*
1129 * SETCLIENTID state
1130 */
1131
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001132static unsigned int clientid_hashval(u32 id)
1133{
1134 return id & CLIENT_HASH_MASK;
1135}
1136
Scott Mayhew6b189102019-03-26 18:06:26 -04001137static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001138{
Scott Mayhew6b189102019-03-26 18:06:26 -04001139 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001140}
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001143 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1144 * st_{access,deny}_bmap field of the stateid, in order to track not
1145 * only what share bits are currently in force, but also what
1146 * combinations of share bits previous opens have used. This allows us
1147 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1148 * return an error if the client attempt to downgrade to a combination
1149 * of share bits not explicable by closing some of its previous opens.
1150 *
1151 * XXX: This enforcement is actually incomplete, since we don't keep
1152 * track of access/deny bit combinations; so, e.g., we allow:
1153 *
1154 * OPEN allow read, deny write
1155 * OPEN allow both, deny none
1156 * DOWNGRADE allow read, deny none
1157 *
1158 * which we should reject.
1159 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001160static unsigned int
1161bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001162 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001163 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001164
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001165 for (i = 1; i < 4; i++) {
1166 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001167 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001168 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001169 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001170}
1171
Jeff Layton82c5ff12012-05-11 09:45:13 -04001172/* set share access for a given stateid */
1173static inline void
1174set_access(u32 access, struct nfs4_ol_stateid *stp)
1175{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001176 unsigned char mask = 1 << access;
1177
1178 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1179 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001180}
1181
1182/* clear share access for a given stateid */
1183static inline void
1184clear_access(u32 access, struct nfs4_ol_stateid *stp)
1185{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001186 unsigned char mask = 1 << access;
1187
1188 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1189 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001190}
1191
1192/* test whether a given stateid has access */
1193static inline bool
1194test_access(u32 access, struct nfs4_ol_stateid *stp)
1195{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001196 unsigned char mask = 1 << access;
1197
1198 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001199}
1200
Jeff Laytonce0fc432012-05-11 09:45:14 -04001201/* set share deny for a given stateid */
1202static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001203set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001204{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001205 unsigned char mask = 1 << deny;
1206
1207 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1208 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001209}
1210
1211/* clear share deny for a given stateid */
1212static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001213clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001214{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001215 unsigned char mask = 1 << deny;
1216
1217 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1218 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001219}
1220
1221/* test whether a given stateid is denying specific access */
1222static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001223test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001224{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001225 unsigned char mask = 1 << deny;
1226
1227 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001228}
1229
1230static int nfs4_access_to_omode(u32 access)
1231{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001232 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001233 case NFS4_SHARE_ACCESS_READ:
1234 return O_RDONLY;
1235 case NFS4_SHARE_ACCESS_WRITE:
1236 return O_WRONLY;
1237 case NFS4_SHARE_ACCESS_BOTH:
1238 return O_RDWR;
1239 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001240 WARN_ON_ONCE(1);
1241 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001242}
1243
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001244/*
1245 * A stateid that had a deny mode associated with it is being released
1246 * or downgraded. Recalculate the deny mode on the file.
1247 */
1248static void
1249recalculate_deny_mode(struct nfs4_file *fp)
1250{
1251 struct nfs4_ol_stateid *stp;
1252
1253 spin_lock(&fp->fi_lock);
1254 fp->fi_share_deny = 0;
1255 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1256 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1257 spin_unlock(&fp->fi_lock);
1258}
1259
1260static void
1261reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1262{
1263 int i;
1264 bool change = false;
1265
1266 for (i = 1; i < 4; i++) {
1267 if ((i & deny) != i) {
1268 change = true;
1269 clear_deny(i, stp);
1270 }
1271 }
1272
1273 /* Recalculate per-file deny mode if there was a change */
1274 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001275 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001276}
1277
Jeff Layton82c5ff12012-05-11 09:45:13 -04001278/* release all access and file references for a given stateid */
1279static void
1280release_all_access(struct nfs4_ol_stateid *stp)
1281{
1282 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001283 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001284
1285 if (fp && stp->st_deny_bmap != 0)
1286 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001287
1288 for (i = 1; i < 4; i++) {
1289 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001290 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001291 clear_access(i, stp);
1292 }
1293}
1294
Kinglong Meed50ffde2015-07-16 12:05:07 +08001295static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1296{
1297 kfree(sop->so_owner.data);
1298 sop->so_ops->so_free(sop);
1299}
1300
Jeff Layton6b180f02014-07-29 21:34:26 -04001301static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1302{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001303 struct nfs4_client *clp = sop->so_client;
1304
1305 might_lock(&clp->cl_lock);
1306
1307 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001308 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001309 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001310 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001311 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001312}
1313
Trond Myklebusta451b122020-03-01 18:21:38 -05001314static bool
1315nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1316{
1317 return list_empty(&stp->st_perfile);
1318}
1319
Jeff Laytone8568732015-08-24 12:41:47 -04001320static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001321{
Trond Myklebust11b91642014-07-29 21:34:08 -04001322 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001323
Jeff Layton1c755dc2014-07-29 21:34:12 -04001324 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1325
Jeff Laytone8568732015-08-24 12:41:47 -04001326 if (list_empty(&stp->st_perfile))
1327 return false;
1328
Trond Myklebust1d31a252014-07-10 14:07:25 -04001329 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001330 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001331 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001332 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001333 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001334}
1335
Trond Myklebust60116952014-07-29 21:34:06 -04001336static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001337{
Trond Myklebust60116952014-07-29 21:34:06 -04001338 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001339
Sachin Bhamare8287f002015-04-27 14:50:14 +02001340 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001341 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001342 if (stp->st_stateowner)
1343 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001344 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001345}
1346
Jeff Laytonb49e0842014-07-29 21:34:11 -04001347static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001348{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001349 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1350 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001351 struct nfsd_file *nf;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001352
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001353 nf = find_any_file(stp->st_stid.sc_file);
1354 if (nf) {
1355 get_file(nf->nf_file);
1356 filp_close(nf->nf_file, (fl_owner_t)lo);
1357 nfsd_file_put(nf);
1358 }
Jeff Laytonb49e0842014-07-29 21:34:11 -04001359 nfs4_free_ol_stateid(stid);
1360}
1361
Jeff Layton2c41beb2014-07-29 21:34:41 -04001362/*
1363 * Put the persistent reference to an already unhashed generic stateid, while
1364 * holding the cl_lock. If it's the last reference, then put it onto the
1365 * reaplist for later destruction.
1366 */
1367static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1368 struct list_head *reaplist)
1369{
1370 struct nfs4_stid *s = &stp->st_stid;
1371 struct nfs4_client *clp = s->sc_client;
1372
1373 lockdep_assert_held(&clp->cl_lock);
1374
1375 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1376
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001377 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001378 wake_up_all(&close_wq);
1379 return;
1380 }
1381
1382 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1383 list_add(&stp->st_locks, reaplist);
1384}
1385
Jeff Laytone8568732015-08-24 12:41:47 -04001386static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001387{
Chuck Leverf46c4452016-10-29 18:19:03 -04001388 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001389
Trond Myklebusta451b122020-03-01 18:21:38 -05001390 if (!unhash_ol_stateid(stp))
1391 return false;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001392 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001393 nfs4_unhash_stid(&stp->st_stid);
Trond Myklebusta451b122020-03-01 18:21:38 -05001394 return true;
Jeff Layton3c1c9952014-07-29 21:34:39 -04001395}
1396
Jeff Layton5adfd882014-07-29 21:34:31 -04001397static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001398{
Chuck Leverf46c4452016-10-29 18:19:03 -04001399 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001400 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001401
Chuck Leverf46c4452016-10-29 18:19:03 -04001402 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001403 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001404 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001405 if (unhashed)
1406 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001407}
1408
Trond Myklebustc58c6612014-07-29 21:34:35 -04001409static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001410{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001411 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001412
Trond Myklebustd4f04892014-07-29 21:34:36 -04001413 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001414
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001415 list_del_init(&lo->lo_owner.so_strhash);
1416}
1417
Jeff Layton2c41beb2014-07-29 21:34:41 -04001418/*
1419 * Free a list of generic stateids that were collected earlier after being
1420 * fully unhashed.
1421 */
1422static void
1423free_ol_stateid_reaplist(struct list_head *reaplist)
1424{
1425 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001426 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001427
1428 might_sleep();
1429
1430 while (!list_empty(reaplist)) {
1431 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1432 st_locks);
1433 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001434 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001435 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001436 if (fp)
1437 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001438 }
1439}
1440
Jeff Laytond83017f2014-07-29 21:34:42 -04001441static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1442 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001443{
1444 struct nfs4_ol_stateid *stp;
1445
Jeff Laytone8568732015-08-24 12:41:47 -04001446 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1447
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001448 while (!list_empty(&open_stp->st_locks)) {
1449 stp = list_entry(open_stp->st_locks.next,
1450 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001451 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001452 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001453 }
1454}
1455
Jeff Laytone8568732015-08-24 12:41:47 -04001456static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001457 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001458{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001459 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1460
Trond Myklebusta451b122020-03-01 18:21:38 -05001461 if (!unhash_ol_stateid(stp))
1462 return false;
Jeff Laytond83017f2014-07-29 21:34:42 -04001463 release_open_stateid_locks(stp, reaplist);
Trond Myklebusta451b122020-03-01 18:21:38 -05001464 return true;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001465}
1466
1467static void release_open_stateid(struct nfs4_ol_stateid *stp)
1468{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001469 LIST_HEAD(reaplist);
1470
1471 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001472 if (unhash_open_stateid(stp, &reaplist))
1473 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001474 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1475 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001476}
1477
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001478static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001479{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001480 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001481
Trond Myklebustd4f04892014-07-29 21:34:36 -04001482 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001483
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001484 list_del_init(&oo->oo_owner.so_strhash);
1485 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001486}
1487
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001488static void release_last_closed_stateid(struct nfs4_openowner *oo)
1489{
Jeff Layton217526e2014-07-30 08:27:11 -04001490 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1491 nfsd_net_id);
1492 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001493
Jeff Layton217526e2014-07-30 08:27:11 -04001494 spin_lock(&nn->client_lock);
1495 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001496 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001497 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001498 oo->oo_last_closed_stid = NULL;
1499 }
Jeff Layton217526e2014-07-30 08:27:11 -04001500 spin_unlock(&nn->client_lock);
1501 if (s)
1502 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001503}
1504
Jeff Layton2c41beb2014-07-29 21:34:41 -04001505static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001506{
1507 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001508 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001509 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001510
Jeff Layton2c41beb2014-07-29 21:34:41 -04001511 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001512
Trond Myklebustd4f04892014-07-29 21:34:36 -04001513 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001514 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001515 while (!list_empty(&oo->oo_owner.so_stateids)) {
1516 stp = list_first_entry(&oo->oo_owner.so_stateids,
1517 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001518 if (unhash_open_stateid(stp, &reaplist))
1519 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001520 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001521 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001522 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001523 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001524 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001525}
1526
Marc Eshel5282fd72009-04-03 08:27:52 +03001527static inline int
1528hash_sessionid(struct nfs4_sessionid *sessionid)
1529{
1530 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1531
1532 return sid->sequence % SESSION_HASH_SIZE;
1533}
1534
Mark Salter135dd002015-04-06 09:46:00 -04001535#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001536static inline void
1537dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1538{
1539 u32 *ptr = (u32 *)(&sessionid->data[0]);
1540 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1541}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001542#else
1543static inline void
1544dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1545{
1546}
1547#endif
1548
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001549/*
1550 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1551 * won't be used for replay.
1552 */
1553void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1554{
1555 struct nfs4_stateowner *so = cstate->replay_owner;
1556
1557 if (nfserr == nfserr_replay_me)
1558 return;
1559
1560 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001561 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001562 return;
1563 }
1564 if (!so)
1565 return;
1566 if (so->so_is_open_owner)
1567 release_last_closed_stateid(openowner(so));
1568 so->so_seqid++;
1569 return;
1570}
Marc Eshel5282fd72009-04-03 08:27:52 +03001571
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001572static void
1573gen_sessionid(struct nfsd4_session *ses)
1574{
1575 struct nfs4_client *clp = ses->se_client;
1576 struct nfsd4_sessionid *sid;
1577
1578 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1579 sid->clientid = clp->cl_clientid;
1580 sid->sequence = current_sessionid++;
1581 sid->reserved = 0;
1582}
1583
1584/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001585 * The protocol defines ca_maxresponssize_cached to include the size of
1586 * the rpc header, but all we need to cache is the data starting after
1587 * the end of the initial SEQUENCE operation--the rest we regenerate
1588 * each time. Therefore we can advertise a ca_maxresponssize_cached
1589 * value that is the number of bytes in our cache plus a few additional
1590 * bytes. In order to stay on the safe side, and not promise more than
1591 * we can cache, those additional bytes must be the minimum possible: 24
1592 * bytes of rpc header (xid through accept state, with AUTH_NULL
1593 * verifier), 12 for the compound header (with zero-length tag), and 44
1594 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001595 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001596#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1597
Andy Adamson557ce262009-08-28 08:45:04 -04001598static void
1599free_session_slots(struct nfsd4_session *ses)
1600{
1601 int i;
1602
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001603 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1604 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001605 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001606 }
Andy Adamson557ce262009-08-28 08:45:04 -04001607}
1608
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001609/*
1610 * We don't actually need to cache the rpc and session headers, so we
1611 * can allocate a little less for each slot:
1612 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001613static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001614{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001615 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001616
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001617 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1618 size = 0;
1619 else
1620 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1621 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001622}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001623
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001624/*
1625 * XXX: If we run out of reserved DRC memory we could (up to a point)
1626 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001627 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001628 */
NeilBrown2030ca52019-09-20 16:36:45 +10001629static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001630{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001631 u32 slotsize = slot_bytes(ca);
1632 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001633 unsigned long avail, total_avail;
NeilBrown2030ca52019-09-20 16:36:45 +10001634 unsigned int scale_factor;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001635
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001636 spin_lock(&nfsd_drc_lock);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001637 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1638 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1639 else
1640 /* We have handed out more space than we chose in
1641 * set_max_drc() to allow. That isn't really a
1642 * problem as long as that doesn't make us think we
1643 * have lots more due to integer overflow.
1644 */
1645 total_avail = 0;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001646 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001647 /*
NeilBrown2030ca52019-09-20 16:36:45 +10001648 * Never use more than a fraction of the remaining memory,
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001649 * unless it's the only way to give this client a slot.
NeilBrown2030ca52019-09-20 16:36:45 +10001650 * The chosen fraction is either 1/8 or 1/number of threads,
1651 * whichever is smaller. This ensures there are adequate
1652 * slots to support multiple clients per thread.
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001653 * Give the client one slot even if that would require
1654 * over-allocation--it is better than failure.
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001655 */
NeilBrown2030ca52019-09-20 16:36:45 +10001656 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1657
1658 avail = clamp_t(unsigned long, avail, slotsize,
1659 total_avail/scale_factor);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001660 num = min_t(int, num, avail / slotsize);
NeilBrown7f49fd5d2019-09-20 16:33:16 +10001661 num = max_t(int, num, 1);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001662 nfsd_drc_mem_used += num * slotsize;
1663 spin_unlock(&nfsd_drc_lock);
1664
1665 return num;
1666}
1667
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001668static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001669{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001670 int slotsize = slot_bytes(ca);
1671
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001672 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001673 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001674 spin_unlock(&nfsd_drc_lock);
1675}
1676
Kinglong Mee60810e52014-01-01 00:35:47 +08001677static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1678 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001679{
Kinglong Mee60810e52014-01-01 00:35:47 +08001680 int numslots = fattrs->maxreqs;
1681 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001682 struct nfsd4_session *new;
1683 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001684
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001685 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001686 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1687 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001688
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001689 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001690 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001691 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001692 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001693 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001694 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001695 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001696 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001697 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001698
1699 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1700 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1701
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001702 return new;
1703out_free:
1704 while (i--)
1705 kfree(new->se_slots[i]);
1706 kfree(new);
1707 return NULL;
1708}
1709
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001710static void free_conn(struct nfsd4_conn *c)
1711{
1712 svc_xprt_put(c->cn_xprt);
1713 kfree(c);
1714}
1715
1716static void nfsd4_conn_lost(struct svc_xpt_user *u)
1717{
1718 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1719 struct nfs4_client *clp = c->cn_session->se_client;
1720
1721 spin_lock(&clp->cl_lock);
1722 if (!list_empty(&c->cn_persession)) {
1723 list_del(&c->cn_persession);
1724 free_conn(c);
1725 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001726 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001727 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001728}
1729
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001730static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001731{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001732 struct nfsd4_conn *conn;
1733
1734 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1735 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001736 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001737 svc_xprt_get(rqstp->rq_xprt);
1738 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001739 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001740 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1741 return conn;
1742}
1743
J. Bruce Fields328ead22010-09-29 16:11:06 -04001744static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1745{
1746 conn->cn_session = ses;
1747 list_add(&conn->cn_persession, &ses->se_conns);
1748}
1749
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001750static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1751{
1752 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001753
1754 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001755 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001756 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001757}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001758
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001759static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001760{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001761 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001762 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001763}
1764
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001765static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001766{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001767 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001768
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001769 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001770 ret = nfsd4_register_conn(conn);
1771 if (ret)
1772 /* oops; xprt is already down: */
1773 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001774 /* We may have gained or lost a callback channel: */
1775 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001776}
1777
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001778static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001779{
1780 u32 dir = NFS4_CDFC4_FORE;
1781
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001782 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001783 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001784 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001785}
1786
1787/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001788static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001789{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001790 struct nfs4_client *clp = s->se_client;
1791 struct nfsd4_conn *c;
1792
1793 spin_lock(&clp->cl_lock);
1794 while (!list_empty(&s->se_conns)) {
1795 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1796 list_del_init(&c->cn_persession);
1797 spin_unlock(&clp->cl_lock);
1798
1799 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1800 free_conn(c);
1801
1802 spin_lock(&clp->cl_lock);
1803 }
1804 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001805}
1806
J. Bruce Fields1377b692012-09-11 21:42:40 -04001807static void __free_session(struct nfsd4_session *ses)
1808{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001809 free_session_slots(ses);
1810 kfree(ses);
1811}
1812
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001813static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001814{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001815 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001816 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001817 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001818}
Andy Adamson557ce262009-08-28 08:45:04 -04001819
Fengguang Wu135ae822012-11-10 07:20:25 -05001820static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001821{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001822 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001823 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001824
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001825 new->se_client = clp;
1826 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001827
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001828 INIT_LIST_HEAD(&new->se_conns);
1829
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001830 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001831 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001832 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001833 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001834 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001835 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001836 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001837 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001838 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001839 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001840
Chuck Leverb0d2e422014-08-22 15:10:59 -04001841 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001842 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001843 /*
1844 * This is a little silly; with sessions there's no real
1845 * use for the callback address. Use the peer address
1846 * as a reasonable default for now, but consider fixing
1847 * the rpc client not to require an address in the
1848 * future:
1849 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001850 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1851 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001852 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001853}
1854
Benny Halevy9089f1b2010-05-12 00:12:26 +03001855/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001856static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001857__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001858{
1859 struct nfsd4_session *elem;
1860 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001861 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001862
Trond Myklebust0a880a22014-07-30 08:27:10 -04001863 lockdep_assert_held(&nn->client_lock);
1864
Marc Eshel5282fd72009-04-03 08:27:52 +03001865 dump_sessionid(__func__, sessionid);
1866 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001867 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001868 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001869 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1870 NFS4_MAX_SESSIONID_LEN)) {
1871 return elem;
1872 }
1873 }
1874
1875 dprintk("%s: session not found\n", __func__);
1876 return NULL;
1877}
1878
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001879static struct nfsd4_session *
1880find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1881 __be32 *ret)
1882{
1883 struct nfsd4_session *session;
1884 __be32 status = nfserr_badsession;
1885
1886 session = __find_in_sessionid_hashtbl(sessionid, net);
1887 if (!session)
1888 goto out;
1889 status = nfsd4_get_session_locked(session);
1890 if (status)
1891 session = NULL;
1892out:
1893 *ret = status;
1894 return session;
1895}
1896
Benny Halevy9089f1b2010-05-12 00:12:26 +03001897/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001898static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001899unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001900{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001901 struct nfs4_client *clp = ses->se_client;
1902 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1903
1904 lockdep_assert_held(&nn->client_lock);
1905
Andy Adamson7116ed62009-04-03 08:27:43 +03001906 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001907 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001908 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001909 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001910}
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1913static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001914STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001916 /*
1917 * We're assuming the clid was not given out from a boot
1918 * precisely 2^32 (about 136 years) before this one. That seems
1919 * a safe assumption:
1920 */
1921 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return 0;
Arnd Bergmann9cc76802019-10-31 15:53:13 +01001923 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08llx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001924 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return 1;
1926}
1927
1928/*
1929 * XXX Should we use a slab cache ?
1930 * This type of memory management is somewhat inefficient, but we use it
1931 * anyway since SETCLIENTID is not a common operation.
1932 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001933static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001936 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Jeff Layton9258a2d2018-03-16 09:47:22 -04001938 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001939 if (clp == NULL)
1940 return NULL;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04001941 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001942 if (clp->cl_name.data == NULL)
1943 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001944 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1945 sizeof(struct list_head),
1946 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001947 if (!clp->cl_ownerstr_hashtbl)
1948 goto err_no_hashtbl;
1949 for (i = 0; i < OWNER_HASH_SIZE; i++)
1950 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
Trond Myklebust5694c932014-04-18 14:43:56 -04001951 INIT_LIST_HEAD(&clp->cl_sessions);
1952 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001953 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04001954 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1955 INIT_LIST_HEAD(&clp->cl_idhash);
1956 INIT_LIST_HEAD(&clp->cl_openowners);
1957 INIT_LIST_HEAD(&clp->cl_delegations);
1958 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001959 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001960#ifdef CONFIG_NFSD_PNFS
1961 INIT_LIST_HEAD(&clp->cl_lo_states);
1962#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001963 INIT_LIST_HEAD(&clp->async_copies);
1964 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001965 spin_lock_init(&clp->cl_lock);
1966 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001968err_no_hashtbl:
1969 kfree(clp->cl_name.data);
1970err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001971 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001972 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001975static void __free_client(struct kref *k)
1976{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001977 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1978 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001979
1980 free_svc_cred(&clp->cl_cred);
1981 kfree(clp->cl_ownerstr_hashtbl);
1982 kfree(clp->cl_name.data);
J. Bruce Fields79123442019-06-05 12:42:05 -04001983 kfree(clp->cl_nii_domain.data);
1984 kfree(clp->cl_nii_name.data);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001985 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001986 kmem_cache_free(client_slab, clp);
1987}
1988
YueHaibing297e57a2019-07-08 15:29:33 +08001989static void drop_client(struct nfs4_client *clp)
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001990{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001991 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001992}
1993
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001994static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995free_client(struct nfs4_client *clp)
1996{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001997 while (!list_empty(&clp->cl_sessions)) {
1998 struct nfsd4_session *ses;
1999 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2000 se_perclnt);
2001 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04002002 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2003 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002004 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04002005 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002006 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002007 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002008 clp->cl_nfsd_dentry = NULL;
2009 wake_up_all(&expiry_wq);
2010 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04002011 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
Benny Halevy84d38ac2010-05-12 00:13:16 +03002014/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04002015static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03002016unhash_client_locked(struct nfs4_client *clp)
2017{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002018 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002019 struct nfsd4_session *ses;
2020
Trond Myklebust0a880a22014-07-30 08:27:10 -04002021 lockdep_assert_held(&nn->client_lock);
2022
Trond Myklebust4beb3452014-07-30 08:27:02 -04002023 /* Mark the client as expired! */
2024 clp->cl_time = 0;
2025 /* Make it invisible */
2026 if (!list_empty(&clp->cl_idhash)) {
2027 list_del_init(&clp->cl_idhash);
2028 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2029 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2030 else
2031 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2032 }
2033 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002034 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04002035 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2036 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04002037 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03002038}
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04002041unhash_client(struct nfs4_client *clp)
2042{
2043 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2044
2045 spin_lock(&nn->client_lock);
2046 unhash_client_locked(clp);
2047 spin_unlock(&nn->client_lock);
2048}
2049
Jeff Layton97403d92014-07-30 08:27:12 -04002050static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2051{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04002052 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04002053 return nfserr_jukebox;
2054 unhash_client_locked(clp);
2055 return nfs_ok;
2056}
2057
Trond Myklebust4beb3452014-07-30 08:27:02 -04002058static void
2059__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002061 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002062 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 struct list_head reaplist;
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04002067 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07002068 while (!list_empty(&clp->cl_delegations)) {
2069 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04002070 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04002071 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
Benny Halevycdc975052014-05-30 09:09:30 -04002073 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 while (!list_empty(&reaplist)) {
2075 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04002076 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05002077 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04002079 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05002080 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04002081 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04002082 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002083 }
NeilBrownea1da632005-06-23 22:04:17 -07002084 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002085 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002086 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002087 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002089 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2090 struct nfs4_stateowner *so, *tmp;
2091
2092 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2093 so_strhash) {
2094 /* Should be no openowners at this point */
2095 WARN_ON_ONCE(so->so_is_open_owner);
2096 remove_blocked_locks(lockowner(so));
2097 }
2098 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002099 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002100 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002101 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002102 if (clp->cl_cb_conn.cb_xprt)
2103 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002104 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002105 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
Trond Myklebust4beb3452014-07-30 08:27:02 -04002108static void
2109destroy_client(struct nfs4_client *clp)
2110{
2111 unhash_client(clp);
2112 __destroy_client(clp);
2113}
2114
Scott Mayhew362063a2019-03-26 18:06:28 -04002115static void inc_reclaim_complete(struct nfs4_client *clp)
2116{
2117 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2118
2119 if (!nn->track_reclaim_completes)
2120 return;
2121 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2122 return;
2123 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2124 nn->reclaim_str_hashtbl_size) {
2125 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2126 clp->net->ns.inum);
2127 nfsd4_end_grace(nn);
2128 }
2129}
2130
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002131static void expire_client(struct nfs4_client *clp)
2132{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002133 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002134 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002135 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002136}
2137
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002138static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2139{
2140 memcpy(target->cl_verifier.data, source->data,
2141 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002144static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2145{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2147 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2148}
2149
J. Bruce Fields50043852015-11-20 15:58:37 -05002150static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2151{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002152 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2153 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2154 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002155 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2156 if ((source->cr_principal && !target->cr_principal) ||
2157 (source->cr_raw_principal && !target->cr_raw_principal) ||
2158 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002159 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002160
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002161 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 target->cr_uid = source->cr_uid;
2163 target->cr_gid = source->cr_gid;
2164 target->cr_group_info = source->cr_group_info;
2165 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002166 target->cr_gss_mech = source->cr_gss_mech;
2167 if (source->cr_gss_mech)
2168 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002172static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002173compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2174{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002175 if (o1->len < o2->len)
2176 return -1;
2177 if (o1->len > o2->len)
2178 return 1;
2179 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002180}
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002183same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2184{
2185 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
2188static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002189same_clid(clientid_t *cl1, clientid_t *cl2)
2190{
2191 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002194static bool groups_equal(struct group_info *g1, struct group_info *g2)
2195{
2196 int i;
2197
2198 if (g1->ngroups != g2->ngroups)
2199 return false;
2200 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002201 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002202 return false;
2203 return true;
2204}
2205
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002206/*
2207 * RFC 3530 language requires clid_inuse be returned when the
2208 * "principal" associated with a requests differs from that previously
2209 * used. We use uid, gid's, and gss principal string as our best
2210 * approximation. We also don't want to allow non-gss use of a client
2211 * established using gss: in theory cr_principal should catch that
2212 * change, but in practice cr_principal can be null even in the gss case
2213 * since gssd doesn't always pass down a principal string.
2214 */
2215static bool is_gss_cred(struct svc_cred *cr)
2216{
2217 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2218 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2219}
2220
2221
Vivek Trivedi5559b502012-07-24 21:18:20 +05302222static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002223same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2224{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002225 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002226 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2227 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002228 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2229 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002230 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002231 if (cr1->cr_principal == cr2->cr_principal)
2232 return true;
2233 if (!cr1->cr_principal || !cr2->cr_principal)
2234 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302235 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
J. Bruce Fields57266a62013-04-13 14:27:29 -04002238static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2239{
2240 struct svc_cred *cr = &rqstp->rq_cred;
2241 u32 service;
2242
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002243 if (!cr->cr_gss_mech)
2244 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002245 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2246 return service == RPC_GSS_SVC_INTEGRITY ||
2247 service == RPC_GSS_SVC_PRIVACY;
2248}
2249
Andrew Elblededeb132016-06-15 12:52:08 -04002250bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002251{
2252 struct svc_cred *cr = &rqstp->rq_cred;
2253
2254 if (!cl->cl_mach_cred)
2255 return true;
2256 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2257 return false;
2258 if (!svc_rqst_integrity_protected(rqstp))
2259 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002260 if (cl->cl_cred.cr_raw_principal)
2261 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2262 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002263 if (!cr->cr_principal)
2264 return false;
2265 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2266}
2267
Jeff Layton294ac322014-07-30 08:27:15 -04002268static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002269{
Chuck Leverab4684d2012-03-02 17:13:50 -05002270 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Jeff Laytonf4199922014-06-17 07:44:11 -04002272 /*
2273 * This is opaque to client, so no need to byte-swap. Use
2274 * __force to keep sparse happy
2275 */
Arnd Bergmann9104ae42019-11-04 16:45:30 +01002276 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002277 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002278 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
Jeff Layton294ac322014-07-30 08:27:15 -04002281static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2282{
Arnd Bergmann9cc76802019-10-31 15:53:13 +01002283 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
Jeff Layton294ac322014-07-30 08:27:15 -04002284 clp->cl_clientid.cl_id = nn->clientid_counter++;
2285 gen_confirm(clp, nn);
2286}
2287
Jeff Layton4770d722014-07-29 21:34:10 -04002288static struct nfs4_stid *
2289find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002290{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002291 struct nfs4_stid *ret;
2292
2293 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2294 if (!ret || !ret->sc_type)
2295 return NULL;
2296 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002297}
2298
Jeff Layton4770d722014-07-29 21:34:10 -04002299static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002300find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002301{
2302 struct nfs4_stid *s;
2303
Jeff Layton4770d722014-07-29 21:34:10 -04002304 spin_lock(&cl->cl_lock);
2305 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002306 if (s != NULL) {
2307 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002308 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002309 else
2310 s = NULL;
2311 }
Jeff Layton4770d722014-07-29 21:34:10 -04002312 spin_unlock(&cl->cl_lock);
2313 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002314}
2315
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002316static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2317{
2318 struct nfsdfs_client *nc;
2319 nc = get_nfsdfs_client(inode);
2320 if (!nc)
2321 return NULL;
2322 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2323}
2324
J. Bruce Fields169319f2019-06-19 12:39:46 -04002325static void seq_quote_mem(struct seq_file *m, char *data, int len)
2326{
2327 seq_printf(m, "\"");
2328 seq_escape_mem_ascii(m, data, len);
2329 seq_printf(m, "\"");
2330}
2331
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002332static int client_info_show(struct seq_file *m, void *v)
2333{
2334 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002335 struct nfs4_client *clp;
2336 u64 clid;
2337
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002338 clp = get_nfsdfs_clp(inode);
2339 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002340 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002341 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2342 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002343 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2344 seq_printf(m, "name: ");
2345 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2346 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields79123442019-06-05 12:42:05 -04002347 if (clp->cl_nii_domain.data) {
2348 seq_printf(m, "Implementation domain: ");
2349 seq_quote_mem(m, clp->cl_nii_domain.data,
2350 clp->cl_nii_domain.len);
2351 seq_printf(m, "\nImplementation name: ");
2352 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
Arnd Bergmanne29f4702019-10-31 14:52:43 +01002353 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
J. Bruce Fields79123442019-06-05 12:42:05 -04002354 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2355 }
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002356 drop_client(clp);
2357
2358 return 0;
2359}
2360
2361static int client_info_open(struct inode *inode, struct file *file)
2362{
2363 return single_open(file, client_info_show, inode);
2364}
2365
2366static const struct file_operations client_info_fops = {
2367 .open = client_info_open,
2368 .read = seq_read,
2369 .llseek = seq_lseek,
2370 .release = single_release,
2371};
2372
J. Bruce Fields78599c42019-04-22 15:26:23 -04002373static void *states_start(struct seq_file *s, loff_t *pos)
2374 __acquires(&clp->cl_lock)
2375{
2376 struct nfs4_client *clp = s->private;
2377 unsigned long id = *pos;
2378 void *ret;
2379
2380 spin_lock(&clp->cl_lock);
2381 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2382 *pos = id;
2383 return ret;
2384}
2385
2386static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2387{
2388 struct nfs4_client *clp = s->private;
2389 unsigned long id = *pos;
2390 void *ret;
2391
2392 id = *pos;
2393 id++;
2394 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2395 *pos = id;
2396 return ret;
2397}
2398
2399static void states_stop(struct seq_file *s, void *v)
2400 __releases(&clp->cl_lock)
2401{
2402 struct nfs4_client *clp = s->private;
2403
2404 spin_unlock(&clp->cl_lock);
2405}
2406
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002407static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002408{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002409 struct inode *inode = f->nf_inode;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002410
2411 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2412 MAJOR(inode->i_sb->s_dev),
2413 MINOR(inode->i_sb->s_dev),
2414 inode->i_ino);
2415}
2416
2417static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2418{
2419 seq_printf(s, "owner: ");
2420 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2421}
2422
2423static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2424{
2425 struct nfs4_ol_stateid *ols;
2426 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002427 struct nfsd_file *file;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002428 struct nfs4_stateowner *oo;
2429 unsigned int access, deny;
2430
2431 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2432 return 0; /* XXX: or SEQ_SKIP? */
2433 ols = openlockstateid(st);
2434 oo = ols->st_stateowner;
2435 nf = st->sc_file;
2436 file = find_any_file(nf);
2437
2438 seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid);
2439
2440 access = bmap_to_share_mode(ols->st_access_bmap);
2441 deny = bmap_to_share_mode(ols->st_deny_bmap);
2442
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002443 seq_printf(s, "access: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002444 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2445 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
J. Bruce Fieldsc4b77ed2019-10-03 12:33:08 -04002446 seq_printf(s, "deny: %s%s, ",
J. Bruce Fields78599c42019-04-22 15:26:23 -04002447 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2448 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2449
2450 nfs4_show_superblock(s, file);
2451 seq_printf(s, ", ");
2452 nfs4_show_owner(s, oo);
2453 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002454 nfsd_file_put(file);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002455
2456 return 0;
2457}
2458
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002459static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2460{
2461 struct nfs4_ol_stateid *ols;
2462 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002463 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002464 struct nfs4_stateowner *oo;
2465
2466 ols = openlockstateid(st);
2467 oo = ols->st_stateowner;
2468 nf = st->sc_file;
2469 file = find_any_file(nf);
2470
2471 seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid);
2472
2473 /*
2474 * Note: a lock stateid isn't really the same thing as a lock,
2475 * it's the locking state held by one owner on a file, and there
2476 * may be multiple (or no) lock ranges associated with it.
2477 * (Same for the matter is true of open stateids.)
2478 */
2479
2480 nfs4_show_superblock(s, file);
2481 /* XXX: open stateid? */
2482 seq_printf(s, ", ");
2483 nfs4_show_owner(s, oo);
2484 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002485 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002486
2487 return 0;
2488}
2489
2490static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2491{
2492 struct nfs4_delegation *ds;
2493 struct nfs4_file *nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002494 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002495
2496 ds = delegstateid(st);
2497 nf = st->sc_file;
2498 file = nf->fi_deleg_file;
2499
2500 seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid);
2501
2502 /* Kinda dead code as long as we only support read delegs: */
2503 seq_printf(s, "access: %s, ",
2504 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2505
2506 /* XXX: lease time, whether it's being recalled. */
2507
2508 nfs4_show_superblock(s, file);
2509 seq_printf(s, " }\n");
2510
2511 return 0;
2512}
2513
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002514static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2515{
2516 struct nfs4_layout_stateid *ls;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002517 struct nfsd_file *file;
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002518
2519 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2520 file = ls->ls_file;
2521
2522 seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid);
2523
2524 /* XXX: What else would be useful? */
2525
2526 nfs4_show_superblock(s, file);
2527 seq_printf(s, " }\n");
2528
2529 return 0;
2530}
2531
J. Bruce Fields78599c42019-04-22 15:26:23 -04002532static int states_show(struct seq_file *s, void *v)
2533{
2534 struct nfs4_stid *st = v;
2535
2536 switch (st->sc_type) {
2537 case NFS4_OPEN_STID:
2538 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002539 case NFS4_LOCK_STID:
2540 return nfs4_show_lock(s, st);
2541 case NFS4_DELEG_STID:
2542 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002543 case NFS4_LAYOUT_STID:
2544 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002545 default:
2546 return 0; /* XXX: or SEQ_SKIP? */
2547 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002548 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002549}
2550
2551static struct seq_operations states_seq_ops = {
2552 .start = states_start,
2553 .next = states_next,
2554 .stop = states_stop,
2555 .show = states_show
2556};
2557
2558static int client_states_open(struct inode *inode, struct file *file)
2559{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002560 struct seq_file *s;
2561 struct nfs4_client *clp;
2562 int ret;
2563
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002564 clp = get_nfsdfs_clp(inode);
2565 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002566 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002567
2568 ret = seq_open(file, &states_seq_ops);
2569 if (ret)
2570 return ret;
2571 s = file->private_data;
2572 s->private = clp;
2573 return 0;
2574}
2575
2576static int client_opens_release(struct inode *inode, struct file *file)
2577{
2578 struct seq_file *m = file->private_data;
2579 struct nfs4_client *clp = m->private;
2580
2581 /* XXX: alternatively, we could get/drop in seq start/stop */
2582 drop_client(clp);
2583 return 0;
2584}
2585
2586static const struct file_operations client_states_fops = {
2587 .open = client_states_open,
2588 .read = seq_read,
2589 .llseek = seq_lseek,
2590 .release = client_opens_release,
2591};
2592
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002593/*
2594 * Normally we refuse to destroy clients that are in use, but here the
2595 * administrator is telling us to just do it. We also want to wait
2596 * so the caller has a guarantee that the client's locks are gone by
2597 * the time the write returns:
2598 */
YueHaibing297e57a2019-07-08 15:29:33 +08002599static void force_expire_client(struct nfs4_client *clp)
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002600{
2601 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2602 bool already_expired;
2603
2604 spin_lock(&clp->cl_lock);
2605 clp->cl_time = 0;
2606 spin_unlock(&clp->cl_lock);
2607
2608 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2609 spin_lock(&nn->client_lock);
2610 already_expired = list_empty(&clp->cl_lru);
2611 if (!already_expired)
2612 unhash_client_locked(clp);
2613 spin_unlock(&nn->client_lock);
2614
2615 if (!already_expired)
2616 expire_client(clp);
2617 else
2618 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2619}
2620
2621static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2622 size_t size, loff_t *pos)
2623{
2624 char *data;
2625 struct nfs4_client *clp;
2626
2627 data = simple_transaction_get(file, buf, size);
2628 if (IS_ERR(data))
2629 return PTR_ERR(data);
2630 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2631 return -EINVAL;
2632 clp = get_nfsdfs_clp(file_inode(file));
2633 if (!clp)
2634 return -ENXIO;
2635 force_expire_client(clp);
2636 drop_client(clp);
2637 return 7;
2638}
2639
2640static const struct file_operations client_ctl_fops = {
2641 .write = client_ctl_write,
2642 .release = simple_transaction_release,
2643};
2644
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002645static const struct tree_descr client_files[] = {
2646 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002647 [1] = {"states", &client_states_fops, S_IRUSR},
Petr Vorel6cbfad52020-03-13 13:39:57 +01002648 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002649 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002650};
2651
Jeff Layton2216d442012-11-12 15:00:57 -05002652static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002653 struct svc_rqst *rqstp, nfs4_verifier *verf)
2654{
2655 struct nfs4_client *clp;
2656 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002657 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002658 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002659 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002660
2661 clp = alloc_client(name);
2662 if (clp == NULL)
2663 return NULL;
2664
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002665 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2666 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002667 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002668 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002669 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002670 gen_clid(clp, nn);
2671 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002672 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01002673 clp->cl_time = ktime_get_boottime_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002674 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002675 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002676 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002677 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002678 clp->net = net;
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002679 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002680 clp->cl_clientid.cl_id - nn->clientid_base,
2681 client_files);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002682 if (!clp->cl_nfsd_dentry) {
2683 free_client(clp);
2684 return NULL;
2685 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002686 return clp;
2687}
2688
NeilBrownfd39ca92005-06-23 22:04:03 -07002689static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002690add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2691{
2692 struct rb_node **new = &(root->rb_node), *parent = NULL;
2693 struct nfs4_client *clp;
2694
2695 while (*new) {
2696 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2697 parent = *new;
2698
2699 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2700 new = &((*new)->rb_left);
2701 else
2702 new = &((*new)->rb_right);
2703 }
2704
2705 rb_link_node(&new_clp->cl_namenode, parent, new);
2706 rb_insert_color(&new_clp->cl_namenode, root);
2707}
2708
2709static struct nfs4_client *
2710find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2711{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002712 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002713 struct rb_node *node = root->rb_node;
2714 struct nfs4_client *clp;
2715
2716 while (node) {
2717 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2718 cmp = compare_blob(&clp->cl_name, name);
2719 if (cmp > 0)
2720 node = node->rb_left;
2721 else if (cmp < 0)
2722 node = node->rb_right;
2723 else
2724 return clp;
2725 }
2726 return NULL;
2727}
2728
2729static void
2730add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
2732 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002733 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Trond Myklebust0a880a22014-07-30 08:27:10 -04002735 lockdep_assert_held(&nn->client_lock);
2736
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002737 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002738 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002740 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002741 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743
NeilBrownfd39ca92005-06-23 22:04:03 -07002744static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745move_to_confirmed(struct nfs4_client *clp)
2746{
2747 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002748 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Trond Myklebust0a880a22014-07-30 08:27:10 -04002750 lockdep_assert_held(&nn->client_lock);
2751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002753 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002754 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002755 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002756 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002757 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758}
2759
2760static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002761find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
2763 struct nfs4_client *clp;
2764 unsigned int idhashval = clientid_hashval(clid->cl_id);
2765
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002766 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002767 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002768 if ((bool)clp->cl_minorversion != sessions)
2769 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002770 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
2774 return NULL;
2775}
2776
2777static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002778find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2779{
2780 struct list_head *tbl = nn->conf_id_hashtbl;
2781
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);
2784}
2785
2786static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002787find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002789 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Trond Myklebust0a880a22014-07-30 08:27:10 -04002791 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002792 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
2794
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002795static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002796{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002797 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002798}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002799
NeilBrown28ce6052005-06-23 22:03:56 -07002800static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002801find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002802{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002803 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002804 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002805}
2806
2807static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002808find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002809{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002810 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002811 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002812}
2813
NeilBrownfd39ca92005-06-23 22:04:03 -07002814static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002815gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002817 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002818 struct sockaddr *sa = svc_addr(rqstp);
2819 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002820 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Jeff Layton7077ecb2009-08-14 12:57:58 -04002822 /* Currently, we only support tcp and tcp6 for the callback channel */
2823 if (se->se_callback_netid_len == 3 &&
2824 !memcmp(se->se_callback_netid_val, "tcp", 3))
2825 expected_family = AF_INET;
2826 else if (se->se_callback_netid_len == 4 &&
2827 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2828 expected_family = AF_INET6;
2829 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 goto out_err;
2831
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002832 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002833 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002834 (struct sockaddr *)&conn->cb_addr,
2835 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002836
J. Bruce Fields07263f12010-05-31 19:09:40 -04002837 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002839
J. Bruce Fields07263f12010-05-31 19:09:40 -04002840 if (conn->cb_addr.ss_family == AF_INET6)
2841 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002842
J. Bruce Fields07263f12010-05-31 19:09:40 -04002843 conn->cb_prog = se->se_callback_prog;
2844 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002845 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return;
2847out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002848 conn->cb_addr.ss_family = AF_UNSPEC;
2849 conn->cb_addrlen = 0;
Rasmus Villemoes4ab495b2017-02-24 01:15:55 +01002850 dprintk("NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 "will not receive delegations\n",
2852 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return;
2855}
2856
Andy Adamson074fe892009-04-03 08:28:15 +03002857/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002858 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002859 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002860static void
Andy Adamson074fe892009-04-03 08:28:15 +03002861nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2862{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002863 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002864 struct nfsd4_slot *slot = resp->cstate.slot;
2865 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002866
Andy Adamson557ce262009-08-28 08:45:04 -04002867 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002868
J. Bruce Fields085def32017-10-18 16:17:18 -04002869 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002870 slot->sl_opcnt = resp->opcnt;
2871 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002872 free_svc_cred(&slot->sl_cred);
2873 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002874
J. Bruce Fields085def32017-10-18 16:17:18 -04002875 if (!nfsd4_cache_this(resp)) {
2876 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002877 return;
2878 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002879 slot->sl_flags |= NFSD4_SLOT_CACHED;
2880
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002881 base = resp->cstate.data_offset;
2882 slot->sl_datalen = buf->len - base;
2883 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002884 WARN(1, "%s: sessions DRC could not cache compound\n",
2885 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002886 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002887}
2888
2889/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002890 * Encode the replay sequence operation from the slot values.
2891 * If cachethis is FALSE encode the uncached rep error on the next
2892 * operation which sets resp->p and increments resp->opcnt for
2893 * nfs4svc_encode_compoundres.
2894 *
Andy Adamson074fe892009-04-03 08:28:15 +03002895 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002896static __be32
2897nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2898 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002899{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002900 struct nfsd4_op *op;
2901 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002902
Andy Adamsonabfabf82009-07-23 19:02:18 -04002903 /* Encode the replayed sequence operation */
2904 op = &args->ops[resp->opcnt - 1];
2905 nfsd4_encode_operation(resp, op);
2906
J. Bruce Fields085def32017-10-18 16:17:18 -04002907 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2908 return op->status;
2909 if (args->opcnt == 1) {
2910 /*
2911 * The original operation wasn't a solo sequence--we
2912 * always cache those--so this retry must not match the
2913 * original:
2914 */
2915 op->status = nfserr_seq_false_retry;
2916 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002917 op = &args->ops[resp->opcnt++];
2918 op->status = nfserr_retry_uncached_rep;
2919 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002920 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002921 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002922}
2923
2924/*
Andy Adamson557ce262009-08-28 08:45:04 -04002925 * The sequence operation is not cached because we can use the slot and
2926 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002927 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002928static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002929nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2930 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002931{
Andy Adamson557ce262009-08-28 08:45:04 -04002932 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002933 struct xdr_stream *xdr = &resp->xdr;
2934 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002935 __be32 status;
2936
Andy Adamson557ce262009-08-28 08:45:04 -04002937 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002938
Andy Adamsonabfabf82009-07-23 19:02:18 -04002939 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002940 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002941 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002942
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002943 p = xdr_reserve_space(xdr, slot->sl_datalen);
2944 if (!p) {
2945 WARN_ON_ONCE(1);
2946 return nfserr_serverfault;
2947 }
2948 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2949 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002950
Andy Adamson557ce262009-08-28 08:45:04 -04002951 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002952 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002953}
2954
Andy Adamson0733d212009-04-03 08:28:01 +03002955/*
2956 * Set the exchange_id flags returned by the server.
2957 */
2958static void
2959nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2960{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002961#ifdef CONFIG_NFSD_PNFS
2962 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2963#else
Andy Adamson0733d212009-04-03 08:28:01 +03002964 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002965#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002966
2967 /* Referrals are supported, Migration is not. */
2968 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2969
2970 /* set the wire flags to return to client. */
2971 clid->flags = new->cl_exchange_flags;
2972}
2973
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002974static bool client_has_openowners(struct nfs4_client *clp)
2975{
2976 struct nfs4_openowner *oo;
2977
2978 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2979 if (!list_empty(&oo->oo_owner.so_stateids))
2980 return true;
2981 }
2982 return false;
2983}
2984
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002985static bool client_has_state(struct nfs4_client *clp)
2986{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002987 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002988#ifdef CONFIG_NFSD_PNFS
2989 || !list_empty(&clp->cl_lo_states)
2990#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002991 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002992 || !list_empty(&clp->cl_sessions)
2993 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002994}
2995
J. Bruce Fields79123442019-06-05 12:42:05 -04002996static __be32 copy_impl_id(struct nfs4_client *clp,
2997 struct nfsd4_exchange_id *exid)
2998{
2999 if (!exid->nii_domain.data)
3000 return 0;
3001 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3002 if (!clp->cl_nii_domain.data)
3003 return nfserr_jukebox;
3004 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3005 if (!clp->cl_nii_name.data)
3006 return nfserr_jukebox;
Arnd Bergmanne29f4702019-10-31 14:52:43 +01003007 clp->cl_nii_time = exid->nii_time;
J. Bruce Fields79123442019-06-05 12:42:05 -04003008 return 0;
3009}
3010
Al Virob37ad282006-10-19 23:28:59 -07003011__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003012nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3013 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003014{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003015 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003016 struct nfs4_client *conf, *new;
3017 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003018 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04003019 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03003020 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04003021 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003022 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003023 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03003024
Jeff Layton363168b2009-08-14 12:57:56 -04003025 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03003026 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04003027 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03003028 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04003029 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03003030
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003031 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03003032 return nfserr_inval;
3033
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003034 new = create_client(exid->clname, rqstp, &verf);
3035 if (new == NULL)
3036 return nfserr_jukebox;
J. Bruce Fields79123442019-06-05 12:42:05 -04003037 status = copy_impl_id(new, exid);
3038 if (status)
3039 goto out_nolock;
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003040
Andy Adamson0733d212009-04-03 08:28:01 +03003041 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003042 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04003043 exid->spo_must_enforce[0] = 0;
3044 exid->spo_must_enforce[1] = (
3045 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3046 1 << (OP_EXCHANGE_ID - 32) |
3047 1 << (OP_CREATE_SESSION - 32) |
3048 1 << (OP_DESTROY_SESSION - 32) |
3049 1 << (OP_DESTROY_CLIENTID - 32));
3050
3051 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3052 1 << (OP_OPEN_DOWNGRADE) |
3053 1 << (OP_LOCKU) |
3054 1 << (OP_DELEGRETURN));
3055
3056 exid->spo_must_allow[1] &= (
3057 1 << (OP_TEST_STATEID - 32) |
3058 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003059 if (!svc_rqst_integrity_protected(rqstp)) {
3060 status = nfserr_inval;
3061 goto out_nolock;
3062 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003063 /*
3064 * Sometimes userspace doesn't give us a principal.
3065 * Which is a bug, really. Anyway, we can't enforce
3066 * MACH_CRED in that case, better to give up now:
3067 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05003068 if (!new->cl_cred.cr_principal &&
3069 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05003070 status = nfserr_serverfault;
3071 goto out_nolock;
3072 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003073 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03003074 case SP4_NONE:
3075 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003076 default: /* checked by xdr code */
3077 WARN_ON_ONCE(1);
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06003078 /* fall through */
Andy Adamson0733d212009-04-03 08:28:01 +03003079 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08003080 status = nfserr_encr_alg_unsupp;
3081 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03003082 }
3083
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003084 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003085 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003086 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03003087 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003088 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3089 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3090
J. Bruce Fields136e6582012-05-12 20:37:23 -04003091 if (update) {
3092 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003093 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03003094 goto out;
3095 }
Andrew Elblededeb132016-06-15 12:52:08 -04003096 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003097 status = nfserr_wrong_cred;
3098 goto out;
3099 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003100 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03003101 status = nfserr_perm;
3102 goto out;
3103 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003104 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03003105 status = nfserr_not_same;
3106 goto out;
3107 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003108 /* case 6 */
3109 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003110 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003111 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003112 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003113 if (client_has_state(conf)) {
3114 status = nfserr_clid_inuse;
3115 goto out;
3116 }
Andy Adamson0733d212009-04-03 08:28:01 +03003117 goto out_new;
3118 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003119 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003120 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003121 goto out_copy;
3122 }
3123 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003124 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003125 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003126 }
3127
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003128 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003129 status = nfserr_noent;
3130 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003131 }
3132
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003133 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003134 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003135 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003136
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003137 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03003138out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003139 if (conf) {
3140 status = mark_client_expired_locked(conf);
3141 if (status)
3142 goto out;
3143 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003144 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003145 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3146 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003147
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003148 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003149 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003150out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003151 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3152 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003153
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003154 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3155 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003156
3157 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003158 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003159 status = nfs_ok;
3160
3161out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003162 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003163out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003164 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003165 expire_client(new);
3166 if (unconf)
3167 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003168 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003169}
3170
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003171static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003172check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003173{
Andy Adamson88e588d2009-07-23 19:02:15 -04003174 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3175 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003176
3177 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003178 if (slot_inuse) {
3179 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003180 return nfserr_jukebox;
3181 else
3182 return nfserr_seq_misordered;
3183 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003184 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003185 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003186 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003187 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003188 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003189 return nfserr_seq_misordered;
3190}
3191
Andy Adamson49557cc2009-07-23 19:02:16 -04003192/*
3193 * Cache the create session result into the create session single DRC
3194 * slot cache by saving the xdr structure. sl_seqid has been set.
3195 * Do this for solo or embedded create session operations.
3196 */
3197static void
3198nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003199 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003200{
3201 slot->sl_status = nfserr;
3202 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3203}
3204
3205static __be32
3206nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3207 struct nfsd4_clid_slot *slot)
3208{
3209 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3210 return slot->sl_status;
3211}
3212
Mi Jinlong1b74c252011-07-14 14:50:17 +08003213#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3214 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3215 1 + /* MIN tag is length with zero, only length */ \
3216 3 + /* version, opcount, opcode */ \
3217 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3218 /* seqid, slotID, slotID, cache */ \
3219 4 ) * sizeof(__be32))
3220
3221#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3222 2 + /* verifier: AUTH_NULL, length 0 */\
3223 1 + /* status */ \
3224 1 + /* MIN tag is length with zero, only length */ \
3225 3 + /* opcount, opcode, opstatus*/ \
3226 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3227 /* seqid, slotID, slotID, slotID, status */ \
3228 5 ) * sizeof(__be32))
3229
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003230static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003231{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003232 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3233
J. Bruce Fields373cd402013-04-08 15:42:12 -04003234 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3235 return nfserr_toosmall;
3236 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3237 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003238 ca->headerpadsz = 0;
3239 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3240 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3241 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3242 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3243 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3244 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3245 /*
3246 * Note decreasing slot size below client's request may make it
3247 * difficult for client to function correctly, whereas
3248 * decreasing the number of slots will (just?) affect
3249 * performance. When short on memory we therefore prefer to
3250 * decrease number of slots instead of their size. Clients that
3251 * request larger slots than they need will get poor results:
NeilBrown7f49fd5d2019-09-20 16:33:16 +10003252 * Note that we always allow at least one slot, because our
3253 * accounting is soft and provides no guarantees either way.
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003254 */
NeilBrown2030ca52019-09-20 16:36:45 +10003255 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003256
J. Bruce Fields373cd402013-04-08 15:42:12 -04003257 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003258}
3259
Chuck Lever45006322016-03-01 13:06:02 -05003260/*
3261 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3262 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3263 */
3264#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3265 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3266
3267#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3268 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3269
Kinglong Mee8a891632013-12-23 18:11:02 +08003270#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003271 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003272#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003273 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3274 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003275
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003276static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3277{
3278 ca->headerpadsz = 0;
3279
Kinglong Mee8a891632013-12-23 18:11:02 +08003280 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003281 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003282 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003283 return nfserr_toosmall;
3284 ca->maxresp_cached = 0;
3285 if (ca->maxops < 2)
3286 return nfserr_toosmall;
3287
3288 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003289}
3290
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003291static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3292{
3293 switch (cbs->flavor) {
3294 case RPC_AUTH_NULL:
3295 case RPC_AUTH_UNIX:
3296 return nfs_ok;
3297 default:
3298 /*
3299 * GSS case: the spec doesn't allow us to return this
3300 * error. But it also doesn't allow us not to support
3301 * GSS.
3302 * I'd rather this fail hard than return some error the
3303 * client might think it can already handle:
3304 */
3305 return nfserr_encr_alg_unsupp;
3306 }
3307}
3308
Andy Adamson069b6ad2009-04-03 08:27:58 +03003309__be32
3310nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003311 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003312{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003313 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003314 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003315 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003316 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003317 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003318 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003319 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003320 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003321 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003322
Mi Jinlonga62573d2011-03-23 17:57:07 +08003323 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3324 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003325 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3326 if (status)
3327 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003328 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003329 if (status)
3330 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003331 status = check_backchannel_attrs(&cr_ses->back_channel);
3332 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003333 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003334 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003335 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003336 if (!new)
3337 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003338 conn = alloc_conn_from_crses(rqstp, cr_ses);
3339 if (!conn)
3340 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003341
Jeff Laytond20c11d2014-07-30 08:27:07 -04003342 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003343 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003344 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003345 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003346
3347 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003348 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003349 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003350 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003351 cs_slot = &conf->cl_cs_slot;
3352 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003353 if (status) {
3354 if (status == nfserr_replay_cache)
3355 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003356 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003357 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003358 } else if (unconf) {
3359 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003360 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003361 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003362 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003363 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003364 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003365 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003366 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003367 cs_slot = &unconf->cl_cs_slot;
3368 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003369 if (status) {
3370 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003371 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003372 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003373 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003374 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003375 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003376 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003377 if (status) {
3378 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003379 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003380 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003381 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003382 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003383 conf = unconf;
3384 } else {
3385 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003386 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003387 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003388 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003389 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003390 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003391 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003392 cr_ses->flags &= ~SESSION4_RDMA;
3393
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003394 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003395 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003396
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003397 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003398 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003399 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003400 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003401
Jeff Laytond20c11d2014-07-30 08:27:07 -04003402 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003403 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003404 spin_unlock(&nn->client_lock);
3405 /* init connection and backchannel */
3406 nfsd4_init_conn(rqstp, conn, new);
3407 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003408 if (old)
3409 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003410 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003411out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003412 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003413 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003414 if (old)
3415 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003416out_free_session:
3417 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003418out_release_drc_mem:
3419 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003420 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003421}
3422
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003423static __be32 nfsd4_map_bcts_dir(u32 *dir)
3424{
3425 switch (*dir) {
3426 case NFS4_CDFC4_FORE:
3427 case NFS4_CDFC4_BACK:
3428 return nfs_ok;
3429 case NFS4_CDFC4_FORE_OR_BOTH:
3430 case NFS4_CDFC4_BACK_OR_BOTH:
3431 *dir = NFS4_CDFC4_BOTH;
3432 return nfs_ok;
zhengbinfc5fc5d2019-12-19 17:29:20 +08003433 }
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003434 return nfserr_inval;
3435}
3436
Christoph Hellwigeb698532017-05-08 20:58:35 +02003437__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3438 struct nfsd4_compound_state *cstate,
3439 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003440{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003441 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003442 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003443 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003444 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003445
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003446 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3447 if (status)
3448 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003449 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003450 session->se_cb_prog = bc->bc_cb_program;
3451 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003452 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003453
3454 nfsd4_probe_callback(session->se_client);
3455
3456 return nfs_ok;
3457}
3458
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003459__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3460 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003461 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003462{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003463 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003464 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003465 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003466 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003467 struct net *net = SVC_NET(rqstp);
3468 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003469
3470 if (!nfsd4_last_compound_op(rqstp))
3471 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003472 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003473 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003474 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003475 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003476 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003477 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003478 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003479 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003480 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003481 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003482 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003483 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003484 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003485 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003486 goto out;
3487 nfsd4_init_conn(rqstp, conn, session);
3488 status = nfs_ok;
3489out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003490 nfsd4_put_session(session);
3491out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003492 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003493}
3494
J. Bruce Fields665d5072018-04-11 16:53:36 -04003495static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003496{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003497 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003498 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003499 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003500}
3501
Andy Adamson069b6ad2009-04-03 08:27:58 +03003502__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003503nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3504 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003505{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003506 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003507 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003508 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003509 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003510 struct net *net = SVC_NET(r);
3511 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003512
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003513 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003514 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003515 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003516 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003517 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003518 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003519 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003520 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003521 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003522 if (!ses)
3523 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003524 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003525 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003526 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003527 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003528 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003529 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003530 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003531 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003532
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003533 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003534
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003535 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003536 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003537out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003538 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003539out_client_lock:
3540 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003541out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003542 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003543}
3544
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003545static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003546{
3547 struct nfsd4_conn *c;
3548
3549 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003550 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04003551 return c;
3552 }
3553 }
3554 return NULL;
3555}
3556
J. Bruce Fields57266a62013-04-13 14:27:29 -04003557static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003558{
3559 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003560 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003561 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003562 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003563
3564 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003565 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003566 if (c)
3567 goto out_free;
3568 status = nfserr_conn_not_bound_to_session;
3569 if (clp->cl_mach_cred)
3570 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003571 __nfsd4_hash_conn(new, ses);
3572 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003573 ret = nfsd4_register_conn(new);
3574 if (ret)
3575 /* oops; xprt is already down: */
3576 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003577 return nfs_ok;
3578out_free:
3579 spin_unlock(&clp->cl_lock);
3580 free_conn(new);
3581 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003582}
3583
Mi Jinlong868b89c2011-04-27 09:09:58 +08003584static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3585{
3586 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3587
3588 return args->opcnt > session->se_fchannel.maxops;
3589}
3590
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003591static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3592 struct nfsd4_session *session)
3593{
3594 struct xdr_buf *xb = &rqstp->rq_arg;
3595
3596 return xb->len > session->se_fchannel.maxreq_sz;
3597}
3598
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003599static bool replay_matches_cache(struct svc_rqst *rqstp,
3600 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3601{
3602 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3603
3604 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3605 (bool)seq->cachethis)
3606 return false;
3607 /*
Scott Mayhew6e73e922019-10-09 15:11:37 -04003608 * If there's an error then the reply can have fewer ops than
3609 * the call.
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003610 */
Scott Mayhew6e73e922019-10-09 15:11:37 -04003611 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3612 return false;
3613 /*
3614 * But if we cached a reply with *more* ops than the call you're
3615 * sending us now, then this new call is clearly not really a
3616 * replay of the old one:
3617 */
3618 if (slot->sl_opcnt > argp->opcnt)
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003619 return false;
3620 /* This is the only check explicitly called by spec: */
3621 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3622 return false;
3623 /*
3624 * There may be more comparisons we could actually do, but the
3625 * spec doesn't require us to catch every case where the calls
3626 * don't match (that would require caching the call as well as
3627 * the reply), so we don't bother.
3628 */
3629 return true;
3630}
3631
Andy Adamson069b6ad2009-04-03 08:27:58 +03003632__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003633nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3634 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003635{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003636 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003637 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003638 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003639 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003640 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003641 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003642 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003643 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003644 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003645 struct net *net = SVC_NET(rqstp);
3646 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003647
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003648 if (resp->opcnt != 1)
3649 return nfserr_sequence_pos;
3650
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003651 /*
3652 * Will be either used or freed by nfsd4_sequence_check_conn
3653 * below.
3654 */
3655 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3656 if (!conn)
3657 return nfserr_jukebox;
3658
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003659 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003660 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003661 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003662 goto out_no_session;
3663 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003664
Mi Jinlong868b89c2011-04-27 09:09:58 +08003665 status = nfserr_too_many_ops;
3666 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003667 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003668
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003669 status = nfserr_req_too_big;
3670 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003671 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003672
Benny Halevyb85d4c02009-04-03 08:28:08 +03003673 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003674 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003675 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003676
Andy Adamson557ce262009-08-28 08:45:04 -04003677 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003678 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3679
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003680 /* We do not negotiate the number of slots yet, so set the
3681 * maxslots to the session maxreqs which is used to encode
3682 * sr_highest_slotid and the sr_target_slot id to maxslots */
3683 seq->maxslots = session->se_fchannel.maxreqs;
3684
J. Bruce Fields73e79482012-02-13 16:39:00 -05003685 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3686 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003687 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003688 status = nfserr_seq_misordered;
3689 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003690 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003691 status = nfserr_seq_false_retry;
3692 if (!replay_matches_cache(rqstp, seq, slot))
3693 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003694 cstate->slot = slot;
3695 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003696 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003697 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003698 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003699 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003700 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003701 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003702 }
3703 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003704 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003705
J. Bruce Fields57266a62013-04-13 14:27:29 -04003706 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003707 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003708 if (status)
3709 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003710
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003711 buflen = (seq->cachethis) ?
3712 session->se_fchannel.maxresp_cached :
3713 session->se_fchannel.maxresp_sz;
3714 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3715 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003716 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003717 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003718 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003719
3720 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003721 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003722 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003723 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003724 if (seq->cachethis)
3725 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003726 else
3727 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003728
3729 cstate->slot = slot;
3730 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003731 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003732
Benny Halevyb85d4c02009-04-03 08:28:08 +03003733out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003734 switch (clp->cl_cb_state) {
3735 case NFSD4_CB_DOWN:
3736 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3737 break;
3738 case NFSD4_CB_FAULT:
3739 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3740 break;
3741 default:
3742 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003743 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003744 if (!list_empty(&clp->cl_revoked))
3745 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003746out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003747 if (conn)
3748 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003749 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003750 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003751out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003752 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003753 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003754}
3755
Trond Myklebustb6076642014-06-30 11:48:35 -04003756void
3757nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3758{
3759 struct nfsd4_compound_state *cs = &resp->cstate;
3760
3761 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003762 if (cs->status != nfserr_replay_cache) {
3763 nfsd4_store_cache_entry(resp);
3764 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3765 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003766 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003767 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003768 } else if (cs->clp)
3769 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003770}
3771
Mi Jinlong345c2842011-10-20 17:51:39 +08003772__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003773nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3774 struct nfsd4_compound_state *cstate,
3775 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003776{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003777 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003778 struct nfs4_client *conf, *unconf;
3779 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003780 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003781 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003782
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003783 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003784 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003785 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003786 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003787
3788 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003789 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003790 status = nfserr_clientid_busy;
3791 goto out;
3792 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003793 status = mark_client_expired_locked(conf);
3794 if (status)
3795 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003796 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003797 } else if (unconf)
3798 clp = unconf;
3799 else {
3800 status = nfserr_stale_clientid;
3801 goto out;
3802 }
Andrew Elblededeb132016-06-15 12:52:08 -04003803 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003804 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003805 status = nfserr_wrong_cred;
3806 goto out;
3807 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003808 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003809out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003810 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003811 if (clp)
3812 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003813 return status;
3814}
3815
Andy Adamson069b6ad2009-04-03 08:27:58 +03003816__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003817nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3818 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003819{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003820 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003821 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003822
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003823 if (rc->rca_one_fs) {
3824 if (!cstate->current_fh.fh_dentry)
3825 return nfserr_nofilehandle;
3826 /*
3827 * We don't take advantage of the rca_one_fs case.
3828 * That's OK, it's optional, we can safely ignore it.
3829 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003830 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003831 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003832
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003833 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003834 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3835 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003836 goto out;
3837
3838 status = nfserr_stale_clientid;
3839 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003840 /*
3841 * The following error isn't really legal.
3842 * But we only get here if the client just explicitly
3843 * destroyed the client. Surely it no longer cares what
3844 * error it gets back on an operation for the dead
3845 * client.
3846 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003847 goto out;
3848
3849 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003850 nfsd4_client_record_create(cstate->session->se_client);
Scott Mayhew362063a2019-03-26 18:06:28 -04003851 inc_reclaim_complete(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003852out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003853 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003854}
3855
3856__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003857nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003858 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003860 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003861 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003863 struct nfs4_client *conf, *new;
3864 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003865 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003866 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3867
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003868 new = create_client(clname, rqstp, &clverifier);
3869 if (new == NULL)
3870 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003871 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003872 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003873 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003874 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003875 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003877 if (clp_used_exchangeid(conf))
3878 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003879 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003880 char addr_str[INET6_ADDRSTRLEN];
3881 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3882 sizeof(addr_str));
3883 dprintk("NFSD: setclientid: string in use by client "
3884 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 goto out;
3886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003888 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003889 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003890 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003891 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003892 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003894 gen_confirm(new, nn);
3895 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04003896 ;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003897 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003898 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003899 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3901 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3902 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003903 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 status = nfs_ok;
3905out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003906 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003907 if (new)
3908 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003909 if (unconf)
3910 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 return status;
3912}
3913
3914
Al Virob37ad282006-10-19 23:28:59 -07003915__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003916nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003917 struct nfsd4_compound_state *cstate,
3918 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003920 struct nfsd4_setclientid_confirm *setclientid_confirm =
3921 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003922 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003923 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3925 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003926 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003927 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003929 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003931
Jeff Laytond20c11d2014-07-30 08:27:07 -04003932 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003933 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003934 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003935 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003936 * We try hard to give out unique clientid's, so if we get an
3937 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003938 * the client may be buggy; this should never happen.
3939 *
3940 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003941 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003942 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003943 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3944 goto out;
3945 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3946 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003947 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003948 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003949 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3950 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003952 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003953 status = nfserr_stale_clientid;
3954 goto out;
3955 }
3956 status = nfs_ok;
3957 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003958 old = unconf;
3959 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003960 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003961 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003962 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3963 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003964 status = nfserr_clid_inuse;
3965 if (client_has_state(old)
3966 && !same_creds(&unconf->cl_cred,
3967 &old->cl_cred))
3968 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003969 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003970 if (status) {
3971 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003972 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003973 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003974 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003975 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003976 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003977 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003978 get_client_locked(conf);
3979 spin_unlock(&nn->client_lock);
3980 nfsd4_probe_callback(conf);
3981 spin_lock(&nn->client_lock);
3982 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003984 spin_unlock(&nn->client_lock);
3985 if (old)
3986 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 return status;
3988}
3989
J. Bruce Fields32513b42011-10-13 16:00:16 -04003990static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003992 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3993}
3994
3995/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003996static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3997 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003998{
Trond Myklebust950e0112014-06-30 11:48:31 -04003999 lockdep_assert_held(&state_lock);
4000
Elena Reshetova818a34e2017-10-20 12:53:30 +03004001 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004002 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04004003 INIT_LIST_HEAD(&fp->fi_stateids);
4004 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02004005 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04004006 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04004007 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004008 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004009 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004010 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4011 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004012#ifdef CONFIG_NFSD_PNFS
4013 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02004014 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004015#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04004016 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04004019void
NeilBrowne60d4392005-06-23 22:03:01 -07004020nfsd4_free_slabs(void)
4021{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004022 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004023 kmem_cache_destroy(openowner_slab);
4024 kmem_cache_destroy(lockowner_slab);
4025 kmem_cache_destroy(file_slab);
4026 kmem_cache_destroy(stateid_slab);
4027 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004028 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07004029}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
Bryan Schumaker72083392011-11-01 15:24:59 -04004031int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032nfsd4_init_slabs(void)
4033{
Jeff Layton9258a2d2018-03-16 09:47:22 -04004034 client_slab = kmem_cache_create("nfsd4_clients",
4035 sizeof(struct nfs4_client), 0, 0, NULL);
4036 if (client_slab == NULL)
4037 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004038 openowner_slab = kmem_cache_create("nfsd4_openowners",
4039 sizeof(struct nfs4_openowner), 0, 0, NULL);
4040 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04004041 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004042 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08004043 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004044 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004045 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07004046 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09004047 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07004048 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004049 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07004050 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004051 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07004052 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004053 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07004054 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09004055 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07004056 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07004057 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004058 odstate_slab = kmem_cache_create("nfsd4_odstate",
4059 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4060 if (odstate_slab == NULL)
4061 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07004063
Sachin Bhamare8287f002015-04-27 14:50:14 +02004064out_free_deleg_slab:
4065 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004066out_free_stateid_slab:
4067 kmem_cache_destroy(stateid_slab);
4068out_free_file_slab:
4069 kmem_cache_destroy(file_slab);
4070out_free_lockowner_slab:
4071 kmem_cache_destroy(lockowner_slab);
4072out_free_openowner_slab:
4073 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04004074out_free_client_slab:
4075 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07004076out:
NeilBrowne60d4392005-06-23 22:03:01 -07004077 dprintk("nfsd4: out of memory while initializing nfsv4\n");
4078 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079}
4080
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004081static void init_nfs4_replay(struct nfs4_replay *rp)
4082{
4083 rp->rp_status = nfserr_serverfault;
4084 rp->rp_buflen = 0;
4085 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04004086 mutex_init(&rp->rp_mutex);
4087}
4088
4089static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4090 struct nfs4_stateowner *so)
4091{
4092 if (!nfsd4_has_session(cstate)) {
4093 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004094 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004095 }
4096}
4097
4098void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4099{
4100 struct nfs4_stateowner *so = cstate->replay_owner;
4101
4102 if (so != NULL) {
4103 cstate->replay_owner = NULL;
4104 mutex_unlock(&so->so_replay.rp_mutex);
4105 nfs4_put_stateowner(so);
4106 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004107}
4108
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004109static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
4111 struct nfs4_stateowner *sop;
4112
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004113 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004114 if (!sop)
4115 return NULL;
4116
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04004117 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004118 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004119 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004120 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004122
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004123 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004124 sop->so_client = clp;
4125 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04004126 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004127 return sop;
4128}
4129
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004130static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004131{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004132 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004133
Trond Myklebustd4f04892014-07-29 21:34:36 -04004134 list_add(&oo->oo_owner.so_strhash,
4135 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004136 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137}
4138
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004139static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4140{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004141 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004142}
4143
Jeff Layton6b180f02014-07-29 21:34:26 -04004144static void nfs4_free_openowner(struct nfs4_stateowner *so)
4145{
4146 struct nfs4_openowner *oo = openowner(so);
4147
4148 kmem_cache_free(openowner_slab, oo);
4149}
4150
4151static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004152 .so_unhash = nfs4_unhash_openowner,
4153 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004154};
4155
Andrew Elble7fc05642015-11-05 20:42:43 -05004156static struct nfs4_ol_stateid *
4157nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4158{
4159 struct nfs4_ol_stateid *local, *ret = NULL;
4160 struct nfs4_openowner *oo = open->op_openowner;
4161
4162 lockdep_assert_held(&fp->fi_lock);
4163
4164 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4165 /* ignore lock owners */
4166 if (local->st_stateowner->so_is_open_owner == 0)
4167 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004168 if (local->st_stateowner != &oo->oo_owner)
4169 continue;
4170 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004171 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004172 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05004173 break;
4174 }
4175 }
4176 return ret;
4177}
4178
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004179static __be32
4180nfsd4_verify_open_stid(struct nfs4_stid *s)
4181{
4182 __be32 ret = nfs_ok;
4183
4184 switch (s->sc_type) {
4185 default:
4186 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004187 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004188 case NFS4_CLOSED_STID:
4189 case NFS4_CLOSED_DELEG_STID:
4190 ret = nfserr_bad_stateid;
4191 break;
4192 case NFS4_REVOKED_DELEG_STID:
4193 ret = nfserr_deleg_revoked;
4194 }
4195 return ret;
4196}
4197
4198/* Lock the stateid st_mutex, and deal with races with CLOSE */
4199static __be32
4200nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4201{
4202 __be32 ret;
4203
Andrew Elble4f34bd02017-11-08 17:29:51 -05004204 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004205 ret = nfsd4_verify_open_stid(&stp->st_stid);
4206 if (ret != nfs_ok)
4207 mutex_unlock(&stp->st_mutex);
4208 return ret;
4209}
4210
4211static struct nfs4_ol_stateid *
4212nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4213{
4214 struct nfs4_ol_stateid *stp;
4215 for (;;) {
4216 spin_lock(&fp->fi_lock);
4217 stp = nfsd4_find_existing_open(fp, open);
4218 spin_unlock(&fp->fi_lock);
4219 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4220 break;
4221 nfs4_put_stid(&stp->st_stid);
4222 }
4223 return stp;
4224}
4225
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004226static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004227alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004228 struct nfsd4_compound_state *cstate)
4229{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004230 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004231 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004233 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4234 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004236 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004237 oo->oo_owner.so_is_open_owner = 1;
4238 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004239 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004240 if (nfsd4_has_session(cstate))
4241 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004242 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004243 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004244 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004245 spin_lock(&clp->cl_lock);
4246 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004247 if (ret == NULL) {
4248 hash_openowner(oo, clp, strhashval);
4249 ret = oo;
4250 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004251 nfs4_free_stateowner(&oo->oo_owner);
4252
Trond Myklebustd4f04892014-07-29 21:34:36 -04004253 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004254 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
4256
Andrew Elble7fc05642015-11-05 20:42:43 -05004257static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004258init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004259{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Andrew Elble7fc05642015-11-05 20:42:43 -05004261 struct nfs4_openowner *oo = open->op_openowner;
4262 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004263 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004264
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004265 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004266 /* We are moving these outside of the spinlocks to avoid the warnings */
4267 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004268 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004269
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004270retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004271 spin_lock(&oo->oo_owner.so_client->cl_lock);
4272 spin_lock(&fp->fi_lock);
4273
4274 retstp = nfsd4_find_existing_open(fp, open);
4275 if (retstp)
4276 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004277
4278 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004279 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004280 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004281 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004282 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004283 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004284 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 stp->st_access_bmap = 0;
4286 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004287 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004288 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004289 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004290
4291out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004292 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004293 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004294 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004295 /* Handle races with CLOSE */
4296 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4297 nfs4_put_stid(&retstp->st_stid);
4298 goto retry;
4299 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004300 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004301 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004302 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004303 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004304 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305}
4306
Jeff Laytond3134b12014-07-29 21:34:32 -04004307/*
4308 * In the 4.0 case we need to keep the owners around a little while to handle
4309 * CLOSE replay. We still do need to release any file access that is held by
4310 * them before returning however.
4311 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004313move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314{
Jeff Layton217526e2014-07-30 08:27:11 -04004315 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004316 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4317 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4318 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004319
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004320 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
Jeff Laytonb401be222014-07-29 21:34:33 -04004322 /*
4323 * We know that we hold one reference via nfsd4_close, and another
4324 * "persistent" reference for the client. If the refcount is higher
4325 * than 2, then there are still calls in progress that are using this
4326 * stateid. We can't put the sc_file reference until they are finished.
4327 * Wait for the refcount to drop to 2. Since it has been unhashed,
4328 * there should be no danger of the refcount going back up again at
4329 * this point.
4330 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004331 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004332
Jeff Laytond3134b12014-07-29 21:34:32 -04004333 release_all_access(s);
4334 if (s->st_stid.sc_file) {
4335 put_nfs4_file(s->st_stid.sc_file);
4336 s->st_stid.sc_file = NULL;
4337 }
Jeff Layton217526e2014-07-30 08:27:11 -04004338
4339 spin_lock(&nn->client_lock);
4340 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004341 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004342 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004343 oo->oo_time = ktime_get_boottime_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004344 spin_unlock(&nn->client_lock);
4345 if (last)
4346 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347}
4348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349/* search file_hashtbl[] for file */
4350static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04004351find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 struct nfs4_file *fp;
4354
Madhuparna Bhowmik36a80492020-02-13 20:33:31 +05304355 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4356 lockdep_is_held(&state_lock)) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02004357 if (fh_match(&fp->fi_fhandle, fh)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004358 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004359 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 }
4362 return NULL;
4363}
4364
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02004365struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04004366find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004367{
4368 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004369 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004370
Jeff Layton5b095e92014-10-23 08:01:02 -04004371 rcu_read_lock();
4372 fp = find_file_locked(fh, hashval);
4373 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004374 return fp;
4375}
4376
4377static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004378find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004379{
4380 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004381 unsigned int hashval = file_hashval(fh);
4382
4383 rcu_read_lock();
4384 fp = find_file_locked(fh, hashval);
4385 rcu_read_unlock();
4386 if (fp)
4387 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004388
4389 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04004390 fp = find_file_locked(fh, hashval);
4391 if (likely(fp == NULL)) {
4392 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04004393 fp = new;
4394 }
4395 spin_unlock(&state_lock);
4396
4397 return fp;
4398}
4399
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004400/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 * Called to check deny when READ with all zero stateid or
4402 * WRITE with all zero or all one stateid
4403 */
Al Virob37ad282006-10-19 23:28:59 -07004404static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4406{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004408 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Trond Myklebustca943212014-07-23 16:17:39 -04004410 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07004411 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004412 return ret;
4413 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004414 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004415 if (fp->fi_share_deny & deny_type)
4416 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004417 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004418 put_nfs4_file(fp);
4419 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420}
4421
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004422static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004424 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004425 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4426 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004427
Trond Myklebust11b91642014-07-29 21:34:08 -04004428 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004429
Jeff Layton02e12152014-07-16 10:31:57 -04004430 /*
4431 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004432 * already holding inode->i_lock.
4433 *
Jeff Laytondff13992014-07-08 14:02:49 -04004434 * If the dl_time != 0, then we know that it has already been
4435 * queued for a lease break. Don't queue it again.
4436 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004437 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004438 if (dp->dl_time == 0) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01004439 dp->dl_time = ktime_get_boottime_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004440 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004441 }
Jeff Layton02e12152014-07-16 10:31:57 -04004442 spin_unlock(&state_lock);
4443}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004445static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4446 struct rpc_task *task)
4447{
4448 struct nfs4_delegation *dp = cb_to_delegation(cb);
4449
Andrew Elblea4579742015-08-31 12:06:41 -04004450 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
4451 return 1;
4452
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004453 switch (task->tk_status) {
4454 case 0:
4455 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004456 case -NFS4ERR_DELAY:
4457 rpc_delay(task, 2 * HZ);
4458 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004459 case -EBADHANDLE:
4460 case -NFS4ERR_BAD_STATEID:
4461 /*
4462 * Race: client probably got cb_recall before open reply
4463 * granting delegation.
4464 */
4465 if (dp->dl_retries--) {
4466 rpc_delay(task, 2 * HZ);
4467 return 0;
4468 }
4469 /*FALLTHRU*/
4470 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004471 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004472 }
4473}
4474
4475static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4476{
4477 struct nfs4_delegation *dp = cb_to_delegation(cb);
4478
4479 nfs4_put_stid(&dp->dl_stid);
4480}
4481
Julia Lawallc4cb8972015-11-21 22:57:39 +01004482static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004483 .prepare = nfsd4_cb_recall_prepare,
4484 .done = nfsd4_cb_recall_done,
4485 .release = nfsd4_cb_recall_release,
4486};
4487
Jeff Layton02e12152014-07-16 10:31:57 -04004488static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4489{
4490 /*
4491 * We're assuming the state code never drops its reference
4492 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004493 * callback (and since the lease code is serialized by the
4494 * i_lock) we know the server hasn't removed the lease yet, and
4495 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004496 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004497 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004498 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004499}
4500
Jeff Layton1c8c6012013-06-21 08:58:15 -04004501/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004502static bool
4503nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004504{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004505 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004506 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4507 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004508
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004509 /*
4510 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004511 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004512 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004513 */
4514 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Jeff Layton02e12152014-07-16 10:31:57 -04004516 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004517 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004518 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004519 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004520 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521}
4522
Jeff Laytonc45198e2014-09-01 07:12:07 -04004523static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004524nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4525 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526{
4527 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004528 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 else
4530 return -EAGAIN;
4531}
4532
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004533static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004534 .lm_break = nfsd_break_deleg_cb,
4535 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536};
4537
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004538static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4539{
4540 if (nfsd4_has_session(cstate))
4541 return nfs_ok;
4542 if (seqid == so->so_seqid - 1)
4543 return nfserr_replay_me;
4544 if (seqid == so->so_seqid)
4545 return nfs_ok;
4546 return nfserr_bad_seqid;
4547}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
Jeff Layton4b24ca72014-06-30 11:48:44 -04004549static __be32 lookup_clientid(clientid_t *clid,
4550 struct nfsd4_compound_state *cstate,
Olga Kornievskaiab7342202019-09-06 15:17:21 -04004551 struct nfsd_net *nn,
4552 bool sessions)
Jeff Layton4b24ca72014-06-30 11:48:44 -04004553{
4554 struct nfs4_client *found;
4555
4556 if (cstate->clp) {
4557 found = cstate->clp;
4558 if (!same_clid(&found->cl_clientid, clid))
4559 return nfserr_stale_clientid;
4560 return nfs_ok;
4561 }
4562
4563 if (STALE_CLIENTID(clid, nn))
4564 return nfserr_stale_clientid;
4565
4566 /*
4567 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4568 * cached already then we know this is for is for v4.0 and "sessions"
4569 * will be false.
4570 */
4571 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004572 spin_lock(&nn->client_lock);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04004573 found = find_confirmed_client(clid, sessions, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004574 if (!found) {
4575 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004576 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004577 }
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04004578 atomic_inc(&found->cl_rpc_users);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004579 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004580
4581 /* Cache the nfs4_client in cstate! */
4582 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004583 return nfs_ok;
4584}
4585
Al Virob37ad282006-10-19 23:28:59 -07004586__be32
Andy Adamson66689582009-04-03 08:28:45 +03004587nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004588 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 clientid_t *clientid = &open->op_clientid;
4591 struct nfs4_client *clp = NULL;
4592 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004593 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004594 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004596 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004598 /*
4599 * In case we need it later, after we've already created the
4600 * file and don't want to risk a further failure:
4601 */
4602 open->op_file = nfsd4_alloc_file();
4603 if (open->op_file == NULL)
4604 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Olga Kornievskaiab7342202019-09-06 15:17:21 -04004606 status = lookup_clientid(clientid, cstate, nn, false);
Trond Myklebust2d91e892014-06-30 11:48:46 -04004607 if (status)
4608 return status;
4609 clp = cstate->clp;
4610
Trond Myklebustd4f04892014-07-29 21:34:36 -04004611 strhashval = ownerstr_hashval(&open->op_owner);
4612 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004613 open->op_openowner = oo;
4614 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004615 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004617 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004618 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004619 release_openowner(oo);
4620 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004621 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004622 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004623 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4624 if (status)
4625 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004626 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004627new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004628 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004629 if (oo == NULL)
4630 return nfserr_jukebox;
4631 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004632alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004633 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004634 if (!open->op_stp)
4635 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004636
4637 if (nfsd4_has_session(cstate) &&
4638 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4639 open->op_odstate = alloc_clnt_odstate(clp);
4640 if (!open->op_odstate)
4641 return nfserr_jukebox;
4642 }
4643
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004644 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645}
4646
Al Virob37ad282006-10-19 23:28:59 -07004647static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004648nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4649{
4650 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4651 return nfserr_openmode;
4652 else
4653 return nfs_ok;
4654}
4655
Daniel Mackc47d8322011-05-16 16:38:14 +02004656static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004657{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004658 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4659}
4660
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004661static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004662{
4663 struct nfs4_stid *ret;
4664
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004665 ret = find_stateid_by_type(cl, s,
4666 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004667 if (!ret)
4668 return NULL;
4669 return delegstateid(ret);
4670}
4671
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004672static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4673{
4674 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4675 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4676}
4677
Al Virob37ad282006-10-19 23:28:59 -07004678static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004679nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004680 struct nfs4_delegation **dp)
4681{
4682 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004683 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004684 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004685
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004686 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4687 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004688 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004689 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4690 nfs4_put_stid(&deleg->dl_stid);
4691 if (cl->cl_minorversion)
4692 status = nfserr_deleg_revoked;
4693 goto out;
4694 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004695 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004696 status = nfs4_check_delegmode(deleg, flags);
4697 if (status) {
4698 nfs4_put_stid(&deleg->dl_stid);
4699 goto out;
4700 }
4701 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004702out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004703 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004704 return nfs_ok;
4705 if (status)
4706 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004707 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004708 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004709}
4710
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004711static inline int nfs4_access_to_access(u32 nfs4_access)
4712{
4713 int flags = 0;
4714
4715 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4716 flags |= NFSD_MAY_READ;
4717 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4718 flags |= NFSD_MAY_WRITE;
4719 return flags;
4720}
4721
Al Virob37ad282006-10-19 23:28:59 -07004722static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4724 struct nfsd4_open *open)
4725{
4726 struct iattr iattr = {
4727 .ia_valid = ATTR_SIZE,
4728 .ia_size = 0,
4729 };
4730 if (!open->op_truncate)
4731 return 0;
4732 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004733 return nfserr_inval;
Arnd Bergmann2a1aa482019-11-03 17:50:18 +01004734 return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735}
4736
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004737static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004738 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4739 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004740{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004741 struct nfsd_file *nf = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004742 __be32 status;
4743 int oflag = nfs4_access_to_omode(open->op_share_access);
4744 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004745 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004746
Trond Myklebustde186432014-07-10 14:07:26 -04004747 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004748
4749 /*
4750 * Are we trying to set a deny mode that would conflict with
4751 * current access?
4752 */
4753 status = nfs4_file_check_deny(fp, open->op_share_deny);
4754 if (status != nfs_ok) {
4755 spin_unlock(&fp->fi_lock);
4756 goto out;
4757 }
4758
4759 /* set access to the file */
4760 status = nfs4_file_get_access(fp, open->op_share_access);
4761 if (status != nfs_ok) {
4762 spin_unlock(&fp->fi_lock);
4763 goto out;
4764 }
4765
4766 /* Set access bits in stateid */
4767 old_access_bmap = stp->st_access_bmap;
4768 set_access(open->op_share_access, stp);
4769
4770 /* Set new deny mask */
4771 old_deny_bmap = stp->st_deny_bmap;
4772 set_deny(open->op_share_deny, stp);
4773 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4774
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004775 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004776 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004777 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004778 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004779 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004780 spin_lock(&fp->fi_lock);
4781 if (!fp->fi_fds[oflag]) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004782 fp->fi_fds[oflag] = nf;
4783 nf = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -04004784 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004785 }
Trond Myklebustde186432014-07-10 14:07:26 -04004786 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004787 if (nf)
4788 nfsd_file_put(nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004789
4790 status = nfsd4_truncate(rqstp, cur_fh, open);
4791 if (status)
4792 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004793out:
4794 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004795out_put_access:
4796 stp->st_access_bmap = old_access_bmap;
4797 nfs4_file_put_access(fp, open->op_share_access);
4798 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4799 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004800}
4801
Al Virob37ad282006-10-19 23:28:59 -07004802static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004803nfs4_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 -07004804{
Al Virob37ad282006-10-19 23:28:59 -07004805 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004806 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004808 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004809 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004810
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004811 /* test and set deny mode */
4812 spin_lock(&fp->fi_lock);
4813 status = nfs4_file_check_deny(fp, open->op_share_deny);
4814 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004815 set_deny(open->op_share_deny, stp);
4816 fp->fi_share_deny |=
4817 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4818 }
4819 spin_unlock(&fp->fi_lock);
4820
4821 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004824 status = nfsd4_truncate(rqstp, cur_fh, open);
4825 if (status != nfs_ok)
4826 reset_union_bmap_deny(old_deny_bmap, stp);
4827 return status;
4828}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004830/* Should we give out recallable state?: */
4831static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4832{
4833 if (clp->cl_cb_state == NFSD4_CB_UP)
4834 return true;
4835 /*
4836 * In the sessions case, since we don't have to establish a
4837 * separate connection for callbacks, we assume it's OK
4838 * until we hear otherwise:
4839 */
4840 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4841}
4842
J. Bruce Fields653e5142018-02-15 22:08:45 -05004843static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4844 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004845{
4846 struct file_lock *fl;
4847
4848 fl = locks_alloc_lock();
4849 if (!fl)
4850 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004851 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004852 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004853 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4854 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004855 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004856 fl->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004857 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004858 return fl;
4859}
4860
Jeff Layton0b266932014-07-25 07:34:25 -04004861static struct nfs4_delegation *
4862nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004863 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004864{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004865 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004866 struct nfs4_delegation *dp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004867 struct nfsd_file *nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004868 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004869
J. Bruce Fields353601e2018-02-16 14:29:42 -05004870 /*
4871 * The fi_had_conflict and nfs_get_existing_delegation checks
4872 * here are just optimizations; we'll need to recheck them at
4873 * the end:
4874 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004875 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004876 return ERR_PTR(-EAGAIN);
4877
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004878 nf = find_readable_file(fp);
4879 if (!nf) {
J. Bruce Fields353601e2018-02-16 14:29:42 -05004880 /* We should always have a readable file here */
4881 WARN_ON_ONCE(1);
4882 return ERR_PTR(-EBADF);
4883 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004884 spin_lock(&state_lock);
4885 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004886 if (nfs4_delegation_exists(clp, fp))
4887 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004888 else if (!fp->fi_deleg_file) {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004889 fp->fi_deleg_file = nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004890 /* increment early to prevent fi_deleg_file from being
4891 * cleared */
4892 fp->fi_delegees = 1;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004893 nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004894 } else
4895 fp->fi_delegees++;
4896 spin_unlock(&fp->fi_lock);
4897 spin_unlock(&state_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004898 if (nf)
4899 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004900 if (status)
4901 return ERR_PTR(status);
4902
4903 status = -ENOMEM;
4904 dp = alloc_init_deleg(clp, fp, fh, odstate);
4905 if (!dp)
4906 goto out_delegees;
4907
4908 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4909 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04004910 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004911
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004912 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004913 if (fl)
4914 locks_free_lock(fl);
4915 if (status)
4916 goto out_clnt_odstate;
4917
4918 spin_lock(&state_lock);
4919 spin_lock(&fp->fi_lock);
4920 if (fp->fi_had_conflict)
4921 status = -EAGAIN;
4922 else
4923 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04004924 spin_unlock(&fp->fi_lock);
4925 spin_unlock(&state_lock);
4926
4927 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04004928 goto out_unlock;
4929
Jeff Layton0b266932014-07-25 07:34:25 -04004930 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04004931out_unlock:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004932 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004933out_clnt_odstate:
4934 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004935 nfs4_put_stid(&dp->dl_stid);
4936out_delegees:
4937 put_deleg_file(fp);
4938 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004939}
4940
Benny Halevy4aa89132012-02-21 14:16:44 -08004941static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4942{
4943 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4944 if (status == -EAGAIN)
4945 open->op_why_no_deleg = WND4_CONTENTION;
4946 else {
4947 open->op_why_no_deleg = WND4_RESOURCE;
4948 switch (open->op_deleg_want) {
4949 case NFS4_SHARE_WANT_READ_DELEG:
4950 case NFS4_SHARE_WANT_WRITE_DELEG:
4951 case NFS4_SHARE_WANT_ANY_DELEG:
4952 break;
4953 case NFS4_SHARE_WANT_CANCEL:
4954 open->op_why_no_deleg = WND4_CANCELLED;
4955 break;
4956 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004957 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004958 }
4959 }
4960}
4961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962/*
4963 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004964 *
4965 * Note we don't support write delegations, and won't until the vfs has
4966 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 */
4968static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004969nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4970 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971{
4972 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004973 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4974 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004975 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004976 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004978 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004979 open->op_recall = 0;
4980 switch (open->op_claim_type) {
4981 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004982 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004983 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004984 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4985 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004986 break;
4987 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004988 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004989 /*
4990 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004991 * had the chance to reclaim theirs, *and* until
4992 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004993 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004994 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004995 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004996 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004997 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004998 /*
4999 * Also, if the file was opened for write or
5000 * create, there's a good chance the client's
5001 * about to write to it, resulting in an
5002 * immediate recall (since we don't support
5003 * write delegations):
5004 */
NeilBrown7b190fe2005-06-23 22:03:23 -07005005 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04005006 goto out_no_deleg;
5007 if (open->op_create == NFS4_OPEN_CREATE)
5008 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005009 break;
5010 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005011 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07005012 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005013 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04005014 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005015 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005017 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005019 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04005020 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04005021 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04005022 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005023 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05005024out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04005025 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5026 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005027 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04005028 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04005029 open->op_recall = 1;
5030 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04005031
5032 /* 4.1 client asking for a delegation? */
5033 if (open->op_deleg_want)
5034 nfsd4_open_deleg_none_ext(open, status);
5035 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036}
5037
Benny Halevye27f49c2012-02-21 14:16:54 -08005038static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5039 struct nfs4_delegation *dp)
5040{
5041 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5042 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5043 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5044 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5045 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5046 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5047 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5048 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5049 }
5050 /* Otherwise the client must be confused wanting a delegation
5051 * it already has, therefore we don't return
5052 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5053 */
5054}
5055
Al Virob37ad282006-10-19 23:28:59 -07005056__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5058{
Andy Adamson66689582009-04-03 08:28:45 +03005059 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005060 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005062 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07005063 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005064 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005065 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 /*
5068 * Lookup file; if found, lookup stateid and check open request,
5069 * and check for delegations in the process of being recalled.
5070 * If not found, create the nfs4_file struct
5071 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04005072 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04005073 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04005074 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07005075 if (status)
5076 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005077 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04005079 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07005080 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04005081 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07005082 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 }
5084
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005085 if (!stp) {
5086 stp = init_open_stateid(fp, open);
5087 if (!open->op_stp)
5088 new_stp = true;
5089 }
5090
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 /*
5092 * OPEN the file, or upgrade an existing OPEN.
5093 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005094 *
5095 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005097 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005099 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005100 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005101 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005105 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5106 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005107 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005108 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005109 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005110 goto out;
5111 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005112
5113 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5114 open->op_odstate);
5115 if (stp->st_clnt_odstate == open->op_odstate)
5116 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005118
Jeff Layton9767feb2015-10-01 09:05:50 -04005119 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005120 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
Benny Halevyd24433c2012-02-16 20:57:17 +02005122 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005123 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5124 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5125 open->op_why_no_deleg = WND4_NOT_WANTED;
5126 goto nodeleg;
5127 }
5128 }
5129
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 /*
5131 * Attempt to hand out a delegation. No error return, because the
5132 * OPEN succeeds even if we fail.
5133 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005134 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005135nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 status = nfs_ok;
5137
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005138 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005139 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005141 /* 4.1 client trying to upgrade/downgrade delegation? */
5142 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005143 open->op_deleg_want)
5144 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005145
NeilBrown13cd2182005-06-23 22:03:10 -07005146 if (fp)
5147 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005148 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005149 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 /*
5151 * To finish the open response, we just need to set the rflags.
5152 */
5153 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005154 if (nfsd4_has_session(&resp->cstate))
5155 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5156 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005158
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005159 if (dp)
5160 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005161 if (stp)
5162 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163
5164 return status;
5165}
5166
Jeff Layton58fb12e2014-07-29 21:34:27 -04005167void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005168 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005169{
5170 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005171 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005172
Jeff Laytond3134b12014-07-29 21:34:32 -04005173 nfsd4_cstate_assign_replay(cstate, so);
5174 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005175 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005176 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005177 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005178 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005179 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005180 if (open->op_odstate)
5181 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005182}
5183
Al Virob37ad282006-10-19 23:28:59 -07005184__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005185nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005186 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005188 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005190 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005191 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 dprintk("process_renew(%08x/%08x): starting\n",
5194 clid->cl_boot, clid->cl_id);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005195 status = lookup_clientid(clid, cstate, nn, false);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005196 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005198 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07005200 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005201 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 goto out;
5203 status = nfs_ok;
5204out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 return status;
5206}
5207
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005208void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005209nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005210{
Jeff Layton33dcc482012-04-10 11:08:48 -04005211 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005212 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005213 return;
5214
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005215 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005216 /*
5217 * If the server goes down again right now, an NFSv4
5218 * client will still be allowed to reclaim after it comes back up,
5219 * even if it hasn't yet had a chance to reclaim state this time.
5220 *
5221 */
Jeff Layton919b8042014-09-12 16:40:20 -04005222 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005223 /*
5224 * At this point, NFSv4 clients can still reclaim. But if the
5225 * server crashes, any that have not yet reclaimed will be out
5226 * of luck on the next boot.
5227 *
5228 * (NFSv4.1+ clients are considered to have reclaimed once they
5229 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5230 * have reclaimed after their first OPEN.)
5231 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005232 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005233 /*
5234 * At this point, and once lockd and/or any other containers
5235 * exit their grace period, further reclaims will fail and
5236 * regular locking can resume.
5237 */
NeilBrowna76b4312005-06-23 22:04:01 -07005238}
5239
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005240/*
5241 * If we've waited a lease period but there are still clients trying to
5242 * reclaim, wait a little longer to give them a chance to finish.
5243 */
5244static bool clients_still_reclaiming(struct nfsd_net *nn)
5245{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005246 time64_t double_grace_period_end = nn->boot_time +
5247 2 * nn->nfsd4_lease;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005248
Scott Mayhew362063a2019-03-26 18:06:28 -04005249 if (nn->track_reclaim_completes &&
5250 atomic_read(&nn->nr_reclaim_complete) ==
5251 nn->reclaim_str_hashtbl_size)
5252 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005253 if (!nn->somebody_reclaimed)
5254 return false;
5255 nn->somebody_reclaimed = false;
5256 /*
5257 * If we've given them *two* lease times to reclaim, and they're
5258 * still not done, give up:
5259 */
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005260 if (ktime_get_boottime_seconds() > double_grace_period_end)
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005261 return false;
5262 return true;
5263}
5264
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005265static time64_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005266nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267{
5268 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005269 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005271 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005272 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 struct list_head *pos, *next, reaplist;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005274 time64_t cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease;
5275 time64_t t, new_timeo = nn->nfsd4_lease;
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005276 struct nfs4_cpntf_state *cps;
5277 copy_stateid_t *cps_t;
5278 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005281
5282 if (clients_still_reclaiming(nn)) {
5283 new_timeo = 0;
5284 goto out;
5285 }
Scott Mayhew362063a2019-03-26 18:06:28 -04005286 dprintk("NFSD: end of grace period\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005287 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005288 INIT_LIST_HEAD(&reaplist);
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005289
5290 spin_lock(&nn->s2s_cp_lock);
5291 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5292 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5293 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005294 cps->cpntf_time > cutoff)
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005295 _free_cpntf_state_locked(nn, cps);
5296 }
5297 spin_unlock(&nn->s2s_cp_lock);
5298
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005299 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005300 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 clp = list_entry(pos, struct nfs4_client, cl_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005302 if (clp->cl_time > cutoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04005304 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 break;
5306 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04005307 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03005308 dprintk("NFSD: client in use (clientid %08x)\n",
5309 clp->cl_clientid.cl_id);
5310 continue;
5311 }
Trond Myklebust4864af92014-07-30 08:27:03 -04005312 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005313 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005314 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005315 list_for_each_safe(pos, next, &reaplist) {
5316 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 dprintk("NFSD: purging unused client (clientid %08x)\n",
5318 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04005319 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 expire_client(clp);
5321 }
Benny Halevycdc975052014-05-30 09:09:30 -04005322 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005323 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005325 if (dp->dl_time > cutoff) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005326 t = dp->dl_time - cutoff;
5327 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 break;
5329 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005330 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005331 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 }
Benny Halevycdc975052014-05-30 09:09:30 -04005333 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005334 while (!list_empty(&reaplist)) {
5335 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5336 dl_recall_lru);
5337 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005338 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 }
Jeff Layton217526e2014-07-30 08:27:11 -04005340
5341 spin_lock(&nn->client_lock);
5342 while (!list_empty(&nn->close_lru)) {
5343 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5344 oo_close_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005345 if (oo->oo_time > cutoff) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005346 t = oo->oo_time - cutoff;
5347 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 break;
5349 }
Jeff Layton217526e2014-07-30 08:27:11 -04005350 list_del_init(&oo->oo_close_lru);
5351 stp = oo->oo_last_closed_stid;
5352 oo->oo_last_closed_stid = NULL;
5353 spin_unlock(&nn->client_lock);
5354 nfs4_put_stid(&stp->st_stid);
5355 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 }
Jeff Layton217526e2014-07-30 08:27:11 -04005357 spin_unlock(&nn->client_lock);
5358
Jeff Layton7919d0a2016-09-16 16:28:25 -04005359 /*
5360 * It's possible for a client to try and acquire an already held lock
5361 * that is being held for a long time, and then lose interest in it.
5362 * So, we clean out any un-revisited request after a lease period
5363 * under the assumption that the client is no longer interested.
5364 *
5365 * RFC5661, sec. 9.6 states that the client must not rely on getting
5366 * notifications and must continue to poll for locks, even when the
5367 * server supports them. Thus this shouldn't lead to clients blocking
5368 * indefinitely once the lock does become free.
5369 */
5370 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005371 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005372 while (!list_empty(&nn->blocked_locks_lru)) {
5373 nbl = list_first_entry(&nn->blocked_locks_lru,
5374 struct nfsd4_blocked_lock, nbl_lru);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005375 if (nbl->nbl_time > cutoff) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04005376 t = nbl->nbl_time - cutoff;
5377 new_timeo = min(new_timeo, t);
5378 break;
5379 }
5380 list_move(&nbl->nbl_lru, &reaplist);
5381 list_del_init(&nbl->nbl_list);
5382 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005383 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005384
5385 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005386 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005387 struct nfsd4_blocked_lock, nbl_lru);
5388 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005389 free_blocked_lock(nbl);
5390 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005391out:
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005392 new_timeo = max_t(time64_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04005393 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394}
5395
Harvey Harrisona254b242008-02-20 12:49:00 -08005396static struct workqueue_struct *laundry_wq;
5397static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005398
5399static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005400laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401{
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005402 time64_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005403 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005404 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5405 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005407 t = nfs4_laundromat(nn);
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005408 dprintk("NFSD: laundromat_main - sleeping for %lld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005409 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410}
5411
Jeff Layton8fcd4612015-07-30 06:57:46 -04005412static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005413{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005414 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005415 return nfserr_bad_stateid;
5416 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417}
5418
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005420access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005422 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5423 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5424 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425}
5426
5427static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005428access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005430 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5431 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432}
5433
5434static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005435__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
Al Virob37ad282006-10-19 23:28:59 -07005437 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438
J. Bruce Fields02921912010-07-29 15:16:59 -04005439 /* For lock stateid's, we test the parent open, not the lock: */
5440 if (stp->st_openstp)
5441 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005442 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005444 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 goto out;
5446 status = nfs_ok;
5447out:
5448 return status;
5449}
5450
Al Virob37ad282006-10-19 23:28:59 -07005451static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005452check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005454 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005456 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005457 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 * conflicting state; so we must wait out the grace period. */
5459 return nfserr_grace;
5460 } else if (flags & WR_STATE)
5461 return nfs4_share_conflict(current_fh,
5462 NFS4_SHARE_DENY_WRITE);
5463 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5464 return nfs4_share_conflict(current_fh,
5465 NFS4_SHARE_DENY_READ);
5466}
5467
5468/*
5469 * Allow READ/WRITE during grace period on recovered state only for files
5470 * that are not able to provide mandatory locking.
5471 */
5472static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005473grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005475 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476}
5477
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005478static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005479{
Andy Adamson66689582009-04-03 08:28:45 +03005480 /*
5481 * When sessions are used the stateid generation number is ignored
5482 * when it is zero.
5483 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005484 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005485 return nfs_ok;
5486
5487 if (in->si_generation == ref->si_generation)
5488 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005489
J. Bruce Fields0836f582008-01-26 19:08:12 -05005490 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005491 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005492 return nfserr_bad_stateid;
5493 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005494 * However, we could see a stateid from the past, even from a
5495 * non-buggy client. For example, if the client sends a lock
5496 * while some IO is outstanding, the lock may bump si_generation
5497 * while the IO is still in flight. The client could avoid that
5498 * situation by waiting for responses on all the IO requests,
5499 * but better performance may result in retrying IO that
5500 * receives an old_stateid error if requests are rarely
5501 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005502 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005503 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005504}
5505
Trond Myklebust03da3162017-11-03 08:00:16 -04005506static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5507{
5508 __be32 ret;
5509
5510 spin_lock(&s->sc_lock);
5511 ret = nfsd4_verify_open_stid(s);
5512 if (ret == nfs_ok)
5513 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5514 spin_unlock(&s->sc_lock);
5515 return ret;
5516}
5517
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005518static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5519{
5520 if (ols->st_stateowner->so_is_open_owner &&
5521 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5522 return nfserr_bad_stateid;
5523 return nfs_ok;
5524}
5525
Chuck Lever7df302f2012-05-29 13:56:37 -04005526static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005527{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005528 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005529 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005530
Andrew Elbleae254dac2017-11-09 13:41:10 -05005531 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5532 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005533 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005534 /* Client debugging aid. */
5535 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5536 char addr_str[INET6_ADDRSTRLEN];
5537 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5538 sizeof(addr_str));
5539 pr_warn_ratelimited("NFSD: client %s testing state ID "
5540 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005541 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005542 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005543 spin_lock(&cl->cl_lock);
5544 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005545 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005546 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005547 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005548 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005549 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005550 switch (s->sc_type) {
5551 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005552 status = nfs_ok;
5553 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005554 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005555 status = nfserr_deleg_revoked;
5556 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005557 case NFS4_OPEN_STID:
5558 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005559 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005560 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005561 default:
5562 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005563 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04005564 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005565 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005566 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005567 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005568out_unlock:
5569 spin_unlock(&cl->cl_lock);
5570 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005571}
5572
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005573__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005574nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5575 stateid_t *stateid, unsigned char typemask,
5576 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005577{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005578 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005579 bool return_revoked = false;
5580
5581 /*
5582 * only return revoked delegations if explicitly asked.
5583 * otherwise we report revoked or bad_stateid status.
5584 */
5585 if (typemask & NFS4_REVOKED_DELEG_STID)
5586 return_revoked = true;
5587 else if (typemask & NFS4_DELEG_STID)
5588 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005589
Andrew Elbleae254dac2017-11-09 13:41:10 -05005590 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5591 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005592 return nfserr_bad_stateid;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005593 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn,
5594 false);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005595 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005596 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005597 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005598 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005599 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005600 if (status)
5601 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005602 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005603 if (!*s)
5604 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005605 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5606 nfs4_put_stid(*s);
5607 if (cstate->minorversion)
5608 return nfserr_deleg_revoked;
5609 return nfserr_bad_stateid;
5610 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005611 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005612}
5613
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005614static struct nfsd_file *
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005615nfs4_find_file(struct nfs4_stid *s, int flags)
5616{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005617 if (!s)
5618 return NULL;
5619
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005620 switch (s->sc_type) {
5621 case NFS4_DELEG_STID:
5622 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5623 return NULL;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005624 return nfsd_file_get(s->sc_file->fi_deleg_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005625 case NFS4_OPEN_STID:
5626 case NFS4_LOCK_STID:
5627 if (flags & RD_STATE)
5628 return find_readable_file(s->sc_file);
5629 else
5630 return find_writeable_file(s->sc_file);
5631 break;
5632 }
5633
5634 return NULL;
5635}
5636
5637static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005638nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005639{
5640 __be32 status;
5641
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005642 status = nfsd4_check_openowner_confirmed(ols);
5643 if (status)
5644 return status;
5645 return nfs4_check_openmode(ols, flags);
5646}
5647
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005648static __be32
5649nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
Jeff Layton5c4583b2019-08-18 14:18:54 -04005650 struct nfsd_file **nfp, int flags)
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005651{
5652 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005653 struct nfsd_file *nf;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005654 __be32 status;
5655
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005656 nf = nfs4_find_file(s, flags);
5657 if (nf) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005658 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5659 acc | NFSD_MAY_OWNER_OVERRIDE);
Jeff Layton5c4583b2019-08-18 14:18:54 -04005660 if (status) {
5661 nfsd_file_put(nf);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005662 goto out;
Jeff Layton5c4583b2019-08-18 14:18:54 -04005663 }
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005664 } else {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005665 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005666 if (status)
5667 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005668 }
Jeff Layton5c4583b2019-08-18 14:18:54 -04005669 *nfp = nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005670out:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005671 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005672}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005673static void
5674_free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5675{
5676 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5677 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5678 return;
5679 list_del(&cps->cp_list);
5680 idr_remove(&nn->s2s_cp_stateids,
5681 cps->cp_stateid.stid.si_opaque.so_id);
5682 kfree(cps);
5683}
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005684/*
5685 * A READ from an inter server to server COPY will have a
5686 * copy stateid. Look up the copy notify stateid from the
5687 * idr structure and take a reference on it.
5688 */
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005689__be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5690 struct nfs4_client *clp,
5691 struct nfs4_cpntf_state **cps)
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005692{
5693 copy_stateid_t *cps_t;
5694 struct nfs4_cpntf_state *state = NULL;
5695
5696 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5697 return nfserr_bad_stateid;
5698 spin_lock(&nn->s2s_cp_lock);
5699 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5700 if (cps_t) {
5701 state = container_of(cps_t, struct nfs4_cpntf_state,
5702 cp_stateid);
Dan Carpenter5277a792019-12-04 10:59:36 +03005703 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5704 state = NULL;
5705 goto unlock;
5706 }
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005707 if (!clp)
5708 refcount_inc(&state->cp_stateid.sc_count);
5709 else
5710 _free_cpntf_state_locked(nn, state);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005711 }
Dan Carpenter5277a792019-12-04 10:59:36 +03005712unlock:
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005713 spin_unlock(&nn->s2s_cp_lock);
5714 if (!state)
5715 return nfserr_bad_stateid;
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005716 if (!clp && state)
5717 *cps = state;
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005718 return 0;
5719}
5720
5721static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5722 struct nfs4_stid **stid)
5723{
5724 __be32 status;
5725 struct nfs4_cpntf_state *cps = NULL;
5726 struct nfsd4_compound_state cstate;
5727
Olga Kornievskaiace0887a2019-10-09 11:50:48 -04005728 status = manage_cpntf_state(nn, st, NULL, &cps);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005729 if (status)
5730 return status;
5731
Arnd Bergmann20b7d862019-11-04 16:31:52 +01005732 cps->cpntf_time = ktime_get_boottime_seconds();
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005733 memset(&cstate, 0, sizeof(cstate));
5734 status = lookup_clientid(&cps->cp_p_clid, &cstate, nn, true);
5735 if (status)
5736 goto out;
5737 status = nfsd4_lookup_stateid(&cstate, &cps->cp_p_stateid,
5738 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5739 stid, nn);
5740 put_client_renew(cstate.clp);
5741out:
5742 nfs4_put_cpntf_state(nn, cps);
5743 return status;
5744}
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005745
5746void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5747{
5748 spin_lock(&nn->s2s_cp_lock);
5749 _free_cpntf_state_locked(nn, cps);
5750 spin_unlock(&nn->s2s_cp_lock);
5751}
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005752
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005754 * Checks for stateid operations
5755 */
Al Virob37ad282006-10-19 23:28:59 -07005756__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005757nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005758 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005759 stateid_t *stateid, int flags, struct nfsd_file **nfp,
5760 struct nfs4_stid **cstid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005762 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005763 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005764 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005765 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005766 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
Jeff Layton5c4583b2019-08-18 14:18:54 -04005768 if (nfp)
5769 *nfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005771 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 return nfserr_grace;
5773
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005774 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5775 status = check_special_stateids(net, fhp, stateid, flags);
5776 goto done;
5777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005779 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005780 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005781 &s, nn);
Olga Kornievskaiab7342202019-09-06 15:17:21 -04005782 if (status == nfserr_bad_stateid)
5783 status = find_cpntf_state(nn, stateid, &s);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005784 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005785 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005786 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005787 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005788 if (status)
5789 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005790
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005791 switch (s->sc_type) {
5792 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005793 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005794 break;
5795 case NFS4_OPEN_STID:
5796 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005797 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005798 break;
5799 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005800 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005803 if (status)
5804 goto out;
5805 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005806
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005807done:
Jeff Layton5c4583b2019-08-18 14:18:54 -04005808 if (status == nfs_ok && nfp)
5809 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810out:
Olga Kornievskaia624322f2019-10-04 16:34:26 -04005811 if (s) {
5812 if (!status && cstid)
5813 *cstid = s;
5814 else
5815 nfs4_put_stid(s);
5816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 return status;
5818}
5819
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005820/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005821 * Test if the stateid is valid
5822 */
5823__be32
5824nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005825 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005826{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005827 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005828 struct nfsd4_test_stateid_id *stateid;
5829 struct nfs4_client *cl = cstate->session->se_client;
5830
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005831 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005832 stateid->ts_id_status =
5833 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005834
Bryan Schumaker17456802011-07-13 10:50:48 -04005835 return nfs_ok;
5836}
5837
Chuck Lever42691392016-08-11 10:37:30 -04005838static __be32
5839nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5840{
5841 struct nfs4_ol_stateid *stp = openlockstateid(s);
5842 __be32 ret;
5843
Trond Myklebust659aefb2017-11-03 08:00:13 -04005844 ret = nfsd4_lock_ol_stateid(stp);
5845 if (ret)
5846 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005847
5848 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5849 if (ret)
5850 goto out;
5851
5852 ret = nfserr_locks_held;
5853 if (check_for_locks(stp->st_stid.sc_file,
5854 lockowner(stp->st_stateowner)))
5855 goto out;
5856
5857 release_lock_stateid(stp);
5858 ret = nfs_ok;
5859
5860out:
5861 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005862out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005863 nfs4_put_stid(s);
5864 return ret;
5865}
5866
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005867__be32
5868nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005869 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005870{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005871 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005872 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005873 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005874 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005875 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005876 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005877
Jeff Layton1af71cc2014-07-29 21:34:14 -04005878 spin_lock(&cl->cl_lock);
5879 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005880 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005881 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005882 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005883 switch (s->sc_type) {
5884 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005885 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005886 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005887 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005888 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5889 if (ret)
5890 break;
5891 ret = nfserr_locks_held;
5892 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005893 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005894 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005895 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005896 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005897 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005898 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005899 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005900 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005901 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005902 list_del_init(&dp->dl_recall_lru);
5903 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005904 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005905 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005906 goto out;
5907 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005908 }
Trond Myklebust03da3162017-11-03 08:00:16 -04005909 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005910out_unlock:
5911 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005912out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005913 return ret;
5914}
5915
NeilBrown4c4cd222005-07-07 17:59:27 -07005916static inline int
5917setlkflg (int type)
5918{
5919 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5920 RD_STATE : WR_STATE;
5921}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005923static __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 -04005924{
5925 struct svc_fh *current_fh = &cstate->current_fh;
5926 struct nfs4_stateowner *sop = stp->st_stateowner;
5927 __be32 status;
5928
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005929 status = nfsd4_check_seqid(cstate, sop, seqid);
5930 if (status)
5931 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04005932 status = nfsd4_lock_ol_stateid(stp);
5933 if (status != nfs_ok)
5934 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005935 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005936 if (status == nfs_ok)
5937 status = nfs4_check_fh(current_fh, &stp->st_stid);
5938 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005939 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005940 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005941}
5942
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943/*
5944 * Checks for sequence id mutating operations.
5945 */
Al Virob37ad282006-10-19 23:28:59 -07005946static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005947nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005948 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005949 struct nfs4_ol_stateid **stpp,
5950 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005952 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005953 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005954 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005956 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5957 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005958
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005960 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005961 if (status)
5962 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005963 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005964 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005966 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005967 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005968 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005969 else
5970 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005971 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005972}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005973
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005974static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5975 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005976{
5977 __be32 status;
5978 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005979 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005981 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005982 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005983 if (status)
5984 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005985 oo = openowner(stp->st_stateowner);
5986 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005987 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005988 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005989 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005990 }
5991 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005992 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993}
5994
Al Virob37ad282006-10-19 23:28:59 -07005995__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005996nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005997 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005999 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07006000 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006001 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006002 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006003 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004
Al Viroa6a9f182013-09-16 10:57:01 -04006005 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6006 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006008 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07006009 if (status)
6010 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006012 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006013 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006014 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006015 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006016 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006017 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006018 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006019 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006020 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04006021 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04006022 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04006023 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04006024 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006025 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02006026 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006027 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07006028
Jeff Layton2a4317c2012-03-21 16:42:43 -04006029 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04006030 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04006031put_stateid:
6032 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006034 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 return status;
6036}
6037
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006038static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039{
Jeff Layton82c5ff12012-05-11 09:45:13 -04006040 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006041 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04006042 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006043 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006044}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04006045
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006046static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6047{
6048 switch (to_access) {
6049 case NFS4_SHARE_ACCESS_READ:
6050 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6051 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6052 break;
6053 case NFS4_SHARE_ACCESS_WRITE:
6054 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6055 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6056 break;
6057 case NFS4_SHARE_ACCESS_BOTH:
6058 break;
6059 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006060 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 }
6062}
6063
Al Virob37ad282006-10-19 23:28:59 -07006064__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006065nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006066 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006068 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07006069 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006070 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006071 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
Al Viroa6a9f182013-09-16 10:57:01 -04006073 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6074 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04006076 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02006077 if (od->od_deleg_want)
6078 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6079 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006081 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006082 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006083 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04006086 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006087 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006089 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04006091 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04006092 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006094 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04006096 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04006097 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04006098 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006100put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006101 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006102 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006104 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 return status;
6106}
6107
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006108static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6109{
Trond Myklebustacf92952014-06-30 11:48:37 -04006110 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04006111 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04006112 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006113
Jeff Layton2c41beb2014-07-29 21:34:41 -04006114 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04006115 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04006116
Jeff Laytond83017f2014-07-29 21:34:42 -04006117 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04006118 if (unhashed)
6119 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04006120 spin_unlock(&clp->cl_lock);
6121 free_ol_stateid_reaplist(&reaplist);
6122 } else {
6123 spin_unlock(&clp->cl_lock);
6124 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04006125 if (unhashed)
6126 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04006127 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04006128}
6129
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130/*
6131 * nfs4_unlock_state() called after encode
6132 */
Al Virob37ad282006-10-19 23:28:59 -07006133__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006134nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006135 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006137 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07006138 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006139 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006140 struct net *net = SVC_NET(rqstp);
6141 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142
Al Viroa6a9f182013-09-16 10:57:01 -04006143 dprintk("NFSD: nfsd4_close on file %pd\n",
6144 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006146 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6147 &close->cl_stateid,
6148 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006149 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006150 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006151 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006153
6154 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006155
6156 /*
6157 * Technically we don't _really_ have to increment or copy it, since
6158 * it should just be gone after this operation and we clobber the
6159 * copied value below, but we continue to do so here just to ensure
6160 * that racing ops see that there was a state change.
6161 */
Jeff Layton9767feb2015-10-01 09:05:50 -04006162 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04006164 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04006165 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006166
Jeff Laytonbd2deca2018-02-15 15:16:18 -05006167 /* v4.1+ suggests that we send a special stateid in here, since the
6168 * clients should just ignore this anyway. Since this is not useful
6169 * for v4.0 clients either, we set it to the special close_stateid
6170 * universally.
6171 *
6172 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6173 */
6174 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04006175
Trond Myklebust8a0b5892014-07-29 21:34:20 -04006176 /* put reference from nfs4_preprocess_seqid_op */
6177 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 return status;
6180}
6181
Al Virob37ad282006-10-19 23:28:59 -07006182__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006183nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006184 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006186 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006187 struct nfs4_delegation *dp;
6188 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006189 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07006190 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006191 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006193 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006194 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006196 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006197 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006198 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006199 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04006200 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006201 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04006202 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006203
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006204 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006205put_stateid:
6206 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207out:
6208 return status;
6209}
6210
Benny Halevy87df4de2008-12-15 19:42:03 +02006211static inline u64
6212end_offset(u64 start, u64 len)
6213{
6214 u64 end;
6215
6216 end = start + len;
6217 return end >= start ? end: NFS4_MAX_UINT64;
6218}
6219
6220/* last octet in a range */
6221static inline u64
6222last_byte_offset(u64 start, u64 len)
6223{
6224 u64 end;
6225
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006226 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006227 end = start + len;
6228 return end > start ? end - 1: NFS4_MAX_UINT64;
6229}
6230
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231/*
6232 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6233 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6234 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6235 * locking, this prevents us from being completely protocol-compliant. The
6236 * real solution to this problem is to start using unsigned file offsets in
6237 * the VFS, but this is a very deep change!
6238 */
6239static inline void
6240nfs4_transform_lock_offset(struct file_lock *lock)
6241{
6242 if (lock->fl_start < 0)
6243 lock->fl_start = OFFSET_MAX;
6244 if (lock->fl_end < 0)
6245 lock->fl_end = OFFSET_MAX;
6246}
6247
Jeff Laytoncae80b32015-04-03 09:04:04 -04006248static fl_owner_t
6249nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006250{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006251 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6252
6253 nfs4_get_stateowner(&lo->lo_owner);
6254 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006255}
6256
Jeff Laytoncae80b32015-04-03 09:04:04 -04006257static void
6258nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006259{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006260 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006261
Jeff Laytoncae80b32015-04-03 09:04:04 -04006262 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006263 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006264}
6265
Jeff Layton76d348f2016-09-16 16:28:24 -04006266static void
6267nfsd4_lm_notify(struct file_lock *fl)
6268{
6269 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6270 struct net *net = lo->lo_owner.so_client->net;
6271 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6272 struct nfsd4_blocked_lock *nbl = container_of(fl,
6273 struct nfsd4_blocked_lock, nbl_lock);
6274 bool queue = false;
6275
Jeff Layton7919d0a2016-09-16 16:28:25 -04006276 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006277 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006278 if (!list_empty(&nbl->nbl_list)) {
6279 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006280 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006281 queue = true;
6282 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006283 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006284
6285 if (queue)
6286 nfsd4_run_cb(&nbl->nbl_cb);
6287}
6288
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006289static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006290 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006291 .lm_get_owner = nfsd4_fl_get_owner,
6292 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006293};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
6295static inline void
6296nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6297{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006298 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299
NeilBrownd5b90262006-04-10 22:55:22 -07006300 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006301 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04006302 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6303 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006304 if (!deny->ld_owner.data)
6305 /* We just don't care that much */
6306 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006307 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006308 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006309nevermind:
6310 deny->ld_owner.len = 0;
6311 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006312 deny->ld_clientid.cl_boot = 0;
6313 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 }
6315 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006316 deny->ld_length = NFS4_MAX_UINT64;
6317 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6319 deny->ld_type = NFS4_READ_LT;
6320 if (fl->fl_type != F_RDLCK)
6321 deny->ld_type = NFS4_WRITE_LT;
6322}
6323
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006324static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006325find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006327 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006328 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329
Trond Myklebust0a880a22014-07-30 08:27:10 -04006330 lockdep_assert_held(&clp->cl_lock);
6331
Trond Myklebustd4f04892014-07-29 21:34:36 -04006332 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6333 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006334 if (so->so_is_open_owner)
6335 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006336 if (same_owner_str(so, owner))
6337 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 }
6339 return NULL;
6340}
6341
Trond Myklebustc58c6612014-07-29 21:34:35 -04006342static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006343find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006344{
6345 struct nfs4_lockowner *lo;
6346
Trond Myklebustd4f04892014-07-29 21:34:36 -04006347 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006348 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006349 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006350 return lo;
6351}
6352
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006353static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6354{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006355 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006356}
6357
Jeff Layton6b180f02014-07-29 21:34:26 -04006358static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6359{
6360 struct nfs4_lockowner *lo = lockowner(sop);
6361
6362 kmem_cache_free(lockowner_slab, lo);
6363}
6364
6365static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006366 .so_unhash = nfs4_unhash_lockowner,
6367 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006368};
6369
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370/*
6371 * Alloc a lock owner structure.
6372 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006373 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006375 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006377static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006378alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6379 struct nfs4_ol_stateid *open_stp,
6380 struct nfsd4_lock *lock)
6381{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006382 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006384 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6385 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006387 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006388 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6389 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006390 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006391 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006392 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006393 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006394 if (ret == NULL) {
6395 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006396 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006397 ret = lo;
6398 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006399 nfs4_free_stateowner(&lo->lo_owner);
6400
Trond Myklebustd4f04892014-07-29 21:34:36 -04006401 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006402 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403}
6404
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006405static struct nfs4_ol_stateid *
Trond Myklebusta451b122020-03-01 18:21:38 -05006406find_lock_stateid(const struct nfs4_lockowner *lo,
6407 const struct nfs4_ol_stateid *ost)
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006408{
6409 struct nfs4_ol_stateid *lst;
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006410
Trond Myklebusta451b122020-03-01 18:21:38 -05006411 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006412
Trond Myklebusta451b122020-03-01 18:21:38 -05006413 /* If ost is not hashed, ost->st_locks will not be valid */
6414 if (!nfs4_ol_stateid_unhashed(ost))
6415 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6416 if (lst->st_stateowner == &lo->lo_owner) {
6417 refcount_inc(&lst->st_stid.sc_count);
6418 return lst;
6419 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006420 }
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006421 return NULL;
6422}
6423
Trond Myklebustbeeca192017-11-03 08:00:14 -04006424static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006425init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6426 struct nfs4_file *fp, struct inode *inode,
6427 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006429 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006430 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431
Trond Myklebustbeeca192017-11-03 08:00:14 -04006432 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006433 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006434retry:
6435 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006436 if (nfs4_ol_stateid_unhashed(open_stp))
6437 goto out_close;
6438 retstp = find_lock_stateid(lo, open_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006439 if (retstp)
Trond Myklebusta451b122020-03-01 18:21:38 -05006440 goto out_found;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006441 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006442 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006443 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006444 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006445 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006446 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006448 stp->st_openstp = open_stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006449 spin_lock(&fp->fi_lock);
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006450 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006451 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006452 list_add(&stp->st_perfile, &fp->fi_stateids);
6453 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006454 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006455 return stp;
Trond Myklebusta451b122020-03-01 18:21:38 -05006456out_found:
6457 spin_unlock(&clp->cl_lock);
6458 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6459 nfs4_put_stid(&retstp->st_stid);
6460 goto retry;
6461 }
6462 /* To keep mutex tracking happy */
6463 mutex_unlock(&stp->st_mutex);
6464 return retstp;
6465out_close:
6466 spin_unlock(&clp->cl_lock);
6467 mutex_unlock(&stp->st_mutex);
6468 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469}
6470
Jeff Laytonc53530d2014-06-30 11:48:39 -04006471static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006472find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6473 struct inode *inode, struct nfs4_ol_stateid *ost,
6474 bool *new)
6475{
6476 struct nfs4_stid *ns = NULL;
6477 struct nfs4_ol_stateid *lst;
6478 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6479 struct nfs4_client *clp = oo->oo_owner.so_client;
6480
Trond Myklebustbeeca192017-11-03 08:00:14 -04006481 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006482 spin_lock(&clp->cl_lock);
Trond Myklebusta451b122020-03-01 18:21:38 -05006483 lst = find_lock_stateid(lo, ost);
Jeff Layton356a95e2014-07-29 21:34:13 -04006484 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006485 if (lst != NULL) {
6486 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6487 goto out;
6488 nfs4_put_stid(&lst->st_stid);
6489 }
6490 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6491 if (ns == NULL)
6492 return NULL;
6493
6494 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6495 if (lst == openlockstateid(ns))
6496 *new = true;
6497 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006498 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006499out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006500 return lst;
6501}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006502
NeilBrownfd39ca92005-06-23 22:04:03 -07006503static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504check_lock_length(u64 offset, u64 length)
6505{
Kinglong Meee7969312015-07-13 17:34:19 +08006506 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6507 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508}
6509
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006510static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006511{
Trond Myklebust11b91642014-07-29 21:34:08 -04006512 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006513
Jeff Layton7214e862014-07-10 14:07:33 -04006514 lockdep_assert_held(&fp->fi_lock);
6515
Jeff Layton82c5ff12012-05-11 09:45:13 -04006516 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006517 return;
Jeff Layton12659652014-07-10 14:07:28 -04006518 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006519 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006520}
6521
Jeff Layton356a95e2014-07-29 21:34:13 -04006522static __be32
6523lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6524 struct nfs4_ol_stateid *ost,
6525 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006526 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006527{
Jeff Layton5db1c032014-07-29 21:34:28 -04006528 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006529 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006530 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6531 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006532 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006533 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006534 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006535 unsigned int strhashval;
6536
Kinglong Meec8623992015-07-13 17:35:37 +08006537 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006538 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006539 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006540 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6541 if (lo == NULL)
6542 return nfserr_jukebox;
6543 } else {
6544 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006545 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006546 if (!cstate->minorversion &&
6547 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006548 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006549 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006550
Jeff Laytondd257932016-08-11 10:37:39 -04006551 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6552 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006553 status = nfserr_jukebox;
6554 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006555 }
Jeff Laytondd257932016-08-11 10:37:39 -04006556
Jeff Layton5db1c032014-07-29 21:34:28 -04006557 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006558 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006559out:
6560 nfs4_put_stateowner(&lo->lo_owner);
6561 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006562}
6563
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564/*
6565 * LOCK operation
6566 */
Al Virob37ad282006-10-19 23:28:59 -07006567__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006568nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006569 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006571 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006572 struct nfs4_openowner *open_sop = NULL;
6573 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006574 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006575 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006576 struct nfs4_file *fp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006577 struct nfsd_file *nf = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006578 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006579 struct file_lock *file_lock = NULL;
6580 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006581 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006582 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006583 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006584 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006585 unsigned char fl_type;
6586 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006587 struct net *net = SVC_NET(rqstp);
6588 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589
6590 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6591 (long long) lock->lk_offset,
6592 (long long) lock->lk_length);
6593
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 if (check_lock_length(lock->lk_offset, lock->lk_length))
6595 return nfserr_inval;
6596
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006597 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006598 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006599 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6600 return status;
6601 }
6602
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006604 if (nfsd4_has_session(cstate))
6605 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006606 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006607 &cstate->session->se_client->cl_clientid,
6608 sizeof(clientid_t));
6609
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006611 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006615 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 lock->lk_new_open_seqid,
6617 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006618 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006619 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006621 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006622 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006623 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006624 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006625 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006626 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006627 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006628 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006629 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006630 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006631 lock->lk_old_lock_seqid,
6632 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006633 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006634 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006635 if (status)
6636 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006637 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006639 lkflg = setlkflg(lock->lk_type);
6640 status = nfs4_check_openmode(lock_stp, lkflg);
6641 if (status)
6642 goto out;
6643
NeilBrown0dd395d2005-07-07 17:59:15 -07006644 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006645 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006646 goto out;
6647 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006648 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006649 goto out;
6650
Trond Myklebust11b91642014-07-29 21:34:08 -04006651 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006654 if (nfsd4_has_session(cstate))
6655 fl_flags |= FL_SLEEP;
6656 /* Fallthrough */
6657 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006658 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006659 nf = find_readable_file_locked(fp);
6660 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006661 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006662 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006663 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006664 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006666 if (nfsd4_has_session(cstate))
6667 fl_flags |= FL_SLEEP;
6668 /* Fallthrough */
6669 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006670 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006671 nf = find_writeable_file_locked(fp);
6672 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006673 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006674 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006675 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006676 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 default:
6678 status = nfserr_inval;
6679 goto out;
6680 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006681
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006682 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006683 status = nfserr_openmode;
6684 goto out;
6685 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006686
Jeff Layton76d348f2016-09-16 16:28:24 -04006687 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6688 if (!nbl) {
6689 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6690 status = nfserr_jukebox;
6691 goto out;
6692 }
6693
6694 file_lock = &nbl->nbl_lock;
6695 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006696 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006697 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006698 file_lock->fl_file = nf->nf_file;
Jeff Layton76d348f2016-09-16 16:28:24 -04006699 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006700 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6701 file_lock->fl_start = lock->lk_offset;
6702 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6703 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704
Jeff Layton21179d82012-08-21 08:03:32 -04006705 conflock = locks_alloc_lock();
6706 if (!conflock) {
6707 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6708 status = nfserr_jukebox;
6709 goto out;
6710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711
Jeff Layton76d348f2016-09-16 16:28:24 -04006712 if (fl_flags & FL_SLEEP) {
Arnd Bergmann20b7d862019-11-04 16:31:52 +01006713 nbl->nbl_time = ktime_get_boottime_seconds();
Jeff Layton0cc11a62016-10-20 09:34:31 -04006714 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006715 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006716 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006717 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006718 }
6719
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006720 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006721 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006723 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006724 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006725 if (lock->lk_reclaim)
6726 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006727 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006728 case FILE_LOCK_DEFERRED:
6729 nbl = NULL;
6730 /* Fallthrough */
6731 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006732 status = nfserr_denied;
6733 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006734 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006735 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006736 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006738 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006739 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006740 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006741 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006742 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006745 if (nbl) {
6746 /* dequeue it if we queued it before */
6747 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006748 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006749 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006750 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006751 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006752 }
6753 free_blocked_lock(nbl);
6754 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006755 if (nf)
6756 nfsd_file_put(nf);
Jeff Layton5db1c032014-07-29 21:34:28 -04006757 if (lock_stp) {
6758 /* Bump seqid manually if the 4.0 replay owner is openowner */
6759 if (cstate->replay_owner &&
6760 cstate->replay_owner != &lock_sop->lo_owner &&
6761 seqid_mutating_err(ntohl(status)))
6762 lock_sop->lo_owner.so_seqid++;
6763
6764 /*
6765 * If this is a new, never-before-used stateid, and we are
6766 * returning an error, then just go ahead and release it.
6767 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006768 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006769 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006770
6771 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006772
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006773 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006774 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006775 if (open_stp)
6776 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006777 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006778 if (conflock)
6779 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780 return status;
6781}
6782
6783/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006784 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6785 * so we do a temporary open here just to get an open file to pass to
6786 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6787 * inode operation.)
6788 */
Al Viro04da6e92012-04-13 00:00:04 -04006789static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006790{
Jeff Layton6b556ca2019-08-18 14:18:55 -04006791 struct nfsd_file *nf;
6792 __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
Al Viro04da6e92012-04-13 00:00:04 -04006793 if (!err) {
Jeff Layton6b556ca2019-08-18 14:18:55 -04006794 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6795 nfsd_file_put(nf);
Al Viro04da6e92012-04-13 00:00:04 -04006796 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006797 return err;
6798}
6799
6800/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 * LOCKT operation
6802 */
Al Virob37ad282006-10-19 23:28:59 -07006803__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006804nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006805 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006807 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006808 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006809 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006810 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006811 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006813 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814 return nfserr_grace;
6815
6816 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6817 return nfserr_inval;
6818
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006819 if (!nfsd4_has_session(cstate)) {
Olga Kornievskaiab7342202019-09-06 15:17:21 -04006820 status = lookup_clientid(&lockt->lt_clientid, cstate, nn,
6821 false);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006822 if (status)
6823 goto out;
6824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006826 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828
Jeff Layton21179d82012-08-21 08:03:32 -04006829 file_lock = locks_alloc_lock();
6830 if (!file_lock) {
6831 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6832 status = nfserr_jukebox;
6833 goto out;
6834 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006835
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 switch (lockt->lt_type) {
6837 case NFS4_READ_LT:
6838 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006839 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006840 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 case NFS4_WRITE_LT:
6842 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006843 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006844 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006846 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006848 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 }
6850
Kinglong Meec8623992015-07-13 17:35:37 +08006851 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006852 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006853 file_lock->fl_owner = (fl_owner_t)lo;
6854 file_lock->fl_pid = current->tgid;
6855 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856
Jeff Layton21179d82012-08-21 08:03:32 -04006857 file_lock->fl_start = lockt->lt_offset;
6858 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859
Jeff Layton21179d82012-08-21 08:03:32 -04006860 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861
Jeff Layton21179d82012-08-21 08:03:32 -04006862 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006863 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006864 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006865
Jeff Layton21179d82012-08-21 08:03:32 -04006866 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006868 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 }
6870out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006871 if (lo)
6872 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006873 if (file_lock)
6874 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 return status;
6876}
6877
Al Virob37ad282006-10-19 23:28:59 -07006878__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006879nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006880 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006882 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006883 struct nfs4_ol_stateid *stp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006884 struct nfsd_file *nf = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006885 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006886 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006887 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006888 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6889
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6891 (long long) locku->lu_offset,
6892 (long long) locku->lu_length);
6893
6894 if (check_lock_length(locku->lu_offset, locku->lu_length))
6895 return nfserr_inval;
6896
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006897 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006898 &locku->lu_stateid, NFS4_LOCK_STID,
6899 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006900 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 goto out;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006902 nf = find_any_file(stp->st_stid.sc_file);
6903 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006904 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006905 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006906 }
Jeff Layton21179d82012-08-21 08:03:32 -04006907 file_lock = locks_alloc_lock();
6908 if (!file_lock) {
6909 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6910 status = nfserr_jukebox;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006911 goto put_file;
Jeff Layton21179d82012-08-21 08:03:32 -04006912 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006913
Jeff Layton21179d82012-08-21 08:03:32 -04006914 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006915 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006916 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006917 file_lock->fl_file = nf->nf_file;
Jeff Layton21179d82012-08-21 08:03:32 -04006918 file_lock->fl_flags = FL_POSIX;
6919 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6920 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921
Jeff Layton21179d82012-08-21 08:03:32 -04006922 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6923 locku->lu_length);
6924 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006926 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006927 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006928 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 goto out_nfserr;
6930 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006931 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006932put_file:
6933 nfsd_file_put(nf);
Trond Myklebust858cc572014-07-29 21:34:16 -04006934put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006935 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006936 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006938 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006939 if (file_lock)
6940 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 return status;
6942
6943out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006944 status = nfserrno(err);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006945 goto put_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946}
6947
6948/*
6949 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006950 * true: locks held by lockowner
6951 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006953static bool
6954check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006956 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006957 int status = false;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006958 struct nfsd_file *nf = find_any_file(fp);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006959 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006960 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006961
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006962 if (!nf) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006963 /* Any valid lock stateid should have some sort of access */
6964 WARN_ON_ONCE(1);
6965 return status;
6966 }
6967
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006968 inode = locks_inode(nf->nf_file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006969 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006971 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006972 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006973 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6974 if (fl->fl_owner == (fl_owner_t)lowner) {
6975 status = true;
6976 break;
6977 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006978 }
Jeff Layton6109c852015-01-16 15:05:57 -05006979 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006981 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 return status;
6983}
6984
Al Virob37ad282006-10-19 23:28:59 -07006985__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006986nfsd4_release_lockowner(struct svc_rqst *rqstp,
6987 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006988 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006990 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006992 struct nfs4_stateowner *sop;
6993 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006994 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006996 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006997 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006998 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006999 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04007000 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001
7002 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7003 clid->cl_boot, clid->cl_id);
7004
Olga Kornievskaiab7342202019-09-06 15:17:21 -04007005 status = lookup_clientid(clid, cstate, nn, false);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007006 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04007007 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05007008
Trond Myklebustd4f04892014-07-29 21:34:36 -04007009 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04007010 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04007011 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04007012 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04007013 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04007014
7015 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05007016 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04007017
Jeff Layton882e9d22014-07-29 21:34:37 -04007018 /* see if there are still any locks associated with it */
7019 lo = lockowner(sop);
7020 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
7021 if (check_for_locks(stp->st_stid.sc_file, lo)) {
7022 status = nfserr_locks_held;
7023 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04007024 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04007025 }
Jeff Layton5adfd882014-07-29 21:34:31 -04007026 }
Jeff Layton882e9d22014-07-29 21:34:37 -04007027
Kinglong Meeb5971af2014-08-22 10:18:43 -04007028 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04007029 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04007030 }
Chuck Lever88584812016-07-13 16:40:14 -04007031 if (!lo) {
7032 spin_unlock(&clp->cl_lock);
7033 return status;
7034 }
7035
7036 unhash_lockowner_locked(lo);
7037 while (!list_empty(&lo->lo_owner.so_stateids)) {
7038 stp = list_first_entry(&lo->lo_owner.so_stateids,
7039 struct nfs4_ol_stateid,
7040 st_perstateowner);
7041 WARN_ON(!unhash_lock_stateid(stp));
7042 put_ol_stateid_locked(stp, &reaplist);
7043 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04007044 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04007045 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007046 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04007047 nfs4_put_stateowner(&lo->lo_owner);
7048
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 return status;
7050}
7051
7052static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07007053alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054{
NeilBrowna55370a2005-06-23 22:03:52 -07007055 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056}
7057
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007058bool
Scott Mayhew6b189102019-03-26 18:06:26 -04007059nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07007060{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007061 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07007062
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007063 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007064 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07007065}
7066
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067/*
7068 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04007069 *
7070 * The caller is responsible for freeing name.data if NULL is returned (it
7071 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05007073struct nfs4_client_reclaim *
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007074nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7075 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076{
7077 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007078 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079
Scott Mayhew6b189102019-03-26 18:06:26 -04007080 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data);
NeilBrowna55370a2005-06-23 22:03:52 -07007081 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05007082 if (crp) {
7083 strhashval = clientstr_hashval(name);
7084 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007085 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04007086 crp->cr_name.data = name.data;
7087 crp->cr_name.len = name.len;
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007088 crp->cr_princhash.data = princhash.data;
7089 crp->cr_princhash.len = princhash.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05007090 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007091 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05007092 }
7093 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094}
7095
Jeff Layton2a4317c2012-03-21 16:42:43 -04007096void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007097nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05007098{
7099 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04007100 kfree(crp->cr_name.data);
Scott Mayhew6ee95d12019-09-09 16:10:31 -04007101 kfree(crp->cr_princhash.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05007102 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007103 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05007104}
7105
7106void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007107nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108{
7109 struct nfs4_client_reclaim *crp = NULL;
7110 int i;
7111
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007113 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7114 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007116 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117 }
7118 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05007119 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120}
7121
7122/*
7123 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04007124struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04007125nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126{
7127 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 struct nfs4_client_reclaim *crp = NULL;
7129
Scott Mayhew6b189102019-03-26 18:06:26 -04007130 dprintk("NFSD: nfs4_find_reclaim_client for name %.*s\n", name.len, name.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131
Scott Mayhew6b189102019-03-26 18:06:26 -04007132 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03007133 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04007134 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 return crp;
7136 }
7137 }
7138 return NULL;
7139}
7140
7141/*
7142* Called from OPEN. Look for clientid in reclaim list.
7143*/
Al Virob37ad282006-10-19 23:28:59 -07007144__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007145nfs4_check_open_reclaim(clientid_t *clid,
7146 struct nfsd4_compound_state *cstate,
7147 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007149 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04007150
7151 /* find clientid in conf_id_hashtbl */
Olga Kornievskaiab7342202019-09-06 15:17:21 -04007152 status = lookup_clientid(clid, cstate, nn, false);
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007153 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04007154 return nfserr_reclaim_bad;
7155
Jeff Layton3b3e7b72014-09-12 16:40:22 -04007156 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
7157 return nfserr_no_grace;
7158
Trond Myklebust0fe492d2014-06-30 11:48:47 -04007159 if (nfsd4_client_record_check(cstate->clp))
7160 return nfserr_reclaim_bad;
7161
7162 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163}
7164
Bryan Schumaker65178db2011-11-01 13:35:21 -04007165#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04007166static inline void
7167put_client(struct nfs4_client *clp)
7168{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007169 atomic_dec(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04007170}
7171
Jeff Layton285abde2014-07-30 08:27:24 -04007172static struct nfs4_client *
7173nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
7174{
7175 struct nfs4_client *clp;
7176 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7177 nfsd_net_id);
7178
7179 if (!nfsd_netns_ready(nn))
7180 return NULL;
7181
7182 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7183 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
7184 return clp;
7185 }
7186 return NULL;
7187}
7188
Jeff Layton7ec0e362014-07-30 08:27:17 -04007189u64
Jeff Layton285abde2014-07-30 08:27:24 -04007190nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04007191{
7192 struct nfs4_client *clp;
7193 u64 count = 0;
7194 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7195 nfsd_net_id);
7196 char buf[INET6_ADDRSTRLEN];
7197
7198 if (!nfsd_netns_ready(nn))
7199 return 0;
7200
7201 spin_lock(&nn->client_lock);
7202 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7203 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7204 pr_info("NFS Client: %s\n", buf);
7205 ++count;
7206 }
7207 spin_unlock(&nn->client_lock);
7208
7209 return count;
7210}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007211
Jeff Laytona0926d12014-07-30 08:27:18 -04007212u64
Jeff Layton285abde2014-07-30 08:27:24 -04007213nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04007214{
7215 u64 count = 0;
7216 struct nfs4_client *clp;
7217 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7218 nfsd_net_id);
7219
7220 if (!nfsd_netns_ready(nn))
7221 return count;
7222
7223 spin_lock(&nn->client_lock);
7224 clp = nfsd_find_client(addr, addr_size);
7225 if (clp) {
7226 if (mark_client_expired_locked(clp) == nfs_ok)
7227 ++count;
7228 else
7229 clp = NULL;
7230 }
7231 spin_unlock(&nn->client_lock);
7232
7233 if (clp)
7234 expire_client(clp);
7235
7236 return count;
7237}
7238
Jeff Layton69fc9ed2014-07-30 08:27:19 -04007239u64
Jeff Layton285abde2014-07-30 08:27:24 -04007240nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04007241{
7242 u64 count = 0;
7243 struct nfs4_client *clp, *next;
7244 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7245 nfsd_net_id);
7246 LIST_HEAD(reaplist);
7247
7248 if (!nfsd_netns_ready(nn))
7249 return count;
7250
7251 spin_lock(&nn->client_lock);
7252 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7253 if (mark_client_expired_locked(clp) == nfs_ok) {
7254 list_add(&clp->cl_lru, &reaplist);
7255 if (max != 0 && ++count >= max)
7256 break;
7257 }
7258 }
7259 spin_unlock(&nn->client_lock);
7260
7261 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
7262 expire_client(clp);
7263
7264 return count;
7265}
7266
Bryan Schumaker184c1842012-11-29 11:40:44 -05007267static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
7268 const char *type)
7269{
7270 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05007271 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05007272 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
7273}
7274
Jeff Layton016200c2014-07-30 08:27:21 -04007275static void
7276nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
7277 struct list_head *collect)
7278{
7279 struct nfs4_client *clp = lst->st_stid.sc_client;
7280 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7281 nfsd_net_id);
7282
7283 if (!collect)
7284 return;
7285
7286 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007287 atomic_inc(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04007288 list_add(&lst->st_locks, collect);
7289}
7290
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007291static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04007292 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04007293 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05007294{
7295 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007296 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007297 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007298 u64 count = 0;
7299
Jeff Layton016200c2014-07-30 08:27:21 -04007300 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007301 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007302 list_for_each_entry_safe(stp, st_next,
7303 &oop->oo_owner.so_stateids, st_perstateowner) {
7304 list_for_each_entry_safe(lst, lst_next,
7305 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04007306 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04007307 if (func(lst))
7308 nfsd_inject_add_lock_to_list(lst,
7309 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04007310 }
Jeff Layton016200c2014-07-30 08:27:21 -04007311 ++count;
7312 /*
7313 * Despite the fact that these functions deal
7314 * with 64-bit integers for "count", we must
7315 * ensure that it doesn't blow up the
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007316 * clp->cl_rpc_users. Throw a warning if we
Jeff Layton016200c2014-07-30 08:27:21 -04007317 * start to approach INT_MAX here.
7318 */
7319 WARN_ON_ONCE(count == (INT_MAX / 2));
7320 if (count == max)
7321 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007322 }
7323 }
7324 }
Jeff Layton016200c2014-07-30 08:27:21 -04007325out:
7326 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007327
7328 return count;
7329}
7330
Jeff Layton016200c2014-07-30 08:27:21 -04007331static u64
7332nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
7333 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05007334{
Jeff Layton016200c2014-07-30 08:27:21 -04007335 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007336}
7337
Jeff Layton016200c2014-07-30 08:27:21 -04007338static u64
7339nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007340{
Jeff Layton016200c2014-07-30 08:27:21 -04007341 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007342 nfsd_print_count(clp, count, "locked files");
7343 return count;
7344}
7345
Jeff Layton016200c2014-07-30 08:27:21 -04007346u64
Jeff Layton285abde2014-07-30 08:27:24 -04007347nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04007348{
7349 struct nfs4_client *clp;
7350 u64 count = 0;
7351 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7352 nfsd_net_id);
7353
7354 if (!nfsd_netns_ready(nn))
7355 return 0;
7356
7357 spin_lock(&nn->client_lock);
7358 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7359 count += nfsd_print_client_locks(clp);
7360 spin_unlock(&nn->client_lock);
7361
7362 return count;
7363}
7364
7365static void
7366nfsd_reap_locks(struct list_head *reaplist)
7367{
7368 struct nfs4_client *clp;
7369 struct nfs4_ol_stateid *stp, *next;
7370
7371 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
7372 list_del_init(&stp->st_locks);
7373 clp = stp->st_stid.sc_client;
7374 nfs4_put_stid(&stp->st_stid);
7375 put_client(clp);
7376 }
7377}
7378
7379u64
Jeff Layton285abde2014-07-30 08:27:24 -04007380nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04007381{
7382 unsigned int count = 0;
7383 struct nfs4_client *clp;
7384 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7385 nfsd_net_id);
7386 LIST_HEAD(reaplist);
7387
7388 if (!nfsd_netns_ready(nn))
7389 return count;
7390
7391 spin_lock(&nn->client_lock);
7392 clp = nfsd_find_client(addr, addr_size);
7393 if (clp)
7394 count = nfsd_collect_client_locks(clp, &reaplist, 0);
7395 spin_unlock(&nn->client_lock);
7396 nfsd_reap_locks(&reaplist);
7397 return count;
7398}
7399
7400u64
Jeff Layton285abde2014-07-30 08:27:24 -04007401nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04007402{
7403 u64 count = 0;
7404 struct nfs4_client *clp;
7405 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7406 nfsd_net_id);
7407 LIST_HEAD(reaplist);
7408
7409 if (!nfsd_netns_ready(nn))
7410 return count;
7411
7412 spin_lock(&nn->client_lock);
7413 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7414 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
7415 if (max != 0 && count >= max)
7416 break;
7417 }
7418 spin_unlock(&nn->client_lock);
7419 nfsd_reap_locks(&reaplist);
7420 return count;
7421}
7422
Jeff Layton82e05ef2014-07-30 08:27:22 -04007423static u64
7424nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
7425 struct list_head *collect,
7426 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007427{
7428 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04007429 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7430 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007431 u64 count = 0;
7432
Jeff Layton82e05ef2014-07-30 08:27:22 -04007433 lockdep_assert_held(&nn->client_lock);
7434
7435 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007436 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04007437 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007438 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007439 if (collect) {
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007440 atomic_inc(&clp->cl_rpc_users);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007441 list_add(&oop->oo_perclient, collect);
7442 }
7443 }
7444 ++count;
7445 /*
7446 * Despite the fact that these functions deal with
7447 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007448 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton82e05ef2014-07-30 08:27:22 -04007449 * warning if we start to approach INT_MAX here.
7450 */
7451 WARN_ON_ONCE(count == (INT_MAX / 2));
7452 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007453 break;
7454 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04007455 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007456
7457 return count;
7458}
7459
Jeff Layton82e05ef2014-07-30 08:27:22 -04007460static u64
7461nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007462{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007463 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
7464
7465 nfsd_print_count(clp, count, "openowners");
7466 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007467}
7468
Jeff Layton82e05ef2014-07-30 08:27:22 -04007469static u64
7470nfsd_collect_client_openowners(struct nfs4_client *clp,
7471 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007472{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007473 return nfsd_foreach_client_openowner(clp, max, collect,
7474 unhash_openowner_locked);
7475}
7476
7477u64
Jeff Layton285abde2014-07-30 08:27:24 -04007478nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007479{
7480 struct nfs4_client *clp;
7481 u64 count = 0;
7482 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7483 nfsd_net_id);
7484
7485 if (!nfsd_netns_ready(nn))
7486 return 0;
7487
7488 spin_lock(&nn->client_lock);
7489 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7490 count += nfsd_print_client_openowners(clp);
7491 spin_unlock(&nn->client_lock);
7492
7493 return count;
7494}
7495
7496static void
7497nfsd_reap_openowners(struct list_head *reaplist)
7498{
7499 struct nfs4_client *clp;
7500 struct nfs4_openowner *oop, *next;
7501
7502 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
7503 list_del_init(&oop->oo_perclient);
7504 clp = oop->oo_owner.so_client;
7505 release_openowner(oop);
7506 put_client(clp);
7507 }
7508}
7509
7510u64
Jeff Layton285abde2014-07-30 08:27:24 -04007511nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
7512 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007513{
7514 unsigned int count = 0;
7515 struct nfs4_client *clp;
7516 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7517 nfsd_net_id);
7518 LIST_HEAD(reaplist);
7519
7520 if (!nfsd_netns_ready(nn))
7521 return count;
7522
7523 spin_lock(&nn->client_lock);
7524 clp = nfsd_find_client(addr, addr_size);
7525 if (clp)
7526 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
7527 spin_unlock(&nn->client_lock);
7528 nfsd_reap_openowners(&reaplist);
7529 return count;
7530}
7531
7532u64
Jeff Layton285abde2014-07-30 08:27:24 -04007533nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007534{
7535 u64 count = 0;
7536 struct nfs4_client *clp;
7537 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7538 nfsd_net_id);
7539 LIST_HEAD(reaplist);
7540
7541 if (!nfsd_netns_ready(nn))
7542 return count;
7543
7544 spin_lock(&nn->client_lock);
7545 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7546 count += nfsd_collect_client_openowners(clp, &reaplist,
7547 max - count);
7548 if (max != 0 && count >= max)
7549 break;
7550 }
7551 spin_unlock(&nn->client_lock);
7552 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007553 return count;
7554}
7555
Bryan Schumaker269de302012-11-29 11:40:42 -05007556static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
7557 struct list_head *victims)
7558{
7559 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007560 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7561 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05007562 u64 count = 0;
7563
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007564 lockdep_assert_held(&nn->client_lock);
7565
7566 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007567 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04007568 if (victims) {
7569 /*
7570 * It's not safe to mess with delegations that have a
7571 * non-zero dl_time. They might have already been broken
7572 * and could be processed by the laundromat outside of
7573 * the state_lock. Just leave them be.
7574 */
7575 if (dp->dl_time != 0)
7576 continue;
7577
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007578 atomic_inc(&clp->cl_rpc_users);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007579 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007580 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04007581 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007582 ++count;
7583 /*
7584 * Despite the fact that these functions deal with
7585 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007586 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007587 * warning if we start to approach INT_MAX here.
7588 */
7589 WARN_ON_ONCE(count == (INT_MAX / 2));
7590 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05007591 break;
7592 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007593 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007594 return count;
7595}
7596
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007597static u64
7598nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05007599{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007600 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007601
7602 nfsd_print_count(clp, count, "delegations");
7603 return count;
7604}
7605
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007606u64
Jeff Layton285abde2014-07-30 08:27:24 -04007607nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007608{
7609 struct nfs4_client *clp;
7610 u64 count = 0;
7611 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7612 nfsd_net_id);
7613
7614 if (!nfsd_netns_ready(nn))
7615 return 0;
7616
7617 spin_lock(&nn->client_lock);
7618 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7619 count += nfsd_print_client_delegations(clp);
7620 spin_unlock(&nn->client_lock);
7621
7622 return count;
7623}
7624
7625static void
7626nfsd_forget_delegations(struct list_head *reaplist)
7627{
7628 struct nfs4_client *clp;
7629 struct nfs4_delegation *dp, *next;
7630
7631 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7632 list_del_init(&dp->dl_recall_lru);
7633 clp = dp->dl_stid.sc_client;
7634 revoke_delegation(dp);
7635 put_client(clp);
7636 }
7637}
7638
7639u64
Jeff Layton285abde2014-07-30 08:27:24 -04007640nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7641 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007642{
7643 u64 count = 0;
7644 struct nfs4_client *clp;
7645 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7646 nfsd_net_id);
7647 LIST_HEAD(reaplist);
7648
7649 if (!nfsd_netns_ready(nn))
7650 return count;
7651
7652 spin_lock(&nn->client_lock);
7653 clp = nfsd_find_client(addr, addr_size);
7654 if (clp)
7655 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7656 spin_unlock(&nn->client_lock);
7657
7658 nfsd_forget_delegations(&reaplist);
7659 return count;
7660}
7661
7662u64
Jeff Layton285abde2014-07-30 08:27:24 -04007663nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007664{
7665 u64 count = 0;
7666 struct nfs4_client *clp;
7667 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7668 nfsd_net_id);
7669 LIST_HEAD(reaplist);
7670
7671 if (!nfsd_netns_ready(nn))
7672 return count;
7673
7674 spin_lock(&nn->client_lock);
7675 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7676 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7677 if (max != 0 && count >= max)
7678 break;
7679 }
7680 spin_unlock(&nn->client_lock);
7681 nfsd_forget_delegations(&reaplist);
7682 return count;
7683}
7684
7685static void
7686nfsd_recall_delegations(struct list_head *reaplist)
7687{
7688 struct nfs4_client *clp;
7689 struct nfs4_delegation *dp, *next;
7690
7691 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7692 list_del_init(&dp->dl_recall_lru);
7693 clp = dp->dl_stid.sc_client;
7694 /*
7695 * We skipped all entries that had a zero dl_time before,
7696 * so we can now reset the dl_time back to 0. If a delegation
7697 * break comes in now, then it won't make any difference since
7698 * we're recalling it either way.
7699 */
7700 spin_lock(&state_lock);
7701 dp->dl_time = 0;
7702 spin_unlock(&state_lock);
7703 nfsd_break_one_deleg(dp);
7704 put_client(clp);
7705 }
7706}
7707
7708u64
Jeff Layton285abde2014-07-30 08:27:24 -04007709nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007710 size_t addr_size)
7711{
7712 u64 count = 0;
7713 struct nfs4_client *clp;
7714 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7715 nfsd_net_id);
7716 LIST_HEAD(reaplist);
7717
7718 if (!nfsd_netns_ready(nn))
7719 return count;
7720
7721 spin_lock(&nn->client_lock);
7722 clp = nfsd_find_client(addr, addr_size);
7723 if (clp)
7724 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7725 spin_unlock(&nn->client_lock);
7726
7727 nfsd_recall_delegations(&reaplist);
7728 return count;
7729}
7730
7731u64
Jeff Layton285abde2014-07-30 08:27:24 -04007732nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007733{
7734 u64 count = 0;
7735 struct nfs4_client *clp, *next;
7736 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7737 nfsd_net_id);
7738 LIST_HEAD(reaplist);
7739
7740 if (!nfsd_netns_ready(nn))
7741 return count;
7742
7743 spin_lock(&nn->client_lock);
7744 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7745 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7746 if (max != 0 && ++count >= max)
7747 break;
7748 }
7749 spin_unlock(&nn->client_lock);
7750 nfsd_recall_delegations(&reaplist);
7751 return count;
7752}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007753#endif /* CONFIG_NFSD_FAULT_INJECTION */
7754
Meelap Shahc2f1a552007-07-17 04:04:39 -07007755/*
7756 * Since the lifetime of a delegation isn't limited to that of an open, a
7757 * client may quite reasonably hang on to a delegation as long as it has
7758 * the inode cached. This becomes an obvious problem the first time a
7759 * client's inode cache approaches the size of the server's total memory.
7760 *
7761 * For now we avoid this problem by imposing a hard limit on the number
7762 * of delegations, which varies according to the server's memory size.
7763 */
7764static void
7765set_max_delegations(void)
7766{
7767 /*
7768 * Allow at most 4 delegations per megabyte of RAM. Quick
7769 * estimates suggest that in the worst case (where every delegation
7770 * is for a different inode), a delegation could take about 1.5K,
7771 * giving a worst case usage of about 6% of memory.
7772 */
7773 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7774}
7775
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007776static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007777{
7778 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7779 int i;
7780
Kees Cook6da2ec52018-06-12 13:55:00 -07007781 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7782 sizeof(struct list_head),
7783 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007784 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007785 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007786 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7787 sizeof(struct list_head),
7788 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007789 if (!nn->unconf_id_hashtbl)
7790 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007791 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7792 sizeof(struct list_head),
7793 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007794 if (!nn->sessionid_hashtbl)
7795 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007796
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007797 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007798 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007799 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007800 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007801 for (i = 0; i < SESSION_HASH_SIZE; i++)
7802 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007803 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007804 nn->unconf_name_tree = RB_ROOT;
Arnd Bergmann9cc76802019-10-31 15:53:13 +01007805 nn->boot_time = ktime_get_real_seconds();
Vasily Averin81833de2017-11-13 07:25:40 +03007806 nn->grace_ended = false;
7807 nn->nfsd4_manager.block_opens = true;
7808 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007809 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007810 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007811 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007812 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007813 spin_lock_init(&nn->s2s_cp_lock);
7814 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007815
Jeff Layton0cc11a62016-10-20 09:34:31 -04007816 spin_lock_init(&nn->blocked_locks_lock);
7817 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7818
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007819 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007820 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007821
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007822 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007823
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007824err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007825 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007826err_unconf_id:
7827 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007828err:
7829 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007830}
7831
7832static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007833nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007834{
7835 int i;
7836 struct nfs4_client *clp = NULL;
7837 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7838
7839 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7840 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7841 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7842 destroy_client(clp);
7843 }
7844 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007845
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007846 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7847
Kinglong Mee2b905632014-03-26 22:09:30 +08007848 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7849 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7850 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7851 destroy_client(clp);
7852 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007853 }
7854
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007855 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007856 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007857 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007858 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007859}
7860
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007861int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007862nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007863{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007864 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007865 int ret;
7866
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007867 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007868 if (ret)
7869 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007870 locks_start_grace(net, &nn->nfsd4_manager);
7871 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007872 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7873 goto skip_grace;
Arnd Bergmann20b7d862019-11-04 16:31:52 +01007874 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
Vasily Averin7e981a82017-11-06 16:46:04 +03007875 nn->nfsd4_grace, net->ns.inum);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007876 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007877 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007878
7879skip_grace:
7880 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7881 net->ns.inum);
7882 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7883 nfsd4_end_grace(nn);
7884 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007885}
7886
7887/* initialization to perform when the nfsd service is started: */
7888
7889int
7890nfs4_state_start(void)
7891{
7892 int ret;
7893
Jeff Layton51a54562015-08-20 07:17:01 -04007894 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007895 if (laundry_wq == NULL) {
7896 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007897 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007898 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007899 ret = nfsd4_create_callback_queue();
7900 if (ret)
7901 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007902
Meelap Shahc2f1a552007-07-17 04:04:39 -07007903 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007904 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007905
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007906out_free_laundry:
7907 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007908out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007909 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910}
7911
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007912void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007913nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007917 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007918
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007919 cancel_delayed_work_sync(&nn->laundromat_work);
7920 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007921
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007923 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007924 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007926 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007927 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928 }
Benny Halevycdc975052014-05-30 09:09:30 -04007929 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930 list_for_each_safe(pos, next, &reaplist) {
7931 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007932 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007933 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934 }
7935
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007936 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007937 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938}
7939
7940void
7941nfs4_state_shutdown(void)
7942{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007943 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007944 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007946
7947static void
7948get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7949{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007950 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7951 CURRENT_STATEID(stateid))
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007952 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007953}
7954
7955static void
7956put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7957{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007958 if (cstate->minorversion) {
7959 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007960 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007961 }
7962}
7963
7964void
7965clear_current_stateid(struct nfsd4_compound_state *cstate)
7966{
Olga Kornievskaia51100d22018-09-13 13:58:24 -04007967 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007968}
7969
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007970/*
7971 * functions to set current state id
7972 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007973void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007974nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7975 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007976{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007977 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007978}
7979
7980void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007981nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7982 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007983{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007984 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007985}
7986
7987void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007988nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7989 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007990{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007991 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007992}
7993
7994void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007995nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7996 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007997{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007998 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007999}
8000
8001/*
8002 * functions to consume current state id
8003 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008004
8005void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008006nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
8007 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01008008{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008009 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01008010}
8011
8012void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008013nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
8014 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01008015{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008016 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01008017}
8018
8019void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008020nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
8021 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008022{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008023 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008024}
8025
8026void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008027nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
8028 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008029{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008030 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01008031}
8032
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01008033void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008034nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
8035 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008036{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008037 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008038}
8039
8040void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008041nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
8042 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008043{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008044 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01008045}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008046
8047void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008048nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
8049 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008050{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008051 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008052}
8053
8054void
Christoph Hellwig57832e72017-05-08 20:37:33 +02008055nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
8056 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008057{
Christoph Hellwig57832e72017-05-08 20:37:33 +02008058 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01008059}