blob: 640cd221fc775bbec812de8b56d1e6733d2d7780 [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);
1902 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001903 kmem_cache_free(client_slab, clp);
1904}
1905
1906void drop_client(struct nfs4_client *clp)
1907{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001908 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001909}
1910
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001911static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912free_client(struct nfs4_client *clp)
1913{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001914 while (!list_empty(&clp->cl_sessions)) {
1915 struct nfsd4_session *ses;
1916 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1917 se_perclnt);
1918 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001919 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1920 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001921 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001922 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04001923 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001924 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04001925 clp->cl_nfsd_dentry = NULL;
1926 wake_up_all(&expiry_wq);
1927 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001928 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
Benny Halevy84d38ac2010-05-12 00:13:16 +03001931/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001932static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001933unhash_client_locked(struct nfs4_client *clp)
1934{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001935 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001936 struct nfsd4_session *ses;
1937
Trond Myklebust0a880a22014-07-30 08:27:10 -04001938 lockdep_assert_held(&nn->client_lock);
1939
Trond Myklebust4beb3452014-07-30 08:27:02 -04001940 /* Mark the client as expired! */
1941 clp->cl_time = 0;
1942 /* Make it invisible */
1943 if (!list_empty(&clp->cl_idhash)) {
1944 list_del_init(&clp->cl_idhash);
1945 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1946 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1947 else
1948 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1949 }
1950 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001951 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001952 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1953 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001954 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001955}
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001958unhash_client(struct nfs4_client *clp)
1959{
1960 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1961
1962 spin_lock(&nn->client_lock);
1963 unhash_client_locked(clp);
1964 spin_unlock(&nn->client_lock);
1965}
1966
Jeff Layton97403d92014-07-30 08:27:12 -04001967static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1968{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001969 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04001970 return nfserr_jukebox;
1971 unhash_client_locked(clp);
1972 return nfs_ok;
1973}
1974
Trond Myklebust4beb3452014-07-30 08:27:02 -04001975static void
1976__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04001978 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001979 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 struct list_head reaplist;
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04001984 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001985 while (!list_empty(&clp->cl_delegations)) {
1986 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001987 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001988 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
Benny Halevycdc975052014-05-30 09:09:30 -04001990 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 while (!list_empty(&reaplist)) {
1992 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04001993 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001994 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04001996 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05001997 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001998 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04001999 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002000 }
NeilBrownea1da632005-06-23 22:04:17 -07002001 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002002 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002003 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002004 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002006 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2007 struct nfs4_stateowner *so, *tmp;
2008
2009 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2010 so_strhash) {
2011 /* Should be no openowners at this point */
2012 WARN_ON_ONCE(so->so_is_open_owner);
2013 remove_blocked_locks(lockowner(so));
2014 }
2015 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002016 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002017 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002018 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002019 if (clp->cl_cb_conn.cb_xprt)
2020 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002021 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002022 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
Trond Myklebust4beb3452014-07-30 08:27:02 -04002025static void
2026destroy_client(struct nfs4_client *clp)
2027{
2028 unhash_client(clp);
2029 __destroy_client(clp);
2030}
2031
Scott Mayhew362063a2019-03-26 18:06:28 -04002032static void inc_reclaim_complete(struct nfs4_client *clp)
2033{
2034 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2035
2036 if (!nn->track_reclaim_completes)
2037 return;
2038 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2039 return;
2040 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2041 nn->reclaim_str_hashtbl_size) {
2042 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2043 clp->net->ns.inum);
2044 nfsd4_end_grace(nn);
2045 }
2046}
2047
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002048static void expire_client(struct nfs4_client *clp)
2049{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002050 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002051 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002052 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002053}
2054
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002055static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2056{
2057 memcpy(target->cl_verifier.data, source->data,
2058 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002061static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2064 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2065}
2066
J. Bruce Fields50043852015-11-20 15:58:37 -05002067static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2068{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002069 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2070 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2071 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002072 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2073 if ((source->cr_principal && !target->cr_principal) ||
2074 (source->cr_raw_principal && !target->cr_raw_principal) ||
2075 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002076 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002077
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002078 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 target->cr_uid = source->cr_uid;
2080 target->cr_gid = source->cr_gid;
2081 target->cr_group_info = source->cr_group_info;
2082 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002083 target->cr_gss_mech = source->cr_gss_mech;
2084 if (source->cr_gss_mech)
2085 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002089static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002090compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2091{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002092 if (o1->len < o2->len)
2093 return -1;
2094 if (o1->len > o2->len)
2095 return 1;
2096 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002097}
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002100same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2101{
2102 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
2104
2105static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002106same_clid(clientid_t *cl1, clientid_t *cl2)
2107{
2108 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002111static bool groups_equal(struct group_info *g1, struct group_info *g2)
2112{
2113 int i;
2114
2115 if (g1->ngroups != g2->ngroups)
2116 return false;
2117 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002118 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002119 return false;
2120 return true;
2121}
2122
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002123/*
2124 * RFC 3530 language requires clid_inuse be returned when the
2125 * "principal" associated with a requests differs from that previously
2126 * used. We use uid, gid's, and gss principal string as our best
2127 * approximation. We also don't want to allow non-gss use of a client
2128 * established using gss: in theory cr_principal should catch that
2129 * change, but in practice cr_principal can be null even in the gss case
2130 * since gssd doesn't always pass down a principal string.
2131 */
2132static bool is_gss_cred(struct svc_cred *cr)
2133{
2134 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2135 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2136}
2137
2138
Vivek Trivedi5559b502012-07-24 21:18:20 +05302139static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002140same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2141{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002142 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002143 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2144 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002145 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2146 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002147 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002148 if (cr1->cr_principal == cr2->cr_principal)
2149 return true;
2150 if (!cr1->cr_principal || !cr2->cr_principal)
2151 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302152 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
J. Bruce Fields57266a62013-04-13 14:27:29 -04002155static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2156{
2157 struct svc_cred *cr = &rqstp->rq_cred;
2158 u32 service;
2159
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002160 if (!cr->cr_gss_mech)
2161 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002162 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2163 return service == RPC_GSS_SVC_INTEGRITY ||
2164 service == RPC_GSS_SVC_PRIVACY;
2165}
2166
Andrew Elblededeb132016-06-15 12:52:08 -04002167bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002168{
2169 struct svc_cred *cr = &rqstp->rq_cred;
2170
2171 if (!cl->cl_mach_cred)
2172 return true;
2173 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2174 return false;
2175 if (!svc_rqst_integrity_protected(rqstp))
2176 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002177 if (cl->cl_cred.cr_raw_principal)
2178 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2179 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002180 if (!cr->cr_principal)
2181 return false;
2182 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2183}
2184
Jeff Layton294ac322014-07-30 08:27:15 -04002185static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002186{
Chuck Leverab4684d2012-03-02 17:13:50 -05002187 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Jeff Laytonf4199922014-06-17 07:44:11 -04002189 /*
2190 * This is opaque to client, so no need to byte-swap. Use
2191 * __force to keep sparse happy
2192 */
2193 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002194 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002195 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
Jeff Layton294ac322014-07-30 08:27:15 -04002198static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2199{
2200 clp->cl_clientid.cl_boot = nn->boot_time;
2201 clp->cl_clientid.cl_id = nn->clientid_counter++;
2202 gen_confirm(clp, nn);
2203}
2204
Jeff Layton4770d722014-07-29 21:34:10 -04002205static struct nfs4_stid *
2206find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002207{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002208 struct nfs4_stid *ret;
2209
2210 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2211 if (!ret || !ret->sc_type)
2212 return NULL;
2213 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002214}
2215
Jeff Layton4770d722014-07-29 21:34:10 -04002216static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002217find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002218{
2219 struct nfs4_stid *s;
2220
Jeff Layton4770d722014-07-29 21:34:10 -04002221 spin_lock(&cl->cl_lock);
2222 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002223 if (s != NULL) {
2224 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002225 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002226 else
2227 s = NULL;
2228 }
Jeff Layton4770d722014-07-29 21:34:10 -04002229 spin_unlock(&cl->cl_lock);
2230 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002231}
2232
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002233static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2234{
2235 struct nfsdfs_client *nc;
2236 nc = get_nfsdfs_client(inode);
2237 if (!nc)
2238 return NULL;
2239 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2240}
2241
J. Bruce Fields169319f2019-06-19 12:39:46 -04002242static void seq_quote_mem(struct seq_file *m, char *data, int len)
2243{
2244 seq_printf(m, "\"");
2245 seq_escape_mem_ascii(m, data, len);
2246 seq_printf(m, "\"");
2247}
2248
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002249static int client_info_show(struct seq_file *m, void *v)
2250{
2251 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002252 struct nfs4_client *clp;
2253 u64 clid;
2254
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002255 clp = get_nfsdfs_clp(inode);
2256 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002257 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002258 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2259 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002260 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2261 seq_printf(m, "name: ");
2262 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2263 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002264 drop_client(clp);
2265
2266 return 0;
2267}
2268
2269static int client_info_open(struct inode *inode, struct file *file)
2270{
2271 return single_open(file, client_info_show, inode);
2272}
2273
2274static const struct file_operations client_info_fops = {
2275 .open = client_info_open,
2276 .read = seq_read,
2277 .llseek = seq_lseek,
2278 .release = single_release,
2279};
2280
J. Bruce Fields78599c42019-04-22 15:26:23 -04002281static void *states_start(struct seq_file *s, loff_t *pos)
2282 __acquires(&clp->cl_lock)
2283{
2284 struct nfs4_client *clp = s->private;
2285 unsigned long id = *pos;
2286 void *ret;
2287
2288 spin_lock(&clp->cl_lock);
2289 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2290 *pos = id;
2291 return ret;
2292}
2293
2294static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2295{
2296 struct nfs4_client *clp = s->private;
2297 unsigned long id = *pos;
2298 void *ret;
2299
2300 id = *pos;
2301 id++;
2302 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2303 *pos = id;
2304 return ret;
2305}
2306
2307static void states_stop(struct seq_file *s, void *v)
2308 __releases(&clp->cl_lock)
2309{
2310 struct nfs4_client *clp = s->private;
2311
2312 spin_unlock(&clp->cl_lock);
2313}
2314
2315static void nfs4_show_superblock(struct seq_file *s, struct file *f)
2316{
2317 struct inode *inode = file_inode(f);
2318
2319 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2320 MAJOR(inode->i_sb->s_dev),
2321 MINOR(inode->i_sb->s_dev),
2322 inode->i_ino);
2323}
2324
2325static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2326{
2327 seq_printf(s, "owner: ");
2328 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2329}
2330
2331static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2332{
2333 struct nfs4_ol_stateid *ols;
2334 struct nfs4_file *nf;
2335 struct file *file;
2336 struct nfs4_stateowner *oo;
2337 unsigned int access, deny;
2338
2339 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2340 return 0; /* XXX: or SEQ_SKIP? */
2341 ols = openlockstateid(st);
2342 oo = ols->st_stateowner;
2343 nf = st->sc_file;
2344 file = find_any_file(nf);
2345
2346 seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid);
2347
2348 access = bmap_to_share_mode(ols->st_access_bmap);
2349 deny = bmap_to_share_mode(ols->st_deny_bmap);
2350
2351 seq_printf(s, "access: \%s\%s, ",
2352 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2353 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2354 seq_printf(s, "deny: \%s\%s, ",
2355 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2356 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2357
2358 nfs4_show_superblock(s, file);
2359 seq_printf(s, ", ");
2360 nfs4_show_owner(s, oo);
2361 seq_printf(s, " }\n");
J. Bruce Fields78599c42019-04-22 15:26:23 -04002362 fput(file);
2363
2364 return 0;
2365}
2366
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002367static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2368{
2369 struct nfs4_ol_stateid *ols;
2370 struct nfs4_file *nf;
2371 struct file *file;
2372 struct nfs4_stateowner *oo;
2373
2374 ols = openlockstateid(st);
2375 oo = ols->st_stateowner;
2376 nf = st->sc_file;
2377 file = find_any_file(nf);
2378
2379 seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid);
2380
2381 /*
2382 * Note: a lock stateid isn't really the same thing as a lock,
2383 * it's the locking state held by one owner on a file, and there
2384 * may be multiple (or no) lock ranges associated with it.
2385 * (Same for the matter is true of open stateids.)
2386 */
2387
2388 nfs4_show_superblock(s, file);
2389 /* XXX: open stateid? */
2390 seq_printf(s, ", ");
2391 nfs4_show_owner(s, oo);
2392 seq_printf(s, " }\n");
2393 fput(file);
2394
2395 return 0;
2396}
2397
2398static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2399{
2400 struct nfs4_delegation *ds;
2401 struct nfs4_file *nf;
2402 struct file *file;
2403
2404 ds = delegstateid(st);
2405 nf = st->sc_file;
2406 file = nf->fi_deleg_file;
2407
2408 seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid);
2409
2410 /* Kinda dead code as long as we only support read delegs: */
2411 seq_printf(s, "access: %s, ",
2412 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2413
2414 /* XXX: lease time, whether it's being recalled. */
2415
2416 nfs4_show_superblock(s, file);
2417 seq_printf(s, " }\n");
2418
2419 return 0;
2420}
2421
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002422static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2423{
2424 struct nfs4_layout_stateid *ls;
2425 struct file *file;
2426
2427 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2428 file = ls->ls_file;
2429
2430 seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid);
2431
2432 /* XXX: What else would be useful? */
2433
2434 nfs4_show_superblock(s, file);
2435 seq_printf(s, " }\n");
2436
2437 return 0;
2438}
2439
J. Bruce Fields78599c42019-04-22 15:26:23 -04002440static int states_show(struct seq_file *s, void *v)
2441{
2442 struct nfs4_stid *st = v;
2443
2444 switch (st->sc_type) {
2445 case NFS4_OPEN_STID:
2446 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002447 case NFS4_LOCK_STID:
2448 return nfs4_show_lock(s, st);
2449 case NFS4_DELEG_STID:
2450 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002451 case NFS4_LAYOUT_STID:
2452 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002453 default:
2454 return 0; /* XXX: or SEQ_SKIP? */
2455 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002456 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002457}
2458
2459static struct seq_operations states_seq_ops = {
2460 .start = states_start,
2461 .next = states_next,
2462 .stop = states_stop,
2463 .show = states_show
2464};
2465
2466static int client_states_open(struct inode *inode, struct file *file)
2467{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002468 struct seq_file *s;
2469 struct nfs4_client *clp;
2470 int ret;
2471
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002472 clp = get_nfsdfs_clp(inode);
2473 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002474 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002475
2476 ret = seq_open(file, &states_seq_ops);
2477 if (ret)
2478 return ret;
2479 s = file->private_data;
2480 s->private = clp;
2481 return 0;
2482}
2483
2484static int client_opens_release(struct inode *inode, struct file *file)
2485{
2486 struct seq_file *m = file->private_data;
2487 struct nfs4_client *clp = m->private;
2488
2489 /* XXX: alternatively, we could get/drop in seq start/stop */
2490 drop_client(clp);
2491 return 0;
2492}
2493
2494static const struct file_operations client_states_fops = {
2495 .open = client_states_open,
2496 .read = seq_read,
2497 .llseek = seq_lseek,
2498 .release = client_opens_release,
2499};
2500
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002501/*
2502 * Normally we refuse to destroy clients that are in use, but here the
2503 * administrator is telling us to just do it. We also want to wait
2504 * so the caller has a guarantee that the client's locks are gone by
2505 * the time the write returns:
2506 */
2507void force_expire_client(struct nfs4_client *clp)
2508{
2509 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2510 bool already_expired;
2511
2512 spin_lock(&clp->cl_lock);
2513 clp->cl_time = 0;
2514 spin_unlock(&clp->cl_lock);
2515
2516 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2517 spin_lock(&nn->client_lock);
2518 already_expired = list_empty(&clp->cl_lru);
2519 if (!already_expired)
2520 unhash_client_locked(clp);
2521 spin_unlock(&nn->client_lock);
2522
2523 if (!already_expired)
2524 expire_client(clp);
2525 else
2526 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2527}
2528
2529static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2530 size_t size, loff_t *pos)
2531{
2532 char *data;
2533 struct nfs4_client *clp;
2534
2535 data = simple_transaction_get(file, buf, size);
2536 if (IS_ERR(data))
2537 return PTR_ERR(data);
2538 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2539 return -EINVAL;
2540 clp = get_nfsdfs_clp(file_inode(file));
2541 if (!clp)
2542 return -ENXIO;
2543 force_expire_client(clp);
2544 drop_client(clp);
2545 return 7;
2546}
2547
2548static const struct file_operations client_ctl_fops = {
2549 .write = client_ctl_write,
2550 .release = simple_transaction_release,
2551};
2552
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002553static const struct tree_descr client_files[] = {
2554 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002555 [1] = {"states", &client_states_fops, S_IRUSR},
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002556 [2] = {"ctl", &client_ctl_fops, S_IRUSR|S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002557 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002558};
2559
Jeff Layton2216d442012-11-12 15:00:57 -05002560static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002561 struct svc_rqst *rqstp, nfs4_verifier *verf)
2562{
2563 struct nfs4_client *clp;
2564 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002565 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002566 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002567 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002568
2569 clp = alloc_client(name);
2570 if (clp == NULL)
2571 return NULL;
2572
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002573 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2574 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002575 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002576 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002577 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002578 gen_clid(clp, nn);
2579 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002580 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002581 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002582 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002583 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002584 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002585 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002586 clp->net = net;
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002587 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002588 clp->cl_clientid.cl_id - nn->clientid_base,
2589 client_files);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002590 if (!clp->cl_nfsd_dentry) {
2591 free_client(clp);
2592 return NULL;
2593 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002594 return clp;
2595}
2596
NeilBrownfd39ca92005-06-23 22:04:03 -07002597static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002598add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2599{
2600 struct rb_node **new = &(root->rb_node), *parent = NULL;
2601 struct nfs4_client *clp;
2602
2603 while (*new) {
2604 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2605 parent = *new;
2606
2607 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2608 new = &((*new)->rb_left);
2609 else
2610 new = &((*new)->rb_right);
2611 }
2612
2613 rb_link_node(&new_clp->cl_namenode, parent, new);
2614 rb_insert_color(&new_clp->cl_namenode, root);
2615}
2616
2617static struct nfs4_client *
2618find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2619{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002620 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002621 struct rb_node *node = root->rb_node;
2622 struct nfs4_client *clp;
2623
2624 while (node) {
2625 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2626 cmp = compare_blob(&clp->cl_name, name);
2627 if (cmp > 0)
2628 node = node->rb_left;
2629 else if (cmp < 0)
2630 node = node->rb_right;
2631 else
2632 return clp;
2633 }
2634 return NULL;
2635}
2636
2637static void
2638add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
2640 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002641 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Trond Myklebust0a880a22014-07-30 08:27:10 -04002643 lockdep_assert_held(&nn->client_lock);
2644
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002645 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002646 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002648 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002649 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651
NeilBrownfd39ca92005-06-23 22:04:03 -07002652static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653move_to_confirmed(struct nfs4_client *clp)
2654{
2655 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002656 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Trond Myklebust0a880a22014-07-30 08:27:10 -04002658 lockdep_assert_held(&nn->client_lock);
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002661 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002662 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002663 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002664 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002665 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
2668static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002669find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
2671 struct nfs4_client *clp;
2672 unsigned int idhashval = clientid_hashval(clid->cl_id);
2673
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002674 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002675 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002676 if ((bool)clp->cl_minorversion != sessions)
2677 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002678 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 }
2682 return NULL;
2683}
2684
2685static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002686find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2687{
2688 struct list_head *tbl = nn->conf_id_hashtbl;
2689
Trond Myklebust0a880a22014-07-30 08:27:10 -04002690 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002691 return find_client_in_id_table(tbl, clid, sessions);
2692}
2693
2694static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002695find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002697 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Trond Myklebust0a880a22014-07-30 08:27:10 -04002699 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002700 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002703static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002704{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002705 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002706}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002707
NeilBrown28ce6052005-06-23 22:03:56 -07002708static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002709find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002710{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002711 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002712 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002713}
2714
2715static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002716find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002717{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002718 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002719 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002720}
2721
NeilBrownfd39ca92005-06-23 22:04:03 -07002722static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002723gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002725 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002726 struct sockaddr *sa = svc_addr(rqstp);
2727 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002728 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Jeff Layton7077ecb2009-08-14 12:57:58 -04002730 /* Currently, we only support tcp and tcp6 for the callback channel */
2731 if (se->se_callback_netid_len == 3 &&
2732 !memcmp(se->se_callback_netid_val, "tcp", 3))
2733 expected_family = AF_INET;
2734 else if (se->se_callback_netid_len == 4 &&
2735 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2736 expected_family = AF_INET6;
2737 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 goto out_err;
2739
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002740 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002741 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002742 (struct sockaddr *)&conn->cb_addr,
2743 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002744
J. Bruce Fields07263f12010-05-31 19:09:40 -04002745 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002747
J. Bruce Fields07263f12010-05-31 19:09:40 -04002748 if (conn->cb_addr.ss_family == AF_INET6)
2749 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002750
J. Bruce Fields07263f12010-05-31 19:09:40 -04002751 conn->cb_prog = se->se_callback_prog;
2752 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002753 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return;
2755out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002756 conn->cb_addr.ss_family = AF_UNSPEC;
2757 conn->cb_addrlen = 0;
Rasmus Villemoes4ab495b2017-02-24 01:15:55 +01002758 dprintk("NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 "will not receive delegations\n",
2760 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 return;
2763}
2764
Andy Adamson074fe892009-04-03 08:28:15 +03002765/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002766 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002767 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002768static void
Andy Adamson074fe892009-04-03 08:28:15 +03002769nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2770{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002771 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002772 struct nfsd4_slot *slot = resp->cstate.slot;
2773 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002774
Andy Adamson557ce262009-08-28 08:45:04 -04002775 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002776
J. Bruce Fields085def32017-10-18 16:17:18 -04002777 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002778 slot->sl_opcnt = resp->opcnt;
2779 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002780 free_svc_cred(&slot->sl_cred);
2781 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002782
J. Bruce Fields085def32017-10-18 16:17:18 -04002783 if (!nfsd4_cache_this(resp)) {
2784 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002785 return;
2786 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002787 slot->sl_flags |= NFSD4_SLOT_CACHED;
2788
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002789 base = resp->cstate.data_offset;
2790 slot->sl_datalen = buf->len - base;
2791 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002792 WARN(1, "%s: sessions DRC could not cache compound\n",
2793 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002794 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002795}
2796
2797/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002798 * Encode the replay sequence operation from the slot values.
2799 * If cachethis is FALSE encode the uncached rep error on the next
2800 * operation which sets resp->p and increments resp->opcnt for
2801 * nfs4svc_encode_compoundres.
2802 *
Andy Adamson074fe892009-04-03 08:28:15 +03002803 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002804static __be32
2805nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2806 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002807{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002808 struct nfsd4_op *op;
2809 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002810
Andy Adamsonabfabf82009-07-23 19:02:18 -04002811 /* Encode the replayed sequence operation */
2812 op = &args->ops[resp->opcnt - 1];
2813 nfsd4_encode_operation(resp, op);
2814
J. Bruce Fields085def32017-10-18 16:17:18 -04002815 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2816 return op->status;
2817 if (args->opcnt == 1) {
2818 /*
2819 * The original operation wasn't a solo sequence--we
2820 * always cache those--so this retry must not match the
2821 * original:
2822 */
2823 op->status = nfserr_seq_false_retry;
2824 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002825 op = &args->ops[resp->opcnt++];
2826 op->status = nfserr_retry_uncached_rep;
2827 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002828 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002829 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002830}
2831
2832/*
Andy Adamson557ce262009-08-28 08:45:04 -04002833 * The sequence operation is not cached because we can use the slot and
2834 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002835 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002836static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002837nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2838 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002839{
Andy Adamson557ce262009-08-28 08:45:04 -04002840 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002841 struct xdr_stream *xdr = &resp->xdr;
2842 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002843 __be32 status;
2844
Andy Adamson557ce262009-08-28 08:45:04 -04002845 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002846
Andy Adamsonabfabf82009-07-23 19:02:18 -04002847 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002848 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002849 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002850
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002851 p = xdr_reserve_space(xdr, slot->sl_datalen);
2852 if (!p) {
2853 WARN_ON_ONCE(1);
2854 return nfserr_serverfault;
2855 }
2856 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2857 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002858
Andy Adamson557ce262009-08-28 08:45:04 -04002859 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002860 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002861}
2862
Andy Adamson0733d212009-04-03 08:28:01 +03002863/*
2864 * Set the exchange_id flags returned by the server.
2865 */
2866static void
2867nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2868{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002869#ifdef CONFIG_NFSD_PNFS
2870 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2871#else
Andy Adamson0733d212009-04-03 08:28:01 +03002872 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002873#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002874
2875 /* Referrals are supported, Migration is not. */
2876 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2877
2878 /* set the wire flags to return to client. */
2879 clid->flags = new->cl_exchange_flags;
2880}
2881
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002882static bool client_has_openowners(struct nfs4_client *clp)
2883{
2884 struct nfs4_openowner *oo;
2885
2886 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2887 if (!list_empty(&oo->oo_owner.so_stateids))
2888 return true;
2889 }
2890 return false;
2891}
2892
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002893static bool client_has_state(struct nfs4_client *clp)
2894{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002895 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002896#ifdef CONFIG_NFSD_PNFS
2897 || !list_empty(&clp->cl_lo_states)
2898#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002899 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002900 || !list_empty(&clp->cl_sessions)
2901 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002902}
2903
Al Virob37ad282006-10-19 23:28:59 -07002904__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02002905nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2906 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002907{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002908 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002909 struct nfs4_client *conf, *new;
2910 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002911 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002912 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002913 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002914 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002915 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002916 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002917
Jeff Layton363168b2009-08-14 12:57:56 -04002918 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002919 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002920 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002921 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002922 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002923
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002924 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002925 return nfserr_inval;
2926
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002927 new = create_client(exid->clname, rqstp, &verf);
2928 if (new == NULL)
2929 return nfserr_jukebox;
2930
Andy Adamson0733d212009-04-03 08:28:01 +03002931 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002932 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002933 exid->spo_must_enforce[0] = 0;
2934 exid->spo_must_enforce[1] = (
2935 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2936 1 << (OP_EXCHANGE_ID - 32) |
2937 1 << (OP_CREATE_SESSION - 32) |
2938 1 << (OP_DESTROY_SESSION - 32) |
2939 1 << (OP_DESTROY_CLIENTID - 32));
2940
2941 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2942 1 << (OP_OPEN_DOWNGRADE) |
2943 1 << (OP_LOCKU) |
2944 1 << (OP_DELEGRETURN));
2945
2946 exid->spo_must_allow[1] &= (
2947 1 << (OP_TEST_STATEID - 32) |
2948 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002949 if (!svc_rqst_integrity_protected(rqstp)) {
2950 status = nfserr_inval;
2951 goto out_nolock;
2952 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002953 /*
2954 * Sometimes userspace doesn't give us a principal.
2955 * Which is a bug, really. Anyway, we can't enforce
2956 * MACH_CRED in that case, better to give up now:
2957 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002958 if (!new->cl_cred.cr_principal &&
2959 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002960 status = nfserr_serverfault;
2961 goto out_nolock;
2962 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002963 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002964 case SP4_NONE:
2965 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05002966 default: /* checked by xdr code */
2967 WARN_ON_ONCE(1);
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06002968 /* fall through */
Andy Adamson0733d212009-04-03 08:28:01 +03002969 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08002970 status = nfserr_encr_alg_unsupp;
2971 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03002972 }
2973
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002974 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002975 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002976 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03002977 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002978 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2979 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2980
J. Bruce Fields136e6582012-05-12 20:37:23 -04002981 if (update) {
2982 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04002983 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03002984 goto out;
2985 }
Andrew Elblededeb132016-06-15 12:52:08 -04002986 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002987 status = nfserr_wrong_cred;
2988 goto out;
2989 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002990 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03002991 status = nfserr_perm;
2992 goto out;
2993 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002994 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03002995 status = nfserr_not_same;
2996 goto out;
2997 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04002998 /* case 6 */
2999 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003000 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003001 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003002 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003003 if (client_has_state(conf)) {
3004 status = nfserr_clid_inuse;
3005 goto out;
3006 }
Andy Adamson0733d212009-04-03 08:28:01 +03003007 goto out_new;
3008 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003009 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003010 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003011 goto out_copy;
3012 }
3013 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003014 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003015 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003016 }
3017
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003018 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003019 status = nfserr_noent;
3020 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003021 }
3022
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003023 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003024 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003025 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003026
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003027 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03003028out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003029 if (conf) {
3030 status = mark_client_expired_locked(conf);
3031 if (status)
3032 goto out;
3033 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003034 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003035 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3036 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003037
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003038 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003039 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003040out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003041 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3042 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003043
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003044 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3045 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003046
3047 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003048 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003049 status = nfs_ok;
3050
3051out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003052 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003053out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003054 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003055 expire_client(new);
3056 if (unconf)
3057 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003058 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003059}
3060
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003061static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003062check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003063{
Andy Adamson88e588d2009-07-23 19:02:15 -04003064 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3065 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003066
3067 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003068 if (slot_inuse) {
3069 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003070 return nfserr_jukebox;
3071 else
3072 return nfserr_seq_misordered;
3073 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003074 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003075 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003076 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003077 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003078 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003079 return nfserr_seq_misordered;
3080}
3081
Andy Adamson49557cc2009-07-23 19:02:16 -04003082/*
3083 * Cache the create session result into the create session single DRC
3084 * slot cache by saving the xdr structure. sl_seqid has been set.
3085 * Do this for solo or embedded create session operations.
3086 */
3087static void
3088nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003089 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003090{
3091 slot->sl_status = nfserr;
3092 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3093}
3094
3095static __be32
3096nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3097 struct nfsd4_clid_slot *slot)
3098{
3099 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3100 return slot->sl_status;
3101}
3102
Mi Jinlong1b74c252011-07-14 14:50:17 +08003103#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3104 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3105 1 + /* MIN tag is length with zero, only length */ \
3106 3 + /* version, opcount, opcode */ \
3107 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3108 /* seqid, slotID, slotID, cache */ \
3109 4 ) * sizeof(__be32))
3110
3111#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3112 2 + /* verifier: AUTH_NULL, length 0 */\
3113 1 + /* status */ \
3114 1 + /* MIN tag is length with zero, only length */ \
3115 3 + /* opcount, opcode, opstatus*/ \
3116 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3117 /* seqid, slotID, slotID, slotID, status */ \
3118 5 ) * sizeof(__be32))
3119
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003120static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003121{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003122 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3123
J. Bruce Fields373cd402013-04-08 15:42:12 -04003124 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3125 return nfserr_toosmall;
3126 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3127 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003128 ca->headerpadsz = 0;
3129 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3130 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3131 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3132 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3133 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3134 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3135 /*
3136 * Note decreasing slot size below client's request may make it
3137 * difficult for client to function correctly, whereas
3138 * decreasing the number of slots will (just?) affect
3139 * performance. When short on memory we therefore prefer to
3140 * decrease number of slots instead of their size. Clients that
3141 * request larger slots than they need will get poor results:
3142 */
3143 ca->maxreqs = nfsd4_get_drc_mem(ca);
3144 if (!ca->maxreqs)
3145 return nfserr_jukebox;
3146
J. Bruce Fields373cd402013-04-08 15:42:12 -04003147 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003148}
3149
Chuck Lever45006322016-03-01 13:06:02 -05003150/*
3151 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3152 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3153 */
3154#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3155 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3156
3157#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3158 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3159
Kinglong Mee8a891632013-12-23 18:11:02 +08003160#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003161 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003162#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003163 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3164 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003165
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003166static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3167{
3168 ca->headerpadsz = 0;
3169
Kinglong Mee8a891632013-12-23 18:11:02 +08003170 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003171 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003172 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003173 return nfserr_toosmall;
3174 ca->maxresp_cached = 0;
3175 if (ca->maxops < 2)
3176 return nfserr_toosmall;
3177
3178 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003179}
3180
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003181static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3182{
3183 switch (cbs->flavor) {
3184 case RPC_AUTH_NULL:
3185 case RPC_AUTH_UNIX:
3186 return nfs_ok;
3187 default:
3188 /*
3189 * GSS case: the spec doesn't allow us to return this
3190 * error. But it also doesn't allow us not to support
3191 * GSS.
3192 * I'd rather this fail hard than return some error the
3193 * client might think it can already handle:
3194 */
3195 return nfserr_encr_alg_unsupp;
3196 }
3197}
3198
Andy Adamson069b6ad2009-04-03 08:27:58 +03003199__be32
3200nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003201 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003202{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003203 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003204 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003205 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003206 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003207 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003208 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003209 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003210 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003211 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003212
Mi Jinlonga62573d2011-03-23 17:57:07 +08003213 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3214 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003215 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3216 if (status)
3217 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003218 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003219 if (status)
3220 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003221 status = check_backchannel_attrs(&cr_ses->back_channel);
3222 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003223 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003224 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003225 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003226 if (!new)
3227 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003228 conn = alloc_conn_from_crses(rqstp, cr_ses);
3229 if (!conn)
3230 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003231
Jeff Laytond20c11d2014-07-30 08:27:07 -04003232 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003233 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003234 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003235 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003236
3237 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003238 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003239 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003240 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003241 cs_slot = &conf->cl_cs_slot;
3242 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003243 if (status) {
3244 if (status == nfserr_replay_cache)
3245 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003246 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003247 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003248 } else if (unconf) {
3249 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003250 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003251 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003252 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003253 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003254 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003255 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003256 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003257 cs_slot = &unconf->cl_cs_slot;
3258 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003259 if (status) {
3260 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003261 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003262 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003263 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003264 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003265 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003266 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003267 if (status) {
3268 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003269 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003270 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003271 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003272 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003273 conf = unconf;
3274 } else {
3275 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003276 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003277 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003278 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003279 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003280 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003281 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003282 cr_ses->flags &= ~SESSION4_RDMA;
3283
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003284 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003285 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003286
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003287 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003288 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003289 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003290 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003291
Jeff Laytond20c11d2014-07-30 08:27:07 -04003292 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003293 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003294 spin_unlock(&nn->client_lock);
3295 /* init connection and backchannel */
3296 nfsd4_init_conn(rqstp, conn, new);
3297 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003298 if (old)
3299 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003300 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003301out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003302 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003303 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003304 if (old)
3305 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003306out_free_session:
3307 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003308out_release_drc_mem:
3309 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003310 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003311}
3312
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003313static __be32 nfsd4_map_bcts_dir(u32 *dir)
3314{
3315 switch (*dir) {
3316 case NFS4_CDFC4_FORE:
3317 case NFS4_CDFC4_BACK:
3318 return nfs_ok;
3319 case NFS4_CDFC4_FORE_OR_BOTH:
3320 case NFS4_CDFC4_BACK_OR_BOTH:
3321 *dir = NFS4_CDFC4_BOTH;
3322 return nfs_ok;
3323 };
3324 return nfserr_inval;
3325}
3326
Christoph Hellwigeb698532017-05-08 20:58:35 +02003327__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3328 struct nfsd4_compound_state *cstate,
3329 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003330{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003331 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003332 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003333 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003334 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003335
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003336 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3337 if (status)
3338 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003339 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003340 session->se_cb_prog = bc->bc_cb_program;
3341 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003342 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003343
3344 nfsd4_probe_callback(session->se_client);
3345
3346 return nfs_ok;
3347}
3348
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003349__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3350 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003351 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003352{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003353 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003354 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003355 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003356 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003357 struct net *net = SVC_NET(rqstp);
3358 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003359
3360 if (!nfsd4_last_compound_op(rqstp))
3361 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003362 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003363 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003364 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003365 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003366 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003367 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003368 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003369 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003370 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003371 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003372 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003373 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003374 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003375 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003376 goto out;
3377 nfsd4_init_conn(rqstp, conn, session);
3378 status = nfs_ok;
3379out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003380 nfsd4_put_session(session);
3381out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003382 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003383}
3384
J. Bruce Fields665d5072018-04-11 16:53:36 -04003385static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003386{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003387 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003388 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003389 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003390}
3391
Andy Adamson069b6ad2009-04-03 08:27:58 +03003392__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003393nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3394 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003395{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003396 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003397 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003398 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003399 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003400 struct net *net = SVC_NET(r);
3401 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003402
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003403 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003404 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003405 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003406 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003407 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003408 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003409 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003410 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003411 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003412 if (!ses)
3413 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003414 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003415 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003416 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003417 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003418 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003419 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003420 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003421 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003422
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003423 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003424
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003425 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003426 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003427out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003428 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003429out_client_lock:
3430 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003431out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003432 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003433}
3434
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003435static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003436{
3437 struct nfsd4_conn *c;
3438
3439 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003440 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04003441 return c;
3442 }
3443 }
3444 return NULL;
3445}
3446
J. Bruce Fields57266a62013-04-13 14:27:29 -04003447static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003448{
3449 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003450 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003451 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003452 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003453
3454 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003455 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003456 if (c)
3457 goto out_free;
3458 status = nfserr_conn_not_bound_to_session;
3459 if (clp->cl_mach_cred)
3460 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003461 __nfsd4_hash_conn(new, ses);
3462 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003463 ret = nfsd4_register_conn(new);
3464 if (ret)
3465 /* oops; xprt is already down: */
3466 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003467 return nfs_ok;
3468out_free:
3469 spin_unlock(&clp->cl_lock);
3470 free_conn(new);
3471 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003472}
3473
Mi Jinlong868b89c2011-04-27 09:09:58 +08003474static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3475{
3476 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3477
3478 return args->opcnt > session->se_fchannel.maxops;
3479}
3480
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003481static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3482 struct nfsd4_session *session)
3483{
3484 struct xdr_buf *xb = &rqstp->rq_arg;
3485
3486 return xb->len > session->se_fchannel.maxreq_sz;
3487}
3488
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003489static bool replay_matches_cache(struct svc_rqst *rqstp,
3490 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3491{
3492 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3493
3494 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3495 (bool)seq->cachethis)
3496 return false;
3497 /*
3498 * If there's an error than the reply can have fewer ops than
3499 * the call. But if we cached a reply with *more* ops than the
3500 * call you're sending us now, then this new call is clearly not
3501 * really a replay of the old one:
3502 */
3503 if (slot->sl_opcnt < argp->opcnt)
3504 return false;
3505 /* This is the only check explicitly called by spec: */
3506 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3507 return false;
3508 /*
3509 * There may be more comparisons we could actually do, but the
3510 * spec doesn't require us to catch every case where the calls
3511 * don't match (that would require caching the call as well as
3512 * the reply), so we don't bother.
3513 */
3514 return true;
3515}
3516
Andy Adamson069b6ad2009-04-03 08:27:58 +03003517__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003518nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3519 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003520{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003521 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003522 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003523 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003524 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003525 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003526 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003527 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003528 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003529 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003530 struct net *net = SVC_NET(rqstp);
3531 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003532
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003533 if (resp->opcnt != 1)
3534 return nfserr_sequence_pos;
3535
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003536 /*
3537 * Will be either used or freed by nfsd4_sequence_check_conn
3538 * below.
3539 */
3540 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3541 if (!conn)
3542 return nfserr_jukebox;
3543
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003544 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003545 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003546 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003547 goto out_no_session;
3548 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003549
Mi Jinlong868b89c2011-04-27 09:09:58 +08003550 status = nfserr_too_many_ops;
3551 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003552 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003553
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003554 status = nfserr_req_too_big;
3555 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003556 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003557
Benny Halevyb85d4c02009-04-03 08:28:08 +03003558 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003559 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003560 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003561
Andy Adamson557ce262009-08-28 08:45:04 -04003562 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003563 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3564
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003565 /* We do not negotiate the number of slots yet, so set the
3566 * maxslots to the session maxreqs which is used to encode
3567 * sr_highest_slotid and the sr_target_slot id to maxslots */
3568 seq->maxslots = session->se_fchannel.maxreqs;
3569
J. Bruce Fields73e79482012-02-13 16:39:00 -05003570 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3571 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003572 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003573 status = nfserr_seq_misordered;
3574 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003575 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003576 status = nfserr_seq_false_retry;
3577 if (!replay_matches_cache(rqstp, seq, slot))
3578 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003579 cstate->slot = slot;
3580 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003581 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003582 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003583 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003584 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003585 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003586 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003587 }
3588 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003589 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003590
J. Bruce Fields57266a62013-04-13 14:27:29 -04003591 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003592 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003593 if (status)
3594 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003595
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003596 buflen = (seq->cachethis) ?
3597 session->se_fchannel.maxresp_cached :
3598 session->se_fchannel.maxresp_sz;
3599 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3600 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003601 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003602 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003603 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003604
3605 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003606 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003607 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003608 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003609 if (seq->cachethis)
3610 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003611 else
3612 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003613
3614 cstate->slot = slot;
3615 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003616 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003617
Benny Halevyb85d4c02009-04-03 08:28:08 +03003618out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003619 switch (clp->cl_cb_state) {
3620 case NFSD4_CB_DOWN:
3621 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3622 break;
3623 case NFSD4_CB_FAULT:
3624 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3625 break;
3626 default:
3627 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003628 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003629 if (!list_empty(&clp->cl_revoked))
3630 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003631out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003632 if (conn)
3633 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003634 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003635 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003636out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003637 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003638 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003639}
3640
Trond Myklebustb6076642014-06-30 11:48:35 -04003641void
3642nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3643{
3644 struct nfsd4_compound_state *cs = &resp->cstate;
3645
3646 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003647 if (cs->status != nfserr_replay_cache) {
3648 nfsd4_store_cache_entry(resp);
3649 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3650 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003651 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003652 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003653 } else if (cs->clp)
3654 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003655}
3656
Mi Jinlong345c2842011-10-20 17:51:39 +08003657__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003658nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3659 struct nfsd4_compound_state *cstate,
3660 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003661{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003662 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003663 struct nfs4_client *conf, *unconf;
3664 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003665 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003666 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003667
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003668 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003669 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003670 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003671 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003672
3673 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003674 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003675 status = nfserr_clientid_busy;
3676 goto out;
3677 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003678 status = mark_client_expired_locked(conf);
3679 if (status)
3680 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003681 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003682 } else if (unconf)
3683 clp = unconf;
3684 else {
3685 status = nfserr_stale_clientid;
3686 goto out;
3687 }
Andrew Elblededeb132016-06-15 12:52:08 -04003688 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003689 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003690 status = nfserr_wrong_cred;
3691 goto out;
3692 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003693 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003694out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003695 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003696 if (clp)
3697 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003698 return status;
3699}
3700
Andy Adamson069b6ad2009-04-03 08:27:58 +03003701__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003702nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3703 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003704{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003705 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003706 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003707
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003708 if (rc->rca_one_fs) {
3709 if (!cstate->current_fh.fh_dentry)
3710 return nfserr_nofilehandle;
3711 /*
3712 * We don't take advantage of the rca_one_fs case.
3713 * That's OK, it's optional, we can safely ignore it.
3714 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003715 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003716 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003717
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003718 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003719 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3720 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003721 goto out;
3722
3723 status = nfserr_stale_clientid;
3724 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003725 /*
3726 * The following error isn't really legal.
3727 * But we only get here if the client just explicitly
3728 * destroyed the client. Surely it no longer cares what
3729 * error it gets back on an operation for the dead
3730 * client.
3731 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003732 goto out;
3733
3734 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003735 nfsd4_client_record_create(cstate->session->se_client);
Scott Mayhew362063a2019-03-26 18:06:28 -04003736 inc_reclaim_complete(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003737out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003738 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003739}
3740
3741__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003742nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003743 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003745 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003746 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003748 struct nfs4_client *conf, *new;
3749 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003750 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003751 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3752
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003753 new = create_client(clname, rqstp, &clverifier);
3754 if (new == NULL)
3755 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003756 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003757 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003758 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003759 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003760 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003762 if (clp_used_exchangeid(conf))
3763 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003764 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003765 char addr_str[INET6_ADDRSTRLEN];
3766 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3767 sizeof(addr_str));
3768 dprintk("NFSD: setclientid: string in use by client "
3769 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 goto out;
3771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003773 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003774 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003775 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003776 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003777 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003779 gen_confirm(new, nn);
3780 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04003781 ;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003782 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003783 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003784 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3786 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3787 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003788 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 status = nfs_ok;
3790out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003791 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003792 if (new)
3793 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003794 if (unconf)
3795 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 return status;
3797}
3798
3799
Al Virob37ad282006-10-19 23:28:59 -07003800__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003801nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003802 struct nfsd4_compound_state *cstate,
3803 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003805 struct nfsd4_setclientid_confirm *setclientid_confirm =
3806 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003807 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003808 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3810 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003811 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003812 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003814 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003816
Jeff Laytond20c11d2014-07-30 08:27:07 -04003817 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003818 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003819 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003820 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003821 * We try hard to give out unique clientid's, so if we get an
3822 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003823 * the client may be buggy; this should never happen.
3824 *
3825 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003826 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003827 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003828 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3829 goto out;
3830 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3831 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003832 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003833 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003834 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3835 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003837 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003838 status = nfserr_stale_clientid;
3839 goto out;
3840 }
3841 status = nfs_ok;
3842 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003843 old = unconf;
3844 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003845 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003846 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003847 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3848 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003849 status = nfserr_clid_inuse;
3850 if (client_has_state(old)
3851 && !same_creds(&unconf->cl_cred,
3852 &old->cl_cred))
3853 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003854 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003855 if (status) {
3856 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003857 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003858 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003859 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003860 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003861 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003862 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003863 get_client_locked(conf);
3864 spin_unlock(&nn->client_lock);
3865 nfsd4_probe_callback(conf);
3866 spin_lock(&nn->client_lock);
3867 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003869 spin_unlock(&nn->client_lock);
3870 if (old)
3871 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 return status;
3873}
3874
J. Bruce Fields32513b42011-10-13 16:00:16 -04003875static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003877 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3878}
3879
3880/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003881static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3882 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003883{
Trond Myklebust950e0112014-06-30 11:48:31 -04003884 lockdep_assert_held(&state_lock);
3885
Elena Reshetova818a34e2017-10-20 12:53:30 +03003886 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003887 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003888 INIT_LIST_HEAD(&fp->fi_stateids);
3889 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003890 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003891 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003892 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003893 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003894 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003895 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3896 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003897#ifdef CONFIG_NFSD_PNFS
3898 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003899 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003900#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003901 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902}
3903
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003904void
NeilBrowne60d4392005-06-23 22:03:01 -07003905nfsd4_free_slabs(void)
3906{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003907 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003908 kmem_cache_destroy(openowner_slab);
3909 kmem_cache_destroy(lockowner_slab);
3910 kmem_cache_destroy(file_slab);
3911 kmem_cache_destroy(stateid_slab);
3912 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003913 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003914}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Bryan Schumaker72083392011-11-01 15:24:59 -04003916int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917nfsd4_init_slabs(void)
3918{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003919 client_slab = kmem_cache_create("nfsd4_clients",
3920 sizeof(struct nfs4_client), 0, 0, NULL);
3921 if (client_slab == NULL)
3922 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003923 openowner_slab = kmem_cache_create("nfsd4_openowners",
3924 sizeof(struct nfs4_openowner), 0, 0, NULL);
3925 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04003926 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003927 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003928 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003929 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003930 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003931 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003932 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003933 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003934 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003935 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003936 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003937 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003938 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003939 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003940 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003941 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003942 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003943 odstate_slab = kmem_cache_create("nfsd4_odstate",
3944 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3945 if (odstate_slab == NULL)
3946 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003948
Sachin Bhamare8287f002015-04-27 14:50:14 +02003949out_free_deleg_slab:
3950 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003951out_free_stateid_slab:
3952 kmem_cache_destroy(stateid_slab);
3953out_free_file_slab:
3954 kmem_cache_destroy(file_slab);
3955out_free_lockowner_slab:
3956 kmem_cache_destroy(lockowner_slab);
3957out_free_openowner_slab:
3958 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003959out_free_client_slab:
3960 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003961out:
NeilBrowne60d4392005-06-23 22:03:01 -07003962 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3963 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964}
3965
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003966static void init_nfs4_replay(struct nfs4_replay *rp)
3967{
3968 rp->rp_status = nfserr_serverfault;
3969 rp->rp_buflen = 0;
3970 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04003971 mutex_init(&rp->rp_mutex);
3972}
3973
3974static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3975 struct nfs4_stateowner *so)
3976{
3977 if (!nfsd4_has_session(cstate)) {
3978 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04003979 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04003980 }
3981}
3982
3983void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3984{
3985 struct nfs4_stateowner *so = cstate->replay_owner;
3986
3987 if (so != NULL) {
3988 cstate->replay_owner = NULL;
3989 mutex_unlock(&so->so_replay.rp_mutex);
3990 nfs4_put_stateowner(so);
3991 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003992}
3993
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003994static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
3996 struct nfs4_stateowner *sop;
3997
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003998 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04003999 if (!sop)
4000 return NULL;
4001
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04004002 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004003 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004004 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004005 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004007
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004008 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004009 sop->so_client = clp;
4010 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04004011 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004012 return sop;
4013}
4014
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004015static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004016{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004017 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004018
Trond Myklebustd4f04892014-07-29 21:34:36 -04004019 list_add(&oo->oo_owner.so_strhash,
4020 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004021 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022}
4023
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004024static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4025{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004026 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004027}
4028
Jeff Layton6b180f02014-07-29 21:34:26 -04004029static void nfs4_free_openowner(struct nfs4_stateowner *so)
4030{
4031 struct nfs4_openowner *oo = openowner(so);
4032
4033 kmem_cache_free(openowner_slab, oo);
4034}
4035
4036static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004037 .so_unhash = nfs4_unhash_openowner,
4038 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004039};
4040
Andrew Elble7fc05642015-11-05 20:42:43 -05004041static struct nfs4_ol_stateid *
4042nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4043{
4044 struct nfs4_ol_stateid *local, *ret = NULL;
4045 struct nfs4_openowner *oo = open->op_openowner;
4046
4047 lockdep_assert_held(&fp->fi_lock);
4048
4049 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4050 /* ignore lock owners */
4051 if (local->st_stateowner->so_is_open_owner == 0)
4052 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004053 if (local->st_stateowner != &oo->oo_owner)
4054 continue;
4055 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004056 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004057 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05004058 break;
4059 }
4060 }
4061 return ret;
4062}
4063
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004064static __be32
4065nfsd4_verify_open_stid(struct nfs4_stid *s)
4066{
4067 __be32 ret = nfs_ok;
4068
4069 switch (s->sc_type) {
4070 default:
4071 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004072 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004073 case NFS4_CLOSED_STID:
4074 case NFS4_CLOSED_DELEG_STID:
4075 ret = nfserr_bad_stateid;
4076 break;
4077 case NFS4_REVOKED_DELEG_STID:
4078 ret = nfserr_deleg_revoked;
4079 }
4080 return ret;
4081}
4082
4083/* Lock the stateid st_mutex, and deal with races with CLOSE */
4084static __be32
4085nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4086{
4087 __be32 ret;
4088
Andrew Elble4f34bd02017-11-08 17:29:51 -05004089 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004090 ret = nfsd4_verify_open_stid(&stp->st_stid);
4091 if (ret != nfs_ok)
4092 mutex_unlock(&stp->st_mutex);
4093 return ret;
4094}
4095
4096static struct nfs4_ol_stateid *
4097nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4098{
4099 struct nfs4_ol_stateid *stp;
4100 for (;;) {
4101 spin_lock(&fp->fi_lock);
4102 stp = nfsd4_find_existing_open(fp, open);
4103 spin_unlock(&fp->fi_lock);
4104 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4105 break;
4106 nfs4_put_stid(&stp->st_stid);
4107 }
4108 return stp;
4109}
4110
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004111static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004112alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004113 struct nfsd4_compound_state *cstate)
4114{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004115 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004116 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004118 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4119 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004121 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004122 oo->oo_owner.so_is_open_owner = 1;
4123 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004124 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004125 if (nfsd4_has_session(cstate))
4126 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004127 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004128 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004129 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004130 spin_lock(&clp->cl_lock);
4131 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004132 if (ret == NULL) {
4133 hash_openowner(oo, clp, strhashval);
4134 ret = oo;
4135 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004136 nfs4_free_stateowner(&oo->oo_owner);
4137
Trond Myklebustd4f04892014-07-29 21:34:36 -04004138 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004139 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140}
4141
Andrew Elble7fc05642015-11-05 20:42:43 -05004142static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004143init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Andrew Elble7fc05642015-11-05 20:42:43 -05004146 struct nfs4_openowner *oo = open->op_openowner;
4147 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004148 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004149
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004150 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004151 /* We are moving these outside of the spinlocks to avoid the warnings */
4152 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004153 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004154
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004155retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004156 spin_lock(&oo->oo_owner.so_client->cl_lock);
4157 spin_lock(&fp->fi_lock);
4158
4159 retstp = nfsd4_find_existing_open(fp, open);
4160 if (retstp)
4161 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004162
4163 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004164 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004165 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004166 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004167 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004168 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004169 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 stp->st_access_bmap = 0;
4171 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004172 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004173 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004174 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004175
4176out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004177 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004178 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004179 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004180 /* Handle races with CLOSE */
4181 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4182 nfs4_put_stid(&retstp->st_stid);
4183 goto retry;
4184 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004185 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004186 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004187 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004188 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004189 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190}
4191
Jeff Laytond3134b12014-07-29 21:34:32 -04004192/*
4193 * In the 4.0 case we need to keep the owners around a little while to handle
4194 * CLOSE replay. We still do need to release any file access that is held by
4195 * them before returning however.
4196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004198move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199{
Jeff Layton217526e2014-07-30 08:27:11 -04004200 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004201 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4202 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4203 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004204
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004205 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Jeff Laytonb401be222014-07-29 21:34:33 -04004207 /*
4208 * We know that we hold one reference via nfsd4_close, and another
4209 * "persistent" reference for the client. If the refcount is higher
4210 * than 2, then there are still calls in progress that are using this
4211 * stateid. We can't put the sc_file reference until they are finished.
4212 * Wait for the refcount to drop to 2. Since it has been unhashed,
4213 * there should be no danger of the refcount going back up again at
4214 * this point.
4215 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004216 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004217
Jeff Laytond3134b12014-07-29 21:34:32 -04004218 release_all_access(s);
4219 if (s->st_stid.sc_file) {
4220 put_nfs4_file(s->st_stid.sc_file);
4221 s->st_stid.sc_file = NULL;
4222 }
Jeff Layton217526e2014-07-30 08:27:11 -04004223
4224 spin_lock(&nn->client_lock);
4225 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004226 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004227 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004228 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004229 spin_unlock(&nn->client_lock);
4230 if (last)
4231 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232}
4233
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234/* search file_hashtbl[] for file */
4235static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04004236find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 struct nfs4_file *fp;
4239
Jeff Layton5b095e92014-10-23 08:01:02 -04004240 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02004241 if (fh_match(&fp->fi_fhandle, fh)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004242 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004243 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 }
4246 return NULL;
4247}
4248
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02004249struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04004250find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004251{
4252 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004253 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004254
Jeff Layton5b095e92014-10-23 08:01:02 -04004255 rcu_read_lock();
4256 fp = find_file_locked(fh, hashval);
4257 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004258 return fp;
4259}
4260
4261static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004262find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004263{
4264 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004265 unsigned int hashval = file_hashval(fh);
4266
4267 rcu_read_lock();
4268 fp = find_file_locked(fh, hashval);
4269 rcu_read_unlock();
4270 if (fp)
4271 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004272
4273 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04004274 fp = find_file_locked(fh, hashval);
4275 if (likely(fp == NULL)) {
4276 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04004277 fp = new;
4278 }
4279 spin_unlock(&state_lock);
4280
4281 return fp;
4282}
4283
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004284/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 * Called to check deny when READ with all zero stateid or
4286 * WRITE with all zero or all one stateid
4287 */
Al Virob37ad282006-10-19 23:28:59 -07004288static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4290{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004292 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
Trond Myklebustca943212014-07-23 16:17:39 -04004294 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07004295 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004296 return ret;
4297 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004298 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004299 if (fp->fi_share_deny & deny_type)
4300 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004301 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004302 put_nfs4_file(fp);
4303 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304}
4305
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004306static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004308 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004309 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4310 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004311
Trond Myklebust11b91642014-07-29 21:34:08 -04004312 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004313
Jeff Layton02e12152014-07-16 10:31:57 -04004314 /*
4315 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004316 * already holding inode->i_lock.
4317 *
Jeff Laytondff13992014-07-08 14:02:49 -04004318 * If the dl_time != 0, then we know that it has already been
4319 * queued for a lease break. Don't queue it again.
4320 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004321 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004322 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04004323 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004324 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004325 }
Jeff Layton02e12152014-07-16 10:31:57 -04004326 spin_unlock(&state_lock);
4327}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004329static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4330 struct rpc_task *task)
4331{
4332 struct nfs4_delegation *dp = cb_to_delegation(cb);
4333
Andrew Elblea4579742015-08-31 12:06:41 -04004334 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
4335 return 1;
4336
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004337 switch (task->tk_status) {
4338 case 0:
4339 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004340 case -NFS4ERR_DELAY:
4341 rpc_delay(task, 2 * HZ);
4342 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004343 case -EBADHANDLE:
4344 case -NFS4ERR_BAD_STATEID:
4345 /*
4346 * Race: client probably got cb_recall before open reply
4347 * granting delegation.
4348 */
4349 if (dp->dl_retries--) {
4350 rpc_delay(task, 2 * HZ);
4351 return 0;
4352 }
4353 /*FALLTHRU*/
4354 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004355 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004356 }
4357}
4358
4359static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4360{
4361 struct nfs4_delegation *dp = cb_to_delegation(cb);
4362
4363 nfs4_put_stid(&dp->dl_stid);
4364}
4365
Julia Lawallc4cb8972015-11-21 22:57:39 +01004366static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004367 .prepare = nfsd4_cb_recall_prepare,
4368 .done = nfsd4_cb_recall_done,
4369 .release = nfsd4_cb_recall_release,
4370};
4371
Jeff Layton02e12152014-07-16 10:31:57 -04004372static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4373{
4374 /*
4375 * We're assuming the state code never drops its reference
4376 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004377 * callback (and since the lease code is serialized by the
4378 * i_lock) we know the server hasn't removed the lease yet, and
4379 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004380 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004381 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004382 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004383}
4384
Jeff Layton1c8c6012013-06-21 08:58:15 -04004385/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004386static bool
4387nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004388{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004389 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004390 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4391 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004392
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004393 /*
4394 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004395 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004396 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004397 */
4398 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
Jeff Layton02e12152014-07-16 10:31:57 -04004400 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004401 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004402 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004403 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004404 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405}
4406
Jeff Laytonc45198e2014-09-01 07:12:07 -04004407static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004408nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4409 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
4411 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004412 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 else
4414 return -EAGAIN;
4415}
4416
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004417static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004418 .lm_break = nfsd_break_deleg_cb,
4419 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420};
4421
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004422static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4423{
4424 if (nfsd4_has_session(cstate))
4425 return nfs_ok;
4426 if (seqid == so->so_seqid - 1)
4427 return nfserr_replay_me;
4428 if (seqid == so->so_seqid)
4429 return nfs_ok;
4430 return nfserr_bad_seqid;
4431}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Jeff Layton4b24ca72014-06-30 11:48:44 -04004433static __be32 lookup_clientid(clientid_t *clid,
4434 struct nfsd4_compound_state *cstate,
4435 struct nfsd_net *nn)
4436{
4437 struct nfs4_client *found;
4438
4439 if (cstate->clp) {
4440 found = cstate->clp;
4441 if (!same_clid(&found->cl_clientid, clid))
4442 return nfserr_stale_clientid;
4443 return nfs_ok;
4444 }
4445
4446 if (STALE_CLIENTID(clid, nn))
4447 return nfserr_stale_clientid;
4448
4449 /*
4450 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4451 * cached already then we know this is for is for v4.0 and "sessions"
4452 * will be false.
4453 */
4454 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004455 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004456 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004457 if (!found) {
4458 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004459 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004460 }
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04004461 atomic_inc(&found->cl_rpc_users);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004462 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004463
4464 /* Cache the nfs4_client in cstate! */
4465 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004466 return nfs_ok;
4467}
4468
Al Virob37ad282006-10-19 23:28:59 -07004469__be32
Andy Adamson66689582009-04-03 08:28:45 +03004470nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004471 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 clientid_t *clientid = &open->op_clientid;
4474 struct nfs4_client *clp = NULL;
4475 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004476 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004477 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004479 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004481 /*
4482 * In case we need it later, after we've already created the
4483 * file and don't want to risk a further failure:
4484 */
4485 open->op_file = nfsd4_alloc_file();
4486 if (open->op_file == NULL)
4487 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Trond Myklebust2d91e892014-06-30 11:48:46 -04004489 status = lookup_clientid(clientid, cstate, nn);
4490 if (status)
4491 return status;
4492 clp = cstate->clp;
4493
Trond Myklebustd4f04892014-07-29 21:34:36 -04004494 strhashval = ownerstr_hashval(&open->op_owner);
4495 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004496 open->op_openowner = oo;
4497 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004498 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004500 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004501 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004502 release_openowner(oo);
4503 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004504 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004505 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004506 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4507 if (status)
4508 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004509 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004510new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004511 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004512 if (oo == NULL)
4513 return nfserr_jukebox;
4514 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004515alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004516 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004517 if (!open->op_stp)
4518 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004519
4520 if (nfsd4_has_session(cstate) &&
4521 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4522 open->op_odstate = alloc_clnt_odstate(clp);
4523 if (!open->op_odstate)
4524 return nfserr_jukebox;
4525 }
4526
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004527 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528}
4529
Al Virob37ad282006-10-19 23:28:59 -07004530static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004531nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4532{
4533 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4534 return nfserr_openmode;
4535 else
4536 return nfs_ok;
4537}
4538
Daniel Mackc47d8322011-05-16 16:38:14 +02004539static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004540{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004541 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4542}
4543
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004544static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004545{
4546 struct nfs4_stid *ret;
4547
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004548 ret = find_stateid_by_type(cl, s,
4549 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004550 if (!ret)
4551 return NULL;
4552 return delegstateid(ret);
4553}
4554
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004555static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4556{
4557 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4558 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4559}
4560
Al Virob37ad282006-10-19 23:28:59 -07004561static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004562nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004563 struct nfs4_delegation **dp)
4564{
4565 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004566 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004567 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004568
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004569 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4570 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004571 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004572 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4573 nfs4_put_stid(&deleg->dl_stid);
4574 if (cl->cl_minorversion)
4575 status = nfserr_deleg_revoked;
4576 goto out;
4577 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004578 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004579 status = nfs4_check_delegmode(deleg, flags);
4580 if (status) {
4581 nfs4_put_stid(&deleg->dl_stid);
4582 goto out;
4583 }
4584 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004585out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004586 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004587 return nfs_ok;
4588 if (status)
4589 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004590 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004591 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004592}
4593
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004594static inline int nfs4_access_to_access(u32 nfs4_access)
4595{
4596 int flags = 0;
4597
4598 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4599 flags |= NFSD_MAY_READ;
4600 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4601 flags |= NFSD_MAY_WRITE;
4602 return flags;
4603}
4604
Al Virob37ad282006-10-19 23:28:59 -07004605static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4607 struct nfsd4_open *open)
4608{
4609 struct iattr iattr = {
4610 .ia_valid = ATTR_SIZE,
4611 .ia_size = 0,
4612 };
4613 if (!open->op_truncate)
4614 return 0;
4615 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004616 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4618}
4619
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004620static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004621 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4622 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004623{
Trond Myklebustde186432014-07-10 14:07:26 -04004624 struct file *filp = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004625 __be32 status;
4626 int oflag = nfs4_access_to_omode(open->op_share_access);
4627 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004628 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004629
Trond Myklebustde186432014-07-10 14:07:26 -04004630 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004631
4632 /*
4633 * Are we trying to set a deny mode that would conflict with
4634 * current access?
4635 */
4636 status = nfs4_file_check_deny(fp, open->op_share_deny);
4637 if (status != nfs_ok) {
4638 spin_unlock(&fp->fi_lock);
4639 goto out;
4640 }
4641
4642 /* set access to the file */
4643 status = nfs4_file_get_access(fp, open->op_share_access);
4644 if (status != nfs_ok) {
4645 spin_unlock(&fp->fi_lock);
4646 goto out;
4647 }
4648
4649 /* Set access bits in stateid */
4650 old_access_bmap = stp->st_access_bmap;
4651 set_access(open->op_share_access, stp);
4652
4653 /* Set new deny mask */
4654 old_deny_bmap = stp->st_deny_bmap;
4655 set_deny(open->op_share_deny, stp);
4656 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4657
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004658 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004659 spin_unlock(&fp->fi_lock);
4660 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004661 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004662 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004663 spin_lock(&fp->fi_lock);
4664 if (!fp->fi_fds[oflag]) {
4665 fp->fi_fds[oflag] = filp;
4666 filp = NULL;
4667 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004668 }
Trond Myklebustde186432014-07-10 14:07:26 -04004669 spin_unlock(&fp->fi_lock);
4670 if (filp)
4671 fput(filp);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004672
4673 status = nfsd4_truncate(rqstp, cur_fh, open);
4674 if (status)
4675 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004676out:
4677 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004678out_put_access:
4679 stp->st_access_bmap = old_access_bmap;
4680 nfs4_file_put_access(fp, open->op_share_access);
4681 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4682 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004683}
4684
Al Virob37ad282006-10-19 23:28:59 -07004685static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004686nfs4_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 -07004687{
Al Virob37ad282006-10-19 23:28:59 -07004688 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004689 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004691 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004692 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004693
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004694 /* test and set deny mode */
4695 spin_lock(&fp->fi_lock);
4696 status = nfs4_file_check_deny(fp, open->op_share_deny);
4697 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004698 set_deny(open->op_share_deny, stp);
4699 fp->fi_share_deny |=
4700 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4701 }
4702 spin_unlock(&fp->fi_lock);
4703
4704 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004707 status = nfsd4_truncate(rqstp, cur_fh, open);
4708 if (status != nfs_ok)
4709 reset_union_bmap_deny(old_deny_bmap, stp);
4710 return status;
4711}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004713/* Should we give out recallable state?: */
4714static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4715{
4716 if (clp->cl_cb_state == NFSD4_CB_UP)
4717 return true;
4718 /*
4719 * In the sessions case, since we don't have to establish a
4720 * separate connection for callbacks, we assume it's OK
4721 * until we hear otherwise:
4722 */
4723 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4724}
4725
J. Bruce Fields653e5142018-02-15 22:08:45 -05004726static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4727 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004728{
4729 struct file_lock *fl;
4730
4731 fl = locks_alloc_lock();
4732 if (!fl)
4733 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004734 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004735 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004736 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4737 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004738 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004739 fl->fl_pid = current->tgid;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004740 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004741 return fl;
4742}
4743
Jeff Layton0b266932014-07-25 07:34:25 -04004744static struct nfs4_delegation *
4745nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004746 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004747{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004748 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004749 struct nfs4_delegation *dp;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004750 struct file *filp;
4751 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004752
J. Bruce Fields353601e2018-02-16 14:29:42 -05004753 /*
4754 * The fi_had_conflict and nfs_get_existing_delegation checks
4755 * here are just optimizations; we'll need to recheck them at
4756 * the end:
4757 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004758 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004759 return ERR_PTR(-EAGAIN);
4760
J. Bruce Fields353601e2018-02-16 14:29:42 -05004761 filp = find_readable_file(fp);
4762 if (!filp) {
4763 /* We should always have a readable file here */
4764 WARN_ON_ONCE(1);
4765 return ERR_PTR(-EBADF);
4766 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004767 spin_lock(&state_lock);
4768 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004769 if (nfs4_delegation_exists(clp, fp))
4770 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004771 else if (!fp->fi_deleg_file) {
4772 fp->fi_deleg_file = filp;
4773 /* increment early to prevent fi_deleg_file from being
4774 * cleared */
4775 fp->fi_delegees = 1;
4776 filp = NULL;
4777 } else
4778 fp->fi_delegees++;
4779 spin_unlock(&fp->fi_lock);
4780 spin_unlock(&state_lock);
4781 if (filp)
4782 fput(filp);
4783 if (status)
4784 return ERR_PTR(status);
4785
4786 status = -ENOMEM;
4787 dp = alloc_init_deleg(clp, fp, fh, odstate);
4788 if (!dp)
4789 goto out_delegees;
4790
4791 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4792 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04004793 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004794
4795 status = vfs_setlease(fp->fi_deleg_file, fl->fl_type, &fl, NULL);
4796 if (fl)
4797 locks_free_lock(fl);
4798 if (status)
4799 goto out_clnt_odstate;
4800
4801 spin_lock(&state_lock);
4802 spin_lock(&fp->fi_lock);
4803 if (fp->fi_had_conflict)
4804 status = -EAGAIN;
4805 else
4806 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04004807 spin_unlock(&fp->fi_lock);
4808 spin_unlock(&state_lock);
4809
4810 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04004811 goto out_unlock;
4812
Jeff Layton0b266932014-07-25 07:34:25 -04004813 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04004814out_unlock:
4815 vfs_setlease(fp->fi_deleg_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004816out_clnt_odstate:
4817 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004818 nfs4_put_stid(&dp->dl_stid);
4819out_delegees:
4820 put_deleg_file(fp);
4821 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004822}
4823
Benny Halevy4aa89132012-02-21 14:16:44 -08004824static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4825{
4826 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4827 if (status == -EAGAIN)
4828 open->op_why_no_deleg = WND4_CONTENTION;
4829 else {
4830 open->op_why_no_deleg = WND4_RESOURCE;
4831 switch (open->op_deleg_want) {
4832 case NFS4_SHARE_WANT_READ_DELEG:
4833 case NFS4_SHARE_WANT_WRITE_DELEG:
4834 case NFS4_SHARE_WANT_ANY_DELEG:
4835 break;
4836 case NFS4_SHARE_WANT_CANCEL:
4837 open->op_why_no_deleg = WND4_CANCELLED;
4838 break;
4839 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004840 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004841 }
4842 }
4843}
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845/*
4846 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004847 *
4848 * Note we don't support write delegations, and won't until the vfs has
4849 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 */
4851static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004852nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4853 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854{
4855 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004856 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4857 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004858 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004859 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004861 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004862 open->op_recall = 0;
4863 switch (open->op_claim_type) {
4864 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004865 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004866 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004867 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4868 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004869 break;
4870 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004871 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004872 /*
4873 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004874 * had the chance to reclaim theirs, *and* until
4875 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004876 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004877 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004878 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004879 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004880 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004881 /*
4882 * Also, if the file was opened for write or
4883 * create, there's a good chance the client's
4884 * about to write to it, resulting in an
4885 * immediate recall (since we don't support
4886 * write delegations):
4887 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004888 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004889 goto out_no_deleg;
4890 if (open->op_create == NFS4_OPEN_CREATE)
4891 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004892 break;
4893 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004894 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004895 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004896 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004897 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004898 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004900 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004902 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004903 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004904 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004905 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004906 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004907out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004908 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4909 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004910 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004911 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004912 open->op_recall = 1;
4913 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004914
4915 /* 4.1 client asking for a delegation? */
4916 if (open->op_deleg_want)
4917 nfsd4_open_deleg_none_ext(open, status);
4918 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919}
4920
Benny Halevye27f49c2012-02-21 14:16:54 -08004921static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4922 struct nfs4_delegation *dp)
4923{
4924 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4925 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4926 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4927 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4928 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4929 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4930 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4931 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4932 }
4933 /* Otherwise the client must be confused wanting a delegation
4934 * it already has, therefore we don't return
4935 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4936 */
4937}
4938
Al Virob37ad282006-10-19 23:28:59 -07004939__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4941{
Andy Adamson66689582009-04-03 08:28:45 +03004942 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004943 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004945 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004946 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004947 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004948 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 /*
4951 * Lookup file; if found, lookup stateid and check open request,
4952 * and check for delegations in the process of being recalled.
4953 * If not found, create the nfs4_file struct
4954 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004955 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004956 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004957 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004958 if (status)
4959 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004960 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004962 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004963 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004964 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004965 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 }
4967
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004968 if (!stp) {
4969 stp = init_open_stateid(fp, open);
4970 if (!open->op_stp)
4971 new_stp = true;
4972 }
4973
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 /*
4975 * OPEN the file, or upgrade an existing OPEN.
4976 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004977 *
4978 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004980 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04004982 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04004983 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04004984 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04004986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004988 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4989 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004990 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004991 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004992 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004993 goto out;
4994 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004995
4996 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
4997 open->op_odstate);
4998 if (stp->st_clnt_odstate == open->op_odstate)
4999 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005001
Jeff Layton9767feb2015-10-01 09:05:50 -04005002 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005003 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
Benny Halevyd24433c2012-02-16 20:57:17 +02005005 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005006 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5007 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5008 open->op_why_no_deleg = WND4_NOT_WANTED;
5009 goto nodeleg;
5010 }
5011 }
5012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 /*
5014 * Attempt to hand out a delegation. No error return, because the
5015 * OPEN succeeds even if we fail.
5016 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005017 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005018nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 status = nfs_ok;
5020
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005021 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005022 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005024 /* 4.1 client trying to upgrade/downgrade delegation? */
5025 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005026 open->op_deleg_want)
5027 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005028
NeilBrown13cd2182005-06-23 22:03:10 -07005029 if (fp)
5030 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005031 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005032 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 /*
5034 * To finish the open response, we just need to set the rflags.
5035 */
5036 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005037 if (nfsd4_has_session(&resp->cstate))
5038 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5039 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005041
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005042 if (dp)
5043 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005044 if (stp)
5045 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
5047 return status;
5048}
5049
Jeff Layton58fb12e2014-07-29 21:34:27 -04005050void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005051 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005052{
5053 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005054 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005055
Jeff Laytond3134b12014-07-29 21:34:32 -04005056 nfsd4_cstate_assign_replay(cstate, so);
5057 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005058 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005059 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005060 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005061 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005062 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005063 if (open->op_odstate)
5064 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005065}
5066
Al Virob37ad282006-10-19 23:28:59 -07005067__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005068nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005069 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005071 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005073 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005074 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 dprintk("process_renew(%08x/%08x): starting\n",
5077 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04005078 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005079 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005081 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07005083 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005084 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 goto out;
5086 status = nfs_ok;
5087out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 return status;
5089}
5090
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005091void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005092nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005093{
Jeff Layton33dcc482012-04-10 11:08:48 -04005094 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005095 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005096 return;
5097
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005098 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005099 /*
5100 * If the server goes down again right now, an NFSv4
5101 * client will still be allowed to reclaim after it comes back up,
5102 * even if it hasn't yet had a chance to reclaim state this time.
5103 *
5104 */
Jeff Layton919b8042014-09-12 16:40:20 -04005105 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005106 /*
5107 * At this point, NFSv4 clients can still reclaim. But if the
5108 * server crashes, any that have not yet reclaimed will be out
5109 * of luck on the next boot.
5110 *
5111 * (NFSv4.1+ clients are considered to have reclaimed once they
5112 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5113 * have reclaimed after their first OPEN.)
5114 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005115 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005116 /*
5117 * At this point, and once lockd and/or any other containers
5118 * exit their grace period, further reclaims will fail and
5119 * regular locking can resume.
5120 */
NeilBrowna76b4312005-06-23 22:04:01 -07005121}
5122
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005123/*
5124 * If we've waited a lease period but there are still clients trying to
5125 * reclaim, wait a little longer to give them a chance to finish.
5126 */
5127static bool clients_still_reclaiming(struct nfsd_net *nn)
5128{
5129 unsigned long now = get_seconds();
5130 unsigned long double_grace_period_end = nn->boot_time +
5131 2 * nn->nfsd4_lease;
5132
Scott Mayhew362063a2019-03-26 18:06:28 -04005133 if (nn->track_reclaim_completes &&
5134 atomic_read(&nn->nr_reclaim_complete) ==
5135 nn->reclaim_str_hashtbl_size)
5136 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005137 if (!nn->somebody_reclaimed)
5138 return false;
5139 nn->somebody_reclaimed = false;
5140 /*
5141 * If we've given them *two* lease times to reclaim, and they're
5142 * still not done, give up:
5143 */
5144 if (time_after(now, double_grace_period_end))
5145 return false;
5146 return true;
5147}
5148
NeilBrownfd39ca92005-06-23 22:04:03 -07005149static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005150nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151{
5152 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005153 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005155 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005156 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03005158 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04005159 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005162
5163 if (clients_still_reclaiming(nn)) {
5164 new_timeo = 0;
5165 goto out;
5166 }
Scott Mayhew362063a2019-03-26 18:06:28 -04005167 dprintk("NFSD: end of grace period\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005168 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005169 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005170 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005171 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 clp = list_entry(pos, struct nfs4_client, cl_lru);
5173 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
5174 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04005175 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 break;
5177 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04005178 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03005179 dprintk("NFSD: client in use (clientid %08x)\n",
5180 clp->cl_clientid.cl_id);
5181 continue;
5182 }
Trond Myklebust4864af92014-07-30 08:27:03 -04005183 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005184 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005185 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005186 list_for_each_safe(pos, next, &reaplist) {
5187 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 dprintk("NFSD: purging unused client (clientid %08x)\n",
5189 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04005190 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 expire_client(clp);
5192 }
Benny Halevycdc975052014-05-30 09:09:30 -04005193 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005194 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5196 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005197 t = dp->dl_time - cutoff;
5198 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 break;
5200 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005201 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005202 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 }
Benny Halevycdc975052014-05-30 09:09:30 -04005204 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005205 while (!list_empty(&reaplist)) {
5206 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5207 dl_recall_lru);
5208 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005209 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 }
Jeff Layton217526e2014-07-30 08:27:11 -04005211
5212 spin_lock(&nn->client_lock);
5213 while (!list_empty(&nn->close_lru)) {
5214 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5215 oo_close_lru);
5216 if (time_after((unsigned long)oo->oo_time,
5217 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005218 t = oo->oo_time - cutoff;
5219 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 break;
5221 }
Jeff Layton217526e2014-07-30 08:27:11 -04005222 list_del_init(&oo->oo_close_lru);
5223 stp = oo->oo_last_closed_stid;
5224 oo->oo_last_closed_stid = NULL;
5225 spin_unlock(&nn->client_lock);
5226 nfs4_put_stid(&stp->st_stid);
5227 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 }
Jeff Layton217526e2014-07-30 08:27:11 -04005229 spin_unlock(&nn->client_lock);
5230
Jeff Layton7919d0a2016-09-16 16:28:25 -04005231 /*
5232 * It's possible for a client to try and acquire an already held lock
5233 * that is being held for a long time, and then lose interest in it.
5234 * So, we clean out any un-revisited request after a lease period
5235 * under the assumption that the client is no longer interested.
5236 *
5237 * RFC5661, sec. 9.6 states that the client must not rely on getting
5238 * notifications and must continue to poll for locks, even when the
5239 * server supports them. Thus this shouldn't lead to clients blocking
5240 * indefinitely once the lock does become free.
5241 */
5242 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005243 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005244 while (!list_empty(&nn->blocked_locks_lru)) {
5245 nbl = list_first_entry(&nn->blocked_locks_lru,
5246 struct nfsd4_blocked_lock, nbl_lru);
5247 if (time_after((unsigned long)nbl->nbl_time,
5248 (unsigned long)cutoff)) {
5249 t = nbl->nbl_time - cutoff;
5250 new_timeo = min(new_timeo, t);
5251 break;
5252 }
5253 list_move(&nbl->nbl_lru, &reaplist);
5254 list_del_init(&nbl->nbl_list);
5255 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005256 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005257
5258 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005259 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005260 struct nfsd4_blocked_lock, nbl_lru);
5261 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005262 free_blocked_lock(nbl);
5263 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005264out:
Jeff Laytona832e7a2014-05-30 09:09:26 -04005265 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04005266 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267}
5268
Harvey Harrisona254b242008-02-20 12:49:00 -08005269static struct workqueue_struct *laundry_wq;
5270static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005271
5272static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005273laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274{
5275 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005276 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005277 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5278 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005280 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005282 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283}
5284
Jeff Layton8fcd4612015-07-30 06:57:46 -04005285static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005286{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005287 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005288 return nfserr_bad_stateid;
5289 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290}
5291
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005293access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005295 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5296 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5297 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298}
5299
5300static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005301access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005303 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5304 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305}
5306
5307static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005308__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309{
Al Virob37ad282006-10-19 23:28:59 -07005310 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
J. Bruce Fields02921912010-07-29 15:16:59 -04005312 /* For lock stateid's, we test the parent open, not the lock: */
5313 if (stp->st_openstp)
5314 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005315 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005317 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 goto out;
5319 status = nfs_ok;
5320out:
5321 return status;
5322}
5323
Al Virob37ad282006-10-19 23:28:59 -07005324static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005325check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005327 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005329 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005330 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 * conflicting state; so we must wait out the grace period. */
5332 return nfserr_grace;
5333 } else if (flags & WR_STATE)
5334 return nfs4_share_conflict(current_fh,
5335 NFS4_SHARE_DENY_WRITE);
5336 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5337 return nfs4_share_conflict(current_fh,
5338 NFS4_SHARE_DENY_READ);
5339}
5340
5341/*
5342 * Allow READ/WRITE during grace period on recovered state only for files
5343 * that are not able to provide mandatory locking.
5344 */
5345static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005346grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005348 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349}
5350
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005351static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005352{
Andy Adamson66689582009-04-03 08:28:45 +03005353 /*
5354 * When sessions are used the stateid generation number is ignored
5355 * when it is zero.
5356 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005357 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005358 return nfs_ok;
5359
5360 if (in->si_generation == ref->si_generation)
5361 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005362
J. Bruce Fields0836f582008-01-26 19:08:12 -05005363 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005364 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005365 return nfserr_bad_stateid;
5366 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005367 * However, we could see a stateid from the past, even from a
5368 * non-buggy client. For example, if the client sends a lock
5369 * while some IO is outstanding, the lock may bump si_generation
5370 * while the IO is still in flight. The client could avoid that
5371 * situation by waiting for responses on all the IO requests,
5372 * but better performance may result in retrying IO that
5373 * receives an old_stateid error if requests are rarely
5374 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005375 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005376 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005377}
5378
Trond Myklebust03da3162017-11-03 08:00:16 -04005379static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5380{
5381 __be32 ret;
5382
5383 spin_lock(&s->sc_lock);
5384 ret = nfsd4_verify_open_stid(s);
5385 if (ret == nfs_ok)
5386 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5387 spin_unlock(&s->sc_lock);
5388 return ret;
5389}
5390
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005391static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5392{
5393 if (ols->st_stateowner->so_is_open_owner &&
5394 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5395 return nfserr_bad_stateid;
5396 return nfs_ok;
5397}
5398
Chuck Lever7df302f2012-05-29 13:56:37 -04005399static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005400{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005401 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005402 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005403
Andrew Elbleae254dac2017-11-09 13:41:10 -05005404 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5405 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005406 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005407 /* Client debugging aid. */
5408 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5409 char addr_str[INET6_ADDRSTRLEN];
5410 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5411 sizeof(addr_str));
5412 pr_warn_ratelimited("NFSD: client %s testing state ID "
5413 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005414 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005415 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005416 spin_lock(&cl->cl_lock);
5417 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005418 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005419 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005420 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005421 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005422 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005423 switch (s->sc_type) {
5424 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005425 status = nfs_ok;
5426 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005427 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005428 status = nfserr_deleg_revoked;
5429 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005430 case NFS4_OPEN_STID:
5431 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005432 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005433 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005434 default:
5435 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005436 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04005437 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005438 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005439 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005440 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005441out_unlock:
5442 spin_unlock(&cl->cl_lock);
5443 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005444}
5445
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005446__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005447nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5448 stateid_t *stateid, unsigned char typemask,
5449 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005450{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005451 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005452 bool return_revoked = false;
5453
5454 /*
5455 * only return revoked delegations if explicitly asked.
5456 * otherwise we report revoked or bad_stateid status.
5457 */
5458 if (typemask & NFS4_REVOKED_DELEG_STID)
5459 return_revoked = true;
5460 else if (typemask & NFS4_DELEG_STID)
5461 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005462
Andrew Elbleae254dac2017-11-09 13:41:10 -05005463 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5464 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005465 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005466 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005467 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005468 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005469 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005470 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005471 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005472 if (status)
5473 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005474 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005475 if (!*s)
5476 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005477 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5478 nfs4_put_stid(*s);
5479 if (cstate->minorversion)
5480 return nfserr_deleg_revoked;
5481 return nfserr_bad_stateid;
5482 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005483 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005484}
5485
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005486static struct file *
5487nfs4_find_file(struct nfs4_stid *s, int flags)
5488{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005489 if (!s)
5490 return NULL;
5491
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005492 switch (s->sc_type) {
5493 case NFS4_DELEG_STID:
5494 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5495 return NULL;
5496 return get_file(s->sc_file->fi_deleg_file);
5497 case NFS4_OPEN_STID:
5498 case NFS4_LOCK_STID:
5499 if (flags & RD_STATE)
5500 return find_readable_file(s->sc_file);
5501 else
5502 return find_writeable_file(s->sc_file);
5503 break;
5504 }
5505
5506 return NULL;
5507}
5508
5509static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005510nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005511{
5512 __be32 status;
5513
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005514 status = nfsd4_check_openowner_confirmed(ols);
5515 if (status)
5516 return status;
5517 return nfs4_check_openmode(ols, flags);
5518}
5519
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005520static __be32
5521nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5522 struct file **filpp, bool *tmp_file, int flags)
5523{
5524 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5525 struct file *file;
5526 __be32 status;
5527
5528 file = nfs4_find_file(s, flags);
5529 if (file) {
5530 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5531 acc | NFSD_MAY_OWNER_OVERRIDE);
5532 if (status) {
5533 fput(file);
5534 return status;
5535 }
5536
5537 *filpp = file;
5538 } else {
5539 status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
5540 if (status)
5541 return status;
5542
5543 if (tmp_file)
5544 *tmp_file = true;
5545 }
5546
5547 return 0;
5548}
5549
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005551 * Checks for stateid operations
5552 */
Al Virob37ad282006-10-19 23:28:59 -07005553__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005554nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005555 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5556 stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005558 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005559 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005560 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005561 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005562 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 if (filpp)
5565 *filpp = NULL;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005566 if (tmp_file)
5567 *tmp_file = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005569 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 return nfserr_grace;
5571
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005572 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5573 status = check_special_stateids(net, fhp, stateid, flags);
5574 goto done;
5575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005577 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005578 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005579 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005580 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005581 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005582 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005583 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005584 if (status)
5585 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005586
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005587 switch (s->sc_type) {
5588 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005589 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005590 break;
5591 case NFS4_OPEN_STID:
5592 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005593 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005594 break;
5595 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005596 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005597 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005599 if (status)
5600 goto out;
5601 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005602
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005603done:
5604 if (!status && filpp)
5605 status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005607 if (s)
5608 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 return status;
5610}
5611
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005612/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005613 * Test if the stateid is valid
5614 */
5615__be32
5616nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005617 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005618{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005619 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005620 struct nfsd4_test_stateid_id *stateid;
5621 struct nfs4_client *cl = cstate->session->se_client;
5622
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005623 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005624 stateid->ts_id_status =
5625 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005626
Bryan Schumaker17456802011-07-13 10:50:48 -04005627 return nfs_ok;
5628}
5629
Chuck Lever42691392016-08-11 10:37:30 -04005630static __be32
5631nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5632{
5633 struct nfs4_ol_stateid *stp = openlockstateid(s);
5634 __be32 ret;
5635
Trond Myklebust659aefb2017-11-03 08:00:13 -04005636 ret = nfsd4_lock_ol_stateid(stp);
5637 if (ret)
5638 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005639
5640 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5641 if (ret)
5642 goto out;
5643
5644 ret = nfserr_locks_held;
5645 if (check_for_locks(stp->st_stid.sc_file,
5646 lockowner(stp->st_stateowner)))
5647 goto out;
5648
5649 release_lock_stateid(stp);
5650 ret = nfs_ok;
5651
5652out:
5653 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005654out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005655 nfs4_put_stid(s);
5656 return ret;
5657}
5658
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005659__be32
5660nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005661 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005662{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005663 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005664 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005665 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005666 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005667 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005668 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005669
Jeff Layton1af71cc2014-07-29 21:34:14 -04005670 spin_lock(&cl->cl_lock);
5671 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005672 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005673 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005674 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005675 switch (s->sc_type) {
5676 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005677 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005678 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005679 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005680 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5681 if (ret)
5682 break;
5683 ret = nfserr_locks_held;
5684 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005685 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005686 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005687 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005688 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005689 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005690 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005691 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005692 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005693 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005694 list_del_init(&dp->dl_recall_lru);
5695 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005696 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005697 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005698 goto out;
5699 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005700 }
Trond Myklebust03da3162017-11-03 08:00:16 -04005701 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005702out_unlock:
5703 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005704out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005705 return ret;
5706}
5707
NeilBrown4c4cd222005-07-07 17:59:27 -07005708static inline int
5709setlkflg (int type)
5710{
5711 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5712 RD_STATE : WR_STATE;
5713}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005715static __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 -04005716{
5717 struct svc_fh *current_fh = &cstate->current_fh;
5718 struct nfs4_stateowner *sop = stp->st_stateowner;
5719 __be32 status;
5720
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005721 status = nfsd4_check_seqid(cstate, sop, seqid);
5722 if (status)
5723 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04005724 status = nfsd4_lock_ol_stateid(stp);
5725 if (status != nfs_ok)
5726 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005727 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005728 if (status == nfs_ok)
5729 status = nfs4_check_fh(current_fh, &stp->st_stid);
5730 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005731 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005732 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005733}
5734
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735/*
5736 * Checks for sequence id mutating operations.
5737 */
Al Virob37ad282006-10-19 23:28:59 -07005738static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005739nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005740 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005741 struct nfs4_ol_stateid **stpp,
5742 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005744 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005745 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005746 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005748 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5749 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005750
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005752 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005753 if (status)
5754 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005755 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005756 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005758 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005759 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005760 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005761 else
5762 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005763 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005764}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005765
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005766static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5767 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005768{
5769 __be32 status;
5770 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005771 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005773 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005774 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005775 if (status)
5776 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005777 oo = openowner(stp->st_stateowner);
5778 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005779 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005780 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005781 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005782 }
5783 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005784 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785}
5786
Al Virob37ad282006-10-19 23:28:59 -07005787__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005788nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005789 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005791 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07005792 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005793 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005794 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005795 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
Al Viroa6a9f182013-09-16 10:57:01 -04005797 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5798 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005800 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005801 if (status)
5802 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005804 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005805 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005806 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005807 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005808 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005809 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005810 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005811 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005812 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005813 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005814 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005815 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005816 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005817 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005818 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005819 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005820
Jeff Layton2a4317c2012-03-21 16:42:43 -04005821 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005822 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005823put_stateid:
5824 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005826 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 return status;
5828}
5829
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005830static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005832 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005833 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005834 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005835 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005836}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005837
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005838static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5839{
5840 switch (to_access) {
5841 case NFS4_SHARE_ACCESS_READ:
5842 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5843 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5844 break;
5845 case NFS4_SHARE_ACCESS_WRITE:
5846 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5847 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5848 break;
5849 case NFS4_SHARE_ACCESS_BOTH:
5850 break;
5851 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005852 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 }
5854}
5855
Al Virob37ad282006-10-19 23:28:59 -07005856__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005857nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005858 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005860 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07005861 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005862 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005863 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Al Viroa6a9f182013-09-16 10:57:01 -04005865 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5866 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005868 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005869 if (od->od_deleg_want)
5870 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5871 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005873 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005874 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005875 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005878 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005879 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005881 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005883 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005884 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005886 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005888 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005889 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005890 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005892put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005893 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005894 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005896 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 return status;
5898}
5899
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005900static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5901{
Trond Myklebustacf92952014-06-30 11:48:37 -04005902 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005903 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005904 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005905
Jeff Layton2c41beb2014-07-29 21:34:41 -04005906 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005907 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005908
Jeff Laytond83017f2014-07-29 21:34:42 -04005909 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005910 if (unhashed)
5911 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005912 spin_unlock(&clp->cl_lock);
5913 free_ol_stateid_reaplist(&reaplist);
5914 } else {
5915 spin_unlock(&clp->cl_lock);
5916 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005917 if (unhashed)
5918 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005919 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005920}
5921
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922/*
5923 * nfs4_unlock_state() called after encode
5924 */
Al Virob37ad282006-10-19 23:28:59 -07005925__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005926nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005927 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005929 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07005930 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005931 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005932 struct net *net = SVC_NET(rqstp);
5933 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
Al Viroa6a9f182013-09-16 10:57:01 -04005935 dprintk("NFSD: nfsd4_close on file %pd\n",
5936 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005938 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5939 &close->cl_stateid,
5940 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005941 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005942 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005943 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005945
5946 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005947
5948 /*
5949 * Technically we don't _really_ have to increment or copy it, since
5950 * it should just be gone after this operation and we clobber the
5951 * copied value below, but we continue to do so here just to ensure
5952 * that racing ops see that there was a state change.
5953 */
Jeff Layton9767feb2015-10-01 09:05:50 -04005954 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005956 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005957 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005958
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005959 /* v4.1+ suggests that we send a special stateid in here, since the
5960 * clients should just ignore this anyway. Since this is not useful
5961 * for v4.0 clients either, we set it to the special close_stateid
5962 * universally.
5963 *
5964 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
5965 */
5966 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04005967
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005968 /* put reference from nfs4_preprocess_seqid_op */
5969 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 return status;
5972}
5973
Al Virob37ad282006-10-19 23:28:59 -07005974__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005975nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005976 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005978 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005979 struct nfs4_delegation *dp;
5980 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005981 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07005982 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005983 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005985 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005986 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005988 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005989 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005990 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005991 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04005992 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005993 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04005994 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005995
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005996 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005997put_stateid:
5998 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999out:
6000 return status;
6001}
6002
Benny Halevy87df4de2008-12-15 19:42:03 +02006003static inline u64
6004end_offset(u64 start, u64 len)
6005{
6006 u64 end;
6007
6008 end = start + len;
6009 return end >= start ? end: NFS4_MAX_UINT64;
6010}
6011
6012/* last octet in a range */
6013static inline u64
6014last_byte_offset(u64 start, u64 len)
6015{
6016 u64 end;
6017
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006018 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006019 end = start + len;
6020 return end > start ? end - 1: NFS4_MAX_UINT64;
6021}
6022
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023/*
6024 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6025 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6026 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6027 * locking, this prevents us from being completely protocol-compliant. The
6028 * real solution to this problem is to start using unsigned file offsets in
6029 * the VFS, but this is a very deep change!
6030 */
6031static inline void
6032nfs4_transform_lock_offset(struct file_lock *lock)
6033{
6034 if (lock->fl_start < 0)
6035 lock->fl_start = OFFSET_MAX;
6036 if (lock->fl_end < 0)
6037 lock->fl_end = OFFSET_MAX;
6038}
6039
Jeff Laytoncae80b32015-04-03 09:04:04 -04006040static fl_owner_t
6041nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006042{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006043 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6044
6045 nfs4_get_stateowner(&lo->lo_owner);
6046 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006047}
6048
Jeff Laytoncae80b32015-04-03 09:04:04 -04006049static void
6050nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006051{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006052 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006053
Jeff Laytoncae80b32015-04-03 09:04:04 -04006054 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006055 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006056}
6057
Jeff Layton76d348f2016-09-16 16:28:24 -04006058static void
6059nfsd4_lm_notify(struct file_lock *fl)
6060{
6061 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6062 struct net *net = lo->lo_owner.so_client->net;
6063 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6064 struct nfsd4_blocked_lock *nbl = container_of(fl,
6065 struct nfsd4_blocked_lock, nbl_lock);
6066 bool queue = false;
6067
Jeff Layton7919d0a2016-09-16 16:28:25 -04006068 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006069 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006070 if (!list_empty(&nbl->nbl_list)) {
6071 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006072 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006073 queue = true;
6074 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006075 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006076
6077 if (queue)
6078 nfsd4_run_cb(&nbl->nbl_cb);
6079}
6080
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006081static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006082 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006083 .lm_get_owner = nfsd4_fl_get_owner,
6084 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006085};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
6087static inline void
6088nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6089{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006090 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091
NeilBrownd5b90262006-04-10 22:55:22 -07006092 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006093 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04006094 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6095 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006096 if (!deny->ld_owner.data)
6097 /* We just don't care that much */
6098 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006099 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006100 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006101nevermind:
6102 deny->ld_owner.len = 0;
6103 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006104 deny->ld_clientid.cl_boot = 0;
6105 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 }
6107 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006108 deny->ld_length = NFS4_MAX_UINT64;
6109 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6111 deny->ld_type = NFS4_READ_LT;
6112 if (fl->fl_type != F_RDLCK)
6113 deny->ld_type = NFS4_WRITE_LT;
6114}
6115
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006116static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006117find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006119 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006120 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121
Trond Myklebust0a880a22014-07-30 08:27:10 -04006122 lockdep_assert_held(&clp->cl_lock);
6123
Trond Myklebustd4f04892014-07-29 21:34:36 -04006124 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6125 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006126 if (so->so_is_open_owner)
6127 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006128 if (same_owner_str(so, owner))
6129 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 }
6131 return NULL;
6132}
6133
Trond Myklebustc58c6612014-07-29 21:34:35 -04006134static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006135find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006136{
6137 struct nfs4_lockowner *lo;
6138
Trond Myklebustd4f04892014-07-29 21:34:36 -04006139 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006140 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006141 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006142 return lo;
6143}
6144
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006145static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6146{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006147 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006148}
6149
Jeff Layton6b180f02014-07-29 21:34:26 -04006150static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6151{
6152 struct nfs4_lockowner *lo = lockowner(sop);
6153
6154 kmem_cache_free(lockowner_slab, lo);
6155}
6156
6157static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006158 .so_unhash = nfs4_unhash_lockowner,
6159 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006160};
6161
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162/*
6163 * Alloc a lock owner structure.
6164 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006165 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006167 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006169static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006170alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6171 struct nfs4_ol_stateid *open_stp,
6172 struct nfsd4_lock *lock)
6173{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006174 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006176 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6177 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006179 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006180 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6181 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006182 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006183 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006184 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006185 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006186 if (ret == NULL) {
6187 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006188 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006189 ret = lo;
6190 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006191 nfs4_free_stateowner(&lo->lo_owner);
6192
Trond Myklebustd4f04892014-07-29 21:34:36 -04006193 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006194 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195}
6196
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006197static struct nfs4_ol_stateid *
6198find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
6199{
6200 struct nfs4_ol_stateid *lst;
6201 struct nfs4_client *clp = lo->lo_owner.so_client;
6202
6203 lockdep_assert_held(&clp->cl_lock);
6204
6205 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
6206 if (lst->st_stid.sc_type != NFS4_LOCK_STID)
6207 continue;
6208 if (lst->st_stid.sc_file == fp) {
6209 refcount_inc(&lst->st_stid.sc_count);
6210 return lst;
6211 }
6212 }
6213 return NULL;
6214}
6215
Trond Myklebustbeeca192017-11-03 08:00:14 -04006216static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006217init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6218 struct nfs4_file *fp, struct inode *inode,
6219 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006221 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006222 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
Trond Myklebustbeeca192017-11-03 08:00:14 -04006224 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006225 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006226retry:
6227 spin_lock(&clp->cl_lock);
6228 spin_lock(&fp->fi_lock);
6229 retstp = find_lock_stateid(lo, fp);
6230 if (retstp)
6231 goto out_unlock;
Jeff Layton356a95e2014-07-29 21:34:13 -04006232
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006233 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006234 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006235 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006236 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006237 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006238 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006240 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006241 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006242 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006243 list_add(&stp->st_perfile, &fp->fi_stateids);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006244out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04006245 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006246 spin_unlock(&clp->cl_lock);
6247 if (retstp) {
6248 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6249 nfs4_put_stid(&retstp->st_stid);
6250 goto retry;
6251 }
6252 /* To keep mutex tracking happy */
6253 mutex_unlock(&stp->st_mutex);
6254 stp = retstp;
6255 }
6256 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257}
6258
Jeff Laytonc53530d2014-06-30 11:48:39 -04006259static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006260find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6261 struct inode *inode, struct nfs4_ol_stateid *ost,
6262 bool *new)
6263{
6264 struct nfs4_stid *ns = NULL;
6265 struct nfs4_ol_stateid *lst;
6266 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6267 struct nfs4_client *clp = oo->oo_owner.so_client;
6268
Trond Myklebustbeeca192017-11-03 08:00:14 -04006269 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006270 spin_lock(&clp->cl_lock);
6271 lst = find_lock_stateid(lo, fi);
Jeff Layton356a95e2014-07-29 21:34:13 -04006272 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006273 if (lst != NULL) {
6274 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6275 goto out;
6276 nfs4_put_stid(&lst->st_stid);
6277 }
6278 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6279 if (ns == NULL)
6280 return NULL;
6281
6282 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6283 if (lst == openlockstateid(ns))
6284 *new = true;
6285 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006286 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006287out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006288 return lst;
6289}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006290
NeilBrownfd39ca92005-06-23 22:04:03 -07006291static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292check_lock_length(u64 offset, u64 length)
6293{
Kinglong Meee7969312015-07-13 17:34:19 +08006294 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6295 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296}
6297
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006298static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006299{
Trond Myklebust11b91642014-07-29 21:34:08 -04006300 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006301
Jeff Layton7214e862014-07-10 14:07:33 -04006302 lockdep_assert_held(&fp->fi_lock);
6303
Jeff Layton82c5ff12012-05-11 09:45:13 -04006304 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006305 return;
Jeff Layton12659652014-07-10 14:07:28 -04006306 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006307 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006308}
6309
Jeff Layton356a95e2014-07-29 21:34:13 -04006310static __be32
6311lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6312 struct nfs4_ol_stateid *ost,
6313 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006314 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006315{
Jeff Layton5db1c032014-07-29 21:34:28 -04006316 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006317 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006318 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6319 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006320 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006321 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006322 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006323 unsigned int strhashval;
6324
Kinglong Meec8623992015-07-13 17:35:37 +08006325 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006326 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006327 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006328 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6329 if (lo == NULL)
6330 return nfserr_jukebox;
6331 } else {
6332 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006333 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006334 if (!cstate->minorversion &&
6335 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006336 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006337 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006338
Jeff Laytondd257932016-08-11 10:37:39 -04006339 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6340 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006341 status = nfserr_jukebox;
6342 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006343 }
Jeff Laytondd257932016-08-11 10:37:39 -04006344
Jeff Layton5db1c032014-07-29 21:34:28 -04006345 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006346 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006347out:
6348 nfs4_put_stateowner(&lo->lo_owner);
6349 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006350}
6351
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352/*
6353 * LOCK operation
6354 */
Al Virob37ad282006-10-19 23:28:59 -07006355__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006356nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006357 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006359 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006360 struct nfs4_openowner *open_sop = NULL;
6361 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006362 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006363 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006364 struct nfs4_file *fp;
J. Bruce Fields7d947842010-08-20 18:09:31 -04006365 struct file *filp = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006366 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006367 struct file_lock *file_lock = NULL;
6368 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006369 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006370 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006371 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006372 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006373 unsigned char fl_type;
6374 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006375 struct net *net = SVC_NET(rqstp);
6376 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
6378 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6379 (long long) lock->lk_offset,
6380 (long long) lock->lk_length);
6381
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 if (check_lock_length(lock->lk_offset, lock->lk_length))
6383 return nfserr_inval;
6384
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006385 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006386 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006387 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6388 return status;
6389 }
6390
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006392 if (nfsd4_has_session(cstate))
6393 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006394 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006395 &cstate->session->se_client->cl_clientid,
6396 sizeof(clientid_t));
6397
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006399 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006403 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 lock->lk_new_open_seqid,
6405 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006406 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006407 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006409 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006410 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006411 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006412 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006413 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006414 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006415 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006416 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006417 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006418 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006419 lock->lk_old_lock_seqid,
6420 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006421 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006422 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006423 if (status)
6424 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006425 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006427 lkflg = setlkflg(lock->lk_type);
6428 status = nfs4_check_openmode(lock_stp, lkflg);
6429 if (status)
6430 goto out;
6431
NeilBrown0dd395d2005-07-07 17:59:15 -07006432 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006433 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006434 goto out;
6435 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006436 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006437 goto out;
6438
Trond Myklebust11b91642014-07-29 21:34:08 -04006439 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006442 if (nfsd4_has_session(cstate))
6443 fl_flags |= FL_SLEEP;
6444 /* Fallthrough */
6445 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006446 spin_lock(&fp->fi_lock);
6447 filp = find_readable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006448 if (filp)
6449 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006450 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006451 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006454 if (nfsd4_has_session(cstate))
6455 fl_flags |= FL_SLEEP;
6456 /* Fallthrough */
6457 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006458 spin_lock(&fp->fi_lock);
6459 filp = find_writeable_file_locked(fp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006460 if (filp)
6461 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006462 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006463 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006464 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 default:
6466 status = nfserr_inval;
6467 goto out;
6468 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006469
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006470 if (!filp) {
6471 status = nfserr_openmode;
6472 goto out;
6473 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006474
Jeff Layton76d348f2016-09-16 16:28:24 -04006475 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6476 if (!nbl) {
6477 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6478 status = nfserr_jukebox;
6479 goto out;
6480 }
6481
6482 file_lock = &nbl->nbl_lock;
6483 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006484 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006485 file_lock->fl_pid = current->tgid;
6486 file_lock->fl_file = filp;
Jeff Layton76d348f2016-09-16 16:28:24 -04006487 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006488 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6489 file_lock->fl_start = lock->lk_offset;
6490 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6491 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492
Jeff Layton21179d82012-08-21 08:03:32 -04006493 conflock = locks_alloc_lock();
6494 if (!conflock) {
6495 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6496 status = nfserr_jukebox;
6497 goto out;
6498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499
Jeff Layton76d348f2016-09-16 16:28:24 -04006500 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04006501 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04006502 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006503 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006504 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006505 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006506 }
6507
Jeff Layton21179d82012-08-21 08:03:32 -04006508 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006509 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006511 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006512 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006513 if (lock->lk_reclaim)
6514 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006515 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006516 case FILE_LOCK_DEFERRED:
6517 nbl = NULL;
6518 /* Fallthrough */
6519 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006520 status = nfserr_denied;
6521 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006522 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006523 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006524 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006526 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006527 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006528 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006529 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006530 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006533 if (nbl) {
6534 /* dequeue it if we queued it before */
6535 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006536 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006537 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006538 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006539 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006540 }
6541 free_blocked_lock(nbl);
6542 }
Trond Myklebustde186432014-07-10 14:07:26 -04006543 if (filp)
6544 fput(filp);
Jeff Layton5db1c032014-07-29 21:34:28 -04006545 if (lock_stp) {
6546 /* Bump seqid manually if the 4.0 replay owner is openowner */
6547 if (cstate->replay_owner &&
6548 cstate->replay_owner != &lock_sop->lo_owner &&
6549 seqid_mutating_err(ntohl(status)))
6550 lock_sop->lo_owner.so_seqid++;
6551
6552 /*
6553 * If this is a new, never-before-used stateid, and we are
6554 * returning an error, then just go ahead and release it.
6555 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006556 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006557 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006558
6559 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006560
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006561 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006562 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006563 if (open_stp)
6564 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006565 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006566 if (conflock)
6567 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 return status;
6569}
6570
6571/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006572 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6573 * so we do a temporary open here just to get an open file to pass to
6574 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6575 * inode operation.)
6576 */
Al Viro04da6e92012-04-13 00:00:04 -04006577static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006578{
6579 struct file *file;
Al Viro04da6e92012-04-13 00:00:04 -04006580 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
6581 if (!err) {
6582 err = nfserrno(vfs_test_lock(file, lock));
Christoph Hellwigfd891452015-04-28 15:41:16 +02006583 fput(file);
Al Viro04da6e92012-04-13 00:00:04 -04006584 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006585 return err;
6586}
6587
6588/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 * LOCKT operation
6590 */
Al Virob37ad282006-10-19 23:28:59 -07006591__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006592nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006593 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006595 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006596 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006597 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006598 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006599 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006601 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 return nfserr_grace;
6603
6604 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6605 return nfserr_inval;
6606
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006607 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04006608 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006609 if (status)
6610 goto out;
6611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006613 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615
Jeff Layton21179d82012-08-21 08:03:32 -04006616 file_lock = locks_alloc_lock();
6617 if (!file_lock) {
6618 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6619 status = nfserr_jukebox;
6620 goto out;
6621 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006622
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 switch (lockt->lt_type) {
6624 case NFS4_READ_LT:
6625 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006626 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006627 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 case NFS4_WRITE_LT:
6629 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006630 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006631 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006633 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006635 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 }
6637
Kinglong Meec8623992015-07-13 17:35:37 +08006638 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006639 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006640 file_lock->fl_owner = (fl_owner_t)lo;
6641 file_lock->fl_pid = current->tgid;
6642 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643
Jeff Layton21179d82012-08-21 08:03:32 -04006644 file_lock->fl_start = lockt->lt_offset;
6645 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646
Jeff Layton21179d82012-08-21 08:03:32 -04006647 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648
Jeff Layton21179d82012-08-21 08:03:32 -04006649 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006650 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006651 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006652
Jeff Layton21179d82012-08-21 08:03:32 -04006653 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006655 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 }
6657out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006658 if (lo)
6659 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006660 if (file_lock)
6661 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 return status;
6663}
6664
Al Virob37ad282006-10-19 23:28:59 -07006665__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006666nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006667 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006669 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006670 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 struct file *filp = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006672 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006673 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006674 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006675 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6676
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6678 (long long) locku->lu_offset,
6679 (long long) locku->lu_length);
6680
6681 if (check_lock_length(locku->lu_offset, locku->lu_length))
6682 return nfserr_inval;
6683
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006684 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006685 &locku->lu_stateid, NFS4_LOCK_STID,
6686 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006687 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 goto out;
Trond Myklebust11b91642014-07-29 21:34:08 -04006689 filp = find_any_file(stp->st_stid.sc_file);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006690 if (!filp) {
6691 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006692 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006693 }
Jeff Layton21179d82012-08-21 08:03:32 -04006694 file_lock = locks_alloc_lock();
6695 if (!file_lock) {
6696 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6697 status = nfserr_jukebox;
Trond Myklebustde186432014-07-10 14:07:26 -04006698 goto fput;
Jeff Layton21179d82012-08-21 08:03:32 -04006699 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006700
Jeff Layton21179d82012-08-21 08:03:32 -04006701 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006702 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006703 file_lock->fl_pid = current->tgid;
6704 file_lock->fl_file = filp;
6705 file_lock->fl_flags = FL_POSIX;
6706 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6707 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708
Jeff Layton21179d82012-08-21 08:03:32 -04006709 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6710 locku->lu_length);
6711 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712
Jeff Layton21179d82012-08-21 08:03:32 -04006713 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006714 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006715 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 goto out_nfserr;
6717 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006718 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Trond Myklebustde186432014-07-10 14:07:26 -04006719fput:
6720 fput(filp);
Trond Myklebust858cc572014-07-29 21:34:16 -04006721put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006722 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006723 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006725 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006726 if (file_lock)
6727 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 return status;
6729
6730out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006731 status = nfserrno(err);
Trond Myklebustde186432014-07-10 14:07:26 -04006732 goto fput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733}
6734
6735/*
6736 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006737 * true: locks held by lockowner
6738 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006740static bool
6741check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006743 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006744 int status = false;
6745 struct file *filp = find_any_file(fp);
6746 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006747 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006748
6749 if (!filp) {
6750 /* Any valid lock stateid should have some sort of access */
6751 WARN_ON_ONCE(1);
6752 return status;
6753 }
6754
Amir Goldstein64bed6c2018-07-13 17:22:24 +03006755 inode = locks_inode(filp);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006756 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006758 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006759 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006760 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6761 if (fl->fl_owner == (fl_owner_t)lowner) {
6762 status = true;
6763 break;
6764 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006765 }
Jeff Layton6109c852015-01-16 15:05:57 -05006766 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 }
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006768 fput(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 return status;
6770}
6771
Al Virob37ad282006-10-19 23:28:59 -07006772__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006773nfsd4_release_lockowner(struct svc_rqst *rqstp,
6774 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006775 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006777 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006779 struct nfs4_stateowner *sop;
6780 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006781 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006783 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006784 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006785 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006786 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006787 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788
6789 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6790 clid->cl_boot, clid->cl_id);
6791
Jeff Layton4b24ca72014-06-30 11:48:44 -04006792 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006793 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006794 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006795
Trond Myklebustd4f04892014-07-29 21:34:36 -04006796 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006797 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006798 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006799 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006800 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006801
6802 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006803 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006804
Jeff Layton882e9d22014-07-29 21:34:37 -04006805 /* see if there are still any locks associated with it */
6806 lo = lockowner(sop);
6807 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6808 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6809 status = nfserr_locks_held;
6810 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006811 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006812 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006813 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006814
Kinglong Meeb5971af2014-08-22 10:18:43 -04006815 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006816 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006817 }
Chuck Lever88584812016-07-13 16:40:14 -04006818 if (!lo) {
6819 spin_unlock(&clp->cl_lock);
6820 return status;
6821 }
6822
6823 unhash_lockowner_locked(lo);
6824 while (!list_empty(&lo->lo_owner.so_stateids)) {
6825 stp = list_first_entry(&lo->lo_owner.so_stateids,
6826 struct nfs4_ol_stateid,
6827 st_perstateowner);
6828 WARN_ON(!unhash_lock_stateid(stp));
6829 put_ol_stateid_locked(stp, &reaplist);
6830 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006831 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006832 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04006833 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04006834 nfs4_put_stateowner(&lo->lo_owner);
6835
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 return status;
6837}
6838
6839static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006840alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841{
NeilBrowna55370a2005-06-23 22:03:52 -07006842 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843}
6844
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006845bool
Scott Mayhew6b189102019-03-26 18:06:26 -04006846nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006847{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006848 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006849
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006850 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006851 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006852}
6853
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854/*
6855 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04006856 *
6857 * The caller is responsible for freeing name.data if NULL is returned (it
6858 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006860struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04006861nfs4_client_to_reclaim(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862{
6863 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006864 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865
Scott Mayhew6b189102019-03-26 18:06:26 -04006866 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data);
NeilBrowna55370a2005-06-23 22:03:52 -07006867 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006868 if (crp) {
6869 strhashval = clientstr_hashval(name);
6870 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006871 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04006872 crp->cr_name.data = name.data;
6873 crp->cr_name.len = name.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006874 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006875 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006876 }
6877 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878}
6879
Jeff Layton2a4317c2012-03-21 16:42:43 -04006880void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006881nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006882{
6883 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04006884 kfree(crp->cr_name.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05006885 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006886 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006887}
6888
6889void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006890nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891{
6892 struct nfs4_client_reclaim *crp = NULL;
6893 int i;
6894
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006896 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6897 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006899 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 }
6901 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006902 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903}
6904
6905/*
6906 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006907struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04006908nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909{
6910 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 struct nfs4_client_reclaim *crp = NULL;
6912
Scott Mayhew6b189102019-03-26 18:06:26 -04006913 dprintk("NFSD: nfs4_find_reclaim_client for name %.*s\n", name.len, name.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
Scott Mayhew6b189102019-03-26 18:06:26 -04006915 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006916 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04006917 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 return crp;
6919 }
6920 }
6921 return NULL;
6922}
6923
6924/*
6925* Called from OPEN. Look for clientid in reclaim list.
6926*/
Al Virob37ad282006-10-19 23:28:59 -07006927__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006928nfs4_check_open_reclaim(clientid_t *clid,
6929 struct nfsd4_compound_state *cstate,
6930 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006932 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006933
6934 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006935 status = lookup_clientid(clid, cstate, nn);
6936 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006937 return nfserr_reclaim_bad;
6938
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006939 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6940 return nfserr_no_grace;
6941
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006942 if (nfsd4_client_record_check(cstate->clp))
6943 return nfserr_reclaim_bad;
6944
6945 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946}
6947
Bryan Schumaker65178db2011-11-01 13:35:21 -04006948#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006949static inline void
6950put_client(struct nfs4_client *clp)
6951{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04006952 atomic_dec(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04006953}
6954
Jeff Layton285abde2014-07-30 08:27:24 -04006955static struct nfs4_client *
6956nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6957{
6958 struct nfs4_client *clp;
6959 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6960 nfsd_net_id);
6961
6962 if (!nfsd_netns_ready(nn))
6963 return NULL;
6964
6965 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6966 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
6967 return clp;
6968 }
6969 return NULL;
6970}
6971
Jeff Layton7ec0e362014-07-30 08:27:17 -04006972u64
Jeff Layton285abde2014-07-30 08:27:24 -04006973nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04006974{
6975 struct nfs4_client *clp;
6976 u64 count = 0;
6977 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6978 nfsd_net_id);
6979 char buf[INET6_ADDRSTRLEN];
6980
6981 if (!nfsd_netns_ready(nn))
6982 return 0;
6983
6984 spin_lock(&nn->client_lock);
6985 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6986 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
6987 pr_info("NFS Client: %s\n", buf);
6988 ++count;
6989 }
6990 spin_unlock(&nn->client_lock);
6991
6992 return count;
6993}
Bryan Schumaker65178db2011-11-01 13:35:21 -04006994
Jeff Laytona0926d12014-07-30 08:27:18 -04006995u64
Jeff Layton285abde2014-07-30 08:27:24 -04006996nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04006997{
6998 u64 count = 0;
6999 struct nfs4_client *clp;
7000 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7001 nfsd_net_id);
7002
7003 if (!nfsd_netns_ready(nn))
7004 return count;
7005
7006 spin_lock(&nn->client_lock);
7007 clp = nfsd_find_client(addr, addr_size);
7008 if (clp) {
7009 if (mark_client_expired_locked(clp) == nfs_ok)
7010 ++count;
7011 else
7012 clp = NULL;
7013 }
7014 spin_unlock(&nn->client_lock);
7015
7016 if (clp)
7017 expire_client(clp);
7018
7019 return count;
7020}
7021
Jeff Layton69fc9ed2014-07-30 08:27:19 -04007022u64
Jeff Layton285abde2014-07-30 08:27:24 -04007023nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04007024{
7025 u64 count = 0;
7026 struct nfs4_client *clp, *next;
7027 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7028 nfsd_net_id);
7029 LIST_HEAD(reaplist);
7030
7031 if (!nfsd_netns_ready(nn))
7032 return count;
7033
7034 spin_lock(&nn->client_lock);
7035 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7036 if (mark_client_expired_locked(clp) == nfs_ok) {
7037 list_add(&clp->cl_lru, &reaplist);
7038 if (max != 0 && ++count >= max)
7039 break;
7040 }
7041 }
7042 spin_unlock(&nn->client_lock);
7043
7044 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
7045 expire_client(clp);
7046
7047 return count;
7048}
7049
Bryan Schumaker184c1842012-11-29 11:40:44 -05007050static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
7051 const char *type)
7052{
7053 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05007054 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05007055 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
7056}
7057
Jeff Layton016200c2014-07-30 08:27:21 -04007058static void
7059nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
7060 struct list_head *collect)
7061{
7062 struct nfs4_client *clp = lst->st_stid.sc_client;
7063 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7064 nfsd_net_id);
7065
7066 if (!collect)
7067 return;
7068
7069 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007070 atomic_inc(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04007071 list_add(&lst->st_locks, collect);
7072}
7073
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007074static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04007075 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04007076 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05007077{
7078 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007079 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007080 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007081 u64 count = 0;
7082
Jeff Layton016200c2014-07-30 08:27:21 -04007083 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007084 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007085 list_for_each_entry_safe(stp, st_next,
7086 &oop->oo_owner.so_stateids, st_perstateowner) {
7087 list_for_each_entry_safe(lst, lst_next,
7088 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04007089 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04007090 if (func(lst))
7091 nfsd_inject_add_lock_to_list(lst,
7092 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04007093 }
Jeff Layton016200c2014-07-30 08:27:21 -04007094 ++count;
7095 /*
7096 * Despite the fact that these functions deal
7097 * with 64-bit integers for "count", we must
7098 * ensure that it doesn't blow up the
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007099 * clp->cl_rpc_users. Throw a warning if we
Jeff Layton016200c2014-07-30 08:27:21 -04007100 * start to approach INT_MAX here.
7101 */
7102 WARN_ON_ONCE(count == (INT_MAX / 2));
7103 if (count == max)
7104 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007105 }
7106 }
7107 }
Jeff Layton016200c2014-07-30 08:27:21 -04007108out:
7109 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007110
7111 return count;
7112}
7113
Jeff Layton016200c2014-07-30 08:27:21 -04007114static u64
7115nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
7116 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05007117{
Jeff Layton016200c2014-07-30 08:27:21 -04007118 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007119}
7120
Jeff Layton016200c2014-07-30 08:27:21 -04007121static u64
7122nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007123{
Jeff Layton016200c2014-07-30 08:27:21 -04007124 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007125 nfsd_print_count(clp, count, "locked files");
7126 return count;
7127}
7128
Jeff Layton016200c2014-07-30 08:27:21 -04007129u64
Jeff Layton285abde2014-07-30 08:27:24 -04007130nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04007131{
7132 struct nfs4_client *clp;
7133 u64 count = 0;
7134 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7135 nfsd_net_id);
7136
7137 if (!nfsd_netns_ready(nn))
7138 return 0;
7139
7140 spin_lock(&nn->client_lock);
7141 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7142 count += nfsd_print_client_locks(clp);
7143 spin_unlock(&nn->client_lock);
7144
7145 return count;
7146}
7147
7148static void
7149nfsd_reap_locks(struct list_head *reaplist)
7150{
7151 struct nfs4_client *clp;
7152 struct nfs4_ol_stateid *stp, *next;
7153
7154 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
7155 list_del_init(&stp->st_locks);
7156 clp = stp->st_stid.sc_client;
7157 nfs4_put_stid(&stp->st_stid);
7158 put_client(clp);
7159 }
7160}
7161
7162u64
Jeff Layton285abde2014-07-30 08:27:24 -04007163nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04007164{
7165 unsigned int count = 0;
7166 struct nfs4_client *clp;
7167 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7168 nfsd_net_id);
7169 LIST_HEAD(reaplist);
7170
7171 if (!nfsd_netns_ready(nn))
7172 return count;
7173
7174 spin_lock(&nn->client_lock);
7175 clp = nfsd_find_client(addr, addr_size);
7176 if (clp)
7177 count = nfsd_collect_client_locks(clp, &reaplist, 0);
7178 spin_unlock(&nn->client_lock);
7179 nfsd_reap_locks(&reaplist);
7180 return count;
7181}
7182
7183u64
Jeff Layton285abde2014-07-30 08:27:24 -04007184nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04007185{
7186 u64 count = 0;
7187 struct nfs4_client *clp;
7188 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7189 nfsd_net_id);
7190 LIST_HEAD(reaplist);
7191
7192 if (!nfsd_netns_ready(nn))
7193 return count;
7194
7195 spin_lock(&nn->client_lock);
7196 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7197 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
7198 if (max != 0 && count >= max)
7199 break;
7200 }
7201 spin_unlock(&nn->client_lock);
7202 nfsd_reap_locks(&reaplist);
7203 return count;
7204}
7205
Jeff Layton82e05ef2014-07-30 08:27:22 -04007206static u64
7207nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
7208 struct list_head *collect,
7209 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007210{
7211 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04007212 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7213 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007214 u64 count = 0;
7215
Jeff Layton82e05ef2014-07-30 08:27:22 -04007216 lockdep_assert_held(&nn->client_lock);
7217
7218 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007219 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04007220 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007221 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007222 if (collect) {
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007223 atomic_inc(&clp->cl_rpc_users);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007224 list_add(&oop->oo_perclient, collect);
7225 }
7226 }
7227 ++count;
7228 /*
7229 * Despite the fact that these functions deal with
7230 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007231 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton82e05ef2014-07-30 08:27:22 -04007232 * warning if we start to approach INT_MAX here.
7233 */
7234 WARN_ON_ONCE(count == (INT_MAX / 2));
7235 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007236 break;
7237 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04007238 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007239
7240 return count;
7241}
7242
Jeff Layton82e05ef2014-07-30 08:27:22 -04007243static u64
7244nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007245{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007246 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
7247
7248 nfsd_print_count(clp, count, "openowners");
7249 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007250}
7251
Jeff Layton82e05ef2014-07-30 08:27:22 -04007252static u64
7253nfsd_collect_client_openowners(struct nfs4_client *clp,
7254 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007255{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007256 return nfsd_foreach_client_openowner(clp, max, collect,
7257 unhash_openowner_locked);
7258}
7259
7260u64
Jeff Layton285abde2014-07-30 08:27:24 -04007261nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007262{
7263 struct nfs4_client *clp;
7264 u64 count = 0;
7265 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7266 nfsd_net_id);
7267
7268 if (!nfsd_netns_ready(nn))
7269 return 0;
7270
7271 spin_lock(&nn->client_lock);
7272 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7273 count += nfsd_print_client_openowners(clp);
7274 spin_unlock(&nn->client_lock);
7275
7276 return count;
7277}
7278
7279static void
7280nfsd_reap_openowners(struct list_head *reaplist)
7281{
7282 struct nfs4_client *clp;
7283 struct nfs4_openowner *oop, *next;
7284
7285 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
7286 list_del_init(&oop->oo_perclient);
7287 clp = oop->oo_owner.so_client;
7288 release_openowner(oop);
7289 put_client(clp);
7290 }
7291}
7292
7293u64
Jeff Layton285abde2014-07-30 08:27:24 -04007294nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
7295 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007296{
7297 unsigned int count = 0;
7298 struct nfs4_client *clp;
7299 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7300 nfsd_net_id);
7301 LIST_HEAD(reaplist);
7302
7303 if (!nfsd_netns_ready(nn))
7304 return count;
7305
7306 spin_lock(&nn->client_lock);
7307 clp = nfsd_find_client(addr, addr_size);
7308 if (clp)
7309 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
7310 spin_unlock(&nn->client_lock);
7311 nfsd_reap_openowners(&reaplist);
7312 return count;
7313}
7314
7315u64
Jeff Layton285abde2014-07-30 08:27:24 -04007316nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007317{
7318 u64 count = 0;
7319 struct nfs4_client *clp;
7320 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7321 nfsd_net_id);
7322 LIST_HEAD(reaplist);
7323
7324 if (!nfsd_netns_ready(nn))
7325 return count;
7326
7327 spin_lock(&nn->client_lock);
7328 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7329 count += nfsd_collect_client_openowners(clp, &reaplist,
7330 max - count);
7331 if (max != 0 && count >= max)
7332 break;
7333 }
7334 spin_unlock(&nn->client_lock);
7335 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007336 return count;
7337}
7338
Bryan Schumaker269de302012-11-29 11:40:42 -05007339static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
7340 struct list_head *victims)
7341{
7342 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007343 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7344 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05007345 u64 count = 0;
7346
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007347 lockdep_assert_held(&nn->client_lock);
7348
7349 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007350 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04007351 if (victims) {
7352 /*
7353 * It's not safe to mess with delegations that have a
7354 * non-zero dl_time. They might have already been broken
7355 * and could be processed by the laundromat outside of
7356 * the state_lock. Just leave them be.
7357 */
7358 if (dp->dl_time != 0)
7359 continue;
7360
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007361 atomic_inc(&clp->cl_rpc_users);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007362 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007363 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04007364 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007365 ++count;
7366 /*
7367 * Despite the fact that these functions deal with
7368 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007369 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007370 * warning if we start to approach INT_MAX here.
7371 */
7372 WARN_ON_ONCE(count == (INT_MAX / 2));
7373 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05007374 break;
7375 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007376 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007377 return count;
7378}
7379
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007380static u64
7381nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05007382{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007383 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007384
7385 nfsd_print_count(clp, count, "delegations");
7386 return count;
7387}
7388
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007389u64
Jeff Layton285abde2014-07-30 08:27:24 -04007390nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007391{
7392 struct nfs4_client *clp;
7393 u64 count = 0;
7394 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7395 nfsd_net_id);
7396
7397 if (!nfsd_netns_ready(nn))
7398 return 0;
7399
7400 spin_lock(&nn->client_lock);
7401 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7402 count += nfsd_print_client_delegations(clp);
7403 spin_unlock(&nn->client_lock);
7404
7405 return count;
7406}
7407
7408static void
7409nfsd_forget_delegations(struct list_head *reaplist)
7410{
7411 struct nfs4_client *clp;
7412 struct nfs4_delegation *dp, *next;
7413
7414 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7415 list_del_init(&dp->dl_recall_lru);
7416 clp = dp->dl_stid.sc_client;
7417 revoke_delegation(dp);
7418 put_client(clp);
7419 }
7420}
7421
7422u64
Jeff Layton285abde2014-07-30 08:27:24 -04007423nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7424 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007425{
7426 u64 count = 0;
7427 struct nfs4_client *clp;
7428 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7429 nfsd_net_id);
7430 LIST_HEAD(reaplist);
7431
7432 if (!nfsd_netns_ready(nn))
7433 return count;
7434
7435 spin_lock(&nn->client_lock);
7436 clp = nfsd_find_client(addr, addr_size);
7437 if (clp)
7438 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7439 spin_unlock(&nn->client_lock);
7440
7441 nfsd_forget_delegations(&reaplist);
7442 return count;
7443}
7444
7445u64
Jeff Layton285abde2014-07-30 08:27:24 -04007446nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007447{
7448 u64 count = 0;
7449 struct nfs4_client *clp;
7450 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7451 nfsd_net_id);
7452 LIST_HEAD(reaplist);
7453
7454 if (!nfsd_netns_ready(nn))
7455 return count;
7456
7457 spin_lock(&nn->client_lock);
7458 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7459 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7460 if (max != 0 && count >= max)
7461 break;
7462 }
7463 spin_unlock(&nn->client_lock);
7464 nfsd_forget_delegations(&reaplist);
7465 return count;
7466}
7467
7468static void
7469nfsd_recall_delegations(struct list_head *reaplist)
7470{
7471 struct nfs4_client *clp;
7472 struct nfs4_delegation *dp, *next;
7473
7474 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7475 list_del_init(&dp->dl_recall_lru);
7476 clp = dp->dl_stid.sc_client;
7477 /*
7478 * We skipped all entries that had a zero dl_time before,
7479 * so we can now reset the dl_time back to 0. If a delegation
7480 * break comes in now, then it won't make any difference since
7481 * we're recalling it either way.
7482 */
7483 spin_lock(&state_lock);
7484 dp->dl_time = 0;
7485 spin_unlock(&state_lock);
7486 nfsd_break_one_deleg(dp);
7487 put_client(clp);
7488 }
7489}
7490
7491u64
Jeff Layton285abde2014-07-30 08:27:24 -04007492nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007493 size_t addr_size)
7494{
7495 u64 count = 0;
7496 struct nfs4_client *clp;
7497 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7498 nfsd_net_id);
7499 LIST_HEAD(reaplist);
7500
7501 if (!nfsd_netns_ready(nn))
7502 return count;
7503
7504 spin_lock(&nn->client_lock);
7505 clp = nfsd_find_client(addr, addr_size);
7506 if (clp)
7507 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7508 spin_unlock(&nn->client_lock);
7509
7510 nfsd_recall_delegations(&reaplist);
7511 return count;
7512}
7513
7514u64
Jeff Layton285abde2014-07-30 08:27:24 -04007515nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007516{
7517 u64 count = 0;
7518 struct nfs4_client *clp, *next;
7519 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7520 nfsd_net_id);
7521 LIST_HEAD(reaplist);
7522
7523 if (!nfsd_netns_ready(nn))
7524 return count;
7525
7526 spin_lock(&nn->client_lock);
7527 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7528 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7529 if (max != 0 && ++count >= max)
7530 break;
7531 }
7532 spin_unlock(&nn->client_lock);
7533 nfsd_recall_delegations(&reaplist);
7534 return count;
7535}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007536#endif /* CONFIG_NFSD_FAULT_INJECTION */
7537
Meelap Shahc2f1a552007-07-17 04:04:39 -07007538/*
7539 * Since the lifetime of a delegation isn't limited to that of an open, a
7540 * client may quite reasonably hang on to a delegation as long as it has
7541 * the inode cached. This becomes an obvious problem the first time a
7542 * client's inode cache approaches the size of the server's total memory.
7543 *
7544 * For now we avoid this problem by imposing a hard limit on the number
7545 * of delegations, which varies according to the server's memory size.
7546 */
7547static void
7548set_max_delegations(void)
7549{
7550 /*
7551 * Allow at most 4 delegations per megabyte of RAM. Quick
7552 * estimates suggest that in the worst case (where every delegation
7553 * is for a different inode), a delegation could take about 1.5K,
7554 * giving a worst case usage of about 6% of memory.
7555 */
7556 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7557}
7558
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007559static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007560{
7561 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7562 int i;
7563
Kees Cook6da2ec52018-06-12 13:55:00 -07007564 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7565 sizeof(struct list_head),
7566 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007567 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007568 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007569 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7570 sizeof(struct list_head),
7571 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007572 if (!nn->unconf_id_hashtbl)
7573 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007574 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7575 sizeof(struct list_head),
7576 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007577 if (!nn->sessionid_hashtbl)
7578 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007579
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007580 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007581 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007582 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007583 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007584 for (i = 0; i < SESSION_HASH_SIZE; i++)
7585 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007586 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007587 nn->unconf_name_tree = RB_ROOT;
Vasily Averin81833de2017-11-13 07:25:40 +03007588 nn->boot_time = get_seconds();
7589 nn->grace_ended = false;
7590 nn->nfsd4_manager.block_opens = true;
7591 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007592 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007593 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007594 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007595 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007596 spin_lock_init(&nn->s2s_cp_lock);
7597 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007598
Jeff Layton0cc11a62016-10-20 09:34:31 -04007599 spin_lock_init(&nn->blocked_locks_lock);
7600 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7601
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007602 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007603 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007604
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007605 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007606
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007607err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007608 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007609err_unconf_id:
7610 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007611err:
7612 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007613}
7614
7615static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007616nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007617{
7618 int i;
7619 struct nfs4_client *clp = NULL;
7620 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7621
7622 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7623 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7624 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7625 destroy_client(clp);
7626 }
7627 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007628
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007629 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7630
Kinglong Mee2b905632014-03-26 22:09:30 +08007631 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7632 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7633 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7634 destroy_client(clp);
7635 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007636 }
7637
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007638 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007639 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007640 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007641 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007642}
7643
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007644int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007645nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007646{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007647 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007648 int ret;
7649
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007650 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007651 if (ret)
7652 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007653 locks_start_grace(net, &nn->nfsd4_manager);
7654 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007655 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7656 goto skip_grace;
Vasily Averin7e981a82017-11-06 16:46:04 +03007657 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7658 nn->nfsd4_grace, net->ns.inum);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007659 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007660 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007661
7662skip_grace:
7663 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7664 net->ns.inum);
7665 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7666 nfsd4_end_grace(nn);
7667 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007668}
7669
7670/* initialization to perform when the nfsd service is started: */
7671
7672int
7673nfs4_state_start(void)
7674{
7675 int ret;
7676
Jeff Layton51a54562015-08-20 07:17:01 -04007677 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007678 if (laundry_wq == NULL) {
7679 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007680 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007681 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007682 ret = nfsd4_create_callback_queue();
7683 if (ret)
7684 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007685
Meelap Shahc2f1a552007-07-17 04:04:39 -07007686 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007687 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007688
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007689out_free_laundry:
7690 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007691out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007692 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693}
7694
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007695void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007696nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007700 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007701
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007702 cancel_delayed_work_sync(&nn->laundromat_work);
7703 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007704
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007706 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007707 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007709 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007710 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711 }
Benny Halevycdc975052014-05-30 09:09:30 -04007712 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713 list_for_each_safe(pos, next, &reaplist) {
7714 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007715 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007716 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 }
7718
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007719 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007720 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007721}
7722
7723void
7724nfs4_state_shutdown(void)
7725{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007726 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007727 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007729
7730static void
7731get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7732{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007733 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7734 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007735}
7736
7737static void
7738put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7739{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007740 if (cstate->minorversion) {
7741 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7742 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7743 }
7744}
7745
7746void
7747clear_current_stateid(struct nfsd4_compound_state *cstate)
7748{
7749 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007750}
7751
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007752/*
7753 * functions to set current state id
7754 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007755void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007756nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7757 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007758{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007759 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007760}
7761
7762void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007763nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7764 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007765{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007766 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007767}
7768
7769void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007770nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7771 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007772{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007773 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007774}
7775
7776void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007777nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7778 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007779{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007780 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007781}
7782
7783/*
7784 * functions to consume current state id
7785 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007786
7787void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007788nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7789 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007790{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007791 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007792}
7793
7794void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007795nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7796 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007797{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007798 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007799}
7800
7801void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007802nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7803 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007804{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007805 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007806}
7807
7808void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007809nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7810 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007811{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007812 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007813}
7814
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007815void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007816nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7817 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007818{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007819 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007820}
7821
7822void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007823nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7824 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007825{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007826 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007827}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007828
7829void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007830nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7831 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007832{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007833 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007834}
7835
7836void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007837nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7838 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007839{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007840 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007841}