blob: 7857942c5ca6572b8e7b6c2107bca795e2f6c85c [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"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define NFSDDBG_FACILITY NFSDDBG_PROC
55
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050056#define all_ones {{~0,~0},~0}
57static const stateid_t one_stateid = {
58 .si_generation = ~0,
59 .si_opaque = all_ones,
60};
61static const stateid_t zero_stateid = {
62 /* all fields zero */
63};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010064static const stateid_t currentstateid = {
65 .si_generation = 1,
66};
Trond Myklebustfb500a72017-11-03 08:00:12 -040067static const stateid_t close_stateid = {
68 .si_generation = 0xffffffffU,
69};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050070
Andy Adamsonec6b5d72009-04-03 08:28:28 +030071static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070072
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050073#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
74#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010075#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elbleae254dac2017-11-09 13:41:10 -050076#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040079static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040080static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Scott Mayhew362063a2019-03-26 18:06:28 -040081void nfsd4_end_grace(struct nfsd_net *nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
J. Bruce Fields8b671b82009-02-22 14:51:34 -080083/* Locking: */
84
J. Bruce Fields8b671b82009-02-22 14:51:34 -080085/*
86 * Currently used for the del_recall_lru and file hash table. In an
87 * effort to decrease the scope of the client_mutex, this spinlock may
88 * eventually cover more:
89 */
Benny Halevycdc975052014-05-30 09:09:30 -040090static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080091
Andrew Elble4f34bd02017-11-08 17:29:51 -050092enum nfsd4_st_mutex_lock_subclass {
93 OPEN_STATEID_MUTEX = 0,
94 LOCK_STATEID_MUTEX = 1,
95};
96
Jeff Laytonb401be222014-07-29 21:34:33 -040097/*
98 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
99 * the refcount on the open stateid to drop.
100 */
101static DECLARE_WAIT_QUEUE_HEAD(close_wq);
102
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400103/*
104 * A waitqueue where a writer to clients/#/ctl destroying a client can
105 * wait for cl_rpc_users to drop to 0 and then for the client to be
106 * unhashed.
107 */
108static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
109
Jeff Layton9258a2d2018-03-16 09:47:22 -0400110static struct kmem_cache *client_slab;
Christoph Hellwigabf11352014-05-21 07:43:03 -0700111static struct kmem_cache *openowner_slab;
112static struct kmem_cache *lockowner_slab;
113static struct kmem_cache *file_slab;
114static struct kmem_cache *stateid_slab;
115static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200116static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700117
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400118static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800119
Julia Lawallc4cb8972015-11-21 22:57:39 +0100120static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400121static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200122
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400123static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800124{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400125 return ses->se_flags & NFS4_SESSION_DEAD;
126}
127
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400128static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
129{
130 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400131 return nfserr_jukebox;
132 ses->se_flags |= NFS4_SESSION_DEAD;
133 return nfs_ok;
134}
135
J. Bruce Fields221a6872013-04-01 22:23:49 -0400136static bool is_client_expired(struct nfs4_client *clp)
137{
138 return clp->cl_time == 0;
139}
140
J. Bruce Fields221a6872013-04-01 22:23:49 -0400141static __be32 get_client_locked(struct nfs4_client *clp)
142{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400143 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
144
145 lockdep_assert_held(&nn->client_lock);
146
J. Bruce Fields221a6872013-04-01 22:23:49 -0400147 if (is_client_expired(clp))
148 return nfserr_expired;
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400149 atomic_inc(&clp->cl_rpc_users);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400150 return nfs_ok;
151}
152
153/* must be called under the client_lock */
154static inline void
155renew_client_locked(struct nfs4_client *clp)
156{
157 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
158
159 if (is_client_expired(clp)) {
160 WARN_ON(1);
161 printk("%s: client (clientid %08x/%08x) already expired\n",
162 __func__,
163 clp->cl_clientid.cl_boot,
164 clp->cl_clientid.cl_id);
165 return;
166 }
167
168 dprintk("renewing client (clientid %08x/%08x)\n",
169 clp->cl_clientid.cl_boot,
170 clp->cl_clientid.cl_id);
171 list_move_tail(&clp->cl_lru, &nn->client_lru);
172 clp->cl_time = get_seconds();
173}
174
Fengguang Wuba138432013-04-16 22:14:15 -0400175static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400176{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178
179 lockdep_assert_held(&nn->client_lock);
180
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400181 if (!atomic_dec_and_test(&clp->cl_rpc_users))
J. Bruce Fields221a6872013-04-01 22:23:49 -0400182 return;
183 if (!is_client_expired(clp))
184 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400185 else
186 wake_up_all(&expiry_wq);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400187}
188
Jeff Layton4b24ca72014-06-30 11:48:44 -0400189static void put_client_renew(struct nfs4_client *clp)
190{
191 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
192
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400193 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
Jeff Laytond6c249b2014-07-08 14:02:50 -0400194 return;
195 if (!is_client_expired(clp))
196 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400197 else
198 wake_up_all(&expiry_wq);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400199 spin_unlock(&nn->client_lock);
200}
201
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400202static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
203{
204 __be32 status;
205
206 if (is_session_dead(ses))
207 return nfserr_badsession;
208 status = get_client_locked(ses->se_client);
209 if (status)
210 return status;
211 atomic_inc(&ses->se_ref);
212 return nfs_ok;
213}
214
215static void nfsd4_put_session_locked(struct nfsd4_session *ses)
216{
217 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400218 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
219
220 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400221
222 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
223 free_session(ses);
224 put_client_renew_locked(clp);
225}
226
227static void nfsd4_put_session(struct nfsd4_session *ses)
228{
229 struct nfs4_client *clp = ses->se_client;
230 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
231
232 spin_lock(&nn->client_lock);
233 nfsd4_put_session_locked(ses);
234 spin_unlock(&nn->client_lock);
235}
236
Jeff Layton76d348f2016-09-16 16:28:24 -0400237static struct nfsd4_blocked_lock *
238find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
239 struct nfsd_net *nn)
240{
241 struct nfsd4_blocked_lock *cur, *found = NULL;
242
Jeff Layton0cc11a62016-10-20 09:34:31 -0400243 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400244 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
245 if (fh_match(fh, &cur->nbl_fh)) {
246 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400247 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400248 found = cur;
249 break;
250 }
251 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400252 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400253 if (found)
NeilBrowncb03f942018-11-30 10:04:08 +1100254 locks_delete_block(&found->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400255 return found;
256}
257
258static struct nfsd4_blocked_lock *
259find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
260 struct nfsd_net *nn)
261{
262 struct nfsd4_blocked_lock *nbl;
263
264 nbl = find_blocked_lock(lo, fh, nn);
265 if (!nbl) {
266 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
267 if (nbl) {
268 fh_copy_shallow(&nbl->nbl_fh, fh);
269 locks_init_lock(&nbl->nbl_lock);
270 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
271 &nfsd4_cb_notify_lock_ops,
272 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
273 }
274 }
275 return nbl;
276}
277
278static void
279free_blocked_lock(struct nfsd4_blocked_lock *nbl)
280{
Jeff Layton6aaafc42019-04-22 12:34:23 -0400281 locks_delete_block(&nbl->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400282 locks_release_private(&nbl->nbl_lock);
283 kfree(nbl);
284}
285
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400286static void
287remove_blocked_locks(struct nfs4_lockowner *lo)
288{
289 struct nfs4_client *clp = lo->lo_owner.so_client;
290 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
291 struct nfsd4_blocked_lock *nbl;
292 LIST_HEAD(reaplist);
293
294 /* Dequeue all blocked locks */
295 spin_lock(&nn->blocked_locks_lock);
296 while (!list_empty(&lo->lo_blocked)) {
297 nbl = list_first_entry(&lo->lo_blocked,
298 struct nfsd4_blocked_lock,
299 nbl_list);
300 list_del_init(&nbl->nbl_list);
301 list_move(&nbl->nbl_lru, &reaplist);
302 }
303 spin_unlock(&nn->blocked_locks_lock);
304
305 /* Now free them */
306 while (!list_empty(&reaplist)) {
307 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
308 nbl_lru);
309 list_del_init(&nbl->nbl_lru);
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400310 free_blocked_lock(nbl);
311 }
312}
313
Jeff Laytonf4564582019-04-22 12:34:24 -0400314static void
315nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
316{
317 struct nfsd4_blocked_lock *nbl = container_of(cb,
318 struct nfsd4_blocked_lock, nbl_cb);
319 locks_delete_block(&nbl->nbl_lock);
320}
321
Jeff Layton76d348f2016-09-16 16:28:24 -0400322static int
323nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
324{
325 /*
326 * Since this is just an optimization, we don't try very hard if it
327 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
328 * just quit trying on anything else.
329 */
330 switch (task->tk_status) {
331 case -NFS4ERR_DELAY:
332 rpc_delay(task, 1 * HZ);
333 return 0;
334 default:
335 return 1;
336 }
337}
338
339static void
340nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
341{
342 struct nfsd4_blocked_lock *nbl = container_of(cb,
343 struct nfsd4_blocked_lock, nbl_cb);
344
345 free_blocked_lock(nbl);
346}
347
348static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
Jeff Laytonf4564582019-04-22 12:34:24 -0400349 .prepare = nfsd4_cb_notify_lock_prepare,
Jeff Layton76d348f2016-09-16 16:28:24 -0400350 .done = nfsd4_cb_notify_lock_done,
351 .release = nfsd4_cb_notify_lock_release,
352};
353
Kinglong Meeb5971af2014-08-22 10:18:43 -0400354static inline struct nfs4_stateowner *
355nfs4_get_stateowner(struct nfs4_stateowner *sop)
356{
357 atomic_inc(&sop->so_count);
358 return sop;
359}
360
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400361static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400362same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400363{
364 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400365 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400366}
367
368static struct nfs4_openowner *
369find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400370 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400371{
372 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400373
Trond Myklebustd4f04892014-07-29 21:34:36 -0400374 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400375
Trond Myklebustd4f04892014-07-29 21:34:36 -0400376 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
377 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400378 if (!so->so_is_open_owner)
379 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400380 if (same_owner_str(so, &open->op_owner))
381 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400382 }
383 return NULL;
384}
385
386static struct nfs4_openowner *
387find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400388 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400389{
390 struct nfs4_openowner *oo;
391
Trond Myklebustd4f04892014-07-29 21:34:36 -0400392 spin_lock(&clp->cl_lock);
393 oo = find_openstateowner_str_locked(hashval, open, clp);
394 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400395 return oo;
396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static inline u32
399opaque_hashval(const void *ptr, int nbytes)
400{
401 unsigned char *cptr = (unsigned char *) ptr;
402
403 u32 x = 0;
404 while (nbytes--) {
405 x *= 37;
406 x += *cptr++;
407 }
408 return x;
409}
410
Jeff Layton5b095e92014-10-23 08:01:02 -0400411static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400412{
Jeff Layton5b095e92014-10-23 08:01:02 -0400413 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
414
415 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400416}
417
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200418void
NeilBrown13cd2182005-06-23 22:03:10 -0700419put_nfs4_file(struct nfs4_file *fi)
420{
Jeff Layton02e12152014-07-16 10:31:57 -0400421 might_lock(&state_lock);
422
Elena Reshetova818a34e2017-10-20 12:53:30 +0300423 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400424 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400425 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200426 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400427 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
428 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800429 }
NeilBrown13cd2182005-06-23 22:03:10 -0700430}
431
Trond Myklebustde186432014-07-10 14:07:26 -0400432static struct file *
433__nfs4_get_fd(struct nfs4_file *f, int oflag)
434{
435 if (f->fi_fds[oflag])
436 return get_file(f->fi_fds[oflag]);
437 return NULL;
438}
439
440static struct file *
441find_writeable_file_locked(struct nfs4_file *f)
442{
443 struct file *ret;
444
445 lockdep_assert_held(&f->fi_lock);
446
447 ret = __nfs4_get_fd(f, O_WRONLY);
448 if (!ret)
449 ret = __nfs4_get_fd(f, O_RDWR);
450 return ret;
451}
452
453static struct file *
454find_writeable_file(struct nfs4_file *f)
455{
456 struct file *ret;
457
458 spin_lock(&f->fi_lock);
459 ret = find_writeable_file_locked(f);
460 spin_unlock(&f->fi_lock);
461
462 return ret;
463}
464
465static struct file *find_readable_file_locked(struct nfs4_file *f)
466{
467 struct file *ret;
468
469 lockdep_assert_held(&f->fi_lock);
470
471 ret = __nfs4_get_fd(f, O_RDONLY);
472 if (!ret)
473 ret = __nfs4_get_fd(f, O_RDWR);
474 return ret;
475}
476
477static struct file *
478find_readable_file(struct nfs4_file *f)
479{
480 struct file *ret;
481
482 spin_lock(&f->fi_lock);
483 ret = find_readable_file_locked(f);
484 spin_unlock(&f->fi_lock);
485
486 return ret;
487}
488
Christoph Hellwig4d227fc2014-08-17 07:40:00 -0500489struct file *
Trond Myklebustde186432014-07-10 14:07:26 -0400490find_any_file(struct nfs4_file *f)
491{
492 struct file *ret;
493
494 spin_lock(&f->fi_lock);
495 ret = __nfs4_get_fd(f, O_RDWR);
496 if (!ret) {
497 ret = __nfs4_get_fd(f, O_WRONLY);
498 if (!ret)
499 ret = __nfs4_get_fd(f, O_RDONLY);
500 }
501 spin_unlock(&f->fi_lock);
502 return ret;
503}
504
Trond Myklebust02a35082014-07-25 07:34:22 -0400505static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800506unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700507
508/*
509 * Open owner state (share locks)
510 */
511
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500512/* hash tables for lock and open owners */
513#define OWNER_HASH_BITS 8
514#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
515#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700516
Trond Myklebustd4f04892014-07-29 21:34:36 -0400517static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400518{
519 unsigned int ret;
520
521 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500522 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400523}
NeilBrownef0f3392006-04-10 22:55:41 -0700524
NeilBrownef0f3392006-04-10 22:55:41 -0700525/* hash table for nfs4_file */
526#define FILE_HASH_BITS 8
527#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800528
Trond Myklebustca943212014-07-23 16:17:39 -0400529static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400530{
Trond Myklebustca943212014-07-23 16:17:39 -0400531 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
532}
533
534static unsigned int file_hashval(struct knfsd_fh *fh)
535{
536 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
537}
538
Jeff Layton89876f82013-04-02 09:01:59 -0400539static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700540
Jeff Layton12659652014-07-10 14:07:28 -0400541static void
542__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400543{
Jeff Layton7214e862014-07-10 14:07:33 -0400544 lockdep_assert_held(&fp->fi_lock);
545
Jeff Layton12659652014-07-10 14:07:28 -0400546 if (access & NFS4_SHARE_ACCESS_WRITE)
547 atomic_inc(&fp->fi_access[O_WRONLY]);
548 if (access & NFS4_SHARE_ACCESS_READ)
549 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400550}
551
Jeff Layton12659652014-07-10 14:07:28 -0400552static __be32
553nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400554{
Jeff Layton7214e862014-07-10 14:07:33 -0400555 lockdep_assert_held(&fp->fi_lock);
556
Jeff Layton12659652014-07-10 14:07:28 -0400557 /* Does this access mode make sense? */
558 if (access & ~NFS4_SHARE_ACCESS_BOTH)
559 return nfserr_inval;
560
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400561 /* Does it conflict with a deny mode already set? */
562 if ((access & fp->fi_share_deny) != 0)
563 return nfserr_share_denied;
564
Jeff Layton12659652014-07-10 14:07:28 -0400565 __nfs4_file_get_access(fp, access);
566 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400567}
568
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400569static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
570{
571 /* Common case is that there is no deny mode. */
572 if (deny) {
573 /* Does this deny mode make sense? */
574 if (deny & ~NFS4_SHARE_DENY_BOTH)
575 return nfserr_inval;
576
577 if ((deny & NFS4_SHARE_DENY_READ) &&
578 atomic_read(&fp->fi_access[O_RDONLY]))
579 return nfserr_share_denied;
580
581 if ((deny & NFS4_SHARE_DENY_WRITE) &&
582 atomic_read(&fp->fi_access[O_WRONLY]))
583 return nfserr_share_denied;
584 }
585 return nfs_ok;
586}
587
J. Bruce Fields998db522010-08-07 09:21:41 -0400588static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400589{
Trond Myklebustde186432014-07-10 14:07:26 -0400590 might_lock(&fp->fi_lock);
591
592 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
593 struct file *f1 = NULL;
594 struct file *f2 = NULL;
595
Jeff Layton6d338b52014-07-10 14:07:29 -0400596 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400597 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400598 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400599 spin_unlock(&fp->fi_lock);
600 if (f1)
601 fput(f1);
602 if (f2)
603 fput(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400604 }
605}
606
Jeff Layton12659652014-07-10 14:07:28 -0400607static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400608{
Jeff Layton12659652014-07-10 14:07:28 -0400609 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
610
611 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400612 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400613 if (access & NFS4_SHARE_ACCESS_READ)
614 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400615}
616
Sachin Bhamare8287f002015-04-27 14:50:14 +0200617/*
618 * Allocate a new open/delegation state counter. This is needed for
619 * pNFS for proper return on close semantics.
620 *
621 * Note that we only allocate it for pNFS-enabled exports, otherwise
622 * all pointers to struct nfs4_clnt_odstate are always NULL.
623 */
624static struct nfs4_clnt_odstate *
625alloc_clnt_odstate(struct nfs4_client *clp)
626{
627 struct nfs4_clnt_odstate *co;
628
629 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
630 if (co) {
631 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300632 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200633 }
634 return co;
635}
636
637static void
638hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
639{
640 struct nfs4_file *fp = co->co_file;
641
642 lockdep_assert_held(&fp->fi_lock);
643 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
644}
645
646static inline void
647get_clnt_odstate(struct nfs4_clnt_odstate *co)
648{
649 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300650 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200651}
652
653static void
654put_clnt_odstate(struct nfs4_clnt_odstate *co)
655{
656 struct nfs4_file *fp;
657
658 if (!co)
659 return;
660
661 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300662 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200663 list_del(&co->co_perfile);
664 spin_unlock(&fp->fi_lock);
665
666 nfsd4_return_all_file_layouts(co->co_client, fp);
667 kmem_cache_free(odstate_slab, co);
668 }
669}
670
671static struct nfs4_clnt_odstate *
672find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
673{
674 struct nfs4_clnt_odstate *co;
675 struct nfs4_client *cl;
676
677 if (!new)
678 return NULL;
679
680 cl = new->co_client;
681
682 spin_lock(&fp->fi_lock);
683 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
684 if (co->co_client == cl) {
685 get_clnt_odstate(co);
686 goto out;
687 }
688 }
689 co = new;
690 co->co_file = fp;
691 hash_clnt_odstate_locked(new);
692out:
693 spin_unlock(&fp->fi_lock);
694 return co;
695}
696
Kinglong Meed19fb702017-01-18 19:04:42 +0800697struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
698 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400699{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500700 struct nfs4_stid *stid;
701 int new_id;
702
Trond Myklebustf8338832014-07-25 07:34:19 -0400703 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500704 if (!stid)
705 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400706
Jeff Layton4770d722014-07-29 21:34:10 -0400707 idr_preload(GFP_KERNEL);
708 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400709 /* Reserving 0 for start of file in nfsdfs "states" file: */
710 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400711 spin_unlock(&cl->cl_lock);
712 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700713 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500714 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800715
716 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500717 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500718 stid->sc_stateid.si_opaque.so_id = new_id;
719 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
720 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300721 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400722 spin_lock_init(&stid->sc_lock);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500723
J. Bruce Fields996e0932011-10-17 11:14:48 -0400724 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500725 * It shouldn't be a problem to reuse an opaque stateid value.
726 * I don't think it is for 4.1. But with 4.0 I worry that, for
727 * example, a stray write retransmission could be accepted by
728 * the server when it should have been rejected. Therefore,
729 * adopt a trick from the sctp code to attempt to maximize the
730 * amount of time until an id is reused, by ensuring they always
731 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400732 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500733 return stid;
734out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800735 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500736 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400737}
738
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400739/*
740 * Create a unique stateid_t to represent each COPY.
741 */
742int nfs4_init_cp_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
743{
744 int new_id;
745
746 idr_preload(GFP_KERNEL);
747 spin_lock(&nn->s2s_cp_lock);
748 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, copy, 0, 0, GFP_NOWAIT);
749 spin_unlock(&nn->s2s_cp_lock);
750 idr_preload_end();
751 if (new_id < 0)
752 return 0;
753 copy->cp_stateid.si_opaque.so_id = new_id;
754 copy->cp_stateid.si_opaque.so_clid.cl_boot = nn->boot_time;
755 copy->cp_stateid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
756 return 1;
757}
758
759void nfs4_free_cp_state(struct nfsd4_copy *copy)
760{
761 struct nfsd_net *nn;
762
763 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
764 spin_lock(&nn->s2s_cp_lock);
765 idr_remove(&nn->s2s_cp_stateids, copy->cp_stateid.si_opaque.so_id);
766 spin_unlock(&nn->s2s_cp_lock);
767}
768
Jeff Laytonb49e0842014-07-29 21:34:11 -0400769static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400770{
Trond Myklebust60116952014-07-29 21:34:06 -0400771 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400772
Kinglong Meed19fb702017-01-18 19:04:42 +0800773 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400774 if (!stid)
775 return NULL;
776
Kinglong Meed19fb702017-01-18 19:04:42 +0800777 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400778}
779
780static void nfs4_free_deleg(struct nfs4_stid *stid)
781{
Trond Myklebust60116952014-07-29 21:34:06 -0400782 kmem_cache_free(deleg_slab, stid);
783 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400784}
785
NeilBrown6282cd52014-06-04 17:39:26 +1000786/*
787 * When we recall a delegation, we should be careful not to hand it
788 * out again straight away.
789 * To ensure this we keep a pair of bloom filters ('new' and 'old')
790 * in which the filehandles of recalled delegations are "stored".
791 * If a filehandle appear in either filter, a delegation is blocked.
792 * When a delegation is recalled, the filehandle is stored in the "new"
793 * filter.
794 * Every 30 seconds we swap the filters and clear the "new" one,
795 * unless both are empty of course.
796 *
797 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
798 * low 3 bytes as hash-table indices.
799 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400800 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000801 * is used to manage concurrent access. Testing does not need the lock
802 * except when swapping the two filters.
803 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400804static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000805static struct bloom_pair {
806 int entries, old_entries;
807 time_t swap_time;
808 int new; /* index into 'set' */
809 DECLARE_BITMAP(set[2], 256);
810} blocked_delegations;
811
812static int delegation_blocked(struct knfsd_fh *fh)
813{
814 u32 hash;
815 struct bloom_pair *bd = &blocked_delegations;
816
817 if (bd->entries == 0)
818 return 0;
819 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400820 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000821 if (seconds_since_boot() - bd->swap_time > 30) {
822 bd->entries -= bd->old_entries;
823 bd->old_entries = bd->entries;
824 memset(bd->set[bd->new], 0,
825 sizeof(bd->set[0]));
826 bd->new = 1-bd->new;
827 bd->swap_time = seconds_since_boot();
828 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400829 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000830 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100831 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000832 if (test_bit(hash&255, bd->set[0]) &&
833 test_bit((hash>>8)&255, bd->set[0]) &&
834 test_bit((hash>>16)&255, bd->set[0]))
835 return 1;
836
837 if (test_bit(hash&255, bd->set[1]) &&
838 test_bit((hash>>8)&255, bd->set[1]) &&
839 test_bit((hash>>16)&255, bd->set[1]))
840 return 1;
841
842 return 0;
843}
844
845static void block_delegations(struct knfsd_fh *fh)
846{
847 u32 hash;
848 struct bloom_pair *bd = &blocked_delegations;
849
Daniel Borkmann87545892014-12-10 16:33:11 +0100850 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000851
Jeff Laytonf54fe962014-07-25 07:34:26 -0400852 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000853 __set_bit(hash&255, bd->set[bd->new]);
854 __set_bit((hash>>8)&255, bd->set[bd->new]);
855 __set_bit((hash>>16)&255, bd->set[bd->new]);
856 if (bd->entries == 0)
857 bd->swap_time = seconds_since_boot();
858 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400859 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500863alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
864 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200865 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400868 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400871 n = atomic_long_inc_return(&num_delegations);
872 if (n < 0 || n > max_delegations)
873 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000874 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400875 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800876 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700877 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400878 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400879
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400880 /*
881 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400882 * meaning of seqid 0 isn't meaningful, really, but let's avoid
883 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400884 */
885 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700886 INIT_LIST_HEAD(&dp->dl_perfile);
887 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200889 dp->dl_clnt_odstate = odstate;
890 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400891 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200892 dp->dl_retries = 1;
893 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200894 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500895 get_nfs4_file(fp);
896 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400898out_dec:
899 atomic_long_dec(&num_delegations);
900 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903void
Trond Myklebust60116952014-07-29 21:34:06 -0400904nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Trond Myklebust11b91642014-07-29 21:34:08 -0400906 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400907 struct nfs4_client *clp = s->sc_client;
908
Jeff Layton4770d722014-07-29 21:34:10 -0400909 might_lock(&clp->cl_lock);
910
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300911 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400912 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400913 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400914 }
Trond Myklebust60116952014-07-29 21:34:06 -0400915 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400916 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400917 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400918 if (fp)
919 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Jeff Layton9767feb2015-10-01 09:05:50 -0400922void
923nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
924{
925 stateid_t *src = &stid->sc_stateid;
926
927 spin_lock(&stid->sc_lock);
928 if (unlikely(++src->si_generation == 0))
929 src->si_generation = 1;
930 memcpy(dst, src, sizeof(*dst));
931 spin_unlock(&stid->sc_lock);
932}
933
J. Bruce Fields353601e2018-02-16 14:29:42 -0500934static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Jeff Layton6bcc0342014-08-09 10:22:40 -0400936 struct file *filp = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500937
938 spin_lock(&fp->fi_lock);
939 if (--fp->fi_delegees == 0)
940 swap(filp, fp->fi_deleg_file);
941 spin_unlock(&fp->fi_lock);
942
943 if (filp)
944 fput(filp);
945}
946
947static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
948{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -0500949 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500950 struct file *filp = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -0400951
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -0500952 WARN_ON_ONCE(!fp->fi_delegees);
953
J. Bruce Fields353601e2018-02-16 14:29:42 -0500954 vfs_setlease(filp, F_UNLCK, NULL, (void **)&dp);
955 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500958static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
959{
960 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500961 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500962 nfs4_put_stid(&dp->dl_stid);
963}
964
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200965void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400966{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500967 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400968}
969
Andrew Elble34ed9872015-10-15 12:07:28 -0400970/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500971 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -0400972 * @clp: a pointer to the nfs4_client we're granting a delegation to
973 * @fp: a pointer to the nfs4_file we're granting a delegation on
974 *
975 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500976 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -0400977 */
978
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500979static bool
980nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -0400981{
Andrew Elble34ed9872015-10-15 12:07:28 -0400982 struct nfs4_delegation *searchdp = NULL;
983 struct nfs4_client *searchclp = NULL;
984
Benny Halevycdc975052014-05-30 09:09:30 -0400985 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400986 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400987
Andrew Elble34ed9872015-10-15 12:07:28 -0400988 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
989 searchclp = searchdp->dl_stid.sc_client;
990 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +0800991 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -0400992 }
993 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +0800994 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -0400995}
996
997/**
998 * hash_delegation_locked - Add a delegation to the appropriate lists
999 * @dp: a pointer to the nfs4_delegation we are adding.
1000 * @fp: a pointer to the nfs4_file we're granting a delegation on
1001 *
1002 * Return:
1003 * On success: NULL if the delegation was successfully hashed.
1004 *
1005 * On error: -EAGAIN if one was previously granted to this
1006 * nfs4_client for this nfs4_file. Delegation is not hashed.
1007 *
1008 */
1009
1010static int
1011hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1012{
Andrew Elble34ed9872015-10-15 12:07:28 -04001013 struct nfs4_client *clp = dp->dl_stid.sc_client;
1014
1015 lockdep_assert_held(&state_lock);
1016 lockdep_assert_held(&fp->fi_lock);
1017
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001018 if (nfs4_delegation_exists(clp, fp))
1019 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001020 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001021 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001022 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001023 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1024 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001025}
1026
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001027static bool
Jeff Layton42690672014-07-25 07:34:20 -04001028unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Trond Myklebust11b91642014-07-29 21:34:08 -04001030 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001031
Jeff Layton42690672014-07-25 07:34:20 -04001032 lockdep_assert_held(&state_lock);
1033
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001034 if (list_empty(&dp->dl_perfile))
1035 return false;
1036
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001037 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001038 /* Ensure that deleg break won't try to requeue it */
1039 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001040 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001041 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001043 list_del_init(&dp->dl_perfile);
1044 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001045 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001046}
1047
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001048static void destroy_delegation(struct nfs4_delegation *dp)
1049{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001050 bool unhashed;
1051
Jeff Layton42690672014-07-25 07:34:20 -04001052 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001053 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001054 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001055 if (unhashed)
1056 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001057}
1058
1059static void revoke_delegation(struct nfs4_delegation *dp)
1060{
1061 struct nfs4_client *clp = dp->dl_stid.sc_client;
1062
Jeff Layton2d4a5322014-07-25 07:34:21 -04001063 WARN_ON(!list_empty(&dp->dl_recall_lru));
1064
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001065 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001066 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001067 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001068 spin_lock(&clp->cl_lock);
1069 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1070 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001071 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001072 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001073}
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075/*
1076 * SETCLIENTID state
1077 */
1078
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001079static unsigned int clientid_hashval(u32 id)
1080{
1081 return id & CLIENT_HASH_MASK;
1082}
1083
Scott Mayhew6b189102019-03-26 18:06:26 -04001084static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001085{
Scott Mayhew6b189102019-03-26 18:06:26 -04001086 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001090 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1091 * st_{access,deny}_bmap field of the stateid, in order to track not
1092 * only what share bits are currently in force, but also what
1093 * combinations of share bits previous opens have used. This allows us
1094 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1095 * return an error if the client attempt to downgrade to a combination
1096 * of share bits not explicable by closing some of its previous opens.
1097 *
1098 * XXX: This enforcement is actually incomplete, since we don't keep
1099 * track of access/deny bit combinations; so, e.g., we allow:
1100 *
1101 * OPEN allow read, deny write
1102 * OPEN allow both, deny none
1103 * DOWNGRADE allow read, deny none
1104 *
1105 * which we should reject.
1106 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001107static unsigned int
1108bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001109 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001110 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001111
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001112 for (i = 1; i < 4; i++) {
1113 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001114 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001115 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001116 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001117}
1118
Jeff Layton82c5ff12012-05-11 09:45:13 -04001119/* set share access for a given stateid */
1120static inline void
1121set_access(u32 access, struct nfs4_ol_stateid *stp)
1122{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001123 unsigned char mask = 1 << access;
1124
1125 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1126 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001127}
1128
1129/* clear share access for a given stateid */
1130static inline void
1131clear_access(u32 access, struct nfs4_ol_stateid *stp)
1132{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001133 unsigned char mask = 1 << access;
1134
1135 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1136 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001137}
1138
1139/* test whether a given stateid has access */
1140static inline bool
1141test_access(u32 access, struct nfs4_ol_stateid *stp)
1142{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001143 unsigned char mask = 1 << access;
1144
1145 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001146}
1147
Jeff Laytonce0fc432012-05-11 09:45:14 -04001148/* set share deny for a given stateid */
1149static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001150set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001151{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001152 unsigned char mask = 1 << deny;
1153
1154 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1155 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001156}
1157
1158/* clear share deny for a given stateid */
1159static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001160clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001161{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001162 unsigned char mask = 1 << deny;
1163
1164 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1165 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001166}
1167
1168/* test whether a given stateid is denying specific access */
1169static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001170test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001171{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001172 unsigned char mask = 1 << deny;
1173
1174 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001175}
1176
1177static int nfs4_access_to_omode(u32 access)
1178{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001179 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001180 case NFS4_SHARE_ACCESS_READ:
1181 return O_RDONLY;
1182 case NFS4_SHARE_ACCESS_WRITE:
1183 return O_WRONLY;
1184 case NFS4_SHARE_ACCESS_BOTH:
1185 return O_RDWR;
1186 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001187 WARN_ON_ONCE(1);
1188 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001189}
1190
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001191/*
1192 * A stateid that had a deny mode associated with it is being released
1193 * or downgraded. Recalculate the deny mode on the file.
1194 */
1195static void
1196recalculate_deny_mode(struct nfs4_file *fp)
1197{
1198 struct nfs4_ol_stateid *stp;
1199
1200 spin_lock(&fp->fi_lock);
1201 fp->fi_share_deny = 0;
1202 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1203 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1204 spin_unlock(&fp->fi_lock);
1205}
1206
1207static void
1208reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1209{
1210 int i;
1211 bool change = false;
1212
1213 for (i = 1; i < 4; i++) {
1214 if ((i & deny) != i) {
1215 change = true;
1216 clear_deny(i, stp);
1217 }
1218 }
1219
1220 /* Recalculate per-file deny mode if there was a change */
1221 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001222 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001223}
1224
Jeff Layton82c5ff12012-05-11 09:45:13 -04001225/* release all access and file references for a given stateid */
1226static void
1227release_all_access(struct nfs4_ol_stateid *stp)
1228{
1229 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001230 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001231
1232 if (fp && stp->st_deny_bmap != 0)
1233 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001234
1235 for (i = 1; i < 4; i++) {
1236 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001237 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001238 clear_access(i, stp);
1239 }
1240}
1241
Kinglong Meed50ffde2015-07-16 12:05:07 +08001242static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1243{
1244 kfree(sop->so_owner.data);
1245 sop->so_ops->so_free(sop);
1246}
1247
Jeff Layton6b180f02014-07-29 21:34:26 -04001248static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1249{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001250 struct nfs4_client *clp = sop->so_client;
1251
1252 might_lock(&clp->cl_lock);
1253
1254 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001255 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001256 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001257 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001258 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001259}
1260
Jeff Laytone8568732015-08-24 12:41:47 -04001261static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001262{
Trond Myklebust11b91642014-07-29 21:34:08 -04001263 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001264
Jeff Layton1c755dc2014-07-29 21:34:12 -04001265 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1266
Jeff Laytone8568732015-08-24 12:41:47 -04001267 if (list_empty(&stp->st_perfile))
1268 return false;
1269
Trond Myklebust1d31a252014-07-10 14:07:25 -04001270 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001271 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001272 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001273 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001274 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001275}
1276
Trond Myklebust60116952014-07-29 21:34:06 -04001277static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001278{
Trond Myklebust60116952014-07-29 21:34:06 -04001279 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001280
Sachin Bhamare8287f002015-04-27 14:50:14 +02001281 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001282 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001283 if (stp->st_stateowner)
1284 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001285 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001286}
1287
Jeff Laytonb49e0842014-07-29 21:34:11 -04001288static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001289{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001290 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1291 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001292 struct file *file;
1293
Jeff Laytonb49e0842014-07-29 21:34:11 -04001294 file = find_any_file(stp->st_stid.sc_file);
1295 if (file)
1296 filp_close(file, (fl_owner_t)lo);
1297 nfs4_free_ol_stateid(stid);
1298}
1299
Jeff Layton2c41beb2014-07-29 21:34:41 -04001300/*
1301 * Put the persistent reference to an already unhashed generic stateid, while
1302 * holding the cl_lock. If it's the last reference, then put it onto the
1303 * reaplist for later destruction.
1304 */
1305static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1306 struct list_head *reaplist)
1307{
1308 struct nfs4_stid *s = &stp->st_stid;
1309 struct nfs4_client *clp = s->sc_client;
1310
1311 lockdep_assert_held(&clp->cl_lock);
1312
1313 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1314
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001315 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001316 wake_up_all(&close_wq);
1317 return;
1318 }
1319
1320 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1321 list_add(&stp->st_locks, reaplist);
1322}
1323
Jeff Laytone8568732015-08-24 12:41:47 -04001324static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001325{
Chuck Leverf46c4452016-10-29 18:19:03 -04001326 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001327
1328 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001329 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001330 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001331}
1332
Jeff Layton5adfd882014-07-29 21:34:31 -04001333static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001334{
Chuck Leverf46c4452016-10-29 18:19:03 -04001335 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001336 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001337
Chuck Leverf46c4452016-10-29 18:19:03 -04001338 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001339 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001340 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001341 if (unhashed)
1342 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001343}
1344
Trond Myklebustc58c6612014-07-29 21:34:35 -04001345static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001346{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001347 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001348
Trond Myklebustd4f04892014-07-29 21:34:36 -04001349 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001350
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001351 list_del_init(&lo->lo_owner.so_strhash);
1352}
1353
Jeff Layton2c41beb2014-07-29 21:34:41 -04001354/*
1355 * Free a list of generic stateids that were collected earlier after being
1356 * fully unhashed.
1357 */
1358static void
1359free_ol_stateid_reaplist(struct list_head *reaplist)
1360{
1361 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001362 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001363
1364 might_sleep();
1365
1366 while (!list_empty(reaplist)) {
1367 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1368 st_locks);
1369 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001370 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001371 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001372 if (fp)
1373 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001374 }
1375}
1376
Jeff Laytond83017f2014-07-29 21:34:42 -04001377static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1378 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001379{
1380 struct nfs4_ol_stateid *stp;
1381
Jeff Laytone8568732015-08-24 12:41:47 -04001382 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1383
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001384 while (!list_empty(&open_stp->st_locks)) {
1385 stp = list_entry(open_stp->st_locks.next,
1386 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001387 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001388 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001389 }
1390}
1391
Jeff Laytone8568732015-08-24 12:41:47 -04001392static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001393 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001394{
Jeff Laytone8568732015-08-24 12:41:47 -04001395 bool unhashed;
1396
Jeff Layton2c41beb2014-07-29 21:34:41 -04001397 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1398
Jeff Laytone8568732015-08-24 12:41:47 -04001399 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001400 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001401 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001402}
1403
1404static void release_open_stateid(struct nfs4_ol_stateid *stp)
1405{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001406 LIST_HEAD(reaplist);
1407
1408 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001409 if (unhash_open_stateid(stp, &reaplist))
1410 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001411 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1412 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001413}
1414
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001415static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001416{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001417 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001418
Trond Myklebustd4f04892014-07-29 21:34:36 -04001419 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001420
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001421 list_del_init(&oo->oo_owner.so_strhash);
1422 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001423}
1424
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001425static void release_last_closed_stateid(struct nfs4_openowner *oo)
1426{
Jeff Layton217526e2014-07-30 08:27:11 -04001427 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1428 nfsd_net_id);
1429 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001430
Jeff Layton217526e2014-07-30 08:27:11 -04001431 spin_lock(&nn->client_lock);
1432 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001433 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001434 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001435 oo->oo_last_closed_stid = NULL;
1436 }
Jeff Layton217526e2014-07-30 08:27:11 -04001437 spin_unlock(&nn->client_lock);
1438 if (s)
1439 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001440}
1441
Jeff Layton2c41beb2014-07-29 21:34:41 -04001442static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001443{
1444 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001445 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001446 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001447
Jeff Layton2c41beb2014-07-29 21:34:41 -04001448 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001449
Trond Myklebustd4f04892014-07-29 21:34:36 -04001450 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001451 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001452 while (!list_empty(&oo->oo_owner.so_stateids)) {
1453 stp = list_first_entry(&oo->oo_owner.so_stateids,
1454 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001455 if (unhash_open_stateid(stp, &reaplist))
1456 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001457 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001458 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001459 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001460 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001461 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001462}
1463
Marc Eshel5282fd72009-04-03 08:27:52 +03001464static inline int
1465hash_sessionid(struct nfs4_sessionid *sessionid)
1466{
1467 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1468
1469 return sid->sequence % SESSION_HASH_SIZE;
1470}
1471
Mark Salter135dd002015-04-06 09:46:00 -04001472#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001473static inline void
1474dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1475{
1476 u32 *ptr = (u32 *)(&sessionid->data[0]);
1477 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1478}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001479#else
1480static inline void
1481dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1482{
1483}
1484#endif
1485
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001486/*
1487 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1488 * won't be used for replay.
1489 */
1490void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1491{
1492 struct nfs4_stateowner *so = cstate->replay_owner;
1493
1494 if (nfserr == nfserr_replay_me)
1495 return;
1496
1497 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001498 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001499 return;
1500 }
1501 if (!so)
1502 return;
1503 if (so->so_is_open_owner)
1504 release_last_closed_stateid(openowner(so));
1505 so->so_seqid++;
1506 return;
1507}
Marc Eshel5282fd72009-04-03 08:27:52 +03001508
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001509static void
1510gen_sessionid(struct nfsd4_session *ses)
1511{
1512 struct nfs4_client *clp = ses->se_client;
1513 struct nfsd4_sessionid *sid;
1514
1515 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1516 sid->clientid = clp->cl_clientid;
1517 sid->sequence = current_sessionid++;
1518 sid->reserved = 0;
1519}
1520
1521/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001522 * The protocol defines ca_maxresponssize_cached to include the size of
1523 * the rpc header, but all we need to cache is the data starting after
1524 * the end of the initial SEQUENCE operation--the rest we regenerate
1525 * each time. Therefore we can advertise a ca_maxresponssize_cached
1526 * value that is the number of bytes in our cache plus a few additional
1527 * bytes. In order to stay on the safe side, and not promise more than
1528 * we can cache, those additional bytes must be the minimum possible: 24
1529 * bytes of rpc header (xid through accept state, with AUTH_NULL
1530 * verifier), 12 for the compound header (with zero-length tag), and 44
1531 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001532 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001533#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1534
Andy Adamson557ce262009-08-28 08:45:04 -04001535static void
1536free_session_slots(struct nfsd4_session *ses)
1537{
1538 int i;
1539
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001540 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1541 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001542 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001543 }
Andy Adamson557ce262009-08-28 08:45:04 -04001544}
1545
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001546/*
1547 * We don't actually need to cache the rpc and session headers, so we
1548 * can allocate a little less for each slot:
1549 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001550static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001551{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001552 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001553
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001554 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1555 size = 0;
1556 else
1557 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1558 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001559}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001560
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001561/*
1562 * XXX: If we run out of reserved DRC memory we could (up to a point)
1563 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001564 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001565 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001566static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001567{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001568 u32 slotsize = slot_bytes(ca);
1569 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001570 unsigned long avail, total_avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001571
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001572 spin_lock(&nfsd_drc_lock);
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001573 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1574 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001575 /*
1576 * Never use more than a third of the remaining memory,
1577 * unless it's the only way to give this client a slot:
1578 */
Paul Menzel3b2d4dc2019-07-03 13:28:15 +02001579 avail = clamp_t(unsigned long, avail, slotsize, total_avail/3);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001580 num = min_t(int, num, avail / slotsize);
1581 nfsd_drc_mem_used += num * slotsize;
1582 spin_unlock(&nfsd_drc_lock);
1583
1584 return num;
1585}
1586
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001587static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001588{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001589 int slotsize = slot_bytes(ca);
1590
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001591 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001592 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001593 spin_unlock(&nfsd_drc_lock);
1594}
1595
Kinglong Mee60810e52014-01-01 00:35:47 +08001596static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1597 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001598{
Kinglong Mee60810e52014-01-01 00:35:47 +08001599 int numslots = fattrs->maxreqs;
1600 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001601 struct nfsd4_session *new;
1602 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001603
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001604 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001605 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1606 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001607
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001608 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001609 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001610 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001611 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001612 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001613 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001614 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001615 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001616 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001617
1618 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1619 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1620
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001621 return new;
1622out_free:
1623 while (i--)
1624 kfree(new->se_slots[i]);
1625 kfree(new);
1626 return NULL;
1627}
1628
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001629static void free_conn(struct nfsd4_conn *c)
1630{
1631 svc_xprt_put(c->cn_xprt);
1632 kfree(c);
1633}
1634
1635static void nfsd4_conn_lost(struct svc_xpt_user *u)
1636{
1637 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1638 struct nfs4_client *clp = c->cn_session->se_client;
1639
1640 spin_lock(&clp->cl_lock);
1641 if (!list_empty(&c->cn_persession)) {
1642 list_del(&c->cn_persession);
1643 free_conn(c);
1644 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001645 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001646 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001647}
1648
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001649static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001650{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001651 struct nfsd4_conn *conn;
1652
1653 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1654 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001655 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001656 svc_xprt_get(rqstp->rq_xprt);
1657 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001658 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001659 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1660 return conn;
1661}
1662
J. Bruce Fields328ead22010-09-29 16:11:06 -04001663static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1664{
1665 conn->cn_session = ses;
1666 list_add(&conn->cn_persession, &ses->se_conns);
1667}
1668
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001669static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1670{
1671 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001672
1673 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001674 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001675 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001676}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001677
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001678static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001679{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001680 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001681 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001682}
1683
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001684static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001685{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001686 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001687
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001688 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001689 ret = nfsd4_register_conn(conn);
1690 if (ret)
1691 /* oops; xprt is already down: */
1692 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001693 /* We may have gained or lost a callback channel: */
1694 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001695}
1696
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001697static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001698{
1699 u32 dir = NFS4_CDFC4_FORE;
1700
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001701 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001702 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001703 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001704}
1705
1706/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001707static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001708{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001709 struct nfs4_client *clp = s->se_client;
1710 struct nfsd4_conn *c;
1711
1712 spin_lock(&clp->cl_lock);
1713 while (!list_empty(&s->se_conns)) {
1714 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1715 list_del_init(&c->cn_persession);
1716 spin_unlock(&clp->cl_lock);
1717
1718 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1719 free_conn(c);
1720
1721 spin_lock(&clp->cl_lock);
1722 }
1723 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001724}
1725
J. Bruce Fields1377b692012-09-11 21:42:40 -04001726static void __free_session(struct nfsd4_session *ses)
1727{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001728 free_session_slots(ses);
1729 kfree(ses);
1730}
1731
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001732static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001733{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001734 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001735 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001736 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001737}
Andy Adamson557ce262009-08-28 08:45:04 -04001738
Fengguang Wu135ae822012-11-10 07:20:25 -05001739static 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 -04001740{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001741 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001742 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001743
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001744 new->se_client = clp;
1745 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001746
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001747 INIT_LIST_HEAD(&new->se_conns);
1748
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001749 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001750 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001751 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001752 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001753 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001754 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001755 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001756 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001757 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001758 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001759
Chuck Leverb0d2e422014-08-22 15:10:59 -04001760 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001761 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001762 /*
1763 * This is a little silly; with sessions there's no real
1764 * use for the callback address. Use the peer address
1765 * as a reasonable default for now, but consider fixing
1766 * the rpc client not to require an address in the
1767 * future:
1768 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001769 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1770 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001771 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001772}
1773
Benny Halevy9089f1b2010-05-12 00:12:26 +03001774/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001775static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001776__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001777{
1778 struct nfsd4_session *elem;
1779 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001780 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001781
Trond Myklebust0a880a22014-07-30 08:27:10 -04001782 lockdep_assert_held(&nn->client_lock);
1783
Marc Eshel5282fd72009-04-03 08:27:52 +03001784 dump_sessionid(__func__, sessionid);
1785 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001786 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001787 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001788 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1789 NFS4_MAX_SESSIONID_LEN)) {
1790 return elem;
1791 }
1792 }
1793
1794 dprintk("%s: session not found\n", __func__);
1795 return NULL;
1796}
1797
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001798static struct nfsd4_session *
1799find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1800 __be32 *ret)
1801{
1802 struct nfsd4_session *session;
1803 __be32 status = nfserr_badsession;
1804
1805 session = __find_in_sessionid_hashtbl(sessionid, net);
1806 if (!session)
1807 goto out;
1808 status = nfsd4_get_session_locked(session);
1809 if (status)
1810 session = NULL;
1811out:
1812 *ret = status;
1813 return session;
1814}
1815
Benny Halevy9089f1b2010-05-12 00:12:26 +03001816/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001817static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001818unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001819{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001820 struct nfs4_client *clp = ses->se_client;
1821 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1822
1823 lockdep_assert_held(&nn->client_lock);
1824
Andy Adamson7116ed62009-04-03 08:27:43 +03001825 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001826 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001827 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001828 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001829}
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1832static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001833STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001835 /*
1836 * We're assuming the clid was not given out from a boot
1837 * precisely 2^32 (about 136 years) before this one. That seems
1838 * a safe assumption:
1839 */
1840 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001842 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001843 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return 1;
1845}
1846
1847/*
1848 * XXX Should we use a slab cache ?
1849 * This type of memory management is somewhat inefficient, but we use it
1850 * anyway since SETCLIENTID is not a common operation.
1851 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001852static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001855 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Jeff Layton9258a2d2018-03-16 09:47:22 -04001857 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001858 if (clp == NULL)
1859 return NULL;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04001860 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001861 if (clp->cl_name.data == NULL)
1862 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001863 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1864 sizeof(struct list_head),
1865 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001866 if (!clp->cl_ownerstr_hashtbl)
1867 goto err_no_hashtbl;
1868 for (i = 0; i < OWNER_HASH_SIZE; i++)
1869 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
Trond Myklebust5694c932014-04-18 14:43:56 -04001870 INIT_LIST_HEAD(&clp->cl_sessions);
1871 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001872 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04001873 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1874 INIT_LIST_HEAD(&clp->cl_idhash);
1875 INIT_LIST_HEAD(&clp->cl_openowners);
1876 INIT_LIST_HEAD(&clp->cl_delegations);
1877 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001878 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001879#ifdef CONFIG_NFSD_PNFS
1880 INIT_LIST_HEAD(&clp->cl_lo_states);
1881#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001882 INIT_LIST_HEAD(&clp->async_copies);
1883 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001884 spin_lock_init(&clp->cl_lock);
1885 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001887err_no_hashtbl:
1888 kfree(clp->cl_name.data);
1889err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001890 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001891 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001894static void __free_client(struct kref *k)
1895{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001896 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1897 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001898
1899 free_svc_cred(&clp->cl_cred);
1900 kfree(clp->cl_ownerstr_hashtbl);
1901 kfree(clp->cl_name.data);
J. Bruce Fields79123442019-06-05 12:42:05 -04001902 kfree(clp->cl_nii_domain.data);
1903 kfree(clp->cl_nii_name.data);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001904 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001905 kmem_cache_free(client_slab, clp);
1906}
1907
YueHaibing297e57a2019-07-08 15:29:33 +08001908static void drop_client(struct nfs4_client *clp)
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001909{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001910 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001911}
1912
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001913static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914free_client(struct nfs4_client *clp)
1915{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001916 while (!list_empty(&clp->cl_sessions)) {
1917 struct nfsd4_session *ses;
1918 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1919 se_perclnt);
1920 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001921 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1922 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001923 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001924 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04001925 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001926 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04001927 clp->cl_nfsd_dentry = NULL;
1928 wake_up_all(&expiry_wq);
1929 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001930 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Benny Halevy84d38ac2010-05-12 00:13:16 +03001933/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001934static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001935unhash_client_locked(struct nfs4_client *clp)
1936{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001937 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001938 struct nfsd4_session *ses;
1939
Trond Myklebust0a880a22014-07-30 08:27:10 -04001940 lockdep_assert_held(&nn->client_lock);
1941
Trond Myklebust4beb3452014-07-30 08:27:02 -04001942 /* Mark the client as expired! */
1943 clp->cl_time = 0;
1944 /* Make it invisible */
1945 if (!list_empty(&clp->cl_idhash)) {
1946 list_del_init(&clp->cl_idhash);
1947 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1948 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1949 else
1950 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1951 }
1952 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001953 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001954 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1955 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001956 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001957}
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001960unhash_client(struct nfs4_client *clp)
1961{
1962 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1963
1964 spin_lock(&nn->client_lock);
1965 unhash_client_locked(clp);
1966 spin_unlock(&nn->client_lock);
1967}
1968
Jeff Layton97403d92014-07-30 08:27:12 -04001969static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1970{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001971 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04001972 return nfserr_jukebox;
1973 unhash_client_locked(clp);
1974 return nfs_ok;
1975}
1976
Trond Myklebust4beb3452014-07-30 08:27:02 -04001977static void
1978__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04001980 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001981 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 struct list_head reaplist;
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04001986 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001987 while (!list_empty(&clp->cl_delegations)) {
1988 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001989 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001990 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
Benny Halevycdc975052014-05-30 09:09:30 -04001992 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 while (!list_empty(&reaplist)) {
1994 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001995 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001996 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001998 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001999 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04002000 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04002001 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002002 }
NeilBrownea1da632005-06-23 22:04:17 -07002003 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002004 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002005 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002006 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002008 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2009 struct nfs4_stateowner *so, *tmp;
2010
2011 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2012 so_strhash) {
2013 /* Should be no openowners at this point */
2014 WARN_ON_ONCE(so->so_is_open_owner);
2015 remove_blocked_locks(lockowner(so));
2016 }
2017 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002018 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002019 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002020 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002021 if (clp->cl_cb_conn.cb_xprt)
2022 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002023 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002024 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Trond Myklebust4beb3452014-07-30 08:27:02 -04002027static void
2028destroy_client(struct nfs4_client *clp)
2029{
2030 unhash_client(clp);
2031 __destroy_client(clp);
2032}
2033
Scott Mayhew362063a2019-03-26 18:06:28 -04002034static void inc_reclaim_complete(struct nfs4_client *clp)
2035{
2036 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2037
2038 if (!nn->track_reclaim_completes)
2039 return;
2040 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2041 return;
2042 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2043 nn->reclaim_str_hashtbl_size) {
2044 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2045 clp->net->ns.inum);
2046 nfsd4_end_grace(nn);
2047 }
2048}
2049
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002050static void expire_client(struct nfs4_client *clp)
2051{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002052 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002053 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002054 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002055}
2056
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002057static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2058{
2059 memcpy(target->cl_verifier.data, source->data,
2060 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002063static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2064{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2066 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2067}
2068
J. Bruce Fields50043852015-11-20 15:58:37 -05002069static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2070{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002071 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2072 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2073 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002074 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2075 if ((source->cr_principal && !target->cr_principal) ||
2076 (source->cr_raw_principal && !target->cr_raw_principal) ||
2077 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002078 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002079
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002080 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 target->cr_uid = source->cr_uid;
2082 target->cr_gid = source->cr_gid;
2083 target->cr_group_info = source->cr_group_info;
2084 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002085 target->cr_gss_mech = source->cr_gss_mech;
2086 if (source->cr_gss_mech)
2087 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002088 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002091static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002092compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2093{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002094 if (o1->len < o2->len)
2095 return -1;
2096 if (o1->len > o2->len)
2097 return 1;
2098 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002099}
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002102same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2103{
2104 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
2107static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002108same_clid(clientid_t *cl1, clientid_t *cl2)
2109{
2110 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002113static bool groups_equal(struct group_info *g1, struct group_info *g2)
2114{
2115 int i;
2116
2117 if (g1->ngroups != g2->ngroups)
2118 return false;
2119 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002120 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002121 return false;
2122 return true;
2123}
2124
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002125/*
2126 * RFC 3530 language requires clid_inuse be returned when the
2127 * "principal" associated with a requests differs from that previously
2128 * used. We use uid, gid's, and gss principal string as our best
2129 * approximation. We also don't want to allow non-gss use of a client
2130 * established using gss: in theory cr_principal should catch that
2131 * change, but in practice cr_principal can be null even in the gss case
2132 * since gssd doesn't always pass down a principal string.
2133 */
2134static bool is_gss_cred(struct svc_cred *cr)
2135{
2136 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2137 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2138}
2139
2140
Vivek Trivedi5559b502012-07-24 21:18:20 +05302141static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002142same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2143{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002144 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002145 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2146 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002147 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2148 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002149 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002150 if (cr1->cr_principal == cr2->cr_principal)
2151 return true;
2152 if (!cr1->cr_principal || !cr2->cr_principal)
2153 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302154 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
J. Bruce Fields57266a62013-04-13 14:27:29 -04002157static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2158{
2159 struct svc_cred *cr = &rqstp->rq_cred;
2160 u32 service;
2161
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002162 if (!cr->cr_gss_mech)
2163 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002164 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2165 return service == RPC_GSS_SVC_INTEGRITY ||
2166 service == RPC_GSS_SVC_PRIVACY;
2167}
2168
Andrew Elblededeb132016-06-15 12:52:08 -04002169bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002170{
2171 struct svc_cred *cr = &rqstp->rq_cred;
2172
2173 if (!cl->cl_mach_cred)
2174 return true;
2175 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2176 return false;
2177 if (!svc_rqst_integrity_protected(rqstp))
2178 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002179 if (cl->cl_cred.cr_raw_principal)
2180 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2181 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002182 if (!cr->cr_principal)
2183 return false;
2184 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2185}
2186
Jeff Layton294ac322014-07-30 08:27:15 -04002187static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002188{
Chuck Leverab4684d2012-03-02 17:13:50 -05002189 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Jeff Laytonf4199922014-06-17 07:44:11 -04002191 /*
2192 * This is opaque to client, so no need to byte-swap. Use
2193 * __force to keep sparse happy
2194 */
2195 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002196 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002197 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
Jeff Layton294ac322014-07-30 08:27:15 -04002200static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2201{
2202 clp->cl_clientid.cl_boot = nn->boot_time;
2203 clp->cl_clientid.cl_id = nn->clientid_counter++;
2204 gen_confirm(clp, nn);
2205}
2206
Jeff Layton4770d722014-07-29 21:34:10 -04002207static struct nfs4_stid *
2208find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002209{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002210 struct nfs4_stid *ret;
2211
2212 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2213 if (!ret || !ret->sc_type)
2214 return NULL;
2215 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002216}
2217
Jeff Layton4770d722014-07-29 21:34:10 -04002218static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002219find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002220{
2221 struct nfs4_stid *s;
2222
Jeff Layton4770d722014-07-29 21:34:10 -04002223 spin_lock(&cl->cl_lock);
2224 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002225 if (s != NULL) {
2226 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002227 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002228 else
2229 s = NULL;
2230 }
Jeff Layton4770d722014-07-29 21:34:10 -04002231 spin_unlock(&cl->cl_lock);
2232 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002233}
2234
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002235static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2236{
2237 struct nfsdfs_client *nc;
2238 nc = get_nfsdfs_client(inode);
2239 if (!nc)
2240 return NULL;
2241 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2242}
2243
J. Bruce Fields169319f2019-06-19 12:39:46 -04002244static void seq_quote_mem(struct seq_file *m, char *data, int len)
2245{
2246 seq_printf(m, "\"");
2247 seq_escape_mem_ascii(m, data, len);
2248 seq_printf(m, "\"");
2249}
2250
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002251static int client_info_show(struct seq_file *m, void *v)
2252{
2253 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002254 struct nfs4_client *clp;
2255 u64 clid;
2256
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002257 clp = get_nfsdfs_clp(inode);
2258 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002259 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002260 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2261 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002262 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2263 seq_printf(m, "name: ");
2264 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2265 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields79123442019-06-05 12:42:05 -04002266 if (clp->cl_nii_domain.data) {
2267 seq_printf(m, "Implementation domain: ");
2268 seq_quote_mem(m, clp->cl_nii_domain.data,
2269 clp->cl_nii_domain.len);
2270 seq_printf(m, "\nImplementation name: ");
2271 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2272 seq_printf(m, "\nImplementation time: [%ld, %ld]\n",
2273 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2274 }
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002275 drop_client(clp);
2276
2277 return 0;
2278}
2279
2280static int client_info_open(struct inode *inode, struct file *file)
2281{
2282 return single_open(file, client_info_show, inode);
2283}
2284
2285static const struct file_operations client_info_fops = {
2286 .open = client_info_open,
2287 .read = seq_read,
2288 .llseek = seq_lseek,
2289 .release = single_release,
2290};
2291
J. Bruce Fields78599c42019-04-22 15:26:23 -04002292static void *states_start(struct seq_file *s, loff_t *pos)
2293 __acquires(&clp->cl_lock)
2294{
2295 struct nfs4_client *clp = s->private;
2296 unsigned long id = *pos;
2297 void *ret;
2298
2299 spin_lock(&clp->cl_lock);
2300 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2301 *pos = id;
2302 return ret;
2303}
2304
2305static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2306{
2307 struct nfs4_client *clp = s->private;
2308 unsigned long id = *pos;
2309 void *ret;
2310
2311 id = *pos;
2312 id++;
2313 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2314 *pos = id;
2315 return ret;
2316}
2317
2318static void states_stop(struct seq_file *s, void *v)
2319 __releases(&clp->cl_lock)
2320{
2321 struct nfs4_client *clp = s->private;
2322
2323 spin_unlock(&clp->cl_lock);
2324}
2325
2326static void nfs4_show_superblock(struct seq_file *s, struct file *f)
2327{
2328 struct inode *inode = file_inode(f);
2329
2330 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2331 MAJOR(inode->i_sb->s_dev),
2332 MINOR(inode->i_sb->s_dev),
2333 inode->i_ino);
2334}
2335
2336static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2337{
2338 seq_printf(s, "owner: ");
2339 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2340}
2341
2342static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2343{
2344 struct nfs4_ol_stateid *ols;
2345 struct nfs4_file *nf;
2346 struct file *file;
2347 struct nfs4_stateowner *oo;
2348 unsigned int access, deny;
2349
2350 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2351 return 0; /* XXX: or SEQ_SKIP? */
2352 ols = openlockstateid(st);
2353 oo = ols->st_stateowner;
2354 nf = st->sc_file;
2355 file = find_any_file(nf);
2356
2357 seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid);
2358
2359 access = bmap_to_share_mode(ols->st_access_bmap);
2360 deny = bmap_to_share_mode(ols->st_deny_bmap);
2361
2362 seq_printf(s, "access: \%s\%s, ",
2363 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2364 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2365 seq_printf(s, "deny: \%s\%s, ",
2366 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2367 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2368
2369 nfs4_show_superblock(s, file);
2370 seq_printf(s, ", ");
2371 nfs4_show_owner(s, oo);
2372 seq_printf(s, " }\n");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002373 fput(file);
2374
2375 return 0;
2376}
2377
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002378static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2379{
2380 struct nfs4_ol_stateid *ols;
2381 struct nfs4_file *nf;
2382 struct file *file;
2383 struct nfs4_stateowner *oo;
2384
2385 ols = openlockstateid(st);
2386 oo = ols->st_stateowner;
2387 nf = st->sc_file;
2388 file = find_any_file(nf);
2389
2390 seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid);
2391
2392 /*
2393 * Note: a lock stateid isn't really the same thing as a lock,
2394 * it's the locking state held by one owner on a file, and there
2395 * may be multiple (or no) lock ranges associated with it.
2396 * (Same for the matter is true of open stateids.)
2397 */
2398
2399 nfs4_show_superblock(s, file);
2400 /* XXX: open stateid? */
2401 seq_printf(s, ", ");
2402 nfs4_show_owner(s, oo);
2403 seq_printf(s, " }\n");
2404 fput(file);
2405
2406 return 0;
2407}
2408
2409static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2410{
2411 struct nfs4_delegation *ds;
2412 struct nfs4_file *nf;
2413 struct file *file;
2414
2415 ds = delegstateid(st);
2416 nf = st->sc_file;
2417 file = nf->fi_deleg_file;
2418
2419 seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid);
2420
2421 /* Kinda dead code as long as we only support read delegs: */
2422 seq_printf(s, "access: %s, ",
2423 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2424
2425 /* XXX: lease time, whether it's being recalled. */
2426
2427 nfs4_show_superblock(s, file);
2428 seq_printf(s, " }\n");
2429
2430 return 0;
2431}
2432
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002433static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2434{
2435 struct nfs4_layout_stateid *ls;
2436 struct file *file;
2437
2438 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2439 file = ls->ls_file;
2440
2441 seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid);
2442
2443 /* XXX: What else would be useful? */
2444
2445 nfs4_show_superblock(s, file);
2446 seq_printf(s, " }\n");
2447
2448 return 0;
2449}
2450
J. Bruce Fields78599c42019-04-22 15:26:23 -04002451static int states_show(struct seq_file *s, void *v)
2452{
2453 struct nfs4_stid *st = v;
2454
2455 switch (st->sc_type) {
2456 case NFS4_OPEN_STID:
2457 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002458 case NFS4_LOCK_STID:
2459 return nfs4_show_lock(s, st);
2460 case NFS4_DELEG_STID:
2461 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002462 case NFS4_LAYOUT_STID:
2463 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002464 default:
2465 return 0; /* XXX: or SEQ_SKIP? */
2466 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002467 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002468}
2469
2470static struct seq_operations states_seq_ops = {
2471 .start = states_start,
2472 .next = states_next,
2473 .stop = states_stop,
2474 .show = states_show
2475};
2476
2477static int client_states_open(struct inode *inode, struct file *file)
2478{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002479 struct seq_file *s;
2480 struct nfs4_client *clp;
2481 int ret;
2482
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002483 clp = get_nfsdfs_clp(inode);
2484 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002485 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002486
2487 ret = seq_open(file, &states_seq_ops);
2488 if (ret)
2489 return ret;
2490 s = file->private_data;
2491 s->private = clp;
2492 return 0;
2493}
2494
2495static int client_opens_release(struct inode *inode, struct file *file)
2496{
2497 struct seq_file *m = file->private_data;
2498 struct nfs4_client *clp = m->private;
2499
2500 /* XXX: alternatively, we could get/drop in seq start/stop */
2501 drop_client(clp);
2502 return 0;
2503}
2504
2505static const struct file_operations client_states_fops = {
2506 .open = client_states_open,
2507 .read = seq_read,
2508 .llseek = seq_lseek,
2509 .release = client_opens_release,
2510};
2511
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002512/*
2513 * Normally we refuse to destroy clients that are in use, but here the
2514 * administrator is telling us to just do it. We also want to wait
2515 * so the caller has a guarantee that the client's locks are gone by
2516 * the time the write returns:
2517 */
YueHaibing297e57a2019-07-08 15:29:33 +08002518static void force_expire_client(struct nfs4_client *clp)
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002519{
2520 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2521 bool already_expired;
2522
2523 spin_lock(&clp->cl_lock);
2524 clp->cl_time = 0;
2525 spin_unlock(&clp->cl_lock);
2526
2527 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2528 spin_lock(&nn->client_lock);
2529 already_expired = list_empty(&clp->cl_lru);
2530 if (!already_expired)
2531 unhash_client_locked(clp);
2532 spin_unlock(&nn->client_lock);
2533
2534 if (!already_expired)
2535 expire_client(clp);
2536 else
2537 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2538}
2539
2540static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2541 size_t size, loff_t *pos)
2542{
2543 char *data;
2544 struct nfs4_client *clp;
2545
2546 data = simple_transaction_get(file, buf, size);
2547 if (IS_ERR(data))
2548 return PTR_ERR(data);
2549 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2550 return -EINVAL;
2551 clp = get_nfsdfs_clp(file_inode(file));
2552 if (!clp)
2553 return -ENXIO;
2554 force_expire_client(clp);
2555 drop_client(clp);
2556 return 7;
2557}
2558
2559static const struct file_operations client_ctl_fops = {
2560 .write = client_ctl_write,
2561 .release = simple_transaction_release,
2562};
2563
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002564static const struct tree_descr client_files[] = {
2565 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002566 [1] = {"states", &client_states_fops, S_IRUSR},
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002567 [2] = {"ctl", &client_ctl_fops, S_IRUSR|S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002568 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002569};
2570
Jeff Layton2216d442012-11-12 15:00:57 -05002571static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002572 struct svc_rqst *rqstp, nfs4_verifier *verf)
2573{
2574 struct nfs4_client *clp;
2575 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002576 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002577 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002578 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002579
2580 clp = alloc_client(name);
2581 if (clp == NULL)
2582 return NULL;
2583
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002584 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2585 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002586 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002587 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002588 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002589 gen_clid(clp, nn);
2590 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002591 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002592 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002593 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002594 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002595 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002596 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002597 clp->net = net;
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002598 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002599 clp->cl_clientid.cl_id - nn->clientid_base,
2600 client_files);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002601 if (!clp->cl_nfsd_dentry) {
2602 free_client(clp);
2603 return NULL;
2604 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002605 return clp;
2606}
2607
NeilBrownfd39ca92005-06-23 22:04:03 -07002608static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002609add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2610{
2611 struct rb_node **new = &(root->rb_node), *parent = NULL;
2612 struct nfs4_client *clp;
2613
2614 while (*new) {
2615 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2616 parent = *new;
2617
2618 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2619 new = &((*new)->rb_left);
2620 else
2621 new = &((*new)->rb_right);
2622 }
2623
2624 rb_link_node(&new_clp->cl_namenode, parent, new);
2625 rb_insert_color(&new_clp->cl_namenode, root);
2626}
2627
2628static struct nfs4_client *
2629find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2630{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002631 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002632 struct rb_node *node = root->rb_node;
2633 struct nfs4_client *clp;
2634
2635 while (node) {
2636 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2637 cmp = compare_blob(&clp->cl_name, name);
2638 if (cmp > 0)
2639 node = node->rb_left;
2640 else if (cmp < 0)
2641 node = node->rb_right;
2642 else
2643 return clp;
2644 }
2645 return NULL;
2646}
2647
2648static void
2649add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002652 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Trond Myklebust0a880a22014-07-30 08:27:10 -04002654 lockdep_assert_held(&nn->client_lock);
2655
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002656 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002657 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002659 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002660 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
NeilBrownfd39ca92005-06-23 22:04:03 -07002663static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664move_to_confirmed(struct nfs4_client *clp)
2665{
2666 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002667 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Trond Myklebust0a880a22014-07-30 08:27:10 -04002669 lockdep_assert_held(&nn->client_lock);
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002672 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002673 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002674 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002675 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002676 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
2679static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002680find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
2682 struct nfs4_client *clp;
2683 unsigned int idhashval = clientid_hashval(clid->cl_id);
2684
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002685 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002686 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002687 if ((bool)clp->cl_minorversion != sessions)
2688 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002689 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
2693 return NULL;
2694}
2695
2696static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002697find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2698{
2699 struct list_head *tbl = nn->conf_id_hashtbl;
2700
Trond Myklebust0a880a22014-07-30 08:27:10 -04002701 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002702 return find_client_in_id_table(tbl, clid, sessions);
2703}
2704
2705static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002706find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002708 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Trond Myklebust0a880a22014-07-30 08:27:10 -04002710 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002711 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
2713
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002714static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002715{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002716 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002717}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002718
NeilBrown28ce6052005-06-23 22:03:56 -07002719static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002720find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002721{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002722 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002723 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002724}
2725
2726static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002727find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002728{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002729 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002730 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002731}
2732
NeilBrownfd39ca92005-06-23 22:04:03 -07002733static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002734gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002736 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002737 struct sockaddr *sa = svc_addr(rqstp);
2738 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002739 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Jeff Layton7077ecb2009-08-14 12:57:58 -04002741 /* Currently, we only support tcp and tcp6 for the callback channel */
2742 if (se->se_callback_netid_len == 3 &&
2743 !memcmp(se->se_callback_netid_val, "tcp", 3))
2744 expected_family = AF_INET;
2745 else if (se->se_callback_netid_len == 4 &&
2746 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2747 expected_family = AF_INET6;
2748 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 goto out_err;
2750
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002751 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002752 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002753 (struct sockaddr *)&conn->cb_addr,
2754 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002755
J. Bruce Fields07263f12010-05-31 19:09:40 -04002756 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002758
J. Bruce Fields07263f12010-05-31 19:09:40 -04002759 if (conn->cb_addr.ss_family == AF_INET6)
2760 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002761
J. Bruce Fields07263f12010-05-31 19:09:40 -04002762 conn->cb_prog = se->se_callback_prog;
2763 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002764 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return;
2766out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002767 conn->cb_addr.ss_family = AF_UNSPEC;
2768 conn->cb_addrlen = 0;
Rasmus Villemoes4ab495b2017-02-24 01:15:55 +01002769 dprintk("NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 "will not receive delegations\n",
2771 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return;
2774}
2775
Andy Adamson074fe892009-04-03 08:28:15 +03002776/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002777 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002778 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002779static void
Andy Adamson074fe892009-04-03 08:28:15 +03002780nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2781{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002782 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002783 struct nfsd4_slot *slot = resp->cstate.slot;
2784 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002785
Andy Adamson557ce262009-08-28 08:45:04 -04002786 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002787
J. Bruce Fields085def32017-10-18 16:17:18 -04002788 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002789 slot->sl_opcnt = resp->opcnt;
2790 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002791 free_svc_cred(&slot->sl_cred);
2792 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002793
J. Bruce Fields085def32017-10-18 16:17:18 -04002794 if (!nfsd4_cache_this(resp)) {
2795 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002796 return;
2797 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002798 slot->sl_flags |= NFSD4_SLOT_CACHED;
2799
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002800 base = resp->cstate.data_offset;
2801 slot->sl_datalen = buf->len - base;
2802 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002803 WARN(1, "%s: sessions DRC could not cache compound\n",
2804 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002805 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002806}
2807
2808/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002809 * Encode the replay sequence operation from the slot values.
2810 * If cachethis is FALSE encode the uncached rep error on the next
2811 * operation which sets resp->p and increments resp->opcnt for
2812 * nfs4svc_encode_compoundres.
2813 *
Andy Adamson074fe892009-04-03 08:28:15 +03002814 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002815static __be32
2816nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2817 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002818{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002819 struct nfsd4_op *op;
2820 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002821
Andy Adamsonabfabf82009-07-23 19:02:18 -04002822 /* Encode the replayed sequence operation */
2823 op = &args->ops[resp->opcnt - 1];
2824 nfsd4_encode_operation(resp, op);
2825
J. Bruce Fields085def32017-10-18 16:17:18 -04002826 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2827 return op->status;
2828 if (args->opcnt == 1) {
2829 /*
2830 * The original operation wasn't a solo sequence--we
2831 * always cache those--so this retry must not match the
2832 * original:
2833 */
2834 op->status = nfserr_seq_false_retry;
2835 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002836 op = &args->ops[resp->opcnt++];
2837 op->status = nfserr_retry_uncached_rep;
2838 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002839 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002840 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002841}
2842
2843/*
Andy Adamson557ce262009-08-28 08:45:04 -04002844 * The sequence operation is not cached because we can use the slot and
2845 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002846 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002847static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002848nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2849 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002850{
Andy Adamson557ce262009-08-28 08:45:04 -04002851 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002852 struct xdr_stream *xdr = &resp->xdr;
2853 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002854 __be32 status;
2855
Andy Adamson557ce262009-08-28 08:45:04 -04002856 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002857
Andy Adamsonabfabf82009-07-23 19:02:18 -04002858 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002859 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002860 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002861
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002862 p = xdr_reserve_space(xdr, slot->sl_datalen);
2863 if (!p) {
2864 WARN_ON_ONCE(1);
2865 return nfserr_serverfault;
2866 }
2867 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2868 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002869
Andy Adamson557ce262009-08-28 08:45:04 -04002870 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002871 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002872}
2873
Andy Adamson0733d212009-04-03 08:28:01 +03002874/*
2875 * Set the exchange_id flags returned by the server.
2876 */
2877static void
2878nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2879{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002880#ifdef CONFIG_NFSD_PNFS
2881 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2882#else
Andy Adamson0733d212009-04-03 08:28:01 +03002883 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002884#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002885
2886 /* Referrals are supported, Migration is not. */
2887 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2888
2889 /* set the wire flags to return to client. */
2890 clid->flags = new->cl_exchange_flags;
2891}
2892
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002893static bool client_has_openowners(struct nfs4_client *clp)
2894{
2895 struct nfs4_openowner *oo;
2896
2897 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2898 if (!list_empty(&oo->oo_owner.so_stateids))
2899 return true;
2900 }
2901 return false;
2902}
2903
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002904static bool client_has_state(struct nfs4_client *clp)
2905{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002906 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002907#ifdef CONFIG_NFSD_PNFS
2908 || !list_empty(&clp->cl_lo_states)
2909#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002910 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002911 || !list_empty(&clp->cl_sessions)
2912 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002913}
2914
J. Bruce Fields79123442019-06-05 12:42:05 -04002915static __be32 copy_impl_id(struct nfs4_client *clp,
2916 struct nfsd4_exchange_id *exid)
2917{
2918 if (!exid->nii_domain.data)
2919 return 0;
2920 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
2921 if (!clp->cl_nii_domain.data)
2922 return nfserr_jukebox;
2923 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
2924 if (!clp->cl_nii_name.data)
2925 return nfserr_jukebox;
2926 clp->cl_nii_time.tv_sec = exid->nii_time.tv_sec;
2927 clp->cl_nii_time.tv_nsec = exid->nii_time.tv_nsec;
2928 return 0;
2929}
2930
Al Virob37ad282006-10-19 23:28:59 -07002931__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02002932nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2933 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002934{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002935 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002936 struct nfs4_client *conf, *new;
2937 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002938 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002939 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002940 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002941 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002942 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002943 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002944
Jeff Layton363168b2009-08-14 12:57:56 -04002945 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002946 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002947 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002948 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002949 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002950
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002951 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002952 return nfserr_inval;
2953
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002954 new = create_client(exid->clname, rqstp, &verf);
2955 if (new == NULL)
2956 return nfserr_jukebox;
J. Bruce Fields79123442019-06-05 12:42:05 -04002957 status = copy_impl_id(new, exid);
2958 if (status)
2959 goto out_nolock;
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002960
Andy Adamson0733d212009-04-03 08:28:01 +03002961 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002962 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002963 exid->spo_must_enforce[0] = 0;
2964 exid->spo_must_enforce[1] = (
2965 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2966 1 << (OP_EXCHANGE_ID - 32) |
2967 1 << (OP_CREATE_SESSION - 32) |
2968 1 << (OP_DESTROY_SESSION - 32) |
2969 1 << (OP_DESTROY_CLIENTID - 32));
2970
2971 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2972 1 << (OP_OPEN_DOWNGRADE) |
2973 1 << (OP_LOCKU) |
2974 1 << (OP_DELEGRETURN));
2975
2976 exid->spo_must_allow[1] &= (
2977 1 << (OP_TEST_STATEID - 32) |
2978 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002979 if (!svc_rqst_integrity_protected(rqstp)) {
2980 status = nfserr_inval;
2981 goto out_nolock;
2982 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002983 /*
2984 * Sometimes userspace doesn't give us a principal.
2985 * Which is a bug, really. Anyway, we can't enforce
2986 * MACH_CRED in that case, better to give up now:
2987 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002988 if (!new->cl_cred.cr_principal &&
2989 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002990 status = nfserr_serverfault;
2991 goto out_nolock;
2992 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002993 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002994 case SP4_NONE:
2995 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002996 default: /* checked by xdr code */
2997 WARN_ON_ONCE(1);
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06002998 /* fall through */
Andy Adamson0733d212009-04-03 08:28:01 +03002999 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08003000 status = nfserr_encr_alg_unsupp;
3001 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03003002 }
3003
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003004 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003005 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003006 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03003007 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003008 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3009 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3010
J. Bruce Fields136e6582012-05-12 20:37:23 -04003011 if (update) {
3012 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003013 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03003014 goto out;
3015 }
Andrew Elblededeb132016-06-15 12:52:08 -04003016 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003017 status = nfserr_wrong_cred;
3018 goto out;
3019 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003020 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03003021 status = nfserr_perm;
3022 goto out;
3023 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003024 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03003025 status = nfserr_not_same;
3026 goto out;
3027 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003028 /* case 6 */
3029 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003030 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003031 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003032 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003033 if (client_has_state(conf)) {
3034 status = nfserr_clid_inuse;
3035 goto out;
3036 }
Andy Adamson0733d212009-04-03 08:28:01 +03003037 goto out_new;
3038 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003039 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003040 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003041 goto out_copy;
3042 }
3043 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003044 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003045 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003046 }
3047
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003048 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003049 status = nfserr_noent;
3050 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003051 }
3052
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003053 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003054 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003055 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003056
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003057 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03003058out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003059 if (conf) {
3060 status = mark_client_expired_locked(conf);
3061 if (status)
3062 goto out;
3063 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003064 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003065 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3066 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003067
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003068 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003069 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003070out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003071 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3072 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003073
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003074 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3075 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003076
3077 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003078 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003079 status = nfs_ok;
3080
3081out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003082 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003083out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003084 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003085 expire_client(new);
3086 if (unconf)
3087 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003088 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003089}
3090
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003091static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003092check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003093{
Andy Adamson88e588d2009-07-23 19:02:15 -04003094 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3095 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003096
3097 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003098 if (slot_inuse) {
3099 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003100 return nfserr_jukebox;
3101 else
3102 return nfserr_seq_misordered;
3103 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003104 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003105 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003106 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003107 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003108 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003109 return nfserr_seq_misordered;
3110}
3111
Andy Adamson49557cc2009-07-23 19:02:16 -04003112/*
3113 * Cache the create session result into the create session single DRC
3114 * slot cache by saving the xdr structure. sl_seqid has been set.
3115 * Do this for solo or embedded create session operations.
3116 */
3117static void
3118nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003119 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003120{
3121 slot->sl_status = nfserr;
3122 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3123}
3124
3125static __be32
3126nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3127 struct nfsd4_clid_slot *slot)
3128{
3129 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3130 return slot->sl_status;
3131}
3132
Mi Jinlong1b74c252011-07-14 14:50:17 +08003133#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3134 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3135 1 + /* MIN tag is length with zero, only length */ \
3136 3 + /* version, opcount, opcode */ \
3137 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3138 /* seqid, slotID, slotID, cache */ \
3139 4 ) * sizeof(__be32))
3140
3141#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3142 2 + /* verifier: AUTH_NULL, length 0 */\
3143 1 + /* status */ \
3144 1 + /* MIN tag is length with zero, only length */ \
3145 3 + /* opcount, opcode, opstatus*/ \
3146 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3147 /* seqid, slotID, slotID, slotID, status */ \
3148 5 ) * sizeof(__be32))
3149
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003150static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003151{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003152 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3153
J. Bruce Fields373cd402013-04-08 15:42:12 -04003154 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3155 return nfserr_toosmall;
3156 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3157 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003158 ca->headerpadsz = 0;
3159 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3160 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3161 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3162 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3163 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3164 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3165 /*
3166 * Note decreasing slot size below client's request may make it
3167 * difficult for client to function correctly, whereas
3168 * decreasing the number of slots will (just?) affect
3169 * performance. When short on memory we therefore prefer to
3170 * decrease number of slots instead of their size. Clients that
3171 * request larger slots than they need will get poor results:
3172 */
3173 ca->maxreqs = nfsd4_get_drc_mem(ca);
3174 if (!ca->maxreqs)
3175 return nfserr_jukebox;
3176
J. Bruce Fields373cd402013-04-08 15:42:12 -04003177 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003178}
3179
Chuck Lever45006322016-03-01 13:06:02 -05003180/*
3181 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3182 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3183 */
3184#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3185 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3186
3187#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3188 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3189
Kinglong Mee8a891632013-12-23 18:11:02 +08003190#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003191 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003192#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003193 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3194 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003195
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003196static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3197{
3198 ca->headerpadsz = 0;
3199
Kinglong Mee8a891632013-12-23 18:11:02 +08003200 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003201 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003202 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003203 return nfserr_toosmall;
3204 ca->maxresp_cached = 0;
3205 if (ca->maxops < 2)
3206 return nfserr_toosmall;
3207
3208 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003209}
3210
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003211static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3212{
3213 switch (cbs->flavor) {
3214 case RPC_AUTH_NULL:
3215 case RPC_AUTH_UNIX:
3216 return nfs_ok;
3217 default:
3218 /*
3219 * GSS case: the spec doesn't allow us to return this
3220 * error. But it also doesn't allow us not to support
3221 * GSS.
3222 * I'd rather this fail hard than return some error the
3223 * client might think it can already handle:
3224 */
3225 return nfserr_encr_alg_unsupp;
3226 }
3227}
3228
Andy Adamson069b6ad2009-04-03 08:27:58 +03003229__be32
3230nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003231 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003232{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003233 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003234 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003235 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003236 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003237 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003238 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003239 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003240 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003241 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003242
Mi Jinlonga62573d2011-03-23 17:57:07 +08003243 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3244 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003245 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3246 if (status)
3247 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003248 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003249 if (status)
3250 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003251 status = check_backchannel_attrs(&cr_ses->back_channel);
3252 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003253 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003254 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003255 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003256 if (!new)
3257 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003258 conn = alloc_conn_from_crses(rqstp, cr_ses);
3259 if (!conn)
3260 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003261
Jeff Laytond20c11d2014-07-30 08:27:07 -04003262 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003263 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003264 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003265 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003266
3267 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003268 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003269 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003270 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003271 cs_slot = &conf->cl_cs_slot;
3272 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003273 if (status) {
3274 if (status == nfserr_replay_cache)
3275 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003276 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003277 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003278 } else if (unconf) {
3279 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003280 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003281 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003282 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003283 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003284 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003285 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003286 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003287 cs_slot = &unconf->cl_cs_slot;
3288 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003289 if (status) {
3290 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003291 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003292 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003293 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003294 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003295 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003296 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003297 if (status) {
3298 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003299 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003300 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003301 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003302 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003303 conf = unconf;
3304 } else {
3305 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003306 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003307 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003308 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003309 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003310 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003311 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003312 cr_ses->flags &= ~SESSION4_RDMA;
3313
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003314 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003315 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003316
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003317 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003318 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003319 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003320 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003321
Jeff Laytond20c11d2014-07-30 08:27:07 -04003322 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003323 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003324 spin_unlock(&nn->client_lock);
3325 /* init connection and backchannel */
3326 nfsd4_init_conn(rqstp, conn, new);
3327 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003328 if (old)
3329 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003330 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003331out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003332 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003333 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003334 if (old)
3335 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003336out_free_session:
3337 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003338out_release_drc_mem:
3339 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003340 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003341}
3342
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003343static __be32 nfsd4_map_bcts_dir(u32 *dir)
3344{
3345 switch (*dir) {
3346 case NFS4_CDFC4_FORE:
3347 case NFS4_CDFC4_BACK:
3348 return nfs_ok;
3349 case NFS4_CDFC4_FORE_OR_BOTH:
3350 case NFS4_CDFC4_BACK_OR_BOTH:
3351 *dir = NFS4_CDFC4_BOTH;
3352 return nfs_ok;
3353 };
3354 return nfserr_inval;
3355}
3356
Christoph Hellwigeb698532017-05-08 20:58:35 +02003357__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3358 struct nfsd4_compound_state *cstate,
3359 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003360{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003361 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003362 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003363 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003364 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003365
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003366 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3367 if (status)
3368 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003369 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003370 session->se_cb_prog = bc->bc_cb_program;
3371 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003372 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003373
3374 nfsd4_probe_callback(session->se_client);
3375
3376 return nfs_ok;
3377}
3378
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003379__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3380 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003381 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003382{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003383 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003384 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003385 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003386 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003387 struct net *net = SVC_NET(rqstp);
3388 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003389
3390 if (!nfsd4_last_compound_op(rqstp))
3391 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003392 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003393 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003394 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003395 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003396 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003397 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003398 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003399 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003400 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003401 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003402 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003403 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003404 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003405 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003406 goto out;
3407 nfsd4_init_conn(rqstp, conn, session);
3408 status = nfs_ok;
3409out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003410 nfsd4_put_session(session);
3411out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003412 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003413}
3414
J. Bruce Fields665d5072018-04-11 16:53:36 -04003415static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003416{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003417 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003418 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003419 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003420}
3421
Andy Adamson069b6ad2009-04-03 08:27:58 +03003422__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003423nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3424 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003425{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003426 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003427 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003428 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003429 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003430 struct net *net = SVC_NET(r);
3431 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003432
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003433 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003434 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003435 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003436 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003437 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003438 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003439 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003440 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003441 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003442 if (!ses)
3443 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003444 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003445 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003446 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003447 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003448 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003449 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003450 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003451 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003452
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003453 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003454
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003455 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003456 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003457out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003458 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003459out_client_lock:
3460 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003461out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003462 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003463}
3464
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003465static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003466{
3467 struct nfsd4_conn *c;
3468
3469 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003470 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04003471 return c;
3472 }
3473 }
3474 return NULL;
3475}
3476
J. Bruce Fields57266a62013-04-13 14:27:29 -04003477static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003478{
3479 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003480 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003481 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003482 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003483
3484 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003485 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003486 if (c)
3487 goto out_free;
3488 status = nfserr_conn_not_bound_to_session;
3489 if (clp->cl_mach_cred)
3490 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003491 __nfsd4_hash_conn(new, ses);
3492 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003493 ret = nfsd4_register_conn(new);
3494 if (ret)
3495 /* oops; xprt is already down: */
3496 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003497 return nfs_ok;
3498out_free:
3499 spin_unlock(&clp->cl_lock);
3500 free_conn(new);
3501 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003502}
3503
Mi Jinlong868b89c2011-04-27 09:09:58 +08003504static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3505{
3506 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3507
3508 return args->opcnt > session->se_fchannel.maxops;
3509}
3510
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003511static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3512 struct nfsd4_session *session)
3513{
3514 struct xdr_buf *xb = &rqstp->rq_arg;
3515
3516 return xb->len > session->se_fchannel.maxreq_sz;
3517}
3518
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003519static bool replay_matches_cache(struct svc_rqst *rqstp,
3520 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3521{
3522 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3523
3524 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3525 (bool)seq->cachethis)
3526 return false;
3527 /*
3528 * If there's an error than the reply can have fewer ops than
3529 * the call. But if we cached a reply with *more* ops than the
3530 * call you're sending us now, then this new call is clearly not
3531 * really a replay of the old one:
3532 */
3533 if (slot->sl_opcnt < argp->opcnt)
3534 return false;
3535 /* This is the only check explicitly called by spec: */
3536 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3537 return false;
3538 /*
3539 * There may be more comparisons we could actually do, but the
3540 * spec doesn't require us to catch every case where the calls
3541 * don't match (that would require caching the call as well as
3542 * the reply), so we don't bother.
3543 */
3544 return true;
3545}
3546
Andy Adamson069b6ad2009-04-03 08:27:58 +03003547__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003548nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3549 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003550{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003551 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003552 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003553 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003554 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003555 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003556 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003557 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003558 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003559 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003560 struct net *net = SVC_NET(rqstp);
3561 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003562
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003563 if (resp->opcnt != 1)
3564 return nfserr_sequence_pos;
3565
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003566 /*
3567 * Will be either used or freed by nfsd4_sequence_check_conn
3568 * below.
3569 */
3570 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3571 if (!conn)
3572 return nfserr_jukebox;
3573
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003574 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003575 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003576 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003577 goto out_no_session;
3578 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003579
Mi Jinlong868b89c2011-04-27 09:09:58 +08003580 status = nfserr_too_many_ops;
3581 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003582 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003583
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003584 status = nfserr_req_too_big;
3585 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003586 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003587
Benny Halevyb85d4c02009-04-03 08:28:08 +03003588 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003589 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003590 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003591
Andy Adamson557ce262009-08-28 08:45:04 -04003592 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003593 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3594
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003595 /* We do not negotiate the number of slots yet, so set the
3596 * maxslots to the session maxreqs which is used to encode
3597 * sr_highest_slotid and the sr_target_slot id to maxslots */
3598 seq->maxslots = session->se_fchannel.maxreqs;
3599
J. Bruce Fields73e79482012-02-13 16:39:00 -05003600 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3601 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003602 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003603 status = nfserr_seq_misordered;
3604 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003605 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003606 status = nfserr_seq_false_retry;
3607 if (!replay_matches_cache(rqstp, seq, slot))
3608 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003609 cstate->slot = slot;
3610 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003611 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003612 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003613 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003614 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003615 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003616 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003617 }
3618 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003619 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003620
J. Bruce Fields57266a62013-04-13 14:27:29 -04003621 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003622 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003623 if (status)
3624 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003625
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003626 buflen = (seq->cachethis) ?
3627 session->se_fchannel.maxresp_cached :
3628 session->se_fchannel.maxresp_sz;
3629 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3630 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003631 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003632 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003633 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003634
3635 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003636 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003637 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003638 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003639 if (seq->cachethis)
3640 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003641 else
3642 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003643
3644 cstate->slot = slot;
3645 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003646 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003647
Benny Halevyb85d4c02009-04-03 08:28:08 +03003648out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003649 switch (clp->cl_cb_state) {
3650 case NFSD4_CB_DOWN:
3651 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3652 break;
3653 case NFSD4_CB_FAULT:
3654 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3655 break;
3656 default:
3657 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003658 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003659 if (!list_empty(&clp->cl_revoked))
3660 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003661out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003662 if (conn)
3663 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003664 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003665 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003666out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003667 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003668 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003669}
3670
Trond Myklebustb6076642014-06-30 11:48:35 -04003671void
3672nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3673{
3674 struct nfsd4_compound_state *cs = &resp->cstate;
3675
3676 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003677 if (cs->status != nfserr_replay_cache) {
3678 nfsd4_store_cache_entry(resp);
3679 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3680 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003681 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003682 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003683 } else if (cs->clp)
3684 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003685}
3686
Mi Jinlong345c2842011-10-20 17:51:39 +08003687__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003688nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3689 struct nfsd4_compound_state *cstate,
3690 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003691{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003692 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003693 struct nfs4_client *conf, *unconf;
3694 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003695 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003696 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003697
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003698 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003699 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003700 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003701 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003702
3703 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003704 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003705 status = nfserr_clientid_busy;
3706 goto out;
3707 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003708 status = mark_client_expired_locked(conf);
3709 if (status)
3710 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003711 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003712 } else if (unconf)
3713 clp = unconf;
3714 else {
3715 status = nfserr_stale_clientid;
3716 goto out;
3717 }
Andrew Elblededeb132016-06-15 12:52:08 -04003718 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003719 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003720 status = nfserr_wrong_cred;
3721 goto out;
3722 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003723 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003724out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003725 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003726 if (clp)
3727 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003728 return status;
3729}
3730
Andy Adamson069b6ad2009-04-03 08:27:58 +03003731__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003732nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3733 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003734{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003735 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003736 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003737
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003738 if (rc->rca_one_fs) {
3739 if (!cstate->current_fh.fh_dentry)
3740 return nfserr_nofilehandle;
3741 /*
3742 * We don't take advantage of the rca_one_fs case.
3743 * That's OK, it's optional, we can safely ignore it.
3744 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003745 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003746 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003747
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003748 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003749 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3750 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003751 goto out;
3752
3753 status = nfserr_stale_clientid;
3754 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003755 /*
3756 * The following error isn't really legal.
3757 * But we only get here if the client just explicitly
3758 * destroyed the client. Surely it no longer cares what
3759 * error it gets back on an operation for the dead
3760 * client.
3761 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003762 goto out;
3763
3764 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003765 nfsd4_client_record_create(cstate->session->se_client);
Scott Mayhew362063a2019-03-26 18:06:28 -04003766 inc_reclaim_complete(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003767out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003768 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003769}
3770
3771__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003772nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003773 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003775 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003776 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003778 struct nfs4_client *conf, *new;
3779 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003780 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003781 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3782
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003783 new = create_client(clname, rqstp, &clverifier);
3784 if (new == NULL)
3785 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003786 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003787 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003788 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003789 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003790 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003792 if (clp_used_exchangeid(conf))
3793 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003794 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003795 char addr_str[INET6_ADDRSTRLEN];
3796 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3797 sizeof(addr_str));
3798 dprintk("NFSD: setclientid: string in use by client "
3799 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 goto out;
3801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003803 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003804 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003805 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003806 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003807 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003809 gen_confirm(new, nn);
3810 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04003811 ;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003812 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003813 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003814 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3816 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3817 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003818 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 status = nfs_ok;
3820out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003821 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003822 if (new)
3823 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003824 if (unconf)
3825 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 return status;
3827}
3828
3829
Al Virob37ad282006-10-19 23:28:59 -07003830__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003831nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003832 struct nfsd4_compound_state *cstate,
3833 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003835 struct nfsd4_setclientid_confirm *setclientid_confirm =
3836 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003837 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003838 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3840 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003841 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003842 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003844 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003846
Jeff Laytond20c11d2014-07-30 08:27:07 -04003847 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003848 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003849 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003850 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003851 * We try hard to give out unique clientid's, so if we get an
3852 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003853 * the client may be buggy; this should never happen.
3854 *
3855 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003856 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003857 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003858 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3859 goto out;
3860 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3861 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003862 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003863 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003864 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3865 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003867 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003868 status = nfserr_stale_clientid;
3869 goto out;
3870 }
3871 status = nfs_ok;
3872 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003873 old = unconf;
3874 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003875 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003876 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003877 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3878 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003879 status = nfserr_clid_inuse;
3880 if (client_has_state(old)
3881 && !same_creds(&unconf->cl_cred,
3882 &old->cl_cred))
3883 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003884 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003885 if (status) {
3886 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003887 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003888 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003889 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003890 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003891 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003892 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003893 get_client_locked(conf);
3894 spin_unlock(&nn->client_lock);
3895 nfsd4_probe_callback(conf);
3896 spin_lock(&nn->client_lock);
3897 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003899 spin_unlock(&nn->client_lock);
3900 if (old)
3901 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 return status;
3903}
3904
J. Bruce Fields32513b42011-10-13 16:00:16 -04003905static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003907 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3908}
3909
3910/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003911static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3912 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003913{
Trond Myklebust950e0112014-06-30 11:48:31 -04003914 lockdep_assert_held(&state_lock);
3915
Elena Reshetova818a34e2017-10-20 12:53:30 +03003916 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003917 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003918 INIT_LIST_HEAD(&fp->fi_stateids);
3919 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003920 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003921 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003922 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003923 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003924 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003925 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3926 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003927#ifdef CONFIG_NFSD_PNFS
3928 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003929 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003930#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003931 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932}
3933
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003934void
NeilBrowne60d4392005-06-23 22:03:01 -07003935nfsd4_free_slabs(void)
3936{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003937 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003938 kmem_cache_destroy(openowner_slab);
3939 kmem_cache_destroy(lockowner_slab);
3940 kmem_cache_destroy(file_slab);
3941 kmem_cache_destroy(stateid_slab);
3942 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003943 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003944}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
Bryan Schumaker72083392011-11-01 15:24:59 -04003946int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947nfsd4_init_slabs(void)
3948{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003949 client_slab = kmem_cache_create("nfsd4_clients",
3950 sizeof(struct nfs4_client), 0, 0, NULL);
3951 if (client_slab == NULL)
3952 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003953 openowner_slab = kmem_cache_create("nfsd4_openowners",
3954 sizeof(struct nfs4_openowner), 0, 0, NULL);
3955 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04003956 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003957 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003958 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003959 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003960 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003961 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003962 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003963 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003964 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003965 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003966 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003967 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003968 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003969 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003970 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003971 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003972 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003973 odstate_slab = kmem_cache_create("nfsd4_odstate",
3974 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3975 if (odstate_slab == NULL)
3976 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003978
Sachin Bhamare8287f002015-04-27 14:50:14 +02003979out_free_deleg_slab:
3980 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003981out_free_stateid_slab:
3982 kmem_cache_destroy(stateid_slab);
3983out_free_file_slab:
3984 kmem_cache_destroy(file_slab);
3985out_free_lockowner_slab:
3986 kmem_cache_destroy(lockowner_slab);
3987out_free_openowner_slab:
3988 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003989out_free_client_slab:
3990 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003991out:
NeilBrowne60d4392005-06-23 22:03:01 -07003992 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3993 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994}
3995
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003996static void init_nfs4_replay(struct nfs4_replay *rp)
3997{
3998 rp->rp_status = nfserr_serverfault;
3999 rp->rp_buflen = 0;
4000 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04004001 mutex_init(&rp->rp_mutex);
4002}
4003
4004static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4005 struct nfs4_stateowner *so)
4006{
4007 if (!nfsd4_has_session(cstate)) {
4008 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004009 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004010 }
4011}
4012
4013void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4014{
4015 struct nfs4_stateowner *so = cstate->replay_owner;
4016
4017 if (so != NULL) {
4018 cstate->replay_owner = NULL;
4019 mutex_unlock(&so->so_replay.rp_mutex);
4020 nfs4_put_stateowner(so);
4021 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004022}
4023
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004024static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
4026 struct nfs4_stateowner *sop;
4027
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004028 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004029 if (!sop)
4030 return NULL;
4031
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04004032 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004033 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004034 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004035 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004037
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004038 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004039 sop->so_client = clp;
4040 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04004041 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004042 return sop;
4043}
4044
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004045static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004046{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004047 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004048
Trond Myklebustd4f04892014-07-29 21:34:36 -04004049 list_add(&oo->oo_owner.so_strhash,
4050 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004051 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052}
4053
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004054static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4055{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004056 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004057}
4058
Jeff Layton6b180f02014-07-29 21:34:26 -04004059static void nfs4_free_openowner(struct nfs4_stateowner *so)
4060{
4061 struct nfs4_openowner *oo = openowner(so);
4062
4063 kmem_cache_free(openowner_slab, oo);
4064}
4065
4066static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004067 .so_unhash = nfs4_unhash_openowner,
4068 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004069};
4070
Andrew Elble7fc05642015-11-05 20:42:43 -05004071static struct nfs4_ol_stateid *
4072nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4073{
4074 struct nfs4_ol_stateid *local, *ret = NULL;
4075 struct nfs4_openowner *oo = open->op_openowner;
4076
4077 lockdep_assert_held(&fp->fi_lock);
4078
4079 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4080 /* ignore lock owners */
4081 if (local->st_stateowner->so_is_open_owner == 0)
4082 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004083 if (local->st_stateowner != &oo->oo_owner)
4084 continue;
4085 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004086 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004087 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05004088 break;
4089 }
4090 }
4091 return ret;
4092}
4093
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004094static __be32
4095nfsd4_verify_open_stid(struct nfs4_stid *s)
4096{
4097 __be32 ret = nfs_ok;
4098
4099 switch (s->sc_type) {
4100 default:
4101 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004102 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004103 case NFS4_CLOSED_STID:
4104 case NFS4_CLOSED_DELEG_STID:
4105 ret = nfserr_bad_stateid;
4106 break;
4107 case NFS4_REVOKED_DELEG_STID:
4108 ret = nfserr_deleg_revoked;
4109 }
4110 return ret;
4111}
4112
4113/* Lock the stateid st_mutex, and deal with races with CLOSE */
4114static __be32
4115nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4116{
4117 __be32 ret;
4118
Andrew Elble4f34bd02017-11-08 17:29:51 -05004119 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004120 ret = nfsd4_verify_open_stid(&stp->st_stid);
4121 if (ret != nfs_ok)
4122 mutex_unlock(&stp->st_mutex);
4123 return ret;
4124}
4125
4126static struct nfs4_ol_stateid *
4127nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4128{
4129 struct nfs4_ol_stateid *stp;
4130 for (;;) {
4131 spin_lock(&fp->fi_lock);
4132 stp = nfsd4_find_existing_open(fp, open);
4133 spin_unlock(&fp->fi_lock);
4134 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4135 break;
4136 nfs4_put_stid(&stp->st_stid);
4137 }
4138 return stp;
4139}
4140
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004141static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004142alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004143 struct nfsd4_compound_state *cstate)
4144{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004145 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004146 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004148 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4149 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004151 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004152 oo->oo_owner.so_is_open_owner = 1;
4153 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004154 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004155 if (nfsd4_has_session(cstate))
4156 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004157 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004158 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004159 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004160 spin_lock(&clp->cl_lock);
4161 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004162 if (ret == NULL) {
4163 hash_openowner(oo, clp, strhashval);
4164 ret = oo;
4165 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004166 nfs4_free_stateowner(&oo->oo_owner);
4167
Trond Myklebustd4f04892014-07-29 21:34:36 -04004168 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004169 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
Andrew Elble7fc05642015-11-05 20:42:43 -05004172static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004173init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004174{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Andrew Elble7fc05642015-11-05 20:42:43 -05004176 struct nfs4_openowner *oo = open->op_openowner;
4177 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004178 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004179
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004180 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004181 /* We are moving these outside of the spinlocks to avoid the warnings */
4182 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004183 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004184
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004185retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004186 spin_lock(&oo->oo_owner.so_client->cl_lock);
4187 spin_lock(&fp->fi_lock);
4188
4189 retstp = nfsd4_find_existing_open(fp, open);
4190 if (retstp)
4191 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004192
4193 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004194 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004195 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004196 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004197 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004198 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004199 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 stp->st_access_bmap = 0;
4201 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004202 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004203 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004204 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004205
4206out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004207 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004208 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004209 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004210 /* Handle races with CLOSE */
4211 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4212 nfs4_put_stid(&retstp->st_stid);
4213 goto retry;
4214 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004215 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004216 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004217 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004218 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004219 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220}
4221
Jeff Laytond3134b12014-07-29 21:34:32 -04004222/*
4223 * In the 4.0 case we need to keep the owners around a little while to handle
4224 * CLOSE replay. We still do need to release any file access that is held by
4225 * them before returning however.
4226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004228move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
Jeff Layton217526e2014-07-30 08:27:11 -04004230 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004231 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4232 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4233 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004234
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004235 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Jeff Laytonb401be222014-07-29 21:34:33 -04004237 /*
4238 * We know that we hold one reference via nfsd4_close, and another
4239 * "persistent" reference for the client. If the refcount is higher
4240 * than 2, then there are still calls in progress that are using this
4241 * stateid. We can't put the sc_file reference until they are finished.
4242 * Wait for the refcount to drop to 2. Since it has been unhashed,
4243 * there should be no danger of the refcount going back up again at
4244 * this point.
4245 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004246 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004247
Jeff Laytond3134b12014-07-29 21:34:32 -04004248 release_all_access(s);
4249 if (s->st_stid.sc_file) {
4250 put_nfs4_file(s->st_stid.sc_file);
4251 s->st_stid.sc_file = NULL;
4252 }
Jeff Layton217526e2014-07-30 08:27:11 -04004253
4254 spin_lock(&nn->client_lock);
4255 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004256 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004257 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004258 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004259 spin_unlock(&nn->client_lock);
4260 if (last)
4261 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262}
4263
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264/* search file_hashtbl[] for file */
4265static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04004266find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 struct nfs4_file *fp;
4269
Jeff Layton5b095e92014-10-23 08:01:02 -04004270 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02004271 if (fh_match(&fp->fi_fhandle, fh)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004272 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004273 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 }
4276 return NULL;
4277}
4278
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02004279struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04004280find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004281{
4282 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004283 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004284
Jeff Layton5b095e92014-10-23 08:01:02 -04004285 rcu_read_lock();
4286 fp = find_file_locked(fh, hashval);
4287 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004288 return fp;
4289}
4290
4291static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004292find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004293{
4294 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004295 unsigned int hashval = file_hashval(fh);
4296
4297 rcu_read_lock();
4298 fp = find_file_locked(fh, hashval);
4299 rcu_read_unlock();
4300 if (fp)
4301 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004302
4303 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04004304 fp = find_file_locked(fh, hashval);
4305 if (likely(fp == NULL)) {
4306 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04004307 fp = new;
4308 }
4309 spin_unlock(&state_lock);
4310
4311 return fp;
4312}
4313
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004314/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 * Called to check deny when READ with all zero stateid or
4316 * WRITE with all zero or all one stateid
4317 */
Al Virob37ad282006-10-19 23:28:59 -07004318static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4320{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004322 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
Trond Myklebustca943212014-07-23 16:17:39 -04004324 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07004325 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004326 return ret;
4327 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004328 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004329 if (fp->fi_share_deny & deny_type)
4330 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004331 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004332 put_nfs4_file(fp);
4333 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334}
4335
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004336static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004338 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004339 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4340 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004341
Trond Myklebust11b91642014-07-29 21:34:08 -04004342 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004343
Jeff Layton02e12152014-07-16 10:31:57 -04004344 /*
4345 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004346 * already holding inode->i_lock.
4347 *
Jeff Laytondff13992014-07-08 14:02:49 -04004348 * If the dl_time != 0, then we know that it has already been
4349 * queued for a lease break. Don't queue it again.
4350 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004351 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004352 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04004353 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004354 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004355 }
Jeff Layton02e12152014-07-16 10:31:57 -04004356 spin_unlock(&state_lock);
4357}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004359static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4360 struct rpc_task *task)
4361{
4362 struct nfs4_delegation *dp = cb_to_delegation(cb);
4363
Andrew Elblea4579742015-08-31 12:06:41 -04004364 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
4365 return 1;
4366
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004367 switch (task->tk_status) {
4368 case 0:
4369 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004370 case -NFS4ERR_DELAY:
4371 rpc_delay(task, 2 * HZ);
4372 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004373 case -EBADHANDLE:
4374 case -NFS4ERR_BAD_STATEID:
4375 /*
4376 * Race: client probably got cb_recall before open reply
4377 * granting delegation.
4378 */
4379 if (dp->dl_retries--) {
4380 rpc_delay(task, 2 * HZ);
4381 return 0;
4382 }
4383 /*FALLTHRU*/
4384 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004385 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004386 }
4387}
4388
4389static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4390{
4391 struct nfs4_delegation *dp = cb_to_delegation(cb);
4392
4393 nfs4_put_stid(&dp->dl_stid);
4394}
4395
Julia Lawallc4cb8972015-11-21 22:57:39 +01004396static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004397 .prepare = nfsd4_cb_recall_prepare,
4398 .done = nfsd4_cb_recall_done,
4399 .release = nfsd4_cb_recall_release,
4400};
4401
Jeff Layton02e12152014-07-16 10:31:57 -04004402static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4403{
4404 /*
4405 * We're assuming the state code never drops its reference
4406 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004407 * callback (and since the lease code is serialized by the
4408 * i_lock) we know the server hasn't removed the lease yet, and
4409 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004410 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004411 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004412 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004413}
4414
Jeff Layton1c8c6012013-06-21 08:58:15 -04004415/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004416static bool
4417nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004418{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004419 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004420 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4421 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004422
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004423 /*
4424 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004425 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004426 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004427 */
4428 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
Jeff Layton02e12152014-07-16 10:31:57 -04004430 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004431 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004432 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004433 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004434 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
Jeff Laytonc45198e2014-09-01 07:12:07 -04004437static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004438nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4439 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440{
4441 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004442 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 else
4444 return -EAGAIN;
4445}
4446
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004447static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004448 .lm_break = nfsd_break_deleg_cb,
4449 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450};
4451
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004452static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4453{
4454 if (nfsd4_has_session(cstate))
4455 return nfs_ok;
4456 if (seqid == so->so_seqid - 1)
4457 return nfserr_replay_me;
4458 if (seqid == so->so_seqid)
4459 return nfs_ok;
4460 return nfserr_bad_seqid;
4461}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Jeff Layton4b24ca72014-06-30 11:48:44 -04004463static __be32 lookup_clientid(clientid_t *clid,
4464 struct nfsd4_compound_state *cstate,
4465 struct nfsd_net *nn)
4466{
4467 struct nfs4_client *found;
4468
4469 if (cstate->clp) {
4470 found = cstate->clp;
4471 if (!same_clid(&found->cl_clientid, clid))
4472 return nfserr_stale_clientid;
4473 return nfs_ok;
4474 }
4475
4476 if (STALE_CLIENTID(clid, nn))
4477 return nfserr_stale_clientid;
4478
4479 /*
4480 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4481 * cached already then we know this is for is for v4.0 and "sessions"
4482 * will be false.
4483 */
4484 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004485 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004486 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004487 if (!found) {
4488 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004489 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004490 }
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04004491 atomic_inc(&found->cl_rpc_users);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004492 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004493
4494 /* Cache the nfs4_client in cstate! */
4495 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004496 return nfs_ok;
4497}
4498
Al Virob37ad282006-10-19 23:28:59 -07004499__be32
Andy Adamson66689582009-04-03 08:28:45 +03004500nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004501 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 clientid_t *clientid = &open->op_clientid;
4504 struct nfs4_client *clp = NULL;
4505 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004506 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004507 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004509 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004511 /*
4512 * In case we need it later, after we've already created the
4513 * file and don't want to risk a further failure:
4514 */
4515 open->op_file = nfsd4_alloc_file();
4516 if (open->op_file == NULL)
4517 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
Trond Myklebust2d91e892014-06-30 11:48:46 -04004519 status = lookup_clientid(clientid, cstate, nn);
4520 if (status)
4521 return status;
4522 clp = cstate->clp;
4523
Trond Myklebustd4f04892014-07-29 21:34:36 -04004524 strhashval = ownerstr_hashval(&open->op_owner);
4525 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004526 open->op_openowner = oo;
4527 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004528 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004530 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004531 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004532 release_openowner(oo);
4533 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004534 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004535 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004536 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4537 if (status)
4538 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004539 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004540new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004541 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004542 if (oo == NULL)
4543 return nfserr_jukebox;
4544 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004545alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004546 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004547 if (!open->op_stp)
4548 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004549
4550 if (nfsd4_has_session(cstate) &&
4551 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4552 open->op_odstate = alloc_clnt_odstate(clp);
4553 if (!open->op_odstate)
4554 return nfserr_jukebox;
4555 }
4556
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004557 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558}
4559
Al Virob37ad282006-10-19 23:28:59 -07004560static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004561nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4562{
4563 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4564 return nfserr_openmode;
4565 else
4566 return nfs_ok;
4567}
4568
Daniel Mackc47d8322011-05-16 16:38:14 +02004569static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004570{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004571 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4572}
4573
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004574static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004575{
4576 struct nfs4_stid *ret;
4577
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004578 ret = find_stateid_by_type(cl, s,
4579 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004580 if (!ret)
4581 return NULL;
4582 return delegstateid(ret);
4583}
4584
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004585static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4586{
4587 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4588 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4589}
4590
Al Virob37ad282006-10-19 23:28:59 -07004591static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004592nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004593 struct nfs4_delegation **dp)
4594{
4595 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004596 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004597 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004598
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004599 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4600 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004601 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004602 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4603 nfs4_put_stid(&deleg->dl_stid);
4604 if (cl->cl_minorversion)
4605 status = nfserr_deleg_revoked;
4606 goto out;
4607 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004608 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004609 status = nfs4_check_delegmode(deleg, flags);
4610 if (status) {
4611 nfs4_put_stid(&deleg->dl_stid);
4612 goto out;
4613 }
4614 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004615out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004616 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004617 return nfs_ok;
4618 if (status)
4619 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004620 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004621 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004622}
4623
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004624static inline int nfs4_access_to_access(u32 nfs4_access)
4625{
4626 int flags = 0;
4627
4628 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4629 flags |= NFSD_MAY_READ;
4630 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4631 flags |= NFSD_MAY_WRITE;
4632 return flags;
4633}
4634
Al Virob37ad282006-10-19 23:28:59 -07004635static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4637 struct nfsd4_open *open)
4638{
4639 struct iattr iattr = {
4640 .ia_valid = ATTR_SIZE,
4641 .ia_size = 0,
4642 };
4643 if (!open->op_truncate)
4644 return 0;
4645 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004646 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4648}
4649
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004650static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004651 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4652 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004653{
Trond Myklebustde186432014-07-10 14:07:26 -04004654 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004655 __be32 status;
4656 int oflag = nfs4_access_to_omode(open->op_share_access);
4657 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004658 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004659
Trond Myklebustde186432014-07-10 14:07:26 -04004660 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004661
4662 /*
4663 * Are we trying to set a deny mode that would conflict with
4664 * current access?
4665 */
4666 status = nfs4_file_check_deny(fp, open->op_share_deny);
4667 if (status != nfs_ok) {
4668 spin_unlock(&fp->fi_lock);
4669 goto out;
4670 }
4671
4672 /* set access to the file */
4673 status = nfs4_file_get_access(fp, open->op_share_access);
4674 if (status != nfs_ok) {
4675 spin_unlock(&fp->fi_lock);
4676 goto out;
4677 }
4678
4679 /* Set access bits in stateid */
4680 old_access_bmap = stp->st_access_bmap;
4681 set_access(open->op_share_access, stp);
4682
4683 /* Set new deny mask */
4684 old_deny_bmap = stp->st_deny_bmap;
4685 set_deny(open->op_share_deny, stp);
4686 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4687
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004688 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004689 spin_unlock(&fp->fi_lock);
4690 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004691 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004692 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004693 spin_lock(&fp->fi_lock);
4694 if (!fp->fi_fds[oflag]) {
4695 fp->fi_fds[oflag] = filp;
4696 filp = NULL;
4697 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004698 }
Trond Myklebustde186432014-07-10 14:07:26 -04004699 spin_unlock(&fp->fi_lock);
4700 if (filp)
4701 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004702
4703 status = nfsd4_truncate(rqstp, cur_fh, open);
4704 if (status)
4705 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004706out:
4707 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004708out_put_access:
4709 stp->st_access_bmap = old_access_bmap;
4710 nfs4_file_put_access(fp, open->op_share_access);
4711 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4712 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004713}
4714
Al Virob37ad282006-10-19 23:28:59 -07004715static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004716nfs4_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 -07004717{
Al Virob37ad282006-10-19 23:28:59 -07004718 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004719 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004721 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004722 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004723
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004724 /* test and set deny mode */
4725 spin_lock(&fp->fi_lock);
4726 status = nfs4_file_check_deny(fp, open->op_share_deny);
4727 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004728 set_deny(open->op_share_deny, stp);
4729 fp->fi_share_deny |=
4730 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4731 }
4732 spin_unlock(&fp->fi_lock);
4733
4734 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004737 status = nfsd4_truncate(rqstp, cur_fh, open);
4738 if (status != nfs_ok)
4739 reset_union_bmap_deny(old_deny_bmap, stp);
4740 return status;
4741}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004743/* Should we give out recallable state?: */
4744static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4745{
4746 if (clp->cl_cb_state == NFSD4_CB_UP)
4747 return true;
4748 /*
4749 * In the sessions case, since we don't have to establish a
4750 * separate connection for callbacks, we assume it's OK
4751 * until we hear otherwise:
4752 */
4753 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4754}
4755
J. Bruce Fields653e5142018-02-15 22:08:45 -05004756static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4757 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004758{
4759 struct file_lock *fl;
4760
4761 fl = locks_alloc_lock();
4762 if (!fl)
4763 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004764 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004765 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004766 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4767 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004768 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004769 fl->fl_pid = current->tgid;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004770 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004771 return fl;
4772}
4773
Jeff Layton0b266932014-07-25 07:34:25 -04004774static struct nfs4_delegation *
4775nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004776 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004777{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004778 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004779 struct nfs4_delegation *dp;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004780 struct file *filp;
4781 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004782
J. Bruce Fields353601e2018-02-16 14:29:42 -05004783 /*
4784 * The fi_had_conflict and nfs_get_existing_delegation checks
4785 * here are just optimizations; we'll need to recheck them at
4786 * the end:
4787 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004788 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004789 return ERR_PTR(-EAGAIN);
4790
J. Bruce Fields353601e2018-02-16 14:29:42 -05004791 filp = find_readable_file(fp);
4792 if (!filp) {
4793 /* We should always have a readable file here */
4794 WARN_ON_ONCE(1);
4795 return ERR_PTR(-EBADF);
4796 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004797 spin_lock(&state_lock);
4798 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004799 if (nfs4_delegation_exists(clp, fp))
4800 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004801 else if (!fp->fi_deleg_file) {
4802 fp->fi_deleg_file = filp;
4803 /* increment early to prevent fi_deleg_file from being
4804 * cleared */
4805 fp->fi_delegees = 1;
4806 filp = NULL;
4807 } else
4808 fp->fi_delegees++;
4809 spin_unlock(&fp->fi_lock);
4810 spin_unlock(&state_lock);
4811 if (filp)
4812 fput(filp);
4813 if (status)
4814 return ERR_PTR(status);
4815
4816 status = -ENOMEM;
4817 dp = alloc_init_deleg(clp, fp, fh, odstate);
4818 if (!dp)
4819 goto out_delegees;
4820
4821 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4822 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04004823 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004824
4825 status = vfs_setlease(fp->fi_deleg_file, fl->fl_type, &fl, NULL);
4826 if (fl)
4827 locks_free_lock(fl);
4828 if (status)
4829 goto out_clnt_odstate;
4830
4831 spin_lock(&state_lock);
4832 spin_lock(&fp->fi_lock);
4833 if (fp->fi_had_conflict)
4834 status = -EAGAIN;
4835 else
4836 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04004837 spin_unlock(&fp->fi_lock);
4838 spin_unlock(&state_lock);
4839
4840 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04004841 goto out_unlock;
4842
Jeff Layton0b266932014-07-25 07:34:25 -04004843 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04004844out_unlock:
4845 vfs_setlease(fp->fi_deleg_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004846out_clnt_odstate:
4847 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004848 nfs4_put_stid(&dp->dl_stid);
4849out_delegees:
4850 put_deleg_file(fp);
4851 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004852}
4853
Benny Halevy4aa89132012-02-21 14:16:44 -08004854static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4855{
4856 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4857 if (status == -EAGAIN)
4858 open->op_why_no_deleg = WND4_CONTENTION;
4859 else {
4860 open->op_why_no_deleg = WND4_RESOURCE;
4861 switch (open->op_deleg_want) {
4862 case NFS4_SHARE_WANT_READ_DELEG:
4863 case NFS4_SHARE_WANT_WRITE_DELEG:
4864 case NFS4_SHARE_WANT_ANY_DELEG:
4865 break;
4866 case NFS4_SHARE_WANT_CANCEL:
4867 open->op_why_no_deleg = WND4_CANCELLED;
4868 break;
4869 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004870 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004871 }
4872 }
4873}
4874
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875/*
4876 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004877 *
4878 * Note we don't support write delegations, and won't until the vfs has
4879 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 */
4881static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004882nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4883 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884{
4885 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004886 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4887 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004888 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004889 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004891 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004892 open->op_recall = 0;
4893 switch (open->op_claim_type) {
4894 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004895 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004896 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004897 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4898 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004899 break;
4900 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004901 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004902 /*
4903 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004904 * had the chance to reclaim theirs, *and* until
4905 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004906 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004907 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004908 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004909 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004910 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004911 /*
4912 * Also, if the file was opened for write or
4913 * create, there's a good chance the client's
4914 * about to write to it, resulting in an
4915 * immediate recall (since we don't support
4916 * write delegations):
4917 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004918 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004919 goto out_no_deleg;
4920 if (open->op_create == NFS4_OPEN_CREATE)
4921 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004922 break;
4923 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004924 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004925 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004926 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004927 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004928 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004930 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004932 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004933 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004934 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004935 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004936 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004937out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004938 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4939 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004940 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004941 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004942 open->op_recall = 1;
4943 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004944
4945 /* 4.1 client asking for a delegation? */
4946 if (open->op_deleg_want)
4947 nfsd4_open_deleg_none_ext(open, status);
4948 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949}
4950
Benny Halevye27f49c2012-02-21 14:16:54 -08004951static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4952 struct nfs4_delegation *dp)
4953{
4954 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4955 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4956 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4957 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4958 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4959 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4960 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4961 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4962 }
4963 /* Otherwise the client must be confused wanting a delegation
4964 * it already has, therefore we don't return
4965 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4966 */
4967}
4968
Al Virob37ad282006-10-19 23:28:59 -07004969__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4971{
Andy Adamson66689582009-04-03 08:28:45 +03004972 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004973 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004975 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004976 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004977 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004978 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 /*
4981 * Lookup file; if found, lookup stateid and check open request,
4982 * and check for delegations in the process of being recalled.
4983 * If not found, create the nfs4_file struct
4984 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004985 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004986 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004987 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004988 if (status)
4989 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004990 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004992 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004993 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004994 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004995 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 }
4997
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004998 if (!stp) {
4999 stp = init_open_stateid(fp, open);
5000 if (!open->op_stp)
5001 new_stp = true;
5002 }
5003
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 /*
5005 * OPEN the file, or upgrade an existing OPEN.
5006 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005007 *
5008 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005010 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005012 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005013 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005014 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005018 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5019 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005020 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005021 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005022 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005023 goto out;
5024 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005025
5026 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5027 open->op_odstate);
5028 if (stp->st_clnt_odstate == open->op_odstate)
5029 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005031
Jeff Layton9767feb2015-10-01 09:05:50 -04005032 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005033 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Benny Halevyd24433c2012-02-16 20:57:17 +02005035 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005036 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5037 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5038 open->op_why_no_deleg = WND4_NOT_WANTED;
5039 goto nodeleg;
5040 }
5041 }
5042
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 /*
5044 * Attempt to hand out a delegation. No error return, because the
5045 * OPEN succeeds even if we fail.
5046 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005047 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005048nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 status = nfs_ok;
5050
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005051 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005052 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005054 /* 4.1 client trying to upgrade/downgrade delegation? */
5055 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005056 open->op_deleg_want)
5057 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005058
NeilBrown13cd2182005-06-23 22:03:10 -07005059 if (fp)
5060 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005061 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005062 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 /*
5064 * To finish the open response, we just need to set the rflags.
5065 */
5066 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005067 if (nfsd4_has_session(&resp->cstate))
5068 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5069 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005071
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005072 if (dp)
5073 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005074 if (stp)
5075 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
5077 return status;
5078}
5079
Jeff Layton58fb12e2014-07-29 21:34:27 -04005080void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005081 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005082{
5083 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005084 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005085
Jeff Laytond3134b12014-07-29 21:34:32 -04005086 nfsd4_cstate_assign_replay(cstate, so);
5087 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005088 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005089 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005090 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005091 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005092 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005093 if (open->op_odstate)
5094 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005095}
5096
Al Virob37ad282006-10-19 23:28:59 -07005097__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005098nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005099 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005101 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005103 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005104 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 dprintk("process_renew(%08x/%08x): starting\n",
5107 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04005108 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005109 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005111 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07005113 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005114 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 goto out;
5116 status = nfs_ok;
5117out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 return status;
5119}
5120
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005121void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005122nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005123{
Jeff Layton33dcc482012-04-10 11:08:48 -04005124 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005125 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005126 return;
5127
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005128 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005129 /*
5130 * If the server goes down again right now, an NFSv4
5131 * client will still be allowed to reclaim after it comes back up,
5132 * even if it hasn't yet had a chance to reclaim state this time.
5133 *
5134 */
Jeff Layton919b8042014-09-12 16:40:20 -04005135 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005136 /*
5137 * At this point, NFSv4 clients can still reclaim. But if the
5138 * server crashes, any that have not yet reclaimed will be out
5139 * of luck on the next boot.
5140 *
5141 * (NFSv4.1+ clients are considered to have reclaimed once they
5142 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5143 * have reclaimed after their first OPEN.)
5144 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005145 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005146 /*
5147 * At this point, and once lockd and/or any other containers
5148 * exit their grace period, further reclaims will fail and
5149 * regular locking can resume.
5150 */
NeilBrowna76b4312005-06-23 22:04:01 -07005151}
5152
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005153/*
5154 * If we've waited a lease period but there are still clients trying to
5155 * reclaim, wait a little longer to give them a chance to finish.
5156 */
5157static bool clients_still_reclaiming(struct nfsd_net *nn)
5158{
5159 unsigned long now = get_seconds();
5160 unsigned long double_grace_period_end = nn->boot_time +
5161 2 * nn->nfsd4_lease;
5162
Scott Mayhew362063a2019-03-26 18:06:28 -04005163 if (nn->track_reclaim_completes &&
5164 atomic_read(&nn->nr_reclaim_complete) ==
5165 nn->reclaim_str_hashtbl_size)
5166 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005167 if (!nn->somebody_reclaimed)
5168 return false;
5169 nn->somebody_reclaimed = false;
5170 /*
5171 * If we've given them *two* lease times to reclaim, and they're
5172 * still not done, give up:
5173 */
5174 if (time_after(now, double_grace_period_end))
5175 return false;
5176 return true;
5177}
5178
NeilBrownfd39ca92005-06-23 22:04:03 -07005179static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005180nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181{
5182 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005183 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005185 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005186 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03005188 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04005189 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005192
5193 if (clients_still_reclaiming(nn)) {
5194 new_timeo = 0;
5195 goto out;
5196 }
Scott Mayhew362063a2019-03-26 18:06:28 -04005197 dprintk("NFSD: end of grace period\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005198 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005199 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005200 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005201 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 clp = list_entry(pos, struct nfs4_client, cl_lru);
5203 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
5204 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04005205 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 break;
5207 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04005208 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03005209 dprintk("NFSD: client in use (clientid %08x)\n",
5210 clp->cl_clientid.cl_id);
5211 continue;
5212 }
Trond Myklebust4864af92014-07-30 08:27:03 -04005213 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005214 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005215 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005216 list_for_each_safe(pos, next, &reaplist) {
5217 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 dprintk("NFSD: purging unused client (clientid %08x)\n",
5219 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04005220 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 expire_client(clp);
5222 }
Benny Halevycdc975052014-05-30 09:09:30 -04005223 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005224 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5226 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005227 t = dp->dl_time - cutoff;
5228 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 break;
5230 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005231 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005232 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 }
Benny Halevycdc975052014-05-30 09:09:30 -04005234 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005235 while (!list_empty(&reaplist)) {
5236 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5237 dl_recall_lru);
5238 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005239 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 }
Jeff Layton217526e2014-07-30 08:27:11 -04005241
5242 spin_lock(&nn->client_lock);
5243 while (!list_empty(&nn->close_lru)) {
5244 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5245 oo_close_lru);
5246 if (time_after((unsigned long)oo->oo_time,
5247 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005248 t = oo->oo_time - cutoff;
5249 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 break;
5251 }
Jeff Layton217526e2014-07-30 08:27:11 -04005252 list_del_init(&oo->oo_close_lru);
5253 stp = oo->oo_last_closed_stid;
5254 oo->oo_last_closed_stid = NULL;
5255 spin_unlock(&nn->client_lock);
5256 nfs4_put_stid(&stp->st_stid);
5257 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 }
Jeff Layton217526e2014-07-30 08:27:11 -04005259 spin_unlock(&nn->client_lock);
5260
Jeff Layton7919d0a2016-09-16 16:28:25 -04005261 /*
5262 * It's possible for a client to try and acquire an already held lock
5263 * that is being held for a long time, and then lose interest in it.
5264 * So, we clean out any un-revisited request after a lease period
5265 * under the assumption that the client is no longer interested.
5266 *
5267 * RFC5661, sec. 9.6 states that the client must not rely on getting
5268 * notifications and must continue to poll for locks, even when the
5269 * server supports them. Thus this shouldn't lead to clients blocking
5270 * indefinitely once the lock does become free.
5271 */
5272 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005273 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005274 while (!list_empty(&nn->blocked_locks_lru)) {
5275 nbl = list_first_entry(&nn->blocked_locks_lru,
5276 struct nfsd4_blocked_lock, nbl_lru);
5277 if (time_after((unsigned long)nbl->nbl_time,
5278 (unsigned long)cutoff)) {
5279 t = nbl->nbl_time - cutoff;
5280 new_timeo = min(new_timeo, t);
5281 break;
5282 }
5283 list_move(&nbl->nbl_lru, &reaplist);
5284 list_del_init(&nbl->nbl_list);
5285 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005286 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005287
5288 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005289 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005290 struct nfsd4_blocked_lock, nbl_lru);
5291 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005292 free_blocked_lock(nbl);
5293 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005294out:
Jeff Laytona832e7a2014-05-30 09:09:26 -04005295 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04005296 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297}
5298
Harvey Harrisona254b242008-02-20 12:49:00 -08005299static struct workqueue_struct *laundry_wq;
5300static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005301
5302static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005303laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304{
5305 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005306 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005307 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5308 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005310 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005312 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313}
5314
Jeff Layton8fcd4612015-07-30 06:57:46 -04005315static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005316{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005317 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005318 return nfserr_bad_stateid;
5319 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320}
5321
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005323access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005325 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5326 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5327 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328}
5329
5330static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005331access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005333 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5334 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335}
5336
5337static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005338__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339{
Al Virob37ad282006-10-19 23:28:59 -07005340 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
J. Bruce Fields02921912010-07-29 15:16:59 -04005342 /* For lock stateid's, we test the parent open, not the lock: */
5343 if (stp->st_openstp)
5344 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005345 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005347 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 goto out;
5349 status = nfs_ok;
5350out:
5351 return status;
5352}
5353
Al Virob37ad282006-10-19 23:28:59 -07005354static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005355check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005357 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005359 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005360 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 * conflicting state; so we must wait out the grace period. */
5362 return nfserr_grace;
5363 } else if (flags & WR_STATE)
5364 return nfs4_share_conflict(current_fh,
5365 NFS4_SHARE_DENY_WRITE);
5366 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5367 return nfs4_share_conflict(current_fh,
5368 NFS4_SHARE_DENY_READ);
5369}
5370
5371/*
5372 * Allow READ/WRITE during grace period on recovered state only for files
5373 * that are not able to provide mandatory locking.
5374 */
5375static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005376grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005378 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379}
5380
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005381static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005382{
Andy Adamson66689582009-04-03 08:28:45 +03005383 /*
5384 * When sessions are used the stateid generation number is ignored
5385 * when it is zero.
5386 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005387 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005388 return nfs_ok;
5389
5390 if (in->si_generation == ref->si_generation)
5391 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005392
J. Bruce Fields0836f582008-01-26 19:08:12 -05005393 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005394 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005395 return nfserr_bad_stateid;
5396 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005397 * However, we could see a stateid from the past, even from a
5398 * non-buggy client. For example, if the client sends a lock
5399 * while some IO is outstanding, the lock may bump si_generation
5400 * while the IO is still in flight. The client could avoid that
5401 * situation by waiting for responses on all the IO requests,
5402 * but better performance may result in retrying IO that
5403 * receives an old_stateid error if requests are rarely
5404 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005405 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005406 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005407}
5408
Trond Myklebust03da3162017-11-03 08:00:16 -04005409static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5410{
5411 __be32 ret;
5412
5413 spin_lock(&s->sc_lock);
5414 ret = nfsd4_verify_open_stid(s);
5415 if (ret == nfs_ok)
5416 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5417 spin_unlock(&s->sc_lock);
5418 return ret;
5419}
5420
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005421static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5422{
5423 if (ols->st_stateowner->so_is_open_owner &&
5424 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5425 return nfserr_bad_stateid;
5426 return nfs_ok;
5427}
5428
Chuck Lever7df302f2012-05-29 13:56:37 -04005429static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005430{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005431 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005432 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005433
Andrew Elbleae254dac2017-11-09 13:41:10 -05005434 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5435 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005436 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005437 /* Client debugging aid. */
5438 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5439 char addr_str[INET6_ADDRSTRLEN];
5440 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5441 sizeof(addr_str));
5442 pr_warn_ratelimited("NFSD: client %s testing state ID "
5443 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005444 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005445 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005446 spin_lock(&cl->cl_lock);
5447 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005448 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005449 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005450 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005451 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005452 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005453 switch (s->sc_type) {
5454 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005455 status = nfs_ok;
5456 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005457 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005458 status = nfserr_deleg_revoked;
5459 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005460 case NFS4_OPEN_STID:
5461 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005462 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005463 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005464 default:
5465 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005466 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04005467 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005468 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005469 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005470 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005471out_unlock:
5472 spin_unlock(&cl->cl_lock);
5473 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005474}
5475
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005476__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005477nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5478 stateid_t *stateid, unsigned char typemask,
5479 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005480{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005481 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005482 bool return_revoked = false;
5483
5484 /*
5485 * only return revoked delegations if explicitly asked.
5486 * otherwise we report revoked or bad_stateid status.
5487 */
5488 if (typemask & NFS4_REVOKED_DELEG_STID)
5489 return_revoked = true;
5490 else if (typemask & NFS4_DELEG_STID)
5491 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005492
Andrew Elbleae254dac2017-11-09 13:41:10 -05005493 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5494 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005495 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005496 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005497 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005498 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005499 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005500 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005501 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005502 if (status)
5503 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005504 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005505 if (!*s)
5506 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005507 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5508 nfs4_put_stid(*s);
5509 if (cstate->minorversion)
5510 return nfserr_deleg_revoked;
5511 return nfserr_bad_stateid;
5512 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005513 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005514}
5515
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005516static struct file *
5517nfs4_find_file(struct nfs4_stid *s, int flags)
5518{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005519 if (!s)
5520 return NULL;
5521
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005522 switch (s->sc_type) {
5523 case NFS4_DELEG_STID:
5524 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5525 return NULL;
5526 return get_file(s->sc_file->fi_deleg_file);
5527 case NFS4_OPEN_STID:
5528 case NFS4_LOCK_STID:
5529 if (flags & RD_STATE)
5530 return find_readable_file(s->sc_file);
5531 else
5532 return find_writeable_file(s->sc_file);
5533 break;
5534 }
5535
5536 return NULL;
5537}
5538
5539static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005540nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005541{
5542 __be32 status;
5543
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005544 status = nfsd4_check_openowner_confirmed(ols);
5545 if (status)
5546 return status;
5547 return nfs4_check_openmode(ols, flags);
5548}
5549
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005550static __be32
5551nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5552 struct file **filpp, bool *tmp_file, int flags)
5553{
5554 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5555 struct file *file;
5556 __be32 status;
5557
5558 file = nfs4_find_file(s, flags);
5559 if (file) {
5560 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5561 acc | NFSD_MAY_OWNER_OVERRIDE);
5562 if (status) {
5563 fput(file);
5564 return status;
5565 }
5566
5567 *filpp = file;
5568 } else {
5569 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5570 if (status)
5571 return status;
5572
5573 if (tmp_file)
5574 *tmp_file = true;
5575 }
5576
5577 return 0;
5578}
5579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005581 * Checks for stateid operations
5582 */
Al Virob37ad282006-10-19 23:28:59 -07005583__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005584nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005585 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5586 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005588 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005589 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005590 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005591 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005592 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 if (filpp)
5595 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005596 if (tmp_file)
5597 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005599 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 return nfserr_grace;
5601
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005602 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5603 status = check_special_stateids(net, fhp, stateid, flags);
5604 goto done;
5605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005607 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005608 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005609 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005610 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005611 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005612 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005613 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005614 if (status)
5615 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005616
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005617 switch (s->sc_type) {
5618 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005619 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005620 break;
5621 case NFS4_OPEN_STID:
5622 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005623 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005624 break;
5625 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005626 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005627 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005629 if (status)
5630 goto out;
5631 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005632
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005633done:
5634 if (!status && filpp)
5635 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005637 if (s)
5638 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 return status;
5640}
5641
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005642/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005643 * Test if the stateid is valid
5644 */
5645__be32
5646nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005647 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005648{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005649 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005650 struct nfsd4_test_stateid_id *stateid;
5651 struct nfs4_client *cl = cstate->session->se_client;
5652
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005653 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005654 stateid->ts_id_status =
5655 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005656
Bryan Schumaker17456802011-07-13 10:50:48 -04005657 return nfs_ok;
5658}
5659
Chuck Lever42691392016-08-11 10:37:30 -04005660static __be32
5661nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5662{
5663 struct nfs4_ol_stateid *stp = openlockstateid(s);
5664 __be32 ret;
5665
Trond Myklebust659aefb2017-11-03 08:00:13 -04005666 ret = nfsd4_lock_ol_stateid(stp);
5667 if (ret)
5668 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005669
5670 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5671 if (ret)
5672 goto out;
5673
5674 ret = nfserr_locks_held;
5675 if (check_for_locks(stp->st_stid.sc_file,
5676 lockowner(stp->st_stateowner)))
5677 goto out;
5678
5679 release_lock_stateid(stp);
5680 ret = nfs_ok;
5681
5682out:
5683 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005684out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005685 nfs4_put_stid(s);
5686 return ret;
5687}
5688
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005689__be32
5690nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005691 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005692{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005693 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005694 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005695 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005696 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005697 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005698 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005699
Jeff Layton1af71cc2014-07-29 21:34:14 -04005700 spin_lock(&cl->cl_lock);
5701 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005702 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005703 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005704 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005705 switch (s->sc_type) {
5706 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005707 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005708 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005709 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005710 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5711 if (ret)
5712 break;
5713 ret = nfserr_locks_held;
5714 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005715 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005716 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005717 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005718 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005719 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005720 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005721 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005722 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005723 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005724 list_del_init(&dp->dl_recall_lru);
5725 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005726 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005727 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005728 goto out;
5729 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005730 }
Trond Myklebust03da3162017-11-03 08:00:16 -04005731 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005732out_unlock:
5733 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005734out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005735 return ret;
5736}
5737
NeilBrown4c4cd222005-07-07 17:59:27 -07005738static inline int
5739setlkflg (int type)
5740{
5741 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5742 RD_STATE : WR_STATE;
5743}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005745static __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 -04005746{
5747 struct svc_fh *current_fh = &cstate->current_fh;
5748 struct nfs4_stateowner *sop = stp->st_stateowner;
5749 __be32 status;
5750
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005751 status = nfsd4_check_seqid(cstate, sop, seqid);
5752 if (status)
5753 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04005754 status = nfsd4_lock_ol_stateid(stp);
5755 if (status != nfs_ok)
5756 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005757 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005758 if (status == nfs_ok)
5759 status = nfs4_check_fh(current_fh, &stp->st_stid);
5760 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005761 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005762 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005763}
5764
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765/*
5766 * Checks for sequence id mutating operations.
5767 */
Al Virob37ad282006-10-19 23:28:59 -07005768static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005769nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005770 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005771 struct nfs4_ol_stateid **stpp,
5772 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005774 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005775 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005776 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005778 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5779 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005780
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005782 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005783 if (status)
5784 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005785 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005786 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005788 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005789 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005790 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005791 else
5792 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005793 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005794}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005795
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005796static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5797 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005798{
5799 __be32 status;
5800 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005801 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005803 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005804 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005805 if (status)
5806 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005807 oo = openowner(stp->st_stateowner);
5808 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005809 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005810 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005811 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005812 }
5813 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005814 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815}
5816
Al Virob37ad282006-10-19 23:28:59 -07005817__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005818nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005819 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005821 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07005822 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005823 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005824 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005825 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
Al Viroa6a9f182013-09-16 10:57:01 -04005827 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5828 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005830 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005831 if (status)
5832 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005834 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005835 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005836 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005837 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005838 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005839 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005840 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005841 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005842 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005843 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005844 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005845 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005846 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005847 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005848 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005849 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005850
Jeff Layton2a4317c2012-03-21 16:42:43 -04005851 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005852 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005853put_stateid:
5854 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005856 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 return status;
5858}
5859
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005860static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005862 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005863 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005864 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005865 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005866}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005867
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005868static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5869{
5870 switch (to_access) {
5871 case NFS4_SHARE_ACCESS_READ:
5872 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5873 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5874 break;
5875 case NFS4_SHARE_ACCESS_WRITE:
5876 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5877 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5878 break;
5879 case NFS4_SHARE_ACCESS_BOTH:
5880 break;
5881 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005882 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 }
5884}
5885
Al Virob37ad282006-10-19 23:28:59 -07005886__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005887nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005888 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005890 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07005891 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005892 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005893 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
Al Viroa6a9f182013-09-16 10:57:01 -04005895 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5896 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005898 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005899 if (od->od_deleg_want)
5900 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5901 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005903 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005904 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005905 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005908 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005909 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005911 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005913 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005914 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005916 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005918 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005919 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005920 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005922put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005923 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005924 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005926 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 return status;
5928}
5929
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005930static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5931{
Trond Myklebustacf92952014-06-30 11:48:37 -04005932 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005933 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005934 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005935
Jeff Layton2c41beb2014-07-29 21:34:41 -04005936 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005937 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005938
Jeff Laytond83017f2014-07-29 21:34:42 -04005939 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005940 if (unhashed)
5941 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005942 spin_unlock(&clp->cl_lock);
5943 free_ol_stateid_reaplist(&reaplist);
5944 } else {
5945 spin_unlock(&clp->cl_lock);
5946 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005947 if (unhashed)
5948 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005949 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005950}
5951
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952/*
5953 * nfs4_unlock_state() called after encode
5954 */
Al Virob37ad282006-10-19 23:28:59 -07005955__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005956nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005957 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005959 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07005960 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005961 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005962 struct net *net = SVC_NET(rqstp);
5963 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964
Al Viroa6a9f182013-09-16 10:57:01 -04005965 dprintk("NFSD: nfsd4_close on file %pd\n",
5966 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005968 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5969 &close->cl_stateid,
5970 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005971 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005972 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005973 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005975
5976 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005977
5978 /*
5979 * Technically we don't _really_ have to increment or copy it, since
5980 * it should just be gone after this operation and we clobber the
5981 * copied value below, but we continue to do so here just to ensure
5982 * that racing ops see that there was a state change.
5983 */
Jeff Layton9767feb2015-10-01 09:05:50 -04005984 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005986 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005987 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005988
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005989 /* v4.1+ suggests that we send a special stateid in here, since the
5990 * clients should just ignore this anyway. Since this is not useful
5991 * for v4.0 clients either, we set it to the special close_stateid
5992 * universally.
5993 *
5994 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
5995 */
5996 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04005997
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005998 /* put reference from nfs4_preprocess_seqid_op */
5999 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 return status;
6002}
6003
Al Virob37ad282006-10-19 23:28:59 -07006004__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006005nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006006 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006008 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006009 struct nfs4_delegation *dp;
6010 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006011 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07006012 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006013 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006015 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006016 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006018 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006019 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006020 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006021 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04006022 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006023 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04006024 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006025
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006026 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006027put_stateid:
6028 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029out:
6030 return status;
6031}
6032
Benny Halevy87df4de2008-12-15 19:42:03 +02006033static inline u64
6034end_offset(u64 start, u64 len)
6035{
6036 u64 end;
6037
6038 end = start + len;
6039 return end >= start ? end: NFS4_MAX_UINT64;
6040}
6041
6042/* last octet in a range */
6043static inline u64
6044last_byte_offset(u64 start, u64 len)
6045{
6046 u64 end;
6047
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006048 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006049 end = start + len;
6050 return end > start ? end - 1: NFS4_MAX_UINT64;
6051}
6052
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053/*
6054 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6055 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6056 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6057 * locking, this prevents us from being completely protocol-compliant. The
6058 * real solution to this problem is to start using unsigned file offsets in
6059 * the VFS, but this is a very deep change!
6060 */
6061static inline void
6062nfs4_transform_lock_offset(struct file_lock *lock)
6063{
6064 if (lock->fl_start < 0)
6065 lock->fl_start = OFFSET_MAX;
6066 if (lock->fl_end < 0)
6067 lock->fl_end = OFFSET_MAX;
6068}
6069
Jeff Laytoncae80b32015-04-03 09:04:04 -04006070static fl_owner_t
6071nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006072{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006073 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6074
6075 nfs4_get_stateowner(&lo->lo_owner);
6076 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006077}
6078
Jeff Laytoncae80b32015-04-03 09:04:04 -04006079static void
6080nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006081{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006082 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006083
Jeff Laytoncae80b32015-04-03 09:04:04 -04006084 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006085 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006086}
6087
Jeff Layton76d348f2016-09-16 16:28:24 -04006088static void
6089nfsd4_lm_notify(struct file_lock *fl)
6090{
6091 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6092 struct net *net = lo->lo_owner.so_client->net;
6093 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6094 struct nfsd4_blocked_lock *nbl = container_of(fl,
6095 struct nfsd4_blocked_lock, nbl_lock);
6096 bool queue = false;
6097
Jeff Layton7919d0a2016-09-16 16:28:25 -04006098 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006099 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006100 if (!list_empty(&nbl->nbl_list)) {
6101 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006102 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006103 queue = true;
6104 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006105 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006106
6107 if (queue)
6108 nfsd4_run_cb(&nbl->nbl_cb);
6109}
6110
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006111static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006112 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006113 .lm_get_owner = nfsd4_fl_get_owner,
6114 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006115};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116
6117static inline void
6118nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6119{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006120 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121
NeilBrownd5b90262006-04-10 22:55:22 -07006122 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006123 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04006124 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6125 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006126 if (!deny->ld_owner.data)
6127 /* We just don't care that much */
6128 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006129 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006130 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006131nevermind:
6132 deny->ld_owner.len = 0;
6133 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006134 deny->ld_clientid.cl_boot = 0;
6135 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 }
6137 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006138 deny->ld_length = NFS4_MAX_UINT64;
6139 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6141 deny->ld_type = NFS4_READ_LT;
6142 if (fl->fl_type != F_RDLCK)
6143 deny->ld_type = NFS4_WRITE_LT;
6144}
6145
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006146static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006147find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006149 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006150 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151
Trond Myklebust0a880a22014-07-30 08:27:10 -04006152 lockdep_assert_held(&clp->cl_lock);
6153
Trond Myklebustd4f04892014-07-29 21:34:36 -04006154 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6155 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006156 if (so->so_is_open_owner)
6157 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006158 if (same_owner_str(so, owner))
6159 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 }
6161 return NULL;
6162}
6163
Trond Myklebustc58c6612014-07-29 21:34:35 -04006164static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006165find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006166{
6167 struct nfs4_lockowner *lo;
6168
Trond Myklebustd4f04892014-07-29 21:34:36 -04006169 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006170 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006171 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006172 return lo;
6173}
6174
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006175static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6176{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006177 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006178}
6179
Jeff Layton6b180f02014-07-29 21:34:26 -04006180static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6181{
6182 struct nfs4_lockowner *lo = lockowner(sop);
6183
6184 kmem_cache_free(lockowner_slab, lo);
6185}
6186
6187static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006188 .so_unhash = nfs4_unhash_lockowner,
6189 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006190};
6191
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192/*
6193 * Alloc a lock owner structure.
6194 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006195 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006197 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006199static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006200alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6201 struct nfs4_ol_stateid *open_stp,
6202 struct nfsd4_lock *lock)
6203{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006204 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006206 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6207 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006209 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006210 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6211 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006212 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006213 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006214 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006215 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006216 if (ret == NULL) {
6217 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006218 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006219 ret = lo;
6220 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006221 nfs4_free_stateowner(&lo->lo_owner);
6222
Trond Myklebustd4f04892014-07-29 21:34:36 -04006223 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006224 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225}
6226
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006227static struct nfs4_ol_stateid *
6228find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
6229{
6230 struct nfs4_ol_stateid *lst;
6231 struct nfs4_client *clp = lo->lo_owner.so_client;
6232
6233 lockdep_assert_held(&clp->cl_lock);
6234
6235 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
6236 if (lst->st_stid.sc_type != NFS4_LOCK_STID)
6237 continue;
6238 if (lst->st_stid.sc_file == fp) {
6239 refcount_inc(&lst->st_stid.sc_count);
6240 return lst;
6241 }
6242 }
6243 return NULL;
6244}
6245
Trond Myklebustbeeca192017-11-03 08:00:14 -04006246static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006247init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6248 struct nfs4_file *fp, struct inode *inode,
6249 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006251 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006252 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253
Trond Myklebustbeeca192017-11-03 08:00:14 -04006254 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006255 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006256retry:
6257 spin_lock(&clp->cl_lock);
6258 spin_lock(&fp->fi_lock);
6259 retstp = find_lock_stateid(lo, fp);
6260 if (retstp)
6261 goto out_unlock;
Jeff Layton356a95e2014-07-29 21:34:13 -04006262
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006263 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006264 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006265 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006266 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006267 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006268 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006270 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006271 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006272 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006273 list_add(&stp->st_perfile, &fp->fi_stateids);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006274out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04006275 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006276 spin_unlock(&clp->cl_lock);
6277 if (retstp) {
6278 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6279 nfs4_put_stid(&retstp->st_stid);
6280 goto retry;
6281 }
6282 /* To keep mutex tracking happy */
6283 mutex_unlock(&stp->st_mutex);
6284 stp = retstp;
6285 }
6286 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287}
6288
Jeff Laytonc53530d2014-06-30 11:48:39 -04006289static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006290find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6291 struct inode *inode, struct nfs4_ol_stateid *ost,
6292 bool *new)
6293{
6294 struct nfs4_stid *ns = NULL;
6295 struct nfs4_ol_stateid *lst;
6296 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6297 struct nfs4_client *clp = oo->oo_owner.so_client;
6298
Trond Myklebustbeeca192017-11-03 08:00:14 -04006299 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006300 spin_lock(&clp->cl_lock);
6301 lst = find_lock_stateid(lo, fi);
Jeff Layton356a95e2014-07-29 21:34:13 -04006302 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006303 if (lst != NULL) {
6304 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6305 goto out;
6306 nfs4_put_stid(&lst->st_stid);
6307 }
6308 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6309 if (ns == NULL)
6310 return NULL;
6311
6312 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6313 if (lst == openlockstateid(ns))
6314 *new = true;
6315 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006316 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006317out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006318 return lst;
6319}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006320
NeilBrownfd39ca92005-06-23 22:04:03 -07006321static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322check_lock_length(u64 offset, u64 length)
6323{
Kinglong Meee7969312015-07-13 17:34:19 +08006324 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6325 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326}
6327
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006328static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006329{
Trond Myklebust11b91642014-07-29 21:34:08 -04006330 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006331
Jeff Layton7214e862014-07-10 14:07:33 -04006332 lockdep_assert_held(&fp->fi_lock);
6333
Jeff Layton82c5ff12012-05-11 09:45:13 -04006334 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006335 return;
Jeff Layton12659652014-07-10 14:07:28 -04006336 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006337 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006338}
6339
Jeff Layton356a95e2014-07-29 21:34:13 -04006340static __be32
6341lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6342 struct nfs4_ol_stateid *ost,
6343 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006344 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006345{
Jeff Layton5db1c032014-07-29 21:34:28 -04006346 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006347 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006348 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6349 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006350 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006351 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006352 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006353 unsigned int strhashval;
6354
Kinglong Meec8623992015-07-13 17:35:37 +08006355 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006356 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006357 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006358 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6359 if (lo == NULL)
6360 return nfserr_jukebox;
6361 } else {
6362 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006363 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006364 if (!cstate->minorversion &&
6365 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006366 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006367 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006368
Jeff Laytondd257932016-08-11 10:37:39 -04006369 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6370 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006371 status = nfserr_jukebox;
6372 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006373 }
Jeff Laytondd257932016-08-11 10:37:39 -04006374
Jeff Layton5db1c032014-07-29 21:34:28 -04006375 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006376 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006377out:
6378 nfs4_put_stateowner(&lo->lo_owner);
6379 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006380}
6381
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382/*
6383 * LOCK operation
6384 */
Al Virob37ad282006-10-19 23:28:59 -07006385__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006386nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006387 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006389 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006390 struct nfs4_openowner *open_sop = NULL;
6391 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006392 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006393 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006394 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04006395 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006396 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006397 struct file_lock *file_lock = NULL;
6398 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006399 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006400 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006401 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006402 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006403 unsigned char fl_type;
6404 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006405 struct net *net = SVC_NET(rqstp);
6406 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407
6408 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6409 (long long) lock->lk_offset,
6410 (long long) lock->lk_length);
6411
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 if (check_lock_length(lock->lk_offset, lock->lk_length))
6413 return nfserr_inval;
6414
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006415 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006416 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006417 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6418 return status;
6419 }
6420
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006422 if (nfsd4_has_session(cstate))
6423 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006424 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006425 &cstate->session->se_client->cl_clientid,
6426 sizeof(clientid_t));
6427
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006429 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006433 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 lock->lk_new_open_seqid,
6435 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006436 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006437 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006439 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006440 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006441 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006442 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006443 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006444 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006445 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006446 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006447 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006448 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006449 lock->lk_old_lock_seqid,
6450 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006451 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006452 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006453 if (status)
6454 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006455 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006457 lkflg = setlkflg(lock->lk_type);
6458 status = nfs4_check_openmode(lock_stp, lkflg);
6459 if (status)
6460 goto out;
6461
NeilBrown0dd395d2005-07-07 17:59:15 -07006462 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006463 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006464 goto out;
6465 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006466 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006467 goto out;
6468
Trond Myklebust11b91642014-07-29 21:34:08 -04006469 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006472 if (nfsd4_has_session(cstate))
6473 fl_flags |= FL_SLEEP;
6474 /* Fallthrough */
6475 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006476 spin_lock(&fp->fi_lock);
6477 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006478 if (filp)
6479 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006480 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006481 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006482 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006484 if (nfsd4_has_session(cstate))
6485 fl_flags |= FL_SLEEP;
6486 /* Fallthrough */
6487 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006488 spin_lock(&fp->fi_lock);
6489 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006490 if (filp)
6491 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006492 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006493 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006494 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 default:
6496 status = nfserr_inval;
6497 goto out;
6498 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006499
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006500 if (!filp) {
6501 status = nfserr_openmode;
6502 goto out;
6503 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006504
Jeff Layton76d348f2016-09-16 16:28:24 -04006505 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6506 if (!nbl) {
6507 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6508 status = nfserr_jukebox;
6509 goto out;
6510 }
6511
6512 file_lock = &nbl->nbl_lock;
6513 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006514 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006515 file_lock->fl_pid = current->tgid;
6516 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04006517 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006518 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6519 file_lock->fl_start = lock->lk_offset;
6520 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6521 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522
Jeff Layton21179d82012-08-21 08:03:32 -04006523 conflock = locks_alloc_lock();
6524 if (!conflock) {
6525 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6526 status = nfserr_jukebox;
6527 goto out;
6528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529
Jeff Layton76d348f2016-09-16 16:28:24 -04006530 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04006531 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04006532 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006533 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006534 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006535 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006536 }
6537
Jeff Layton21179d82012-08-21 08:03:32 -04006538 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006539 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006541 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006542 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006543 if (lock->lk_reclaim)
6544 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006545 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006546 case FILE_LOCK_DEFERRED:
6547 nbl = NULL;
6548 /* Fallthrough */
6549 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006550 status = nfserr_denied;
6551 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006552 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006553 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006554 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006556 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006557 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006558 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006559 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006560 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006563 if (nbl) {
6564 /* dequeue it if we queued it before */
6565 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006566 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006567 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006568 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006569 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006570 }
6571 free_blocked_lock(nbl);
6572 }
Trond Myklebustde186432014-07-10 14:07:26 -04006573 if (filp)
6574 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04006575 if (lock_stp) {
6576 /* Bump seqid manually if the 4.0 replay owner is openowner */
6577 if (cstate->replay_owner &&
6578 cstate->replay_owner != &lock_sop->lo_owner &&
6579 seqid_mutating_err(ntohl(status)))
6580 lock_sop->lo_owner.so_seqid++;
6581
6582 /*
6583 * If this is a new, never-before-used stateid, and we are
6584 * returning an error, then just go ahead and release it.
6585 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006586 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006587 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006588
6589 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006590
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006591 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006592 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006593 if (open_stp)
6594 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006595 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006596 if (conflock)
6597 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 return status;
6599}
6600
6601/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006602 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6603 * so we do a temporary open here just to get an open file to pass to
6604 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6605 * inode operation.)
6606 */
Al Viro04da6e92012-04-13 00:00:04 -04006607static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006608{
6609 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04006610 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6611 if (!err) {
6612 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02006613 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04006614 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006615 return err;
6616}
6617
6618/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 * LOCKT operation
6620 */
Al Virob37ad282006-10-19 23:28:59 -07006621__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006622nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006623 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006625 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006626 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006627 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006628 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006629 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006631 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 return nfserr_grace;
6633
6634 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6635 return nfserr_inval;
6636
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006637 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04006638 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006639 if (status)
6640 goto out;
6641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006643 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645
Jeff Layton21179d82012-08-21 08:03:32 -04006646 file_lock = locks_alloc_lock();
6647 if (!file_lock) {
6648 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6649 status = nfserr_jukebox;
6650 goto out;
6651 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006652
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 switch (lockt->lt_type) {
6654 case NFS4_READ_LT:
6655 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006656 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006657 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 case NFS4_WRITE_LT:
6659 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006660 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006661 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006663 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006665 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 }
6667
Kinglong Meec8623992015-07-13 17:35:37 +08006668 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006669 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006670 file_lock->fl_owner = (fl_owner_t)lo;
6671 file_lock->fl_pid = current->tgid;
6672 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673
Jeff Layton21179d82012-08-21 08:03:32 -04006674 file_lock->fl_start = lockt->lt_offset;
6675 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676
Jeff Layton21179d82012-08-21 08:03:32 -04006677 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678
Jeff Layton21179d82012-08-21 08:03:32 -04006679 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006680 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006681 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006682
Jeff Layton21179d82012-08-21 08:03:32 -04006683 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006685 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 }
6687out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006688 if (lo)
6689 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006690 if (file_lock)
6691 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 return status;
6693}
6694
Al Virob37ad282006-10-19 23:28:59 -07006695__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006696nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006697 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006699 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006700 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006702 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006703 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006704 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006705 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6706
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6708 (long long) locku->lu_offset,
6709 (long long) locku->lu_length);
6710
6711 if (check_lock_length(locku->lu_offset, locku->lu_length))
6712 return nfserr_inval;
6713
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006714 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006715 &locku->lu_stateid, NFS4_LOCK_STID,
6716 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006717 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006719 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006720 if (!filp) {
6721 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006722 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006723 }
Jeff Layton21179d82012-08-21 08:03:32 -04006724 file_lock = locks_alloc_lock();
6725 if (!file_lock) {
6726 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6727 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006728 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006729 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006730
Jeff Layton21179d82012-08-21 08:03:32 -04006731 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006732 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006733 file_lock->fl_pid = current->tgid;
6734 file_lock->fl_file = filp;
6735 file_lock->fl_flags = FL_POSIX;
6736 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6737 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
Jeff Layton21179d82012-08-21 08:03:32 -04006739 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6740 locku->lu_length);
6741 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742
Jeff Layton21179d82012-08-21 08:03:32 -04006743 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006744 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006745 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 goto out_nfserr;
6747 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006748 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006749fput:
6750 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006751put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006752 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006753 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006755 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006756 if (file_lock)
6757 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 return status;
6759
6760out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006761 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006762 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763}
6764
6765/*
6766 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006767 * true: locks held by lockowner
6768 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006770static bool
6771check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006773 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006774 int status = false;
6775 struct file *filp = find_any_file(fp);
6776 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006777 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006778
6779 if (!filp) {
6780 /* Any valid lock stateid should have some sort of access */
6781 WARN_ON_ONCE(1);
6782 return status;
6783 }
6784
Amir Goldstein64bed6c2018-07-13 17:22:24 +03006785 inode = locks_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006786 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006788 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006789 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006790 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6791 if (fl->fl_owner == (fl_owner_t)lowner) {
6792 status = true;
6793 break;
6794 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006795 }
Jeff Layton6109c852015-01-16 15:05:57 -05006796 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006798 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799 return status;
6800}
6801
Al Virob37ad282006-10-19 23:28:59 -07006802__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006803nfsd4_release_lockowner(struct svc_rqst *rqstp,
6804 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_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006809 struct nfs4_stateowner *sop;
6810 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006811 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006813 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006814 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006815 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006816 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006817 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818
6819 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6820 clid->cl_boot, clid->cl_id);
6821
Jeff Layton4b24ca72014-06-30 11:48:44 -04006822 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006823 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006824 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006825
Trond Myklebustd4f04892014-07-29 21:34:36 -04006826 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006827 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006828 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006829 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006830 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006831
6832 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006833 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006834
Jeff Layton882e9d22014-07-29 21:34:37 -04006835 /* see if there are still any locks associated with it */
6836 lo = lockowner(sop);
6837 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6838 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6839 status = nfserr_locks_held;
6840 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006841 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006842 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006843 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006844
Kinglong Meeb5971af2014-08-22 10:18:43 -04006845 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006846 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006847 }
Chuck Lever88584812016-07-13 16:40:14 -04006848 if (!lo) {
6849 spin_unlock(&clp->cl_lock);
6850 return status;
6851 }
6852
6853 unhash_lockowner_locked(lo);
6854 while (!list_empty(&lo->lo_owner.so_stateids)) {
6855 stp = list_first_entry(&lo->lo_owner.so_stateids,
6856 struct nfs4_ol_stateid,
6857 st_perstateowner);
6858 WARN_ON(!unhash_lock_stateid(stp));
6859 put_ol_stateid_locked(stp, &reaplist);
6860 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006861 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006862 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04006863 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04006864 nfs4_put_stateowner(&lo->lo_owner);
6865
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 return status;
6867}
6868
6869static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006870alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871{
NeilBrowna55370a2005-06-23 22:03:52 -07006872 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873}
6874
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006875bool
Scott Mayhew6b189102019-03-26 18:06:26 -04006876nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006877{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006878 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006879
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006880 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006881 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006882}
6883
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884/*
6885 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04006886 *
6887 * The caller is responsible for freeing name.data if NULL is returned (it
6888 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006890struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04006891nfs4_client_to_reclaim(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892{
6893 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006894 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895
Scott Mayhew6b189102019-03-26 18:06:26 -04006896 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data);
NeilBrowna55370a2005-06-23 22:03:52 -07006897 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006898 if (crp) {
6899 strhashval = clientstr_hashval(name);
6900 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006901 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04006902 crp->cr_name.data = name.data;
6903 crp->cr_name.len = name.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006904 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006905 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006906 }
6907 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908}
6909
Jeff Layton2a4317c2012-03-21 16:42:43 -04006910void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006911nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006912{
6913 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04006914 kfree(crp->cr_name.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05006915 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006916 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006917}
6918
6919void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006920nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921{
6922 struct nfs4_client_reclaim *crp = NULL;
6923 int i;
6924
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006926 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6927 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006929 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930 }
6931 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006932 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933}
6934
6935/*
6936 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006937struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04006938nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939{
6940 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 struct nfs4_client_reclaim *crp = NULL;
6942
Scott Mayhew6b189102019-03-26 18:06:26 -04006943 dprintk("NFSD: nfs4_find_reclaim_client for name %.*s\n", name.len, name.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944
Scott Mayhew6b189102019-03-26 18:06:26 -04006945 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006946 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04006947 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 return crp;
6949 }
6950 }
6951 return NULL;
6952}
6953
6954/*
6955* Called from OPEN. Look for clientid in reclaim list.
6956*/
Al Virob37ad282006-10-19 23:28:59 -07006957__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006958nfs4_check_open_reclaim(clientid_t *clid,
6959 struct nfsd4_compound_state *cstate,
6960 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006962 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006963
6964 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006965 status = lookup_clientid(clid, cstate, nn);
6966 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006967 return nfserr_reclaim_bad;
6968
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006969 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6970 return nfserr_no_grace;
6971
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006972 if (nfsd4_client_record_check(cstate->clp))
6973 return nfserr_reclaim_bad;
6974
6975 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976}
6977
Bryan Schumaker65178db2011-11-01 13:35:21 -04006978#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006979static inline void
6980put_client(struct nfs4_client *clp)
6981{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04006982 atomic_dec(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04006983}
6984
Jeff Layton285abde2014-07-30 08:27:24 -04006985static struct nfs4_client *
6986nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6987{
6988 struct nfs4_client *clp;
6989 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6990 nfsd_net_id);
6991
6992 if (!nfsd_netns_ready(nn))
6993 return NULL;
6994
6995 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6996 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6997 return clp;
6998 }
6999 return NULL;
7000}
7001
Jeff Layton7ec0e362014-07-30 08:27:17 -04007002u64
Jeff Layton285abde2014-07-30 08:27:24 -04007003nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04007004{
7005 struct nfs4_client *clp;
7006 u64 count = 0;
7007 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7008 nfsd_net_id);
7009 char buf[INET6_ADDRSTRLEN];
7010
7011 if (!nfsd_netns_ready(nn))
7012 return 0;
7013
7014 spin_lock(&nn->client_lock);
7015 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7016 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7017 pr_info("NFS Client: %s\n", buf);
7018 ++count;
7019 }
7020 spin_unlock(&nn->client_lock);
7021
7022 return count;
7023}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007024
Jeff Laytona0926d12014-07-30 08:27:18 -04007025u64
Jeff Layton285abde2014-07-30 08:27:24 -04007026nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04007027{
7028 u64 count = 0;
7029 struct nfs4_client *clp;
7030 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7031 nfsd_net_id);
7032
7033 if (!nfsd_netns_ready(nn))
7034 return count;
7035
7036 spin_lock(&nn->client_lock);
7037 clp = nfsd_find_client(addr, addr_size);
7038 if (clp) {
7039 if (mark_client_expired_locked(clp) == nfs_ok)
7040 ++count;
7041 else
7042 clp = NULL;
7043 }
7044 spin_unlock(&nn->client_lock);
7045
7046 if (clp)
7047 expire_client(clp);
7048
7049 return count;
7050}
7051
Jeff Layton69fc9ed2014-07-30 08:27:19 -04007052u64
Jeff Layton285abde2014-07-30 08:27:24 -04007053nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04007054{
7055 u64 count = 0;
7056 struct nfs4_client *clp, *next;
7057 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7058 nfsd_net_id);
7059 LIST_HEAD(reaplist);
7060
7061 if (!nfsd_netns_ready(nn))
7062 return count;
7063
7064 spin_lock(&nn->client_lock);
7065 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7066 if (mark_client_expired_locked(clp) == nfs_ok) {
7067 list_add(&clp->cl_lru, &reaplist);
7068 if (max != 0 && ++count >= max)
7069 break;
7070 }
7071 }
7072 spin_unlock(&nn->client_lock);
7073
7074 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
7075 expire_client(clp);
7076
7077 return count;
7078}
7079
Bryan Schumaker184c1842012-11-29 11:40:44 -05007080static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
7081 const char *type)
7082{
7083 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05007084 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05007085 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
7086}
7087
Jeff Layton016200c2014-07-30 08:27:21 -04007088static void
7089nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
7090 struct list_head *collect)
7091{
7092 struct nfs4_client *clp = lst->st_stid.sc_client;
7093 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7094 nfsd_net_id);
7095
7096 if (!collect)
7097 return;
7098
7099 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007100 atomic_inc(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04007101 list_add(&lst->st_locks, collect);
7102}
7103
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007104static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04007105 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04007106 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05007107{
7108 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007109 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007110 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007111 u64 count = 0;
7112
Jeff Layton016200c2014-07-30 08:27:21 -04007113 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007114 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007115 list_for_each_entry_safe(stp, st_next,
7116 &oop->oo_owner.so_stateids, st_perstateowner) {
7117 list_for_each_entry_safe(lst, lst_next,
7118 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04007119 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04007120 if (func(lst))
7121 nfsd_inject_add_lock_to_list(lst,
7122 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04007123 }
Jeff Layton016200c2014-07-30 08:27:21 -04007124 ++count;
7125 /*
7126 * Despite the fact that these functions deal
7127 * with 64-bit integers for "count", we must
7128 * ensure that it doesn't blow up the
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007129 * clp->cl_rpc_users. Throw a warning if we
Jeff Layton016200c2014-07-30 08:27:21 -04007130 * start to approach INT_MAX here.
7131 */
7132 WARN_ON_ONCE(count == (INT_MAX / 2));
7133 if (count == max)
7134 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007135 }
7136 }
7137 }
Jeff Layton016200c2014-07-30 08:27:21 -04007138out:
7139 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007140
7141 return count;
7142}
7143
Jeff Layton016200c2014-07-30 08:27:21 -04007144static u64
7145nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
7146 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05007147{
Jeff Layton016200c2014-07-30 08:27:21 -04007148 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007149}
7150
Jeff Layton016200c2014-07-30 08:27:21 -04007151static u64
7152nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007153{
Jeff Layton016200c2014-07-30 08:27:21 -04007154 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007155 nfsd_print_count(clp, count, "locked files");
7156 return count;
7157}
7158
Jeff Layton016200c2014-07-30 08:27:21 -04007159u64
Jeff Layton285abde2014-07-30 08:27:24 -04007160nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04007161{
7162 struct nfs4_client *clp;
7163 u64 count = 0;
7164 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7165 nfsd_net_id);
7166
7167 if (!nfsd_netns_ready(nn))
7168 return 0;
7169
7170 spin_lock(&nn->client_lock);
7171 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7172 count += nfsd_print_client_locks(clp);
7173 spin_unlock(&nn->client_lock);
7174
7175 return count;
7176}
7177
7178static void
7179nfsd_reap_locks(struct list_head *reaplist)
7180{
7181 struct nfs4_client *clp;
7182 struct nfs4_ol_stateid *stp, *next;
7183
7184 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
7185 list_del_init(&stp->st_locks);
7186 clp = stp->st_stid.sc_client;
7187 nfs4_put_stid(&stp->st_stid);
7188 put_client(clp);
7189 }
7190}
7191
7192u64
Jeff Layton285abde2014-07-30 08:27:24 -04007193nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04007194{
7195 unsigned int count = 0;
7196 struct nfs4_client *clp;
7197 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7198 nfsd_net_id);
7199 LIST_HEAD(reaplist);
7200
7201 if (!nfsd_netns_ready(nn))
7202 return count;
7203
7204 spin_lock(&nn->client_lock);
7205 clp = nfsd_find_client(addr, addr_size);
7206 if (clp)
7207 count = nfsd_collect_client_locks(clp, &reaplist, 0);
7208 spin_unlock(&nn->client_lock);
7209 nfsd_reap_locks(&reaplist);
7210 return count;
7211}
7212
7213u64
Jeff Layton285abde2014-07-30 08:27:24 -04007214nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04007215{
7216 u64 count = 0;
7217 struct nfs4_client *clp;
7218 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7219 nfsd_net_id);
7220 LIST_HEAD(reaplist);
7221
7222 if (!nfsd_netns_ready(nn))
7223 return count;
7224
7225 spin_lock(&nn->client_lock);
7226 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7227 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
7228 if (max != 0 && count >= max)
7229 break;
7230 }
7231 spin_unlock(&nn->client_lock);
7232 nfsd_reap_locks(&reaplist);
7233 return count;
7234}
7235
Jeff Layton82e05ef2014-07-30 08:27:22 -04007236static u64
7237nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
7238 struct list_head *collect,
7239 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007240{
7241 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04007242 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7243 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007244 u64 count = 0;
7245
Jeff Layton82e05ef2014-07-30 08:27:22 -04007246 lockdep_assert_held(&nn->client_lock);
7247
7248 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007249 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04007250 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007251 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007252 if (collect) {
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007253 atomic_inc(&clp->cl_rpc_users);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007254 list_add(&oop->oo_perclient, collect);
7255 }
7256 }
7257 ++count;
7258 /*
7259 * Despite the fact that these functions deal with
7260 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007261 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton82e05ef2014-07-30 08:27:22 -04007262 * warning if we start to approach INT_MAX here.
7263 */
7264 WARN_ON_ONCE(count == (INT_MAX / 2));
7265 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007266 break;
7267 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04007268 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007269
7270 return count;
7271}
7272
Jeff Layton82e05ef2014-07-30 08:27:22 -04007273static u64
7274nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007275{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007276 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
7277
7278 nfsd_print_count(clp, count, "openowners");
7279 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007280}
7281
Jeff Layton82e05ef2014-07-30 08:27:22 -04007282static u64
7283nfsd_collect_client_openowners(struct nfs4_client *clp,
7284 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007285{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007286 return nfsd_foreach_client_openowner(clp, max, collect,
7287 unhash_openowner_locked);
7288}
7289
7290u64
Jeff Layton285abde2014-07-30 08:27:24 -04007291nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007292{
7293 struct nfs4_client *clp;
7294 u64 count = 0;
7295 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7296 nfsd_net_id);
7297
7298 if (!nfsd_netns_ready(nn))
7299 return 0;
7300
7301 spin_lock(&nn->client_lock);
7302 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7303 count += nfsd_print_client_openowners(clp);
7304 spin_unlock(&nn->client_lock);
7305
7306 return count;
7307}
7308
7309static void
7310nfsd_reap_openowners(struct list_head *reaplist)
7311{
7312 struct nfs4_client *clp;
7313 struct nfs4_openowner *oop, *next;
7314
7315 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
7316 list_del_init(&oop->oo_perclient);
7317 clp = oop->oo_owner.so_client;
7318 release_openowner(oop);
7319 put_client(clp);
7320 }
7321}
7322
7323u64
Jeff Layton285abde2014-07-30 08:27:24 -04007324nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
7325 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007326{
7327 unsigned int count = 0;
7328 struct nfs4_client *clp;
7329 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7330 nfsd_net_id);
7331 LIST_HEAD(reaplist);
7332
7333 if (!nfsd_netns_ready(nn))
7334 return count;
7335
7336 spin_lock(&nn->client_lock);
7337 clp = nfsd_find_client(addr, addr_size);
7338 if (clp)
7339 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
7340 spin_unlock(&nn->client_lock);
7341 nfsd_reap_openowners(&reaplist);
7342 return count;
7343}
7344
7345u64
Jeff Layton285abde2014-07-30 08:27:24 -04007346nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007347{
7348 u64 count = 0;
7349 struct nfs4_client *clp;
7350 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7351 nfsd_net_id);
7352 LIST_HEAD(reaplist);
7353
7354 if (!nfsd_netns_ready(nn))
7355 return count;
7356
7357 spin_lock(&nn->client_lock);
7358 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7359 count += nfsd_collect_client_openowners(clp, &reaplist,
7360 max - count);
7361 if (max != 0 && count >= max)
7362 break;
7363 }
7364 spin_unlock(&nn->client_lock);
7365 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007366 return count;
7367}
7368
Bryan Schumaker269de302012-11-29 11:40:42 -05007369static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
7370 struct list_head *victims)
7371{
7372 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007373 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7374 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05007375 u64 count = 0;
7376
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007377 lockdep_assert_held(&nn->client_lock);
7378
7379 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007380 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04007381 if (victims) {
7382 /*
7383 * It's not safe to mess with delegations that have a
7384 * non-zero dl_time. They might have already been broken
7385 * and could be processed by the laundromat outside of
7386 * the state_lock. Just leave them be.
7387 */
7388 if (dp->dl_time != 0)
7389 continue;
7390
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007391 atomic_inc(&clp->cl_rpc_users);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007392 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007393 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04007394 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007395 ++count;
7396 /*
7397 * Despite the fact that these functions deal with
7398 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007399 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007400 * warning if we start to approach INT_MAX here.
7401 */
7402 WARN_ON_ONCE(count == (INT_MAX / 2));
7403 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05007404 break;
7405 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007406 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007407 return count;
7408}
7409
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007410static u64
7411nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05007412{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007413 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007414
7415 nfsd_print_count(clp, count, "delegations");
7416 return count;
7417}
7418
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007419u64
Jeff Layton285abde2014-07-30 08:27:24 -04007420nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007421{
7422 struct nfs4_client *clp;
7423 u64 count = 0;
7424 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7425 nfsd_net_id);
7426
7427 if (!nfsd_netns_ready(nn))
7428 return 0;
7429
7430 spin_lock(&nn->client_lock);
7431 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7432 count += nfsd_print_client_delegations(clp);
7433 spin_unlock(&nn->client_lock);
7434
7435 return count;
7436}
7437
7438static void
7439nfsd_forget_delegations(struct list_head *reaplist)
7440{
7441 struct nfs4_client *clp;
7442 struct nfs4_delegation *dp, *next;
7443
7444 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7445 list_del_init(&dp->dl_recall_lru);
7446 clp = dp->dl_stid.sc_client;
7447 revoke_delegation(dp);
7448 put_client(clp);
7449 }
7450}
7451
7452u64
Jeff Layton285abde2014-07-30 08:27:24 -04007453nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7454 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007455{
7456 u64 count = 0;
7457 struct nfs4_client *clp;
7458 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7459 nfsd_net_id);
7460 LIST_HEAD(reaplist);
7461
7462 if (!nfsd_netns_ready(nn))
7463 return count;
7464
7465 spin_lock(&nn->client_lock);
7466 clp = nfsd_find_client(addr, addr_size);
7467 if (clp)
7468 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7469 spin_unlock(&nn->client_lock);
7470
7471 nfsd_forget_delegations(&reaplist);
7472 return count;
7473}
7474
7475u64
Jeff Layton285abde2014-07-30 08:27:24 -04007476nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007477{
7478 u64 count = 0;
7479 struct nfs4_client *clp;
7480 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7481 nfsd_net_id);
7482 LIST_HEAD(reaplist);
7483
7484 if (!nfsd_netns_ready(nn))
7485 return count;
7486
7487 spin_lock(&nn->client_lock);
7488 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7489 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7490 if (max != 0 && count >= max)
7491 break;
7492 }
7493 spin_unlock(&nn->client_lock);
7494 nfsd_forget_delegations(&reaplist);
7495 return count;
7496}
7497
7498static void
7499nfsd_recall_delegations(struct list_head *reaplist)
7500{
7501 struct nfs4_client *clp;
7502 struct nfs4_delegation *dp, *next;
7503
7504 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7505 list_del_init(&dp->dl_recall_lru);
7506 clp = dp->dl_stid.sc_client;
7507 /*
7508 * We skipped all entries that had a zero dl_time before,
7509 * so we can now reset the dl_time back to 0. If a delegation
7510 * break comes in now, then it won't make any difference since
7511 * we're recalling it either way.
7512 */
7513 spin_lock(&state_lock);
7514 dp->dl_time = 0;
7515 spin_unlock(&state_lock);
7516 nfsd_break_one_deleg(dp);
7517 put_client(clp);
7518 }
7519}
7520
7521u64
Jeff Layton285abde2014-07-30 08:27:24 -04007522nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007523 size_t addr_size)
7524{
7525 u64 count = 0;
7526 struct nfs4_client *clp;
7527 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7528 nfsd_net_id);
7529 LIST_HEAD(reaplist);
7530
7531 if (!nfsd_netns_ready(nn))
7532 return count;
7533
7534 spin_lock(&nn->client_lock);
7535 clp = nfsd_find_client(addr, addr_size);
7536 if (clp)
7537 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7538 spin_unlock(&nn->client_lock);
7539
7540 nfsd_recall_delegations(&reaplist);
7541 return count;
7542}
7543
7544u64
Jeff Layton285abde2014-07-30 08:27:24 -04007545nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007546{
7547 u64 count = 0;
7548 struct nfs4_client *clp, *next;
7549 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7550 nfsd_net_id);
7551 LIST_HEAD(reaplist);
7552
7553 if (!nfsd_netns_ready(nn))
7554 return count;
7555
7556 spin_lock(&nn->client_lock);
7557 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7558 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7559 if (max != 0 && ++count >= max)
7560 break;
7561 }
7562 spin_unlock(&nn->client_lock);
7563 nfsd_recall_delegations(&reaplist);
7564 return count;
7565}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007566#endif /* CONFIG_NFSD_FAULT_INJECTION */
7567
Meelap Shahc2f1a552007-07-17 04:04:39 -07007568/*
7569 * Since the lifetime of a delegation isn't limited to that of an open, a
7570 * client may quite reasonably hang on to a delegation as long as it has
7571 * the inode cached. This becomes an obvious problem the first time a
7572 * client's inode cache approaches the size of the server's total memory.
7573 *
7574 * For now we avoid this problem by imposing a hard limit on the number
7575 * of delegations, which varies according to the server's memory size.
7576 */
7577static void
7578set_max_delegations(void)
7579{
7580 /*
7581 * Allow at most 4 delegations per megabyte of RAM. Quick
7582 * estimates suggest that in the worst case (where every delegation
7583 * is for a different inode), a delegation could take about 1.5K,
7584 * giving a worst case usage of about 6% of memory.
7585 */
7586 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7587}
7588
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007589static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007590{
7591 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7592 int i;
7593
Kees Cook6da2ec52018-06-12 13:55:00 -07007594 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7595 sizeof(struct list_head),
7596 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007597 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007598 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007599 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7600 sizeof(struct list_head),
7601 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007602 if (!nn->unconf_id_hashtbl)
7603 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007604 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7605 sizeof(struct list_head),
7606 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007607 if (!nn->sessionid_hashtbl)
7608 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007609
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007610 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007611 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007612 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007613 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007614 for (i = 0; i < SESSION_HASH_SIZE; i++)
7615 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007616 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007617 nn->unconf_name_tree = RB_ROOT;
Vasily Averin81833de2017-11-13 07:25:40 +03007618 nn->boot_time = get_seconds();
7619 nn->grace_ended = false;
7620 nn->nfsd4_manager.block_opens = true;
7621 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007622 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007623 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007624 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007625 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007626 spin_lock_init(&nn->s2s_cp_lock);
7627 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007628
Jeff Layton0cc11a62016-10-20 09:34:31 -04007629 spin_lock_init(&nn->blocked_locks_lock);
7630 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7631
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007632 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007633 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007634
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007635 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007636
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007637err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007638 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007639err_unconf_id:
7640 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007641err:
7642 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007643}
7644
7645static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007646nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007647{
7648 int i;
7649 struct nfs4_client *clp = NULL;
7650 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7651
7652 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7653 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7654 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7655 destroy_client(clp);
7656 }
7657 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007658
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007659 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7660
Kinglong Mee2b905632014-03-26 22:09:30 +08007661 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7662 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7663 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7664 destroy_client(clp);
7665 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007666 }
7667
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007668 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007669 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007670 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007671 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007672}
7673
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007674int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007675nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007676{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007677 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007678 int ret;
7679
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007680 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007681 if (ret)
7682 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007683 locks_start_grace(net, &nn->nfsd4_manager);
7684 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007685 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7686 goto skip_grace;
Vasily Averin7e981a82017-11-06 16:46:04 +03007687 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7688 nn->nfsd4_grace, net->ns.inum);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007689 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007690 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007691
7692skip_grace:
7693 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7694 net->ns.inum);
7695 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7696 nfsd4_end_grace(nn);
7697 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007698}
7699
7700/* initialization to perform when the nfsd service is started: */
7701
7702int
7703nfs4_state_start(void)
7704{
7705 int ret;
7706
Jeff Layton51a54562015-08-20 07:17:01 -04007707 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007708 if (laundry_wq == NULL) {
7709 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007710 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007711 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007712 ret = nfsd4_create_callback_queue();
7713 if (ret)
7714 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007715
Meelap Shahc2f1a552007-07-17 04:04:39 -07007716 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007717 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007718
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007719out_free_laundry:
7720 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007721out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007722 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723}
7724
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007725void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007726nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007730 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007731
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007732 cancel_delayed_work_sync(&nn->laundromat_work);
7733 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007734
Linus Torvalds1da177e2005-04-16 15:20:36 -07007735 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007736 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007737 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007739 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007740 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007741 }
Benny Halevycdc975052014-05-30 09:09:30 -04007742 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 list_for_each_safe(pos, next, &reaplist) {
7744 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007745 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007746 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747 }
7748
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007749 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007750 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007751}
7752
7753void
7754nfs4_state_shutdown(void)
7755{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007756 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007757 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007758}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007759
7760static void
7761get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7762{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007763 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7764 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007765}
7766
7767static void
7768put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7769{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007770 if (cstate->minorversion) {
7771 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7772 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7773 }
7774}
7775
7776void
7777clear_current_stateid(struct nfsd4_compound_state *cstate)
7778{
7779 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007780}
7781
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007782/*
7783 * functions to set current state id
7784 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007785void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007786nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7787 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007788{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007789 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007790}
7791
7792void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007793nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7794 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007795{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007796 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007797}
7798
7799void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007800nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7801 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007802{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007803 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007804}
7805
7806void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007807nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7808 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007809{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007810 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007811}
7812
7813/*
7814 * functions to consume current state id
7815 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007816
7817void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007818nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7819 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007820{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007821 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007822}
7823
7824void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007825nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7826 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007827{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007828 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007829}
7830
7831void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007832nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7833 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007834{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007835 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007836}
7837
7838void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007839nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7840 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007841{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007842 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007843}
7844
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007845void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007846nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7847 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007848{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007849 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007850}
7851
7852void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007853nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7854 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007855{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007856 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007857}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007858
7859void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007860nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7861 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007862{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007863 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007864}
7865
7866void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007867nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7868 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007869{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007870 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007871}