blob: da1093dd501686b5de0a1d96d968fa16faf5c539 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
Dave Hansenaceaf782008-02-15 14:37:31 -080035#include <linux/file.h>
Arnd Bergmannb89f4322010-09-18 15:09:31 +020036#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
NeilBrown0964a3d2005-06-23 22:04:32 -070038#include <linux/namei.h>
Meelap Shahc2f1a552007-07-17 04:04:39 -070039#include <linux/swap.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040040#include <linux/pagemap.h>
Chuck Lever7df302f2012-05-29 13:56:37 -040041#include <linux/ratelimit.h>
Olga Kornievskaia68e76ad2008-12-23 16:17:15 -050042#include <linux/sunrpc/svcauth_gss.h>
Jeff Layton59766872013-02-04 12:50:00 -050043#include <linux/sunrpc/addr.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010044#include <linux/jhash.h>
J. Bruce Fields169319f2019-06-19 12:39:46 -040045#include <linux/string_helpers.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020046#include "xdr4.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040047#include "xdr4cb.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050048#include "vfs.h"
J. Bruce Fieldsbfa4b362012-04-25 16:49:18 -040049#include "current_stateid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040051#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020052#include "pnfs.h"
Jeff Laytonfd4f83f2019-08-18 14:18:52 -040053#include "filecache.h"
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +040054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define NFSDDBG_FACILITY NFSDDBG_PROC
56
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050057#define all_ones {{~0,~0},~0}
58static const stateid_t one_stateid = {
59 .si_generation = ~0,
60 .si_opaque = all_ones,
61};
62static const stateid_t zero_stateid = {
63 /* all fields zero */
64};
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010065static const stateid_t currentstateid = {
66 .si_generation = 1,
67};
Trond Myklebustfb500a72017-11-03 08:00:12 -040068static const stateid_t close_stateid = {
69 .si_generation = 0xffffffffU,
70};
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050071
Andy Adamsonec6b5d72009-04-03 08:28:28 +030072static u64 current_sessionid = 1;
NeilBrownfd39ca92005-06-23 22:04:03 -070073
J. Bruce Fieldsf32f3c22011-12-12 15:00:35 -050074#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
75#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
Tigran Mkrtchyan19ff0f22012-02-13 22:55:23 +010076#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
Andrew Elbleae254dac2017-11-09 13:41:10 -050077#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* forward declarations */
Jeff Laytonf9c00c32014-07-23 16:17:41 -040080static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
Trond Myklebust60116952014-07-29 21:34:06 -040081static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
Scott Mayhew362063a2019-03-26 18:06:28 -040082void nfsd4_end_grace(struct nfsd_net *nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
J. Bruce Fields8b671b82009-02-22 14:51:34 -080084/* Locking: */
85
J. Bruce Fields8b671b82009-02-22 14:51:34 -080086/*
87 * Currently used for the del_recall_lru and file hash table. In an
88 * effort to decrease the scope of the client_mutex, this spinlock may
89 * eventually cover more:
90 */
Benny Halevycdc975052014-05-30 09:09:30 -040091static DEFINE_SPINLOCK(state_lock);
J. Bruce Fields8b671b82009-02-22 14:51:34 -080092
Andrew Elble4f34bd02017-11-08 17:29:51 -050093enum nfsd4_st_mutex_lock_subclass {
94 OPEN_STATEID_MUTEX = 0,
95 LOCK_STATEID_MUTEX = 1,
96};
97
Jeff Laytonb401be222014-07-29 21:34:33 -040098/*
99 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
100 * the refcount on the open stateid to drop.
101 */
102static DECLARE_WAIT_QUEUE_HEAD(close_wq);
103
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400104/*
105 * A waitqueue where a writer to clients/#/ctl destroying a client can
106 * wait for cl_rpc_users to drop to 0 and then for the client to be
107 * unhashed.
108 */
109static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
110
Jeff Layton9258a2d2018-03-16 09:47:22 -0400111static struct kmem_cache *client_slab;
Christoph Hellwigabf11352014-05-21 07:43:03 -0700112static struct kmem_cache *openowner_slab;
113static struct kmem_cache *lockowner_slab;
114static struct kmem_cache *file_slab;
115static struct kmem_cache *stateid_slab;
116static struct kmem_cache *deleg_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +0200117static struct kmem_cache *odstate_slab;
NeilBrowne60d4392005-06-23 22:03:01 -0700118
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400119static void free_session(struct nfsd4_session *);
Benny Halevy508dc6e2012-02-23 17:40:52 -0800120
Julia Lawallc4cb8972015-11-21 22:57:39 +0100121static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
Jeff Layton76d348f2016-09-16 16:28:24 -0400122static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200123
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400124static bool is_session_dead(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -0800125{
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400126 return ses->se_flags & NFS4_SESSION_DEAD;
127}
128
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -0400129static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
130{
131 if (atomic_read(&ses->se_ref) > ref_held_by_me)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -0400132 return nfserr_jukebox;
133 ses->se_flags |= NFS4_SESSION_DEAD;
134 return nfs_ok;
135}
136
J. Bruce Fields221a6872013-04-01 22:23:49 -0400137static bool is_client_expired(struct nfs4_client *clp)
138{
139 return clp->cl_time == 0;
140}
141
J. Bruce Fields221a6872013-04-01 22:23:49 -0400142static __be32 get_client_locked(struct nfs4_client *clp)
143{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400144 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
145
146 lockdep_assert_held(&nn->client_lock);
147
J. Bruce Fields221a6872013-04-01 22:23:49 -0400148 if (is_client_expired(clp))
149 return nfserr_expired;
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400150 atomic_inc(&clp->cl_rpc_users);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400151 return nfs_ok;
152}
153
154/* must be called under the client_lock */
155static inline void
156renew_client_locked(struct nfs4_client *clp)
157{
158 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
159
160 if (is_client_expired(clp)) {
161 WARN_ON(1);
162 printk("%s: client (clientid %08x/%08x) already expired\n",
163 __func__,
164 clp->cl_clientid.cl_boot,
165 clp->cl_clientid.cl_id);
166 return;
167 }
168
169 dprintk("renewing client (clientid %08x/%08x)\n",
170 clp->cl_clientid.cl_boot,
171 clp->cl_clientid.cl_id);
172 list_move_tail(&clp->cl_lru, &nn->client_lru);
173 clp->cl_time = get_seconds();
174}
175
Fengguang Wuba138432013-04-16 22:14:15 -0400176static void put_client_renew_locked(struct nfs4_client *clp)
J. Bruce Fields221a6872013-04-01 22:23:49 -0400177{
Trond Myklebust0a880a22014-07-30 08:27:10 -0400178 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
179
180 lockdep_assert_held(&nn->client_lock);
181
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400182 if (!atomic_dec_and_test(&clp->cl_rpc_users))
J. Bruce Fields221a6872013-04-01 22:23:49 -0400183 return;
184 if (!is_client_expired(clp))
185 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400186 else
187 wake_up_all(&expiry_wq);
J. Bruce Fields221a6872013-04-01 22:23:49 -0400188}
189
Jeff Layton4b24ca72014-06-30 11:48:44 -0400190static void put_client_renew(struct nfs4_client *clp)
191{
192 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
193
J. Bruce Fields14ed14c2019-03-20 11:54:11 -0400194 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
Jeff Laytond6c249b2014-07-08 14:02:50 -0400195 return;
196 if (!is_client_expired(clp))
197 renew_client_locked(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -0400198 else
199 wake_up_all(&expiry_wq);
Jeff Layton4b24ca72014-06-30 11:48:44 -0400200 spin_unlock(&nn->client_lock);
201}
202
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400203static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
204{
205 __be32 status;
206
207 if (is_session_dead(ses))
208 return nfserr_badsession;
209 status = get_client_locked(ses->se_client);
210 if (status)
211 return status;
212 atomic_inc(&ses->se_ref);
213 return nfs_ok;
214}
215
216static void nfsd4_put_session_locked(struct nfsd4_session *ses)
217{
218 struct nfs4_client *clp = ses->se_client;
Trond Myklebust0a880a22014-07-30 08:27:10 -0400219 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
220
221 lockdep_assert_held(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -0400222
223 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
224 free_session(ses);
225 put_client_renew_locked(clp);
226}
227
228static void nfsd4_put_session(struct nfsd4_session *ses)
229{
230 struct nfs4_client *clp = ses->se_client;
231 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
232
233 spin_lock(&nn->client_lock);
234 nfsd4_put_session_locked(ses);
235 spin_unlock(&nn->client_lock);
236}
237
Jeff Layton76d348f2016-09-16 16:28:24 -0400238static struct nfsd4_blocked_lock *
239find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
240 struct nfsd_net *nn)
241{
242 struct nfsd4_blocked_lock *cur, *found = NULL;
243
Jeff Layton0cc11a62016-10-20 09:34:31 -0400244 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400245 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
246 if (fh_match(fh, &cur->nbl_fh)) {
247 list_del_init(&cur->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -0400248 list_del_init(&cur->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -0400249 found = cur;
250 break;
251 }
252 }
Jeff Layton0cc11a62016-10-20 09:34:31 -0400253 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400254 if (found)
NeilBrowncb03f942018-11-30 10:04:08 +1100255 locks_delete_block(&found->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400256 return found;
257}
258
259static struct nfsd4_blocked_lock *
260find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
261 struct nfsd_net *nn)
262{
263 struct nfsd4_blocked_lock *nbl;
264
265 nbl = find_blocked_lock(lo, fh, nn);
266 if (!nbl) {
267 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
268 if (nbl) {
269 fh_copy_shallow(&nbl->nbl_fh, fh);
270 locks_init_lock(&nbl->nbl_lock);
271 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
272 &nfsd4_cb_notify_lock_ops,
273 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
274 }
275 }
276 return nbl;
277}
278
279static void
280free_blocked_lock(struct nfsd4_blocked_lock *nbl)
281{
Jeff Layton6aaafc42019-04-22 12:34:23 -0400282 locks_delete_block(&nbl->nbl_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -0400283 locks_release_private(&nbl->nbl_lock);
284 kfree(nbl);
285}
286
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400287static void
288remove_blocked_locks(struct nfs4_lockowner *lo)
289{
290 struct nfs4_client *clp = lo->lo_owner.so_client;
291 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
292 struct nfsd4_blocked_lock *nbl;
293 LIST_HEAD(reaplist);
294
295 /* Dequeue all blocked locks */
296 spin_lock(&nn->blocked_locks_lock);
297 while (!list_empty(&lo->lo_blocked)) {
298 nbl = list_first_entry(&lo->lo_blocked,
299 struct nfsd4_blocked_lock,
300 nbl_list);
301 list_del_init(&nbl->nbl_list);
302 list_move(&nbl->nbl_lru, &reaplist);
303 }
304 spin_unlock(&nn->blocked_locks_lock);
305
306 /* Now free them */
307 while (!list_empty(&reaplist)) {
308 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
309 nbl_lru);
310 list_del_init(&nbl->nbl_lru);
Jeff Layton68ef3bc2018-03-16 11:32:02 -0400311 free_blocked_lock(nbl);
312 }
313}
314
Jeff Laytonf4564582019-04-22 12:34:24 -0400315static void
316nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
317{
318 struct nfsd4_blocked_lock *nbl = container_of(cb,
319 struct nfsd4_blocked_lock, nbl_cb);
320 locks_delete_block(&nbl->nbl_lock);
321}
322
Jeff Layton76d348f2016-09-16 16:28:24 -0400323static int
324nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
325{
326 /*
327 * Since this is just an optimization, we don't try very hard if it
328 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
329 * just quit trying on anything else.
330 */
331 switch (task->tk_status) {
332 case -NFS4ERR_DELAY:
333 rpc_delay(task, 1 * HZ);
334 return 0;
335 default:
336 return 1;
337 }
338}
339
340static void
341nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
342{
343 struct nfsd4_blocked_lock *nbl = container_of(cb,
344 struct nfsd4_blocked_lock, nbl_cb);
345
346 free_blocked_lock(nbl);
347}
348
349static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
Jeff Laytonf4564582019-04-22 12:34:24 -0400350 .prepare = nfsd4_cb_notify_lock_prepare,
Jeff Layton76d348f2016-09-16 16:28:24 -0400351 .done = nfsd4_cb_notify_lock_done,
352 .release = nfsd4_cb_notify_lock_release,
353};
354
Kinglong Meeb5971af2014-08-22 10:18:43 -0400355static inline struct nfs4_stateowner *
356nfs4_get_stateowner(struct nfs4_stateowner *sop)
357{
358 atomic_inc(&sop->so_count);
359 return sop;
360}
361
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400362static int
Trond Myklebustd4f04892014-07-29 21:34:36 -0400363same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400364{
365 return (sop->so_owner.len == owner->len) &&
Trond Myklebustd4f04892014-07-29 21:34:36 -0400366 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400367}
368
369static struct nfs4_openowner *
370find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400371 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400372{
373 struct nfs4_stateowner *so;
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400374
Trond Myklebustd4f04892014-07-29 21:34:36 -0400375 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400376
Trond Myklebustd4f04892014-07-29 21:34:36 -0400377 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
378 so_strhash) {
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400379 if (!so->so_is_open_owner)
380 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -0400381 if (same_owner_str(so, &open->op_owner))
382 return openowner(nfs4_get_stateowner(so));
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400383 }
384 return NULL;
385}
386
387static struct nfs4_openowner *
388find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
Trond Myklebustd4f04892014-07-29 21:34:36 -0400389 struct nfs4_client *clp)
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400390{
391 struct nfs4_openowner *oo;
392
Trond Myklebustd4f04892014-07-29 21:34:36 -0400393 spin_lock(&clp->cl_lock);
394 oo = find_openstateowner_str_locked(hashval, open, clp);
395 spin_unlock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -0400396 return oo;
397}
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399static inline u32
400opaque_hashval(const void *ptr, int nbytes)
401{
402 unsigned char *cptr = (unsigned char *) ptr;
403
404 u32 x = 0;
405 while (nbytes--) {
406 x *= 37;
407 x += *cptr++;
408 }
409 return x;
410}
411
Jeff Layton5b095e92014-10-23 08:01:02 -0400412static void nfsd4_free_file_rcu(struct rcu_head *rcu)
J. Bruce Fields32513b42011-10-13 16:00:16 -0400413{
Jeff Layton5b095e92014-10-23 08:01:02 -0400414 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
415
416 kmem_cache_free(file_slab, fp);
J. Bruce Fields32513b42011-10-13 16:00:16 -0400417}
418
Christoph Hellwige6ba76e2014-08-14 08:50:16 +0200419void
NeilBrown13cd2182005-06-23 22:03:10 -0700420put_nfs4_file(struct nfs4_file *fi)
421{
Jeff Layton02e12152014-07-16 10:31:57 -0400422 might_lock(&state_lock);
423
Elena Reshetova818a34e2017-10-20 12:53:30 +0300424 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
Jeff Layton5b095e92014-10-23 08:01:02 -0400425 hlist_del_rcu(&fi->fi_hash);
Benny Halevycdc975052014-05-30 09:09:30 -0400426 spin_unlock(&state_lock);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200427 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
Jeff Layton5b095e92014-10-23 08:01:02 -0400428 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
429 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
J. Bruce Fields8b671b82009-02-22 14:51:34 -0800430 }
NeilBrown13cd2182005-06-23 22:03:10 -0700431}
432
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400433static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400434__nfs4_get_fd(struct nfs4_file *f, int oflag)
435{
436 if (f->fi_fds[oflag])
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400437 return nfsd_file_get(f->fi_fds[oflag]);
Trond Myklebustde186432014-07-10 14:07:26 -0400438 return NULL;
439}
440
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400441static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400442find_writeable_file_locked(struct nfs4_file *f)
443{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400444 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400445
446 lockdep_assert_held(&f->fi_lock);
447
448 ret = __nfs4_get_fd(f, O_WRONLY);
449 if (!ret)
450 ret = __nfs4_get_fd(f, O_RDWR);
451 return ret;
452}
453
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400454static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400455find_writeable_file(struct nfs4_file *f)
456{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400457 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400458
459 spin_lock(&f->fi_lock);
460 ret = find_writeable_file_locked(f);
461 spin_unlock(&f->fi_lock);
462
463 return ret;
464}
465
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400466static struct nfsd_file *
467find_readable_file_locked(struct nfs4_file *f)
Trond Myklebustde186432014-07-10 14:07:26 -0400468{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400469 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400470
471 lockdep_assert_held(&f->fi_lock);
472
473 ret = __nfs4_get_fd(f, O_RDONLY);
474 if (!ret)
475 ret = __nfs4_get_fd(f, O_RDWR);
476 return ret;
477}
478
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400479static struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400480find_readable_file(struct nfs4_file *f)
481{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400482 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400483
484 spin_lock(&f->fi_lock);
485 ret = find_readable_file_locked(f);
486 spin_unlock(&f->fi_lock);
487
488 return ret;
489}
490
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400491struct nfsd_file *
Trond Myklebustde186432014-07-10 14:07:26 -0400492find_any_file(struct nfs4_file *f)
493{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400494 struct nfsd_file *ret;
Trond Myklebustde186432014-07-10 14:07:26 -0400495
496 spin_lock(&f->fi_lock);
497 ret = __nfs4_get_fd(f, O_RDWR);
498 if (!ret) {
499 ret = __nfs4_get_fd(f, O_WRONLY);
500 if (!ret)
501 ret = __nfs4_get_fd(f, O_RDONLY);
502 }
503 spin_unlock(&f->fi_lock);
504 return ret;
505}
506
Trond Myklebust02a35082014-07-25 07:34:22 -0400507static atomic_long_t num_delegations;
Zhang Yanfei697ce9b2013-02-22 16:35:47 -0800508unsigned long max_delegations;
NeilBrownef0f3392006-04-10 22:55:41 -0700509
510/*
511 * Open owner state (share locks)
512 */
513
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500514/* hash tables for lock and open owners */
515#define OWNER_HASH_BITS 8
516#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
517#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
NeilBrownef0f3392006-04-10 22:55:41 -0700518
Trond Myklebustd4f04892014-07-29 21:34:36 -0400519static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400520{
521 unsigned int ret;
522
523 ret = opaque_hashval(ownername->data, ownername->len);
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -0500524 return ret & OWNER_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400525}
NeilBrownef0f3392006-04-10 22:55:41 -0700526
NeilBrownef0f3392006-04-10 22:55:41 -0700527/* hash table for nfs4_file */
528#define FILE_HASH_BITS 8
529#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
Shan Wei35079582011-01-14 17:35:59 +0800530
Trond Myklebustca943212014-07-23 16:17:39 -0400531static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -0400532{
Trond Myklebustca943212014-07-23 16:17:39 -0400533 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
534}
535
536static unsigned int file_hashval(struct knfsd_fh *fh)
537{
538 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
539}
540
Jeff Layton89876f82013-04-02 09:01:59 -0400541static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
NeilBrownef0f3392006-04-10 22:55:41 -0700542
Jeff Layton12659652014-07-10 14:07:28 -0400543static void
544__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields34775652013-08-23 17:55:18 -0400545{
Jeff Layton7214e862014-07-10 14:07:33 -0400546 lockdep_assert_held(&fp->fi_lock);
547
Jeff Layton12659652014-07-10 14:07:28 -0400548 if (access & NFS4_SHARE_ACCESS_WRITE)
549 atomic_inc(&fp->fi_access[O_WRONLY]);
550 if (access & NFS4_SHARE_ACCESS_READ)
551 atomic_inc(&fp->fi_access[O_RDONLY]);
J. Bruce Fields34775652013-08-23 17:55:18 -0400552}
553
Jeff Layton12659652014-07-10 14:07:28 -0400554static __be32
555nfs4_file_get_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400556{
Jeff Layton7214e862014-07-10 14:07:33 -0400557 lockdep_assert_held(&fp->fi_lock);
558
Jeff Layton12659652014-07-10 14:07:28 -0400559 /* Does this access mode make sense? */
560 if (access & ~NFS4_SHARE_ACCESS_BOTH)
561 return nfserr_inval;
562
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400563 /* Does it conflict with a deny mode already set? */
564 if ((access & fp->fi_share_deny) != 0)
565 return nfserr_share_denied;
566
Jeff Layton12659652014-07-10 14:07:28 -0400567 __nfs4_file_get_access(fp, access);
568 return nfs_ok;
J. Bruce Fields998db522010-08-07 09:21:41 -0400569}
570
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -0400571static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
572{
573 /* Common case is that there is no deny mode. */
574 if (deny) {
575 /* Does this deny mode make sense? */
576 if (deny & ~NFS4_SHARE_DENY_BOTH)
577 return nfserr_inval;
578
579 if ((deny & NFS4_SHARE_DENY_READ) &&
580 atomic_read(&fp->fi_access[O_RDONLY]))
581 return nfserr_share_denied;
582
583 if ((deny & NFS4_SHARE_DENY_WRITE) &&
584 atomic_read(&fp->fi_access[O_WRONLY]))
585 return nfserr_share_denied;
586 }
587 return nfs_ok;
588}
589
J. Bruce Fields998db522010-08-07 09:21:41 -0400590static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400591{
Trond Myklebustde186432014-07-10 14:07:26 -0400592 might_lock(&fp->fi_lock);
593
594 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400595 struct nfsd_file *f1 = NULL;
596 struct nfsd_file *f2 = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -0400597
Jeff Layton6d338b52014-07-10 14:07:29 -0400598 swap(f1, fp->fi_fds[oflag]);
J. Bruce Fields0c7c3e62013-03-28 20:37:14 -0400599 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
Jeff Layton6d338b52014-07-10 14:07:29 -0400600 swap(f2, fp->fi_fds[O_RDWR]);
Trond Myklebustde186432014-07-10 14:07:26 -0400601 spin_unlock(&fp->fi_lock);
602 if (f1)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400603 nfsd_file_put(f1);
Trond Myklebustde186432014-07-10 14:07:26 -0400604 if (f2)
Jeff Laytonfd4f83f2019-08-18 14:18:52 -0400605 nfsd_file_put(f2);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -0400606 }
607}
608
Jeff Layton12659652014-07-10 14:07:28 -0400609static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
J. Bruce Fields998db522010-08-07 09:21:41 -0400610{
Jeff Layton12659652014-07-10 14:07:28 -0400611 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
612
613 if (access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields998db522010-08-07 09:21:41 -0400614 __nfs4_file_put_access(fp, O_WRONLY);
Jeff Layton12659652014-07-10 14:07:28 -0400615 if (access & NFS4_SHARE_ACCESS_READ)
616 __nfs4_file_put_access(fp, O_RDONLY);
J. Bruce Fields998db522010-08-07 09:21:41 -0400617}
618
Sachin Bhamare8287f002015-04-27 14:50:14 +0200619/*
620 * Allocate a new open/delegation state counter. This is needed for
621 * pNFS for proper return on close semantics.
622 *
623 * Note that we only allocate it for pNFS-enabled exports, otherwise
624 * all pointers to struct nfs4_clnt_odstate are always NULL.
625 */
626static struct nfs4_clnt_odstate *
627alloc_clnt_odstate(struct nfs4_client *clp)
628{
629 struct nfs4_clnt_odstate *co;
630
631 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
632 if (co) {
633 co->co_client = clp;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300634 refcount_set(&co->co_odcount, 1);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200635 }
636 return co;
637}
638
639static void
640hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
641{
642 struct nfs4_file *fp = co->co_file;
643
644 lockdep_assert_held(&fp->fi_lock);
645 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
646}
647
648static inline void
649get_clnt_odstate(struct nfs4_clnt_odstate *co)
650{
651 if (co)
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300652 refcount_inc(&co->co_odcount);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200653}
654
655static void
656put_clnt_odstate(struct nfs4_clnt_odstate *co)
657{
658 struct nfs4_file *fp;
659
660 if (!co)
661 return;
662
663 fp = co->co_file;
Elena Reshetovacff7cb22017-10-20 12:53:29 +0300664 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
Sachin Bhamare8287f002015-04-27 14:50:14 +0200665 list_del(&co->co_perfile);
666 spin_unlock(&fp->fi_lock);
667
668 nfsd4_return_all_file_layouts(co->co_client, fp);
669 kmem_cache_free(odstate_slab, co);
670 }
671}
672
673static struct nfs4_clnt_odstate *
674find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
675{
676 struct nfs4_clnt_odstate *co;
677 struct nfs4_client *cl;
678
679 if (!new)
680 return NULL;
681
682 cl = new->co_client;
683
684 spin_lock(&fp->fi_lock);
685 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
686 if (co->co_client == cl) {
687 get_clnt_odstate(co);
688 goto out;
689 }
690 }
691 co = new;
692 co->co_file = fp;
693 hash_clnt_odstate_locked(new);
694out:
695 spin_unlock(&fp->fi_lock);
696 return co;
697}
698
Kinglong Meed19fb702017-01-18 19:04:42 +0800699struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
700 void (*sc_free)(struct nfs4_stid *))
J. Bruce Fields996e0932011-10-17 11:14:48 -0400701{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500702 struct nfs4_stid *stid;
703 int new_id;
704
Trond Myklebustf8338832014-07-25 07:34:19 -0400705 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500706 if (!stid)
707 return NULL;
J. Bruce Fields996e0932011-10-17 11:14:48 -0400708
Jeff Layton4770d722014-07-29 21:34:10 -0400709 idr_preload(GFP_KERNEL);
710 spin_lock(&cl->cl_lock);
J. Bruce Fields78599c42019-04-22 15:26:23 -0400711 /* Reserving 0 for start of file in nfsdfs "states" file: */
712 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
Jeff Layton4770d722014-07-29 21:34:10 -0400713 spin_unlock(&cl->cl_lock);
714 idr_preload_end();
Tejun Heoebd6c702013-03-13 14:59:37 -0700715 if (new_id < 0)
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500716 goto out_free;
Kinglong Meed19fb702017-01-18 19:04:42 +0800717
718 stid->sc_free = sc_free;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500719 stid->sc_client = cl;
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500720 stid->sc_stateid.si_opaque.so_id = new_id;
721 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
722 /* Will be incremented before return to client: */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300723 refcount_set(&stid->sc_count, 1);
Jeff Layton9767feb2015-10-01 09:05:50 -0400724 spin_lock_init(&stid->sc_lock);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500725
J. Bruce Fields996e0932011-10-17 11:14:48 -0400726 /*
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500727 * It shouldn't be a problem to reuse an opaque stateid value.
728 * I don't think it is for 4.1. But with 4.0 I worry that, for
729 * example, a stray write retransmission could be accepted by
730 * the server when it should have been rejected. Therefore,
731 * adopt a trick from the sctp code to attempt to maximize the
732 * amount of time until an id is reused, by ensuring they always
733 * "increase" (mod INT_MAX):
J. Bruce Fields996e0932011-10-17 11:14:48 -0400734 */
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500735 return stid;
736out_free:
Wei Yongjun2c44a232013-04-09 14:15:31 +0800737 kmem_cache_free(slab, stid);
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500738 return NULL;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400739}
740
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -0400741/*
742 * Create a unique stateid_t to represent each COPY.
743 */
744int nfs4_init_cp_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
745{
746 int new_id;
747
748 idr_preload(GFP_KERNEL);
749 spin_lock(&nn->s2s_cp_lock);
750 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, copy, 0, 0, GFP_NOWAIT);
751 spin_unlock(&nn->s2s_cp_lock);
752 idr_preload_end();
753 if (new_id < 0)
754 return 0;
755 copy->cp_stateid.si_opaque.so_id = new_id;
756 copy->cp_stateid.si_opaque.so_clid.cl_boot = nn->boot_time;
757 copy->cp_stateid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
758 return 1;
759}
760
761void nfs4_free_cp_state(struct nfsd4_copy *copy)
762{
763 struct nfsd_net *nn;
764
765 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
766 spin_lock(&nn->s2s_cp_lock);
767 idr_remove(&nn->s2s_cp_stateids, copy->cp_stateid.si_opaque.so_id);
768 spin_unlock(&nn->s2s_cp_lock);
769}
770
Jeff Laytonb49e0842014-07-29 21:34:11 -0400771static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400772{
Trond Myklebust60116952014-07-29 21:34:06 -0400773 struct nfs4_stid *stid;
Trond Myklebust60116952014-07-29 21:34:06 -0400774
Kinglong Meed19fb702017-01-18 19:04:42 +0800775 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
Trond Myklebust60116952014-07-29 21:34:06 -0400776 if (!stid)
777 return NULL;
778
Kinglong Meed19fb702017-01-18 19:04:42 +0800779 return openlockstateid(stid);
Trond Myklebust60116952014-07-29 21:34:06 -0400780}
781
782static void nfs4_free_deleg(struct nfs4_stid *stid)
783{
Trond Myklebust60116952014-07-29 21:34:06 -0400784 kmem_cache_free(deleg_slab, stid);
785 atomic_long_dec(&num_delegations);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -0400786}
787
NeilBrown6282cd52014-06-04 17:39:26 +1000788/*
789 * When we recall a delegation, we should be careful not to hand it
790 * out again straight away.
791 * To ensure this we keep a pair of bloom filters ('new' and 'old')
792 * in which the filehandles of recalled delegations are "stored".
793 * If a filehandle appear in either filter, a delegation is blocked.
794 * When a delegation is recalled, the filehandle is stored in the "new"
795 * filter.
796 * Every 30 seconds we swap the filters and clear the "new" one,
797 * unless both are empty of course.
798 *
799 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
800 * low 3 bytes as hash-table indices.
801 *
Jeff Laytonf54fe962014-07-25 07:34:26 -0400802 * 'blocked_delegations_lock', which is always taken in block_delegations(),
NeilBrown6282cd52014-06-04 17:39:26 +1000803 * is used to manage concurrent access. Testing does not need the lock
804 * except when swapping the two filters.
805 */
Jeff Laytonf54fe962014-07-25 07:34:26 -0400806static DEFINE_SPINLOCK(blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000807static struct bloom_pair {
808 int entries, old_entries;
809 time_t swap_time;
810 int new; /* index into 'set' */
811 DECLARE_BITMAP(set[2], 256);
812} blocked_delegations;
813
814static int delegation_blocked(struct knfsd_fh *fh)
815{
816 u32 hash;
817 struct bloom_pair *bd = &blocked_delegations;
818
819 if (bd->entries == 0)
820 return 0;
821 if (seconds_since_boot() - bd->swap_time > 30) {
Jeff Laytonf54fe962014-07-25 07:34:26 -0400822 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000823 if (seconds_since_boot() - bd->swap_time > 30) {
824 bd->entries -= bd->old_entries;
825 bd->old_entries = bd->entries;
826 memset(bd->set[bd->new], 0,
827 sizeof(bd->set[0]));
828 bd->new = 1-bd->new;
829 bd->swap_time = seconds_since_boot();
830 }
Jeff Laytonf54fe962014-07-25 07:34:26 -0400831 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000832 }
Daniel Borkmann87545892014-12-10 16:33:11 +0100833 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000834 if (test_bit(hash&255, bd->set[0]) &&
835 test_bit((hash>>8)&255, bd->set[0]) &&
836 test_bit((hash>>16)&255, bd->set[0]))
837 return 1;
838
839 if (test_bit(hash&255, bd->set[1]) &&
840 test_bit((hash>>8)&255, bd->set[1]) &&
841 test_bit((hash>>16)&255, bd->set[1]))
842 return 1;
843
844 return 0;
845}
846
847static void block_delegations(struct knfsd_fh *fh)
848{
849 u32 hash;
850 struct bloom_pair *bd = &blocked_delegations;
851
Daniel Borkmann87545892014-12-10 16:33:11 +0100852 hash = jhash(&fh->fh_base, fh->fh_size, 0);
NeilBrown6282cd52014-06-04 17:39:26 +1000853
Jeff Laytonf54fe962014-07-25 07:34:26 -0400854 spin_lock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000855 __set_bit(hash&255, bd->set[bd->new]);
856 __set_bit((hash>>8)&255, bd->set[bd->new]);
857 __set_bit((hash>>16)&255, bd->set[bd->new]);
858 if (bd->entries == 0)
859 bd->swap_time = seconds_since_boot();
860 bd->entries += 1;
Jeff Laytonf54fe962014-07-25 07:34:26 -0400861 spin_unlock(&blocked_delegations_lock);
NeilBrown6282cd52014-06-04 17:39:26 +1000862}
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864static struct nfs4_delegation *
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500865alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
866 struct svc_fh *current_fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +0200867 struct nfs4_clnt_odstate *odstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 struct nfs4_delegation *dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400870 long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 dprintk("NFSD alloc_init_deleg\n");
Trond Myklebust02a35082014-07-25 07:34:22 -0400873 n = atomic_long_inc_return(&num_delegations);
874 if (n < 0 || n > max_delegations)
875 goto out_dec;
NeilBrown6282cd52014-06-04 17:39:26 +1000876 if (delegation_blocked(&current_fh->fh_handle))
Trond Myklebust02a35082014-07-25 07:34:22 -0400877 goto out_dec;
Kinglong Meed19fb702017-01-18 19:04:42 +0800878 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
NeilBrown5b2d21c2005-06-23 22:03:04 -0700879 if (dp == NULL)
Trond Myklebust02a35082014-07-25 07:34:22 -0400880 goto out_dec;
Trond Myklebust60116952014-07-29 21:34:06 -0400881
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400882 /*
883 * delegation seqid's are never incremented. The 4.1 special
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400884 * meaning of seqid 0 isn't meaningful, really, but let's avoid
885 * 0 anyway just for consistency and use 1:
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400886 */
887 dp->dl_stid.sc_stateid.si_generation = 1;
NeilBrownea1da632005-06-23 22:04:17 -0700888 INIT_LIST_HEAD(&dp->dl_perfile);
889 INIT_LIST_HEAD(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 INIT_LIST_HEAD(&dp->dl_recall_lru);
Sachin Bhamare8287f002015-04-27 14:50:14 +0200891 dp->dl_clnt_odstate = odstate;
892 get_clnt_odstate(odstate);
J. Bruce Fields99c41512013-05-21 16:21:25 -0400893 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
Christoph Hellwigf0b5de12014-09-24 12:19:18 +0200894 dp->dl_retries = 1;
895 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
Christoph Hellwig0162ac22014-09-24 12:19:19 +0200896 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
J. Bruce Fields86d29b12018-02-21 15:27:28 -0500897 get_nfs4_file(fp);
898 dp->dl_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return dp;
Trond Myklebust02a35082014-07-25 07:34:22 -0400900out_dec:
901 atomic_long_dec(&num_delegations);
902 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905void
Trond Myklebust60116952014-07-29 21:34:06 -0400906nfs4_put_stid(struct nfs4_stid *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Trond Myklebust11b91642014-07-29 21:34:08 -0400908 struct nfs4_file *fp = s->sc_file;
Trond Myklebust60116952014-07-29 21:34:06 -0400909 struct nfs4_client *clp = s->sc_client;
910
Jeff Layton4770d722014-07-29 21:34:10 -0400911 might_lock(&clp->cl_lock);
912
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300913 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
Jeff Laytonb401be222014-07-29 21:34:33 -0400914 wake_up_all(&close_wq);
Trond Myklebust60116952014-07-29 21:34:06 -0400915 return;
Jeff Laytonb401be222014-07-29 21:34:33 -0400916 }
Trond Myklebust60116952014-07-29 21:34:06 -0400917 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
Jeff Layton4770d722014-07-29 21:34:10 -0400918 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400919 s->sc_free(s);
Trond Myklebust11b91642014-07-29 21:34:08 -0400920 if (fp)
921 put_nfs4_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Jeff Layton9767feb2015-10-01 09:05:50 -0400924void
925nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
926{
927 stateid_t *src = &stid->sc_stateid;
928
929 spin_lock(&stid->sc_lock);
930 if (unlikely(++src->si_generation == 0))
931 src->si_generation = 1;
932 memcpy(dst, src, sizeof(*dst));
933 spin_unlock(&stid->sc_lock);
934}
935
J. Bruce Fields353601e2018-02-16 14:29:42 -0500936static void put_deleg_file(struct nfs4_file *fp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400938 struct nfsd_file *nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -0500939
940 spin_lock(&fp->fi_lock);
941 if (--fp->fi_delegees == 0)
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400942 swap(nf, fp->fi_deleg_file);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500943 spin_unlock(&fp->fi_lock);
944
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400945 if (nf)
946 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500947}
948
949static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
950{
J. Bruce Fieldscba7b3d2018-02-15 15:50:49 -0500951 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400952 struct nfsd_file *nf = fp->fi_deleg_file;
Jeff Layton417c6622014-07-21 09:34:57 -0400953
J. Bruce Fieldsb8232d32018-02-15 15:29:15 -0500954 WARN_ON_ONCE(!fp->fi_delegees);
955
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400956 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500957 put_deleg_file(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500960static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
961{
962 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -0500963 nfs4_unlock_deleg_lease(dp);
J. Bruce Fields0af6e692018-02-21 15:11:03 -0500964 nfs4_put_stid(&dp->dl_stid);
965}
966
Christoph Hellwigcd61c522014-08-14 08:44:57 +0200967void nfs4_unhash_stid(struct nfs4_stid *s)
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400968{
J. Bruce Fields3abdb602013-02-03 12:23:01 -0500969 s->sc_type = 0;
J. Bruce Fields6136d2b2011-09-23 16:21:15 -0400970}
971
Andrew Elble34ed9872015-10-15 12:07:28 -0400972/**
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500973 * nfs4_delegation_exists - Discover if this delegation already exists
Andrew Elble34ed9872015-10-15 12:07:28 -0400974 * @clp: a pointer to the nfs4_client we're granting a delegation to
975 * @fp: a pointer to the nfs4_file we're granting a delegation on
976 *
977 * Return:
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500978 * On success: true iff an existing delegation is found
Andrew Elble34ed9872015-10-15 12:07:28 -0400979 */
980
J. Bruce Fields68b18f52018-02-19 11:38:33 -0500981static bool
982nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
Benny Halevy931ee562014-05-30 09:09:27 -0400983{
Andrew Elble34ed9872015-10-15 12:07:28 -0400984 struct nfs4_delegation *searchdp = NULL;
985 struct nfs4_client *searchclp = NULL;
986
Benny Halevycdc975052014-05-30 09:09:30 -0400987 lockdep_assert_held(&state_lock);
Jeff Layton417c6622014-07-21 09:34:57 -0400988 lockdep_assert_held(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -0400989
Andrew Elble34ed9872015-10-15 12:07:28 -0400990 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
991 searchclp = searchdp->dl_stid.sc_client;
992 if (clp == searchclp) {
Fengguang Wu51d87bc2018-03-22 13:37:20 +0800993 return true;
Andrew Elble34ed9872015-10-15 12:07:28 -0400994 }
995 }
Fengguang Wu51d87bc2018-03-22 13:37:20 +0800996 return false;
Andrew Elble34ed9872015-10-15 12:07:28 -0400997}
998
999/**
1000 * hash_delegation_locked - Add a delegation to the appropriate lists
1001 * @dp: a pointer to the nfs4_delegation we are adding.
1002 * @fp: a pointer to the nfs4_file we're granting a delegation on
1003 *
1004 * Return:
1005 * On success: NULL if the delegation was successfully hashed.
1006 *
1007 * On error: -EAGAIN if one was previously granted to this
1008 * nfs4_client for this nfs4_file. Delegation is not hashed.
1009 *
1010 */
1011
1012static int
1013hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1014{
Andrew Elble34ed9872015-10-15 12:07:28 -04001015 struct nfs4_client *clp = dp->dl_stid.sc_client;
1016
1017 lockdep_assert_held(&state_lock);
1018 lockdep_assert_held(&fp->fi_lock);
1019
J. Bruce Fields68b18f52018-02-19 11:38:33 -05001020 if (nfs4_delegation_exists(clp, fp))
1021 return -EAGAIN;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001022 refcount_inc(&dp->dl_stid.sc_count);
Benny Halevy3fb87d12014-05-30 09:09:31 -04001023 dp->dl_stid.sc_type = NFS4_DELEG_STID;
Benny Halevy931ee562014-05-30 09:09:27 -04001024 list_add(&dp->dl_perfile, &fp->fi_delegations);
Andrew Elble34ed9872015-10-15 12:07:28 -04001025 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1026 return 0;
Benny Halevy931ee562014-05-30 09:09:27 -04001027}
1028
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001029static bool
Jeff Layton42690672014-07-25 07:34:20 -04001030unhash_delegation_locked(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Trond Myklebust11b91642014-07-29 21:34:08 -04001032 struct nfs4_file *fp = dp->dl_stid.sc_file;
Jeff Layton02e12152014-07-16 10:31:57 -04001033
Jeff Layton42690672014-07-25 07:34:20 -04001034 lockdep_assert_held(&state_lock);
1035
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001036 if (list_empty(&dp->dl_perfile))
1037 return false;
1038
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04001039 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
Jeff Laytond55a1662014-07-22 13:52:06 -04001040 /* Ensure that deleg break won't try to requeue it */
1041 ++dp->dl_time;
Jeff Layton417c6622014-07-21 09:34:57 -04001042 spin_lock(&fp->fi_lock);
Benny Halevy931ee562014-05-30 09:09:27 -04001043 list_del_init(&dp->dl_perclnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 list_del_init(&dp->dl_recall_lru);
Jeff Layton02e12152014-07-16 10:31:57 -04001045 list_del_init(&dp->dl_perfile);
1046 spin_unlock(&fp->fi_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001047 return true;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001048}
1049
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001050static void destroy_delegation(struct nfs4_delegation *dp)
1051{
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001052 bool unhashed;
1053
Jeff Layton42690672014-07-25 07:34:20 -04001054 spin_lock(&state_lock);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001055 unhashed = unhash_delegation_locked(dp);
Jeff Layton42690672014-07-25 07:34:20 -04001056 spin_unlock(&state_lock);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001057 if (unhashed)
1058 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001059}
1060
1061static void revoke_delegation(struct nfs4_delegation *dp)
1062{
1063 struct nfs4_client *clp = dp->dl_stid.sc_client;
1064
Jeff Layton2d4a5322014-07-25 07:34:21 -04001065 WARN_ON(!list_empty(&dp->dl_recall_lru));
1066
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001067 if (clp->cl_minorversion) {
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001068 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001069 refcount_inc(&dp->dl_stid.sc_count);
Jeff Layton2d4a5322014-07-25 07:34:21 -04001070 spin_lock(&clp->cl_lock);
1071 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1072 spin_unlock(&clp->cl_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001073 }
J. Bruce Fields0af6e692018-02-21 15:11:03 -05001074 destroy_unhashed_deleg(dp);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04001075}
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077/*
1078 * SETCLIENTID state
1079 */
1080
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001081static unsigned int clientid_hashval(u32 id)
1082{
1083 return id & CLIENT_HASH_MASK;
1084}
1085
Scott Mayhew6b189102019-03-26 18:06:26 -04001086static unsigned int clientstr_hashval(struct xdr_netobj name)
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001087{
Scott Mayhew6b189102019-03-26 18:06:26 -04001088 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
J. Bruce Fieldsddc04c42011-07-30 23:46:29 -04001089}
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091/*
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001092 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1093 * st_{access,deny}_bmap field of the stateid, in order to track not
1094 * only what share bits are currently in force, but also what
1095 * combinations of share bits previous opens have used. This allows us
1096 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1097 * return an error if the client attempt to downgrade to a combination
1098 * of share bits not explicable by closing some of its previous opens.
1099 *
1100 * XXX: This enforcement is actually incomplete, since we don't keep
1101 * track of access/deny bit combinations; so, e.g., we allow:
1102 *
1103 * OPEN allow read, deny write
1104 * OPEN allow both, deny none
1105 * DOWNGRADE allow read, deny none
1106 *
1107 * which we should reject.
1108 */
Jeff Layton5ae037e2012-05-11 09:45:11 -04001109static unsigned int
1110bmap_to_share_mode(unsigned long bmap) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001111 int i;
Jeff Layton5ae037e2012-05-11 09:45:11 -04001112 unsigned int access = 0;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001113
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001114 for (i = 1; i < 4; i++) {
1115 if (test_bit(i, &bmap))
Jeff Layton5ae037e2012-05-11 09:45:11 -04001116 access |= i;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001117 }
Jeff Layton5ae037e2012-05-11 09:45:11 -04001118 return access;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001119}
1120
Jeff Layton82c5ff12012-05-11 09:45:13 -04001121/* set share access for a given stateid */
1122static inline void
1123set_access(u32 access, struct nfs4_ol_stateid *stp)
1124{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001125 unsigned char mask = 1 << access;
1126
1127 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1128 stp->st_access_bmap |= mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001129}
1130
1131/* clear share access for a given stateid */
1132static inline void
1133clear_access(u32 access, struct nfs4_ol_stateid *stp)
1134{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001135 unsigned char mask = 1 << access;
1136
1137 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1138 stp->st_access_bmap &= ~mask;
Jeff Layton82c5ff12012-05-11 09:45:13 -04001139}
1140
1141/* test whether a given stateid has access */
1142static inline bool
1143test_access(u32 access, struct nfs4_ol_stateid *stp)
1144{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001145 unsigned char mask = 1 << access;
1146
1147 return (bool)(stp->st_access_bmap & mask);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001148}
1149
Jeff Laytonce0fc432012-05-11 09:45:14 -04001150/* set share deny for a given stateid */
1151static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001152set_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001153{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001154 unsigned char mask = 1 << deny;
1155
1156 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1157 stp->st_deny_bmap |= mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001158}
1159
1160/* clear share deny for a given stateid */
1161static inline void
Jeff Laytonc11c5912014-07-10 14:07:30 -04001162clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001163{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001164 unsigned char mask = 1 << deny;
1165
1166 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1167 stp->st_deny_bmap &= ~mask;
Jeff Laytonce0fc432012-05-11 09:45:14 -04001168}
1169
1170/* test whether a given stateid is denying specific access */
1171static inline bool
Jeff Laytonc11c5912014-07-10 14:07:30 -04001172test_deny(u32 deny, struct nfs4_ol_stateid *stp)
Jeff Laytonce0fc432012-05-11 09:45:14 -04001173{
Jeff Laytonc11c5912014-07-10 14:07:30 -04001174 unsigned char mask = 1 << deny;
1175
1176 return (bool)(stp->st_deny_bmap & mask);
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001177}
1178
1179static int nfs4_access_to_omode(u32 access)
1180{
J. Bruce Fields8f34a432010-09-02 15:23:16 -04001181 switch (access & NFS4_SHARE_ACCESS_BOTH) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001182 case NFS4_SHARE_ACCESS_READ:
1183 return O_RDONLY;
1184 case NFS4_SHARE_ACCESS_WRITE:
1185 return O_WRONLY;
1186 case NFS4_SHARE_ACCESS_BOTH:
1187 return O_RDWR;
1188 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05001189 WARN_ON_ONCE(1);
1190 return O_RDONLY;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04001191}
1192
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001193/*
1194 * A stateid that had a deny mode associated with it is being released
1195 * or downgraded. Recalculate the deny mode on the file.
1196 */
1197static void
1198recalculate_deny_mode(struct nfs4_file *fp)
1199{
1200 struct nfs4_ol_stateid *stp;
1201
1202 spin_lock(&fp->fi_lock);
1203 fp->fi_share_deny = 0;
1204 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1205 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1206 spin_unlock(&fp->fi_lock);
1207}
1208
1209static void
1210reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1211{
1212 int i;
1213 bool change = false;
1214
1215 for (i = 1; i < 4; i++) {
1216 if ((i & deny) != i) {
1217 change = true;
1218 clear_deny(i, stp);
1219 }
1220 }
1221
1222 /* Recalculate per-file deny mode if there was a change */
1223 if (change)
Trond Myklebust11b91642014-07-29 21:34:08 -04001224 recalculate_deny_mode(stp->st_stid.sc_file);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001225}
1226
Jeff Layton82c5ff12012-05-11 09:45:13 -04001227/* release all access and file references for a given stateid */
1228static void
1229release_all_access(struct nfs4_ol_stateid *stp)
1230{
1231 int i;
Trond Myklebust11b91642014-07-29 21:34:08 -04001232 struct nfs4_file *fp = stp->st_stid.sc_file;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04001233
1234 if (fp && stp->st_deny_bmap != 0)
1235 recalculate_deny_mode(fp);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001236
1237 for (i = 1; i < 4; i++) {
1238 if (test_access(i, stp))
Trond Myklebust11b91642014-07-29 21:34:08 -04001239 nfs4_file_put_access(stp->st_stid.sc_file, i);
Jeff Layton82c5ff12012-05-11 09:45:13 -04001240 clear_access(i, stp);
1241 }
1242}
1243
Kinglong Meed50ffde2015-07-16 12:05:07 +08001244static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1245{
1246 kfree(sop->so_owner.data);
1247 sop->so_ops->so_free(sop);
1248}
1249
Jeff Layton6b180f02014-07-29 21:34:26 -04001250static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1251{
Jeff Laytona819ecc2014-07-29 21:34:38 -04001252 struct nfs4_client *clp = sop->so_client;
1253
1254 might_lock(&clp->cl_lock);
1255
1256 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
Jeff Layton6b180f02014-07-29 21:34:26 -04001257 return;
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001258 sop->so_ops->so_unhash(sop);
Jeff Laytona819ecc2014-07-29 21:34:38 -04001259 spin_unlock(&clp->cl_lock);
Kinglong Meed50ffde2015-07-16 12:05:07 +08001260 nfs4_free_stateowner(sop);
Jeff Layton6b180f02014-07-29 21:34:26 -04001261}
1262
Jeff Laytone8568732015-08-24 12:41:47 -04001263static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001264{
Trond Myklebust11b91642014-07-29 21:34:08 -04001265 struct nfs4_file *fp = stp->st_stid.sc_file;
Trond Myklebust1d31a252014-07-10 14:07:25 -04001266
Jeff Layton1c755dc2014-07-29 21:34:12 -04001267 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1268
Jeff Laytone8568732015-08-24 12:41:47 -04001269 if (list_empty(&stp->st_perfile))
1270 return false;
1271
Trond Myklebust1d31a252014-07-10 14:07:25 -04001272 spin_lock(&fp->fi_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001273 list_del_init(&stp->st_perfile);
Trond Myklebust1d31a252014-07-10 14:07:25 -04001274 spin_unlock(&fp->fi_lock);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001275 list_del(&stp->st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001276 return true;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001277}
1278
Trond Myklebust60116952014-07-29 21:34:06 -04001279static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001280{
Trond Myklebust60116952014-07-29 21:34:06 -04001281 struct nfs4_ol_stateid *stp = openlockstateid(stid);
J. Bruce Fields4665e2b2011-09-06 14:50:49 -04001282
Sachin Bhamare8287f002015-04-27 14:50:14 +02001283 put_clnt_odstate(stp->st_clnt_odstate);
Trond Myklebust60116952014-07-29 21:34:06 -04001284 release_all_access(stp);
Jeff Laytond3134b12014-07-29 21:34:32 -04001285 if (stp->st_stateowner)
1286 nfs4_put_stateowner(stp->st_stateowner);
Trond Myklebust60116952014-07-29 21:34:06 -04001287 kmem_cache_free(stateid_slab, stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001288}
1289
Jeff Laytonb49e0842014-07-29 21:34:11 -04001290static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001291{
Jeff Laytonb49e0842014-07-29 21:34:11 -04001292 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1293 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001294 struct nfsd_file *nf;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001295
Jeff Laytoneb82dd32019-08-18 14:18:53 -04001296 nf = find_any_file(stp->st_stid.sc_file);
1297 if (nf) {
1298 get_file(nf->nf_file);
1299 filp_close(nf->nf_file, (fl_owner_t)lo);
1300 nfsd_file_put(nf);
1301 }
Jeff Laytonb49e0842014-07-29 21:34:11 -04001302 nfs4_free_ol_stateid(stid);
1303}
1304
Jeff Layton2c41beb2014-07-29 21:34:41 -04001305/*
1306 * Put the persistent reference to an already unhashed generic stateid, while
1307 * holding the cl_lock. If it's the last reference, then put it onto the
1308 * reaplist for later destruction.
1309 */
1310static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1311 struct list_head *reaplist)
1312{
1313 struct nfs4_stid *s = &stp->st_stid;
1314 struct nfs4_client *clp = s->sc_client;
1315
1316 lockdep_assert_held(&clp->cl_lock);
1317
1318 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1319
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03001320 if (!refcount_dec_and_test(&s->sc_count)) {
Jeff Layton2c41beb2014-07-29 21:34:41 -04001321 wake_up_all(&close_wq);
1322 return;
1323 }
1324
1325 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1326 list_add(&stp->st_locks, reaplist);
1327}
1328
Jeff Laytone8568732015-08-24 12:41:47 -04001329static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Layton3c1c9952014-07-29 21:34:39 -04001330{
Chuck Leverf46c4452016-10-29 18:19:03 -04001331 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001332
1333 list_del_init(&stp->st_locks);
Christoph Hellwigcd61c522014-08-14 08:44:57 +02001334 nfs4_unhash_stid(&stp->st_stid);
Jeff Laytone8568732015-08-24 12:41:47 -04001335 return unhash_ol_stateid(stp);
Jeff Layton3c1c9952014-07-29 21:34:39 -04001336}
1337
Jeff Layton5adfd882014-07-29 21:34:31 -04001338static void release_lock_stateid(struct nfs4_ol_stateid *stp)
Jeff Laytonb49e0842014-07-29 21:34:11 -04001339{
Chuck Leverf46c4452016-10-29 18:19:03 -04001340 struct nfs4_client *clp = stp->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04001341 bool unhashed;
Jeff Layton1c755dc2014-07-29 21:34:12 -04001342
Chuck Leverf46c4452016-10-29 18:19:03 -04001343 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001344 unhashed = unhash_lock_stateid(stp);
Chuck Leverf46c4452016-10-29 18:19:03 -04001345 spin_unlock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001346 if (unhashed)
1347 nfs4_put_stid(&stp->st_stid);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001348}
1349
Trond Myklebustc58c6612014-07-29 21:34:35 -04001350static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001351{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001352 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustc58c6612014-07-29 21:34:35 -04001353
Trond Myklebustd4f04892014-07-29 21:34:36 -04001354 lockdep_assert_held(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04001355
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001356 list_del_init(&lo->lo_owner.so_strhash);
1357}
1358
Jeff Layton2c41beb2014-07-29 21:34:41 -04001359/*
1360 * Free a list of generic stateids that were collected earlier after being
1361 * fully unhashed.
1362 */
1363static void
1364free_ol_stateid_reaplist(struct list_head *reaplist)
1365{
1366 struct nfs4_ol_stateid *stp;
Kinglong Meefb94d762014-08-05 21:20:27 +08001367 struct nfs4_file *fp;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001368
1369 might_sleep();
1370
1371 while (!list_empty(reaplist)) {
1372 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1373 st_locks);
1374 list_del(&stp->st_locks);
Kinglong Meefb94d762014-08-05 21:20:27 +08001375 fp = stp->st_stid.sc_file;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001376 stp->st_stid.sc_free(&stp->st_stid);
Kinglong Meefb94d762014-08-05 21:20:27 +08001377 if (fp)
1378 put_nfs4_file(fp);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001379 }
1380}
1381
Jeff Laytond83017f2014-07-29 21:34:42 -04001382static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1383 struct list_head *reaplist)
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001384{
1385 struct nfs4_ol_stateid *stp;
1386
Jeff Laytone8568732015-08-24 12:41:47 -04001387 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1388
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04001389 while (!list_empty(&open_stp->st_locks)) {
1390 stp = list_entry(open_stp->st_locks.next,
1391 struct nfs4_ol_stateid, st_locks);
Jeff Laytone8568732015-08-24 12:41:47 -04001392 WARN_ON(!unhash_lock_stateid(stp));
Jeff Laytond83017f2014-07-29 21:34:42 -04001393 put_ol_stateid_locked(stp, reaplist);
J. Bruce Fields529d7b22011-03-02 23:48:33 -05001394 }
1395}
1396
Jeff Laytone8568732015-08-24 12:41:47 -04001397static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
Jeff Laytond83017f2014-07-29 21:34:42 -04001398 struct list_head *reaplist)
J. Bruce Fields22839632009-01-11 14:27:17 -05001399{
Jeff Laytone8568732015-08-24 12:41:47 -04001400 bool unhashed;
1401
Jeff Layton2c41beb2014-07-29 21:34:41 -04001402 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1403
Jeff Laytone8568732015-08-24 12:41:47 -04001404 unhashed = unhash_ol_stateid(stp);
Jeff Laytond83017f2014-07-29 21:34:42 -04001405 release_open_stateid_locks(stp, reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04001406 return unhashed;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001407}
1408
1409static void release_open_stateid(struct nfs4_ol_stateid *stp)
1410{
Jeff Layton2c41beb2014-07-29 21:34:41 -04001411 LIST_HEAD(reaplist);
1412
1413 spin_lock(&stp->st_stid.sc_client->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04001414 if (unhash_open_stateid(stp, &reaplist))
1415 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001416 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1417 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fields22839632009-01-11 14:27:17 -05001418}
1419
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001420static void unhash_openowner_locked(struct nfs4_openowner *oo)
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001421{
Trond Myklebustd4f04892014-07-29 21:34:36 -04001422 struct nfs4_client *clp = oo->oo_owner.so_client;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001423
Trond Myklebustd4f04892014-07-29 21:34:36 -04001424 lockdep_assert_held(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001425
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001426 list_del_init(&oo->oo_owner.so_strhash);
1427 list_del_init(&oo->oo_perclient);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001428}
1429
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001430static void release_last_closed_stateid(struct nfs4_openowner *oo)
1431{
Jeff Layton217526e2014-07-30 08:27:11 -04001432 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1433 nfsd_net_id);
1434 struct nfs4_ol_stateid *s;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001435
Jeff Layton217526e2014-07-30 08:27:11 -04001436 spin_lock(&nn->client_lock);
1437 s = oo->oo_last_closed_stid;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001438 if (s) {
Jeff Laytond3134b12014-07-29 21:34:32 -04001439 list_del_init(&oo->oo_close_lru);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001440 oo->oo_last_closed_stid = NULL;
1441 }
Jeff Layton217526e2014-07-30 08:27:11 -04001442 spin_unlock(&nn->client_lock);
1443 if (s)
1444 nfs4_put_stid(&s->st_stid);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001445}
1446
Jeff Layton2c41beb2014-07-29 21:34:41 -04001447static void release_openowner(struct nfs4_openowner *oo)
Jeff Layton8f4b54c2014-07-29 21:34:29 -04001448{
1449 struct nfs4_ol_stateid *stp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001450 struct nfs4_client *clp = oo->oo_owner.so_client;
Jeff Layton2c41beb2014-07-29 21:34:41 -04001451 struct list_head reaplist;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001452
Jeff Layton2c41beb2014-07-29 21:34:41 -04001453 INIT_LIST_HEAD(&reaplist);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001454
Trond Myklebustd4f04892014-07-29 21:34:36 -04001455 spin_lock(&clp->cl_lock);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04001456 unhash_openowner_locked(oo);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001457 while (!list_empty(&oo->oo_owner.so_stateids)) {
1458 stp = list_first_entry(&oo->oo_owner.so_stateids,
1459 struct nfs4_ol_stateid, st_perstateowner);
Jeff Laytone8568732015-08-24 12:41:47 -04001460 if (unhash_open_stateid(stp, &reaplist))
1461 put_ol_stateid_locked(stp, &reaplist);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001462 }
Trond Myklebustd4f04892014-07-29 21:34:36 -04001463 spin_unlock(&clp->cl_lock);
Jeff Layton2c41beb2014-07-29 21:34:41 -04001464 free_ol_stateid_reaplist(&reaplist);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04001465 release_last_closed_stateid(oo);
Jeff Layton6b180f02014-07-29 21:34:26 -04001466 nfs4_put_stateowner(&oo->oo_owner);
J. Bruce Fieldsf1d110c2009-01-11 14:37:31 -05001467}
1468
Marc Eshel5282fd72009-04-03 08:27:52 +03001469static inline int
1470hash_sessionid(struct nfs4_sessionid *sessionid)
1471{
1472 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1473
1474 return sid->sequence % SESSION_HASH_SIZE;
1475}
1476
Mark Salter135dd002015-04-06 09:46:00 -04001477#ifdef CONFIG_SUNRPC_DEBUG
Marc Eshel5282fd72009-04-03 08:27:52 +03001478static inline void
1479dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1480{
1481 u32 *ptr = (u32 *)(&sessionid->data[0]);
1482 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1483}
Trond Myklebust8f199b82012-03-20 15:11:17 -04001484#else
1485static inline void
1486dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1487{
1488}
1489#endif
1490
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001491/*
1492 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1493 * won't be used for replay.
1494 */
1495void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1496{
1497 struct nfs4_stateowner *so = cstate->replay_owner;
1498
1499 if (nfserr == nfserr_replay_me)
1500 return;
1501
1502 if (!seqid_mutating_err(ntohl(nfserr))) {
Jeff Layton58fb12e2014-07-29 21:34:27 -04001503 nfsd4_cstate_clear_replay(cstate);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04001504 return;
1505 }
1506 if (!so)
1507 return;
1508 if (so->so_is_open_owner)
1509 release_last_closed_stateid(openowner(so));
1510 so->so_seqid++;
1511 return;
1512}
Marc Eshel5282fd72009-04-03 08:27:52 +03001513
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001514static void
1515gen_sessionid(struct nfsd4_session *ses)
1516{
1517 struct nfs4_client *clp = ses->se_client;
1518 struct nfsd4_sessionid *sid;
1519
1520 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1521 sid->clientid = clp->cl_clientid;
1522 sid->sequence = current_sessionid++;
1523 sid->reserved = 0;
1524}
1525
1526/*
Andy Adamsona649637c72009-08-28 08:45:01 -04001527 * The protocol defines ca_maxresponssize_cached to include the size of
1528 * the rpc header, but all we need to cache is the data starting after
1529 * the end of the initial SEQUENCE operation--the rest we regenerate
1530 * each time. Therefore we can advertise a ca_maxresponssize_cached
1531 * value that is the number of bytes in our cache plus a few additional
1532 * bytes. In order to stay on the safe side, and not promise more than
1533 * we can cache, those additional bytes must be the minimum possible: 24
1534 * bytes of rpc header (xid through accept state, with AUTH_NULL
1535 * verifier), 12 for the compound header (with zero-length tag), and 44
1536 * for the SEQUENCE op response:
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001537 */
Andy Adamsona649637c72009-08-28 08:45:01 -04001538#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1539
Andy Adamson557ce262009-08-28 08:45:04 -04001540static void
1541free_session_slots(struct nfsd4_session *ses)
1542{
1543 int i;
1544
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001545 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1546 free_svc_cred(&ses->se_slots[i]->sl_cred);
Andy Adamson557ce262009-08-28 08:45:04 -04001547 kfree(ses->se_slots[i]);
J. Bruce Fields53da6a52017-10-17 20:38:49 -04001548 }
Andy Adamson557ce262009-08-28 08:45:04 -04001549}
1550
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001551/*
1552 * We don't actually need to cache the rpc and session headers, so we
1553 * can allocate a little less for each slot:
1554 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001555static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001556{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001557 u32 size;
J. Bruce Fieldsefe0cb62009-10-24 20:52:16 -04001558
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001559 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1560 size = 0;
1561 else
1562 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1563 return size + sizeof(struct nfsd4_slot);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001564}
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001565
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001566/*
1567 * XXX: If we run out of reserved DRC memory we could (up to a point)
1568 * re-negotiate active sessions and reduce their slot usage to make
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001569 * room for new connections. For now we just fail the create session.
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001570 */
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001571static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001572{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001573 u32 slotsize = slot_bytes(ca);
1574 u32 num = ca->maxreqs;
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001575 unsigned long avail, total_avail;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001576
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001577 spin_lock(&nfsd_drc_lock);
J. Bruce Fieldsc54f24e2019-02-21 10:47:00 -05001578 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1579 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
J. Bruce Fieldsde766e52017-09-19 19:25:41 -04001580 /*
1581 * Never use more than a third of the remaining memory,
1582 * unless it's the only way to give this client a slot:
1583 */
Paul Menzel3b2d4dc2019-07-03 13:28:15 +02001584 avail = clamp_t(unsigned long, avail, slotsize, total_avail/3);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001585 num = min_t(int, num, avail / slotsize);
1586 nfsd_drc_mem_used += num * slotsize;
1587 spin_unlock(&nfsd_drc_lock);
1588
1589 return num;
1590}
1591
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001592static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001593{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001594 int slotsize = slot_bytes(ca);
1595
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001596 spin_lock(&nfsd_drc_lock);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001597 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001598 spin_unlock(&nfsd_drc_lock);
1599}
1600
Kinglong Mee60810e52014-01-01 00:35:47 +08001601static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1602 struct nfsd4_channel_attrs *battrs)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001603{
Kinglong Mee60810e52014-01-01 00:35:47 +08001604 int numslots = fattrs->maxreqs;
1605 int slotsize = slot_bytes(fattrs);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001606 struct nfsd4_session *new;
1607 int mem, i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001608
J. Bruce Fieldsc23753d2010-09-27 16:22:30 -04001609 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001610 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1611 mem = numslots * sizeof(struct nfsd4_slot *);
Andy Adamson557ce262009-08-28 08:45:04 -04001612
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001613 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001614 if (!new)
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001615 return NULL;
Andy Adamson557ce262009-08-28 08:45:04 -04001616 /* allocate each struct nfsd4_slot and data cache in one piece */
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001617 for (i = 0; i < numslots; i++) {
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001618 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001619 if (!new->se_slots[i])
Andy Adamson557ce262009-08-28 08:45:04 -04001620 goto out_free;
Andy Adamson557ce262009-08-28 08:45:04 -04001621 }
Kinglong Mee60810e52014-01-01 00:35:47 +08001622
1623 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1624 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1625
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001626 return new;
1627out_free:
1628 while (i--)
1629 kfree(new->se_slots[i]);
1630 kfree(new);
1631 return NULL;
1632}
1633
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001634static void free_conn(struct nfsd4_conn *c)
1635{
1636 svc_xprt_put(c->cn_xprt);
1637 kfree(c);
1638}
1639
1640static void nfsd4_conn_lost(struct svc_xpt_user *u)
1641{
1642 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1643 struct nfs4_client *clp = c->cn_session->se_client;
1644
1645 spin_lock(&clp->cl_lock);
1646 if (!list_empty(&c->cn_persession)) {
1647 list_del(&c->cn_persession);
1648 free_conn(c);
1649 }
J. Bruce Fieldseea49802010-11-18 08:34:12 -05001650 nfsd4_probe_callback(clp);
J. Bruce Fields2e4b7232013-03-08 09:30:43 -05001651 spin_unlock(&clp->cl_lock);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001652}
1653
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001654static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001655{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001656 struct nfsd4_conn *conn;
1657
1658 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1659 if (!conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001660 return NULL;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001661 svc_xprt_get(rqstp->rq_xprt);
1662 conn->cn_xprt = rqstp->rq_xprt;
J. Bruce Fieldsd29c3742010-06-15 17:34:11 -04001663 conn->cn_flags = flags;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001664 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1665 return conn;
1666}
1667
J. Bruce Fields328ead22010-09-29 16:11:06 -04001668static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1669{
1670 conn->cn_session = ses;
1671 list_add(&conn->cn_persession, &ses->se_conns);
1672}
1673
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001674static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1675{
1676 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001677
1678 spin_lock(&clp->cl_lock);
J. Bruce Fields328ead22010-09-29 16:11:06 -04001679 __nfsd4_hash_conn(conn, ses);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001680 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001681}
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001682
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001683static int nfsd4_register_conn(struct nfsd4_conn *conn)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001684{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001685 conn->cn_xpt_user.callback = nfsd4_conn_lost;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001686 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001687}
1688
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001689static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001690{
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001691 int ret;
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001692
J. Bruce Fieldsdb906812010-09-29 15:29:32 -04001693 nfsd4_hash_conn(conn, ses);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04001694 ret = nfsd4_register_conn(conn);
1695 if (ret)
1696 /* oops; xprt is already down: */
1697 nfsd4_conn_lost(&conn->cn_xpt_user);
J. Bruce Fields57a37142014-07-18 15:06:47 -04001698 /* We may have gained or lost a callback channel: */
1699 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001700}
1701
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001702static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001703{
1704 u32 dir = NFS4_CDFC4_FORE;
1705
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001706 if (cses->flags & SESSION4_BACK_CHAN)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001707 dir |= NFS4_CDFC4_BACK;
J. Bruce Fieldse1ff3712012-09-11 17:10:25 -04001708 return alloc_conn(rqstp, dir);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001709}
1710
1711/* must be called under client_lock */
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001712static void nfsd4_del_conns(struct nfsd4_session *s)
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001713{
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04001714 struct nfs4_client *clp = s->se_client;
1715 struct nfsd4_conn *c;
1716
1717 spin_lock(&clp->cl_lock);
1718 while (!list_empty(&s->se_conns)) {
1719 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1720 list_del_init(&c->cn_persession);
1721 spin_unlock(&clp->cl_lock);
1722
1723 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1724 free_conn(c);
1725
1726 spin_lock(&clp->cl_lock);
1727 }
1728 spin_unlock(&clp->cl_lock);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001729}
1730
J. Bruce Fields1377b692012-09-11 21:42:40 -04001731static void __free_session(struct nfsd4_session *ses)
1732{
J. Bruce Fields1377b692012-09-11 21:42:40 -04001733 free_session_slots(ses);
1734 kfree(ses);
1735}
1736
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001737static void free_session(struct nfsd4_session *ses)
Benny Halevy508dc6e2012-02-23 17:40:52 -08001738{
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001739 nfsd4_del_conns(ses);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04001740 nfsd4_put_drc_mem(&ses->se_fchannel);
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001741 __free_session(ses);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001742}
Andy Adamson557ce262009-08-28 08:45:04 -04001743
Fengguang Wu135ae822012-11-10 07:20:25 -05001744static 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 -04001745{
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001746 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001747 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsa827bcb2012-09-12 09:51:34 -04001748
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001749 new->se_client = clp;
1750 gen_sessionid(new);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001751
J. Bruce Fieldsc7662512010-06-06 18:12:14 -04001752 INIT_LIST_HEAD(&new->se_conns);
1753
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04001754 new->se_cb_seq_nr = 1;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001755 new->se_flags = cses->flags;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -04001756 new->se_cb_prog = cses->callback_prog;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001757 new->se_cb_sec = cses->cb_sec;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001758 atomic_set(&new->se_ref, 0);
J. Bruce Fields5b6feee2010-09-27 17:12:05 -04001759 idx = hash_sessionid(&new->se_sessionid);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001760 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001761 spin_lock(&clp->cl_lock);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001762 list_add(&new->se_perclnt, &clp->cl_sessions);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001763 spin_unlock(&clp->cl_lock);
Kinglong Mee60810e52014-01-01 00:35:47 +08001764
Chuck Leverb0d2e422014-08-22 15:10:59 -04001765 {
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001766 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001767 /*
1768 * This is a little silly; with sessions there's no real
1769 * use for the callback address. Use the peer address
1770 * as a reasonable default for now, but consider fixing
1771 * the rpc client not to require an address in the
1772 * future:
1773 */
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001774 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1775 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04001776 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001777}
1778
Benny Halevy9089f1b2010-05-12 00:12:26 +03001779/* caller must hold client_lock */
Marc Eshel5282fd72009-04-03 08:27:52 +03001780static struct nfsd4_session *
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001781__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
Marc Eshel5282fd72009-04-03 08:27:52 +03001782{
1783 struct nfsd4_session *elem;
1784 int idx;
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001785 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Marc Eshel5282fd72009-04-03 08:27:52 +03001786
Trond Myklebust0a880a22014-07-30 08:27:10 -04001787 lockdep_assert_held(&nn->client_lock);
1788
Marc Eshel5282fd72009-04-03 08:27:52 +03001789 dump_sessionid(__func__, sessionid);
1790 idx = hash_sessionid(sessionid);
Marc Eshel5282fd72009-04-03 08:27:52 +03001791 /* Search in the appropriate list */
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03001792 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
Marc Eshel5282fd72009-04-03 08:27:52 +03001793 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1794 NFS4_MAX_SESSIONID_LEN)) {
1795 return elem;
1796 }
1797 }
1798
1799 dprintk("%s: session not found\n", __func__);
1800 return NULL;
1801}
1802
Trond Myklebustd4e19e702014-06-30 11:48:42 -04001803static struct nfsd4_session *
1804find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1805 __be32 *ret)
1806{
1807 struct nfsd4_session *session;
1808 __be32 status = nfserr_badsession;
1809
1810 session = __find_in_sessionid_hashtbl(sessionid, net);
1811 if (!session)
1812 goto out;
1813 status = nfsd4_get_session_locked(session);
1814 if (status)
1815 session = NULL;
1816out:
1817 *ret = status;
1818 return session;
1819}
1820
Benny Halevy9089f1b2010-05-12 00:12:26 +03001821/* caller must hold client_lock */
Andy Adamson7116ed62009-04-03 08:27:43 +03001822static void
Marc Eshel5282fd72009-04-03 08:27:52 +03001823unhash_session(struct nfsd4_session *ses)
Andy Adamson7116ed62009-04-03 08:27:43 +03001824{
Trond Myklebust0a880a22014-07-30 08:27:10 -04001825 struct nfs4_client *clp = ses->se_client;
1826 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1827
1828 lockdep_assert_held(&nn->client_lock);
1829
Andy Adamson7116ed62009-04-03 08:27:43 +03001830 list_del(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001831 spin_lock(&ses->se_client->cl_lock);
Andy Adamson7116ed62009-04-03 08:27:43 +03001832 list_del(&ses->se_perclnt);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001833 spin_unlock(&ses->se_client->cl_lock);
Marc Eshel5282fd72009-04-03 08:27:52 +03001834}
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1837static int
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001838STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
J. Bruce Fieldsbbc7f332015-01-20 11:51:26 -05001840 /*
1841 * We're assuming the clid was not given out from a boot
1842 * precisely 2^32 (about 136 years) before this one. That seems
1843 * a safe assumption:
1844 */
1845 if (clid->cl_boot == (u32)nn->boot_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return 0;
Andy Adamson60adfc52009-04-03 08:28:50 +03001847 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04001848 clid->cl_boot, clid->cl_id, nn->boot_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return 1;
1850}
1851
1852/*
1853 * XXX Should we use a slab cache ?
1854 * This type of memory management is somewhat inefficient, but we use it
1855 * anyway since SETCLIENTID is not a common operation.
1856 */
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001857static struct nfs4_client *alloc_client(struct xdr_netobj name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
1859 struct nfs4_client *clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001860 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Jeff Layton9258a2d2018-03-16 09:47:22 -04001862 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05001863 if (clp == NULL)
1864 return NULL;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04001865 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001866 if (clp->cl_name.data == NULL)
1867 goto err_no_name;
Kees Cook6da2ec52018-06-12 13:55:00 -07001868 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1869 sizeof(struct list_head),
1870 GFP_KERNEL);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001871 if (!clp->cl_ownerstr_hashtbl)
1872 goto err_no_hashtbl;
1873 for (i = 0; i < OWNER_HASH_SIZE; i++)
1874 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
Trond Myklebust5694c932014-04-18 14:43:56 -04001875 INIT_LIST_HEAD(&clp->cl_sessions);
1876 idr_init(&clp->cl_stateids);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001877 atomic_set(&clp->cl_rpc_users, 0);
Trond Myklebust5694c932014-04-18 14:43:56 -04001878 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1879 INIT_LIST_HEAD(&clp->cl_idhash);
1880 INIT_LIST_HEAD(&clp->cl_openowners);
1881 INIT_LIST_HEAD(&clp->cl_delegations);
1882 INIT_LIST_HEAD(&clp->cl_lru);
Trond Myklebust5694c932014-04-18 14:43:56 -04001883 INIT_LIST_HEAD(&clp->cl_revoked);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001884#ifdef CONFIG_NFSD_PNFS
1885 INIT_LIST_HEAD(&clp->cl_lo_states);
1886#endif
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04001887 INIT_LIST_HEAD(&clp->async_copies);
1888 spin_lock_init(&clp->async_lock);
Trond Myklebust5694c932014-04-18 14:43:56 -04001889 spin_lock_init(&clp->cl_lock);
1890 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return clp;
Trond Myklebustd4f04892014-07-29 21:34:36 -04001892err_no_hashtbl:
1893 kfree(clp->cl_name.data);
1894err_no_name:
Jeff Layton9258a2d2018-03-16 09:47:22 -04001895 kmem_cache_free(client_slab, clp);
Trond Myklebustd4f04892014-07-29 21:34:36 -04001896 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001899static void __free_client(struct kref *k)
1900{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001901 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1902 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001903
1904 free_svc_cred(&clp->cl_cred);
1905 kfree(clp->cl_ownerstr_hashtbl);
1906 kfree(clp->cl_name.data);
J. Bruce Fields79123442019-06-05 12:42:05 -04001907 kfree(clp->cl_nii_domain.data);
1908 kfree(clp->cl_nii_name.data);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001909 idr_destroy(&clp->cl_stateids);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001910 kmem_cache_free(client_slab, clp);
1911}
1912
YueHaibing297e57a2019-07-08 15:29:33 +08001913static void drop_client(struct nfs4_client *clp)
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001914{
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001915 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001916}
1917
Trond Myklebust4dd86e152014-04-18 14:43:58 -04001918static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919free_client(struct nfs4_client *clp)
1920{
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001921 while (!list_empty(&clp->cl_sessions)) {
1922 struct nfsd4_session *ses;
1923 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1924 se_perclnt);
1925 list_del(&ses->se_perclnt);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04001926 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1927 free_session(ses);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001928 }
Trond Myklebust4cb57e32014-04-18 14:43:57 -04001929 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04001930 if (clp->cl_nfsd_dentry) {
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04001931 nfsd_client_rmdir(clp->cl_nfsd_dentry);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04001932 clp->cl_nfsd_dentry = NULL;
1933 wake_up_all(&expiry_wq);
1934 }
J. Bruce Fields59f8e912019-03-20 20:03:02 -04001935 drop_client(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
Benny Halevy84d38ac2010-05-12 00:13:16 +03001938/* must be called under the client_lock */
Trond Myklebust4beb3452014-07-30 08:27:02 -04001939static void
Benny Halevy84d38ac2010-05-12 00:13:16 +03001940unhash_client_locked(struct nfs4_client *clp)
1941{
Trond Myklebust4beb3452014-07-30 08:27:02 -04001942 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001943 struct nfsd4_session *ses;
1944
Trond Myklebust0a880a22014-07-30 08:27:10 -04001945 lockdep_assert_held(&nn->client_lock);
1946
Trond Myklebust4beb3452014-07-30 08:27:02 -04001947 /* Mark the client as expired! */
1948 clp->cl_time = 0;
1949 /* Make it invisible */
1950 if (!list_empty(&clp->cl_idhash)) {
1951 list_del_init(&clp->cl_idhash);
1952 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1953 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1954 else
1955 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1956 }
1957 list_del_init(&clp->cl_lru);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001958 spin_lock(&clp->cl_lock);
J. Bruce Fields792c95d2010-10-12 19:55:25 -04001959 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1960 list_del_init(&ses->se_hash);
J. Bruce Fields4c649372010-06-15 14:22:37 -04001961 spin_unlock(&clp->cl_lock);
Benny Halevy84d38ac2010-05-12 00:13:16 +03001962}
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964static void
Trond Myklebust4beb3452014-07-30 08:27:02 -04001965unhash_client(struct nfs4_client *clp)
1966{
1967 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1968
1969 spin_lock(&nn->client_lock);
1970 unhash_client_locked(clp);
1971 spin_unlock(&nn->client_lock);
1972}
1973
Jeff Layton97403d92014-07-30 08:27:12 -04001974static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1975{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04001976 if (atomic_read(&clp->cl_rpc_users))
Jeff Layton97403d92014-07-30 08:27:12 -04001977 return nfserr_jukebox;
1978 unhash_client_locked(clp);
1979 return nfs_ok;
1980}
1981
Trond Myklebust4beb3452014-07-30 08:27:02 -04001982static void
1983__destroy_client(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Jeff Layton68ef3bc2018-03-16 11:32:02 -04001985 int i;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001986 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 struct nfs4_delegation *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 struct list_head reaplist;
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04001991 spin_lock(&state_lock);
NeilBrownea1da632005-06-23 22:04:17 -07001992 while (!list_empty(&clp->cl_delegations)) {
1993 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04001994 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04001995 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
Benny Halevycdc975052014-05-30 09:09:30 -04001997 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 while (!list_empty(&reaplist)) {
1999 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04002000 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05002001 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
Jeff Layton2d4a5322014-07-25 07:34:21 -04002003 while (!list_empty(&clp->cl_revoked)) {
Andrew Elblec8764862015-02-25 17:46:27 -05002004 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
Jeff Layton2d4a5322014-07-25 07:34:21 -04002005 list_del_init(&dp->dl_recall_lru);
Trond Myklebust60116952014-07-29 21:34:06 -04002006 nfs4_put_stid(&dp->dl_stid);
Benny Halevy956c4fe2013-10-29 11:39:12 +02002007 }
NeilBrownea1da632005-06-23 22:04:17 -07002008 while (!list_empty(&clp->cl_openowners)) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002009 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
Kinglong Meeb5971af2014-08-22 10:18:43 -04002010 nfs4_get_stateowner(&oo->oo_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04002011 release_openowner(oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
Jeff Layton68ef3bc2018-03-16 11:32:02 -04002013 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2014 struct nfs4_stateowner *so, *tmp;
2015
2016 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2017 so_strhash) {
2018 /* Should be no openowners at this point */
2019 WARN_ON_ONCE(so->so_is_open_owner);
2020 remove_blocked_locks(lockowner(so));
2021 }
2022 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002023 nfsd4_return_all_client_layouts(clp);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002024 nfsd4_shutdown_copy(clp);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04002025 nfsd4_shutdown_callback(clp);
J. Bruce Fields2bf23872010-03-08 12:37:27 -05002026 if (clp->cl_cb_conn.cb_xprt)
2027 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
J. Bruce Fields221a6872013-04-01 22:23:49 -04002028 free_client(clp);
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002029 wake_up_all(&expiry_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Trond Myklebust4beb3452014-07-30 08:27:02 -04002032static void
2033destroy_client(struct nfs4_client *clp)
2034{
2035 unhash_client(clp);
2036 __destroy_client(clp);
2037}
2038
Scott Mayhew362063a2019-03-26 18:06:28 -04002039static void inc_reclaim_complete(struct nfs4_client *clp)
2040{
2041 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2042
2043 if (!nn->track_reclaim_completes)
2044 return;
2045 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2046 return;
2047 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2048 nn->reclaim_str_hashtbl_size) {
2049 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2050 clp->net->ns.inum);
2051 nfsd4_end_grace(nn);
2052 }
2053}
2054
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002055static void expire_client(struct nfs4_client *clp)
2056{
Trond Myklebust4beb3452014-07-30 08:27:02 -04002057 unhash_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002058 nfsd4_client_record_remove(clp);
Trond Myklebust4beb3452014-07-30 08:27:02 -04002059 __destroy_client(clp);
J. Bruce Fields0d22f682012-09-26 11:36:16 -04002060}
2061
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002062static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2063{
2064 memcpy(target->cl_verifier.data, source->data,
2065 sizeof(target->cl_verifier.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
J. Bruce Fields35bba9a2007-11-21 22:07:08 -05002068static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2069{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2071 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2072}
2073
J. Bruce Fields50043852015-11-20 15:58:37 -05002074static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2075{
NeilBrown2f10fdc2017-03-23 16:57:36 +08002076 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2077 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2078 GFP_KERNEL);
Chuck Lever9abdda52018-08-16 12:05:59 -04002079 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2080 if ((source->cr_principal && !target->cr_principal) ||
2081 (source->cr_raw_principal && !target->cr_raw_principal) ||
2082 (source->cr_targ_princ && !target->cr_targ_princ))
NeilBrown2f10fdc2017-03-23 16:57:36 +08002083 return -ENOMEM;
J. Bruce Fields50043852015-11-20 15:58:37 -05002084
J. Bruce Fieldsd5497fc2012-05-14 22:06:49 -04002085 target->cr_flavor = source->cr_flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 target->cr_uid = source->cr_uid;
2087 target->cr_gid = source->cr_gid;
2088 target->cr_group_info = source->cr_group_info;
2089 get_group_info(target->cr_group_info);
J. Bruce Fields0dc15312013-05-14 16:07:13 -04002090 target->cr_gss_mech = source->cr_gss_mech;
2091 if (source->cr_gss_mech)
2092 gss_mech_get(source->cr_gss_mech);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002096static int
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002097compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2098{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002099 if (o1->len < o2->len)
2100 return -1;
2101 if (o1->len > o2->len)
2102 return 1;
2103 return memcmp(o1->data, o2->data, o1->len);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002104}
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002107same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2108{
2109 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
2112static int
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002113same_clid(clientid_t *cl1, clientid_t *cl2)
2114{
2115 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002118static bool groups_equal(struct group_info *g1, struct group_info *g2)
2119{
2120 int i;
2121
2122 if (g1->ngroups != g2->ngroups)
2123 return false;
2124 for (i=0; i<g1->ngroups; i++)
Alexey Dobriyan81243ea2016-10-07 17:03:12 -07002125 if (!gid_eq(g1->gid[i], g2->gid[i]))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002126 return false;
2127 return true;
2128}
2129
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002130/*
2131 * RFC 3530 language requires clid_inuse be returned when the
2132 * "principal" associated with a requests differs from that previously
2133 * used. We use uid, gid's, and gss principal string as our best
2134 * approximation. We also don't want to allow non-gss use of a client
2135 * established using gss: in theory cr_principal should catch that
2136 * change, but in practice cr_principal can be null even in the gss case
2137 * since gssd doesn't always pass down a principal string.
2138 */
2139static bool is_gss_cred(struct svc_cred *cr)
2140{
2141 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2142 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2143}
2144
2145
Vivek Trivedi5559b502012-07-24 21:18:20 +05302146static bool
J. Bruce Fields599e0a22007-07-26 17:04:54 -04002147same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2148{
J. Bruce Fields68eb3502012-08-21 12:48:30 -04002149 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
Eric W. Biederman6fab8772013-02-02 06:53:11 -08002150 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2151 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002152 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2153 return false;
Chuck Lever9abdda52018-08-16 12:05:59 -04002154 /* XXX: check that cr_targ_princ fields match ? */
J. Bruce Fields8fbba962012-05-14 21:20:54 -04002155 if (cr1->cr_principal == cr2->cr_principal)
2156 return true;
2157 if (!cr1->cr_principal || !cr2->cr_principal)
2158 return false;
Vivek Trivedi5559b502012-07-24 21:18:20 +05302159 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
J. Bruce Fields57266a62013-04-13 14:27:29 -04002162static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2163{
2164 struct svc_cred *cr = &rqstp->rq_cred;
2165 u32 service;
2166
J. Bruce Fieldsc4720592013-08-07 11:41:49 -04002167 if (!cr->cr_gss_mech)
2168 return false;
J. Bruce Fields57266a62013-04-13 14:27:29 -04002169 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2170 return service == RPC_GSS_SVC_INTEGRITY ||
2171 service == RPC_GSS_SVC_PRIVACY;
2172}
2173
Andrew Elblededeb132016-06-15 12:52:08 -04002174bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
J. Bruce Fields57266a62013-04-13 14:27:29 -04002175{
2176 struct svc_cred *cr = &rqstp->rq_cred;
2177
2178 if (!cl->cl_mach_cred)
2179 return true;
2180 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2181 return false;
2182 if (!svc_rqst_integrity_protected(rqstp))
2183 return false;
J. Bruce Fields414ca012015-11-20 10:48:02 -05002184 if (cl->cl_cred.cr_raw_principal)
2185 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2186 cr->cr_raw_principal);
J. Bruce Fields57266a62013-04-13 14:27:29 -04002187 if (!cr->cr_principal)
2188 return false;
2189 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2190}
2191
Jeff Layton294ac322014-07-30 08:27:15 -04002192static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
J. Bruce Fieldsdeda2fa2007-11-19 20:31:04 -05002193{
Chuck Leverab4684d2012-03-02 17:13:50 -05002194 __be32 verf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Jeff Laytonf4199922014-06-17 07:44:11 -04002196 /*
2197 * This is opaque to client, so no need to byte-swap. Use
2198 * __force to keep sparse happy
2199 */
2200 verf[0] = (__force __be32)get_seconds();
Kinglong Mee19311aa82015-07-18 07:33:31 +08002201 verf[1] = (__force __be32)nn->clverifier_counter++;
Chuck Leverab4684d2012-03-02 17:13:50 -05002202 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
Jeff Layton294ac322014-07-30 08:27:15 -04002205static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2206{
2207 clp->cl_clientid.cl_boot = nn->boot_time;
2208 clp->cl_clientid.cl_id = nn->clientid_counter++;
2209 gen_confirm(clp, nn);
2210}
2211
Jeff Layton4770d722014-07-29 21:34:10 -04002212static struct nfs4_stid *
2213find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
J. Bruce Fields4581d142011-09-06 14:56:09 -04002214{
J. Bruce Fields3abdb602013-02-03 12:23:01 -05002215 struct nfs4_stid *ret;
2216
2217 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2218 if (!ret || !ret->sc_type)
2219 return NULL;
2220 return ret;
J. Bruce Fields4d71ab82011-09-06 16:48:57 -04002221}
2222
Jeff Layton4770d722014-07-29 21:34:10 -04002223static struct nfs4_stid *
Jeff Layton4770d722014-07-29 21:34:10 -04002224find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04002225{
2226 struct nfs4_stid *s;
2227
Jeff Layton4770d722014-07-29 21:34:10 -04002228 spin_lock(&cl->cl_lock);
2229 s = find_stateid_locked(cl, t);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002230 if (s != NULL) {
2231 if (typemask & s->sc_type)
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03002232 refcount_inc(&s->sc_count);
Trond Myklebust2d3f9662014-07-29 21:34:25 -04002233 else
2234 s = NULL;
2235 }
Jeff Layton4770d722014-07-29 21:34:10 -04002236 spin_unlock(&cl->cl_lock);
2237 return s;
J. Bruce Fields4581d142011-09-06 14:56:09 -04002238}
2239
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002240static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2241{
2242 struct nfsdfs_client *nc;
2243 nc = get_nfsdfs_client(inode);
2244 if (!nc)
2245 return NULL;
2246 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2247}
2248
J. Bruce Fields169319f2019-06-19 12:39:46 -04002249static void seq_quote_mem(struct seq_file *m, char *data, int len)
2250{
2251 seq_printf(m, "\"");
2252 seq_escape_mem_ascii(m, data, len);
2253 seq_printf(m, "\"");
2254}
2255
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002256static int client_info_show(struct seq_file *m, void *v)
2257{
2258 struct inode *inode = m->private;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002259 struct nfs4_client *clp;
2260 u64 clid;
2261
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002262 clp = get_nfsdfs_clp(inode);
2263 if (!clp)
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002264 return -ENXIO;
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002265 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2266 seq_printf(m, "clientid: 0x%llx\n", clid);
J. Bruce Fields169319f2019-06-19 12:39:46 -04002267 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2268 seq_printf(m, "name: ");
2269 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2270 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
J. Bruce Fields79123442019-06-05 12:42:05 -04002271 if (clp->cl_nii_domain.data) {
2272 seq_printf(m, "Implementation domain: ");
2273 seq_quote_mem(m, clp->cl_nii_domain.data,
2274 clp->cl_nii_domain.len);
2275 seq_printf(m, "\nImplementation name: ");
2276 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2277 seq_printf(m, "\nImplementation time: [%ld, %ld]\n",
2278 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2279 }
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002280 drop_client(clp);
2281
2282 return 0;
2283}
2284
2285static int client_info_open(struct inode *inode, struct file *file)
2286{
2287 return single_open(file, client_info_show, inode);
2288}
2289
2290static const struct file_operations client_info_fops = {
2291 .open = client_info_open,
2292 .read = seq_read,
2293 .llseek = seq_lseek,
2294 .release = single_release,
2295};
2296
J. Bruce Fields78599c42019-04-22 15:26:23 -04002297static void *states_start(struct seq_file *s, loff_t *pos)
2298 __acquires(&clp->cl_lock)
2299{
2300 struct nfs4_client *clp = s->private;
2301 unsigned long id = *pos;
2302 void *ret;
2303
2304 spin_lock(&clp->cl_lock);
2305 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2306 *pos = id;
2307 return ret;
2308}
2309
2310static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2311{
2312 struct nfs4_client *clp = s->private;
2313 unsigned long id = *pos;
2314 void *ret;
2315
2316 id = *pos;
2317 id++;
2318 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2319 *pos = id;
2320 return ret;
2321}
2322
2323static void states_stop(struct seq_file *s, void *v)
2324 __releases(&clp->cl_lock)
2325{
2326 struct nfs4_client *clp = s->private;
2327
2328 spin_unlock(&clp->cl_lock);
2329}
2330
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002331static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002332{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002333 struct inode *inode = f->nf_inode;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002334
2335 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2336 MAJOR(inode->i_sb->s_dev),
2337 MINOR(inode->i_sb->s_dev),
2338 inode->i_ino);
2339}
2340
2341static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2342{
2343 seq_printf(s, "owner: ");
2344 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2345}
2346
2347static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2348{
2349 struct nfs4_ol_stateid *ols;
2350 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002351 struct nfsd_file *file;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002352 struct nfs4_stateowner *oo;
2353 unsigned int access, deny;
2354
2355 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2356 return 0; /* XXX: or SEQ_SKIP? */
2357 ols = openlockstateid(st);
2358 oo = ols->st_stateowner;
2359 nf = st->sc_file;
2360 file = find_any_file(nf);
2361
2362 seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid);
2363
2364 access = bmap_to_share_mode(ols->st_access_bmap);
2365 deny = bmap_to_share_mode(ols->st_deny_bmap);
2366
2367 seq_printf(s, "access: \%s\%s, ",
2368 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2369 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2370 seq_printf(s, "deny: \%s\%s, ",
2371 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2372 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2373
2374 nfs4_show_superblock(s, file);
2375 seq_printf(s, ", ");
2376 nfs4_show_owner(s, oo);
2377 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002378 nfsd_file_put(file);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002379
2380 return 0;
2381}
2382
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002383static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2384{
2385 struct nfs4_ol_stateid *ols;
2386 struct nfs4_file *nf;
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002387 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002388 struct nfs4_stateowner *oo;
2389
2390 ols = openlockstateid(st);
2391 oo = ols->st_stateowner;
2392 nf = st->sc_file;
2393 file = find_any_file(nf);
2394
2395 seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid);
2396
2397 /*
2398 * Note: a lock stateid isn't really the same thing as a lock,
2399 * it's the locking state held by one owner on a file, and there
2400 * may be multiple (or no) lock ranges associated with it.
2401 * (Same for the matter is true of open stateids.)
2402 */
2403
2404 nfs4_show_superblock(s, file);
2405 /* XXX: open stateid? */
2406 seq_printf(s, ", ");
2407 nfs4_show_owner(s, oo);
2408 seq_printf(s, " }\n");
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04002409 nfsd_file_put(file);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002410
2411 return 0;
2412}
2413
2414static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2415{
2416 struct nfs4_delegation *ds;
2417 struct nfs4_file *nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002418 struct nfsd_file *file;
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002419
2420 ds = delegstateid(st);
2421 nf = st->sc_file;
2422 file = nf->fi_deleg_file;
2423
2424 seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid);
2425
2426 /* Kinda dead code as long as we only support read delegs: */
2427 seq_printf(s, "access: %s, ",
2428 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2429
2430 /* XXX: lease time, whether it's being recalled. */
2431
2432 nfs4_show_superblock(s, file);
2433 seq_printf(s, " }\n");
2434
2435 return 0;
2436}
2437
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002438static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2439{
2440 struct nfs4_layout_stateid *ls;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04002441 struct nfsd_file *file;
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002442
2443 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2444 file = ls->ls_file;
2445
2446 seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid);
2447
2448 /* XXX: What else would be useful? */
2449
2450 nfs4_show_superblock(s, file);
2451 seq_printf(s, " }\n");
2452
2453 return 0;
2454}
2455
J. Bruce Fields78599c42019-04-22 15:26:23 -04002456static int states_show(struct seq_file *s, void *v)
2457{
2458 struct nfs4_stid *st = v;
2459
2460 switch (st->sc_type) {
2461 case NFS4_OPEN_STID:
2462 return nfs4_show_open(s, st);
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002463 case NFS4_LOCK_STID:
2464 return nfs4_show_lock(s, st);
2465 case NFS4_DELEG_STID:
2466 return nfs4_show_deleg(s, st);
J. Bruce Fields0c4b62b2019-05-14 15:52:57 -04002467 case NFS4_LAYOUT_STID:
2468 return nfs4_show_layout(s, st);
J. Bruce Fields78599c42019-04-22 15:26:23 -04002469 default:
2470 return 0; /* XXX: or SEQ_SKIP? */
2471 }
J. Bruce Fields16d36e02019-05-10 15:27:50 -04002472 /* XXX: copy stateids? */
J. Bruce Fields78599c42019-04-22 15:26:23 -04002473}
2474
2475static struct seq_operations states_seq_ops = {
2476 .start = states_start,
2477 .next = states_next,
2478 .stop = states_stop,
2479 .show = states_show
2480};
2481
2482static int client_states_open(struct inode *inode, struct file *file)
2483{
J. Bruce Fields78599c42019-04-22 15:26:23 -04002484 struct seq_file *s;
2485 struct nfs4_client *clp;
2486 int ret;
2487
J. Bruce Fieldsa204f252019-06-19 12:54:45 -04002488 clp = get_nfsdfs_clp(inode);
2489 if (!clp)
J. Bruce Fields78599c42019-04-22 15:26:23 -04002490 return -ENXIO;
J. Bruce Fields78599c42019-04-22 15:26:23 -04002491
2492 ret = seq_open(file, &states_seq_ops);
2493 if (ret)
2494 return ret;
2495 s = file->private_data;
2496 s->private = clp;
2497 return 0;
2498}
2499
2500static int client_opens_release(struct inode *inode, struct file *file)
2501{
2502 struct seq_file *m = file->private_data;
2503 struct nfs4_client *clp = m->private;
2504
2505 /* XXX: alternatively, we could get/drop in seq start/stop */
2506 drop_client(clp);
2507 return 0;
2508}
2509
2510static const struct file_operations client_states_fops = {
2511 .open = client_states_open,
2512 .read = seq_read,
2513 .llseek = seq_lseek,
2514 .release = client_opens_release,
2515};
2516
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002517/*
2518 * Normally we refuse to destroy clients that are in use, but here the
2519 * administrator is telling us to just do it. We also want to wait
2520 * so the caller has a guarantee that the client's locks are gone by
2521 * the time the write returns:
2522 */
YueHaibing297e57a2019-07-08 15:29:33 +08002523static void force_expire_client(struct nfs4_client *clp)
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002524{
2525 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2526 bool already_expired;
2527
2528 spin_lock(&clp->cl_lock);
2529 clp->cl_time = 0;
2530 spin_unlock(&clp->cl_lock);
2531
2532 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2533 spin_lock(&nn->client_lock);
2534 already_expired = list_empty(&clp->cl_lru);
2535 if (!already_expired)
2536 unhash_client_locked(clp);
2537 spin_unlock(&nn->client_lock);
2538
2539 if (!already_expired)
2540 expire_client(clp);
2541 else
2542 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2543}
2544
2545static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2546 size_t size, loff_t *pos)
2547{
2548 char *data;
2549 struct nfs4_client *clp;
2550
2551 data = simple_transaction_get(file, buf, size);
2552 if (IS_ERR(data))
2553 return PTR_ERR(data);
2554 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2555 return -EINVAL;
2556 clp = get_nfsdfs_clp(file_inode(file));
2557 if (!clp)
2558 return -ENXIO;
2559 force_expire_client(clp);
2560 drop_client(clp);
2561 return 7;
2562}
2563
2564static const struct file_operations client_ctl_fops = {
2565 .write = client_ctl_write,
2566 .release = simple_transaction_release,
2567};
2568
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002569static const struct tree_descr client_files[] = {
2570 [0] = {"info", &client_info_fops, S_IRUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002571 [1] = {"states", &client_states_fops, S_IRUSR},
J. Bruce Fields89c905b2019-06-19 12:43:11 -04002572 [2] = {"ctl", &client_ctl_fops, S_IRUSR|S_IWUSR},
J. Bruce Fields78599c42019-04-22 15:26:23 -04002573 [3] = {""},
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002574};
2575
Jeff Layton2216d442012-11-12 15:00:57 -05002576static struct nfs4_client *create_client(struct xdr_netobj name,
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002577 struct svc_rqst *rqstp, nfs4_verifier *verf)
2578{
2579 struct nfs4_client *clp;
2580 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002581 int ret;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002582 struct net *net = SVC_NET(rqstp);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002583 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002584
2585 clp = alloc_client(name);
2586 if (clp == NULL)
2587 return NULL;
2588
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002589 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2590 if (ret) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002591 free_client(clp);
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -04002592 return NULL;
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002593 }
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002594 gen_clid(clp, nn);
2595 kref_init(&clp->cl_nfsdfs.cl_ref);
Christoph Hellwig0162ac22014-09-24 12:19:19 +02002596 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
Benny Halevy07cd4902010-05-12 00:13:41 +03002597 clp->cl_time = get_seconds();
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002598 clear_bit(0, &clp->cl_cb_slot_busy);
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002599 copy_verf(clp, verf);
J. Bruce Fields3bade242019-05-14 21:38:11 -04002600 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
J. Bruce Fieldsedd76782010-06-14 22:26:31 -04002601 clp->cl_cb_session = NULL;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002602 clp->net = net;
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002603 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
J. Bruce Fields97ad4032019-04-09 15:56:57 -04002604 clp->cl_clientid.cl_id - nn->clientid_base,
2605 client_files);
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04002606 if (!clp->cl_nfsd_dentry) {
2607 free_client(clp);
2608 return NULL;
2609 }
Ricardo Labiagab09333c2009-09-10 12:27:34 +03002610 return clp;
2611}
2612
NeilBrownfd39ca92005-06-23 22:04:03 -07002613static void
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002614add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2615{
2616 struct rb_node **new = &(root->rb_node), *parent = NULL;
2617 struct nfs4_client *clp;
2618
2619 while (*new) {
2620 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2621 parent = *new;
2622
2623 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2624 new = &((*new)->rb_left);
2625 else
2626 new = &((*new)->rb_right);
2627 }
2628
2629 rb_link_node(&new_clp->cl_namenode, parent, new);
2630 rb_insert_color(&new_clp->cl_namenode, root);
2631}
2632
2633static struct nfs4_client *
2634find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2635{
Rasmus Villemoesef17af22014-12-05 16:40:07 +01002636 int cmp;
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002637 struct rb_node *node = root->rb_node;
2638 struct nfs4_client *clp;
2639
2640 while (node) {
2641 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2642 cmp = compare_blob(&clp->cl_name, name);
2643 if (cmp > 0)
2644 node = node->rb_left;
2645 else if (cmp < 0)
2646 node = node->rb_right;
2647 else
2648 return clp;
2649 }
2650 return NULL;
2651}
2652
2653static void
2654add_to_unconfirmed(struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
2656 unsigned int idhashval;
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002657 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Trond Myklebust0a880a22014-07-30 08:27:10 -04002659 lockdep_assert_held(&nn->client_lock);
2660
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002661 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002662 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002664 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002665 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
NeilBrownfd39ca92005-06-23 22:04:03 -07002668static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669move_to_confirmed(struct nfs4_client *clp)
2670{
2671 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002672 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Trond Myklebust0a880a22014-07-30 08:27:10 -04002674 lockdep_assert_held(&nn->client_lock);
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03002677 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002678 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002679 add_clp_to_name_tree(clp, &nn->conf_name_tree);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05002680 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002681 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682}
2683
2684static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002685find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
2687 struct nfs4_client *clp;
2688 unsigned int idhashval = clientid_hashval(clid->cl_id);
2689
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002690 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002691 if (same_clid(&clp->cl_clientid, clid)) {
J. Bruce Fieldsd15c0772012-09-13 16:19:31 -04002692 if ((bool)clp->cl_minorversion != sessions)
2693 return NULL;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002694 renew_client_locked(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 return clp;
J. Bruce Fieldsa50d2ad2011-10-12 16:24:27 -04002696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
2698 return NULL;
2699}
2700
2701static struct nfs4_client *
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002702find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2703{
2704 struct list_head *tbl = nn->conf_id_hashtbl;
2705
Trond Myklebust0a880a22014-07-30 08:27:10 -04002706 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002707 return find_client_in_id_table(tbl, clid, sessions);
2708}
2709
2710static struct nfs4_client *
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03002711find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002713 struct list_head *tbl = nn->unconf_id_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Trond Myklebust0a880a22014-07-30 08:27:10 -04002715 lockdep_assert_held(&nn->client_lock);
J. Bruce Fieldsbfa85e832013-03-14 18:24:52 -04002716 return find_client_in_id_table(tbl, clid, sessions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002719static bool clp_used_exchangeid(struct nfs4_client *clp)
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002720{
J. Bruce Fields6e5f15c2010-11-24 17:17:34 -05002721 return clp->cl_exchange_flags != 0;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05002722}
Andy Adamsona1bcecd2009-04-03 08:28:05 +03002723
NeilBrown28ce6052005-06-23 22:03:56 -07002724static struct nfs4_client *
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002725find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002726{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002727 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03002728 return find_clp_in_name_tree(name, &nn->conf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002729}
2730
2731static struct nfs4_client *
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002732find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
NeilBrown28ce6052005-06-23 22:03:56 -07002733{
Trond Myklebust0a880a22014-07-30 08:27:10 -04002734 lockdep_assert_held(&nn->client_lock);
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03002735 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
NeilBrown28ce6052005-06-23 22:03:56 -07002736}
2737
NeilBrownfd39ca92005-06-23 22:04:03 -07002738static void
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002739gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
J. Bruce Fields07263f12010-05-31 19:09:40 -04002741 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09002742 struct sockaddr *sa = svc_addr(rqstp);
2743 u32 scopeid = rpc_get_scope_id(sa);
Jeff Layton7077ecb2009-08-14 12:57:58 -04002744 unsigned short expected_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Jeff Layton7077ecb2009-08-14 12:57:58 -04002746 /* Currently, we only support tcp and tcp6 for the callback channel */
2747 if (se->se_callback_netid_len == 3 &&
2748 !memcmp(se->se_callback_netid_val, "tcp", 3))
2749 expected_family = AF_INET;
2750 else if (se->se_callback_netid_len == 4 &&
2751 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2752 expected_family = AF_INET6;
2753 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 goto out_err;
2755
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002756 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002757 se->se_callback_addr_len,
J. Bruce Fields07263f12010-05-31 19:09:40 -04002758 (struct sockaddr *)&conn->cb_addr,
2759 sizeof(conn->cb_addr));
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002760
J. Bruce Fields07263f12010-05-31 19:09:40 -04002761 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 goto out_err;
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -04002763
J. Bruce Fields07263f12010-05-31 19:09:40 -04002764 if (conn->cb_addr.ss_family == AF_INET6)
2765 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
Jeff Laytonfbf46652009-08-14 12:57:59 -04002766
J. Bruce Fields07263f12010-05-31 19:09:40 -04002767 conn->cb_prog = se->se_callback_prog;
2768 conn->cb_ident = se->se_callback_ident;
Mi Jinlong849a1cf2011-08-30 17:18:41 +08002769 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return;
2771out_err:
J. Bruce Fields07263f12010-05-31 19:09:40 -04002772 conn->cb_addr.ss_family = AF_UNSPEC;
2773 conn->cb_addrlen = 0;
Rasmus Villemoes4ab495b2017-02-24 01:15:55 +01002774 dprintk("NFSD: this client (clientid %08x/%08x) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 "will not receive delegations\n",
2776 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return;
2779}
2780
Andy Adamson074fe892009-04-03 08:28:15 +03002781/*
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04002782 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
Andy Adamson074fe892009-04-03 08:28:15 +03002783 */
Trond Myklebustb6076642014-06-30 11:48:35 -04002784static void
Andy Adamson074fe892009-04-03 08:28:15 +03002785nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2786{
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002787 struct xdr_buf *buf = resp->xdr.buf;
Andy Adamson557ce262009-08-28 08:45:04 -04002788 struct nfsd4_slot *slot = resp->cstate.slot;
2789 unsigned int base;
Andy Adamson074fe892009-04-03 08:28:15 +03002790
Andy Adamson557ce262009-08-28 08:45:04 -04002791 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002792
J. Bruce Fields085def32017-10-18 16:17:18 -04002793 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
Andy Adamson557ce262009-08-28 08:45:04 -04002794 slot->sl_opcnt = resp->opcnt;
2795 slot->sl_status = resp->cstate.status;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04002796 free_svc_cred(&slot->sl_cred);
2797 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
Andy Adamsonbf864a32009-04-03 08:28:35 +03002798
J. Bruce Fields085def32017-10-18 16:17:18 -04002799 if (!nfsd4_cache_this(resp)) {
2800 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
Andy Adamsonbf864a32009-04-03 08:28:35 +03002801 return;
2802 }
J. Bruce Fields085def32017-10-18 16:17:18 -04002803 slot->sl_flags |= NFSD4_SLOT_CACHED;
2804
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002805 base = resp->cstate.data_offset;
2806 slot->sl_datalen = buf->len - base;
2807 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
Dan Carpenterd3f03402015-11-21 13:28:50 +03002808 WARN(1, "%s: sessions DRC could not cache compound\n",
2809 __func__);
Andy Adamson557ce262009-08-28 08:45:04 -04002810 return;
Andy Adamson074fe892009-04-03 08:28:15 +03002811}
2812
2813/*
Andy Adamsonabfabf82009-07-23 19:02:18 -04002814 * Encode the replay sequence operation from the slot values.
2815 * If cachethis is FALSE encode the uncached rep error on the next
2816 * operation which sets resp->p and increments resp->opcnt for
2817 * nfs4svc_encode_compoundres.
2818 *
Andy Adamson074fe892009-04-03 08:28:15 +03002819 */
Andy Adamsonabfabf82009-07-23 19:02:18 -04002820static __be32
2821nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2822 struct nfsd4_compoundres *resp)
Andy Adamson074fe892009-04-03 08:28:15 +03002823{
Andy Adamsonabfabf82009-07-23 19:02:18 -04002824 struct nfsd4_op *op;
2825 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson074fe892009-04-03 08:28:15 +03002826
Andy Adamsonabfabf82009-07-23 19:02:18 -04002827 /* Encode the replayed sequence operation */
2828 op = &args->ops[resp->opcnt - 1];
2829 nfsd4_encode_operation(resp, op);
2830
J. Bruce Fields085def32017-10-18 16:17:18 -04002831 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2832 return op->status;
2833 if (args->opcnt == 1) {
2834 /*
2835 * The original operation wasn't a solo sequence--we
2836 * always cache those--so this retry must not match the
2837 * original:
2838 */
2839 op->status = nfserr_seq_false_retry;
2840 } else {
Andy Adamsonabfabf82009-07-23 19:02:18 -04002841 op = &args->ops[resp->opcnt++];
2842 op->status = nfserr_retry_uncached_rep;
2843 nfsd4_encode_operation(resp, op);
Andy Adamson074fe892009-04-03 08:28:15 +03002844 }
Andy Adamsonabfabf82009-07-23 19:02:18 -04002845 return op->status;
Andy Adamson074fe892009-04-03 08:28:15 +03002846}
2847
2848/*
Andy Adamson557ce262009-08-28 08:45:04 -04002849 * The sequence operation is not cached because we can use the slot and
2850 * session values.
Andy Adamson074fe892009-04-03 08:28:15 +03002851 */
J. Bruce Fields3ca2eb92014-03-21 12:04:21 -04002852static __be32
Andy Adamsonbf864a32009-04-03 08:28:35 +03002853nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2854 struct nfsd4_sequence *seq)
Andy Adamson074fe892009-04-03 08:28:15 +03002855{
Andy Adamson557ce262009-08-28 08:45:04 -04002856 struct nfsd4_slot *slot = resp->cstate.slot;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002857 struct xdr_stream *xdr = &resp->xdr;
2858 __be32 *p;
Andy Adamson074fe892009-04-03 08:28:15 +03002859 __be32 status;
2860
Andy Adamson557ce262009-08-28 08:45:04 -04002861 dprintk("--> %s slot %p\n", __func__, slot);
Andy Adamson074fe892009-04-03 08:28:15 +03002862
Andy Adamsonabfabf82009-07-23 19:02:18 -04002863 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
J. Bruce Fields0da7b192014-03-21 11:43:34 -04002864 if (status)
Andy Adamsonabfabf82009-07-23 19:02:18 -04002865 return status;
Andy Adamson074fe892009-04-03 08:28:15 +03002866
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002867 p = xdr_reserve_space(xdr, slot->sl_datalen);
2868 if (!p) {
2869 WARN_ON_ONCE(1);
2870 return nfserr_serverfault;
2871 }
2872 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2873 xdr_commit_encode(xdr);
Andy Adamson074fe892009-04-03 08:28:15 +03002874
Andy Adamson557ce262009-08-28 08:45:04 -04002875 resp->opcnt = slot->sl_opcnt;
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04002876 return slot->sl_status;
Andy Adamson074fe892009-04-03 08:28:15 +03002877}
2878
Andy Adamson0733d212009-04-03 08:28:01 +03002879/*
2880 * Set the exchange_id flags returned by the server.
2881 */
2882static void
2883nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2884{
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002885#ifdef CONFIG_NFSD_PNFS
2886 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
2887#else
Andy Adamson0733d212009-04-03 08:28:01 +03002888 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002889#endif
Andy Adamson0733d212009-04-03 08:28:01 +03002890
2891 /* Referrals are supported, Migration is not. */
2892 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2893
2894 /* set the wire flags to return to client. */
2895 clid->flags = new->cl_exchange_flags;
2896}
2897
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002898static bool client_has_openowners(struct nfs4_client *clp)
2899{
2900 struct nfs4_openowner *oo;
2901
2902 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
2903 if (!list_empty(&oo->oo_owner.so_stateids))
2904 return true;
2905 }
2906 return false;
2907}
2908
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002909static bool client_has_state(struct nfs4_client *clp)
2910{
J. Bruce Fields4eaea132015-10-15 16:11:01 -04002911 return client_has_openowners(clp)
Kinglong Mee47e970b2015-07-21 13:09:17 +08002912#ifdef CONFIG_NFSD_PNFS
2913 || !list_empty(&clp->cl_lo_states)
2914#endif
J. Bruce Fields6eccece2012-05-29 16:37:44 -04002915 || !list_empty(&clp->cl_delegations)
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04002916 || !list_empty(&clp->cl_sessions)
2917 || !list_empty(&clp->async_copies);
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04002918}
2919
J. Bruce Fields79123442019-06-05 12:42:05 -04002920static __be32 copy_impl_id(struct nfs4_client *clp,
2921 struct nfsd4_exchange_id *exid)
2922{
2923 if (!exid->nii_domain.data)
2924 return 0;
2925 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
2926 if (!clp->cl_nii_domain.data)
2927 return nfserr_jukebox;
2928 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
2929 if (!clp->cl_nii_name.data)
2930 return nfserr_jukebox;
2931 clp->cl_nii_time.tv_sec = exid->nii_time.tv_sec;
2932 clp->cl_nii_time.tv_nsec = exid->nii_time.tv_nsec;
2933 return 0;
2934}
2935
Al Virob37ad282006-10-19 23:28:59 -07002936__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02002937nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2938 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03002939{
Christoph Hellwigeb698532017-05-08 20:58:35 +02002940 struct nfsd4_exchange_id *exid = &u->exchange_id;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04002941 struct nfs4_client *conf, *new;
2942 struct nfs4_client *unconf = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04002943 __be32 status;
Jeff Layton363168b2009-08-14 12:57:56 -04002944 char addr_str[INET6_ADDRSTRLEN];
Andy Adamson0733d212009-04-03 08:28:01 +03002945 nfs4_verifier verf = exid->verifier;
Jeff Layton363168b2009-08-14 12:57:56 -04002946 struct sockaddr *sa = svc_addr(rqstp);
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04002947 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03002948 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03002949
Jeff Layton363168b2009-08-14 12:57:56 -04002950 rpc_ntop(sa, addr_str, sizeof(addr_str));
Andy Adamson0733d212009-04-03 08:28:01 +03002951 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
Jeff Layton363168b2009-08-14 12:57:56 -04002952 "ip_addr=%s flags %x, spa_how %d\n",
Andy Adamson0733d212009-04-03 08:28:01 +03002953 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
Jeff Layton363168b2009-08-14 12:57:56 -04002954 addr_str, exid->flags, exid->spa_how);
Andy Adamson0733d212009-04-03 08:28:01 +03002955
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04002956 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
Andy Adamson0733d212009-04-03 08:28:01 +03002957 return nfserr_inval;
2958
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002959 new = create_client(exid->clname, rqstp, &verf);
2960 if (new == NULL)
2961 return nfserr_jukebox;
J. Bruce Fields79123442019-06-05 12:42:05 -04002962 status = copy_impl_id(new, exid);
2963 if (status)
2964 goto out_nolock;
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002965
Andy Adamson0733d212009-04-03 08:28:01 +03002966 switch (exid->spa_how) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04002967 case SP4_MACH_CRED:
Andrew Elbleed941642016-06-15 12:52:09 -04002968 exid->spo_must_enforce[0] = 0;
2969 exid->spo_must_enforce[1] = (
2970 1 << (OP_BIND_CONN_TO_SESSION - 32) |
2971 1 << (OP_EXCHANGE_ID - 32) |
2972 1 << (OP_CREATE_SESSION - 32) |
2973 1 << (OP_DESTROY_SESSION - 32) |
2974 1 << (OP_DESTROY_CLIENTID - 32));
2975
2976 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
2977 1 << (OP_OPEN_DOWNGRADE) |
2978 1 << (OP_LOCKU) |
2979 1 << (OP_DELEGRETURN));
2980
2981 exid->spo_must_allow[1] &= (
2982 1 << (OP_TEST_STATEID - 32) |
2983 1 << (OP_FREE_STATEID - 32));
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002984 if (!svc_rqst_integrity_protected(rqstp)) {
2985 status = nfserr_inval;
2986 goto out_nolock;
2987 }
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002988 /*
2989 * Sometimes userspace doesn't give us a principal.
2990 * Which is a bug, really. Anyway, we can't enforce
2991 * MACH_CRED in that case, better to give up now:
2992 */
J. Bruce Fields414ca012015-11-20 10:48:02 -05002993 if (!new->cl_cred.cr_principal &&
2994 !new->cl_cred.cr_raw_principal) {
J. Bruce Fields920dd9b2015-11-20 16:42:40 -05002995 status = nfserr_serverfault;
2996 goto out_nolock;
2997 }
J. Bruce Fields50c7b942015-11-23 15:39:12 -07002998 new->cl_mach_cred = true;
Andy Adamson0733d212009-04-03 08:28:01 +03002999 case SP4_NONE:
3000 break;
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05003001 default: /* checked by xdr code */
3002 WARN_ON_ONCE(1);
Gustavo A. R. Silva0a4c9262019-01-23 02:48:28 -06003003 /* fall through */
Andy Adamson0733d212009-04-03 08:28:01 +03003004 case SP4_SSV:
Kinglong Mee8edf4b02016-02-26 22:36:42 +08003005 status = nfserr_encr_alg_unsupp;
3006 goto out_nolock;
Andy Adamson0733d212009-04-03 08:28:01 +03003007 }
3008
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003009 /* Cases below refer to rfc 5661 section 18.35.4: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003010 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003011 conf = find_confirmed_client_by_name(&exid->clname, nn);
Andy Adamson0733d212009-04-03 08:28:01 +03003012 if (conf) {
J. Bruce Fields83e08fd2012-05-14 09:08:10 -04003013 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3014 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3015
J. Bruce Fields136e6582012-05-12 20:37:23 -04003016 if (update) {
3017 if (!clp_used_exchangeid(conf)) { /* buggy client */
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003018 status = nfserr_inval;
Andy Adamson0733d212009-04-03 08:28:01 +03003019 goto out;
3020 }
Andrew Elblededeb132016-06-15 12:52:08 -04003021 if (!nfsd4_mach_creds_match(conf, rqstp)) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003022 status = nfserr_wrong_cred;
3023 goto out;
3024 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003025 if (!creds_match) { /* case 9 */
Andy Adamson0733d212009-04-03 08:28:01 +03003026 status = nfserr_perm;
3027 goto out;
3028 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003029 if (!verfs_match) { /* case 8 */
Andy Adamson0733d212009-04-03 08:28:01 +03003030 status = nfserr_not_same;
3031 goto out;
3032 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003033 /* case 6 */
3034 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003035 goto out_copy;
Andy Adamson0733d212009-04-03 08:28:01 +03003036 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003037 if (!creds_match) { /* case 3 */
J. Bruce Fields631fc9e2012-05-14 15:57:23 -04003038 if (client_has_state(conf)) {
3039 status = nfserr_clid_inuse;
3040 goto out;
3041 }
Andy Adamson0733d212009-04-03 08:28:01 +03003042 goto out_new;
3043 }
J. Bruce Fields136e6582012-05-12 20:37:23 -04003044 if (verfs_match) { /* case 2 */
J. Bruce Fields0f1ba0e2012-05-25 21:24:40 -04003045 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003046 goto out_copy;
3047 }
3048 /* case 5, client reboot */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003049 conf = NULL;
J. Bruce Fields136e6582012-05-12 20:37:23 -04003050 goto out_new;
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003051 }
3052
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003053 if (update) { /* case 7 */
Mike Sager6ddbbbf2009-06-16 04:20:47 +03003054 status = nfserr_noent;
3055 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03003056 }
3057
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003058 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003059 if (unconf) /* case 4, possible retry or client restart */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003060 unhash_client_locked(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003061
J. Bruce Fields2dbb2692012-05-14 09:47:11 -04003062 /* case 1 (normal case) */
Andy Adamson0733d212009-04-03 08:28:01 +03003063out_new:
Jeff Laytonfd699b82014-07-30 08:27:14 -04003064 if (conf) {
3065 status = mark_client_expired_locked(conf);
3066 if (status)
3067 goto out;
3068 }
J. Bruce Fields4f540e22013-05-07 11:57:52 -04003069 new->cl_minorversion = cstate->minorversion;
Andrew Elbleed941642016-06-15 12:52:09 -04003070 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3071 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
Andy Adamson0733d212009-04-03 08:28:01 +03003072
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003073 add_to_unconfirmed(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003074 swap(new, conf);
Andy Adamson0733d212009-04-03 08:28:01 +03003075out_copy:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003076 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3077 exid->clientid.cl_id = conf->cl_clientid.cl_id;
Andy Adamson0733d212009-04-03 08:28:01 +03003078
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003079 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3080 nfsd4_set_ex_flags(conf, exid);
Andy Adamson0733d212009-04-03 08:28:01 +03003081
3082 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003083 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
Andy Adamson0733d212009-04-03 08:28:01 +03003084 status = nfs_ok;
3085
3086out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003087 spin_unlock(&nn->client_lock);
J. Bruce Fields50c7b942015-11-23 15:39:12 -07003088out_nolock:
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003089 if (new)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003090 expire_client(new);
3091 if (unconf)
3092 expire_client(unconf);
Andy Adamson0733d212009-04-03 08:28:01 +03003093 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003094}
3095
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003096static __be32
Andy Adamson88e588d2009-07-23 19:02:15 -04003097check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003098{
Andy Adamson88e588d2009-07-23 19:02:15 -04003099 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3100 slot_seqid);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003101
3102 /* The slot is in use, and no response has been sent. */
Andy Adamson88e588d2009-07-23 19:02:15 -04003103 if (slot_inuse) {
3104 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003105 return nfserr_jukebox;
3106 else
3107 return nfserr_seq_misordered;
3108 }
J. Bruce Fieldsf6d82482012-02-13 16:13:41 -05003109 /* Note unsigned 32-bit arithmetic handles wraparound: */
Andy Adamson88e588d2009-07-23 19:02:15 -04003110 if (likely(seqid == slot_seqid + 1))
Benny Halevyb85d4c02009-04-03 08:28:08 +03003111 return nfs_ok;
Andy Adamson88e588d2009-07-23 19:02:15 -04003112 if (seqid == slot_seqid)
Benny Halevyb85d4c02009-04-03 08:28:08 +03003113 return nfserr_replay_cache;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003114 return nfserr_seq_misordered;
3115}
3116
Andy Adamson49557cc2009-07-23 19:02:16 -04003117/*
3118 * Cache the create session result into the create session single DRC
3119 * slot cache by saving the xdr structure. sl_seqid has been set.
3120 * Do this for solo or embedded create session operations.
3121 */
3122static void
3123nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003124 struct nfsd4_clid_slot *slot, __be32 nfserr)
Andy Adamson49557cc2009-07-23 19:02:16 -04003125{
3126 slot->sl_status = nfserr;
3127 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3128}
3129
3130static __be32
3131nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3132 struct nfsd4_clid_slot *slot)
3133{
3134 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3135 return slot->sl_status;
3136}
3137
Mi Jinlong1b74c252011-07-14 14:50:17 +08003138#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3139 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3140 1 + /* MIN tag is length with zero, only length */ \
3141 3 + /* version, opcount, opcode */ \
3142 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3143 /* seqid, slotID, slotID, cache */ \
3144 4 ) * sizeof(__be32))
3145
3146#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3147 2 + /* verifier: AUTH_NULL, length 0 */\
3148 1 + /* status */ \
3149 1 + /* MIN tag is length with zero, only length */ \
3150 3 + /* opcount, opcode, opstatus*/ \
3151 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3152 /* seqid, slotID, slotID, slotID, status */ \
3153 5 ) * sizeof(__be32))
3154
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003155static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
Mi Jinlong1b74c252011-07-14 14:50:17 +08003156{
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003157 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3158
J. Bruce Fields373cd402013-04-08 15:42:12 -04003159 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3160 return nfserr_toosmall;
3161 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3162 return nfserr_toosmall;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003163 ca->headerpadsz = 0;
3164 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3165 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3166 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3167 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3168 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3169 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3170 /*
3171 * Note decreasing slot size below client's request may make it
3172 * difficult for client to function correctly, whereas
3173 * decreasing the number of slots will (just?) affect
3174 * performance. When short on memory we therefore prefer to
3175 * decrease number of slots instead of their size. Clients that
3176 * request larger slots than they need will get poor results:
3177 */
3178 ca->maxreqs = nfsd4_get_drc_mem(ca);
3179 if (!ca->maxreqs)
3180 return nfserr_jukebox;
3181
J. Bruce Fields373cd402013-04-08 15:42:12 -04003182 return nfs_ok;
Mi Jinlong1b74c252011-07-14 14:50:17 +08003183}
3184
Chuck Lever45006322016-03-01 13:06:02 -05003185/*
3186 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3187 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3188 */
3189#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3190 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3191
3192#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3193 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3194
Kinglong Mee8a891632013-12-23 18:11:02 +08003195#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003196 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003197#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
Chuck Lever45006322016-03-01 13:06:02 -05003198 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3199 sizeof(__be32))
Kinglong Mee8a891632013-12-23 18:11:02 +08003200
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003201static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3202{
3203 ca->headerpadsz = 0;
3204
Kinglong Mee8a891632013-12-23 18:11:02 +08003205 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003206 return nfserr_toosmall;
Kinglong Mee8a891632013-12-23 18:11:02 +08003207 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003208 return nfserr_toosmall;
3209 ca->maxresp_cached = 0;
3210 if (ca->maxops < 2)
3211 return nfserr_toosmall;
3212
3213 return nfs_ok;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003214}
3215
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003216static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3217{
3218 switch (cbs->flavor) {
3219 case RPC_AUTH_NULL:
3220 case RPC_AUTH_UNIX:
3221 return nfs_ok;
3222 default:
3223 /*
3224 * GSS case: the spec doesn't allow us to return this
3225 * error. But it also doesn't allow us not to support
3226 * GSS.
3227 * I'd rather this fail hard than return some error the
3228 * client might think it can already handle:
3229 */
3230 return nfserr_encr_alg_unsupp;
3231 }
3232}
3233
Andy Adamson069b6ad2009-04-03 08:27:58 +03003234__be32
3235nfsd4_create_session(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003236 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003237{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003238 struct nfsd4_create_session *cr_ses = &u->create_session;
Jeff Layton363168b2009-08-14 12:57:56 -04003239 struct sockaddr *sa = svc_addr(rqstp);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003240 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003241 struct nfs4_client *old = NULL;
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003242 struct nfsd4_session *new;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003243 struct nfsd4_conn *conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003244 struct nfsd4_clid_slot *cs_slot = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003245 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003246 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003247
Mi Jinlonga62573d2011-03-23 17:57:07 +08003248 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3249 return nfserr_inval;
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003250 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3251 if (status)
3252 return status;
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003253 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
J. Bruce Fields373cd402013-04-08 15:42:12 -04003254 if (status)
3255 return status;
J. Bruce Fields06b332a2013-04-09 11:34:36 -04003256 status = check_backchannel_attrs(&cr_ses->back_channel);
3257 if (status)
Kinglong Meef403e452013-12-23 17:31:21 +08003258 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003259 status = nfserr_jukebox;
Kinglong Mee60810e52014-01-01 00:35:47 +08003260 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003261 if (!new)
3262 goto out_release_drc_mem;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003263 conn = alloc_conn_from_crses(rqstp, cr_ses);
3264 if (!conn)
3265 goto out_free_session;
Mi Jinlonga62573d2011-03-23 17:57:07 +08003266
Jeff Laytond20c11d2014-07-30 08:27:07 -04003267 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003268 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003269 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003270 WARN_ON_ONCE(conf && unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003271
3272 if (conf) {
J. Bruce Fields57266a62013-04-13 14:27:29 -04003273 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003274 if (!nfsd4_mach_creds_match(conf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003275 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003276 cs_slot = &conf->cl_cs_slot;
3277 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Kinglong Meef5e22bb2015-07-13 17:32:34 +08003278 if (status) {
3279 if (status == nfserr_replay_cache)
3280 status = nfsd4_replay_create_session(cr_ses, cs_slot);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003281 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003282 }
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003283 } else if (unconf) {
3284 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
Jeff Layton363168b2009-08-14 12:57:56 -04003285 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003286 status = nfserr_clid_inuse;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003287 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003288 }
J. Bruce Fields57266a62013-04-13 14:27:29 -04003289 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003290 if (!nfsd4_mach_creds_match(unconf, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003291 goto out_free_conn;
Andy Adamson49557cc2009-07-23 19:02:16 -04003292 cs_slot = &unconf->cl_cs_slot;
3293 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
Andy Adamson38eb76a2009-04-03 08:28:32 +03003294 if (status) {
3295 /* an unconfirmed replay returns misordered */
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003296 status = nfserr_seq_misordered;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003297 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003298 }
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003299 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003300 if (old) {
Jeff Laytond20c11d2014-07-30 08:27:07 -04003301 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003302 if (status) {
3303 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003304 goto out_free_conn;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003305 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003306 }
J. Bruce Fields8f9d3d32012-09-12 14:41:31 -04003307 move_to_confirmed(unconf);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003308 conf = unconf;
3309 } else {
3310 status = nfserr_stale_clientid;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003311 goto out_free_conn;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003312 }
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003313 status = nfs_ok;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003314 /* Persistent sessions are not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003315 cr_ses->flags &= ~SESSION4_PERSIST;
Chuck Lever4ce85c82016-03-01 13:05:27 -05003316 /* Upshifting from TCP to RDMA is not supported */
J. Bruce Fields408b79b2010-04-15 15:11:09 -04003317 cr_ses->flags &= ~SESSION4_RDMA;
3318
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003319 init_session(rqstp, new, conf, cr_ses);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003320 nfsd4_get_session_locked(new);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003321
J. Bruce Fieldsac7c46f22010-06-14 19:01:57 -04003322 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003323 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields86c3e162010-10-02 17:04:00 -04003324 cs_slot->sl_seqid++;
Andy Adamson49557cc2009-07-23 19:02:16 -04003325 cr_ses->seqid = cs_slot->sl_seqid;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003326
Jeff Laytond20c11d2014-07-30 08:27:07 -04003327 /* cache solo and embedded create sessions under the client_lock */
Andy Adamson49557cc2009-07-23 19:02:16 -04003328 nfsd4_cache_create_session(cr_ses, cs_slot, status);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003329 spin_unlock(&nn->client_lock);
3330 /* init connection and backchannel */
3331 nfsd4_init_conn(rqstp, conn, new);
3332 nfsd4_put_session(new);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003333 if (old)
3334 expire_client(old);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003335 return status;
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003336out_free_conn:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003337 spin_unlock(&nn->client_lock);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003338 free_conn(conn);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003339 if (old)
3340 expire_client(old);
J. Bruce Fields81f0b2a2012-09-12 11:04:33 -04003341out_free_session:
3342 __free_session(new);
J. Bruce Fields55c760c2013-04-08 16:44:14 -04003343out_release_drc_mem:
3344 nfsd4_put_drc_mem(&cr_ses->fore_channel);
J. Bruce Fields1ca50792013-03-14 18:12:03 -04003345 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003346}
3347
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003348static __be32 nfsd4_map_bcts_dir(u32 *dir)
3349{
3350 switch (*dir) {
3351 case NFS4_CDFC4_FORE:
3352 case NFS4_CDFC4_BACK:
3353 return nfs_ok;
3354 case NFS4_CDFC4_FORE_OR_BOTH:
3355 case NFS4_CDFC4_BACK_OR_BOTH:
3356 *dir = NFS4_CDFC4_BOTH;
3357 return nfs_ok;
3358 };
3359 return nfserr_inval;
3360}
3361
Christoph Hellwigeb698532017-05-08 20:58:35 +02003362__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3363 struct nfsd4_compound_state *cstate,
3364 union nfsd4_op_u *u)
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003365{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003366 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003367 struct nfsd4_session *session = cstate->session;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003368 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003369 __be32 status;
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003370
J. Bruce Fieldsb78724b2013-05-15 17:34:39 -04003371 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3372 if (status)
3373 return status;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003374 spin_lock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003375 session->se_cb_prog = bc->bc_cb_program;
3376 session->se_cb_sec = bc->bc_cb_sec;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003377 spin_unlock(&nn->client_lock);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04003378
3379 nfsd4_probe_callback(session->se_client);
3380
3381 return nfs_ok;
3382}
3383
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003384__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3385 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003386 union nfsd4_op_u *u)
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003387{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003388 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003389 __be32 status;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003390 struct nfsd4_conn *conn;
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003391 struct nfsd4_session *session;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003392 struct net *net = SVC_NET(rqstp);
3393 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003394
3395 if (!nfsd4_last_compound_op(rqstp))
3396 return nfserr_not_only_op;
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003397 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003398 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003399 spin_unlock(&nn->client_lock);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003400 if (!session)
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003401 goto out_no_session;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003402 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003403 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
J. Bruce Fields57266a62013-04-13 14:27:29 -04003404 goto out;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003405 status = nfsd4_map_bcts_dir(&bcts->dir);
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003406 if (status)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003407 goto out;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003408 conn = alloc_conn(rqstp, bcts->dir);
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003409 status = nfserr_jukebox;
J. Bruce Fields3ba63672012-09-11 15:37:40 -04003410 if (!conn)
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003411 goto out;
3412 nfsd4_init_conn(rqstp, conn, session);
3413 status = nfs_ok;
3414out:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003415 nfsd4_put_session(session);
3416out_no_session:
J. Bruce Fields4f6e6c12013-03-18 17:31:30 -04003417 return status;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003418}
3419
J. Bruce Fields665d5072018-04-11 16:53:36 -04003420static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003421{
J. Bruce Fields665d5072018-04-11 16:53:36 -04003422 if (!cstate->session)
Fengguang Wu51d87bc2018-03-22 13:37:20 +08003423 return false;
J. Bruce Fields665d5072018-04-11 16:53:36 -04003424 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
J. Bruce Fields5d4cec22010-05-01 12:56:06 -04003425}
3426
Andy Adamson069b6ad2009-04-03 08:27:58 +03003427__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003428nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3429 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003430{
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003431 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003432 struct nfsd4_session *ses;
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003433 __be32 status;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003434 int ref_held_by_me = 0;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003435 struct net *net = SVC_NET(r);
3436 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003437
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003438 status = nfserr_not_only_op;
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003439 if (nfsd4_compound_in_session(cstate, sessionid)) {
J. Bruce Fields57716352010-04-21 12:27:19 -04003440 if (!nfsd4_last_compound_op(r))
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003441 goto out;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003442 ref_held_by_me++;
J. Bruce Fields57716352010-04-21 12:27:19 -04003443 }
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003444 dump_sessionid(__func__, sessionid);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003445 spin_lock(&nn->client_lock);
J. Bruce Fieldsca0552f2018-04-11 17:01:53 -04003446 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003447 if (!ses)
3448 goto out_client_lock;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003449 status = nfserr_wrong_cred;
Andrew Elblededeb132016-06-15 12:52:08 -04003450 if (!nfsd4_mach_creds_match(ses->se_client, r))
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003451 goto out_put_session;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003452 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003453 if (status)
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003454 goto out_put_session;
Benny Halevye10e0cf2009-04-03 08:28:38 +03003455 unhash_session(ses);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003456 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003457
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -05003458 nfsd4_probe_callback_sync(ses->se_client);
J. Bruce Fields19cf5c02010-06-06 18:37:16 -04003459
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003460 spin_lock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003461 status = nfs_ok;
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003462out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003463 nfsd4_put_session_locked(ses);
J. Bruce Fieldsabcdff02013-03-14 19:55:33 -04003464out_client_lock:
3465 spin_unlock(&nn->client_lock);
Benny Halevye10e0cf2009-04-03 08:28:38 +03003466out:
Benny Halevye10e0cf2009-04-03 08:28:38 +03003467 return status;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003468}
3469
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003470static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003471{
3472 struct nfsd4_conn *c;
3473
3474 list_for_each_entry(c, &s->se_conns, cn_persession) {
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003475 if (c->cn_xprt == xpt) {
J. Bruce Fields328ead22010-09-29 16:11:06 -04003476 return c;
3477 }
3478 }
3479 return NULL;
3480}
3481
J. Bruce Fields57266a62013-04-13 14:27:29 -04003482static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
J. Bruce Fields328ead22010-09-29 16:11:06 -04003483{
3484 struct nfs4_client *clp = ses->se_client;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003485 struct nfsd4_conn *c;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003486 __be32 status = nfs_ok;
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003487 int ret;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003488
3489 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003490 c = __nfsd4_find_conn(new->cn_xprt, ses);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003491 if (c)
3492 goto out_free;
3493 status = nfserr_conn_not_bound_to_session;
3494 if (clp->cl_mach_cred)
3495 goto out_free;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003496 __nfsd4_hash_conn(new, ses);
3497 spin_unlock(&clp->cl_lock);
J. Bruce Fields21b75b02010-10-26 10:07:17 -04003498 ret = nfsd4_register_conn(new);
3499 if (ret)
3500 /* oops; xprt is already down: */
3501 nfsd4_conn_lost(&new->cn_xpt_user);
J. Bruce Fields57266a62013-04-13 14:27:29 -04003502 return nfs_ok;
3503out_free:
3504 spin_unlock(&clp->cl_lock);
3505 free_conn(new);
3506 return status;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003507}
3508
Mi Jinlong868b89c2011-04-27 09:09:58 +08003509static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3510{
3511 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3512
3513 return args->opcnt > session->se_fchannel.maxops;
3514}
3515
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003516static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3517 struct nfsd4_session *session)
3518{
3519 struct xdr_buf *xb = &rqstp->rq_arg;
3520
3521 return xb->len > session->se_fchannel.maxreq_sz;
3522}
3523
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003524static bool replay_matches_cache(struct svc_rqst *rqstp,
3525 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3526{
3527 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3528
3529 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3530 (bool)seq->cachethis)
3531 return false;
3532 /*
3533 * If there's an error than the reply can have fewer ops than
3534 * the call. But if we cached a reply with *more* ops than the
3535 * call you're sending us now, then this new call is clearly not
3536 * really a replay of the old one:
3537 */
3538 if (slot->sl_opcnt < argp->opcnt)
3539 return false;
3540 /* This is the only check explicitly called by spec: */
3541 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3542 return false;
3543 /*
3544 * There may be more comparisons we could actually do, but the
3545 * spec doesn't require us to catch every case where the calls
3546 * don't match (that would require caching the call as well as
3547 * the reply), so we don't bother.
3548 */
3549 return true;
3550}
3551
Andy Adamson069b6ad2009-04-03 08:27:58 +03003552__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003553nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3554 union nfsd4_op_u *u)
Andy Adamson069b6ad2009-04-03 08:27:58 +03003555{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003556 struct nfsd4_sequence *seq = &u->sequence;
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003557 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003558 struct xdr_stream *xdr = &resp->xdr;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003559 struct nfsd4_session *session;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003560 struct nfs4_client *clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003561 struct nfsd4_slot *slot;
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003562 struct nfsd4_conn *conn;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003563 __be32 status;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003564 int buflen;
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003565 struct net *net = SVC_NET(rqstp);
3566 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003567
Andy Adamsonf9bb94c42009-04-03 08:28:12 +03003568 if (resp->opcnt != 1)
3569 return nfserr_sequence_pos;
3570
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003571 /*
3572 * Will be either used or freed by nfsd4_sequence_check_conn
3573 * below.
3574 */
3575 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3576 if (!conn)
3577 return nfserr_jukebox;
3578
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003579 spin_lock(&nn->client_lock);
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003580 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003581 if (!session)
J. Bruce Fields221a6872013-04-01 22:23:49 -04003582 goto out_no_session;
3583 clp = session->se_client;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003584
Mi Jinlong868b89c2011-04-27 09:09:58 +08003585 status = nfserr_too_many_ops;
3586 if (nfsd4_session_too_many_ops(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003587 goto out_put_session;
Mi Jinlong868b89c2011-04-27 09:09:58 +08003588
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003589 status = nfserr_req_too_big;
3590 if (nfsd4_request_too_big(rqstp, session))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003591 goto out_put_session;
Mi Jinlongae82a8d2011-07-14 14:56:02 +08003592
Benny Halevyb85d4c02009-04-03 08:28:08 +03003593 status = nfserr_badslot;
Alexandros Batsakis6c18ba92009-06-16 04:19:13 +03003594 if (seq->slotid >= session->se_fchannel.maxreqs)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003595 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003596
Andy Adamson557ce262009-08-28 08:45:04 -04003597 slot = session->se_slots[seq->slotid];
Benny Halevyb85d4c02009-04-03 08:28:08 +03003598 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3599
Andy Adamsona8dfdae2009-08-28 08:45:02 -04003600 /* We do not negotiate the number of slots yet, so set the
3601 * maxslots to the session maxreqs which is used to encode
3602 * sr_highest_slotid and the sr_target_slot id to maxslots */
3603 seq->maxslots = session->se_fchannel.maxreqs;
3604
J. Bruce Fields73e79482012-02-13 16:39:00 -05003605 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3606 slot->sl_flags & NFSD4_SLOT_INUSE);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003607 if (status == nfserr_replay_cache) {
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003608 status = nfserr_seq_misordered;
3609 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003610 goto out_put_session;
J. Bruce Fields53da6a52017-10-17 20:38:49 -04003611 status = nfserr_seq_false_retry;
3612 if (!replay_matches_cache(rqstp, seq, slot))
3613 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003614 cstate->slot = slot;
3615 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003616 cstate->clp = clp;
Andy Adamsonda3846a2009-04-03 08:28:22 +03003617 /* Return the cached reply status and set cstate->status
Andy Adamson557ce262009-08-28 08:45:04 -04003618 * for nfsd4_proc_compound processing */
Andy Adamsonbf864a32009-04-03 08:28:35 +03003619 status = nfsd4_replay_cache_entry(resp, seq);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003620 cstate->status = nfserr_replay_cache;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003621 goto out;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003622 }
3623 if (status)
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003624 goto out_put_session;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003625
J. Bruce Fields57266a62013-04-13 14:27:29 -04003626 status = nfsd4_sequence_check_conn(conn, session);
J. Bruce Fieldsa663bdd2010-10-21 17:17:31 -04003627 conn = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003628 if (status)
3629 goto out_put_session;
J. Bruce Fields328ead22010-09-29 16:11:06 -04003630
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003631 buflen = (seq->cachethis) ?
3632 session->se_fchannel.maxresp_cached :
3633 session->se_fchannel.maxresp_sz;
3634 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3635 nfserr_rep_too_big;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04003636 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003637 goto out_put_session;
J. Bruce Fields32aaa622014-03-20 20:47:41 -04003638 svc_reserve(rqstp, buflen);
J. Bruce Fields47ee5292014-03-12 21:39:35 -04003639
3640 status = nfs_ok;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003641 /* Success! bump slot seqid */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003642 slot->sl_seqid = seq->seqid;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003643 slot->sl_flags |= NFSD4_SLOT_INUSE;
J. Bruce Fields73e79482012-02-13 16:39:00 -05003644 if (seq->cachethis)
3645 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
J. Bruce Fieldsbf5c43c2012-02-13 16:56:19 -05003646 else
3647 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003648
3649 cstate->slot = slot;
3650 cstate->session = session;
Jeff Layton4b24ca72014-06-30 11:48:44 -04003651 cstate->clp = clp;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003652
Benny Halevyb85d4c02009-04-03 08:28:08 +03003653out:
J. Bruce Fields221a6872013-04-01 22:23:49 -04003654 switch (clp->cl_cb_state) {
3655 case NFSD4_CB_DOWN:
3656 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3657 break;
3658 case NFSD4_CB_FAULT:
3659 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3660 break;
3661 default:
3662 seq->status_flags = 0;
Benny Halevyaaf84eb2009-08-20 03:21:56 +03003663 }
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04003664 if (!list_empty(&clp->cl_revoked))
3665 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003666out_no_session:
Kinglong Mee3f42d2c2014-03-24 11:56:59 +08003667 if (conn)
3668 free_conn(conn);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03003669 spin_unlock(&nn->client_lock);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003670 return status;
J. Bruce Fields66b2b9b2013-03-19 12:05:39 -04003671out_put_session:
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003672 nfsd4_put_session_locked(session);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003673 goto out_no_session;
Andy Adamson069b6ad2009-04-03 08:27:58 +03003674}
3675
Trond Myklebustb6076642014-06-30 11:48:35 -04003676void
3677nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3678{
3679 struct nfsd4_compound_state *cs = &resp->cstate;
3680
3681 if (nfsd4_has_session(cs)) {
Trond Myklebustb6076642014-06-30 11:48:35 -04003682 if (cs->status != nfserr_replay_cache) {
3683 nfsd4_store_cache_entry(resp);
3684 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3685 }
Trond Myklebustd4e19e702014-06-30 11:48:42 -04003686 /* Drop session reference that was taken in nfsd4_sequence() */
Trond Myklebustb6076642014-06-30 11:48:35 -04003687 nfsd4_put_session(cs->session);
Jeff Layton4b24ca72014-06-30 11:48:44 -04003688 } else if (cs->clp)
3689 put_client_renew(cs->clp);
Trond Myklebustb6076642014-06-30 11:48:35 -04003690}
3691
Mi Jinlong345c2842011-10-20 17:51:39 +08003692__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003693nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3694 struct nfsd4_compound_state *cstate,
3695 union nfsd4_op_u *u)
Mi Jinlong345c2842011-10-20 17:51:39 +08003696{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003697 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003698 struct nfs4_client *conf, *unconf;
3699 struct nfs4_client *clp = NULL;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003700 __be32 status = 0;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003701 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Mi Jinlong345c2842011-10-20 17:51:39 +08003702
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003703 spin_lock(&nn->client_lock);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003704 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003705 conf = find_confirmed_client(&dc->clientid, true, nn);
J. Bruce Fields78389042013-03-12 10:12:37 -04003706 WARN_ON_ONCE(conf && unconf);
Mi Jinlong345c2842011-10-20 17:51:39 +08003707
3708 if (conf) {
J. Bruce Fieldsc0293b02013-03-14 18:20:01 -04003709 if (client_has_state(conf)) {
Mi Jinlong345c2842011-10-20 17:51:39 +08003710 status = nfserr_clientid_busy;
3711 goto out;
3712 }
Jeff Laytonfd699b82014-07-30 08:27:14 -04003713 status = mark_client_expired_locked(conf);
3714 if (status)
3715 goto out;
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003716 clp = conf;
Mi Jinlong345c2842011-10-20 17:51:39 +08003717 } else if (unconf)
3718 clp = unconf;
3719 else {
3720 status = nfserr_stale_clientid;
3721 goto out;
3722 }
Andrew Elblededeb132016-06-15 12:52:08 -04003723 if (!nfsd4_mach_creds_match(clp, rqstp)) {
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003724 clp = NULL;
J. Bruce Fields57266a62013-04-13 14:27:29 -04003725 status = nfserr_wrong_cred;
3726 goto out;
3727 }
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003728 unhash_client_locked(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003729out:
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003730 spin_unlock(&nn->client_lock);
Trond Myklebust6b10ad12014-07-30 08:27:08 -04003731 if (clp)
3732 expire_client(clp);
Mi Jinlong345c2842011-10-20 17:51:39 +08003733 return status;
3734}
3735
Andy Adamson069b6ad2009-04-03 08:27:58 +03003736__be32
Christoph Hellwigeb698532017-05-08 20:58:35 +02003737nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3738 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003739{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003740 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003741 __be32 status = 0;
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003742
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003743 if (rc->rca_one_fs) {
3744 if (!cstate->current_fh.fh_dentry)
3745 return nfserr_nofilehandle;
3746 /*
3747 * We don't take advantage of the rca_one_fs case.
3748 * That's OK, it's optional, we can safely ignore it.
3749 */
Christophe JAILLETd28c4422016-07-02 08:24:32 +02003750 return nfs_ok;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003751 }
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003752
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003753 status = nfserr_complete_already;
Jeff Laytona52d7262012-03-21 09:52:02 -04003754 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3755 &cstate->session->se_client->cl_flags))
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003756 goto out;
3757
3758 status = nfserr_stale_clientid;
3759 if (is_client_expired(cstate->session->se_client))
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003760 /*
3761 * The following error isn't really legal.
3762 * But we only get here if the client just explicitly
3763 * destroyed the client. Surely it no longer cares what
3764 * error it gets back on an operation for the dead
3765 * client.
3766 */
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003767 goto out;
3768
3769 status = nfs_ok;
Jeff Layton2a4317c2012-03-21 16:42:43 -04003770 nfsd4_client_record_create(cstate->session->se_client);
Scott Mayhew362063a2019-03-26 18:06:28 -04003771 inc_reclaim_complete(cstate->session->se_client);
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003772out:
Mi Jinlongbcecf1c2011-04-27 09:14:30 +08003773 return status;
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04003774}
3775
3776__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003777nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003778 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003780 struct nfsd4_setclientid *setclid = &u->setclientid;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04003781 struct xdr_netobj clname = setclid->se_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 nfs4_verifier clverifier = setclid->se_verf;
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003783 struct nfs4_client *conf, *new;
3784 struct nfs4_client *unconf = NULL;
Al Virob37ad282006-10-19 23:28:59 -07003785 __be32 status;
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +03003786 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3787
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003788 new = create_client(clname, rqstp, &clverifier);
3789 if (new == NULL)
3790 return nfserr_jukebox;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003791 /* Cases below refer to rfc 3530 section 14.2.33: */
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003792 spin_lock(&nn->client_lock);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03003793 conf = find_confirmed_client_by_name(&clname, nn);
J. Bruce Fields2b634822015-10-15 15:33:23 -04003794 if (conf && client_has_state(conf)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003795 /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 status = nfserr_clid_inuse;
J. Bruce Fieldse203d502010-11-24 17:30:54 -05003797 if (clp_used_exchangeid(conf))
3798 goto out;
J. Bruce Fields026722c2009-03-18 15:06:26 -04003799 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
Jeff Layton363168b2009-08-14 12:57:56 -04003800 char addr_str[INET6_ADDRSTRLEN];
3801 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
3802 sizeof(addr_str));
3803 dprintk("NFSD: setclientid: string in use by client "
3804 "at %s\n", addr_str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 goto out;
3806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03003808 unconf = find_unconfirmed_client_by_name(&clname, nn);
J. Bruce Fields8f930712012-05-18 22:06:41 -04003809 if (unconf)
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003810 unhash_client_locked(unconf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003811 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
J. Bruce Fields63db4632012-05-18 21:54:19 -04003812 /* case 1: probable callback update */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 copy_clid(new, conf);
Kinglong Mee41eb1672015-07-13 17:29:41 +08003814 gen_confirm(new, nn);
3815 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
J. Bruce Fieldse8a79fb2019-03-22 11:11:06 -04003816 ;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -04003817 new->cl_minorversion = 0;
Takuma Umeya6f3d7722010-12-15 14:09:01 +09003818 gen_callback(new, setclid, rqstp);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05003819 add_to_unconfirmed(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3821 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3822 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003823 new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 status = nfs_ok;
3825out:
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003826 spin_unlock(&nn->client_lock);
Trond Myklebust5cc40fd2014-07-30 08:27:05 -04003827 if (new)
3828 free_client(new);
Trond Myklebust3dbacee2014-07-30 08:27:06 -04003829 if (unconf)
3830 expire_client(unconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 return status;
3832}
3833
3834
Al Virob37ad282006-10-19 23:28:59 -07003835__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08003836nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02003837 struct nfsd4_compound_state *cstate,
3838 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
Christoph Hellwigeb698532017-05-08 20:58:35 +02003840 struct nfsd4_setclientid_confirm *setclientid_confirm =
3841 &u->setclientid_confirm;
NeilBrown21ab45a2005-06-23 22:04:14 -07003842 struct nfs4_client *conf, *unconf;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003843 struct nfs4_client *old = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3845 clientid_t * clid = &setclientid_confirm->sc_clientid;
Al Virob37ad282006-10-19 23:28:59 -07003846 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03003847 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04003849 if (STALE_CLIENTID(clid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 return nfserr_stale_clientid;
NeilBrown21ab45a2005-06-23 22:04:14 -07003851
Jeff Laytond20c11d2014-07-30 08:27:07 -04003852 spin_lock(&nn->client_lock);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03003853 conf = find_confirmed_client(clid, false, nn);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03003854 unconf = find_unconfirmed_client(clid, false, nn);
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003855 /*
J. Bruce Fields8695b902012-05-19 10:05:58 -04003856 * We try hard to give out unique clientid's, so if we get an
3857 * attempt to confirm the same clientid with a different cred,
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003858 * the client may be buggy; this should never happen.
3859 *
3860 * Nevertheless, RFC 7530 recommends INUSE for this case:
J. Bruce Fieldsa186e762007-11-20 16:11:27 -05003861 */
J. Bruce Fieldsf984a7c2015-09-01 13:40:53 -04003862 status = nfserr_clid_inuse;
J. Bruce Fields8695b902012-05-19 10:05:58 -04003863 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3864 goto out;
3865 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3866 goto out;
J. Bruce Fields63db4632012-05-18 21:54:19 -04003867 /* cases below refer to rfc 3530 section 14.2.34: */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003868 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003869 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
3870 /* case 2: probable retransmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 status = nfs_ok;
J. Bruce Fields7d22fc12016-09-20 20:55:36 -04003872 } else /* case 4: client hasn't noticed we rebooted yet? */
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003873 status = nfserr_stale_clientid;
3874 goto out;
3875 }
3876 status = nfs_ok;
3877 if (conf) { /* case 1: callback update */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003878 old = unconf;
3879 unhash_client_locked(old);
J. Bruce Fields8695b902012-05-19 10:05:58 -04003880 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
J. Bruce Fields90d700b2012-05-19 13:55:22 -04003881 } else { /* case 3: normal case; new or rebooted client */
Jeff Laytond20c11d2014-07-30 08:27:07 -04003882 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3883 if (old) {
J. Bruce Fields2b634822015-10-15 15:33:23 -04003884 status = nfserr_clid_inuse;
3885 if (client_has_state(old)
3886 && !same_creds(&unconf->cl_cred,
3887 &old->cl_cred))
3888 goto out;
Jeff Laytond20c11d2014-07-30 08:27:07 -04003889 status = mark_client_expired_locked(old);
Jeff Layton7abea1e2014-07-30 08:27:13 -04003890 if (status) {
3891 old = NULL;
J. Bruce Fields221a6872013-04-01 22:23:49 -04003892 goto out;
Jeff Layton7abea1e2014-07-30 08:27:13 -04003893 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04003894 }
J. Bruce Fields8695b902012-05-19 10:05:58 -04003895 move_to_confirmed(unconf);
Jeff Laytond20c11d2014-07-30 08:27:07 -04003896 conf = unconf;
NeilBrown08e89872005-06-23 22:04:11 -07003897 }
Jeff Laytond20c11d2014-07-30 08:27:07 -04003898 get_client_locked(conf);
3899 spin_unlock(&nn->client_lock);
3900 nfsd4_probe_callback(conf);
3901 spin_lock(&nn->client_lock);
3902 put_client_renew_locked(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903out:
Jeff Laytond20c11d2014-07-30 08:27:07 -04003904 spin_unlock(&nn->client_lock);
3905 if (old)
3906 expire_client(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 return status;
3908}
3909
J. Bruce Fields32513b42011-10-13 16:00:16 -04003910static struct nfs4_file *nfsd4_alloc_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911{
J. Bruce Fields32513b42011-10-13 16:00:16 -04003912 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3913}
3914
3915/* OPEN Share state helper functions */
Jeff Layton5b095e92014-10-23 08:01:02 -04003916static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
3917 struct nfs4_file *fp)
J. Bruce Fields32513b42011-10-13 16:00:16 -04003918{
Trond Myklebust950e0112014-06-30 11:48:31 -04003919 lockdep_assert_held(&state_lock);
3920
Elena Reshetova818a34e2017-10-20 12:53:30 +03003921 refcount_set(&fp->fi_ref, 1);
Trond Myklebust1d31a252014-07-10 14:07:25 -04003922 spin_lock_init(&fp->fi_lock);
J. Bruce Fields32513b42011-10-13 16:00:16 -04003923 INIT_LIST_HEAD(&fp->fi_stateids);
3924 INIT_LIST_HEAD(&fp->fi_delegations);
Sachin Bhamare8287f002015-04-27 14:50:14 +02003925 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
Trond Myklebuste2cf80d2014-07-23 16:17:38 -04003926 fh_copy_shallow(&fp->fi_fhandle, fh);
Jeff Layton0c637be2014-08-22 12:05:43 -04003927 fp->fi_deleg_file = NULL;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003928 fp->fi_had_conflict = false;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04003929 fp->fi_share_deny = 0;
J. Bruce Fields32513b42011-10-13 16:00:16 -04003930 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3931 memset(fp->fi_access, 0, sizeof(fp->fi_access));
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003932#ifdef CONFIG_NFSD_PNFS
3933 INIT_LIST_HEAD(&fp->fi_lo_states);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02003934 atomic_set(&fp->fi_lo_recalls, 0);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003935#endif
Jeff Layton5b095e92014-10-23 08:01:02 -04003936 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
J. Bruce Fieldse8ff2a82007-08-01 15:30:59 -04003939void
NeilBrowne60d4392005-06-23 22:03:01 -07003940nfsd4_free_slabs(void)
3941{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003942 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003943 kmem_cache_destroy(openowner_slab);
3944 kmem_cache_destroy(lockowner_slab);
3945 kmem_cache_destroy(file_slab);
3946 kmem_cache_destroy(stateid_slab);
3947 kmem_cache_destroy(deleg_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003948 kmem_cache_destroy(odstate_slab);
NeilBrowne60d4392005-06-23 22:03:01 -07003949}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
Bryan Schumaker72083392011-11-01 15:24:59 -04003951int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952nfsd4_init_slabs(void)
3953{
Jeff Layton9258a2d2018-03-16 09:47:22 -04003954 client_slab = kmem_cache_create("nfsd4_clients",
3955 sizeof(struct nfs4_client), 0, 0, NULL);
3956 if (client_slab == NULL)
3957 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003958 openowner_slab = kmem_cache_create("nfsd4_openowners",
3959 sizeof(struct nfs4_openowner), 0, 0, NULL);
3960 if (openowner_slab == NULL)
Jeff Layton9258a2d2018-03-16 09:47:22 -04003961 goto out_free_client_slab;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003962 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
Yanchuan Nian3c407942012-10-24 14:44:19 +08003963 sizeof(struct nfs4_lockowner), 0, 0, NULL);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04003964 if (lockowner_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003965 goto out_free_openowner_slab;
NeilBrowne60d4392005-06-23 22:03:01 -07003966 file_slab = kmem_cache_create("nfsd4_files",
Paul Mundt20c2df82007-07-20 10:11:58 +09003967 sizeof(struct nfs4_file), 0, 0, NULL);
NeilBrowne60d4392005-06-23 22:03:01 -07003968 if (file_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003969 goto out_free_lockowner_slab;
NeilBrown5ac049a2005-06-23 22:03:03 -07003970 stateid_slab = kmem_cache_create("nfsd4_stateids",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04003971 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
NeilBrown5ac049a2005-06-23 22:03:03 -07003972 if (stateid_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003973 goto out_free_file_slab;
NeilBrown5b2d21c2005-06-23 22:03:04 -07003974 deleg_slab = kmem_cache_create("nfsd4_delegations",
Paul Mundt20c2df82007-07-20 10:11:58 +09003975 sizeof(struct nfs4_delegation), 0, 0, NULL);
NeilBrown5b2d21c2005-06-23 22:03:04 -07003976 if (deleg_slab == NULL)
Christoph Hellwigabf11352014-05-21 07:43:03 -07003977 goto out_free_stateid_slab;
Sachin Bhamare8287f002015-04-27 14:50:14 +02003978 odstate_slab = kmem_cache_create("nfsd4_odstate",
3979 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
3980 if (odstate_slab == NULL)
3981 goto out_free_deleg_slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 return 0;
Christoph Hellwigabf11352014-05-21 07:43:03 -07003983
Sachin Bhamare8287f002015-04-27 14:50:14 +02003984out_free_deleg_slab:
3985 kmem_cache_destroy(deleg_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003986out_free_stateid_slab:
3987 kmem_cache_destroy(stateid_slab);
3988out_free_file_slab:
3989 kmem_cache_destroy(file_slab);
3990out_free_lockowner_slab:
3991 kmem_cache_destroy(lockowner_slab);
3992out_free_openowner_slab:
3993 kmem_cache_destroy(openowner_slab);
Jeff Layton9258a2d2018-03-16 09:47:22 -04003994out_free_client_slab:
3995 kmem_cache_destroy(client_slab);
Christoph Hellwigabf11352014-05-21 07:43:03 -07003996out:
NeilBrowne60d4392005-06-23 22:03:01 -07003997 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3998 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999}
4000
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004001static void init_nfs4_replay(struct nfs4_replay *rp)
4002{
4003 rp->rp_status = nfserr_serverfault;
4004 rp->rp_buflen = 0;
4005 rp->rp_buf = rp->rp_ibuf;
Jeff Layton58fb12e2014-07-29 21:34:27 -04004006 mutex_init(&rp->rp_mutex);
4007}
4008
4009static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4010 struct nfs4_stateowner *so)
4011{
4012 if (!nfsd4_has_session(cstate)) {
4013 mutex_lock(&so->so_replay.rp_mutex);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004014 cstate->replay_owner = nfs4_get_stateowner(so);
Jeff Layton58fb12e2014-07-29 21:34:27 -04004015 }
4016}
4017
4018void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4019{
4020 struct nfs4_stateowner *so = cstate->replay_owner;
4021
4022 if (so != NULL) {
4023 cstate->replay_owner = NULL;
4024 mutex_unlock(&so->so_replay.rp_mutex);
4025 nfs4_put_stateowner(so);
4026 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004027}
4028
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004029static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030{
4031 struct nfs4_stateowner *sop;
4032
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004033 sop = kmem_cache_alloc(slab, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004034 if (!sop)
4035 return NULL;
4036
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04004037 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004038 if (!sop->so_owner.data) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004039 kmem_cache_free(slab, sop);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004040 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 }
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004042
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004043 INIT_LIST_HEAD(&sop->so_stateids);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004044 sop->so_client = clp;
4045 init_nfs4_replay(&sop->so_replay);
Jeff Layton6b180f02014-07-29 21:34:26 -04004046 atomic_set(&sop->so_count, 1);
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004047 return sop;
4048}
4049
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004050static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
J. Bruce Fieldsff194bd2011-08-12 09:42:57 -04004051{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004052 lockdep_assert_held(&clp->cl_lock);
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03004053
Trond Myklebustd4f04892014-07-29 21:34:36 -04004054 list_add(&oo->oo_owner.so_strhash,
4055 &clp->cl_ownerstr_hashtbl[strhashval]);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004056 list_add(&oo->oo_perclient, &clp->cl_openowners);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057}
4058
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004059static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4060{
Trond Myklebustd4f04892014-07-29 21:34:36 -04004061 unhash_openowner_locked(openowner(so));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004062}
4063
Jeff Layton6b180f02014-07-29 21:34:26 -04004064static void nfs4_free_openowner(struct nfs4_stateowner *so)
4065{
4066 struct nfs4_openowner *oo = openowner(so);
4067
4068 kmem_cache_free(openowner_slab, oo);
4069}
4070
4071static const struct nfs4_stateowner_operations openowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04004072 .so_unhash = nfs4_unhash_openowner,
4073 .so_free = nfs4_free_openowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04004074};
4075
Andrew Elble7fc05642015-11-05 20:42:43 -05004076static struct nfs4_ol_stateid *
4077nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4078{
4079 struct nfs4_ol_stateid *local, *ret = NULL;
4080 struct nfs4_openowner *oo = open->op_openowner;
4081
4082 lockdep_assert_held(&fp->fi_lock);
4083
4084 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4085 /* ignore lock owners */
4086 if (local->st_stateowner->so_is_open_owner == 0)
4087 continue;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004088 if (local->st_stateowner != &oo->oo_owner)
4089 continue;
4090 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
Andrew Elble7fc05642015-11-05 20:42:43 -05004091 ret = local;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004092 refcount_inc(&ret->st_stid.sc_count);
Andrew Elble7fc05642015-11-05 20:42:43 -05004093 break;
4094 }
4095 }
4096 return ret;
4097}
4098
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004099static __be32
4100nfsd4_verify_open_stid(struct nfs4_stid *s)
4101{
4102 __be32 ret = nfs_ok;
4103
4104 switch (s->sc_type) {
4105 default:
4106 break;
Trond Myklebust4f176412018-01-12 17:42:30 -05004107 case 0:
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004108 case NFS4_CLOSED_STID:
4109 case NFS4_CLOSED_DELEG_STID:
4110 ret = nfserr_bad_stateid;
4111 break;
4112 case NFS4_REVOKED_DELEG_STID:
4113 ret = nfserr_deleg_revoked;
4114 }
4115 return ret;
4116}
4117
4118/* Lock the stateid st_mutex, and deal with races with CLOSE */
4119static __be32
4120nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4121{
4122 __be32 ret;
4123
Andrew Elble4f34bd02017-11-08 17:29:51 -05004124 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004125 ret = nfsd4_verify_open_stid(&stp->st_stid);
4126 if (ret != nfs_ok)
4127 mutex_unlock(&stp->st_mutex);
4128 return ret;
4129}
4130
4131static struct nfs4_ol_stateid *
4132nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4133{
4134 struct nfs4_ol_stateid *stp;
4135 for (;;) {
4136 spin_lock(&fp->fi_lock);
4137 stp = nfsd4_find_existing_open(fp, open);
4138 spin_unlock(&fp->fi_lock);
4139 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4140 break;
4141 nfs4_put_stid(&stp->st_stid);
4142 }
4143 return stp;
4144}
4145
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004146static struct nfs4_openowner *
Trond Myklebust13d6f662014-06-30 11:48:45 -04004147alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004148 struct nfsd4_compound_state *cstate)
4149{
Trond Myklebust13d6f662014-06-30 11:48:45 -04004150 struct nfs4_client *clp = cstate->clp;
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004151 struct nfs4_openowner *oo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004153 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4154 if (!oo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 return NULL;
Jeff Layton6b180f02014-07-29 21:34:26 -04004156 oo->oo_owner.so_ops = &openowner_ops;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004157 oo->oo_owner.so_is_open_owner = 1;
4158 oo->oo_owner.so_seqid = open->op_seqid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004159 oo->oo_flags = 0;
Jeff Laytondb24b3b2014-06-30 11:48:36 -04004160 if (nfsd4_has_session(cstate))
4161 oo->oo_flags |= NFS4_OO_CONFIRMED;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004162 oo->oo_time = 0;
J. Bruce Fields38c387b2011-09-16 17:42:48 -04004163 oo->oo_last_closed_stid = NULL;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004164 INIT_LIST_HEAD(&oo->oo_close_lru);
Trond Myklebustd4f04892014-07-29 21:34:36 -04004165 spin_lock(&clp->cl_lock);
4166 ret = find_openstateowner_str_locked(strhashval, open, clp);
Trond Myklebust7ffb5882014-07-29 21:34:34 -04004167 if (ret == NULL) {
4168 hash_openowner(oo, clp, strhashval);
4169 ret = oo;
4170 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08004171 nfs4_free_stateowner(&oo->oo_owner);
4172
Trond Myklebustd4f04892014-07-29 21:34:36 -04004173 spin_unlock(&clp->cl_lock);
Jeff Laytonc5952332015-03-23 10:53:42 -04004174 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175}
4176
Andrew Elble7fc05642015-11-05 20:42:43 -05004177static struct nfs4_ol_stateid *
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004178init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
Andrew Elble7fc05642015-11-05 20:42:43 -05004179{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Andrew Elble7fc05642015-11-05 20:42:43 -05004181 struct nfs4_openowner *oo = open->op_openowner;
4182 struct nfs4_ol_stateid *retstp = NULL;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004183 struct nfs4_ol_stateid *stp;
Andrew Elble7fc05642015-11-05 20:42:43 -05004184
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004185 stp = open->op_stp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004186 /* We are moving these outside of the spinlocks to avoid the warnings */
4187 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05004188 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004189
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004190retry:
Andrew Elble7fc05642015-11-05 20:42:43 -05004191 spin_lock(&oo->oo_owner.so_client->cl_lock);
4192 spin_lock(&fp->fi_lock);
4193
4194 retstp = nfsd4_find_existing_open(fp, open);
4195 if (retstp)
4196 goto out_unlock;
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004197
4198 open->op_stp = NULL;
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004199 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05004200 stp->st_stid.sc_type = NFS4_OPEN_STID;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04004201 INIT_LIST_HEAD(&stp->st_locks);
Kinglong Meeb5971af2014-08-22 10:18:43 -04004202 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07004203 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04004204 stp->st_stid.sc_file = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 stp->st_access_bmap = 0;
4206 stp->st_deny_bmap = 0;
NeilBrown4c4cd222005-07-07 17:59:27 -07004207 stp->st_openstp = NULL;
Jeff Layton1c755dc2014-07-29 21:34:12 -04004208 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04004209 list_add(&stp->st_perfile, &fp->fi_stateids);
Andrew Elble7fc05642015-11-05 20:42:43 -05004210
4211out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04004212 spin_unlock(&fp->fi_lock);
Jeff Layton1c755dc2014-07-29 21:34:12 -04004213 spin_unlock(&oo->oo_owner.so_client->cl_lock);
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004214 if (retstp) {
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004215 /* Handle races with CLOSE */
4216 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4217 nfs4_put_stid(&retstp->st_stid);
4218 goto retry;
4219 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004220 /* To keep mutex tracking happy */
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004221 mutex_unlock(&stp->st_mutex);
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004222 stp = retstp;
Oleg Drokin5cc1fb22016-06-14 23:28:05 -04004223 }
Oleg Drokin8c7245ab2016-06-14 23:28:06 -04004224 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225}
4226
Jeff Laytond3134b12014-07-29 21:34:32 -04004227/*
4228 * In the 4.0 case we need to keep the owners around a little while to handle
4229 * CLOSE replay. We still do need to release any file access that is held by
4230 * them before returning however.
4231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232static void
Jeff Laytond3134b12014-07-29 21:34:32 -04004233move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234{
Jeff Layton217526e2014-07-30 08:27:11 -04004235 struct nfs4_ol_stateid *last;
Jeff Laytond3134b12014-07-29 21:34:32 -04004236 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4237 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4238 nfsd_net_id);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004239
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004240 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Jeff Laytonb401be222014-07-29 21:34:33 -04004242 /*
4243 * We know that we hold one reference via nfsd4_close, and another
4244 * "persistent" reference for the client. If the refcount is higher
4245 * than 2, then there are still calls in progress that are using this
4246 * stateid. We can't put the sc_file reference until they are finished.
4247 * Wait for the refcount to drop to 2. Since it has been unhashed,
4248 * there should be no danger of the refcount going back up again at
4249 * this point.
4250 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004251 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
Jeff Laytonb401be222014-07-29 21:34:33 -04004252
Jeff Laytond3134b12014-07-29 21:34:32 -04004253 release_all_access(s);
4254 if (s->st_stid.sc_file) {
4255 put_nfs4_file(s->st_stid.sc_file);
4256 s->st_stid.sc_file = NULL;
4257 }
Jeff Layton217526e2014-07-30 08:27:11 -04004258
4259 spin_lock(&nn->client_lock);
4260 last = oo->oo_last_closed_stid;
Jeff Laytond3134b12014-07-29 21:34:32 -04004261 oo->oo_last_closed_stid = s;
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03004262 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004263 oo->oo_time = get_seconds();
Jeff Layton217526e2014-07-30 08:27:11 -04004264 spin_unlock(&nn->client_lock);
4265 if (last)
4266 nfs4_put_stid(&last->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267}
4268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269/* search file_hashtbl[] for file */
4270static struct nfs4_file *
Jeff Layton5b095e92014-10-23 08:01:02 -04004271find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 struct nfs4_file *fp;
4274
Jeff Layton5b095e92014-10-23 08:01:02 -04004275 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
Christoph Hellwig4d94c2e2014-08-14 08:41:48 +02004276 if (fh_match(&fp->fi_fhandle, fh)) {
Elena Reshetova818a34e2017-10-20 12:53:30 +03004277 if (refcount_inc_not_zero(&fp->fi_ref))
Jeff Layton5b095e92014-10-23 08:01:02 -04004278 return fp;
NeilBrown13cd2182005-06-23 22:03:10 -07004279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 }
4281 return NULL;
4282}
4283
Christoph Hellwige6ba76e2014-08-14 08:50:16 +02004284struct nfs4_file *
Trond Myklebustca943212014-07-23 16:17:39 -04004285find_file(struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004286{
4287 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004288 unsigned int hashval = file_hashval(fh);
Trond Myklebust950e0112014-06-30 11:48:31 -04004289
Jeff Layton5b095e92014-10-23 08:01:02 -04004290 rcu_read_lock();
4291 fp = find_file_locked(fh, hashval);
4292 rcu_read_unlock();
Trond Myklebust950e0112014-06-30 11:48:31 -04004293 return fp;
4294}
4295
4296static struct nfs4_file *
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004297find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
Trond Myklebust950e0112014-06-30 11:48:31 -04004298{
4299 struct nfs4_file *fp;
Jeff Layton5b095e92014-10-23 08:01:02 -04004300 unsigned int hashval = file_hashval(fh);
4301
4302 rcu_read_lock();
4303 fp = find_file_locked(fh, hashval);
4304 rcu_read_unlock();
4305 if (fp)
4306 return fp;
Trond Myklebust950e0112014-06-30 11:48:31 -04004307
4308 spin_lock(&state_lock);
Jeff Layton5b095e92014-10-23 08:01:02 -04004309 fp = find_file_locked(fh, hashval);
4310 if (likely(fp == NULL)) {
4311 nfsd4_init_file(fh, hashval, new);
Trond Myklebust950e0112014-06-30 11:48:31 -04004312 fp = new;
4313 }
4314 spin_unlock(&state_lock);
4315
4316 return fp;
4317}
4318
J. Bruce Fields4f83aa32008-07-07 15:02:02 -04004319/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 * Called to check deny when READ with all zero stateid or
4321 * WRITE with all zero or all one stateid
4322 */
Al Virob37ad282006-10-19 23:28:59 -07004323static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 struct nfs4_file *fp;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004327 __be32 ret = nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
Trond Myklebustca943212014-07-23 16:17:39 -04004329 fp = find_file(&current_fh->fh_handle);
NeilBrown13cd2182005-06-23 22:03:10 -07004330 if (!fp)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004331 return ret;
4332 /* Check for conflicting share reservations */
Trond Myklebust1d31a252014-07-10 14:07:25 -04004333 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004334 if (fp->fi_share_deny & deny_type)
4335 ret = nfserr_locked;
Trond Myklebust1d31a252014-07-10 14:07:25 -04004336 spin_unlock(&fp->fi_lock);
NeilBrown13cd2182005-06-23 22:03:10 -07004337 put_nfs4_file(fp);
4338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339}
4340
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004341static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004343 struct nfs4_delegation *dp = cb_to_delegation(cb);
Trond Myklebust11b91642014-07-29 21:34:08 -04004344 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4345 nfsd_net_id);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04004346
Trond Myklebust11b91642014-07-29 21:34:08 -04004347 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
Jeff Laytonf54fe962014-07-25 07:34:26 -04004348
Jeff Layton02e12152014-07-16 10:31:57 -04004349 /*
4350 * We can't do this in nfsd_break_deleg_cb because it is
Jeff Laytonf54fe962014-07-25 07:34:26 -04004351 * already holding inode->i_lock.
4352 *
Jeff Laytondff13992014-07-08 14:02:49 -04004353 * If the dl_time != 0, then we know that it has already been
4354 * queued for a lease break. Don't queue it again.
4355 */
Jeff Laytonf54fe962014-07-25 07:34:26 -04004356 spin_lock(&state_lock);
Jeff Laytondff13992014-07-08 14:02:49 -04004357 if (dp->dl_time == 0) {
Jeff Laytondff13992014-07-08 14:02:49 -04004358 dp->dl_time = get_seconds();
Jeff Layton02e12152014-07-16 10:31:57 -04004359 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
Jeff Laytondff13992014-07-08 14:02:49 -04004360 }
Jeff Layton02e12152014-07-16 10:31:57 -04004361 spin_unlock(&state_lock);
4362}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004364static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4365 struct rpc_task *task)
4366{
4367 struct nfs4_delegation *dp = cb_to_delegation(cb);
4368
Andrew Elblea4579742015-08-31 12:06:41 -04004369 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
4370 return 1;
4371
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004372 switch (task->tk_status) {
4373 case 0:
4374 return 1;
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004375 case -NFS4ERR_DELAY:
4376 rpc_delay(task, 2 * HZ);
4377 return 0;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004378 case -EBADHANDLE:
4379 case -NFS4ERR_BAD_STATEID:
4380 /*
4381 * Race: client probably got cb_recall before open reply
4382 * granting delegation.
4383 */
4384 if (dp->dl_retries--) {
4385 rpc_delay(task, 2 * HZ);
4386 return 0;
4387 }
4388 /*FALLTHRU*/
4389 default:
Scott Mayhew1c73b9d2019-05-02 13:32:12 -04004390 return 1;
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004391 }
4392}
4393
4394static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4395{
4396 struct nfs4_delegation *dp = cb_to_delegation(cb);
4397
4398 nfs4_put_stid(&dp->dl_stid);
4399}
4400
Julia Lawallc4cb8972015-11-21 22:57:39 +01004401static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
Christoph Hellwig0162ac22014-09-24 12:19:19 +02004402 .prepare = nfsd4_cb_recall_prepare,
4403 .done = nfsd4_cb_recall_done,
4404 .release = nfsd4_cb_recall_release,
4405};
4406
Jeff Layton02e12152014-07-16 10:31:57 -04004407static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4408{
4409 /*
4410 * We're assuming the state code never drops its reference
4411 * without first removing the lease. Since we're in this lease
J. Bruce Fields4a269efb2018-04-13 17:34:35 -04004412 * callback (and since the lease code is serialized by the
4413 * i_lock) we know the server hasn't removed the lease yet, and
4414 * we know it's safe to take a reference.
Jeff Layton02e12152014-07-16 10:31:57 -04004415 */
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03004416 refcount_inc(&dp->dl_stid.sc_count);
Christoph Hellwigf0b5de12014-09-24 12:19:18 +02004417 nfsd4_run_cb(&dp->dl_recall);
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004418}
4419
Jeff Layton1c8c6012013-06-21 08:58:15 -04004420/* Called from break_lease() with i_lock held. */
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004421static bool
4422nfsd_break_deleg_cb(struct file_lock *fl)
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004423{
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004424 bool ret = false;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004425 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4426 struct nfs4_file *fp = dp->dl_stid.sc_file;
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004427
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004428 /*
4429 * We don't want the locks code to timeout the lease for us;
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004430 * we'll remove it ourself if a delegation isn't returned
J. Bruce Fields6b57d9c2011-01-31 11:54:04 -05004431 * in time:
J. Bruce Fields0272e1f2007-09-12 18:56:12 -04004432 */
4433 fl->fl_break_time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Jeff Layton02e12152014-07-16 10:31:57 -04004435 spin_lock(&fp->fi_lock);
Jeff Layton417c6622014-07-21 09:34:57 -04004436 fp->fi_had_conflict = true;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004437 nfsd_break_one_deleg(dp);
Jeff Layton02e12152014-07-16 10:31:57 -04004438 spin_unlock(&fp->fi_lock);
Jeff Layton4d01b7f2014-09-01 15:06:54 -04004439 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440}
4441
Jeff Laytonc45198e2014-09-01 07:12:07 -04004442static int
Jeff Layton7448cc32015-01-16 15:05:57 -05004443nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4444 struct list_head *dispose)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445{
4446 if (arg & F_UNLCK)
Jeff Laytonc45198e2014-09-01 07:12:07 -04004447 return lease_modify(onlist, arg, dispose);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 else
4449 return -EAGAIN;
4450}
4451
Alexey Dobriyan7b021962009-09-21 17:01:12 -07004452static const struct lock_manager_operations nfsd_lease_mng_ops = {
J. Bruce Fields8fb47a42011-07-20 20:21:59 -04004453 .lm_break = nfsd_break_deleg_cb,
4454 .lm_change = nfsd_change_deleg_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455};
4456
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04004457static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4458{
4459 if (nfsd4_has_session(cstate))
4460 return nfs_ok;
4461 if (seqid == so->so_seqid - 1)
4462 return nfserr_replay_me;
4463 if (seqid == so->so_seqid)
4464 return nfs_ok;
4465 return nfserr_bad_seqid;
4466}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Jeff Layton4b24ca72014-06-30 11:48:44 -04004468static __be32 lookup_clientid(clientid_t *clid,
4469 struct nfsd4_compound_state *cstate,
4470 struct nfsd_net *nn)
4471{
4472 struct nfs4_client *found;
4473
4474 if (cstate->clp) {
4475 found = cstate->clp;
4476 if (!same_clid(&found->cl_clientid, clid))
4477 return nfserr_stale_clientid;
4478 return nfs_ok;
4479 }
4480
4481 if (STALE_CLIENTID(clid, nn))
4482 return nfserr_stale_clientid;
4483
4484 /*
4485 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4486 * cached already then we know this is for is for v4.0 and "sessions"
4487 * will be false.
4488 */
4489 WARN_ON_ONCE(cstate->session);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004490 spin_lock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004491 found = find_confirmed_client(clid, false, nn);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004492 if (!found) {
4493 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004494 return nfserr_expired;
Trond Myklebust3e339f92014-07-30 08:27:09 -04004495 }
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04004496 atomic_inc(&found->cl_rpc_users);
Trond Myklebust3e339f92014-07-30 08:27:09 -04004497 spin_unlock(&nn->client_lock);
Jeff Layton4b24ca72014-06-30 11:48:44 -04004498
4499 /* Cache the nfs4_client in cstate! */
4500 cstate->clp = found;
Jeff Layton4b24ca72014-06-30 11:48:44 -04004501 return nfs_ok;
4502}
4503
Al Virob37ad282006-10-19 23:28:59 -07004504__be32
Andy Adamson66689582009-04-03 08:28:45 +03004505nfsd4_process_open1(struct nfsd4_compound_state *cstate,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03004506 struct nfsd4_open *open, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 clientid_t *clientid = &open->op_clientid;
4509 struct nfs4_client *clp = NULL;
4510 unsigned int strhashval;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004511 struct nfs4_openowner *oo = NULL;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004512 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04004514 if (STALE_CLIENTID(&open->op_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 return nfserr_stale_clientid;
J. Bruce Fields32513b42011-10-13 16:00:16 -04004516 /*
4517 * In case we need it later, after we've already created the
4518 * file and don't want to risk a further failure:
4519 */
4520 open->op_file = nfsd4_alloc_file();
4521 if (open->op_file == NULL)
4522 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
Trond Myklebust2d91e892014-06-30 11:48:46 -04004524 status = lookup_clientid(clientid, cstate, nn);
4525 if (status)
4526 return status;
4527 clp = cstate->clp;
4528
Trond Myklebustd4f04892014-07-29 21:34:36 -04004529 strhashval = ownerstr_hashval(&open->op_owner);
4530 oo = find_openstateowner_str(strhashval, open, clp);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004531 open->op_openowner = oo;
4532 if (!oo) {
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004533 goto new_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004535 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004536 /* Replace unconfirmed owners without checking for replay. */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004537 release_openowner(oo);
4538 open->op_openowner = NULL;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004539 goto new_owner;
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004540 }
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004541 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4542 if (status)
4543 return status;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004544 goto alloc_stateid;
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004545new_owner:
Trond Myklebust13d6f662014-06-30 11:48:45 -04004546 oo = alloc_init_open_stateowner(strhashval, open, cstate);
J. Bruce Fieldsbcf130f2011-10-12 20:44:20 -04004547 if (oo == NULL)
4548 return nfserr_jukebox;
4549 open->op_openowner = oo;
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004550alloc_stateid:
Jeff Laytonb49e0842014-07-29 21:34:11 -04004551 open->op_stp = nfs4_alloc_open_stateid(clp);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04004552 if (!open->op_stp)
4553 return nfserr_jukebox;
Sachin Bhamare8287f002015-04-27 14:50:14 +02004554
4555 if (nfsd4_has_session(cstate) &&
4556 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4557 open->op_odstate = alloc_clnt_odstate(clp);
4558 if (!open->op_odstate)
4559 return nfserr_jukebox;
4560 }
4561
J. Bruce Fields0f442aa2006-01-18 17:43:34 -08004562 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563}
4564
Al Virob37ad282006-10-19 23:28:59 -07004565static inline __be32
NeilBrown4a6e43e2005-06-23 22:02:50 -07004566nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4567{
4568 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4569 return nfserr_openmode;
4570 else
4571 return nfs_ok;
4572}
4573
Daniel Mackc47d8322011-05-16 16:38:14 +02004574static int share_access_to_flags(u32 share_access)
J. Bruce Fields24a01112010-05-18 20:01:35 -04004575{
J. Bruce Fields24a01112010-05-18 20:01:35 -04004576 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4577}
4578
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004579static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004580{
4581 struct nfs4_stid *ret;
4582
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004583 ret = find_stateid_by_type(cl, s,
4584 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
J. Bruce Fieldsf459e452011-09-09 09:06:12 -04004585 if (!ret)
4586 return NULL;
4587 return delegstateid(ret);
4588}
4589
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004590static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4591{
4592 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4593 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4594}
4595
Al Virob37ad282006-10-19 23:28:59 -07004596static __be32
J. Bruce Fields41d22662013-03-21 15:49:47 -04004597nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
NeilBrown567d9822005-06-23 22:02:53 -07004598 struct nfs4_delegation **dp)
4599{
4600 int flags;
Al Virob37ad282006-10-19 23:28:59 -07004601 __be32 status = nfserr_bad_stateid;
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004602 struct nfs4_delegation *deleg;
NeilBrown567d9822005-06-23 22:02:53 -07004603
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004604 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4605 if (deleg == NULL)
NeilBrownc44c5ee2005-06-23 22:02:54 -07004606 goto out;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04004607 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4608 nfs4_put_stid(&deleg->dl_stid);
4609 if (cl->cl_minorversion)
4610 status = nfserr_deleg_revoked;
4611 goto out;
4612 }
J. Bruce Fields24a01112010-05-18 20:01:35 -04004613 flags = share_access_to_flags(open->op_share_access);
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04004614 status = nfs4_check_delegmode(deleg, flags);
4615 if (status) {
4616 nfs4_put_stid(&deleg->dl_stid);
4617 goto out;
4618 }
4619 *dp = deleg;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004620out:
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004621 if (!nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07004622 return nfs_ok;
4623 if (status)
4624 return status;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004625 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004626 return nfs_ok;
NeilBrown567d9822005-06-23 22:02:53 -07004627}
4628
J. Bruce Fields21fb4012010-07-28 12:21:23 -04004629static inline int nfs4_access_to_access(u32 nfs4_access)
4630{
4631 int flags = 0;
4632
4633 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4634 flags |= NFSD_MAY_READ;
4635 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4636 flags |= NFSD_MAY_WRITE;
4637 return flags;
4638}
4639
Al Virob37ad282006-10-19 23:28:59 -07004640static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4642 struct nfsd4_open *open)
4643{
4644 struct iattr iattr = {
4645 .ia_valid = ATTR_SIZE,
4646 .ia_size = 0,
4647 };
4648 if (!open->op_truncate)
4649 return 0;
4650 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
Al Viro92465852006-01-18 17:43:46 -08004651 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
4653}
4654
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004655static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004656 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4657 struct nfsd4_open *open)
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004658{
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004659 struct nfsd_file *nf = NULL;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004660 __be32 status;
4661 int oflag = nfs4_access_to_omode(open->op_share_access);
4662 int access = nfs4_access_to_access(open->op_share_access);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004663 unsigned char old_access_bmap, old_deny_bmap;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004664
Trond Myklebustde186432014-07-10 14:07:26 -04004665 spin_lock(&fp->fi_lock);
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004666
4667 /*
4668 * Are we trying to set a deny mode that would conflict with
4669 * current access?
4670 */
4671 status = nfs4_file_check_deny(fp, open->op_share_deny);
4672 if (status != nfs_ok) {
4673 spin_unlock(&fp->fi_lock);
4674 goto out;
4675 }
4676
4677 /* set access to the file */
4678 status = nfs4_file_get_access(fp, open->op_share_access);
4679 if (status != nfs_ok) {
4680 spin_unlock(&fp->fi_lock);
4681 goto out;
4682 }
4683
4684 /* Set access bits in stateid */
4685 old_access_bmap = stp->st_access_bmap;
4686 set_access(open->op_share_access, stp);
4687
4688 /* Set new deny mask */
4689 old_deny_bmap = stp->st_deny_bmap;
4690 set_deny(open->op_share_deny, stp);
4691 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4692
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004693 if (!fp->fi_fds[oflag]) {
Trond Myklebustde186432014-07-10 14:07:26 -04004694 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004695 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004696 if (status)
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004697 goto out_put_access;
Trond Myklebustde186432014-07-10 14:07:26 -04004698 spin_lock(&fp->fi_lock);
4699 if (!fp->fi_fds[oflag]) {
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004700 fp->fi_fds[oflag] = nf;
4701 nf = NULL;
Trond Myklebustde186432014-07-10 14:07:26 -04004702 }
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004703 }
Trond Myklebustde186432014-07-10 14:07:26 -04004704 spin_unlock(&fp->fi_lock);
Jeff Laytonfd4f83f2019-08-18 14:18:52 -04004705 if (nf)
4706 nfsd_file_put(nf);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004707
4708 status = nfsd4_truncate(rqstp, cur_fh, open);
4709 if (status)
4710 goto out_put_access;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004711out:
4712 return status;
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004713out_put_access:
4714 stp->st_access_bmap = old_access_bmap;
4715 nfs4_file_put_access(fp, open->op_share_access);
4716 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4717 goto out;
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004718}
4719
Al Virob37ad282006-10-19 23:28:59 -07004720static __be32
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004721nfs4_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 -07004722{
Al Virob37ad282006-10-19 23:28:59 -07004723 __be32 status;
Arnd Bergmann6ac75362015-05-12 23:31:29 +02004724 unsigned char old_deny_bmap = stp->st_deny_bmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04004726 if (!test_access(open->op_share_access, stp))
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004727 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
Christoph Hellwig7e6a72e2014-06-30 11:48:30 -04004728
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004729 /* test and set deny mode */
4730 spin_lock(&fp->fi_lock);
4731 status = nfs4_file_check_deny(fp, open->op_share_deny);
4732 if (status == nfs_ok) {
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004733 set_deny(open->op_share_deny, stp);
4734 fp->fi_share_deny |=
4735 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4736 }
4737 spin_unlock(&fp->fi_lock);
4738
4739 if (status != nfs_ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
Jeff Laytonbaeb4ff2014-07-10 14:07:34 -04004742 status = nfsd4_truncate(rqstp, cur_fh, open);
4743 if (status != nfs_ok)
4744 reset_union_bmap_deny(old_deny_bmap, stp);
4745 return status;
4746}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004748/* Should we give out recallable state?: */
4749static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4750{
4751 if (clp->cl_cb_state == NFSD4_CB_UP)
4752 return true;
4753 /*
4754 * In the sessions case, since we don't have to establish a
4755 * separate connection for callbacks, we assume it's OK
4756 * until we hear otherwise:
4757 */
4758 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4759}
4760
J. Bruce Fields653e5142018-02-15 22:08:45 -05004761static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4762 int flag)
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004763{
4764 struct file_lock *fl;
4765
4766 fl = locks_alloc_lock();
4767 if (!fl)
4768 return NULL;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004769 fl->fl_lmops = &nfsd_lease_mng_ops;
J. Bruce Fields617588d2011-07-01 15:18:34 -04004770 fl->fl_flags = FL_DELEG;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004771 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4772 fl->fl_end = OFFSET_MAX;
J. Bruce Fields653e5142018-02-15 22:08:45 -05004773 fl->fl_owner = (fl_owner_t)dp;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004774 fl->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004775 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
J. Bruce Fields22d38c42011-01-31 11:55:12 -05004776 return fl;
4777}
4778
Jeff Layton0b266932014-07-25 07:34:25 -04004779static struct nfs4_delegation *
4780nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
Sachin Bhamare8287f002015-04-27 14:50:14 +02004781 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
J. Bruce Fieldsacfdf5c2011-01-31 19:20:39 -05004782{
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004783 int status = 0;
Jeff Layton0b266932014-07-25 07:34:25 -04004784 struct nfs4_delegation *dp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004785 struct nfsd_file *nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004786 struct file_lock *fl;
Jeff Layton417c6622014-07-21 09:34:57 -04004787
J. Bruce Fields353601e2018-02-16 14:29:42 -05004788 /*
4789 * The fi_had_conflict and nfs_get_existing_delegation checks
4790 * here are just optimizations; we'll need to recheck them at
4791 * the end:
4792 */
J. Bruce Fieldsbf7bd3e2013-08-15 16:55:26 -04004793 if (fp->fi_had_conflict)
Jeff Layton0b266932014-07-25 07:34:25 -04004794 return ERR_PTR(-EAGAIN);
4795
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004796 nf = find_readable_file(fp);
4797 if (!nf) {
J. Bruce Fields353601e2018-02-16 14:29:42 -05004798 /* We should always have a readable file here */
4799 WARN_ON_ONCE(1);
4800 return ERR_PTR(-EBADF);
4801 }
Andrew Elble34ed9872015-10-15 12:07:28 -04004802 spin_lock(&state_lock);
4803 spin_lock(&fp->fi_lock);
J. Bruce Fields68b18f52018-02-19 11:38:33 -05004804 if (nfs4_delegation_exists(clp, fp))
4805 status = -EAGAIN;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004806 else if (!fp->fi_deleg_file) {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004807 fp->fi_deleg_file = nf;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004808 /* increment early to prevent fi_deleg_file from being
4809 * cleared */
4810 fp->fi_delegees = 1;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004811 nf = NULL;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004812 } else
4813 fp->fi_delegees++;
4814 spin_unlock(&fp->fi_lock);
4815 spin_unlock(&state_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004816 if (nf)
4817 nfsd_file_put(nf);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004818 if (status)
4819 return ERR_PTR(status);
4820
4821 status = -ENOMEM;
4822 dp = alloc_init_deleg(clp, fp, fh, odstate);
4823 if (!dp)
4824 goto out_delegees;
4825
4826 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4827 if (!fl)
Andrew Elblebd8d7252018-10-05 09:32:08 -04004828 goto out_clnt_odstate;
J. Bruce Fields353601e2018-02-16 14:29:42 -05004829
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004830 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004831 if (fl)
4832 locks_free_lock(fl);
4833 if (status)
4834 goto out_clnt_odstate;
4835
4836 spin_lock(&state_lock);
4837 spin_lock(&fp->fi_lock);
4838 if (fp->fi_had_conflict)
4839 status = -EAGAIN;
4840 else
4841 status = hash_delegation_locked(dp, fp);
Andrew Elble34ed9872015-10-15 12:07:28 -04004842 spin_unlock(&fp->fi_lock);
4843 spin_unlock(&state_lock);
4844
4845 if (status)
Andrew Elble692ad282018-04-18 17:04:37 -04004846 goto out_unlock;
4847
Jeff Layton0b266932014-07-25 07:34:25 -04004848 return dp;
Andrew Elble692ad282018-04-18 17:04:37 -04004849out_unlock:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04004850 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004851out_clnt_odstate:
4852 put_clnt_odstate(dp->dl_clnt_odstate);
J. Bruce Fields353601e2018-02-16 14:29:42 -05004853 nfs4_put_stid(&dp->dl_stid);
4854out_delegees:
4855 put_deleg_file(fp);
4856 return ERR_PTR(status);
J. Bruce Fieldsedab9782011-01-31 17:58:10 -05004857}
4858
Benny Halevy4aa89132012-02-21 14:16:44 -08004859static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
4860{
4861 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4862 if (status == -EAGAIN)
4863 open->op_why_no_deleg = WND4_CONTENTION;
4864 else {
4865 open->op_why_no_deleg = WND4_RESOURCE;
4866 switch (open->op_deleg_want) {
4867 case NFS4_SHARE_WANT_READ_DELEG:
4868 case NFS4_SHARE_WANT_WRITE_DELEG:
4869 case NFS4_SHARE_WANT_ANY_DELEG:
4870 break;
4871 case NFS4_SHARE_WANT_CANCEL:
4872 open->op_why_no_deleg = WND4_CANCELLED;
4873 break;
4874 case NFS4_SHARE_WANT_NO_DELEG:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05004875 WARN_ON_ONCE(1);
Benny Halevy4aa89132012-02-21 14:16:44 -08004876 }
4877 }
4878}
4879
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880/*
4881 * Attempt to hand out a delegation.
J. Bruce Fields99c41512013-05-21 16:21:25 -04004882 *
4883 * Note we don't support write delegations, and won't until the vfs has
4884 * proper support for them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 */
4886static void
Jeff Layton4cf59222014-07-25 07:34:24 -04004887nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
4888 struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889{
4890 struct nfs4_delegation *dp;
Jeff Layton4cf59222014-07-25 07:34:24 -04004891 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
4892 struct nfs4_client *clp = stp->st_stid.sc_client;
J. Bruce Fields14a24e92010-12-10 19:02:49 -05004893 int cb_up;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004894 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04004896 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
NeilBrown7b190fe2005-06-23 22:03:23 -07004897 open->op_recall = 0;
4898 switch (open->op_claim_type) {
4899 case NFS4_OPEN_CLAIM_PREVIOUS:
J. Bruce Fields2bf23872010-03-08 12:37:27 -05004900 if (!cb_up)
NeilBrown7b190fe2005-06-23 22:03:23 -07004901 open->op_recall = 1;
J. Bruce Fields99c41512013-05-21 16:21:25 -04004902 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
4903 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004904 break;
4905 case NFS4_OPEN_CLAIM_NULL:
Ming Chened47b062014-01-09 21:26:10 +00004906 case NFS4_OPEN_CLAIM_FH:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004907 /*
4908 * Let's not give out any delegations till everyone's
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04004909 * had the chance to reclaim theirs, *and* until
4910 * NLM locks have all been reclaimed:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004911 */
Jeff Layton4cf59222014-07-25 07:34:24 -04004912 if (locks_in_grace(clp->net))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004913 goto out_no_deleg;
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04004914 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
J. Bruce Fields99c41512013-05-21 16:21:25 -04004915 goto out_no_deleg;
Steve Dickson9a0590a2013-05-15 14:51:49 -04004916 /*
4917 * Also, if the file was opened for write or
4918 * create, there's a good chance the client's
4919 * about to write to it, resulting in an
4920 * immediate recall (since we don't support
4921 * write delegations):
4922 */
NeilBrown7b190fe2005-06-23 22:03:23 -07004923 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
J. Bruce Fields99c41512013-05-21 16:21:25 -04004924 goto out_no_deleg;
4925 if (open->op_create == NFS4_OPEN_CREATE)
4926 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004927 break;
4928 default:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004929 goto out_no_deleg;
NeilBrown7b190fe2005-06-23 22:03:23 -07004930 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02004931 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
Jeff Layton0b266932014-07-25 07:34:25 -04004932 if (IS_ERR(dp))
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004933 goto out_no_deleg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004935 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
Benny Halevy8c10cbd2009-10-19 12:04:53 +02004937 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -04004938 STATEID_VAL(&dp->dl_stid.sc_stateid));
J. Bruce Fields99c41512013-05-21 16:21:25 -04004939 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
Trond Myklebust67cb1272014-07-29 21:34:17 -04004940 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004941 return;
J. Bruce Fieldsdd239cc2011-01-31 17:14:55 -05004942out_no_deleg:
J. Bruce Fields99c41512013-05-21 16:21:25 -04004943 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
4944 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004945 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
J. Bruce Fields99c41512013-05-21 16:21:25 -04004946 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
J. Bruce Fieldsd08d32e2013-06-21 11:05:32 -04004947 open->op_recall = 1;
4948 }
J. Bruce Fields99c41512013-05-21 16:21:25 -04004949
4950 /* 4.1 client asking for a delegation? */
4951 if (open->op_deleg_want)
4952 nfsd4_open_deleg_none_ext(open, status);
4953 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
4955
Benny Halevye27f49c2012-02-21 14:16:54 -08004956static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4957 struct nfs4_delegation *dp)
4958{
4959 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4960 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4961 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4962 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4963 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4964 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4965 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4966 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4967 }
4968 /* Otherwise the client must be confused wanting a delegation
4969 * it already has, therefore we don't return
4970 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4971 */
4972}
4973
Al Virob37ad282006-10-19 23:28:59 -07004974__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4976{
Andy Adamson66689582009-04-03 08:28:45 +03004977 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04004978 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 struct nfs4_file *fp = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04004980 struct nfs4_ol_stateid *stp = NULL;
NeilBrown567d9822005-06-23 22:02:53 -07004981 struct nfs4_delegation *dp = NULL;
Al Virob37ad282006-10-19 23:28:59 -07004982 __be32 status;
Trond Myklebustd8a1a002017-11-03 08:00:11 -04004983 bool new_stp = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 /*
4986 * Lookup file; if found, lookup stateid and check open request,
4987 * and check for delegations in the process of being recalled.
4988 * If not found, create the nfs4_file struct
4989 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04004990 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
Trond Myklebust950e0112014-06-30 11:48:31 -04004991 if (fp != open->op_file) {
J. Bruce Fields41d22662013-03-21 15:49:47 -04004992 status = nfs4_check_deleg(cl, open, &dp);
NeilBrownc44c5ee2005-06-23 22:02:54 -07004993 if (status)
4994 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04004995 stp = nfsd4_find_and_lock_existing_open(fp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 } else {
Trond Myklebust950e0112014-06-30 11:48:31 -04004997 open->op_file = NULL;
NeilBrownc44c5ee2005-06-23 22:02:54 -07004998 status = nfserr_bad_stateid;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04004999 if (nfsd4_is_deleg_cur(open))
NeilBrownc44c5ee2005-06-23 22:02:54 -07005000 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 }
5002
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005003 if (!stp) {
5004 stp = init_open_stateid(fp, open);
5005 if (!open->op_stp)
5006 new_stp = true;
5007 }
5008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 /*
5010 * OPEN the file, or upgrade an existing OPEN.
5011 * If truncate fails, the OPEN fails.
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005012 *
5013 * stp is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 */
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005015 if (!new_stp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 /* Stateid was found, this is an OPEN upgrade */
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04005017 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005018 if (status) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005019 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 goto out;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 } else {
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005023 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5024 if (status) {
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005025 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005026 release_open_stateid(stp);
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005027 mutex_unlock(&stp->st_mutex);
Jeff Layton6eb3a1d2014-07-10 14:07:31 -04005028 goto out;
5029 }
Sachin Bhamare8287f002015-04-27 14:50:14 +02005030
5031 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5032 open->op_odstate);
5033 if (stp->st_clnt_odstate == open->op_odstate)
5034 open->op_odstate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 }
Trond Myklebustd8a1a002017-11-03 08:00:11 -04005036
Jeff Layton9767feb2015-10-01 09:05:50 -04005037 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005038 mutex_unlock(&stp->st_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039
Benny Halevyd24433c2012-02-16 20:57:17 +02005040 if (nfsd4_has_session(&resp->cstate)) {
Benny Halevyd24433c2012-02-16 20:57:17 +02005041 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5042 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5043 open->op_why_no_deleg = WND4_NOT_WANTED;
5044 goto nodeleg;
5045 }
5046 }
5047
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 /*
5049 * Attempt to hand out a delegation. No error return, because the
5050 * OPEN succeeds even if we fail.
5051 */
Jeff Layton4cf59222014-07-25 07:34:24 -04005052 nfs4_open_delegation(current_fh, open, stp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005053nodeleg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 status = nfs_ok;
5055
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005056 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005057 STATEID_VAL(&stp->st_stid.sc_stateid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058out:
Benny Halevyd24433c2012-02-16 20:57:17 +02005059 /* 4.1 client trying to upgrade/downgrade delegation? */
5060 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
Benny Halevye27f49c2012-02-21 14:16:54 -08005061 open->op_deleg_want)
5062 nfsd4_deleg_xgrade_none_ext(open, dp);
Benny Halevyd24433c2012-02-16 20:57:17 +02005063
NeilBrown13cd2182005-06-23 22:03:10 -07005064 if (fp)
5065 put_nfs4_file(fp);
NeilBrown37515172005-07-07 17:59:16 -07005066 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
Kinglong Mee87186022015-07-13 17:32:59 +08005067 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 /*
5069 * To finish the open response, we just need to set the rflags.
5070 */
5071 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
Jeff Layton19e4c342016-09-16 16:28:27 -04005072 if (nfsd4_has_session(&resp->cstate))
5073 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5074 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
Jeff Layton19e4c342016-09-16 16:28:27 -04005076
Trond Myklebustdcd94cc2014-07-29 21:34:18 -04005077 if (dp)
5078 nfs4_put_stid(&dp->dl_stid);
Trond Myklebustd6f2bc52014-07-29 21:34:19 -04005079 if (stp)
5080 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
5082 return status;
5083}
5084
Jeff Layton58fb12e2014-07-29 21:34:27 -04005085void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
Jeff Layton42297892015-03-23 10:53:44 -04005086 struct nfsd4_open *open)
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005087{
5088 if (open->op_openowner) {
Jeff Laytond3134b12014-07-29 21:34:32 -04005089 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005090
Jeff Laytond3134b12014-07-29 21:34:32 -04005091 nfsd4_cstate_assign_replay(cstate, so);
5092 nfs4_put_stateowner(so);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005093 }
J. Bruce Fields32513b42011-10-13 16:00:16 -04005094 if (open->op_file)
Jeff Layton5b095e92014-10-23 08:01:02 -04005095 kmem_cache_free(file_slab, open->op_file);
J. Bruce Fields4cdc9512011-10-17 15:57:47 -04005096 if (open->op_stp)
Trond Myklebust60116952014-07-29 21:34:06 -04005097 nfs4_put_stid(&open->op_stp->st_stid);
Sachin Bhamare8287f002015-04-27 14:50:14 +02005098 if (open->op_odstate)
5099 kmem_cache_free(odstate_slab, open->op_odstate);
J. Bruce Fieldsd29b20c2011-10-13 15:12:59 -04005100}
5101
Al Virob37ad282006-10-19 23:28:59 -07005102__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08005103nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005104 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005106 clientid_t *clid = &u->renew;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 struct nfs4_client *clp;
Al Virob37ad282006-10-19 23:28:59 -07005108 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03005109 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 dprintk("process_renew(%08x/%08x): starting\n",
5112 clid->cl_boot, clid->cl_id);
Jeff Layton4b24ca72014-06-30 11:48:44 -04005113 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05005114 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 goto out;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005116 clp = cstate->clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 status = nfserr_cb_path_down;
NeilBrownea1da632005-06-23 22:04:17 -07005118 if (!list_empty(&clp->cl_delegations)
J. Bruce Fields77a35692010-04-30 18:51:44 -04005119 && clp->cl_cb_state != NFSD4_CB_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 goto out;
5121 status = nfs_ok;
5122out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 return status;
5124}
5125
Jeff Layton7f5ef2e2014-09-12 16:40:21 -04005126void
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005127nfsd4_end_grace(struct nfsd_net *nn)
NeilBrowna76b4312005-06-23 22:04:01 -07005128{
Jeff Layton33dcc482012-04-10 11:08:48 -04005129 /* do nothing if grace period already ended */
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005130 if (nn->grace_ended)
Jeff Layton33dcc482012-04-10 11:08:48 -04005131 return;
5132
Stanislav Kinsburskya51c84e2012-07-25 16:57:37 +04005133 nn->grace_ended = true;
J. Bruce Fields70b28232014-09-16 17:37:32 -04005134 /*
5135 * If the server goes down again right now, an NFSv4
5136 * client will still be allowed to reclaim after it comes back up,
5137 * even if it hasn't yet had a chance to reclaim state this time.
5138 *
5139 */
Jeff Layton919b8042014-09-12 16:40:20 -04005140 nfsd4_record_grace_done(nn);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005141 /*
5142 * At this point, NFSv4 clients can still reclaim. But if the
5143 * server crashes, any that have not yet reclaimed will be out
5144 * of luck on the next boot.
5145 *
5146 * (NFSv4.1+ clients are considered to have reclaimed once they
5147 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5148 * have reclaimed after their first OPEN.)
5149 */
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04005150 locks_end_grace(&nn->nfsd4_manager);
J. Bruce Fields70b28232014-09-16 17:37:32 -04005151 /*
5152 * At this point, and once lockd and/or any other containers
5153 * exit their grace period, further reclaims will fail and
5154 * regular locking can resume.
5155 */
NeilBrowna76b4312005-06-23 22:04:01 -07005156}
5157
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005158/*
5159 * If we've waited a lease period but there are still clients trying to
5160 * reclaim, wait a little longer to give them a chance to finish.
5161 */
5162static bool clients_still_reclaiming(struct nfsd_net *nn)
5163{
5164 unsigned long now = get_seconds();
5165 unsigned long double_grace_period_end = nn->boot_time +
5166 2 * nn->nfsd4_lease;
5167
Scott Mayhew362063a2019-03-26 18:06:28 -04005168 if (nn->track_reclaim_completes &&
5169 atomic_read(&nn->nr_reclaim_complete) ==
5170 nn->reclaim_str_hashtbl_size)
5171 return false;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005172 if (!nn->somebody_reclaimed)
5173 return false;
5174 nn->somebody_reclaimed = false;
5175 /*
5176 * If we've given them *two* lease times to reclaim, and they're
5177 * still not done, give up:
5178 */
5179 if (time_after(now, double_grace_period_end))
5180 return false;
5181 return true;
5182}
5183
NeilBrownfd39ca92005-06-23 22:04:03 -07005184static time_t
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005185nfs4_laundromat(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186{
5187 struct nfs4_client *clp;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005188 struct nfs4_openowner *oo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 struct nfs4_delegation *dp;
Jeff Layton217526e2014-07-30 08:27:11 -04005190 struct nfs4_ol_stateid *stp;
Jeff Layton7919d0a2016-09-16 16:28:25 -04005191 struct nfsd4_blocked_lock *nbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03005193 time_t cutoff = get_seconds() - nn->nfsd4_lease;
Jeff Laytona832e7a2014-05-30 09:09:26 -04005194 time_t t, new_timeo = nn->nfsd4_lease;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 dprintk("NFSD: laundromat service - starting\n");
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005197
5198 if (clients_still_reclaiming(nn)) {
5199 new_timeo = 0;
5200 goto out;
5201 }
Scott Mayhew362063a2019-03-26 18:06:28 -04005202 dprintk("NFSD: end of grace period\n");
Stanislav Kinsbursky12760c62012-11-14 18:22:12 +03005203 nfsd4_end_grace(nn);
Benny Halevy36acb662010-05-12 00:13:04 +03005204 INIT_LIST_HEAD(&reaplist);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005205 spin_lock(&nn->client_lock);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03005206 list_for_each_safe(pos, next, &nn->client_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 clp = list_entry(pos, struct nfs4_client, cl_lru);
5208 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
5209 t = clp->cl_time - cutoff;
Jeff Laytona832e7a2014-05-30 09:09:26 -04005210 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 break;
5212 }
J. Bruce Fields221a6872013-04-01 22:23:49 -04005213 if (mark_client_expired_locked(clp)) {
Benny Halevyd7682982010-05-12 00:13:54 +03005214 dprintk("NFSD: client in use (clientid %08x)\n",
5215 clp->cl_clientid.cl_id);
5216 continue;
5217 }
Trond Myklebust4864af92014-07-30 08:27:03 -04005218 list_add(&clp->cl_lru, &reaplist);
Benny Halevy36acb662010-05-12 00:13:04 +03005219 }
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03005220 spin_unlock(&nn->client_lock);
Benny Halevy36acb662010-05-12 00:13:04 +03005221 list_for_each_safe(pos, next, &reaplist) {
5222 clp = list_entry(pos, struct nfs4_client, cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 dprintk("NFSD: purging unused client (clientid %08x)\n",
5224 clp->cl_clientid.cl_id);
Trond Myklebust4864af92014-07-30 08:27:03 -04005225 list_del_init(&clp->cl_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 expire_client(clp);
5227 }
Benny Halevycdc975052014-05-30 09:09:30 -04005228 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04005229 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5231 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005232 t = dp->dl_time - cutoff;
5233 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 break;
5235 }
Jeff Layton3fcbbd22015-08-24 12:41:48 -04005236 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04005237 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 }
Benny Halevycdc975052014-05-30 09:09:30 -04005239 spin_unlock(&state_lock);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005240 while (!list_empty(&reaplist)) {
5241 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5242 dl_recall_lru);
5243 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005244 revoke_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 }
Jeff Layton217526e2014-07-30 08:27:11 -04005246
5247 spin_lock(&nn->client_lock);
5248 while (!list_empty(&nn->close_lru)) {
5249 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5250 oo_close_lru);
5251 if (time_after((unsigned long)oo->oo_time,
5252 (unsigned long)cutoff)) {
Jeff Laytona832e7a2014-05-30 09:09:26 -04005253 t = oo->oo_time - cutoff;
5254 new_timeo = min(new_timeo, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 break;
5256 }
Jeff Layton217526e2014-07-30 08:27:11 -04005257 list_del_init(&oo->oo_close_lru);
5258 stp = oo->oo_last_closed_stid;
5259 oo->oo_last_closed_stid = NULL;
5260 spin_unlock(&nn->client_lock);
5261 nfs4_put_stid(&stp->st_stid);
5262 spin_lock(&nn->client_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 }
Jeff Layton217526e2014-07-30 08:27:11 -04005264 spin_unlock(&nn->client_lock);
5265
Jeff Layton7919d0a2016-09-16 16:28:25 -04005266 /*
5267 * It's possible for a client to try and acquire an already held lock
5268 * that is being held for a long time, and then lose interest in it.
5269 * So, we clean out any un-revisited request after a lease period
5270 * under the assumption that the client is no longer interested.
5271 *
5272 * RFC5661, sec. 9.6 states that the client must not rely on getting
5273 * notifications and must continue to poll for locks, even when the
5274 * server supports them. Thus this shouldn't lead to clients blocking
5275 * indefinitely once the lock does become free.
5276 */
5277 BUG_ON(!list_empty(&reaplist));
Jeff Layton0cc11a62016-10-20 09:34:31 -04005278 spin_lock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005279 while (!list_empty(&nn->blocked_locks_lru)) {
5280 nbl = list_first_entry(&nn->blocked_locks_lru,
5281 struct nfsd4_blocked_lock, nbl_lru);
5282 if (time_after((unsigned long)nbl->nbl_time,
5283 (unsigned long)cutoff)) {
5284 t = nbl->nbl_time - cutoff;
5285 new_timeo = min(new_timeo, t);
5286 break;
5287 }
5288 list_move(&nbl->nbl_lru, &reaplist);
5289 list_del_init(&nbl->nbl_list);
5290 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04005291 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005292
5293 while (!list_empty(&reaplist)) {
Naofumi Honda64ebe1242017-11-09 10:57:16 -05005294 nbl = list_first_entry(&reaplist,
Jeff Layton7919d0a2016-09-16 16:28:25 -04005295 struct nfsd4_blocked_lock, nbl_lru);
5296 list_del_init(&nbl->nbl_lru);
Jeff Layton7919d0a2016-09-16 16:28:25 -04005297 free_blocked_lock(nbl);
5298 }
J. Bruce Fields03f318c2018-06-08 12:28:47 -04005299out:
Jeff Laytona832e7a2014-05-30 09:09:26 -04005300 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
Jeff Laytona832e7a2014-05-30 09:09:26 -04005301 return new_timeo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302}
5303
Harvey Harrisona254b242008-02-20 12:49:00 -08005304static struct workqueue_struct *laundry_wq;
5305static void laundromat_main(struct work_struct *);
Harvey Harrisona254b242008-02-20 12:49:00 -08005306
5307static void
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005308laundromat_main(struct work_struct *laundry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309{
5310 time_t t;
Geliang Tang2e55f3a2016-01-01 22:06:28 +08005311 struct delayed_work *dwork = to_delayed_work(laundry);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005312 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5313 laundromat_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005315 t = nfs4_laundromat(nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03005317 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318}
5319
Jeff Layton8fcd4612015-07-30 06:57:46 -04005320static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
NeilBrownf8816512005-07-07 17:59:25 -07005321{
Jeff Layton8fcd4612015-07-30 06:57:46 -04005322 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005323 return nfserr_bad_stateid;
5324 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325}
5326
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005328access_permit_read(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005330 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5331 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5332 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333}
5334
5335static inline int
Jeff Layton82c5ff12012-05-11 09:45:13 -04005336access_permit_write(struct nfs4_ol_stateid *stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005338 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5339 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340}
5341
5342static
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005343__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344{
Al Virob37ad282006-10-19 23:28:59 -07005345 __be32 status = nfserr_openmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346
J. Bruce Fields02921912010-07-29 15:16:59 -04005347 /* For lock stateid's, we test the parent open, not the lock: */
5348 if (stp->st_openstp)
5349 stp = stp->st_openstp;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005350 if ((flags & WR_STATE) && !access_permit_write(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 goto out;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005352 if ((flags & RD_STATE) && !access_permit_read(stp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 goto out;
5354 status = nfs_ok;
5355out:
5356 return status;
5357}
5358
Al Virob37ad282006-10-19 23:28:59 -07005359static inline __be32
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005360check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361{
J. Bruce Fields203a8c82009-02-21 13:29:14 -08005362 if (ONE_STATEID(stateid) && (flags & RD_STATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 return nfs_ok;
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005364 else if (opens_in_grace(net)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005365 /* Answer in remaining cases depends on existence of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 * conflicting state; so we must wait out the grace period. */
5367 return nfserr_grace;
5368 } else if (flags & WR_STATE)
5369 return nfs4_share_conflict(current_fh,
5370 NFS4_SHARE_DENY_WRITE);
5371 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5372 return nfs4_share_conflict(current_fh,
5373 NFS4_SHARE_DENY_READ);
5374}
5375
5376/*
5377 * Allow READ/WRITE during grace period on recovered state only for files
5378 * that are not able to provide mandatory locking.
5379 */
5380static inline int
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005381grace_disallows_io(struct net *net, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382{
J. Bruce Fieldsc87fb4a2015-08-06 12:47:02 -04005383 return opens_in_grace(net) && mandatory_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384}
5385
J. Bruce Fields57b7b432012-04-25 17:58:50 -04005386static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
J. Bruce Fields0836f582008-01-26 19:08:12 -05005387{
Andy Adamson66689582009-04-03 08:28:45 +03005388 /*
5389 * When sessions are used the stateid generation number is ignored
5390 * when it is zero.
5391 */
J. Bruce Fields28dde242011-08-22 10:07:12 -04005392 if (has_session && in->si_generation == 0)
J. Bruce Fields81b82962011-08-23 11:03:29 -04005393 return nfs_ok;
5394
5395 if (in->si_generation == ref->si_generation)
5396 return nfs_ok;
Andy Adamson66689582009-04-03 08:28:45 +03005397
J. Bruce Fields0836f582008-01-26 19:08:12 -05005398 /* If the client sends us a stateid from the future, it's buggy: */
Jeff Layton14b7f4a2016-05-05 06:53:47 -04005399 if (nfsd4_stateid_generation_after(in, ref))
J. Bruce Fields0836f582008-01-26 19:08:12 -05005400 return nfserr_bad_stateid;
5401 /*
J. Bruce Fields81b82962011-08-23 11:03:29 -04005402 * However, we could see a stateid from the past, even from a
5403 * non-buggy client. For example, if the client sends a lock
5404 * while some IO is outstanding, the lock may bump si_generation
5405 * while the IO is still in flight. The client could avoid that
5406 * situation by waiting for responses on all the IO requests,
5407 * but better performance may result in retrying IO that
5408 * receives an old_stateid error if requests are rarely
5409 * reordered in flight:
J. Bruce Fields0836f582008-01-26 19:08:12 -05005410 */
J. Bruce Fields81b82962011-08-23 11:03:29 -04005411 return nfserr_old_stateid;
J. Bruce Fields0836f582008-01-26 19:08:12 -05005412}
5413
Trond Myklebust03da3162017-11-03 08:00:16 -04005414static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5415{
5416 __be32 ret;
5417
5418 spin_lock(&s->sc_lock);
5419 ret = nfsd4_verify_open_stid(s);
5420 if (ret == nfs_ok)
5421 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5422 spin_unlock(&s->sc_lock);
5423 return ret;
5424}
5425
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005426static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5427{
5428 if (ols->st_stateowner->so_is_open_owner &&
5429 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5430 return nfserr_bad_stateid;
5431 return nfs_ok;
5432}
5433
Chuck Lever7df302f2012-05-29 13:56:37 -04005434static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
Bryan Schumaker17456802011-07-13 10:50:48 -04005435{
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005436 struct nfs4_stid *s;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005437 __be32 status = nfserr_bad_stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04005438
Andrew Elbleae254dac2017-11-09 13:41:10 -05005439 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5440 CLOSE_STATEID(stateid))
Jeff Layton1af71cc2014-07-29 21:34:14 -04005441 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005442 /* Client debugging aid. */
5443 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
5444 char addr_str[INET6_ADDRSTRLEN];
5445 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
5446 sizeof(addr_str));
5447 pr_warn_ratelimited("NFSD: client %s testing state ID "
5448 "with incorrect client ID\n", addr_str);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005449 return status;
Chuck Lever7df302f2012-05-29 13:56:37 -04005450 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005451 spin_lock(&cl->cl_lock);
5452 s = find_stateid_locked(cl, stateid);
J. Bruce Fields97b7e3b2011-09-09 11:26:58 -04005453 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005454 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005455 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
Bryan Schumaker17456802011-07-13 10:50:48 -04005456 if (status)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005457 goto out_unlock;
J. Bruce Fields23340032013-04-09 17:42:28 -04005458 switch (s->sc_type) {
5459 case NFS4_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005460 status = nfs_ok;
5461 break;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005462 case NFS4_REVOKED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005463 status = nfserr_deleg_revoked;
5464 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005465 case NFS4_OPEN_STID:
5466 case NFS4_LOCK_STID:
Christoph Hellwigebe9cb32015-04-28 15:41:15 +02005467 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
Jeff Layton1af71cc2014-07-29 21:34:14 -04005468 break;
J. Bruce Fields23340032013-04-09 17:42:28 -04005469 default:
5470 printk("unknown stateid type %x\n", s->sc_type);
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005471 /* Fallthrough */
J. Bruce Fields23340032013-04-09 17:42:28 -04005472 case NFS4_CLOSED_STID:
Trond Myklebustb0fc29d2014-07-16 10:31:59 -04005473 case NFS4_CLOSED_DELEG_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005474 status = nfserr_bad_stateid;
J. Bruce Fields23340032013-04-09 17:42:28 -04005475 }
Jeff Layton1af71cc2014-07-29 21:34:14 -04005476out_unlock:
5477 spin_unlock(&cl->cl_lock);
5478 return status;
Bryan Schumaker17456802011-07-13 10:50:48 -04005479}
5480
Christoph Hellwigcd61c522014-08-14 08:44:57 +02005481__be32
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005482nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5483 stateid_t *stateid, unsigned char typemask,
5484 struct nfs4_stid **s, struct nfsd_net *nn)
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005485{
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005486 __be32 status;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005487 bool return_revoked = false;
5488
5489 /*
5490 * only return revoked delegations if explicitly asked.
5491 * otherwise we report revoked or bad_stateid status.
5492 */
5493 if (typemask & NFS4_REVOKED_DELEG_STID)
5494 return_revoked = true;
5495 else if (typemask & NFS4_DELEG_STID)
5496 typemask |= NFS4_REVOKED_DELEG_STID;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005497
Andrew Elbleae254dac2017-11-09 13:41:10 -05005498 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5499 CLOSE_STATEID(stateid))
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005500 return nfserr_bad_stateid;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005501 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005502 if (status == nfserr_stale_clientid) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04005503 if (cstate->session)
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005504 return nfserr_bad_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005505 return nfserr_stale_stateid;
Trond Myklebusta8a7c672014-03-29 14:43:38 -04005506 }
J. Bruce Fields0eb6f202013-03-12 17:36:17 -04005507 if (status)
5508 return status;
Jeff Layton4b24ca72014-06-30 11:48:44 -04005509 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005510 if (!*s)
5511 return nfserr_bad_stateid;
Andrew Elble95da1b3a2017-11-03 14:06:31 -04005512 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5513 nfs4_put_stid(*s);
5514 if (cstate->minorversion)
5515 return nfserr_deleg_revoked;
5516 return nfserr_bad_stateid;
5517 }
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005518 return nfs_ok;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005519}
5520
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005521static struct nfsd_file *
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005522nfs4_find_file(struct nfs4_stid *s, int flags)
5523{
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005524 if (!s)
5525 return NULL;
5526
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005527 switch (s->sc_type) {
5528 case NFS4_DELEG_STID:
5529 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5530 return NULL;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005531 return nfsd_file_get(s->sc_file->fi_deleg_file);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005532 case NFS4_OPEN_STID:
5533 case NFS4_LOCK_STID:
5534 if (flags & RD_STATE)
5535 return find_readable_file(s->sc_file);
5536 else
5537 return find_writeable_file(s->sc_file);
5538 break;
5539 }
5540
5541 return NULL;
5542}
5543
5544static __be32
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005545nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005546{
5547 __be32 status;
5548
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005549 status = nfsd4_check_openowner_confirmed(ols);
5550 if (status)
5551 return status;
5552 return nfs4_check_openmode(ols, flags);
5553}
5554
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005555static __be32
5556nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
Jeff Layton5c4583b2019-08-18 14:18:54 -04005557 struct nfsd_file **nfp, int flags)
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005558{
5559 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005560 struct nfsd_file *nf;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005561 __be32 status;
5562
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005563 nf = nfs4_find_file(s, flags);
5564 if (nf) {
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005565 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5566 acc | NFSD_MAY_OWNER_OVERRIDE);
Jeff Layton5c4583b2019-08-18 14:18:54 -04005567 if (status) {
5568 nfsd_file_put(nf);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005569 goto out;
Jeff Layton5c4583b2019-08-18 14:18:54 -04005570 }
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005571 } else {
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005572 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005573 if (status)
5574 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005575 }
Jeff Layton5c4583b2019-08-18 14:18:54 -04005576 *nfp = nf;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005577out:
Jeff Laytoneb82dd32019-08-18 14:18:53 -04005578 return status;
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005579}
5580
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581/*
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005582 * Checks for stateid operations
5583 */
Al Virob37ad282006-10-19 23:28:59 -07005584__be32
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005585nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
Anna Schumakeraa0d6ae2015-12-03 12:59:51 +01005586 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
Jeff Layton5c4583b2019-08-18 14:18:54 -04005587 stateid_t *stateid, int flags, struct nfsd_file **nfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588{
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005589 struct inode *ino = d_inode(fhp->fh_dentry);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005590 struct net *net = SVC_NET(rqstp);
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005591 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005592 struct nfs4_stid *s = NULL;
Al Virob37ad282006-10-19 23:28:59 -07005593 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
Jeff Layton5c4583b2019-08-18 14:18:54 -04005595 if (nfp)
5596 *nfp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04005598 if (grace_disallows_io(net, ino))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 return nfserr_grace;
5600
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005601 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5602 status = check_special_stateids(net, fhp, stateid, flags);
5603 goto done;
5604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005606 status = nfsd4_lookup_stateid(cstate, stateid,
Jeff Laytondb24b3b2014-06-30 11:48:36 -04005607 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005608 &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005609 if (status)
Trond Myklebustc2d1d6a2014-07-30 08:27:25 -04005610 return status;
Trond Myklebust03da3162017-11-03 08:00:16 -04005611 status = nfsd4_stid_check_stateid_generation(stateid, s,
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005612 nfsd4_has_session(cstate));
J. Bruce Fields69064a22011-09-09 11:54:57 -04005613 if (status)
5614 goto out;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005615
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005616 switch (s->sc_type) {
5617 case NFS4_DELEG_STID:
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005618 status = nfs4_check_delegmode(delegstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005619 break;
5620 case NFS4_OPEN_STID:
5621 case NFS4_LOCK_STID:
J. Bruce Fieldsd8836f72018-11-05 20:04:06 -05005622 status = nfs4_check_olstateid(openlockstateid(s), flags);
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005623 break;
5624 default:
Trond Myklebust14bcab12014-04-18 14:44:07 -04005625 status = nfserr_bad_stateid;
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005626 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 }
Jeff Layton8fcd4612015-07-30 06:57:46 -04005628 if (status)
5629 goto out;
5630 status = nfs4_check_fh(fhp, s);
Christoph Hellwiga0649b22015-06-18 16:44:59 +02005631
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005632done:
Jeff Layton5c4583b2019-08-18 14:18:54 -04005633 if (status == nfs_ok && nfp)
5634 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635out:
Christoph Hellwigaf90f702015-06-18 16:45:00 +02005636 if (s)
5637 nfs4_put_stid(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 return status;
5639}
5640
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005641/*
Bryan Schumaker17456802011-07-13 10:50:48 -04005642 * Test if the stateid is valid
5643 */
5644__be32
5645nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005646 union nfsd4_op_u *u)
Bryan Schumaker17456802011-07-13 10:50:48 -04005647{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005648 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005649 struct nfsd4_test_stateid_id *stateid;
5650 struct nfs4_client *cl = cstate->session->se_client;
5651
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005652 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
Chuck Lever7df302f2012-05-29 13:56:37 -04005653 stateid->ts_id_status =
5654 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05005655
Bryan Schumaker17456802011-07-13 10:50:48 -04005656 return nfs_ok;
5657}
5658
Chuck Lever42691392016-08-11 10:37:30 -04005659static __be32
5660nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5661{
5662 struct nfs4_ol_stateid *stp = openlockstateid(s);
5663 __be32 ret;
5664
Trond Myklebust659aefb2017-11-03 08:00:13 -04005665 ret = nfsd4_lock_ol_stateid(stp);
5666 if (ret)
5667 goto out_put_stid;
Chuck Lever42691392016-08-11 10:37:30 -04005668
5669 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5670 if (ret)
5671 goto out;
5672
5673 ret = nfserr_locks_held;
5674 if (check_for_locks(stp->st_stid.sc_file,
5675 lockowner(stp->st_stateowner)))
5676 goto out;
5677
5678 release_lock_stateid(stp);
5679 ret = nfs_ok;
5680
5681out:
5682 mutex_unlock(&stp->st_mutex);
Trond Myklebust659aefb2017-11-03 08:00:13 -04005683out_put_stid:
Chuck Lever42691392016-08-11 10:37:30 -04005684 nfs4_put_stid(s);
5685 return ret;
5686}
5687
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005688__be32
5689nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005690 union nfsd4_op_u *u)
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005691{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005692 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005693 stateid_t *stateid = &free_stateid->fr_stateid;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005694 struct nfs4_stid *s;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005695 struct nfs4_delegation *dp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005696 struct nfs4_client *cl = cstate->session->se_client;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005697 __be32 ret = nfserr_bad_stateid;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005698
Jeff Layton1af71cc2014-07-29 21:34:14 -04005699 spin_lock(&cl->cl_lock);
5700 s = find_stateid_locked(cl, stateid);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005701 if (!s)
Jeff Layton1af71cc2014-07-29 21:34:14 -04005702 goto out_unlock;
Trond Myklebust03da3162017-11-03 08:00:16 -04005703 spin_lock(&s->sc_lock);
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005704 switch (s->sc_type) {
5705 case NFS4_DELEG_STID:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005706 ret = nfserr_locks_held;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005707 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005708 case NFS4_OPEN_STID:
Jeff Layton1af71cc2014-07-29 21:34:14 -04005709 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5710 if (ret)
5711 break;
5712 ret = nfserr_locks_held;
5713 break;
J. Bruce Fields2da1cec2011-09-16 18:56:20 -04005714 case NFS4_LOCK_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005715 spin_unlock(&s->sc_lock);
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03005716 refcount_inc(&s->sc_count);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005717 spin_unlock(&cl->cl_lock);
Chuck Lever42691392016-08-11 10:37:30 -04005718 ret = nfsd4_free_lock_stateid(stateid, s);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005719 goto out;
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005720 case NFS4_REVOKED_DELEG_STID:
Trond Myklebust03da3162017-11-03 08:00:16 -04005721 spin_unlock(&s->sc_lock);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005722 dp = delegstateid(s);
Jeff Layton2d4a5322014-07-25 07:34:21 -04005723 list_del_init(&dp->dl_recall_lru);
5724 spin_unlock(&cl->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -04005725 nfs4_put_stid(s);
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04005726 ret = nfs_ok;
Jeff Layton1af71cc2014-07-29 21:34:14 -04005727 goto out;
5728 /* Default falls through and returns nfserr_bad_stateid */
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005729 }
Trond Myklebust03da3162017-11-03 08:00:16 -04005730 spin_unlock(&s->sc_lock);
Jeff Layton1af71cc2014-07-29 21:34:14 -04005731out_unlock:
5732 spin_unlock(&cl->cl_lock);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005733out:
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04005734 return ret;
5735}
5736
NeilBrown4c4cd222005-07-07 17:59:27 -07005737static inline int
5738setlkflg (int type)
5739{
5740 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
5741 RD_STATE : WR_STATE;
5742}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005744static __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 -04005745{
5746 struct svc_fh *current_fh = &cstate->current_fh;
5747 struct nfs4_stateowner *sop = stp->st_stateowner;
5748 __be32 status;
5749
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005750 status = nfsd4_check_seqid(cstate, sop, seqid);
5751 if (status)
5752 return status;
Trond Myklebust9271d7e2017-11-03 08:00:15 -04005753 status = nfsd4_lock_ol_stateid(stp);
5754 if (status != nfs_ok)
5755 return status;
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005756 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
Jeff Layton35a92fe2015-09-17 07:47:08 -04005757 if (status == nfs_ok)
5758 status = nfs4_check_fh(current_fh, &stp->st_stid);
5759 if (status != nfs_ok)
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005760 mutex_unlock(&stp->st_mutex);
Jeff Layton35a92fe2015-09-17 07:47:08 -04005761 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005762}
5763
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764/*
5765 * Checks for sequence id mutating operations.
5766 */
Al Virob37ad282006-10-19 23:28:59 -07005767static __be32
Benny Halevydd453df2009-04-03 08:28:41 +03005768nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
J. Bruce Fields2288d0e2011-09-06 15:50:21 -04005769 stateid_t *stateid, char typemask,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005770 struct nfs4_ol_stateid **stpp,
5771 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772{
J. Bruce Fields0836f582008-01-26 19:08:12 -05005773 __be32 status;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04005774 struct nfs4_stid *s;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005775 struct nfs4_ol_stateid *stp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005777 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
5778 seqid, STATEID_VAL(stateid));
NeilBrown3a4f98b2005-07-07 17:59:26 -07005779
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 *stpp = NULL;
Trond Myklebust2dd6e452014-06-30 11:48:43 -04005781 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005782 if (status)
5783 return status;
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005784 stp = openlockstateid(s);
Jeff Layton58fb12e2014-07-29 21:34:27 -04005785 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005787 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
Trond Myklebustfd911012014-07-29 21:34:24 -04005788 if (!status)
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005789 *stpp = stp;
Trond Myklebustfd911012014-07-29 21:34:24 -04005790 else
5791 nfs4_put_stid(&stp->st_stid);
Trond Myklebuste17f99b2014-06-30 11:48:34 -04005792 return status;
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005793}
J. Bruce Fields39325bd2007-11-26 17:06:39 -05005794
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005795static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
5796 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005797{
5798 __be32 status;
5799 struct nfs4_openowner *oo;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005800 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005802 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005803 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields7a8711c2011-09-02 09:03:37 -04005804 if (status)
5805 return status;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005806 oo = openowner(stp->st_stateowner);
5807 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005808 mutex_unlock(&stp->st_mutex);
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005809 nfs4_put_stid(&stp->st_stid);
NeilBrown3a4f98b2005-07-07 17:59:26 -07005810 return nfserr_bad_stateid;
Trond Myklebust4cbfc9f2014-07-29 21:34:23 -04005811 }
5812 *stpp = stp;
NeilBrown3a4f98b2005-07-07 17:59:26 -07005813 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814}
5815
Al Virob37ad282006-10-19 23:28:59 -07005816__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005817nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005818 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005820 struct nfsd4_open_confirm *oc = &u->open_confirm;
Al Virob37ad282006-10-19 23:28:59 -07005821 __be32 status;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005822 struct nfs4_openowner *oo;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005823 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005824 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
Al Viroa6a9f182013-09-16 10:57:01 -04005826 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5827 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005829 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
J. Bruce Fieldsa8cddc52006-06-30 01:56:13 -07005830 if (status)
5831 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005833 status = nfs4_preprocess_seqid_op(cstate,
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005834 oc->oc_seqid, &oc->oc_req_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005835 NFS4_OPEN_STID, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005836 if (status)
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005837 goto out;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04005838 oo = openowner(stp->st_stateowner);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005839 status = nfserr_bad_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005840 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005841 mutex_unlock(&stp->st_mutex);
Trond Myklebust2585fc72014-07-29 21:34:21 -04005842 goto put_stateid;
Jeff Layton35a92fe2015-09-17 07:47:08 -04005843 }
J. Bruce Fieldsdad1c062011-09-12 12:24:13 -04005844 oo->oo_flags |= NFS4_OO_CONFIRMED;
Jeff Layton9767feb2015-10-01 09:05:50 -04005845 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005846 mutex_unlock(&stp->st_mutex);
Benny Halevy8c10cbd2009-10-19 12:04:53 +02005847 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005848 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
NeilBrownc7b9a452005-06-23 22:04:30 -07005849
Jeff Layton2a4317c2012-03-21 16:42:43 -04005850 nfsd4_client_record_create(oo->oo_owner.so_client);
J. Bruce Fields68b66e82011-09-02 12:19:43 -04005851 status = nfs_ok;
Trond Myklebust2585fc72014-07-29 21:34:21 -04005852put_stateid:
5853 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005855 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 return status;
5857}
5858
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005859static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860{
Jeff Layton82c5ff12012-05-11 09:45:13 -04005861 if (!test_access(access, stp))
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005862 return;
Trond Myklebust11b91642014-07-29 21:34:08 -04005863 nfs4_file_put_access(stp->st_stid.sc_file, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04005864 clear_access(access, stp);
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005865}
J. Bruce Fieldsf197c272011-06-29 08:23:50 -04005866
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005867static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
5868{
5869 switch (to_access) {
5870 case NFS4_SHARE_ACCESS_READ:
5871 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
5872 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5873 break;
5874 case NFS4_SHARE_ACCESS_WRITE:
5875 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
5876 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
5877 break;
5878 case NFS4_SHARE_ACCESS_BOTH:
5879 break;
5880 default:
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05005881 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 }
5883}
5884
Al Virob37ad282006-10-19 23:28:59 -07005885__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005886nfsd4_open_downgrade(struct svc_rqst *rqstp,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005887 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005889 struct nfsd4_open_downgrade *od = &u->open_downgrade;
Al Virob37ad282006-10-19 23:28:59 -07005890 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005891 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005892 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893
Al Viroa6a9f182013-09-16 10:57:01 -04005894 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5895 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
J. Bruce Fieldsc30e92d2011-10-10 17:34:31 -04005897 /* We don't yet support WANT bits: */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02005898 if (od->od_deleg_want)
5899 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
5900 od->od_deleg_want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04005902 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005903 &od->od_stateid, &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005904 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 status = nfserr_inval;
Jeff Layton82c5ff12012-05-11 09:45:13 -04005907 if (!test_access(od->od_share_access, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005908 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 stp->st_access_bmap, od->od_share_access);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005910 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 }
Jeff Laytonce0fc432012-05-11 09:45:14 -04005912 if (!test_deny(od->od_share_deny, stp)) {
Jeff Laytonc11c5912014-07-10 14:07:30 -04005913 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 stp->st_deny_bmap, od->od_share_deny);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005915 goto put_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 }
J. Bruce Fields6409a5a2011-09-28 11:37:56 -04005917 nfs4_stateid_downgrade(stp, od->od_share_access);
Jeff Laytonce0fc432012-05-11 09:45:14 -04005918 reset_union_bmap_deny(od->od_share_deny, stp);
Jeff Layton9767feb2015-10-01 09:05:50 -04005919 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 status = nfs_ok;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005921put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04005922 mutex_unlock(&stp->st_mutex);
Trond Myklebust0667b1e2014-07-29 21:34:22 -04005923 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005925 nfsd4_bump_seqid(cstate, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 return status;
5927}
5928
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005929static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
5930{
Trond Myklebustacf92952014-06-30 11:48:37 -04005931 struct nfs4_client *clp = s->st_stid.sc_client;
Jeff Laytone8568732015-08-24 12:41:47 -04005932 bool unhashed;
Jeff Laytond83017f2014-07-29 21:34:42 -04005933 LIST_HEAD(reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005934
Jeff Layton2c41beb2014-07-29 21:34:41 -04005935 spin_lock(&clp->cl_lock);
Jeff Laytone8568732015-08-24 12:41:47 -04005936 unhashed = unhash_open_stateid(s, &reaplist);
Trond Myklebustacf92952014-06-30 11:48:37 -04005937
Jeff Laytond83017f2014-07-29 21:34:42 -04005938 if (clp->cl_minorversion) {
Jeff Laytone8568732015-08-24 12:41:47 -04005939 if (unhashed)
5940 put_ol_stateid_locked(s, &reaplist);
Jeff Laytond83017f2014-07-29 21:34:42 -04005941 spin_unlock(&clp->cl_lock);
5942 free_ol_stateid_reaplist(&reaplist);
5943 } else {
5944 spin_unlock(&clp->cl_lock);
5945 free_ol_stateid_reaplist(&reaplist);
Jeff Laytone8568732015-08-24 12:41:47 -04005946 if (unhashed)
5947 move_to_close_lru(s, clp->net);
Jeff Laytond83017f2014-07-29 21:34:42 -04005948 }
J. Bruce Fields38c387b2011-09-16 17:42:48 -04005949}
5950
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951/*
5952 * nfs4_unlock_state() called after encode
5953 */
Al Virob37ad282006-10-19 23:28:59 -07005954__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08005955nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02005956 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957{
Christoph Hellwigeb698532017-05-08 20:58:35 +02005958 struct nfsd4_close *close = &u->close;
Al Virob37ad282006-10-19 23:28:59 -07005959 __be32 status;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04005960 struct nfs4_ol_stateid *stp;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005961 struct net *net = SVC_NET(rqstp);
5962 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963
Al Viroa6a9f182013-09-16 10:57:01 -04005964 dprintk("NFSD: nfsd4_close on file %pd\n",
5965 cstate->current_fh.fh_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005967 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
5968 &close->cl_stateid,
5969 NFS4_OPEN_STID|NFS4_CLOSED_STID,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03005970 &stp, nn);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005971 nfsd4_bump_seqid(cstate, status);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04005972 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 goto out;
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005974
5975 stp->st_stid.sc_type = NFS4_CLOSED_STID;
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005976
5977 /*
5978 * Technically we don't _really_ have to increment or copy it, since
5979 * it should just be gone after this operation and we clobber the
5980 * copied value below, but we continue to do so here just to ensure
5981 * that racing ops see that there was a state change.
5982 */
Jeff Layton9767feb2015-10-01 09:05:50 -04005983 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
J. Bruce Fieldsf7a4d872011-09-16 20:12:38 -04005985 nfsd4_close_open_stateid(stp);
Trond Myklebust15ca08d2017-11-03 08:00:10 -04005986 mutex_unlock(&stp->st_mutex);
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005987
Jeff Laytonbd2deca2018-02-15 15:16:18 -05005988 /* v4.1+ suggests that we send a special stateid in here, since the
5989 * clients should just ignore this anyway. Since this is not useful
5990 * for v4.0 clients either, we set it to the special close_stateid
5991 * universally.
5992 *
5993 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
5994 */
5995 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
Trond Myklebustfb500a72017-11-03 08:00:12 -04005996
Trond Myklebust8a0b5892014-07-29 21:34:20 -04005997 /* put reference from nfs4_preprocess_seqid_op */
5998 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 return status;
6001}
6002
Al Virob37ad282006-10-19 23:28:59 -07006003__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006004nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006005 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006007 struct nfsd4_delegreturn *dr = &u->delegreturn;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006008 struct nfs4_delegation *dp;
6009 stateid_t *stateid = &dr->dr_stateid;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006010 struct nfs4_stid *s;
Al Virob37ad282006-10-19 23:28:59 -07006011 __be32 status;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006012 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006014 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006015 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016
Trond Myklebust2dd6e452014-06-30 11:48:43 -04006017 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006018 if (status)
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006019 goto out;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04006020 dp = delegstateid(s);
Trond Myklebust03da3162017-11-03 08:00:16 -04006021 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006022 if (status)
Trond Myklebustfd911012014-07-29 21:34:24 -04006023 goto put_stateid;
J. Bruce Fields203a8c82009-02-21 13:29:14 -08006024
J. Bruce Fields3bd64a52013-04-09 17:02:51 -04006025 destroy_delegation(dp);
Trond Myklebustfd911012014-07-29 21:34:24 -04006026put_stateid:
6027 nfs4_put_stid(&dp->dl_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028out:
6029 return status;
6030}
6031
Benny Halevy87df4de2008-12-15 19:42:03 +02006032static inline u64
6033end_offset(u64 start, u64 len)
6034{
6035 u64 end;
6036
6037 end = start + len;
6038 return end >= start ? end: NFS4_MAX_UINT64;
6039}
6040
6041/* last octet in a range */
6042static inline u64
6043last_byte_offset(u64 start, u64 len)
6044{
6045 u64 end;
6046
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006047 WARN_ON_ONCE(!len);
Benny Halevy87df4de2008-12-15 19:42:03 +02006048 end = start + len;
6049 return end > start ? end - 1: NFS4_MAX_UINT64;
6050}
6051
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052/*
6053 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6054 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6055 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6056 * locking, this prevents us from being completely protocol-compliant. The
6057 * real solution to this problem is to start using unsigned file offsets in
6058 * the VFS, but this is a very deep change!
6059 */
6060static inline void
6061nfs4_transform_lock_offset(struct file_lock *lock)
6062{
6063 if (lock->fl_start < 0)
6064 lock->fl_start = OFFSET_MAX;
6065 if (lock->fl_end < 0)
6066 lock->fl_end = OFFSET_MAX;
6067}
6068
Jeff Laytoncae80b32015-04-03 09:04:04 -04006069static fl_owner_t
6070nfsd4_fl_get_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006071{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006072 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6073
6074 nfs4_get_stateowner(&lo->lo_owner);
6075 return owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006076}
6077
Jeff Laytoncae80b32015-04-03 09:04:04 -04006078static void
6079nfsd4_fl_put_owner(fl_owner_t owner)
Kinglong Meeaef95832014-08-22 10:18:44 -04006080{
Jeff Laytoncae80b32015-04-03 09:04:04 -04006081 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
Kinglong Meeaef95832014-08-22 10:18:44 -04006082
Jeff Laytoncae80b32015-04-03 09:04:04 -04006083 if (lo)
Kinglong Meeaef95832014-08-22 10:18:44 -04006084 nfs4_put_stateowner(&lo->lo_owner);
Kinglong Meeaef95832014-08-22 10:18:44 -04006085}
6086
Jeff Layton76d348f2016-09-16 16:28:24 -04006087static void
6088nfsd4_lm_notify(struct file_lock *fl)
6089{
6090 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6091 struct net *net = lo->lo_owner.so_client->net;
6092 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6093 struct nfsd4_blocked_lock *nbl = container_of(fl,
6094 struct nfsd4_blocked_lock, nbl_lock);
6095 bool queue = false;
6096
Jeff Layton7919d0a2016-09-16 16:28:25 -04006097 /* An empty list means that something else is going to be using it */
Jeff Layton0cc11a62016-10-20 09:34:31 -04006098 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006099 if (!list_empty(&nbl->nbl_list)) {
6100 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006101 list_del_init(&nbl->nbl_lru);
Jeff Layton76d348f2016-09-16 16:28:24 -04006102 queue = true;
6103 }
Jeff Layton0cc11a62016-10-20 09:34:31 -04006104 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006105
6106 if (queue)
6107 nfsd4_run_cb(&nbl->nbl_cb);
6108}
6109
Alexey Dobriyan7b021962009-09-21 17:01:12 -07006110static const struct lock_manager_operations nfsd_posix_mng_ops = {
Jeff Layton76d348f2016-09-16 16:28:24 -04006111 .lm_notify = nfsd4_lm_notify,
Kinglong Meeaef95832014-08-22 10:18:44 -04006112 .lm_get_owner = nfsd4_fl_get_owner,
6113 .lm_put_owner = nfsd4_fl_put_owner,
NeilBrownd5b90262006-04-10 22:55:22 -07006114};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115
6116static inline void
6117nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6118{
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006119 struct nfs4_lockowner *lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
NeilBrownd5b90262006-04-10 22:55:22 -07006121 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006122 lo = (struct nfs4_lockowner *) fl->fl_owner;
J. Bruce Fields6f4859b2019-06-19 14:30:33 -04006123 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6124 GFP_KERNEL);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006125 if (!deny->ld_owner.data)
6126 /* We just don't care that much */
6127 goto nevermind;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006128 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
NeilBrownd5b90262006-04-10 22:55:22 -07006129 } else {
J. Bruce Fields7c13f342011-08-30 22:15:47 -04006130nevermind:
6131 deny->ld_owner.len = 0;
6132 deny->ld_owner.data = NULL;
NeilBrownd5b90262006-04-10 22:55:22 -07006133 deny->ld_clientid.cl_boot = 0;
6134 deny->ld_clientid.cl_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 }
6136 deny->ld_start = fl->fl_start;
Benny Halevy87df4de2008-12-15 19:42:03 +02006137 deny->ld_length = NFS4_MAX_UINT64;
6138 if (fl->fl_end != NFS4_MAX_UINT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6140 deny->ld_type = NFS4_READ_LT;
6141 if (fl->fl_type != F_RDLCK)
6142 deny->ld_type = NFS4_WRITE_LT;
6143}
6144
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006145static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006146find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147{
Trond Myklebustd4f04892014-07-29 21:34:36 -04006148 unsigned int strhashval = ownerstr_hashval(owner);
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006149 struct nfs4_stateowner *so;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150
Trond Myklebust0a880a22014-07-30 08:27:10 -04006151 lockdep_assert_held(&clp->cl_lock);
6152
Trond Myklebustd4f04892014-07-29 21:34:36 -04006153 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6154 so_strhash) {
Trond Myklebustb3c32bc2014-06-30 11:48:40 -04006155 if (so->so_is_open_owner)
6156 continue;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006157 if (same_owner_str(so, owner))
6158 return lockowner(nfs4_get_stateowner(so));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 }
6160 return NULL;
6161}
6162
Trond Myklebustc58c6612014-07-29 21:34:35 -04006163static struct nfs4_lockowner *
Kinglong Meec8623992015-07-13 17:35:37 +08006164find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
Trond Myklebustc58c6612014-07-29 21:34:35 -04006165{
6166 struct nfs4_lockowner *lo;
6167
Trond Myklebustd4f04892014-07-29 21:34:36 -04006168 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006169 lo = find_lockowner_str_locked(clp, owner);
Trond Myklebustd4f04892014-07-29 21:34:36 -04006170 spin_unlock(&clp->cl_lock);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006171 return lo;
6172}
6173
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006174static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6175{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006176 unhash_lockowner_locked(lockowner(sop));
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006177}
6178
Jeff Layton6b180f02014-07-29 21:34:26 -04006179static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6180{
6181 struct nfs4_lockowner *lo = lockowner(sop);
6182
6183 kmem_cache_free(lockowner_slab, lo);
6184}
6185
6186static const struct nfs4_stateowner_operations lockowner_ops = {
Jeff Layton8f4b54c2014-07-29 21:34:29 -04006187 .so_unhash = nfs4_unhash_lockowner,
6188 .so_free = nfs4_free_lockowner,
Jeff Layton6b180f02014-07-29 21:34:26 -04006189};
6190
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191/*
6192 * Alloc a lock owner structure.
6193 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006194 * occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 *
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006196 * strhashval = ownerstr_hashval
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 */
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006198static struct nfs4_lockowner *
Trond Myklebustc58c6612014-07-29 21:34:35 -04006199alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6200 struct nfs4_ol_stateid *open_stp,
6201 struct nfsd4_lock *lock)
6202{
Trond Myklebustc58c6612014-07-29 21:34:35 -04006203 struct nfs4_lockowner *lo, *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006205 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6206 if (!lo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 return NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006208 INIT_LIST_HEAD(&lo->lo_blocked);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006209 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6210 lo->lo_owner.so_is_open_owner = 0;
Jeff Layton5db1c032014-07-29 21:34:28 -04006211 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
Jeff Layton6b180f02014-07-29 21:34:26 -04006212 lo->lo_owner.so_ops = &lockowner_ops;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006213 spin_lock(&clp->cl_lock);
Kinglong Meec8623992015-07-13 17:35:37 +08006214 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006215 if (ret == NULL) {
6216 list_add(&lo->lo_owner.so_strhash,
Trond Myklebustd4f04892014-07-29 21:34:36 -04006217 &clp->cl_ownerstr_hashtbl[strhashval]);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006218 ret = lo;
6219 } else
Kinglong Meed50ffde2015-07-16 12:05:07 +08006220 nfs4_free_stateowner(&lo->lo_owner);
6221
Trond Myklebustd4f04892014-07-29 21:34:36 -04006222 spin_unlock(&clp->cl_lock);
J. Bruce Fields340f0ba2015-03-23 11:02:30 -04006223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224}
6225
Trond Myklebustfd1fd682017-11-03 08:00:14 -04006226static struct nfs4_ol_stateid *
6227find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
6228{
6229 struct nfs4_ol_stateid *lst;
6230 struct nfs4_client *clp = lo->lo_owner.so_client;
6231
6232 lockdep_assert_held(&clp->cl_lock);
6233
6234 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
6235 if (lst->st_stid.sc_type != NFS4_LOCK_STID)
6236 continue;
6237 if (lst->st_stid.sc_file == fp) {
6238 refcount_inc(&lst->st_stid.sc_count);
6239 return lst;
6240 }
6241 }
6242 return NULL;
6243}
6244
Trond Myklebustbeeca192017-11-03 08:00:14 -04006245static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006246init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6247 struct nfs4_file *fp, struct inode *inode,
6248 struct nfs4_ol_stateid *open_stp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249{
J. Bruce Fieldsd3b313a2011-09-15 15:02:41 -04006250 struct nfs4_client *clp = lo->lo_owner.so_client;
Trond Myklebustbeeca192017-11-03 08:00:14 -04006251 struct nfs4_ol_stateid *retstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252
Trond Myklebustbeeca192017-11-03 08:00:14 -04006253 mutex_init(&stp->st_mutex);
Andrew Elble4f34bd02017-11-08 17:29:51 -05006254 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006255retry:
6256 spin_lock(&clp->cl_lock);
6257 spin_lock(&fp->fi_lock);
6258 retstp = find_lock_stateid(lo, fp);
6259 if (retstp)
6260 goto out_unlock;
Jeff Layton356a95e2014-07-29 21:34:13 -04006261
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +03006262 refcount_inc(&stp->st_stid.sc_count);
J. Bruce Fields3abdb602013-02-03 12:23:01 -05006263 stp->st_stid.sc_type = NFS4_LOCK_STID;
Kinglong Meeb5971af2014-08-22 10:18:43 -04006264 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
NeilBrown13cd2182005-06-23 22:03:10 -07006265 get_nfs4_file(fp);
Trond Myklebust11b91642014-07-29 21:34:08 -04006266 stp->st_stid.sc_file = fp;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006267 stp->st_access_bmap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 stp->st_deny_bmap = open_stp->st_deny_bmap;
NeilBrown4c4cd222005-07-07 17:59:27 -07006269 stp->st_openstp = open_stp;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04006270 list_add(&stp->st_locks, &open_stp->st_locks);
Jeff Layton1c755dc2014-07-29 21:34:12 -04006271 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
Trond Myklebust1d31a252014-07-10 14:07:25 -04006272 list_add(&stp->st_perfile, &fp->fi_stateids);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006273out_unlock:
Trond Myklebust1d31a252014-07-10 14:07:25 -04006274 spin_unlock(&fp->fi_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006275 spin_unlock(&clp->cl_lock);
6276 if (retstp) {
6277 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6278 nfs4_put_stid(&retstp->st_stid);
6279 goto retry;
6280 }
6281 /* To keep mutex tracking happy */
6282 mutex_unlock(&stp->st_mutex);
6283 stp = retstp;
6284 }
6285 return stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286}
6287
Jeff Laytonc53530d2014-06-30 11:48:39 -04006288static struct nfs4_ol_stateid *
Jeff Layton356a95e2014-07-29 21:34:13 -04006289find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6290 struct inode *inode, struct nfs4_ol_stateid *ost,
6291 bool *new)
6292{
6293 struct nfs4_stid *ns = NULL;
6294 struct nfs4_ol_stateid *lst;
6295 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6296 struct nfs4_client *clp = oo->oo_owner.so_client;
6297
Trond Myklebustbeeca192017-11-03 08:00:14 -04006298 *new = false;
Jeff Layton356a95e2014-07-29 21:34:13 -04006299 spin_lock(&clp->cl_lock);
6300 lst = find_lock_stateid(lo, fi);
Jeff Layton356a95e2014-07-29 21:34:13 -04006301 spin_unlock(&clp->cl_lock);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006302 if (lst != NULL) {
6303 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6304 goto out;
6305 nfs4_put_stid(&lst->st_stid);
6306 }
6307 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6308 if (ns == NULL)
6309 return NULL;
6310
6311 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6312 if (lst == openlockstateid(ns))
6313 *new = true;
6314 else
Jeff Layton356a95e2014-07-29 21:34:13 -04006315 nfs4_put_stid(ns);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006316out:
Jeff Layton356a95e2014-07-29 21:34:13 -04006317 return lst;
6318}
Jeff Laytonc53530d2014-06-30 11:48:39 -04006319
NeilBrownfd39ca92005-06-23 22:04:03 -07006320static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321check_lock_length(u64 offset, u64 length)
6322{
Kinglong Meee7969312015-07-13 17:34:19 +08006323 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6324 (length > ~offset)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325}
6326
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006327static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006328{
Trond Myklebust11b91642014-07-29 21:34:08 -04006329 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
J. Bruce Fields0997b172011-03-02 18:01:35 -05006330
Jeff Layton7214e862014-07-10 14:07:33 -04006331 lockdep_assert_held(&fp->fi_lock);
6332
Jeff Layton82c5ff12012-05-11 09:45:13 -04006333 if (test_access(access, lock_stp))
J. Bruce Fields0997b172011-03-02 18:01:35 -05006334 return;
Jeff Layton12659652014-07-10 14:07:28 -04006335 __nfs4_file_get_access(fp, access);
Jeff Layton82c5ff12012-05-11 09:45:13 -04006336 set_access(access, lock_stp);
J. Bruce Fields0997b172011-03-02 18:01:35 -05006337}
6338
Jeff Layton356a95e2014-07-29 21:34:13 -04006339static __be32
6340lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6341 struct nfs4_ol_stateid *ost,
6342 struct nfsd4_lock *lock,
Jeff Laytondd257932016-08-11 10:37:39 -04006343 struct nfs4_ol_stateid **plst, bool *new)
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006344{
Jeff Layton5db1c032014-07-29 21:34:28 -04006345 __be32 status;
Trond Myklebust11b91642014-07-29 21:34:08 -04006346 struct nfs4_file *fi = ost->st_stid.sc_file;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006347 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6348 struct nfs4_client *cl = oo->oo_owner.so_client;
David Howells2b0143b2015-03-17 22:25:59 +00006349 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006350 struct nfs4_lockowner *lo;
Jeff Laytondd257932016-08-11 10:37:39 -04006351 struct nfs4_ol_stateid *lst;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006352 unsigned int strhashval;
6353
Kinglong Meec8623992015-07-13 17:35:37 +08006354 lo = find_lockowner_str(cl, &lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006355 if (!lo) {
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006356 strhashval = ownerstr_hashval(&lock->lk_new_owner);
Jeff Laytonc53530d2014-06-30 11:48:39 -04006357 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6358 if (lo == NULL)
6359 return nfserr_jukebox;
6360 } else {
6361 /* with an existing lockowner, seqids must be the same */
Jeff Layton5db1c032014-07-29 21:34:28 -04006362 status = nfserr_bad_seqid;
Jeff Laytonc53530d2014-06-30 11:48:39 -04006363 if (!cstate->minorversion &&
6364 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
Jeff Layton5db1c032014-07-29 21:34:28 -04006365 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006366 }
Jeff Laytonc53530d2014-06-30 11:48:39 -04006367
Jeff Laytondd257932016-08-11 10:37:39 -04006368 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6369 if (lst == NULL) {
Jeff Layton5db1c032014-07-29 21:34:28 -04006370 status = nfserr_jukebox;
6371 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006372 }
Jeff Laytondd257932016-08-11 10:37:39 -04006373
Jeff Layton5db1c032014-07-29 21:34:28 -04006374 status = nfs_ok;
Jeff Laytondd257932016-08-11 10:37:39 -04006375 *plst = lst;
Jeff Layton5db1c032014-07-29 21:34:28 -04006376out:
6377 nfs4_put_stateowner(&lo->lo_owner);
6378 return status;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006379}
6380
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381/*
6382 * LOCK operation
6383 */
Al Virob37ad282006-10-19 23:28:59 -07006384__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006385nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006386 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006388 struct nfsd4_lock *lock = &u->lock;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006389 struct nfs4_openowner *open_sop = NULL;
6390 struct nfs4_lockowner *lock_sop = NULL;
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006391 struct nfs4_ol_stateid *lock_stp = NULL;
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006392 struct nfs4_ol_stateid *open_stp = NULL;
Jeff Layton7214e862014-07-10 14:07:33 -04006393 struct nfs4_file *fp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006394 struct nfsd_file *nf = NULL;
Jeff Layton76d348f2016-09-16 16:28:24 -04006395 struct nfsd4_blocked_lock *nbl = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006396 struct file_lock *file_lock = NULL;
6397 struct file_lock *conflock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006398 __be32 status = 0;
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006399 int lkflg;
Al Virob8dd7b92006-10-19 23:29:01 -07006400 int err;
Jeff Layton5db1c032014-07-29 21:34:28 -04006401 bool new = false;
Jeff Layton76d348f2016-09-16 16:28:24 -04006402 unsigned char fl_type;
6403 unsigned int fl_flags = FL_POSIX;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006404 struct net *net = SVC_NET(rqstp);
6405 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406
6407 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6408 (long long) lock->lk_offset,
6409 (long long) lock->lk_length);
6410
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 if (check_lock_length(lock->lk_offset, lock->lk_length))
6412 return nfserr_inval;
6413
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006414 if ((status = fh_verify(rqstp, &cstate->current_fh,
Miklos Szeredi8837abc2008-06-16 13:20:29 +02006415 S_IFREG, NFSD_MAY_LOCK))) {
Andy Adamsona6f6ef22006-01-18 17:43:17 -08006416 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6417 return status;
6418 }
6419
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 if (lock->lk_is_new) {
J. Bruce Fields684e5632011-11-04 17:08:10 -04006421 if (nfsd4_has_session(cstate))
6422 /* See rfc 5661 18.10.3: given clientid is ignored: */
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006423 memcpy(&lock->lk_new_clientid,
J. Bruce Fields684e5632011-11-04 17:08:10 -04006424 &cstate->session->se_client->cl_clientid,
6425 sizeof(clientid_t));
6426
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 status = nfserr_stale_clientid;
Stanislav Kinsbursky2c142ba2012-07-25 16:57:45 +04006428 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 /* validate and update open stateid and open seqid */
J. Bruce Fieldsc0a5d932011-09-06 15:19:46 -04006432 status = nfs4_preprocess_confirmed_seqid_op(cstate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 lock->lk_new_open_seqid,
6434 &lock->lk_new_open_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006435 &open_stp, nn);
NeilBrown37515172005-07-07 17:59:16 -07006436 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 goto out;
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006438 mutex_unlock(&open_stp->st_mutex);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006439 open_sop = openowner(open_stp->st_stateowner);
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006440 status = nfserr_bad_stateid;
J. Bruce Fields684e5632011-11-04 17:08:10 -04006441 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
Kinglong Mee76f6c9e2015-07-13 17:35:10 +08006442 &lock->lk_new_clientid))
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006443 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006444 status = lookup_or_create_lock_state(cstate, open_stp, lock,
Jeff Layton5db1c032014-07-29 21:34:28 -04006445 &lock_stp, &new);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006446 } else {
Benny Halevydd453df2009-04-03 08:28:41 +03006447 status = nfs4_preprocess_seqid_op(cstate,
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006448 lock->lk_old_lock_seqid,
6449 &lock->lk_old_lock_stateid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006450 NFS4_LOCK_STID, &lock_stp, nn);
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006451 }
J. Bruce Fieldse1aaa892012-06-06 16:01:37 -04006452 if (status)
6453 goto out;
J. Bruce Fields64a284d2011-10-20 06:57:46 -04006454 lock_sop = lockowner(lock_stp->st_stateowner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455
J. Bruce Fieldsb34f27a2011-08-22 13:13:31 -04006456 lkflg = setlkflg(lock->lk_type);
6457 status = nfs4_check_openmode(lock_stp, lkflg);
6458 if (status)
6459 goto out;
6460
NeilBrown0dd395d2005-07-07 17:59:15 -07006461 status = nfserr_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006462 if (locks_in_grace(net) && !lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006463 goto out;
6464 status = nfserr_no_grace;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006465 if (!locks_in_grace(net) && lock->lk_reclaim)
NeilBrown0dd395d2005-07-07 17:59:15 -07006466 goto out;
6467
Trond Myklebust11b91642014-07-29 21:34:08 -04006468 fp = lock_stp->st_stid.sc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 switch (lock->lk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 case NFS4_READW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006471 if (nfsd4_has_session(cstate))
6472 fl_flags |= FL_SLEEP;
6473 /* Fallthrough */
6474 case NFS4_READ_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006475 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006476 nf = find_readable_file_locked(fp);
6477 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006478 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
Jeff Layton7214e862014-07-10 14:07:33 -04006479 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006480 fl_type = F_RDLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006481 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 case NFS4_WRITEW_LT:
Jeff Layton76d348f2016-09-16 16:28:24 -04006483 if (nfsd4_has_session(cstate))
6484 fl_flags |= FL_SLEEP;
6485 /* Fallthrough */
6486 case NFS4_WRITE_LT:
Jeff Layton7214e862014-07-10 14:07:33 -04006487 spin_lock(&fp->fi_lock);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006488 nf = find_writeable_file_locked(fp);
6489 if (nf)
J. Bruce Fields0997b172011-03-02 18:01:35 -05006490 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
Jeff Layton7214e862014-07-10 14:07:33 -04006491 spin_unlock(&fp->fi_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006492 fl_type = F_WRLCK;
J. Bruce Fields529d7b22011-03-02 23:48:33 -05006493 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494 default:
6495 status = nfserr_inval;
6496 goto out;
6497 }
Jeff Layton76d348f2016-09-16 16:28:24 -04006498
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006499 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006500 status = nfserr_openmode;
6501 goto out;
6502 }
Kinglong Meeaef95832014-08-22 10:18:44 -04006503
Jeff Layton76d348f2016-09-16 16:28:24 -04006504 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6505 if (!nbl) {
6506 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6507 status = nfserr_jukebox;
6508 goto out;
6509 }
6510
6511 file_lock = &nbl->nbl_lock;
6512 file_lock->fl_type = fl_type;
Kinglong Meeaef95832014-08-22 10:18:44 -04006513 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
Jeff Layton21179d82012-08-21 08:03:32 -04006514 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006515 file_lock->fl_file = nf->nf_file;
Jeff Layton76d348f2016-09-16 16:28:24 -04006516 file_lock->fl_flags = fl_flags;
Jeff Layton21179d82012-08-21 08:03:32 -04006517 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6518 file_lock->fl_start = lock->lk_offset;
6519 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6520 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521
Jeff Layton21179d82012-08-21 08:03:32 -04006522 conflock = locks_alloc_lock();
6523 if (!conflock) {
6524 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6525 status = nfserr_jukebox;
6526 goto out;
6527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528
Jeff Layton76d348f2016-09-16 16:28:24 -04006529 if (fl_flags & FL_SLEEP) {
Jeff Layton7919d0a2016-09-16 16:28:25 -04006530 nbl->nbl_time = jiffies;
Jeff Layton0cc11a62016-10-20 09:34:31 -04006531 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006532 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006533 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006534 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006535 }
6536
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006537 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006538 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 case 0: /* success! */
Jeff Layton9767feb2015-10-01 09:05:50 -04006540 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
Al Virob8dd7b92006-10-19 23:29:01 -07006541 status = 0;
J. Bruce Fields03f318c2018-06-08 12:28:47 -04006542 if (lock->lk_reclaim)
6543 nn->somebody_reclaimed = true;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006544 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006545 case FILE_LOCK_DEFERRED:
6546 nbl = NULL;
6547 /* Fallthrough */
6548 case -EAGAIN: /* conflock holds conflicting lock */
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006549 status = nfserr_denied;
6550 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
Jeff Layton21179d82012-08-21 08:03:32 -04006551 nfs4_set_lock_denied(conflock, &lock->lk_denied);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006552 break;
Jeff Layton76d348f2016-09-16 16:28:24 -04006553 case -EDEADLK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554 status = nfserr_deadlock;
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006555 break;
J. Bruce Fields3e772462011-08-10 19:07:33 -04006556 default:
Marc Eshelfd85b812006-11-28 16:26:41 -05006557 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
J. Bruce Fields3e772462011-08-10 19:07:33 -04006558 status = nfserrno(err);
Andy Adamsoneb76b3f2006-03-26 01:37:26 -08006559 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561out:
Jeff Layton76d348f2016-09-16 16:28:24 -04006562 if (nbl) {
6563 /* dequeue it if we queued it before */
6564 if (fl_flags & FL_SLEEP) {
Jeff Layton0cc11a62016-10-20 09:34:31 -04006565 spin_lock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006566 list_del_init(&nbl->nbl_list);
Jeff Layton7919d0a2016-09-16 16:28:25 -04006567 list_del_init(&nbl->nbl_lru);
Jeff Layton0cc11a62016-10-20 09:34:31 -04006568 spin_unlock(&nn->blocked_locks_lock);
Jeff Layton76d348f2016-09-16 16:28:24 -04006569 }
6570 free_blocked_lock(nbl);
6571 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006572 if (nf)
6573 nfsd_file_put(nf);
Jeff Layton5db1c032014-07-29 21:34:28 -04006574 if (lock_stp) {
6575 /* Bump seqid manually if the 4.0 replay owner is openowner */
6576 if (cstate->replay_owner &&
6577 cstate->replay_owner != &lock_sop->lo_owner &&
6578 seqid_mutating_err(ntohl(status)))
6579 lock_sop->lo_owner.so_seqid++;
6580
6581 /*
6582 * If this is a new, never-before-used stateid, and we are
6583 * returning an error, then just go ahead and release it.
6584 */
J. Bruce Fields25020722018-01-17 16:25:59 -05006585 if (status && new)
Jeff Layton5db1c032014-07-29 21:34:28 -04006586 release_lock_stateid(lock_stp);
Trond Myklebustbeeca192017-11-03 08:00:14 -04006587
6588 mutex_unlock(&lock_stp->st_mutex);
Jeff Layton5db1c032014-07-29 21:34:28 -04006589
Trond Myklebust3d0fabd2014-07-29 21:34:15 -04006590 nfs4_put_stid(&lock_stp->st_stid);
Jeff Layton5db1c032014-07-29 21:34:28 -04006591 }
Trond Myklebust0667b1e2014-07-29 21:34:22 -04006592 if (open_stp)
6593 nfs4_put_stid(&open_stp->st_stid);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006594 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006595 if (conflock)
6596 locks_free_lock(conflock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 return status;
6598}
6599
6600/*
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006601 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6602 * so we do a temporary open here just to get an open file to pass to
6603 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6604 * inode operation.)
6605 */
Al Viro04da6e92012-04-13 00:00:04 -04006606static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006607{
Jeff Layton6b556ca2019-08-18 14:18:55 -04006608 struct nfsd_file *nf;
6609 __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
Al Viro04da6e92012-04-13 00:00:04 -04006610 if (!err) {
Jeff Layton6b556ca2019-08-18 14:18:55 -04006611 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6612 nfsd_file_put(nf);
Al Viro04da6e92012-04-13 00:00:04 -04006613 }
J. Bruce Fields55ef1272008-12-20 11:58:38 -08006614 return err;
6615}
6616
6617/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 * LOCKT operation
6619 */
Al Virob37ad282006-10-19 23:28:59 -07006620__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006621nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006622 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006624 struct nfsd4_lockt *lockt = &u->lockt;
Jeff Layton21179d82012-08-21 08:03:32 -04006625 struct file_lock *file_lock = NULL;
Jeff Layton5db1c032014-07-29 21:34:28 -04006626 struct nfs4_lockowner *lo = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006627 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006628 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629
Stanislav Kinsbursky5ccb0062012-07-25 16:57:22 +04006630 if (locks_in_grace(SVC_NET(rqstp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 return nfserr_grace;
6632
6633 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6634 return nfserr_inval;
6635
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006636 if (!nfsd4_has_session(cstate)) {
Jeff Layton4b24ca72014-06-30 11:48:44 -04006637 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006638 if (status)
6639 goto out;
6640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641
J. Bruce Fields75c096f2011-08-15 18:39:32 -04006642 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644
Jeff Layton21179d82012-08-21 08:03:32 -04006645 file_lock = locks_alloc_lock();
6646 if (!file_lock) {
6647 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6648 status = nfserr_jukebox;
6649 goto out;
6650 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006651
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 switch (lockt->lt_type) {
6653 case NFS4_READ_LT:
6654 case NFS4_READW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006655 file_lock->fl_type = F_RDLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 case NFS4_WRITE_LT:
6658 case NFS4_WRITEW_LT:
Jeff Layton21179d82012-08-21 08:03:32 -04006659 file_lock->fl_type = F_WRLCK;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006660 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661 default:
J. Bruce Fields2fdada02007-07-27 16:10:37 -04006662 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 status = nfserr_inval;
Colin Ian Kingf50c9d72018-11-03 16:12:53 +00006664 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 }
6666
Kinglong Meec8623992015-07-13 17:35:37 +08006667 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04006668 if (lo)
Jeff Layton21179d82012-08-21 08:03:32 -04006669 file_lock->fl_owner = (fl_owner_t)lo;
6670 file_lock->fl_pid = current->tgid;
6671 file_lock->fl_flags = FL_POSIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672
Jeff Layton21179d82012-08-21 08:03:32 -04006673 file_lock->fl_start = lockt->lt_offset;
6674 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675
Jeff Layton21179d82012-08-21 08:03:32 -04006676 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677
Jeff Layton21179d82012-08-21 08:03:32 -04006678 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
Al Viro04da6e92012-04-13 00:00:04 -04006679 if (status)
Marc Eshelfd85b812006-11-28 16:26:41 -05006680 goto out;
Al Viro04da6e92012-04-13 00:00:04 -04006681
Jeff Layton21179d82012-08-21 08:03:32 -04006682 if (file_lock->fl_type != F_UNLCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 status = nfserr_denied;
Jeff Layton21179d82012-08-21 08:03:32 -04006684 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 }
6686out:
Jeff Layton5db1c032014-07-29 21:34:28 -04006687 if (lo)
6688 nfs4_put_stateowner(&lo->lo_owner);
Jeff Layton21179d82012-08-21 08:03:32 -04006689 if (file_lock)
6690 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 return status;
6692}
6693
Al Virob37ad282006-10-19 23:28:59 -07006694__be32
J.Bruce Fieldsca364312006-12-13 00:35:27 -08006695nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006696 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006698 struct nfsd4_locku *locku = &u->locku;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006699 struct nfs4_ol_stateid *stp;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006700 struct nfsd_file *nf = NULL;
Jeff Layton21179d82012-08-21 08:03:32 -04006701 struct file_lock *file_lock = NULL;
Al Virob37ad282006-10-19 23:28:59 -07006702 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07006703 int err;
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006704 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6705
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6707 (long long) locku->lu_offset,
6708 (long long) locku->lu_length);
6709
6710 if (check_lock_length(locku->lu_offset, locku->lu_length))
6711 return nfserr_inval;
6712
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006713 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03006714 &locku->lu_stateid, NFS4_LOCK_STID,
6715 &stp, nn);
J. Bruce Fields9072d5c2011-08-24 12:45:03 -04006716 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 goto out;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006718 nf = find_any_file(stp->st_stid.sc_file);
6719 if (!nf) {
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006720 status = nfserr_lock_range;
Trond Myklebust858cc572014-07-29 21:34:16 -04006721 goto put_stateid;
J. Bruce Fieldsf9d75622010-07-08 11:02:09 -04006722 }
Jeff Layton21179d82012-08-21 08:03:32 -04006723 file_lock = locks_alloc_lock();
6724 if (!file_lock) {
6725 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6726 status = nfserr_jukebox;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006727 goto put_file;
Jeff Layton21179d82012-08-21 08:03:32 -04006728 }
Kinglong Mee6cd90662014-08-15 08:02:55 +08006729
Jeff Layton21179d82012-08-21 08:03:32 -04006730 file_lock->fl_type = F_UNLCK;
Kinglong Meeaef95832014-08-22 10:18:44 -04006731 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
Jeff Layton21179d82012-08-21 08:03:32 -04006732 file_lock->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006733 file_lock->fl_file = nf->nf_file;
Jeff Layton21179d82012-08-21 08:03:32 -04006734 file_lock->fl_flags = FL_POSIX;
6735 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6736 file_lock->fl_start = locku->lu_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737
Jeff Layton21179d82012-08-21 08:03:32 -04006738 file_lock->fl_end = last_byte_offset(locku->lu_offset,
6739 locku->lu_length);
6740 nfs4_transform_lock_offset(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006742 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
Al Virob8dd7b92006-10-19 23:29:01 -07006743 if (err) {
Marc Eshelfd85b812006-11-28 16:26:41 -05006744 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745 goto out_nfserr;
6746 }
Jeff Layton9767feb2015-10-01 09:05:50 -04006747 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006748put_file:
6749 nfsd_file_put(nf);
Trond Myklebust858cc572014-07-29 21:34:16 -04006750put_stateid:
Oleg Drokinfeb9dad2016-06-14 23:28:04 -04006751 mutex_unlock(&stp->st_mutex);
Trond Myklebust858cc572014-07-29 21:34:16 -04006752 nfs4_put_stid(&stp->st_stid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753out:
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04006754 nfsd4_bump_seqid(cstate, status);
Jeff Layton21179d82012-08-21 08:03:32 -04006755 if (file_lock)
6756 locks_free_lock(file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 return status;
6758
6759out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07006760 status = nfserrno(err);
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006761 goto put_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762}
6763
6764/*
6765 * returns
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006766 * true: locks held by lockowner
6767 * false: no locks held by lockowner
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 */
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006769static bool
6770check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771{
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006772 struct file_lock *fl;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006773 int status = false;
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006774 struct nfsd_file *nf = find_any_file(fp);
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006775 struct inode *inode;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006776 struct file_lock_context *flctx;
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006777
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006778 if (!nf) {
Jeff Laytonf9c00c32014-07-23 16:17:41 -04006779 /* Any valid lock stateid should have some sort of access */
6780 WARN_ON_ONCE(1);
6781 return status;
6782 }
6783
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006784 inode = locks_inode(nf->nf_file);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006785 flctx = inode->i_flctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006787 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
Jeff Layton6109c852015-01-16 15:05:57 -05006788 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05006789 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6790 if (fl->fl_owner == (fl_owner_t)lowner) {
6791 status = true;
6792 break;
6793 }
J. Bruce Fields796dadf2006-01-18 17:43:22 -08006794 }
Jeff Layton6109c852015-01-16 15:05:57 -05006795 spin_unlock(&flctx->flc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 }
Jeff Laytoneb82dd32019-08-18 14:18:53 -04006797 nfsd_file_put(nf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 return status;
6799}
6800
Al Virob37ad282006-10-19 23:28:59 -07006801__be32
J.Bruce Fieldsb5914802006-12-13 00:35:38 -08006802nfsd4_release_lockowner(struct svc_rqst *rqstp,
6803 struct nfsd4_compound_state *cstate,
Christoph Hellwigeb698532017-05-08 20:58:35 +02006804 union nfsd4_op_u *u)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805{
Christoph Hellwigeb698532017-05-08 20:58:35 +02006806 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 clientid_t *clid = &rlockowner->rl_clientid;
Jeff Layton882e9d22014-07-29 21:34:37 -04006808 struct nfs4_stateowner *sop;
6809 struct nfs4_lockowner *lo = NULL;
J. Bruce Fieldsdcef0412011-09-07 16:06:42 -04006810 struct nfs4_ol_stateid *stp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 struct xdr_netobj *owner = &rlockowner->rl_owner;
Trond Myklebustd4f04892014-07-29 21:34:36 -04006812 unsigned int hashval = ownerstr_hashval(owner);
Al Virob37ad282006-10-19 23:28:59 -07006813 __be32 status;
Stanislav Kinsbursky7f2210f2012-11-14 18:21:05 +03006814 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebustc58c6612014-07-29 21:34:35 -04006815 struct nfs4_client *clp;
Chuck Lever88584812016-07-13 16:40:14 -04006816 LIST_HEAD (reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817
6818 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6819 clid->cl_boot, clid->cl_id);
6820
Jeff Layton4b24ca72014-06-30 11:48:44 -04006821 status = lookup_clientid(clid, cstate, nn);
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006822 if (status)
Trond Myklebust51f5e782014-07-30 08:27:27 -04006823 return status;
J. Bruce Fields9b2ef622012-12-03 17:24:41 -05006824
Trond Myklebustd4f04892014-07-29 21:34:36 -04006825 clp = cstate->clp;
Jeff Laytonfd449072014-06-30 11:48:41 -04006826 /* Find the matching lock stateowner */
Trond Myklebustd4f04892014-07-29 21:34:36 -04006827 spin_lock(&clp->cl_lock);
Jeff Layton882e9d22014-07-29 21:34:37 -04006828 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
Trond Myklebustd4f04892014-07-29 21:34:36 -04006829 so_strhash) {
Jeff Layton882e9d22014-07-29 21:34:37 -04006830
6831 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
J. Bruce Fields16bfdaaf2011-11-07 17:23:30 -05006832 continue;
Jeff Laytonfd449072014-06-30 11:48:41 -04006833
Jeff Layton882e9d22014-07-29 21:34:37 -04006834 /* see if there are still any locks associated with it */
6835 lo = lockowner(sop);
6836 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6837 if (check_for_locks(stp->st_stid.sc_file, lo)) {
6838 status = nfserr_locks_held;
6839 spin_unlock(&clp->cl_lock);
Trond Myklebust51f5e782014-07-30 08:27:27 -04006840 return status;
Jeff Layton882e9d22014-07-29 21:34:37 -04006841 }
Jeff Layton5adfd882014-07-29 21:34:31 -04006842 }
Jeff Layton882e9d22014-07-29 21:34:37 -04006843
Kinglong Meeb5971af2014-08-22 10:18:43 -04006844 nfs4_get_stateowner(sop);
Jeff Layton882e9d22014-07-29 21:34:37 -04006845 break;
Jeff Laytonfd449072014-06-30 11:48:41 -04006846 }
Chuck Lever88584812016-07-13 16:40:14 -04006847 if (!lo) {
6848 spin_unlock(&clp->cl_lock);
6849 return status;
6850 }
6851
6852 unhash_lockowner_locked(lo);
6853 while (!list_empty(&lo->lo_owner.so_stateids)) {
6854 stp = list_first_entry(&lo->lo_owner.so_stateids,
6855 struct nfs4_ol_stateid,
6856 st_perstateowner);
6857 WARN_ON(!unhash_lock_stateid(stp));
6858 put_ol_stateid_locked(stp, &reaplist);
6859 }
Trond Myklebustc58c6612014-07-29 21:34:35 -04006860 spin_unlock(&clp->cl_lock);
Chuck Lever88584812016-07-13 16:40:14 -04006861 free_ol_stateid_reaplist(&reaplist);
Jeff Layton68ef3bc2018-03-16 11:32:02 -04006862 remove_blocked_locks(lo);
Chuck Lever88584812016-07-13 16:40:14 -04006863 nfs4_put_stateowner(&lo->lo_owner);
6864
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 return status;
6866}
6867
6868static inline struct nfs4_client_reclaim *
NeilBrowna55370a2005-06-23 22:03:52 -07006869alloc_reclaim(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870{
NeilBrowna55370a2005-06-23 22:03:52 -07006871 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872}
6873
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006874bool
Scott Mayhew6b189102019-03-26 18:06:26 -04006875nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
NeilBrownc7b9a452005-06-23 22:04:30 -07006876{
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006877 struct nfs4_client_reclaim *crp;
NeilBrownc7b9a452005-06-23 22:04:30 -07006878
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006879 crp = nfsd4_find_reclaim_client(name, nn);
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006880 return (crp && crp->cr_clp);
NeilBrownc7b9a452005-06-23 22:04:30 -07006881}
6882
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883/*
6884 * failure => all reset bets are off, nfserr_no_grace...
Scott Mayhew6b189102019-03-26 18:06:26 -04006885 *
6886 * The caller is responsible for freeing name.data if NULL is returned (it
6887 * will be freed in nfs4_remove_reclaim_record in the normal case).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 */
Jeff Layton772a9bb2012-11-12 15:00:54 -05006889struct nfs4_client_reclaim *
Scott Mayhew6ee95d12019-09-09 16:10:31 -04006890nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
6891 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892{
6893 unsigned int strhashval;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006894 struct nfs4_client_reclaim *crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895
Scott Mayhew6b189102019-03-26 18:06:26 -04006896 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data);
NeilBrowna55370a2005-06-23 22:03:52 -07006897 crp = alloc_reclaim();
Jeff Layton772a9bb2012-11-12 15:00:54 -05006898 if (crp) {
6899 strhashval = clientstr_hashval(name);
6900 INIT_LIST_HEAD(&crp->cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006901 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
Scott Mayhew6b189102019-03-26 18:06:26 -04006902 crp->cr_name.data = name.data;
6903 crp->cr_name.len = name.len;
Scott Mayhew6ee95d12019-09-09 16:10:31 -04006904 crp->cr_princhash.data = princhash.data;
6905 crp->cr_princhash.len = princhash.len;
Jeff Layton0ce0c2b2012-11-12 15:00:55 -05006906 crp->cr_clp = NULL;
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006907 nn->reclaim_str_hashtbl_size++;
Jeff Layton772a9bb2012-11-12 15:00:54 -05006908 }
6909 return crp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910}
6911
Jeff Layton2a4317c2012-03-21 16:42:43 -04006912void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006913nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
Jeff Laytonce30e532012-11-12 15:00:53 -05006914{
6915 list_del(&crp->cr_strhash);
Scott Mayhew6b189102019-03-26 18:06:26 -04006916 kfree(crp->cr_name.data);
Scott Mayhew6ee95d12019-09-09 16:10:31 -04006917 kfree(crp->cr_princhash.data);
Jeff Laytonce30e532012-11-12 15:00:53 -05006918 kfree(crp);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006919 nn->reclaim_str_hashtbl_size--;
Jeff Laytonce30e532012-11-12 15:00:53 -05006920}
6921
6922void
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006923nfs4_release_reclaim(struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924{
6925 struct nfs4_client_reclaim *crp = NULL;
6926 int i;
6927
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006929 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
6930 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 struct nfs4_client_reclaim, cr_strhash);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006932 nfs4_remove_reclaim_record(crp, nn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 }
6934 }
J. Bruce Fields063b0fb2012-11-25 14:48:10 -05006935 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936}
6937
6938/*
6939 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
Jeff Layton2a4317c2012-03-21 16:42:43 -04006940struct nfs4_client_reclaim *
Scott Mayhew6b189102019-03-26 18:06:26 -04006941nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942{
6943 unsigned int strhashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 struct nfs4_client_reclaim *crp = NULL;
6945
Scott Mayhew6b189102019-03-26 18:06:26 -04006946 dprintk("NFSD: nfs4_find_reclaim_client for name %.*s\n", name.len, name.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947
Scott Mayhew6b189102019-03-26 18:06:26 -04006948 strhashval = clientstr_hashval(name);
Stanislav Kinsbursky52e19c02012-11-14 18:21:16 +03006949 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
Scott Mayhew6b189102019-03-26 18:06:26 -04006950 if (compare_blob(&crp->cr_name, &name) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 return crp;
6952 }
6953 }
6954 return NULL;
6955}
6956
6957/*
6958* Called from OPEN. Look for clientid in reclaim list.
6959*/
Al Virob37ad282006-10-19 23:28:59 -07006960__be32
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006961nfs4_check_open_reclaim(clientid_t *clid,
6962 struct nfsd4_compound_state *cstate,
6963 struct nfsd_net *nn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964{
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006965 __be32 status;
Jeff Laytona52d7262012-03-21 09:52:02 -04006966
6967 /* find clientid in conf_id_hashtbl */
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006968 status = lookup_clientid(clid, cstate, nn);
6969 if (status)
Jeff Laytona52d7262012-03-21 09:52:02 -04006970 return nfserr_reclaim_bad;
6971
Jeff Layton3b3e7b72014-09-12 16:40:22 -04006972 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
6973 return nfserr_no_grace;
6974
Trond Myklebust0fe492d2014-06-30 11:48:47 -04006975 if (nfsd4_client_record_check(cstate->clp))
6976 return nfserr_reclaim_bad;
6977
6978 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979}
6980
Bryan Schumaker65178db2011-11-01 13:35:21 -04006981#ifdef CONFIG_NFSD_FAULT_INJECTION
Jeff Layton016200c2014-07-30 08:27:21 -04006982static inline void
6983put_client(struct nfs4_client *clp)
6984{
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04006985 atomic_dec(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04006986}
6987
Jeff Layton285abde2014-07-30 08:27:24 -04006988static struct nfs4_client *
6989nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
6990{
6991 struct nfs4_client *clp;
6992 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6993 nfsd_net_id);
6994
6995 if (!nfsd_netns_ready(nn))
6996 return NULL;
6997
6998 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6999 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
7000 return clp;
7001 }
7002 return NULL;
7003}
7004
Jeff Layton7ec0e362014-07-30 08:27:17 -04007005u64
Jeff Layton285abde2014-07-30 08:27:24 -04007006nfsd_inject_print_clients(void)
Jeff Layton7ec0e362014-07-30 08:27:17 -04007007{
7008 struct nfs4_client *clp;
7009 u64 count = 0;
7010 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7011 nfsd_net_id);
7012 char buf[INET6_ADDRSTRLEN];
7013
7014 if (!nfsd_netns_ready(nn))
7015 return 0;
7016
7017 spin_lock(&nn->client_lock);
7018 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7019 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7020 pr_info("NFS Client: %s\n", buf);
7021 ++count;
7022 }
7023 spin_unlock(&nn->client_lock);
7024
7025 return count;
7026}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007027
Jeff Laytona0926d12014-07-30 08:27:18 -04007028u64
Jeff Layton285abde2014-07-30 08:27:24 -04007029nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
Jeff Laytona0926d12014-07-30 08:27:18 -04007030{
7031 u64 count = 0;
7032 struct nfs4_client *clp;
7033 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7034 nfsd_net_id);
7035
7036 if (!nfsd_netns_ready(nn))
7037 return count;
7038
7039 spin_lock(&nn->client_lock);
7040 clp = nfsd_find_client(addr, addr_size);
7041 if (clp) {
7042 if (mark_client_expired_locked(clp) == nfs_ok)
7043 ++count;
7044 else
7045 clp = NULL;
7046 }
7047 spin_unlock(&nn->client_lock);
7048
7049 if (clp)
7050 expire_client(clp);
7051
7052 return count;
7053}
7054
Jeff Layton69fc9ed2014-07-30 08:27:19 -04007055u64
Jeff Layton285abde2014-07-30 08:27:24 -04007056nfsd_inject_forget_clients(u64 max)
Jeff Layton69fc9ed2014-07-30 08:27:19 -04007057{
7058 u64 count = 0;
7059 struct nfs4_client *clp, *next;
7060 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7061 nfsd_net_id);
7062 LIST_HEAD(reaplist);
7063
7064 if (!nfsd_netns_ready(nn))
7065 return count;
7066
7067 spin_lock(&nn->client_lock);
7068 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7069 if (mark_client_expired_locked(clp) == nfs_ok) {
7070 list_add(&clp->cl_lru, &reaplist);
7071 if (max != 0 && ++count >= max)
7072 break;
7073 }
7074 }
7075 spin_unlock(&nn->client_lock);
7076
7077 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
7078 expire_client(clp);
7079
7080 return count;
7081}
7082
Bryan Schumaker184c1842012-11-29 11:40:44 -05007083static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
7084 const char *type)
7085{
7086 char buf[INET6_ADDRSTRLEN];
Bryan Schumaker0a5c33e2012-12-07 16:17:28 -05007087 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
Bryan Schumaker184c1842012-11-29 11:40:44 -05007088 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
7089}
7090
Jeff Layton016200c2014-07-30 08:27:21 -04007091static void
7092nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
7093 struct list_head *collect)
7094{
7095 struct nfs4_client *clp = lst->st_stid.sc_client;
7096 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7097 nfsd_net_id);
7098
7099 if (!collect)
7100 return;
7101
7102 lockdep_assert_held(&nn->client_lock);
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007103 atomic_inc(&clp->cl_rpc_users);
Jeff Layton016200c2014-07-30 08:27:21 -04007104 list_add(&lst->st_locks, collect);
7105}
7106
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007107static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
Jeff Layton3738d502014-07-30 08:27:20 -04007108 struct list_head *collect,
Jeff Laytone8568732015-08-24 12:41:47 -04007109 bool (*func)(struct nfs4_ol_stateid *))
Bryan Schumakerfc291712012-11-29 11:40:40 -05007110{
7111 struct nfs4_openowner *oop;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007112 struct nfs4_ol_stateid *stp, *st_next;
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007113 struct nfs4_ol_stateid *lst, *lst_next;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007114 u64 count = 0;
7115
Jeff Layton016200c2014-07-30 08:27:21 -04007116 spin_lock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007117 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
Trond Myklebust3c87b9b2014-06-30 11:48:38 -04007118 list_for_each_entry_safe(stp, st_next,
7119 &oop->oo_owner.so_stateids, st_perstateowner) {
7120 list_for_each_entry_safe(lst, lst_next,
7121 &stp->st_locks, st_locks) {
Jeff Layton3738d502014-07-30 08:27:20 -04007122 if (func) {
Jeff Laytone8568732015-08-24 12:41:47 -04007123 if (func(lst))
7124 nfsd_inject_add_lock_to_list(lst,
7125 collect);
Jeff Layton3738d502014-07-30 08:27:20 -04007126 }
Jeff Layton016200c2014-07-30 08:27:21 -04007127 ++count;
7128 /*
7129 * Despite the fact that these functions deal
7130 * with 64-bit integers for "count", we must
7131 * ensure that it doesn't blow up the
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007132 * clp->cl_rpc_users. Throw a warning if we
Jeff Layton016200c2014-07-30 08:27:21 -04007133 * start to approach INT_MAX here.
7134 */
7135 WARN_ON_ONCE(count == (INT_MAX / 2));
7136 if (count == max)
7137 goto out;
Bryan Schumakerfc291712012-11-29 11:40:40 -05007138 }
7139 }
7140 }
Jeff Layton016200c2014-07-30 08:27:21 -04007141out:
7142 spin_unlock(&clp->cl_lock);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007143
7144 return count;
7145}
7146
Jeff Layton016200c2014-07-30 08:27:21 -04007147static u64
7148nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
7149 u64 max)
Bryan Schumakerfc291712012-11-29 11:40:40 -05007150{
Jeff Layton016200c2014-07-30 08:27:21 -04007151 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
Bryan Schumakerfc291712012-11-29 11:40:40 -05007152}
7153
Jeff Layton016200c2014-07-30 08:27:21 -04007154static u64
7155nfsd_print_client_locks(struct nfs4_client *clp)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007156{
Jeff Layton016200c2014-07-30 08:27:21 -04007157 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007158 nfsd_print_count(clp, count, "locked files");
7159 return count;
7160}
7161
Jeff Layton016200c2014-07-30 08:27:21 -04007162u64
Jeff Layton285abde2014-07-30 08:27:24 -04007163nfsd_inject_print_locks(void)
Jeff Layton016200c2014-07-30 08:27:21 -04007164{
7165 struct nfs4_client *clp;
7166 u64 count = 0;
7167 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7168 nfsd_net_id);
7169
7170 if (!nfsd_netns_ready(nn))
7171 return 0;
7172
7173 spin_lock(&nn->client_lock);
7174 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7175 count += nfsd_print_client_locks(clp);
7176 spin_unlock(&nn->client_lock);
7177
7178 return count;
7179}
7180
7181static void
7182nfsd_reap_locks(struct list_head *reaplist)
7183{
7184 struct nfs4_client *clp;
7185 struct nfs4_ol_stateid *stp, *next;
7186
7187 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
7188 list_del_init(&stp->st_locks);
7189 clp = stp->st_stid.sc_client;
7190 nfs4_put_stid(&stp->st_stid);
7191 put_client(clp);
7192 }
7193}
7194
7195u64
Jeff Layton285abde2014-07-30 08:27:24 -04007196nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
Jeff Layton016200c2014-07-30 08:27:21 -04007197{
7198 unsigned int count = 0;
7199 struct nfs4_client *clp;
7200 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7201 nfsd_net_id);
7202 LIST_HEAD(reaplist);
7203
7204 if (!nfsd_netns_ready(nn))
7205 return count;
7206
7207 spin_lock(&nn->client_lock);
7208 clp = nfsd_find_client(addr, addr_size);
7209 if (clp)
7210 count = nfsd_collect_client_locks(clp, &reaplist, 0);
7211 spin_unlock(&nn->client_lock);
7212 nfsd_reap_locks(&reaplist);
7213 return count;
7214}
7215
7216u64
Jeff Layton285abde2014-07-30 08:27:24 -04007217nfsd_inject_forget_locks(u64 max)
Jeff Layton016200c2014-07-30 08:27:21 -04007218{
7219 u64 count = 0;
7220 struct nfs4_client *clp;
7221 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7222 nfsd_net_id);
7223 LIST_HEAD(reaplist);
7224
7225 if (!nfsd_netns_ready(nn))
7226 return count;
7227
7228 spin_lock(&nn->client_lock);
7229 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7230 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
7231 if (max != 0 && count >= max)
7232 break;
7233 }
7234 spin_unlock(&nn->client_lock);
7235 nfsd_reap_locks(&reaplist);
7236 return count;
7237}
7238
Jeff Layton82e05ef2014-07-30 08:27:22 -04007239static u64
7240nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
7241 struct list_head *collect,
7242 void (*func)(struct nfs4_openowner *))
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007243{
7244 struct nfs4_openowner *oop, *next;
Jeff Layton82e05ef2014-07-30 08:27:22 -04007245 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7246 nfsd_net_id);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007247 u64 count = 0;
7248
Jeff Layton82e05ef2014-07-30 08:27:22 -04007249 lockdep_assert_held(&nn->client_lock);
7250
7251 spin_lock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007252 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
Jeff Layton82e05ef2014-07-30 08:27:22 -04007253 if (func) {
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007254 func(oop);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007255 if (collect) {
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007256 atomic_inc(&clp->cl_rpc_users);
Jeff Layton82e05ef2014-07-30 08:27:22 -04007257 list_add(&oop->oo_perclient, collect);
7258 }
7259 }
7260 ++count;
7261 /*
7262 * Despite the fact that these functions deal with
7263 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007264 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton82e05ef2014-07-30 08:27:22 -04007265 * warning if we start to approach INT_MAX here.
7266 */
7267 WARN_ON_ONCE(count == (INT_MAX / 2));
7268 if (count == max)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007269 break;
7270 }
Jeff Layton82e05ef2014-07-30 08:27:22 -04007271 spin_unlock(&clp->cl_lock);
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007272
7273 return count;
7274}
7275
Jeff Layton82e05ef2014-07-30 08:27:22 -04007276static u64
7277nfsd_print_client_openowners(struct nfs4_client *clp)
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007278{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007279 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
7280
7281 nfsd_print_count(clp, count, "openowners");
7282 return count;
Bryan Schumaker4dbdbda2012-11-29 11:40:41 -05007283}
7284
Jeff Layton82e05ef2014-07-30 08:27:22 -04007285static u64
7286nfsd_collect_client_openowners(struct nfs4_client *clp,
7287 struct list_head *collect, u64 max)
Bryan Schumaker184c1842012-11-29 11:40:44 -05007288{
Jeff Layton82e05ef2014-07-30 08:27:22 -04007289 return nfsd_foreach_client_openowner(clp, max, collect,
7290 unhash_openowner_locked);
7291}
7292
7293u64
Jeff Layton285abde2014-07-30 08:27:24 -04007294nfsd_inject_print_openowners(void)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007295{
7296 struct nfs4_client *clp;
7297 u64 count = 0;
7298 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7299 nfsd_net_id);
7300
7301 if (!nfsd_netns_ready(nn))
7302 return 0;
7303
7304 spin_lock(&nn->client_lock);
7305 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7306 count += nfsd_print_client_openowners(clp);
7307 spin_unlock(&nn->client_lock);
7308
7309 return count;
7310}
7311
7312static void
7313nfsd_reap_openowners(struct list_head *reaplist)
7314{
7315 struct nfs4_client *clp;
7316 struct nfs4_openowner *oop, *next;
7317
7318 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
7319 list_del_init(&oop->oo_perclient);
7320 clp = oop->oo_owner.so_client;
7321 release_openowner(oop);
7322 put_client(clp);
7323 }
7324}
7325
7326u64
Jeff Layton285abde2014-07-30 08:27:24 -04007327nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
7328 size_t addr_size)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007329{
7330 unsigned int count = 0;
7331 struct nfs4_client *clp;
7332 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7333 nfsd_net_id);
7334 LIST_HEAD(reaplist);
7335
7336 if (!nfsd_netns_ready(nn))
7337 return count;
7338
7339 spin_lock(&nn->client_lock);
7340 clp = nfsd_find_client(addr, addr_size);
7341 if (clp)
7342 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
7343 spin_unlock(&nn->client_lock);
7344 nfsd_reap_openowners(&reaplist);
7345 return count;
7346}
7347
7348u64
Jeff Layton285abde2014-07-30 08:27:24 -04007349nfsd_inject_forget_openowners(u64 max)
Jeff Layton82e05ef2014-07-30 08:27:22 -04007350{
7351 u64 count = 0;
7352 struct nfs4_client *clp;
7353 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7354 nfsd_net_id);
7355 LIST_HEAD(reaplist);
7356
7357 if (!nfsd_netns_ready(nn))
7358 return count;
7359
7360 spin_lock(&nn->client_lock);
7361 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7362 count += nfsd_collect_client_openowners(clp, &reaplist,
7363 max - count);
7364 if (max != 0 && count >= max)
7365 break;
7366 }
7367 spin_unlock(&nn->client_lock);
7368 nfsd_reap_openowners(&reaplist);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007369 return count;
7370}
7371
Bryan Schumaker269de302012-11-29 11:40:42 -05007372static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
7373 struct list_head *victims)
7374{
7375 struct nfs4_delegation *dp, *next;
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007376 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7377 nfsd_net_id);
Bryan Schumaker269de302012-11-29 11:40:42 -05007378 u64 count = 0;
7379
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007380 lockdep_assert_held(&nn->client_lock);
7381
7382 spin_lock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007383 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
Jeff Laytondff13992014-07-08 14:02:49 -04007384 if (victims) {
7385 /*
7386 * It's not safe to mess with delegations that have a
7387 * non-zero dl_time. They might have already been broken
7388 * and could be processed by the laundromat outside of
7389 * the state_lock. Just leave them be.
7390 */
7391 if (dp->dl_time != 0)
7392 continue;
7393
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007394 atomic_inc(&clp->cl_rpc_users);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007395 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007396 list_add(&dp->dl_recall_lru, victims);
Jeff Laytondff13992014-07-08 14:02:49 -04007397 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007398 ++count;
7399 /*
7400 * Despite the fact that these functions deal with
7401 * 64-bit integers for "count", we must ensure that
J. Bruce Fields14ed14c2019-03-20 11:54:11 -04007402 * it doesn't blow up the clp->cl_rpc_users. Throw a
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007403 * warning if we start to approach INT_MAX here.
7404 */
7405 WARN_ON_ONCE(count == (INT_MAX / 2));
7406 if (count == max)
Bryan Schumaker269de302012-11-29 11:40:42 -05007407 break;
7408 }
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007409 spin_unlock(&state_lock);
Bryan Schumaker269de302012-11-29 11:40:42 -05007410 return count;
7411}
7412
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007413static u64
7414nfsd_print_client_delegations(struct nfs4_client *clp)
Bryan Schumaker269de302012-11-29 11:40:42 -05007415{
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007416 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
Bryan Schumaker184c1842012-11-29 11:40:44 -05007417
7418 nfsd_print_count(clp, count, "delegations");
7419 return count;
7420}
7421
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007422u64
Jeff Layton285abde2014-07-30 08:27:24 -04007423nfsd_inject_print_delegations(void)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007424{
7425 struct nfs4_client *clp;
7426 u64 count = 0;
7427 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7428 nfsd_net_id);
7429
7430 if (!nfsd_netns_ready(nn))
7431 return 0;
7432
7433 spin_lock(&nn->client_lock);
7434 list_for_each_entry(clp, &nn->client_lru, cl_lru)
7435 count += nfsd_print_client_delegations(clp);
7436 spin_unlock(&nn->client_lock);
7437
7438 return count;
7439}
7440
7441static void
7442nfsd_forget_delegations(struct list_head *reaplist)
7443{
7444 struct nfs4_client *clp;
7445 struct nfs4_delegation *dp, *next;
7446
7447 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7448 list_del_init(&dp->dl_recall_lru);
7449 clp = dp->dl_stid.sc_client;
7450 revoke_delegation(dp);
7451 put_client(clp);
7452 }
7453}
7454
7455u64
Jeff Layton285abde2014-07-30 08:27:24 -04007456nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7457 size_t addr_size)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007458{
7459 u64 count = 0;
7460 struct nfs4_client *clp;
7461 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7462 nfsd_net_id);
7463 LIST_HEAD(reaplist);
7464
7465 if (!nfsd_netns_ready(nn))
7466 return count;
7467
7468 spin_lock(&nn->client_lock);
7469 clp = nfsd_find_client(addr, addr_size);
7470 if (clp)
7471 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7472 spin_unlock(&nn->client_lock);
7473
7474 nfsd_forget_delegations(&reaplist);
7475 return count;
7476}
7477
7478u64
Jeff Layton285abde2014-07-30 08:27:24 -04007479nfsd_inject_forget_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007480{
7481 u64 count = 0;
7482 struct nfs4_client *clp;
7483 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7484 nfsd_net_id);
7485 LIST_HEAD(reaplist);
7486
7487 if (!nfsd_netns_ready(nn))
7488 return count;
7489
7490 spin_lock(&nn->client_lock);
7491 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7492 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7493 if (max != 0 && count >= max)
7494 break;
7495 }
7496 spin_unlock(&nn->client_lock);
7497 nfsd_forget_delegations(&reaplist);
7498 return count;
7499}
7500
7501static void
7502nfsd_recall_delegations(struct list_head *reaplist)
7503{
7504 struct nfs4_client *clp;
7505 struct nfs4_delegation *dp, *next;
7506
7507 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
7508 list_del_init(&dp->dl_recall_lru);
7509 clp = dp->dl_stid.sc_client;
7510 /*
7511 * We skipped all entries that had a zero dl_time before,
7512 * so we can now reset the dl_time back to 0. If a delegation
7513 * break comes in now, then it won't make any difference since
7514 * we're recalling it either way.
7515 */
7516 spin_lock(&state_lock);
7517 dp->dl_time = 0;
7518 spin_unlock(&state_lock);
7519 nfsd_break_one_deleg(dp);
7520 put_client(clp);
7521 }
7522}
7523
7524u64
Jeff Layton285abde2014-07-30 08:27:24 -04007525nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007526 size_t addr_size)
7527{
7528 u64 count = 0;
7529 struct nfs4_client *clp;
7530 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7531 nfsd_net_id);
7532 LIST_HEAD(reaplist);
7533
7534 if (!nfsd_netns_ready(nn))
7535 return count;
7536
7537 spin_lock(&nn->client_lock);
7538 clp = nfsd_find_client(addr, addr_size);
7539 if (clp)
7540 count = nfsd_find_all_delegations(clp, 0, &reaplist);
7541 spin_unlock(&nn->client_lock);
7542
7543 nfsd_recall_delegations(&reaplist);
7544 return count;
7545}
7546
7547u64
Jeff Layton285abde2014-07-30 08:27:24 -04007548nfsd_inject_recall_delegations(u64 max)
Jeff Layton98d5c7c2014-07-30 08:27:23 -04007549{
7550 u64 count = 0;
7551 struct nfs4_client *clp, *next;
7552 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7553 nfsd_net_id);
7554 LIST_HEAD(reaplist);
7555
7556 if (!nfsd_netns_ready(nn))
7557 return count;
7558
7559 spin_lock(&nn->client_lock);
7560 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
7561 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
7562 if (max != 0 && ++count >= max)
7563 break;
7564 }
7565 spin_unlock(&nn->client_lock);
7566 nfsd_recall_delegations(&reaplist);
7567 return count;
7568}
Bryan Schumaker65178db2011-11-01 13:35:21 -04007569#endif /* CONFIG_NFSD_FAULT_INJECTION */
7570
Meelap Shahc2f1a552007-07-17 04:04:39 -07007571/*
7572 * Since the lifetime of a delegation isn't limited to that of an open, a
7573 * client may quite reasonably hang on to a delegation as long as it has
7574 * the inode cached. This becomes an obvious problem the first time a
7575 * client's inode cache approaches the size of the server's total memory.
7576 *
7577 * For now we avoid this problem by imposing a hard limit on the number
7578 * of delegations, which varies according to the server's memory size.
7579 */
7580static void
7581set_max_delegations(void)
7582{
7583 /*
7584 * Allow at most 4 delegations per megabyte of RAM. Quick
7585 * estimates suggest that in the worst case (where every delegation
7586 * is for a different inode), a delegation could take about 1.5K,
7587 * giving a worst case usage of about 6% of memory.
7588 */
7589 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7590}
7591
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007592static int nfs4_state_create_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007593{
7594 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7595 int i;
7596
Kees Cook6da2ec52018-06-12 13:55:00 -07007597 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7598 sizeof(struct list_head),
7599 GFP_KERNEL);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007600 if (!nn->conf_id_hashtbl)
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007601 goto err;
Kees Cook6da2ec52018-06-12 13:55:00 -07007602 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7603 sizeof(struct list_head),
7604 GFP_KERNEL);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007605 if (!nn->unconf_id_hashtbl)
7606 goto err_unconf_id;
Kees Cook6da2ec52018-06-12 13:55:00 -07007607 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7608 sizeof(struct list_head),
7609 GFP_KERNEL);
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007610 if (!nn->sessionid_hashtbl)
7611 goto err_sessionid;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007612
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007613 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007614 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007615 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007616 }
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007617 for (i = 0; i < SESSION_HASH_SIZE; i++)
7618 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007619 nn->conf_name_tree = RB_ROOT;
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007620 nn->unconf_name_tree = RB_ROOT;
Vasily Averin81833de2017-11-13 07:25:40 +03007621 nn->boot_time = get_seconds();
7622 nn->grace_ended = false;
7623 nn->nfsd4_manager.block_opens = true;
7624 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
Stanislav Kinsbursky5ed58bb2012-11-14 18:21:56 +03007625 INIT_LIST_HEAD(&nn->client_lru);
Stanislav Kinsbursky73758fed2012-11-14 18:22:01 +03007626 INIT_LIST_HEAD(&nn->close_lru);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007627 INIT_LIST_HEAD(&nn->del_recall_lru);
Stanislav Kinsburskyc9a49622012-11-26 15:21:58 +03007628 spin_lock_init(&nn->client_lock);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04007629 spin_lock_init(&nn->s2s_cp_lock);
7630 idr_init(&nn->s2s_cp_stateids);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007631
Jeff Layton0cc11a62016-10-20 09:34:31 -04007632 spin_lock_init(&nn->blocked_locks_lock);
7633 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7634
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007635 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007636 get_net(net);
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007637
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007638 return 0;
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007639
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007640err_sessionid:
Stanislav Kinsbursky9b531132012-11-14 18:21:41 +03007641 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007642err_unconf_id:
7643 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky382a62e2012-11-14 18:21:26 +03007644err:
7645 return -ENOMEM;
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007646}
7647
7648static void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007649nfs4_state_destroy_net(struct net *net)
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007650{
7651 int i;
7652 struct nfs4_client *clp = NULL;
7653 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7654
7655 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7656 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7657 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7658 destroy_client(clp);
7659 }
7660 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007661
Jeff Layton68ef3bc2018-03-16 11:32:02 -04007662 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7663
Kinglong Mee2b905632014-03-26 22:09:30 +08007664 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7665 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7666 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7667 destroy_client(clp);
7668 }
Stanislav Kinsburskya99454a2012-11-14 18:21:36 +03007669 }
7670
Stanislav Kinsbursky1872de02012-11-14 18:21:51 +03007671 kfree(nn->sessionid_hashtbl);
Stanislav Kinsbursky0a7ec372012-11-14 18:21:31 +03007672 kfree(nn->unconf_id_hashtbl);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007673 kfree(nn->conf_id_hashtbl);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007674 put_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007675}
7676
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007677int
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007678nfs4_state_start_net(struct net *net)
NeilBrownac4d8ff22005-06-23 22:03:30 -07007679{
Stanislav Kinsbursky5e1533c2012-07-25 16:56:58 +04007680 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007681 int ret;
7682
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007683 ret = nfs4_state_create_net(net);
Stanislav Kinsbursky8daae4d2012-11-14 18:21:21 +03007684 if (ret)
7685 return ret;
Jeff Laytond4318ac2014-09-12 16:40:21 -04007686 locks_start_grace(net, &nn->nfsd4_manager);
7687 nfsd4_client_tracking_init(net);
Scott Mayhew362063a2019-03-26 18:06:28 -04007688 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7689 goto skip_grace;
Vasily Averin7e981a82017-11-06 16:46:04 +03007690 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n",
7691 nn->nfsd4_grace, net->ns.inum);
Stanislav Kinsbursky5284b442012-11-27 14:11:49 +03007692 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007693 return 0;
Scott Mayhew362063a2019-03-26 18:06:28 -04007694
7695skip_grace:
7696 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7697 net->ns.inum);
7698 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7699 nfsd4_end_grace(nn);
7700 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007701}
7702
7703/* initialization to perform when the nfsd service is started: */
7704
7705int
7706nfs4_state_start(void)
7707{
7708 int ret;
7709
Jeff Layton51a54562015-08-20 07:17:01 -04007710 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
Jeff Laytona6d6b782012-03-05 11:42:36 -05007711 if (laundry_wq == NULL) {
7712 ret = -ENOMEM;
Chuck Levera26dd642018-08-16 12:06:09 -04007713 goto out;
Jeff Laytona6d6b782012-03-05 11:42:36 -05007714 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007715 ret = nfsd4_create_callback_queue();
7716 if (ret)
7717 goto out_free_laundry;
Stanislav Kinsbursky09121282012-11-14 18:22:17 +03007718
Meelap Shahc2f1a552007-07-17 04:04:39 -07007719 set_max_delegations();
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007720 return 0;
Stanislav Kinsburskyd85ed442012-11-26 15:22:13 +03007721
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007722out_free_laundry:
7723 destroy_workqueue(laundry_wq);
Chuck Levera26dd642018-08-16 12:06:09 -04007724out:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05007725 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007726}
7727
Stanislav Kinsburskyf252bc62012-11-26 15:22:18 +03007728void
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007729nfs4_state_shutdown_net(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007730{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007731 struct nfs4_delegation *dp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732 struct list_head *pos, *next, reaplist;
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007733 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007735 cancel_delayed_work_sync(&nn->laundromat_work);
7736 locks_end_grace(&nn->nfsd4_manager);
Jeff Laytonac55fdc2012-11-12 15:00:56 -05007737
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738 INIT_LIST_HEAD(&reaplist);
Benny Halevycdc975052014-05-30 09:09:30 -04007739 spin_lock(&state_lock);
J. Bruce Fieldse8c69d12013-03-21 15:19:33 -04007740 list_for_each_safe(pos, next, &nn->del_recall_lru) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007741 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton3fcbbd22015-08-24 12:41:48 -04007742 WARN_ON(!unhash_delegation_locked(dp));
Jeff Layton42690672014-07-25 07:34:20 -04007743 list_add(&dp->dl_recall_lru, &reaplist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007744 }
Benny Halevycdc975052014-05-30 09:09:30 -04007745 spin_unlock(&state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007746 list_for_each_safe(pos, next, &reaplist) {
7747 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
Jeff Layton42690672014-07-25 07:34:20 -04007748 list_del_init(&dp->dl_recall_lru);
J. Bruce Fields0af6e692018-02-21 15:11:03 -05007749 destroy_unhashed_deleg(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750 }
7751
Stanislav Kinsbursky3320fef192012-11-14 18:22:07 +03007752 nfsd4_client_tracking_exit(net);
Stanislav Kinsbursky4dce0ac2012-11-26 15:22:08 +03007753 nfs4_state_destroy_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754}
7755
7756void
7757nfs4_state_shutdown(void)
7758{
NeilBrown5e8d5c22006-04-10 22:55:37 -07007759 destroy_workqueue(laundry_wq);
J. Bruce Fieldsc3935e32010-06-04 16:42:08 -04007760 nfsd4_destroy_callback_queue();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761}
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007762
7763static void
7764get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7765{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007766 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
7767 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007768}
7769
7770static void
7771put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7772{
Tigran Mkrtchyan37c593c2012-02-13 22:55:32 +01007773 if (cstate->minorversion) {
7774 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7775 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
7776 }
7777}
7778
7779void
7780clear_current_stateid(struct nfsd4_compound_state *cstate)
7781{
7782 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007783}
7784
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007785/*
7786 * functions to set current state id
7787 */
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007788void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007789nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7790 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007791{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007792 put_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007793}
7794
7795void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007796nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7797 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007798{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007799 put_stateid(cstate, &u->open.op_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007800}
7801
7802void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007803nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7804 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007805{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007806 put_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007807}
7808
7809void
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007810nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7811 union nfsd4_op_u *u)
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007812{
Christoph Hellwigb60e9852017-05-08 20:03:15 +02007813 put_stateid(cstate, &u->lock.lk_resp_stateid);
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007814}
7815
7816/*
7817 * functions to consume current state id
7818 */
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007819
7820void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007821nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7822 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007823{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007824 get_stateid(cstate, &u->open_downgrade.od_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007825}
7826
7827void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007828nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7829 union nfsd4_op_u *u)
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007830{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007831 get_stateid(cstate, &u->delegreturn.dr_stateid);
Tigran Mkrtchyan9428fe12012-02-13 22:55:31 +01007832}
7833
7834void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007835nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7836 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007837{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007838 get_stateid(cstate, &u->free_stateid.fr_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007839}
7840
7841void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007842nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7843 union nfsd4_op_u *u)
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007844{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007845 get_stateid(cstate, &u->setattr.sa_stateid);
Tigran Mkrtchyan1e97b512012-02-13 22:55:30 +01007846}
7847
Tigran Mkrtchyan62cd4a52012-02-13 22:55:25 +01007848void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007849nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7850 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007851{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007852 get_stateid(cstate, &u->close.cl_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007853}
7854
7855void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007856nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7857 union nfsd4_op_u *u)
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007858{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007859 get_stateid(cstate, &u->locku.lu_stateid);
Tigran Mkrtchyan8b704842012-02-13 22:55:24 +01007860}
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007861
7862void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007863nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7864 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007865{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007866 get_stateid(cstate, &u->read.rd_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007867}
7868
7869void
Christoph Hellwig57832e72017-05-08 20:37:33 +02007870nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7871 union nfsd4_op_u *u)
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007872{
Christoph Hellwig57832e72017-05-08 20:37:33 +02007873 get_stateid(cstate, &u->write.wr_stateid);
Tigran Mkrtchyan30813e22012-02-13 22:55:26 +01007874}